Skip to content
Snippets Groups Projects
Teacher.php 2.92 KiB
Newer Older
<?php

class Controller_Api_V0_Teacher extends Controller_Handler_Api
{
    private function getDepIDByName($name) {
        try {
            $id = Model_Faculties::getDepartmentIdByName($name);
        }
        catch (InvalidArgumentException $e) {
            return NULL;
        }
        return $id;
    }

    private function createTeacher($data) {
        $teacher = Model_Teacher::make()
            ->jobPosition($data->jobPositionID)
            ->department($data->departmentID)
            ->firstName($data->firstName)
            ->secondName($data->secondName)
            ->lastName($data->lastName)
            ->create();
        return ['ID' => $teacher->ID, 'Code' => $teacher->ActivationCode];
    }

    private function normalizeTeacherData($data) {
        if ( $data->departmentID === NULL && $data->departmentName !== NULL)
            $data->departmentID = $this->getDepIDByName($data->departmentName);

        if ( $data->jobPositionID === NULL && $data->jobPositionName !== NULL )
            $data->jobPositionID = Model_Faculties::getJobPositionIdByName($data->jobPositionName);

        return $data;
    }

Artem Konenko's avatar
Artem Konenko committed
    /**
     * @api {put} api/v0/teacher Add new teacher(s)
     * @apiName Add new teacher
     * @apiGroup Teacher
     * @apiVersion 0.1.0
     * @apiParam {String} token Api key
     * @apiParam {String} [batch] Flag about massive addition
     * @apiParam {String} firstName
     * @apiParam {String} secondName
     * @apiParam {String} lastName
     * @apiParam {String} departmentName
     * @apiParam {String} [departmentID]
     * @apiParam {String} jobPositionName
     * @apiParam {String} [jobPositionID]
     * @apiParam {String} status
     */
    public function action_put_index() {
        try {
            if ( $this->request->query('batch') !== NULL ) {
                $data = json_decode($this->request->body());

                $res = [];
                foreach ($data as $item) {
                    $data = $this->normalizeTeacherData($item);
                    $res[] = $this->createTeacher($data);
                }
            } else {
                $data = [ 'firstName' => $this->request->query('firstName')
                    , 'secondName' => $this->request->query('secondName')
                    , 'lastName' => $this->request->query('lastName')
                    , 'departmentName' => $this->request->query('departmentName')
                    , 'departmentID' => $this->request->query('departmentID')
                    , 'jobPositionName' => $this->request->query('jobPositionName')
                    , 'jobPositionID' => $this->request->query('jobPositionID')
                    , 'status' => $this->request->query('status')];

                $data = $this->normalizeTeacherData((object)$data);
                $res[] = $this->createTeacher($data);
            }
        } catch (Exception $e) {
            $this->badRequestError($e->getMessage());
        }

        return $res;
    }

}