course2session.php 9.2 KB

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