course2session.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This script moves all courses to a session in the past (closing the month before)
  5. * @package chamilo.tests.scripts
  6. */
  7. /**
  8. * Init
  9. */
  10. // comment exit statement before executing
  11. exit;
  12. require __DIR__ . '/../../main/inc/global.inc.php';
  13. $debug = 1;
  14. // Who will be the general coach assigned to all sessions created?
  15. $generalCoach = 'username';
  16. // List of tables that will need an update
  17. $tables = array(
  18. 'c_announcement' => array('c' => 'c_id', 's' => 'session_id'),
  19. 'c_attendance' => array('c' => 'c_id', 's' => 'session_id'),
  20. 'c_blog' => array('c' => 'c_id', 's' => 'session_id'),
  21. 'c_calendar_event' => array('c' => 'c_id', 's' => 'session_id'),
  22. 'c_chat_connected' => array('c' => 'c_id', 's' => 'session_id'),
  23. 'c_course_description' => array('c' => 'c_id', 's' => 'session_id'),
  24. 'c_document' => array('c' => 'c_id', 's' => 'session_id'),
  25. 'c_dropbox_category' => array('c' => 'c_id', 's' => 'session_id'),
  26. 'c_dropbox_file' => array('c' => 'c_id', 's' => 'session_id'),
  27. 'c_dropbox_post' => array('c' => 'c_id', 's' => 'session_id'),
  28. 'c_forum_category' => array('c' => 'c_id', 's' => 'session_id'),
  29. 'c_forum_forum' => array('c' => 'c_id', 's' => 'session_id'),
  30. 'c_forum_thread' => array('c' => 'c_id', 's' => 'session_id'),
  31. 'c_forum_thread_qualify' => array('c' => 'c_id', 's' => 'session_id'),
  32. 'c_forum_thread_qualify_log' => array('c' => 'c_id', 's' => 'session_id'),
  33. 'c_glossary' => array('c' => 'c_id', 's' => 'session_id'),
  34. 'c_group_info' => array('c' => 'c_id', 's' => 'session_id'),
  35. 'c_item_property' => array('c' => 'c_id', 's' => 'id_session'),
  36. 'c_link' => array('c' => 'c_id', 's' => 'session_id'),
  37. 'c_link_category' => array('c' => 'c_id', 's' => 'session_id'),
  38. 'c_lp' => array('c' => 'c_id', 's' => 'session_id'),
  39. 'c_lp_view' => array('c' => 'c_id', 's' => 'session_id'),
  40. 'c_notebook' => array('c' => 'c_id', 's' => 'session_id'),
  41. 'c_quiz' => array('c' => 'c_id', 's' => 'session_id'),
  42. 'c_student_publication' => array('c' => 'c_id', 's' => 'session_id'),
  43. 'c_survey' => array('c' => 'c_id', 's' => 'session_id'),
  44. 'c_survey_invitation' => array('c' => 'c_id', 's' => 'session_id'),
  45. 'c_thematic' => array('c' => 'c_id', 's' => 'session_id'),
  46. 'c_tool' => array('c' => 'c_id', 's' => 'session_id'),
  47. 'c_tool_intro' => array('c' => 'c_id', 's' => 'session_id'),
  48. 'c_wiki' => array('c' => 'c_id', 's' => 'session_id'),
  49. 'c_wiki_mailcue' => array('c' => 'c_id', 's' => 'session_id'),
  50. 'gradebook_category' => array('c' => 'course_code', 's' => 'session_id'),
  51. //'session_rel_course',
  52. //'session_rel_course_rel_user',
  53. //'session_rel_user',
  54. 'track_course_ranking' => array('c' => 'c_id', 's' => 'session_id'),
  55. 'track_e_access' => array('access_cours_code' => 'c_id', 's' => 'access_session_id'),
  56. 'track_e_attempt' => array('c' => 'course_code', 's' => 'session_id'),
  57. 'track_e_course_access' => array('c' => 'course_code', 's' => 'session_id'),
  58. 'track_e_downloads' => array('c' => 'down_cours_id', 's' => 'down_session_id'),
  59. 'track_e_exercices' => array('c' => 'exe_cours_id', 's' => 'session_id'),
  60. 'track_e_item_property' => array('c' => 'course_id', 's' => 'session_id'),
  61. 'track_e_lastaccess' => array('c' => 'access_cours_code', 's' => 'access_session_id'),
  62. 'track_e_links' => array('c' => 'links_cours_id', 's' => 'links_session_id'),
  63. 'track_e_online' => array('c' => 'course', 's' => 'session_id'),
  64. 'track_e_uploads' => array('c' => 'upload_cours_id', 's' => 'upload_session_id'),
  65. 'user_rel_course_vote' => array('c' => 'c_id', 's' => 'session_id'),
  66. );
  67. // Users related tables. From those tables above, only a few have data related
  68. // to users. Other data need not be changed, otherwise the resources will only
  69. // be visible from a specific session.
  70. $userTables = array(
  71. 'c_attendance' => array('c' => 'c_id', 's' => 'session_id'),
  72. 'c_blog' => array('c' => 'c_id', 's' => 'session_id'),
  73. 'c_calendar_event' => array('c' => 'c_id', 's' => 'session_id'),
  74. 'c_chat_connected' => array('c' => 'c_id', 's' => 'session_id'),
  75. 'c_dropbox_category' => array('c' => 'c_id', 's' => 'session_id'),
  76. 'c_dropbox_file' => array('c' => 'c_id', 's' => 'session_id'),
  77. 'c_dropbox_post' => array('c' => 'c_id', 's' => 'session_id'),
  78. 'c_forum_thread' => array('c' => 'c_id', 's' => 'session_id'),
  79. 'c_forum_thread_qualify' => array('c' => 'c_id', 's' => 'session_id'),
  80. 'c_forum_thread_qualify_log' => array('c' => 'c_id', 's' => 'session_id'),
  81. 'c_group_info' => array('c' => 'c_id', 's' => 'session_id'),
  82. 'c_lp_view' => array('c' => 'c_id', 's' => 'session_id'),
  83. 'c_notebook' => array('c' => 'c_id', 's' => 'session_id'),
  84. 'c_student_publication' => array('c' => 'c_id', 's' => 'session_id'),
  85. 'c_wiki' => array('c' => 'c_id', 's' => 'session_id'),
  86. 'c_wiki_mailcue' => array('c' => 'c_id', 's' => 'session_id'),
  87. 'gradebook_category' => array('c' => 'course_code', 's' => 'session_id'),
  88. //'session_rel_course',
  89. //'session_rel_course_rel_user',
  90. //'session_rel_user',
  91. 'track_course_ranking' => array('c' => 'c_id', 's' => 'session_id'),
  92. 'track_e_access' => array('c' => 'c_id', 's' => 'access_session_id'),
  93. 'track_e_attempt' => array('c' => 'c_id', 's' => 'session_id'),
  94. 'track_e_course_access' => array('c' => 'c_id', 's' => 'session_id'),
  95. 'track_e_downloads' => array('c' => 'c_id', 's' => 'down_session_id'),
  96. 'track_e_exercices' => array('c' => 'c_id', 's' => 'session_id'),
  97. 'track_e_item_property' => array('c' => 'course_id', 's' => 'session_id'),
  98. 'track_e_lastaccess' => array('c' => 'c_id', 's' => 'access_session_id'),
  99. 'track_e_links' => array('c' => 'c_id', 's' => 'links_session_id'),
  100. 'track_e_online' => array('c' => 'c_id', 's' => 'session_id'),
  101. 'track_e_uploads' => array('c' => 'upload_cours_id', 's' => 'upload_session_id'),
  102. 'user_rel_course_vote' => array('c' => 'c_id', 's' => 'session_id'),
  103. );
  104. /**
  105. * Create the sessions
  106. * For each existing course, create a session that ends on the last day of the
  107. * past month and starts 2 years before that.
  108. */
  109. $year = date('Y');
  110. $month = date('m');
  111. $end = api_strtotime($year.'-'.$month.'-01 00:00:00') - 1;
  112. $start = $end - (2*365*86400);
  113. // Prepare a list of admin users to avoid removing their relation to the base course
  114. $sql = 'SELECT user_id FROM '.Database::get_main_table(TABLE_MAIN_ADMIN);
  115. $resultAdmin = Database::query($sql);
  116. $admins = array();
  117. while ($row = Database::fetch_assoc($resultAdmin)) {
  118. $admins[] = $row['user_id'];
  119. }
  120. $eol = PHP_EOL;
  121. $eof = PHP_EOL;
  122. if (php_sapi_name() != 'cli') {
  123. echo '<pre>';
  124. $eol = "<br />".PHP_EOL;
  125. $eof = '</pre>';
  126. }
  127. $res = Database::select('id, title, code', Database::get_main_table(TABLE_MAIN_COURSE));
  128. foreach ($res as $course) {
  129. if ($debug) {
  130. echo $course['title'] . $eol;
  131. }
  132. $sessionTitle = $course['title'] . ' (' . $month . '/' . $year . ' - a)';
  133. $startDate = ($year-2) . '-' . $month . '-01';
  134. $endDate = $year . '-' . $month . '-01';
  135. $id = SessionManager::create_session(
  136. $sessionTitle,
  137. $startDate,
  138. $endDate,
  139. 0,
  140. 0,
  141. 0,
  142. $username,
  143. 0,
  144. SESSION_VISIBLE_READ_ONLY
  145. );
  146. while ($id == 'SessionNameAlreadyExists') {
  147. if ($debug) {
  148. echo "Could not create session $sessionTitle" . $eol;
  149. }
  150. // Increase the last letter
  151. $sessionTitle = substr($sessionTitle, 0, -1) . chr(ord(substr($sessionTitle, -1, 1))+1);
  152. $id = SessionManager::create_session(
  153. $sessionTitle,
  154. $startDate,
  155. $endDate,
  156. 0,
  157. 0,
  158. 0,
  159. $username,
  160. 0,
  161. SESSION_VISIBLE_READ_ONLY
  162. );
  163. }
  164. if ($debug) {
  165. echo "Session $sessionTitle created with ID $id" . $eol;
  166. }
  167. SessionManager::add_courses_to_session($id, array($course['id']));
  168. $resultUsers = Database::query("SELECT user_id FROM " . Database::get_main_table(TABLE_MAIN_COURSE_USER). " WHERE c_id = '" . $course['id'] . "'");
  169. $users = array();
  170. while ($row = Database::fetch_assoc($resultUsers)) {
  171. $users[] = $row['user_id'];
  172. }
  173. if ($debug) {
  174. echo count($users) . " users in course " . $course['title'] . " will be moved to session $id (unless they're admins)" . $eol;
  175. }
  176. SessionManager::subscribe_users_to_session_course($users, $id, $course['code']);
  177. foreach ($userTables as $table => $fields) {
  178. $table = Database::get_main_database().'.'.$table;
  179. //c_id + course_id = int, others = char
  180. if ($fields['c'] == 'c_id' or $fields['c'] == 'course_id') {
  181. $sql = "UPDATE $table SET " . $fields['s'] . " = $id WHERE " . $fields['c'] . " = " . $course['id'];
  182. } else {
  183. $sql = "UPDATE $table SET " . $fields['s'] . " = $id WHERE " . $fields['c'] . " = '" . $course['code'] . "'";
  184. }
  185. if ($debug) {
  186. echo $sql . $eol;
  187. }
  188. $resultChange = Database::query($sql);
  189. }
  190. // Now clean up by un-subscribing the user from the course itself manually
  191. // to avoid deleting other stuff
  192. foreach ($users as $user) {
  193. if (in_array($user, $admins)) {
  194. // Skip un-subscribing of admin users
  195. continue;
  196. }
  197. $table = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  198. $sql = "DELETE FROM $table WHERE user_id = $user AND c_id = '" . $course['id'] . "'";
  199. if ($debug) {
  200. echo $sql . $eol;
  201. }
  202. $resultRemove = Database::query($sql);
  203. }
  204. }
  205. if ($debug) {
  206. echo "End of moving process" . $eol;
  207. }
  208. echo $eof;