public function actionTest($id)
 {
     // renders the view file 'protected/views/screen/display.php'
     // using the default layout 'protected/views/layouts/billboard.php'
     $this->screen_id = $id;
     $screen = Screen::model()->findByPk($id);
     if ($screen === null) {
         throw new CHttpException(404, Yii::t('error', 'The requested screen does not exist.'));
     }
     Yii::app()->theme = count($screen->themes) == 0 ? 'he' : $screen->themes[0]->theme;
     $conditions = array();
     $conditions[] = 'ads.expire>=:now';
     $conditions[] = 'ads.start<=:now';
     $conditions[] = 'ads.' . date('D', strtotime('now')) . '=1';
     $ads = $screen->ads(array('condition' => implode(' AND ', $conditions), 'params' => array(':now' => date('Y-m-d', strtotime('now')))));
     $yeshuv = $screen->yeshuv->name_heb;
     $commercial = $screen->commercials[0]->file_name;
     date_default_timezone_set('Asia/Jerusalem');
     if ($this->isweekend()) {
         $holiday = Holiday::model()->find(array('condition' => 'name = :name', 'params' => array(':name' => 'friday')));
     } else {
         $holiday = Holiday::model()->find(array('condition' => 'end_date >= :now && start_date <= :now', 'params' => array(':now' => date('Y-m-d H:i:s', strtotime('now')))));
     }
     $this->render('test', array('ads' => $ads, 'screen' => $screen, 'yeshuv' => $yeshuv, 'commercial' => 5, 'holiday' => $holiday));
 }
 public function actionHoliday()
 {
     date_default_timezone_set('Asia/Jerusalem');
     $holiday = Holiday::model()->find(array('condition' => 'end_date >= :now && start_date <= :now', 'params' => array(':now' => date('Y-m-d H:i:s', strtotime('now')))));
     if ($holiday) {
         ScreenAjax::model()->updateAll(array('refresh' => 1));
     }
 }
Exemple #3
0
 public function actionAutopost()
 {
     $semester = Semesters::model()->actual();
     if (!$semester) {
         throw new CException('Нет семестра');
     }
     $tomorrow_date = (new DateTime())->add(new DateInterval("P1D"))->format('Y-m-d');
     $tomorrow_time = strtotime($tomorrow_date);
     if (date('N', $tomorrow_time) == 7) {
         throw new CException('Завтра выходной');
     }
     $current_hour = date('G');
     $week_number = ($semester->week_number + date('W', $tomorrow_time) - date('W', strtotime($semester->start_date))) % 2 ? 1 : 2;
     $week_day = date('N', $tomorrow_time);
     /** @var GroupAutopost[] $autoposts */
     if (!($autoposts = GroupAutopost::model()->with(['group' => ['with' => ['schedule_elements' => ['condition' => 'week_number = :week_number AND week_day = :week_day AND semester_id = :semester_id', 'params' => [':week_number' => $week_number, ':week_day' => $week_day, ':semester_id' => $semester->id]]]]])->findAllByAttributes(['hour' => $current_hour, 'status' => GroupAutopost::STATUS_ACTIVE]))) {
         throw new CException('Нет групп для автопостинга');
     }
     foreach ($autoposts as $autopost) {
         $replaces = CHtml::listData(GroupReplace::model()->with(['subject', 'classroom', 'teacher'])->findAllByAttributes(['group_id' => $autopost->group_id, 'date' => $tomorrow_date]), 'number', function ($model) {
             return array_merge($model->attributes, ['teacher' => $model->teacher, 'classroom' => $model->classroom, 'subject' => $model->subject]);
         });
         $schedule_elements = CHtml::listData($autopost->group->schedule_elements, 'number', function ($model) {
             return array_merge($model->attributes, ['teacher' => $model->teacher, 'classroom' => $model->classroom, 'subject' => $model->subject]);
         });
         $schedule = $replaces + $schedule_elements;
         ksort($schedule);
         $schedule_text = 'Расписание на завтра:' . PHP_EOL;
         $schedule_count = 0;
         /** @var $holiday Holiday */
         if ($holiday = Holiday::model()->findByAttributes(['date' => $tomorrow_date])) {
             $schedule_text .= 'Выходной - ' . $holiday->name;
         } else {
             if ($schedule) {
                 foreach ($schedule as $subject) {
                     if (isset($subject['cancel']) && $subject['cancel']) {
                         continue;
                     }
                     $schedule_count++;
                     $schedule_text .= $subject['number'] . ') ' . $subject['subject']->name;
                     if ($subject['teacher']) {
                         $schedule_text .= ', ' . $subject['teacher']->lastname . ' ' . mb_substr($subject['teacher']->firstname, 0, 1, "UTF-8") . '.' . mb_substr($subject['teacher']->middlename, 0, 1, "UTF-8") . '.';
                     }
                     if ($subject['classroom']) {
                         $schedule_text .= ' (' . $subject['classroom']->name . ')';
                     }
                     if (isset($subject['comment']) && strlen($subject['comment'])) {
                         $schedule_text .= ' - ' . $subject['comment'];
                     }
                     $schedule_text .= PHP_EOL;
                 }
             }
             if (!$schedule || $schedule_count == 0) {
                 $schedule_text .= 'Пар нет' . PHP_EOL;
             }
         }
         $schedule_text .= 'Данные предоставлены проектом @studyschedule (Расписание ККЭП)';
         $params = http_build_query(['owner_id' => $autopost->page_id, 'message' => $schedule_text, 'from_group' => 1, 'access_token' => $autopost->access_token]);
         echo "----" . $autopost->group->number . "----" . PHP_EOL;
         $response = file_get_contents('https://api.vk.com/method/wall.post?' . $params);
         if ($response) {
             $answer = json_decode($response, true);
             var_dump($answer);
             if (isset($answer['error'])) {
                 $autopost->status = GroupAutopost::STATUS_DISABLE;
                 $autopost->save();
                 $email = $autopost->group->owner->email;
                 $mail = new YiiMailer("autopost", ['group' => $autopost->group]);
                 $mail->setFrom(isset(Yii::app()->params->YiiMailer->Username) ? Yii::app()->params->YiiMailer->Username : Yii::app()->params->adminEmail, 'Система управления учебным расписанием');
                 $mail->setTo($email);
                 $mail->setSubject('Ошибка автопостинга в ВКонтакте');
                 $mail->send();
             }
         } else {
             echo 'Error when send request...';
         }
         echo PHP_EOL;
     }
 }
Exemple #4
0
 /**
  * Delete all the holidays of the given year and locale
  * 
  * @param string $year
  * @param string $locale
  * @throws Exception 
  */
 public function deleteHolidays($year, $locale = 'en')
 {
     if (empty($year) || empty($locale)) {
         throw new \Exception('No year or locale given for the holidays delete function.');
     }
     $startYear = mktime(0, 0, 0, 1, 1, $year);
     $endYear = mktime(23, 59, 59, 12, 31, $year);
     $findCriteria = \GO\Base\Db\FindCriteria::newInstance()->addCondition('date', date('Y-m-d', $startYear), '>=')->addCondition('date', date('Y-m-d', $endYear), '<=')->addCondition('region', $locale);
     $findParams = \GO\Base\Db\FindParams::newInstance()->criteria($findCriteria);
     $holidays = Holiday::model()->find($findParams);
     while ($holiday = $holidays->fetch()) {
         $holiday->delete();
     }
 }
 public function actionIndex($group = false)
 {
     /** @var Group $group */
     if ($group != false && !($group = Group::model()->filled()->findByAttributes(['number' => $group]))) {
         throw new CHttpException(404, 'Данной группы не найден');
     }
     /** @var Semesters $semester */
     $semester = Semesters::model()->actual();
     $current_date = date('Y-m-d');
     $current_time = strtotime($current_date);
     if (!$semester) {
         throw new CHttpException(404, 'Сейчас нет семестра :-(');
     }
     $group_list = CHtml::listData(Group::model()->filled()->findAll(), 'number', 'number');
     $schedule = [];
     if ($group) {
         $time_one_day = 60 * 60 * 24;
         for ($i = $current_time; $i <= $current_time + $time_one_day * 7; $i = $i + $time_one_day) {
             $week_day = date('N', $i);
             $date = date('d.n.Y', $i);
             /** @var Holiday $holiday */
             if (($holiday = Holiday::model()->findByAttributes(['date' => date('Y-m-d', $i)])) || $week_day == 7) {
                 $schedule[$date] = ['holiday' => true];
                 if ($holiday) {
                     $schedule[$date]['name'] = $holiday->name;
                 }
                 continue;
             }
             $schedule[$date] = [];
             $week_number = ($semester->week_number + (date('W', $i) - date('W', strtotime($semester->start_date)))) % 2 ? 1 : 2;
             $schedule_elements = ScheduleElement::model()->findAllByAttributes(['group_id' => $group->id, 'semester_id' => $semester->id, 'week_number' => $week_number, 'week_day' => $week_day]);
             $numbers = [1, 2, 3, 4, 5];
             /** @var ScheduleElement $schedule_element */
             foreach ($schedule_elements as $schedule_element) {
                 unset($numbers[array_search($schedule_element->number, $numbers)]);
                 /** @var GroupReplace $replace */
                 $replace = GroupReplace::model()->findByAttributes(['date' => date('Y-m-d', $i), 'number' => $schedule_element->number, 'group_id' => $group->id]);
                 if ($replace && $replace->cancel) {
                     $schedule[$date][$schedule_element->number] = ['cancel' => true, 'replace' => true];
                 } elseif ($replace) {
                     $schedule[$date][$schedule_element->number] = ['subject' => $replace->subject->name, 'replace' => true, 'comment' => $replace->comment];
                     if ($replace->teacher_id) {
                         $schedule[$date][$schedule_element->number]['teacher'] = $replace->teacher->lastname . ' ' . mb_substr($replace->teacher->firstname, 0, 1, "UTF-8") . '.' . mb_substr($replace->teacher->middlename, 0, 1, "UTF-8") . '.';
                     }
                     if ($replace->classroom_id) {
                         $schedule[$date][$schedule_element->number]['classroom'] = $replace->classroom->name;
                     }
                 } else {
                     $schedule[$date][$schedule_element->number] = ['subject' => $schedule_element->subject->name];
                     if ($schedule_element->teacher_id) {
                         $schedule[$date][$schedule_element->number]['teacher'] = $schedule_element->teacher->lastname . ' ' . mb_substr($schedule_element->teacher->firstname, 0, 1, "UTF-8") . '.' . mb_substr($schedule_element->teacher->middlename, 0, 1, "UTF-8") . '.';
                     }
                     if ($schedule_element->classroom_id) {
                         $schedule[$date][$schedule_element->number]['classroom'] = $schedule_element->classroom->name;
                     }
                 }
             }
             if (count($numbers)) {
                 foreach ($numbers as $number) {
                     /** @var GroupReplace $replace */
                     $replace = GroupReplace::model()->findByAttributes(['date' => date('Y-m-d', $i), 'number' => $number, 'group_id' => $group->id]);
                     if ($replace && $replace->cancel) {
                         $schedule[$date][$replace->number] = ['cancel' => true, 'replace' => true];
                     } elseif ($replace) {
                         $schedule[$date][$replace->number] = ['subject' => $replace->subject->name, 'replace' => true];
                         if ($replace->teacher_id) {
                             $schedule[$date][$replace->number]['teacher'] = $replace->teacher->lastname . ' ' . mb_substr($replace->teacher->firstname, 0, 1, "UTF-8") . '.' . mb_substr($replace->teacher->middlename, 0, 1, "UTF-8") . '.';
                         }
                         if ($replace->classroom_id) {
                             $schedule[$date][$replace->number]['classroom'] = $replace->classroom->name;
                         }
                     }
                 }
             }
             ksort($schedule[$date]);
         }
     }
     Yii::app()->clientScript->registerMetaTag('noarchive', 'robots');
     $this->render('index', ['group' => $group ? $group->number : false, 'group_list' => $group_list, 'schedule' => $schedule]);
 }
 /**
  * Returns the data model based on the primary key given in the GET variable.
  * If the data model is not found, an HTTP exception will be raised.
  * @param integer $id the ID of the model to be loaded
  * @return Holiday the loaded model
  * @throws CHttpException
  */
 public function loadModel($id)
 {
     $model = Holiday::model()->findByPk($id);
     if ($model === null) {
         throw new CHttpException(404, 'The requested page does not exist.');
     }
     return $model;
 }
 public function actionGroup($id)
 {
     if (!isset($_SERVER['HTTP_USER_AGENT'])) {
         throw new CHttpException(403, 'У вас нет useragent, поэтому сюда вам нельзя');
     }
     /** @var Semesters $semester */
     if (!($semester = Semesters::model()->with(['call_list', 'call_list_short'])->actual())) {
         throw new CHttpException(404, 'Сейчас нет семестра :-(');
     }
     // Аналитика
     if (!($unique_ics_id = Yii::app()->session->get('unique_ics_id', false))) {
         $unique_ics_id = uniqid();
         Yii::app()->session->add('unique_ics_id', $unique_ics_id);
     }
     $analytics = new IcsAnalytics();
     $analytics->setAttributes(['useragent' => $_SERVER['HTTP_USER_AGENT'], 'group' => $id, 'time' => date("Y-m-d H:i:s"), 'ip' => get_client_ip(), 'unique_id' => $unique_ics_id]);
     $analytics->save();
     // Даты
     $count_days = 24;
     $current_date = date('Y-m-d');
     $period_interval = new DateInterval('P1D');
     $period_start = new DateTime($current_date);
     $period_start->modify('-' . floor($count_days / 2) . ' days');
     $period_end = new DateTime($current_date);
     $period_end->modify('+' . floor($count_days / 2) . ' days');
     $period = new DatePeriod($period_start, $period_interval, $period_end);
     $period_dates = [];
     $semester_start = new DateTime($semester->start_date);
     $semester_end = new DateTime($semester->end_date);
     /** @var DateTime $period_date */
     foreach ($period as $period_date) {
         $period_dates[] = $period_date->format('Y-m-d');
     }
     // Загрузка группы с расписанием и заменами за нужный период
     /** @var Group $group */
     if (!($group = Group::model()->filled()->with(['schedule_elements' => ['with' => ['teacher' => ['alias' => 's_teacher'], 'classroom' => ['alias' => 's_classroom'], 'subject' => ['alias' => 's_subject']], 'condition' => 'semester_id = :semester_id', 'params' => [':semester_id' => $semester->id]], 'replaces' => ['together' => false, 'with' => ['teacher' => ['alias' => 'r_teacher'], 'classroom' => ['alias' => 'r_classroom'], 'subject' => ['alias' => 'r_subject']], 'condition' => 'date >= :start_date AND date <= :end_date', 'params' => [':start_date' => $period_start->format('Y-m-d'), ':end_date' => $period_end->format('Y-m-d')]]])->findByAttributes(['number' => $id]))) {
         throw new CHttpException(404, 'Группа не найдена или незаполнена');
     }
     // Преобразование массивов
     /** @var CallListsElements[] $call_list */
     $call_list = CHtml::listData($semester->call_list()->elements, 'number', function ($model) {
         return $model;
     });
     /** @var CallListsElements[] $call_list_short */
     $call_list_short = CHtml::listData($semester->call_list_short()->elements, 'number', function ($model) {
         return $model;
     });
     $schedule_elements = [];
     for ($i = 1; $i <= 2; $i++) {
         for ($j = 1; $j <= 6; $j++) {
             $schedule_elements[$i][$j] = CHtml::listData(array_filter($group->schedule_elements, function ($model) use(&$i, &$j) {
                 return $model->week_number == $i && $model->week_day == $j;
             }), 'number', function ($model) {
                 return $model;
             });
             ksort($schedule_elements[$i][$j], SORT_NUMERIC);
         }
         ksort($schedule_elements[$i], SORT_NUMERIC);
     }
     $replaces = [];
     foreach ($group->replaces as $replace) {
         $replaces[$replace->date][$replace->number] = $replace;
     }
     $calendar = new Calendar();
     $calendar->setProdId('-//Sc0Rp1D//KKEP//RU');
     $calendar->setTimezone(new DateTimeZone('Europe/Moscow'));
     $calendar->setCustomHeaders(['X-PUBLISHED-TTL' => 'PT1H', 'REFRESH-INTERVAL' => 'VALUE=DURATION:PT1H']);
     $holidays = CHtml::listData(Holiday::model()->findAllByAttributes(['date' => $period_dates]), 'date', 'name');
     $short_days = CHtml::listData(ShortDay::model()->findAllByAttributes(['date' => $period_dates]), 'date', 'name');
     /** @var DateTime $period_element */
     foreach ($period as $period_element) {
         $week_day = $period_element->format('N');
         $date_formatted = $period_element->format('Y-m-d');
         if ($period_element < $semester_start || $week_day == 7 || array_key_exists($date_formatted, $holidays)) {
             continue;
         }
         if ($period_element > $semester_end) {
             break;
         }
         if ($week_day == 6 || array_key_exists($date_formatted, $short_days)) {
             $current_call_list = $call_list_short;
         } else {
             $current_call_list = $call_list;
         }
         $week_number = ($semester->week_number + ($period_element->format('W') - $semester_start->format('W'))) % 2 ? 1 : 2;
         /** @var ScheduleElement $schedule_element */
         foreach ((isset($replaces[$date_formatted]) ? $replaces[$date_formatted] : []) + $schedule_elements[$week_number][$week_day] as $schedule_element) {
             if (isset($schedule_element->cancel) && $schedule_element->cancel) {
                 continue;
             }
             $event = new CalendarEvent();
             $start_time = clone $period_element;
             $call_list_start = explode(':', $current_call_list[$schedule_element->number]->start_time);
             $call_list_end = explode(':', $current_call_list[$schedule_element->number]->end_time);
             $start_time->setTime($call_list_start[0], $call_list_start[1]);
             $event->setStart($start_time);
             $end_time = clone $period_element;
             $end_time->setTime($call_list_end[0], $call_list_end[1]);
             $event->setEnd($end_time);
             $event->setSummary($schedule_element->subject->name);
             $event->setUid(md5($date_formatted . ' ' . $current_call_list[$schedule_element->number]->start_time));
             if ($schedule_element->classroom_id || $schedule_element->teacher_id) {
                 $location = new Location();
                 $desc = [];
                 if ($schedule_element->teacher_id) {
                     $desc[] = $schedule_element->teacher->lastname . ' ' . mb_substr($schedule_element->teacher->firstname, 0, 1, "UTF-8") . '.' . mb_substr($schedule_element->teacher->middlename, 0, 1, "UTF-8") . '.';
                 }
                 if ($schedule_element->classroom_id) {
                     $desc[] = $schedule_element->classroom->name;
                 }
                 $location->setName(implode(' | ', $desc));
                 $event->addLocation($location);
             }
             if ($schedule_element instanceof GroupReplace && strlen($schedule_element->comment)) {
                 $event->setDescription($schedule_element->comment);
             }
             $calendar->addEvent($event);
         }
     }
     $calendarExport = new CalendarExport(new CalendarStream(), new Formatter());
     $calendarExport->addCalendar($calendar);
     //header('Content-Type: text/calendar; charset=utf-8');
     echo $calendarExport->getStream();
 }