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

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

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

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

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

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

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

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

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

    # DELETE: /handler/discipline/delete/723
xamgore's avatar
xamgore committed
    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)
RomanSteinberg's avatar
RomanSteinberg committed
            throw new LogicException(Error::ACCESS_DENIED);
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() {
        $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);
    }
xamgore's avatar
xamgore committed
}