diff --git a/tests/scripts/move_users.php b/tests/scripts/move_users.php new file mode 100644 index 0000000000..6c4c7d3daf --- /dev/null +++ b/tests/scripts/move_users.php @@ -0,0 +1,153 @@ +'. + ''. + ''. + ''. + ''. + ''. + $output. + ''; + +/** + * Moves a user from course A to course B "the hard way", by only changing + * the course_rel_user table. This does not remove any data registered in + * course A, as per requirements. + * @param string $originCourse Origin course's code + * @param string $destinationCourse Destination course's code + * @param bool $debug Whether to only show potential action, or to execute them + * @return string Output string + */ +function moveUserFromCourseToCourse($originCourse, $destinationCourse, $debug = true) +{ + $eol = PHP_EOL; + $output = ''; + if (PHP_SAPI != 'cli') { + $eol = "
".$eol; + } + + if (empty($originCourse)) { + return $output; + } else { + $originCourse = Database::escape_string($originCourse); + } + if (empty($destinationCourse)) { + return $output; + } else { + $destinationCourse = Database::escape_string($destinationCourse); + } + $output .= 'Moving students who have no exe results from course '.$originCourse.' to course '.$destinationCourse.$eol; + $tableCRU = Database::get_main_table(TABLE_MAIN_COURSE_USER); + $tableTEE = Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES); + // Get the users who have passed an exam in the course of origin + $sql = "SELECT distinct(exe_user_id) FROM $tableTEE + WHERE exe_cours_id = '$originCourse'"; + //AND status != 'incomplete'"; + $output .= "$sql".$eol; + $res = Database::query($sql); + $users = array(); // users list in array format + while ($row = Database::fetch_row($res)) { + $users[] = $row[0]; + } + // Now get the list of users subscribed to the course of origin + $sql = "SELECT user_id + FROM $tableCRU + WHERE status = ".STUDENT." + AND course_code = '$originCourse'"; + $output .= "$sql".$eol; + $res = Database::query($sql); + $numUsers = Database::num_rows($res); + if ($numUsers < 1) { + return $output; //no user registered in first course + } + // Now get the list of users subscribed to the course of origin + $sqlDestination = "SELECT user_id + FROM $tableCRU + WHERE status = ".STUDENT." + AND course_code = '$destinationCourse'"; + $output .= "$sqlDestination".$eol; + $resDestination = Database::query($sqlDestination); + $destinationUsers = array(); + while ($row = Database::fetch_assoc($resDestination)) { + $destinationUsers[] = $row['user_id']; + } + + // List of users with no attempt + $noAttemptUsers = array(); + // List of users with an attempt + $attemptUsers = array(); + $i = 0; + $output .= ''; + if ($debug) { + return $output; + } + // If not debug mode, execute the move! + $j = 0; + foreach ($noAttemptUsers as $userId => $userInfo) { + // unsubscribe + $sql = "DELETE FROM $tableCRU WHERE course_code = '$originCourse' AND user_id = $userId"; + $output .= $sql.$eol; + Database::query($sql); + $sql = "INSERT INTO $tableCRU (course_code, user_id, status) + VALUES ('$destinationCourse', $userId, ".STUDENT.")"; + $output .= $sql.$eol; + Database::query($sql); + $j++; + } + $output .= "$j users have been moved".$eol; + return $output; +}