move_users.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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_EXERCISES);
  67. $courseId = api_get_course_int_id($originCourse);
  68. // Get the users who have passed an exam in the course of origin
  69. $sql = "SELECT distinct(exe_user_id) FROM $tableTEE
  70. WHERE c_id = $courseId";
  71. //AND status != 'incomplete'";
  72. $output .= "$sql".$eol;
  73. $res = Database::query($sql);
  74. $users = array(); // users list in array format
  75. while ($row = Database::fetch_row($res)) {
  76. $users[] = $row[0];
  77. }
  78. // Now get the list of users subscribed to the course of origin
  79. $sql = "SELECT user_id
  80. FROM $tableCRU
  81. WHERE status = ".STUDENT."
  82. AND c_id = '$courseId'";
  83. $output .= "$sql".$eol;
  84. $res = Database::query($sql);
  85. $numUsers = Database::num_rows($res);
  86. if ($numUsers < 1) {
  87. return $output; //no user registered in first course
  88. }
  89. // Now get the list of users subscribed to the course of origin
  90. $sqlDestination = "SELECT user_id
  91. FROM $tableCRU
  92. WHERE status = ".STUDENT."
  93. AND course_code = '$destinationCourse'";
  94. $output .= "$sqlDestination".$eol;
  95. $resDestination = Database::query($sqlDestination);
  96. $destinationUsers = array();
  97. while ($row = Database::fetch_assoc($resDestination)) {
  98. $destinationUsers[] = $row['user_id'];
  99. }
  100. // List of users with no attempt
  101. $noAttemptUsers = array();
  102. // List of users with an attempt
  103. $attemptUsers = array();
  104. $i = 0;
  105. $output .= '<ul>';
  106. while ($row = Database::fetch_assoc($res)) {
  107. $i++;
  108. // If there are results from
  109. if (in_array($row['user_id'], $users)) {
  110. // This user has already attempted
  111. $u = api_get_user_info($row['user_id']);
  112. $attemptUsers[$row['user_id']] = $u;
  113. $output .= '<li class="muted">';
  114. $output .= $i.' - User '.$u['lastname'].' '.$u['firstname'].' <'.$u['email'].'> has results.';
  115. $output .= '</li>'.PHP_EOL;
  116. } else {
  117. // This user hasn't attempted anything
  118. $u = api_get_user_info($row['user_id']);
  119. if (in_array($row['user_id'], $destinationUsers)) {
  120. $output .= '<li class="muted">';
  121. $output .= $i.' - User '.$u['lastname'].' '.$u['firstname'].' <'.$u['email'].'> has no results but is already in the destination course.'.$eol;
  122. $output .= '</li>'.PHP_EOL;
  123. } else {
  124. $output .= '<li class="">';
  125. $output .= $i.' - User '.$u['lastname'].' '.$u['firstname'].' <'.$u['email'].'> has no results and will be moved.'.$eol;
  126. $noAttemptUsers[$row['user_id']] = $u;
  127. $output .= '</li>'.PHP_EOL;
  128. }
  129. }
  130. }
  131. $output .= '</ul>';
  132. if ($debug) {
  133. return $output;
  134. }
  135. // If not debug mode, execute the move!
  136. $j = 0;
  137. foreach ($noAttemptUsers as $userId => $userInfo) {
  138. // unsubscribe
  139. $sql = "DELETE FROM $tableCRU WHERE course_code = '$originCourse' AND user_id = $userId";
  140. $output .= $sql.$eol;
  141. Database::query($sql);
  142. $sql = "INSERT INTO $tableCRU (course_code, user_id, status)
  143. VALUES ('$destinationCourse', $userId, ".STUDENT.")";
  144. $output .= $sql.$eol;
  145. Database::query($sql);
  146. $j++;
  147. }
  148. $output .= "$j users have been moved".$eol;
  149. return $output;
  150. }