From 24a8fc23e35d169713455385c9c343c4adfd5d39 Mon Sep 17 00:00:00 2001
From: xamgore <xamgore@ya.ru>
Date: Wed, 24 Jun 2015 19:22:26 +0300
Subject: [PATCH] Removed Model_Students::collect() method

Field Model_Factory::ID came public, new method getStudents() added.
Renamed Model_Students::searchStudents() to search(); removed duplicate method NotAttendingDiscipline().
---
 .../Controller/Handler/AdmStudents.php        | 41 +++++++------------
 .../classes/Controller/Handler/Map.php        |  2 +-
 .../application/classes/Model/Faculty.php     | 38 ++++++++++++++---
 .../application/classes/Model/Students.php    | 41 +------------------
 4 files changed, 49 insertions(+), 73 deletions(-)

diff --git a/~dev_rating/application/classes/Controller/Handler/AdmStudents.php b/~dev_rating/application/classes/Controller/Handler/AdmStudents.php
index 4008b9183..24b603146 100644
--- a/~dev_rating/application/classes/Controller/Handler/AdmStudents.php
+++ b/~dev_rating/application/classes/Controller/Handler/AdmStudents.php
@@ -1,14 +1,5 @@
 <?php defined('SYSPATH') or die('No direct script access.');
 
-abstract class SortingOptions
-{ //extends SplEnum {  // todo: think about usage & file system
-    //const __default = self::Name;
-
-    const Name  = 0;
-    const Grade = 1;
-    const Group = 2;
-}
-
 class Controller_Handler_AdmStudents extends Controller_Handler
 {
     const STUDENTS_LIST = '/handler/listStudents';
@@ -90,17 +81,12 @@ class Controller_Handler_AdmStudents extends Controller_Handler
         }
     }
 
-    public function action_getStudentsList($option = SortingOptions::Name) {
-        $twig = Twig::factory(self::STUDENTS_LIST);
+    public function action_getStudentsList() {
+        $faculty = Model_Faculty::with($this->post['facultyID']);
 
-        $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;
-        }
+        $twig = Twig::factory(self::STUDENTS_LIST, [
+            'List' => $faculty->getStudents($this->post['gradeID'], $this->post['groupID'])
+        ]);
 
         $this->response->body($twig);
     }
@@ -109,21 +95,22 @@ class Controller_Handler_AdmStudents extends Controller_Handler
         $groupID = $this->post['studyGroupID'];
 
         if ($groupID != 0) {
+            # todo: links to dean / admin page
             $twig = Twig::factory(self::STUDENTS_LIST);
-            $twig->List = Model_Students::byStudyGroup($groupID);
+            $twig->List = Model_Students::ofGroup($groupID);
             $this->response->body($twig);
         }
     }
 
+    # todo: check, is it used?
     public function action_getStudentsByFaculty() {
-        $facultyID = $this->post['facultyID'];
+        $faculty = Model_Faculty::with($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);
-            $this->response->body($twig);
-        }
+        $twig = Twig::factory(self::STUDENTS_LIST, [
+            'List' => $faculty->getStudents()
+        ]);
+
+        $this->response->body($twig);
     }
 
     public function action_getStudentsByName() {
diff --git a/~dev_rating/application/classes/Controller/Handler/Map.php b/~dev_rating/application/classes/Controller/Handler/Map.php
index 021b14179..b5a45c350 100644
--- a/~dev_rating/application/classes/Controller/Handler/Map.php
+++ b/~dev_rating/application/classes/Controller/Handler/Map.php
@@ -398,7 +398,7 @@ class Controller_Handler_Map extends Controller_Handler {
 						-> rule('DisciplineID', 'not_empty')
 						-> rule('DisciplineID', 'digit');
 			if($this->post->check()) {	
-				$searchResult = Model_Students::NotAttendingDiscipline(
+				$searchResult = Model_Students::search(
 					$this->post['GradeID'],
 					$this->post['GroupID'],
 					$this->post['FacultyID'],
diff --git a/~dev_rating/application/classes/Model/Faculty.php b/~dev_rating/application/classes/Model/Faculty.php
index ca7140acd..946663293 100644
--- a/~dev_rating/application/classes/Model/Faculty.php
+++ b/~dev_rating/application/classes/Model/Faculty.php
@@ -1,12 +1,24 @@
 <?php defined('SYSPATH') or die('No direct script access.');
 
+/**
+ * @property-read $ID int
+ */
 class Model_Faculty extends Model
 {
-    private $id;
+    private $ID;
+
+    public function __get($name) {
+        if ($name === 'ID')
+            return $this->ID;
+        throw new RuntimeException();
+    }
 
     public static function with($id) {
+        if (!is_numeric($id) || $id <= 0)
+            throw new InvalidArgumentException('ID is incorrect!');
+
         $f = new self();
-        $f->id = (int) $id;
+        $f->ID = (int) $id;
         return $f;
     }
 
@@ -15,7 +27,7 @@ class Model_Faculty extends Model
         $semesterID = $semesterID ? $semesterID : User::instance()->SemesterID;
         $sql = "CALL `GetDisciplines`(:id, :semesterID)";
         $query = DB::query(Database::SELECT, $sql)
-            ->param(':id', $this->id)
+            ->param(':id', $this->ID)
             ->param(':semesterID', $semesterID)
             ->execute();
 
@@ -26,18 +38,34 @@ class Model_Faculty extends Model
         return $list;
     }
 
+    /**
+     * Pass groupID == 0 & gradeID == 0 to get all students.
+     */
+    public function getStudents($gradeID = null, $groupID = null, $semesterID = null) {
+        $semesterID = $semesterID ? $semesterID : User::instance()->SemesterID;
+
+        $sql = 'CALL `GetStudentsByFaculty`(:faculty, :grade, :group, :semester)';
+        return DB::query(Database::SELECT, $sql)
+            ->parameters([
+                ':faculty'  => (int) $this->ID,
+                ':grade'    => (int) $gradeID,
+                ':group'    => (int) $groupID,
+                ':semester' => (int) $semesterID,
+            ])->execute()->as_array();
+    }
+
     public function getGroups($grade) {
         $sql = "CALL `GetGroups`(:grade, :id)";
         return DB::query(Database::SELECT, $sql)
             ->param(':grade', (int) $grade)
-            ->param(':id', $this->id)
+            ->param(':id', $this->ID)
             ->execute()->as_array();
     }
 
     public function getSubjects() {
         $sql = "CALL `GetSubjects`(:faculty)";
         return DB::query(Database::SELECT, $sql)
-            ->param(':faculty', $this->id)
+            ->param(':faculty', $this->ID)
             ->execute()->as_array();
     }
 }
diff --git a/~dev_rating/application/classes/Model/Students.php b/~dev_rating/application/classes/Model/Students.php
index 8069c1962..7ce63425f 100644
--- a/~dev_rating/application/classes/Model/Students.php
+++ b/~dev_rating/application/classes/Model/Students.php
@@ -30,26 +30,7 @@ class Model_Students extends Model
             ->execute()->get('ID');
     }
 
-    public static function byStudyGroup($groupID) {
-        return self::collect(self::ofGroup($groupID));
-    }
-
-    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);
-    }
-
-    public static function searchStudents($gradeID, $groupID, $facultyID, $name, $disciplineID) {
+    public static function search($gradeID, $groupID, $facultyID, $name, $disciplineID) {
         $sql = 'CALL `SearchStudents`(:grade, :group, :faculty, :name, :discipline)';
         return DB::query(Database::SELECT, $sql)
             ->parameters([
@@ -74,24 +55,4 @@ class Model_Students extends Model
                 ':semester' => (int) $semesterID,
             ])->execute()->as_array();
     }
-
-    private static function collect($students) {
-        $studentsHandled = [];
-        foreach ($students as $row) {
-            $row['Degree'] = Model_Grades::getDegreeTitle($row['Degree']);  // todo: remove this one
-            $studentsHandled[] = $row;
-        }
-        return $studentsHandled;
-    }
-
-    public static function NotAttendingDiscipline($GradeID, $GroupID, $FacultyID, $Name, $DisciplineID) {
-        $students = self::searchStudents($GradeID, $GroupID, $FacultyID, $Name, $DisciplineID);
-        return self::collect($students);
-    }
-
-    /*
-     * TODO:
-     *      a wrapper around `view_students` table
-     *      see GetStudents, SearchStudents procedures.
-     */
 }
\ No newline at end of file
-- 
GitLab