create_course_sessions.php 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 month.
  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 First day of the month
  22. * @return array First and last days of the month
  23. */
  24. function getMonthFirstAndLastDates($initialDate = null) {
  25. $startDate = $initialDate ? $initialDate : date("Y-m-01");
  26. $nextMonthStartDate = date("Y-m-d", api_strtotime($startDate." + 1 month"));
  27. $endDate = date("Y-m-d", api_strtotime($nextMonthStartDate." - 1 minute"));
  28. return array('startDate' => $startDate, 'endDate' => $endDate);
  29. }
  30. /**
  31. * Creates one session per course with $administratorId as the creator and
  32. * adds it to the session starting on $startDate and finishing on $endDate
  33. * @param array Courses
  34. * @param int Administrator id
  35. * @param date First day of the month
  36. * @param date Last day of the month
  37. * @return void
  38. */
  39. function createCourseSessions($courses, $administratorId, $startDate, $endDate) {
  40. echo "\n";
  41. echo $courses ?
  42. "Creating sessions and adding courses for the period between ".$startDate." and ".$endDate :
  43. "Every course is already in session for the period between ".$startDate." and ".$endDate;
  44. echo "\n=====================================================================================\n\n";
  45. // Loop through courses creating one session per each and adding them
  46. foreach ($courses as $course) {
  47. $sessionName = $course['title']." (".date("M Y", api_strtotime($startDate)).")";
  48. $sessionId = SessionManager::create_session(
  49. $sessionName,
  50. $startDate,
  51. $endDate,
  52. 0,
  53. 0,
  54. null,
  55. $administratorId,
  56. 0,
  57. SESSION_INVISIBLE
  58. );
  59. SessionManager::add_courses_to_session($sessionId, array($course['code']));
  60. echo "Session '".$sessionName."' created.\nCourse '".$course['title']."' added.\n\n";
  61. }
  62. }
  63. // Starts the script
  64. // Get first active administrator
  65. $administrators = array_reverse(UserManager::get_all_administrators());
  66. $lastingAdministrators = count($administrators);
  67. while (!$administrators[$lastingAdministrators - 1]['active'] && $lastingAdministrators > 0) {
  68. $lastingAdministrators--;
  69. }
  70. if (!$lastingAdministrators) {
  71. echo "There are no active administrators. Process halted.\n";
  72. exit;
  73. }
  74. $administratorId = intval($administrators[$lastingAdministrators - 1]['user_id']);
  75. // Creates course sessions for the current month
  76. $dates = getMonthFirstAndLastDates();
  77. // Get courses that don't have any session
  78. $courses = CourseManager::getCoursesWithoutSession();
  79. createCourseSessions($courses, $administratorId, $dates['startDate'], $dates['endDate']);
  80. // Creates course sessions for the following month
  81. if (date("Y-m-d") >= date("Y-m-".OFFSET)) {
  82. $dates = getMonthFirstAndLastDates(date("Y-m-d", api_strtotime(date("Y-m-01")." + 1 month")));
  83. // Get courses that don't have any session the next month
  84. $courses = CourseManager::getCoursesWithoutSession($dates['startDate'], $dates['endDate']);
  85. createCourseSessions($courses, $administratorId, $dates['startDate'], $dates['endDate']);
  86. }