Skip to content

Commit f52df87

Browse files
committed
Add script to move users from one course to another depending on them having passed an exam or not - refs BT#8845
1 parent 78818df commit f52df87

File tree

1 file changed

+153
-0
lines changed

1 file changed

+153
-0
lines changed

tests/scripts/move_users.php

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

0 commit comments

Comments
 (0)