123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- <?php
- if (php_sapi_name() != 'cli') {
- exit;
- }
- require_once __DIR__."/../inc/global.inc.php";
- define("OFFSET", "15");
- function getMonthFirstAndLastDates($initialDate = null)
- {
- $startDate = $initialDate ? $initialDate : date("Y-m-01");
- $nextMonthStartDate = date("Y-m-d", api_strtotime($startDate." + 1 month"));
- $endDate = date("Y-m-d", api_strtotime($nextMonthStartDate." - 1 minute"));
- return array('startDate' => $startDate, 'endDate' => $endDate);
- }
- function getQuarterFirstAndLastDates($initialDate = null)
- {
- $startDate = $initialDate ? $initialDate : date("Y-m-01");
- $month = getQuarterFirstMonth(getQuarter(date('m', $startDate)));
- $startDate = substr($startDate, 0, 5).$month.'-01';
- $nextQuarterStartDate = date('Y-m-d', api_strtotime($startDate.' + 3 month'));
- $endDate = date('Y-m-d', api_strtotime($nextQuarterStartDate.' - 1 minute'));
- return array('startDate' => $startDate, 'endDate' => $endDate);
- }
- function getQuarter($month)
- {
- $quarter = 1;
-
- if (substr($month, 0, 1) == '0') {
- $month = substr($month, 1);
- }
-
- switch ($month) {
- case 1:
-
- case 2:
-
- case 3:
- $quarter = 1;
- break;
- case 4:
-
- case 5:
-
- case 6:
- $quarter = 2;
- break;
- case 7:
-
- case 8:
-
- case 9:
- $quarter = 3;
- break;
- case 10:
-
- case 11:
-
- case 12:
- $quarter = 4;
- break;
- }
- return $quarter;
- }
- function getQuarterFirstMonth($quarter)
- {
- switch ($quarter) {
- case 1:
- return '01';
- case 2:
- return '04';
- case 3:
- return '07';
- case 4:
- return '10';
- }
- return false;
- }
- function getQuarterRoman($quarter)
- {
- switch ($quarter) {
- case 1:
- return 'I';
- case 2:
- return 'II';
- case 3:
- return 'III';
- case 4:
- return 'IV';
- }
- }
- function createCourseSessions($courses, $administratorId, $startDate, $endDate)
- {
- echo "\n";
- echo $courses ?
- "Creating sessions and adding courses for the period between ".$startDate." and ".$endDate : "Every course is already in session for the period between ".$startDate." and ".$endDate;
- echo "\n=====================================================================================\n\n";
-
- foreach ($courses as $course) {
-
- $month = date("m", api_strtotime($startDate));
- $year = date("Y", api_strtotime($startDate));
- $quarter = getQuarter($month);
- $quarter = getQuarterRoman($quarter);
- $period = $year.'-'.$quarter;
- $sessionName = '['.$period.'] '.$course['title'];
- $sessionId = SessionManager::create_session(
- $sessionName,
- $startDate,
- $endDate,
- null,
- null,
- null,
- null,
- $administratorId,
- 0,
- SESSION_INVISIBLE
- );
- SessionManager::add_courses_to_session($sessionId, array($course['id']));
- echo "Session '".$sessionName."' created.\nCourse '".$course['title']."' added.\n\n";
- }
- }
- echo "Starting process...".PHP_EOL;
- $administrators = array_reverse(UserManager::get_all_administrators());
- $lastingAdministrators = count($administrators);
- while (!$administrators[$lastingAdministrators - 1]['active'] && $lastingAdministrators > 0) {
- $lastingAdministrators--;
- }
- if (!$lastingAdministrators) {
- echo "There are no active administrators. Process halted.\n";
- exit;
- }
- $administratorId = intval($administrators[$lastingAdministrators - 1]['user_id']);
- $dates = getQuarterFirstAndLastDates(date('Y-m-').'01');
- $courses = CourseManager::getCoursesWithoutSession($dates['startDate'], $dates['endDate']);
- createCourseSessions($courses, $administratorId, $dates['startDate'], $dates['endDate']);
- $offsetDay = intval(substr($dates['endDate'], 8, 2)) - OFFSET;
- if (date("Y-m-d") >= date(substr($dates['endDate'], 0, 8).$offsetDay)) {
- $dates = getQuarterFirstAndLastDates(date("Y-m-d", api_strtotime(date("Y-m-01")." + 3 month")));
-
- $courses = CourseManager::getCoursesWithoutSession($dates['startDate'], $dates['endDate']);
- createCourseSessions($courses, $administratorId, $dates['startDate'], $dates['endDate']);
- }
|