Skip to content
Snippets Groups Projects
Discipline.php 8.03 KiB
Newer Older
<?php defined('SYSPATH') || die('No direct script access.');
xamgore's avatar
xamgore committed

class Controller_Handler_Discipline extends Controller_Handler_Api
xamgore's avatar
xamgore committed
{
    /** @var  Model_User_Teacher */
    protected $user;
xamgore's avatar
xamgore committed

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

    # /handler/discipline/create
xamgore's avatar
xamgore committed
    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();
xamgore's avatar
xamgore committed

        if ($this->post['bonus'] == "true")
            Model_Map::of($discipline)->addModuleBonus($this->user->TeacherID);
xamgore's avatar
xamgore committed

        return ['ID' => $discipline->ID];
xamgore's avatar
xamgore committed
    }
xamgore's avatar
xamgore committed

    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
xamgore's avatar
xamgore committed
    public function action_update() {
        $this->user->checkAccess(User::RIGHTS_TEACHER);
        $id = (int) $this->request->post('id');
xamgore's avatar
xamgore committed

        if ($id <= 0)
            throw new InvalidArgumentException(Error::ID_IS_INCORRECT);
xamgore's avatar
xamgore committed

        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
xamgore's avatar
xamgore committed
    public function action_delete() {
        if (!Model_System::loadConfig()->Functional->DisciplineCreation)
            throw HTTP_Exception::factory(404);

        $discipline = $this->loadDisciplineAndAuthorCheck();
xamgore's avatar
xamgore committed
        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();
xamgore's avatar
xamgore committed
    }

    public function action_delegate() {
//        if (!Model_System::loadConfig()->Functional->DisciplineCreation)
//            throw HTTP_Exception::factory(404);

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

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

    public function action_unbind() {
        $discipline = $this->loadDisciplineAndAuthorCheck();
        $teacherID = (int) $this->request->post('teacherID');
        $teacher = Model_Teacher::with($teacherID);
        $discipline->unbind($teacher);
    }
    public function action_copyStructure() {
        $discipline = $this->loadDisciplineAndAuthorCheck();
        $teacherID = (int) $this->request->post('teacherID');
        $disciplineFromID = (int) $this->request->post('disciplineFromID');
        $discipline->copyDisciplineStructureFrom($teacherID, $disciplineFromID);
    }

    public function action_copyMap() {
        $discipline = $this->loadDisciplineAndAuthorCheck();
        $disciplineFromID = (int) $this->request->post('disciplineFromID');
        $disciplineFrom = Model_Discipline::load($disciplineFromID);
        if ($discipline->Type === 'exam' && ($disciplineFrom->Type === 'credit' || $disciplineFrom->Type === 'grading_credit')) {
            return array(
                'return_code' => 2,
                'success' => false,
            );
        }
        $result = $discipline->copyDisciplineMapFrom($discipline->ID, $disciplineFromID);
        return array(
            'return_code' => $result,
            'success' => $result === 0,
        );
    }

    public function action_clearModules() {
        $discipline = $this->loadDisciplineAndAuthorCheck();
        $teacherID = $this->user->TeacherID;
        $code = $discipline->clearModules($teacherID);
        $code = $code.'';
        return json_encode($code);
    }

    public function action_hasModules() {
        $discipline = $this->loadDisciplineAndAuthorCheck();
        $code = $discipline->hasModules();
        $code = $code.'';
        return json_encode($code);
    }

    public function action_setInactive() {
        $this->user->checkAccess(User::RIGHTS_TEACHER);
        $id = (int)$this->request->post('id');
        $discipline = Model_Discipline::load($id);
        if (($discipline->AuthorID != $this->user->TeacherID) && !($this->user->isDean() || $this->user->isAdmin()))
            throw new LogicException(Error::ACCESS_DENIED);
        $code = $discipline->setInactive();
        $code = $code.'';
        return json_encode($code);
    }

    public function action_setActive() {
        $this->user->checkAccess(User::RIGHTS_TEACHER);
        $id = (int)$this->request->post('id');
        $discipline = Model_Discipline::load($id);
        if (($discipline->AuthorID != $this->user->TeacherID) && !($this->user->isDean() || $this->user->isAdmin()))
            throw new LogicException(Error::ACCESS_DENIED);
        $code = $discipline->setActive();
        $code = $code.'';
    public function action_getSimilarDisciplines() {
        $discipline = $this->loadDisciplineAndAuthorCheck();
        $teacherID = (int) $this->user->TeacherID;
        $teacher = Model_Teacher::with($teacherID);
        $sameSemester = $this->request->post('sameSemester') === 'true';
        $sameSubject = $this->request->post('sameSubject') === 'true';
        $subjectID = $discipline->SubjectID;
        if (!$sameSubject) {
            $subjectID = null;
        }
        $semesterID = $discipline->SemesterID;
        if (!$sameSemester) {
            $semesterID = null;
        }
        return json_encode($teacher->getSimilarDisciplines($subjectID, $semesterID));
    public function action_getDisciplinesForGroup() {
        $groupID = $this->request->post('groupID');
        $semesterID = User::instance()->SemesterID;
        //$students = Model_Students::ofGroup($groupID, $semesterID);

        // TODO: refactor after new groups implemented
        $disciplines = Model_Group::with($groupID)->getDisciplinesForExport(false, $semesterID);

        return json_encode($disciplines);
    }

    public function action_setDisciplineErrorForGroup() {
        $groupID = $this->request->post('groupID');
        $disciplineID = $this->request->post('disciplineID');
        $semesterID = User::instance()->SemesterID;
        $error = $this->request->post('error');

        $res = Model_Logs::setLastFormExportError($disciplineID, $groupID, $semesterID, $error);
        return json_encode($res);
    }

    /**
     * @return Model_Discipline
     */
    private function loadDisciplineAndAuthorCheck() {
        $this->user->checkAccess(User::RIGHTS_TEACHER);
        $id = (int)$this->request->post('id');
        $discipline = Model_Discipline::load($id);
        if (($discipline->IsInactive) || ($discipline->AuthorID != $this->user->TeacherID) && !($this->user->isDean() || $this->user->isAdmin()))
            throw new LogicException(Error::ACCESS_DENIED);