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

class Controller_Handler_Discipline extends Controller_Handler_Api
{
    /** @var  Model_User_Teacher */
    protected $user;

    # /handler/discipline/
    public function action_index() {
        $id = (int) $this->request->post('id');
        return Model_Discipline::load($id);
    }

    # /handler/discipline/create
    public function action_create() {
        if (!Model_System::loadConfig()->Functional->DisciplineCreation)
            throw HTTP_Exception::factory(404);

        $this->user->checkAccess(User::RIGHTS_TEACHER);

        $discipline = Model_Discipline::make()
            ->author  ($this->user->TeacherID)
            ->semester($this->post['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::of($discipline)->addModuleBonus($this->user->TeacherID);

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

    public function action_getCompounds() {
        $ret = '<option value="0"> -- Нет -- </option>';
        $compounds = Model_Grades::getCompoundDisciplinesForGrade($this->post['gradeID']);
        foreach($compounds as $cmp){
            $cmpID = $cmp['ID'];
            $cmpName = $cmp['Name'];
            $ret .= "<option value = $cmpID> $cmpName </option>";
        }
        return $ret;
    }

    # /handler/discipline/update
    public function action_update() {
        $this->user->checkAccess(User::RIGHTS_TEACHER);

        $id = (int) $this->request->post('id');

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

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

    # /handler/discipline/clear
    public function action_clear() {
        # todo: implement me
        $this->user->checkAccess(User::RIGHTS_ADMIN);

        $id = (int) $this->request->post('id');
//        Model_Discipline::load($id)->clear()->delete();
    }

    # /handler/discipline/delete
    public function action_delete() {
        if (!Model_System::loadConfig()->Functional->DisciplineCreation)
            throw HTTP_Exception::factory(404);

        $this->user->checkAccess(User::RIGHTS_TEACHER);

        $id = (int) $this->request->post('id');
        $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() {
        if (!Model_System::loadConfig()->Functional->DisciplineCreation)
            throw HTTP_Exception::factory(404);

        $this->user->checkAccess(User::RIGHTS_TEACHER);

        $id = (int) $this->request->post('id');
        $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() {
        if (!Model_System::loadConfig()->Functional->DisciplineCreation)
            throw HTTP_Exception::factory(404);

        $this->user->checkAccess(User::RIGHTS_TEACHER);

        $id = (int) $this->request->post('id');
        $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() {
        if (!Model_System::loadConfig()->Functional->DisciplineCreation)
            throw HTTP_Exception::factory(404);

        $this->user->checkAccess(User::RIGHTS_TEACHER);

        $id = (int) $this->request->post('id');
        $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);
    }
}