From 0b645dc7dc4e798be0ff9ede5e3e6073b92758c8 Mon Sep 17 00:00:00 2001 From: xamgore <xamgore@ya.ru> Date: Wed, 10 Jun 2015 18:29:04 +0300 Subject: [PATCH] Renamed listOutput to listStudent + Code refactoring in the AdmStudent controller --- .../Controller/Handler/AdmStudents.php | 218 ++++++++---------- .../application/classes/Model/Students.php | 15 +- .../{listOutput.twig => listStudents.twig} | 2 +- 3 files changed, 112 insertions(+), 123 deletions(-) rename ~dev_rating/application/views/handler/{listOutput.twig => listStudents.twig} (94%) diff --git a/~dev_rating/application/classes/Controller/Handler/AdmStudents.php b/~dev_rating/application/classes/Controller/Handler/AdmStudents.php index ec074a12c..057136e53 100644 --- a/~dev_rating/application/classes/Controller/Handler/AdmStudents.php +++ b/~dev_rating/application/classes/Controller/Handler/AdmStudents.php @@ -1,147 +1,130 @@ <?php defined('SYSPATH') or die('No direct script access.'); -abstract class SortingOptions { //extends SplEnum { // todo: think about usage & file system +abstract class SortingOptions +{ //extends SplEnum { // todo: think about usage & file system //const __default = self::Name; - const Name = 0; + const Name = 0; const Grade = 1; const Group = 2; } class Controller_Handler_AdmStudents extends Controller_Handler { - const STUDENTS_LIST = '/handler/listOutput'; - - public function before() { - $this->setAccessLevel(self::ACCESS_USER); - parent::before(); + const STUDENTS_LIST = '/handler/listStudents'; + + public function before() { + $this->setAccessLevel(self::ACCESS_USER); + parent::before(); + } + + public function action_createStudent() { + $response['success'] = false; + $this->post->rule('firstName', 'not_empty') + ->rule('lastName', 'not_empty') + ->rule('gradeNum', 'not_empty') + ->rule('gradeNum', 'digit') + ->rule('groupNum', 'not_empty') + ->rule('groupNum', 'digit') + ->rule('facultyID', 'not_empty') + ->rule('facultyID', 'digit'); + + if ($this->post['gradeNum'] == 0) { + $this->post->error('gradeNum', 'not_empty'); + $response['success'] = false; } - public function action_createStudent() - { + if ($this->post['groupNum'] == 0) { + $this->post->error('groupNum', 'not_empty'); $response['success'] = false; - $this->post - ->rule('firstName', 'not_empty') - // ->rule('firstName', 'alpha_dash', array(':value', TRUE)) - // ->rule('secondName', 'not_empty') - // ->rule('secondName', 'alpha_dash', array(':value', TRUE)) - ->rule('lastName', 'not_empty') - // ->rule('lastName', 'alpha_dash', array(':value', TRUE)) - ->rule('gradeNum', 'not_empty') - ->rule('gradeNum', 'digit') - ->rule('groupNum', 'not_empty') - ->rule('groupNum', 'digit') - ->rule('facultyID', 'not_empty') - ->rule('facultyID', 'digit'); - if($this->post['gradeNum'] == 0) - { - $this->post->error('gradeNum', 'not_empty'); - $response['success'] = false; - } - if($this->post['groupNum'] == 0) - { - $this->post->error('groupNum', 'not_empty'); - $response['success'] = false; - } - if($this->post['facultyID'] == 0) - { - $this->post->error('facultyID', 'not_empty'); - $response['success'] = false; - } - if($this->post->check()) - { - $code = Account::instance() - ->createStudent( - $this->post['lastName'], - $this->post['firstName'], - $this->post['secondName'], - $this->post['gradeNum'], - $this->post['groupNum'], - $this->post['facultyID'] - ); - - if($code != -1) - { - $response['success'] = true; - $response['code'] = $code; - } - else { - $response['success'] = false; - } - } - else - { + } + + if ($this->post['facultyID'] == 0) { + $this->post->error('facultyID', 'not_empty'); + $response['success'] = false; + } + + if ($this->post->check()) { + $code = Account::instance() + ->createStudent( + $this->post['lastName'], $this->post['firstName'], $this->post['secondName'], + $this->post['gradeNum'], $this->post['groupNum'], $this->post['facultyID'] + ); + + if ($code != -1) { + $response['success'] = true; + $response['code'] = $code; + } else { $response['success'] = false; - $response['messages'] = $this->post->errors(); } - $this->response->body(json_encode($response)); + } else { + $response['success'] = false; + $response['messages'] = $this->post->errors(); } - - public function action_getGroups() - { - $facultyID = $this->post['facultyID']; - $gradeID = $this->post['gradeNum']; - if(($facultyID && $gradeID) != 0) - { - $groups = Model_Faculty::with($facultyID)->getGroups($gradeID); - $groupsHandled = array(); $i = $j = $id = 0; - foreach($groups as $row) - { - if($id != $row['SpecID']) - { - $i++; $j = 0; - $id = $row['SpecID']; - } - $j++; - if (is_null($row['SpecName'])) - $groupsHandled[$i]['SpecName'] = "<без специализации>"; - else - $groupsHandled[$i]['SpecName'] = $row['SpecName']; - $groupsHandled[$i]['Groups'][$j]['ID'] = $row['ID']; - $groupsHandled[$i]['Groups'][$j]['Num'] = $row['GroupNum']; + $this->response->body(json_encode($response)); + } + + public function action_getGroups() { + $facultyID = $this->post['facultyID']; + $gradeID = $this->post['gradeNum']; + if (($facultyID && $gradeID) != 0) { + $groups = Model_Faculty::with($facultyID)->getGroups($gradeID); + $groupsHandled = array(); + $i = $j = $id = 0; + foreach ($groups as $row) { + if ($id != $row['SpecID']) { + $i++; + $j = 0; + $id = $row['SpecID']; } - $this->response->body(json_encode($groupsHandled)); + $j++; + if (is_null($row['SpecName'])) { + $groupsHandled[$i]['SpecName'] = "<без специализации>"; + } else { + $groupsHandled[$i]['SpecName'] = $row['SpecName']; + } + $groupsHandled[$i]['Groups'][$j]['ID'] = $row['ID']; + $groupsHandled[$i]['Groups'][$j]['Num'] = $row['GroupNum']; } + $this->response->body(json_encode($groupsHandled)); } + } - public function action_getStudentsList($option = SortingOptions::Name) { - $twig = Twig::factory(self::STUDENTS_LIST); + public function action_getStudentsList($option = SortingOptions::Name) { + $twig = Twig::factory(self::STUDENTS_LIST); - $facultyID = (int) $this->post['facultyID']; - $gradeID = (int) $this->post['gradeID']; - $groupID = (int) $this->post['groupID']; + $facultyID = (int) $this->post['facultyID']; + $gradeID = (int) $this->post['gradeID']; + $groupID = (int) $this->post['groupID']; - if ($this->post->check()) { - $result = Model_Students::byFaculty($facultyID , $gradeID, $groupID); - $twig->List = $result; - } + if ($this->post->check()) { + $result = Model_Students::byFaculty($facultyID, $gradeID, $groupID); + $twig->List = $result; + } + $this->response->body($twig); + } + + public function action_getStudentsByStudyGroup() { + $groupID = $this->post['studyGroupID']; + + if ($groupID != 0) { + $twig = Twig::factory(self::STUDENTS_LIST); + $twig->List = Model_Students::byStudyGroup($groupID); $this->response->body($twig); } + } - public function action_getStudentsByStudyGroup() - { - $groupID = $this->post['studyGroupID']; - if($groupID != 0) - { - $twig = Twig::factory(self::STUDENTS_LIST); - $twig->List = Model_Students::byStudyGroup($groupID); - $this->response->body($twig); - } - } + public function action_getStudentsByFaculty() { + $facultyID = $this->post['facultyID']; - /** @deprecated */ - public function action_getStudentsByFaculty() - { - $facultyID = $this->post['facultyID']; - if($facultyID != 0) - { - $twig = Twig::factory(self::STUDENTS_LIST); - // (Grade, Group) = (0,0), to ignore (search all students) - $twig->List = Model_Students::byFaculty($facultyID, 0, 0); - $this->response->body($twig); - } + if ($facultyID != 0) { + $twig = Twig::factory(self::STUDENTS_LIST); + // (Grade, Group) = (0,0), to ignore (search all students) + $twig->List = Model_Students::byFaculty($facultyID); + $this->response->body($twig); } + } public function action_getStudentsByName() { $twig = Twig::factory(self::STUDENTS_LIST); @@ -161,8 +144,7 @@ class Controller_Handler_AdmStudents extends Controller_Handler public function action_giveLeave() { // fixme $id = (int) $this->get['id']; - Model_Student::load($id) - ->toAcademicLeave(); + Model_Student::load($id)->toAcademicLeave(); } public function action_stopLeave() { diff --git a/~dev_rating/application/classes/Model/Students.php b/~dev_rating/application/classes/Model/Students.php index 307302451..0b85d5220 100644 --- a/~dev_rating/application/classes/Model/Students.php +++ b/~dev_rating/application/classes/Model/Students.php @@ -27,10 +27,17 @@ class Model_Students extends Model return self::collect(self::ofGroup($groupID)); } - public static function byFaculty($facultyID, $gradeID, $groupID, $semesterID = null) { - $semesterID = $semesterID ? $semesterID : User::instance()->SemesterID;; - $sql = "CALL `GetStudentsByFaculty`('$facultyID', '$gradeID', $groupID, $semesterID); "; - $students = DB::query(Database::SELECT, $sql)->execute(); + public static function byFaculty($facultyID, $gradeID = null, $groupID = null, $semesterID = null) { + $semesterID = $semesterID ? $semesterID : User::instance()->SemesterID; + $sql = "CALL `GetStudentsByFaculty`(:faculty, :grade, :group, :semester); "; + $students = DB::query(Database::SELECT, $sql) + ->parameters([ + ':faculty' => (int) $facultyID, + ':grade' => (int) $gradeID, + ':group' => (int) $groupID, + ':semester' => (int) $semesterID, + ]) + ->execute(); return self::collect($students); } diff --git a/~dev_rating/application/views/handler/listOutput.twig b/~dev_rating/application/views/handler/listStudents.twig similarity index 94% rename from ~dev_rating/application/views/handler/listOutput.twig rename to ~dev_rating/application/views/handler/listStudents.twig index fae1d351e..26da8e174 100644 --- a/~dev_rating/application/views/handler/listOutput.twig +++ b/~dev_rating/application/views/handler/listStudents.twig @@ -12,7 +12,7 @@ <div class="search_item_info"> <div class="search_item_firstLine"> {% set name = row.LastName ~ ' ' ~ row.FirstName ~ ' ' ~ row.SecondName %} - {{ HTML.anchor('dean_office/students/' ~ row.AccountID, name|e)|raw }} + {{ HTML.anchor('students/' ~ row.AccountID, name|e)|raw }} </div> </div> <div class="search_item_actions"> -- GitLab