/**
  * @return void
  * @throws ApiException
  */
 private function initialize()
 {
     $lessons = LessonModel::getAllByGroupNameOrGroupId($this->group['group_id']);
     $this->weeks[1] = new WeekModel(1);
     $this->weeks[2] = new WeekModel(2);
     foreach ($lessons as $lesson) {
         $this->weeks[$lesson['lesson_week']]->days[$lesson['day_number']]->lessons[] = $lesson;
     }
 }
 /**
  * @param string|int $teacher_name
  * @param string $cond
  * @return array
  * @throws ApiException
  */
 public static function getAllByTeacherNameOrTeacherId($teacher_name, $cond = "")
 {
     $result = array();
     //hide some properties
     $hideFilter = array('group_id' => true);
     Registry::set('LessonModelHideFilter', $hideFilter);
     $teacher_name = urldecode(trim($teacher_name));
     /** @var PDO $db */
     $db = Registry::get('db');
     $teacher_id = $db->query("\r\n            SELECT teacher_id FROM " . TeacherModel::TABLE . "\r\n                WHERE (teacher_name = " . $db->quote($teacher_name) . " OR teacher_id = " . $db->quote($teacher_name) . ")\r\n        ")->fetchColumn();
     $count = 0;
     if ($teacher_id) {
         $count = $db->query("\r\n            SELECT COUNT(*) FROM " . LessonModel::TABLE . " AS t1\r\n                JOIN " . TeacherModel::RELATION_TABLE . " as t2 USING(" . LessonModel::PRIMARY_KEY . ")\r\n                    WHERE t2." . TeacherModel::PRIMARY_KEY . " = '" . abs(intval($teacher_id)) . "' {$cond}\r\n            ")->fetchColumn();
     }
     if ($count > 0) {
         $query = $db->query("\r\n               SELECT *\r\n                   FROM " . LessonModel::TABLE . " AS t1\r\n                    JOIN " . TeacherModel::RELATION_TABLE . " as t2 USING(" . LessonModel::PRIMARY_KEY . ")\r\n                        WHERE t2." . TeacherModel::PRIMARY_KEY . " = '" . abs(intval($teacher_id)) . "' {$cond}\r\n                            GROUP BY t1.day_number,t1.lesson_number,t1.lesson_week\r\n                                ORDER BY t1.lesson_week,t1.day_number,t1.lesson_number ASC\r\n            ");
         while ($data = $query->fetch(PDO::FETCH_ASSOC)) {
             $lessonModel = new LessonModel();
             $lessonModel->unpack($data);
             //$lessonModel->teachers = TeacherModel::getAllByLessonId($lessonModel->lesson_id);
             //load rooms
             $lessonModel->rooms = RoomModel::getAllByLessonId($lessonModel->lesson_id);
             //load groups
             $q = $db->query("\r\n                    SELECT t1.* FROM " . GroupModel::TABLE . " as t1\r\n                        JOIN " . LessonModel::TABLE . " as t2 USING(" . GroupModel::PRIMARY_KEY . ")\r\n                            WHERE t2.day_number = '" . $lessonModel->day_number . "'\r\n                            AND t2.lesson_number = '" . $lessonModel->lesson_number . "'\r\n                            AND t2.teacher_name = " . $db->quote($lessonModel->teacher_name) . "\r\n                            AND t2.lesson_week = '" . $lessonModel->lesson_week . "'\r\n                ");
             $groupsRes = array();
             while ($groupsData = $q->fetch(PDO::FETCH_ASSOC)) {
                 $group = new GroupModel();
                 $group->unpack($groupsData);
                 $groupsRes[] = $group->toArray();
             }
             $m = $lessonModel->toArray();
             $m["groups"] = $groupsRes;
             $result[] = $m;
         }
     } else {
         throw new ApiException("Lessons not found");
     }
     return $result;
 }
 public function groups_lessonsRelationAction($data)
 {
     $groupName = $data["groups"];
     $groupName = urldecode($groupName);
     $cond = "";
     if (isset($_GET["week"])) {
         $week = abs(intval($_GET["week"]));
         if ($week > 2) {
             throw new Exception("404 - Not Found");
         }
         $cond .= " AND lesson_week = {$week}";
     }
     if (isset($_GET["day"])) {
         $day = abs(intval($_GET["day"]));
         if ($day > 7) {
             throw new Exception("404 - Not Found");
         }
         $cond .= " AND day_number = {$day}";
     }
     $this->data = LessonModel::getAllByGroupNameOrGroupId($groupName, $cond);
     return $this->send(200);
 }
 /**
  * @api
  * @param $data
  * @return string
  * @throws ApiException
  */
 public function groups_lessonsRelationAction($data)
 {
     $filter = $this->_fc->getFilter();
     $condition = "";
     if ($filter != null) {
         $allowFilters = ['day_number' => true, 'day_name' => true, 'lesson_number' => true, 'lesson_week' => true, 'lesson_type' => true, 'rate' => true];
         $filterContext = $this->_fc->getArrayDepth($filter) == 1 ? new FilterContext(new AndFilterStrategy()) : new FilterContext(new OrFilterStrategy());
         $condition = $filterContext->buildCondition($filter, $allowFilters);
     }
     $data["groups"] = urldecode($data["groups"]);
     $response = LessonModel::getAllByGroupNameOrGroupId($data['groups'], $condition);
     $this->debugInfo = "duplicateTeachersFilter is enable";
     $this->data = $response;
     return $this->send(200);
 }