move_users.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Script to move users from one course to another if they haven't taken the
  5. * test in the first course.
  6. * This script includes logic and side-effects, which is contrary to PSR-1, but
  7. * it is not considered as a "finished" script to be included in Chamilo.
  8. * Refs BT#8845
  9. */
  10. /**
  11. * Init
  12. */
  13. die('Remove the "die()" line to execute this script'.PHP_EOL);
  14. require __DIR__.'/../../main/inc/global.inc.php';
  15. // Define origin and destination courses' code
  16. $originCourse = 'XYZ2014';
  17. $destinationCourse = 'XYZ2014C2';
  18. // Set to true to only show what it would do
  19. $debug = true;
  20. /**
  21. * Check and move users
  22. */
  23. $output = moveUserFromCourseToCourse($originCourse, $destinationCourse, $debug);
  24. if (empty($output)) {
  25. $output = 'No output';
  26. }
  27. /**
  28. * Output on screen (use in HTTP only for now)
  29. */
  30. echo '<!DOCTYPE html><html lang="en">'.
  31. '<head>'.
  32. '<meta charset="utf-8">'.
  33. '<link href="'.api_get_path(WEB_CODE_PATH).'css/base.css" media="all" rel="stylesheet" type="text/css" />'.
  34. '</head>'.
  35. '<body>'.
  36. $output.
  37. '</body></html>';
  38. /**
  39. * Moves a user from course A to course B "the hard way", by only changing
  40. * the course_rel_user table. This does not remove any data registered in
  41. * course A, as per requirements.
  42. * @param string $originCourse Origin course's code
  43. * @param string $destinationCourse Destination course's code
  44. * @param bool $debug Whether to only show potential action, or to execute them
  45. * @return string Output string
  46. */
  47. function moveUserFromCourseToCourse($originCourse, $destinationCourse, $debug = true)
  48. {
  49. $eol = PHP_EOL;
  50. $output = '';
  51. if (PHP_SAPI != 'cli') {
  52. $eol = "<br />".$eol;
  53. }
  54. if (empty($originCourse)) {
  55. return $output;
  56. } else {
  57. $originCourse = Database::escape_string($originCourse);
  58. }
  59. if (empty($destinationCourse)) {
  60. return $output;
  61. } else {
  62. $destinationCourse = Database::escape_string($destinationCourse);
  63. }
  64. $output .= 'Moving students who have no exe results from course '.$originCourse.' to course '.$destinationCourse.$eol;
  65. $tableCRU = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  66. $tableTEE = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES);
  67. // Get the users who have passed an exam in the course of origin
  68. $sql = "SELECT distinct(exe_user_id) FROM $tableTEE
  69. WHERE exe_cours_id = '$originCourse'";
  70. //AND status != 'incomplete'";
  71. $output .= "$sql".$eol;
  72. $res = Database::query($sql);
  73. $users = array(); // users list in array format
  74. while ($row = Database::fetch_row($res)) {
  75. $users[] = $row[0];
  76. }
  77. // Now get the list of users subscribed to the course of origin
  78. $sql = "SELECT user_id
  79. FROM $tableCRU
  80. WHERE status = ".STUDENT."
  81. AND course_code = '$originCourse'";
  82. $output .= "$sql".$eol;
  83. $res = Database::query($sql);
  84. $numUsers = Database::num_rows($res);
  85. if ($numUsers < 1) {
  86. return $output; //no user registered in first course
  87. }
  88. // Now get the list of users subscribed to the course of origin
  89. $sqlDestination = "SELECT user_id
  90. FROM $tableCRU
  91. WHERE status = ".STUDENT."
  92. AND course_code = '$destinationCourse'";
  93. $output .= "$sqlDestination".$eol;
  94. $resDestination = Database::query($sqlDestination);
  95. $destinationUsers = array();
  96. while ($row = Database::fetch_assoc($resDestination)) {
  97. $destinationUsers[] = $row['user_id'];
  98. }
  99. // List of users with no attempt
  100. $noAttemptUsers = array();
  101. // List of users with an attempt
  102. $attemptUsers = array();
  103. $i = 0;
  104. $output .= '<ul>';
  105. while ($row = Database::fetch_assoc($res)) {
  106. $i++;
  107. // If there are results from
  108. if (in_array($row['user_id'], $users)) {
  109. // This user has already attempted
  110. $u = api_get_user_info($row['user_id']);
  111. $attemptUsers[$row['user_id']] = $u;
  112. $output .= '<li class="muted">';
  113. $output .= $i.' - User '.$u['lastname'].' '.$u['firstname'].' <'.$u['email'].'> has results.';
  114. $output .= '</li>'.PHP_EOL;
  115. } else {
  116. // This user hasn't attempted anything
  117. $u = api_get_user_info($row['user_id']);
  118. if (in_array($row['user_id'], $destinationUsers)) {
  119. $output .= '<li class="muted">';
  120. $output .= $i.' - User '.$u['lastname'].' '.$u['firstname'].' <'.$u['email'].'> has no results but is already in the destination course.'.$eol;
  121. $output .= '</li>'.PHP_EOL;
  122. } else {
  123. $output .= '<li class="">';
  124. $output .= $i.' - User '.$u['lastname'].' '.$u['firstname'].' <'.$u['email'].'> has no results and will be moved.'.$eol;
  125. $noAttemptUsers[$row['user_id']] = $u;
  126. $output .= '</li>'.PHP_EOL;
  127. }
  128. }
  129. }
  130. $output .= '</ul>';
  131. if ($debug) {
  132. return $output;
  133. }
  134. // If not debug mode, execute the move!
  135. $j = 0;
  136. foreach ($noAttemptUsers as $userId => $userInfo) {
  137. // unsubscribe
  138. $sql = "DELETE FROM $tableCRU WHERE course_code = '$originCourse' AND user_id = $userId";
  139. $output .= $sql.$eol;
  140. Database::query($sql);
  141. $sql = "INSERT INTO $tableCRU (course_code, user_id, status)
  142. VALUES ('$destinationCourse', $userId, ".STUDENT.")";
  143. $output .= $sql.$eol;
  144. Database::query($sql);
  145. $j++;
  146. }
  147. $output .= "$j users have been moved".$eol;
  148. return $output;
  149. }