create_course_sessions.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. * @param string $month
  49. * @return int The yearly quarter (1, 2, 3 or 4) in which this month lies
  50. */
  51. function getQuarter($month)
  52. {
  53. $quarter = 1;
  54. // Remove the leading 0 if any
  55. if (substr($month, 0, 1) == '0') {
  56. $month = substr($month, 1);
  57. }
  58. // reduce to 4 quarters: 1..3=1; 4..6=2
  59. switch ($month) {
  60. case 1:
  61. //no break
  62. case 2:
  63. //no break
  64. case 3:
  65. $quarter = 1;
  66. break;
  67. case 4:
  68. //no break
  69. case 5:
  70. //no break
  71. case 6:
  72. $quarter = 2;
  73. break;
  74. case 7:
  75. //no break
  76. case 8:
  77. //no break
  78. case 9:
  79. $quarter = 3;
  80. break;
  81. case 10:
  82. //no break
  83. case 11:
  84. //no break
  85. case 12:
  86. $quarter = 4;
  87. break;
  88. }
  89. return $quarter;
  90. }
  91. /**
  92. * Returns the first month of the quarter
  93. * @param int Quarter
  94. * @param integer $quarter
  95. * @return string Number of the month, with leading 0
  96. */
  97. function getQuarterFirstMonth($quarter)
  98. {
  99. switch ($quarter) {
  100. case 1:
  101. return '01';
  102. case 2:
  103. return '04';
  104. case 3:
  105. return '07';
  106. case 4:
  107. return '10';
  108. }
  109. return false;
  110. }
  111. /**
  112. * Get the quarter in Roman letters
  113. * @param int Quarter
  114. * @param integer $quarter
  115. * @return string Roman letters
  116. */
  117. function getQuarterRoman($quarter)
  118. {
  119. switch ($quarter) {
  120. case 1:
  121. return 'I';
  122. case 2:
  123. return 'II';
  124. case 3:
  125. return 'III';
  126. case 4:
  127. return 'IV';
  128. }
  129. }
  130. /**
  131. * Creates one session per course with $administratorId as the creator and
  132. * adds it to the session starting on $startDate and finishing on $endDate
  133. * @param array $courses Courses
  134. * @param int $administratorId Administrator id
  135. * @param date $startDate First day of the month
  136. * @param date $endDate Last day of the month
  137. * @return void
  138. */
  139. function createCourseSessions($courses, $administratorId, $startDate, $endDate)
  140. {
  141. echo "\n";
  142. echo $courses ?
  143. "Creating sessions and adding courses for the period between ".$startDate." and ".$endDate :
  144. "Every course is already in session for the period between ".$startDate." and ".$endDate;
  145. echo "\n=====================================================================================\n\n";
  146. // Loop through courses creating one session per each and adding them
  147. foreach ($courses as $course) {
  148. //$period = date("m/Y", api_strtotime($startDate));
  149. $month = date("m", api_strtotime($startDate));
  150. $year = date("Y", api_strtotime($startDate));
  151. $quarter = getQuarter($month);
  152. $quarter = getQuarterRoman($quarter);
  153. $period = $year . '-' . $quarter;
  154. $sessionName = '[' . $period . '] ' . $course['title'];
  155. $sessionId = SessionManager::create_session(
  156. $sessionName,
  157. $startDate,
  158. $endDate,
  159. null,
  160. null,
  161. null,
  162. null,
  163. $administratorId,
  164. 0,
  165. SESSION_INVISIBLE
  166. );
  167. SessionManager::add_courses_to_session($sessionId, array($course['id']));
  168. echo "Session '".$sessionName."' created.\nCourse '".$course['title']."' added.\n\n";
  169. }
  170. }
  171. // Starts the script
  172. echo "Starting process..." . PHP_EOL;
  173. // Get first active administrator
  174. $administrators = array_reverse(UserManager::get_all_administrators());
  175. $lastingAdministrators = count($administrators);
  176. while (!$administrators[$lastingAdministrators - 1]['active'] && $lastingAdministrators > 0) {
  177. $lastingAdministrators--;
  178. }
  179. if (!$lastingAdministrators) {
  180. echo "There are no active administrators. Process halted.\n";
  181. exit;
  182. }
  183. $administratorId = intval($administrators[$lastingAdministrators - 1]['user_id']);
  184. // Creates course sessions for the current month
  185. $dates = getQuarterFirstAndLastDates(date('Y-m-').'01');
  186. // Get courses that don't have any session
  187. $courses = CourseManager::getCoursesWithoutSession($dates['startDate'], $dates['endDate']);
  188. createCourseSessions($courses, $administratorId, $dates['startDate'], $dates['endDate']);
  189. // Creates course sessions for the following month
  190. $offsetDay = intval(substr($dates['endDate'], 8, 2)) - OFFSET;
  191. if (date("Y-m-d") >= date(substr($dates['endDate'], 0, 8) . $offsetDay)) {
  192. $dates = getQuarterFirstAndLastDates(date("Y-m-d", api_strtotime(date("Y-m-01")." + 3 month")));
  193. // Get courses that don't have any session the next month
  194. $courses = CourseManager::getCoursesWithoutSession($dates['startDate'], $dates['endDate']);
  195. createCourseSessions($courses, $administratorId, $dates['startDate'], $dates['endDate']);
  196. }