create_course_sessions.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Create course sessions procedure. It creates sessions for courses that haven't it yet.
  5. * If today is greater than OFFSET, it will create them also for the next quarter
  6. * @package chamilo.cron
  7. * @author Imanol Losada <imanol.losada@beeznest.com>
  8. */
  9. /**
  10. * Initialization
  11. */
  12. if (php_sapi_name() != 'cli') {
  13. exit; //do not run from browser
  14. }
  15. require_once __DIR__ . "/../inc/global.inc.php";
  16. // First day of the current month to create sessions and add courses for the next month (e.g. "07")
  17. define("OFFSET", "15");
  18. /**
  19. * If no $initialDate is supplied, returns an array with the first and last days of the current
  20. * month. Otherwise, returns an array with the first and last days of the $initialDate month .
  21. * @param array $initialDate First day of the month
  22. * @return array First and last days of the month
  23. */
  24. function getMonthFirstAndLastDates($initialDate = null)
  25. {
  26. $startDate = $initialDate ? $initialDate : date("Y-m-01");
  27. $nextMonthStartDate = date("Y-m-d", api_strtotime($startDate." + 1 month"));
  28. $endDate = date("Y-m-d", api_strtotime($nextMonthStartDate." - 1 minute"));
  29. return array('startDate' => $startDate, 'endDate' => $endDate);
  30. }
  31. /**
  32. * Same as month, but for quarters
  33. * @param array $initialDate First day of the quarter
  34. * @return array First and last days of the quarter
  35. */
  36. function getQuarterFirstAndLastDates($initialDate = null)
  37. {
  38. $startDate = $initialDate ? $initialDate : date("Y-m-01");
  39. $month = getQuarterFirstMonth(getQuarter(date('m', $startDate)));
  40. $startDate = substr($startDate, 0, 5) . $month . '-01';
  41. $nextQuarterStartDate = date('Y-m-d', api_strtotime($startDate.' + 3 month'));
  42. $endDate = date('Y-m-d', api_strtotime($nextQuarterStartDate.' - 1 minute'));
  43. return array('startDate' => $startDate, 'endDate' => $endDate);
  44. }
  45. /**
  46. * Returns a quarter from a month
  47. * @param string The month (digit), with or without leading 0
  48. * @return int The yearly quarter (1, 2, 3 or 4) in which this month lies
  49. */
  50. function getQuarter($month)
  51. {
  52. $quarter = 1;
  53. // Remove the leading 0 if any
  54. if (substr($month, 0, 1) == '0') {
  55. $month = substr($month, 1);
  56. }
  57. // reduce to 4 quarters: 1..3=1; 4..6=2
  58. switch ($month) {
  59. case 1:
  60. //no break
  61. case 2:
  62. //no break
  63. case 3:
  64. $quarter = 1;
  65. break;
  66. case 4:
  67. //no break
  68. case 5:
  69. //no break
  70. case 6:
  71. $quarter = 2;
  72. break;
  73. case 7:
  74. //no break
  75. case 8:
  76. //no break
  77. case 9:
  78. $quarter = 3;
  79. break;
  80. case 10:
  81. //no break
  82. case 11:
  83. //no break
  84. case 12:
  85. $quarter = 4;
  86. break;
  87. }
  88. return $quarter;
  89. }
  90. /**
  91. * Returns the first month of the quarter
  92. * @param int Quarter
  93. * @return string Number of the month, with leading 0
  94. */
  95. function getQuarterFirstMonth($quarter)
  96. {
  97. switch ($quarter) {
  98. case 1:
  99. return '01';
  100. case 2:
  101. return '04';
  102. case 3:
  103. return '07';
  104. case 4:
  105. return '10';
  106. }
  107. return false;
  108. }
  109. /**
  110. * Get the quarter in Roman letters
  111. * @param int Quarter
  112. * @return string Roman letters
  113. */
  114. function getQuarterRoman($quarter)
  115. {
  116. switch ($quarter) {
  117. case 1:
  118. return 'I';
  119. case 2:
  120. return 'II';
  121. case 3:
  122. return 'III';
  123. case 4:
  124. return 'IV';
  125. }
  126. }
  127. /**
  128. * Creates one session per course with $administratorId as the creator and
  129. * adds it to the session starting on $startDate and finishing on $endDate
  130. * @param array $courses Courses
  131. * @param int $administratorId Administrator id
  132. * @param date $startDate First day of the month
  133. * @param date $endDate Last day of the month
  134. * @return void
  135. */
  136. function createCourseSessions($courses, $administratorId, $startDate, $endDate)
  137. {
  138. echo "\n";
  139. echo $courses ?
  140. "Creating sessions and adding courses for the period between ".$startDate." and ".$endDate :
  141. "Every course is already in session for the period between ".$startDate." and ".$endDate;
  142. echo "\n=====================================================================================\n\n";
  143. // Loop through courses creating one session per each and adding them
  144. foreach ($courses as $course) {
  145. //$period = date("m/Y", api_strtotime($startDate));
  146. $month = date("m", api_strtotime($startDate));
  147. $year = date("Y", api_strtotime($startDate));
  148. $quarter = getQuarter($month);
  149. $quarter = getQuarterRoman($quarter);
  150. $period = $year . '-' . $quarter;
  151. $sessionName = '[' . $period . '] ' . $course['title'];
  152. $sessionId = SessionManager::create_session(
  153. $sessionName,
  154. $startDate,
  155. $endDate,
  156. null,
  157. null,
  158. null,
  159. null,
  160. $administratorId,
  161. 0,
  162. SESSION_INVISIBLE
  163. );
  164. SessionManager::add_courses_to_session($sessionId, array($course['id']));
  165. echo "Session '".$sessionName."' created.\nCourse '".$course['title']."' added.\n\n";
  166. }
  167. }
  168. // Starts the script
  169. echo "Starting process..." . PHP_EOL;
  170. // Get first active administrator
  171. $administrators = array_reverse(UserManager::get_all_administrators());
  172. $lastingAdministrators = count($administrators);
  173. while (!$administrators[$lastingAdministrators - 1]['active'] && $lastingAdministrators > 0) {
  174. $lastingAdministrators--;
  175. }
  176. if (!$lastingAdministrators) {
  177. echo "There are no active administrators. Process halted.\n";
  178. exit;
  179. }
  180. $administratorId = intval($administrators[$lastingAdministrators - 1]['user_id']);
  181. // Creates course sessions for the current month
  182. $dates = getQuarterFirstAndLastDates(date('Y-m-').'01');
  183. // Get courses that don't have any session
  184. $courses = CourseManager::getCoursesWithoutSession($dates['startDate'], $dates['endDate']);
  185. createCourseSessions($courses, $administratorId, $dates['startDate'], $dates['endDate']);
  186. // Creates course sessions for the following month
  187. $offsetDay = intval(substr($dates['endDate'], 8, 2)) - OFFSET;
  188. if (date("Y-m-d") >= date(substr($dates['endDate'], 0, 8) . $offsetDay)) {
  189. $dates = getQuarterFirstAndLastDates(date("Y-m-d", api_strtotime(date("Y-m-01")." + 3 month")));
  190. // Get courses that don't have any session the next month
  191. $courses = CourseManager::getCoursesWithoutSession($dates['startDate'], $dates['endDate']);
  192. createCourseSessions($courses, $administratorId, $dates['startDate'], $dates['endDate']);
  193. }