Skip to content
Snippets Groups Projects
Teachers.php 2.64 KiB
Newer Older
<?php defined('SYSPATH') || die('No direct script access.');
xamgore's avatar
xamgore committed
class Controller_Handler_Teachers extends Controller_Handler
{
xamgore's avatar
xamgore committed
    public function before() {
        parent::before();

        $this->user->checkAccess(User::RIGHTS_ADMIN | User::RIGHTS_DEAN);
xamgore's avatar
xamgore committed
    }

    protected function action_createTeacher() {
        $teacher = Model_Teacher::make()
            ->jobPosition($_POST['jobPositionID'])
            ->department($_POST['departmentID'])
            ->firstName($_POST['firstName'])
            ->secondName($_POST['secondName'])
            ->lastName($_POST['lastName'])
            ->create();

        $res = ['ID' => $teacher->ID, 'Code' => $teacher->ActivationCode];
        $this->request->body(json_encode($res));
    }

    public function action_search() {
        $facultyID = $this->post['FacultyID']; //?: $this->user->Faculty->ID;
xamgore's avatar
xamgore committed
        $departmentID = $this->post['DepartmentID'];
        $name = $this->post['Name'];

        $p = preg_split('/\s+/', $name);
        $list = Model_Teachers::search($p, $facultyID, $departmentID);
xamgore's avatar
xamgore committed
        $this->response->body(json_encode($list));
    }

    public function action_upload() {
        $faculty = $this->user->Faculty;
        if ($this->user->isAdmin() && $_POST['facultyID'])
            $faculty = Model_Faculty::with($_POST['facultyID']);

        $departments = Arr::groupByUniqueKey('Name', $faculty->getDepartments());
        
        foreach ($_POST['departments'] as $dep) {
            if ($dep['name'] === 'Без кафедры')
                $dep['name'] = '';
            
            $dep['ID'] = !isset($departments[$dep['name']])
                ? self::createDepartment($dep['name'], $faculty)
                : $departments[$dep['name']]['ID'];
            
            foreach ($dep['people'] as $name) {

                // Проверим, что входная строка не пуста.
                $name = trim($name);
                if (empty($name))
                    continue;

                list($lastName, $firstName, $secondName) = Text::parseFullName($name);

                Model_Teacher::make()
                    ->department($dep['ID'])
                    ->firstName($firstName)
                    ->lastName($lastName)
                    ->secondName($secondName)
                    ->create();
            }
        }
    }
    
    private static function createDepartment($name, Model_Faculty $faculty) {
        $sql = 'SELECT * FROM Department_Create(:name, :faculty) AS "id"';
        return DB::query(Database::SELECT, $sql)
            ->param(':name', $name)
            ->param(':faculty', $faculty->ID)
            ->execute()->get('id');
    }