Browse Source

Add create_course_sessions cron script. Add active column to admin query. Add 'getCoursesWithoutSession' function to course.lib.php - refs BT#9437

Imanol Losada 10 years ago
parent
commit
08458c1147
3 changed files with 106 additions and 2 deletions
  1. 80 0
      main/cron/create_course_sessions.php
  2. 24 0
      main/inc/lib/course.lib.php
  3. 2 2
      main/inc/lib/usermanager.lib.php

+ 80 - 0
main/cron/create_course_sessions.php

@@ -0,0 +1,80 @@
+<?php
+/* For licensing terms, see /license.txt */
+/**
+ * Create course sessions procedure. It creates sessions for courses that haven't it yet.
+ * If today is greater than OFFSET, it will create them also for the next month.
+ * @package chamilo.cron
+ * @author Imanol Losada <imanol.losada@beeznest.com>
+ */
+
+/**
+ * Initialization
+ */
+if (php_sapi_name() != 'cli') {
+    exit; //do not run from browser
+}
+
+require_once __DIR__ . "/../inc/global.inc.php";
+
+// First day of the current month to create sessions and add courses for the next month (e.g. "07")
+define("OFFSET", "15");
+
+function getMonthFirstAndLastDates($initialDate = null) {
+    $startDate = $initialDate ? $initialDate : date("Y-m-01");
+    $nextMonthStartDate = date("Y-m-d", strtotime($startDate." + 1 month"));
+    $endDate = date("Y-m-d", strtotime($nextMonthStartDate." - 1 minute"));
+    return array('startDate' => $startDate, 'endDate' => $endDate);
+}
+
+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";
+    // Loop through courses creating one session per each and adding them
+    foreach ($courses as $course) {
+        $sessionName = $course['title']." (".date("M Y", strtotime($startDate)).")";
+        $sessionId = SessionManager::create_session(
+            $sessionName,
+            $startDate,
+            $endDate,
+            0,
+            0,
+            null,
+            $administratorId,
+            0,
+            SESSION_INVISIBLE
+        );
+        SessionManager::add_courses_to_session($sessionId, array($course['code']));
+        echo "Session '".$sessionName."' created.\nCourse '".$course['title']."' added.\n\n";
+    }
+}
+
+// Starts the script
+
+// Get first active administrator
+$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']);
+
+// Creates course sessions for the current month
+$dates = getMonthFirstAndLastDates();
+// Get courses that don't have any session
+$courses = CourseManager::getCoursesWithoutSession();
+createCourseSessions($courses, $administratorId, $dates['startDate'], $dates['endDate']);
+
+// Creates course sessions for the following month
+if (date("Y-m-d") >= date("Y-m-".OFFSET)) {
+    $dates = getMonthFirstAndLastDates(date("Y-m-d", strtotime(date("Y-m-01")." + 1 month")));
+    // Get courses that don't have any session the next month
+    $courses = CourseManager::getCoursesWithoutSession($dates['startDate'], $dates['endDate']);
+    createCourseSessions($courses, $administratorId, $dates['startDate'], $dates['endDate']);
+}

+ 24 - 0
main/inc/lib/course.lib.php

@@ -5483,4 +5483,28 @@ class CourseManager
         return $result;
     }
 
+    /**
+     * This function gets all the courses that are not in a session
+     * @param date Start date
+     * @param date End date
+     * @return array Not-in-session courses
+     */
+    public static function getCoursesWithoutSession($startDate = null, $endDate = null)
+    {
+        $dateConditional = ($startDate && $endDate) ?
+            " WHERE id_session IN (SELECT id FROM ".Database::get_main_table(TABLE_MAIN_SESSION).
+            " WHERE date_start = '$startDate' AND date_end = '$endDate')" :
+            null;
+        $query = "SELECT id, code, title FROM ".Database::get_main_table(TABLE_MAIN_COURSE)." WHERE CODE NOT IN
+            (SELECT DISTINCT course_code FROM ".Database::get_main_table(TABLE_MAIN_SESSION_COURSE).$dateConditional.")
+            ORDER BY id";
+
+        $result = Database::query($query);
+        $courses = array();
+        while ($row = Database::fetch_array($result)) {
+            $courses[] = $row;
+        }
+        return $courses;
+    }
+
 }

+ 2 - 2
main/inc/lib/usermanager.lib.php

@@ -3577,7 +3577,7 @@ class UserManager
         $tbl_url_rel_user = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
         $access_url_id = api_get_current_access_url_id();
         if (api_get_multiple_access_url()) {
-            $sql = "SELECT admin.user_id, username, firstname, lastname, email
+            $sql = "SELECT admin.user_id, username, firstname, lastname, email, active
                     FROM $tbl_url_rel_user as url
                     INNER JOIN $table_admin as admin
                     ON (admin.user_id=url.user_id)
@@ -3585,7 +3585,7 @@ class UserManager
                     ON (u.user_id=admin.user_id)
                     WHERE access_url_id ='".$access_url_id."'";
         } else {
-            $sql = "SELECT admin.user_id, username, firstname, lastname, email
+            $sql = "SELECT admin.user_id, username, firstname, lastname, email, active
                     FROM $table_admin as admin
                     INNER JOIN $table_user u
                     ON (u.user_id=admin.user_id)";