create_course_sessions.php 6.5 KB

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