|
@@ -1,4 +1,5 @@
|
|
|
<?php
|
|
|
+/* For licensing terms, see /license.txt */
|
|
|
/**
|
|
|
* This script moves all courses to a session in the past (closing the month before)
|
|
|
* @package chamilo.tests.scripts
|
|
@@ -6,7 +7,12 @@
|
|
|
/**
|
|
|
* Init
|
|
|
*/
|
|
|
+// comment exit statement before executing
|
|
|
+//exit;
|
|
|
require __DIR__ . '/../../main/inc/global.inc.php';
|
|
|
+
|
|
|
+$debug = 1;
|
|
|
+
|
|
|
// List of tables that will need an update
|
|
|
$tables = array(
|
|
|
'c_announcement' => array('c' => 'c_id', 's' => 'session_id'),
|
|
@@ -58,6 +64,41 @@ $tables = array(
|
|
|
'track_e_uploads' => array('c' => 'upload_cours_id', 's' => 'upload_session_id'),
|
|
|
'user_rel_course_vote' => array('c' => 'c_id', 's' => 'session_id'),
|
|
|
);
|
|
|
+// Users related tables. From those tables above, only a few have data related
|
|
|
+// to users. Other data need not be changed, otherwise the resources will only
|
|
|
+// be visible from a specific session.
|
|
|
+$userTables = array(
|
|
|
+ 'c_attendance' => array('c' => 'c_id', 's' => 'session_id'),
|
|
|
+ 'c_blog' => array('c' => 'c_id', 's' => 'session_id'),
|
|
|
+ 'c_calendar_event' => array('c' => 'c_id', 's' => 'session_id'),
|
|
|
+ 'c_chat_connected' => array('c' => 'c_id', 's' => 'session_id'),
|
|
|
+ 'c_dropbox_category' => array('c' => 'c_id', 's' => 'session_id'),
|
|
|
+ 'c_dropbox_file' => array('c' => 'c_id', 's' => 'session_id'),
|
|
|
+ 'c_dropbox_post' => array('c' => 'c_id', 's' => 'session_id'),
|
|
|
+ 'c_forum_thread' => array('c' => 'c_id', 's' => 'session_id'),
|
|
|
+ 'c_forum_thread_qualify' => array('c' => 'c_id', 's' => 'session_id'),
|
|
|
+ 'c_forum_thread_qualify_log' => array('c' => 'c_id', 's' => 'session_id'),
|
|
|
+ 'c_group_info' => array('c' => 'c_id', 's' => 'session_id'),
|
|
|
+ 'c_lp_view' => array('c' => 'c_id', 's' => 'session_id'),
|
|
|
+ 'c_notebook' => array('c' => 'c_id', 's' => 'session_id'),
|
|
|
+ 'c_student_publication' => array('c' => 'c_id', 's' => 'session_id'),
|
|
|
+ 'c_wiki' => array('c' => 'c_id', 's' => 'session_id'),
|
|
|
+ 'c_wiki_mailcue' => array('c' => 'c_id', 's' => 'session_id'),
|
|
|
+ 'gradebook_category' => array('c' => 'course_code', 's' => 'session_id'),
|
|
|
+ 'track_course_ranking' => array('c' => 'c_id', 's' => 'session_id'),
|
|
|
+ 'track_e_access' => array('access_cours_code' => 'c_id', 's' => 'access_session_id'),
|
|
|
+ 'track_e_attempt' => array('c' => 'course_code', 's' => 'session_id'),
|
|
|
+ 'track_e_course_access' => array('c' => 'course_code', 's' => 'session_id'),
|
|
|
+ 'track_e_downloads' => array('c' => 'down_cours_id', 's' => 'down_session_id'),
|
|
|
+ 'track_e_exercices' => array('c' => 'exe_cours_id', 's' => 'session_id'),
|
|
|
+ 'track_e_item_property' => array('c' => 'course_id', 's' => 'session_id'),
|
|
|
+ 'track_e_lastaccess' => array('c' => 'access_cours_code', 's' => 'access_session_id'),
|
|
|
+ 'track_e_links' => array('c' => 'links_cours_id', 's' => 'links_session_id'),
|
|
|
+ 'track_e_online' => array('c' => 'course', 's' => 'session_id'),
|
|
|
+ 'track_e_uploads' => array('c' => 'upload_cours_id', 's' => 'upload_session_id'),
|
|
|
+ 'user_rel_course_vote' => array('c' => 'c_id', 's' => 'session_id'),
|
|
|
+);
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* Create the sessions
|
|
@@ -69,34 +110,97 @@ $month = date('m');
|
|
|
$end = api_strtotime($year.'-'.$month.'-01 00:00:00') - 1;
|
|
|
$start = $end - (2*365*86400);
|
|
|
|
|
|
+// Prepare a list of admin users to avoid removing their relation to the base course
|
|
|
+$sql = 'SELECT user_id FROM admin';
|
|
|
+$resultAdmin = Database::query($sql);
|
|
|
+$admins = array();
|
|
|
+while ($row = Database::fetch_assoc($resultAdmin)) {
|
|
|
+ $admins[] = $row['user_id'];
|
|
|
+}
|
|
|
+
|
|
|
$res = Database::select('id, title, code', TABLE_MAIN_COURSE);
|
|
|
foreach ($res as $course) {
|
|
|
- echo $course['title'] . PHP_EOL;
|
|
|
- $sessionTitle = $course['title'] . $month . '-' . $year;
|
|
|
- $id = SessionManager::create_session($sessionTitle, $year, $month, 0, $year, $month, 28, 0, 0, 0, 'info@contidosdixitais.com', 0, SESSION_VISIBLE_READ_ONLY);
|
|
|
- if ($id === false) {
|
|
|
- echo "Could not create session $sessionTitle" . PHP_EOL;
|
|
|
+ if ($debug) {
|
|
|
+ echo $course['title'] . PHP_EOL;
|
|
|
+ }
|
|
|
+ $sessionTitle = $course['title'] . ' ' . $month . '-' . $year . ' - a';
|
|
|
+ $id = SessionManager::create_session(
|
|
|
+ $sessionTitle,
|
|
|
+ $year-2,
|
|
|
+ $month,
|
|
|
+ 1,
|
|
|
+ $year,
|
|
|
+ $month,
|
|
|
+ 1,
|
|
|
+ 0,
|
|
|
+ 0,
|
|
|
+ 0,
|
|
|
+ 'info@contidosdixitais.com',
|
|
|
+ 0,
|
|
|
+ SESSION_VISIBLE_READ_ONLY
|
|
|
+ );
|
|
|
+
|
|
|
+ while ($id == 'SessionNameAlreadyExists') {
|
|
|
+ if ($debug) {
|
|
|
+ echo "Could not create session $sessionTitle" . PHP_EOL;
|
|
|
+ }
|
|
|
+ // Increase the last letter
|
|
|
+ $sessionTitle = substr($sessionTitle, 0, -1) . chr(ord(substr($sessionTitle, -1, 1))+1);
|
|
|
+ $id = SessionManager::create_session(
|
|
|
+ $sessionTitle,
|
|
|
+ $year-2,
|
|
|
+ $month,
|
|
|
+ 1,
|
|
|
+ $year,
|
|
|
+ $month,
|
|
|
+ 1,
|
|
|
+ 0,
|
|
|
+ 0,
|
|
|
+ 0,
|
|
|
+ 'info@contidosdixitais.com',
|
|
|
+ 0,
|
|
|
+ SESSION_VISIBLE_READ_ONLY
|
|
|
+ );
|
|
|
+ }
|
|
|
+ if ($debug) {
|
|
|
+ echo "Session $sessionTitle created with ID $id" . PHP_EOL;
|
|
|
}
|
|
|
SessionManager::add_courses_to_session($id, array($course['code']));
|
|
|
- $resultUsers = Database::select('user_id', TABLE_MAIN_COURSE_USER, array('where course_code = ?' => array($course['code'])));
|
|
|
+ $resultUsers = Database::query("SELECT user_id FROM " . Database::get_main_table(TABLE_MAIN_COURSE_USER). " WHERE course_code = '" . $course['code'] . "'");
|
|
|
$users = array();
|
|
|
- foreach ($resultUsers as $user_id) {
|
|
|
- $users[] = $user_id;
|
|
|
+ while ($row = Database::fetch_assoc($resultUsers)) {
|
|
|
+ $users[] = $row['user_id'];
|
|
|
+ }
|
|
|
+ if ($debug) {
|
|
|
+ echo count($users) . " users in course " . $course['title'] . " will be moved to session $id (unless they're admins)" . PHP_EOL;
|
|
|
}
|
|
|
SessionManager::subscribe_users_to_session_course($users, $id, $course['code']);
|
|
|
- foreach ($tables as $table => $fields) {
|
|
|
+ foreach ($userTables as $table => $fields) {
|
|
|
//c_id + course_id = int, others = char
|
|
|
if ($fields['c'] == 'c_id' or $fields['c'] == 'course_id') {
|
|
|
$sql = "UPDATE $table SET " . $fields['s'] . " = $id WHERE " . $fields['c'] . " = " . $course['id'];
|
|
|
} else {
|
|
|
- $sql = "UPDATE $table SET " . $fields['s'] . " = $id WHERE " . $fields['c'] . " = " . $course['code'];
|
|
|
+ $sql = "UPDATE $table SET " . $fields['s'] . " = $id WHERE " . $fields['c'] . " = '" . $course['code'] . "'";
|
|
|
+ }
|
|
|
+ if ($debug) {
|
|
|
+ echo $sql . PHP_EOL;
|
|
|
}
|
|
|
- $resultChange = Database::query($sql);
|
|
|
+ //$resultChange = Database::query($sql);
|
|
|
}
|
|
|
- // Now clean up by unsubscribing the user from the course itself manually
|
|
|
+ // Now clean up by un-subscribing the user from the course itself manually
|
|
|
// to avoid deleting other stuff
|
|
|
foreach ($users as $user) {
|
|
|
- $sql = "DELETE FROM course_rel_user WHERE user_id = $user AND course_code = " . $course['code'];
|
|
|
- $resultRemove = Database::query($sql);
|
|
|
+ if (in_array($user, $admins)) {
|
|
|
+ // Skip un-subscribing of admin users
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ $sql = "DELETE FROM course_rel_user WHERE user_id = $user AND course_code = '" . $course['code'] . "'";
|
|
|
+ if ($debug) {
|
|
|
+ echo $sql . PHP_EOL;
|
|
|
+ }
|
|
|
+ //$resultRemove = Database::query($sql);
|
|
|
}
|
|
|
+}
|
|
|
+if ($debug) {
|
|
|
+ echo "End of moving process" . PH_EOL;
|
|
|
}
|