<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Api_Discipline extends Controller_Handler
{
    /** @var  Model_User_Teacher */
    protected $user;

    # GET: /handler/discipline/723
    public function action_index() {
        $id = $this->request->param('id', 0);
        return Model_Discipline::load($id);
    }

    # POST: /handler/discipline/create
    public function action_create() {
        $role = $this->user->RoleMark;
        Access::instance($role)->checkAccess(Access::RIGHTS_TEACHER);

        $discipline = Model_Discipline::make()
            ->author  ($this->user->TeacherID)
            ->semester($this->user->SemesterID)
            ->faculty ($this->post['facultyID'])
            ->subject ($this->post['subjectID'])
            ->grade   ($this->post['gradeID'])
            ->lectures($this->post['lectures'])
            ->practice($this->post['practice'])
            ->labs    ($this->post['labs'])
            ->type    ($this->post['type'])
            ->create();

        if ($this->post['bonus'] == "true")
            Model_Map::AddModuleBonus($this->user->TeacherID, $discipline->ID);

        return ['ID' => $discipline->ID];
    }

    # PUT: /handler/discipline/update/723
    public function action_update() {
        $role = $this->user->RoleMark;
        Access::instance($role)->checkAccess(Access::RIGHTS_TEACHER);
        
        $id = $this->request->param('id', 0);

        if ($id <= 0)
            throw new InvalidArgumentException(Error::ID_IS_INCORRECT);

        throw new LogicException('Not implemented yet');
    }

    # DELETE: /handler/discipline/delete/723
    public function action_delete() {
        $role = $this->user->RoleMark;
        Access::instance($role)->checkAccess(Access::RIGHTS_TEACHER);
        
        $id = (int) $this->request->param('id', 0);
        $discipline = Model_Discipline::load($id);

        // delete only if discipline cleared (doesn't exists related rating's records)
        if ($discipline->AuthorID != $this->user->TeacherID)
            throw new LogicException(Error::ACCESS_DENIED);
        if ($discipline->IsLocked)
            throw new LogicException(Error::DISCIPLINE_IS_LOCKED);
        if (Model_Rating::count($discipline) > 0)
            throw new LogicException(Error::DISCIPLINE_IS_LOCKED);

        $discipline->delete();
    }

    public function action_delegate() {
        $role = $this->user->RoleMark;
        Access::instance($role)->checkAccess(Access::RIGHTS_TEACHER);
        
        $id = (int) $this->request->param('id', 0);
        $discipline = Model_Discipline::load($id);
        if ($discipline->AuthorID != $this->user->TeacherID)
            throw new LogicException(Error::ACCESS_DENIED);

        $teacherID = (int) $this->request->post('teacherID');
        $teacher = Model_Teacher::with($teacherID);
        $discipline->delegateTo($teacher);
    }

    public function action_bind() {
        $role = $this->user->RoleMark;
        Access::instance($role)->checkAccess(Access::RIGHTS_TEACHER);
        
        $id = (int) $this->request->param('id', 0);
        $discipline = Model_Discipline::load($id);
        if ($discipline->AuthorID != $this->user->TeacherID)
            throw new LogicException(Error::ACCESS_DENIED);

        $teacherID = (int) $this->request->post('teacherID');
        $teacher = Model_Teacher::with($teacherID);
        $discipline->bind($teacher);
    }

    public function action_unbind() {
        $role = $this->user->RoleMark;
        Access::instance($role)->checkAccess(Access::RIGHTS_TEACHER);
        
        $id = (int) $this->request->param('id', 0);
        $discipline = Model_Discipline::load($id);
        if ($discipline->AuthorID != $this->user->TeacherID)
            throw new LogicException(Error::ACCESS_DENIED);

        $teacherID = (int) $this->request->post('teacherID');
        $teacher = Model_Teacher::with($teacherID);
        $discipline->unbind($teacher);
    }
}