Updated the 19 build version to 20101023
[moodle.git] / admin / mnet / enr_course_enrol.php
blob1bb20d68361649ed1dca17d4942f49d7e809bf86
1 <?PHP // $Id$
2 // enrol_config.php - allows admin to edit all enrollment variables
3 // Yes, enrol is correct English spelling.
5 require_once(dirname(__FILE__) . "/../../config.php");
6 require_once($CFG->libdir.'/adminlib.php');
7 include_once($CFG->dirroot.'/mnet/xmlrpc/client.php');
9 if (!confirm_sesskey()) {
10 print_error('confirmsesskeybad', 'error');
13 admin_externalpage_setup('mnetenrol');
14 $CFG->pagepath = 'admin/mnet';
16 require_once("$CFG->dirroot/enrol/enrol.class.php"); /// Open the factory class
17 $enrolment = enrolment_factory::factory('mnet');
19 $mnethostid = required_param('host', PARAM_INT);
20 $courseid = required_param('courseid', PARAM_INT);
22 $mnet_peer = new mnet_peer();
23 if (!$mnet_peer->set_id($mnethostid)) {
24 print_error('hostcoursenotfound','mnet');
27 $course = get_record('mnet_enrol_course', 'id', $courseid, 'hostid', $mnet_peer->id);
29 if (empty($course)) {
30 print_error('hostcoursenotfound','mnet');
33 define("MAX_USERS_PER_PAGE", 5000);
35 $add = optional_param('add', 0, PARAM_BOOL);
36 $remove = optional_param('remove', 0, PARAM_BOOL);
37 $showall = optional_param('showall', 0, PARAM_BOOL);
38 $searchtext = optional_param('searchtext', '', PARAM_RAW); // search string
39 $previoussearch = optional_param('previoussearch', 0, PARAM_BOOL);
40 $userid = optional_param('userid', 0, PARAM_INT); // needed for user tabs
42 $errors = array();
44 $previoussearch = ($searchtext != '') or ($previoussearch) ? 1:0;
46 $baseurl = "remote_enrolment.php?courseid={$course->id}&amp;host={$mnet_peer->id}";
47 if (!empty($userid)) {
48 $baseurl .= '&amp;userid='.$userid;
51 /// Process incoming role assignment
53 if ($frm = data_submitted()) {
54 if ($add and !empty($frm->addselect) and confirm_sesskey()) {
55 $timemodified = time();
57 foreach ($frm->addselect as $adduser) {
58 if (!$adduser = clean_param($adduser, PARAM_INT)) {
59 continue;
61 if (! $enrolment->req_enrol_user($adduser, $course->id)) {
62 $errors[] = "Could not add user with id $adduser to course {$course->id}!";
65 } else if ($remove and !empty($frm->removeselect) and confirm_sesskey()) {
66 foreach ($frm->removeselect as $removeuser) {
67 $removeuser = clean_param($removeuser, PARAM_INT);
68 if (! $enrolment->req_unenrol_user($removeuser, $course->id)) {
69 $errors[] = "Could not remove user with id $removeuser from course {$course->id}!";
72 } else if ($showall) {
73 $searchtext = '';
74 $previoussearch = 0;
78 /// Prepare data for users / enrolled users panes
81 /// Create a new request object
82 $mnet_request = new mnet_xmlrpc_client();
84 /// Pass it the path to the method that we want to execute
85 $mnet_request->set_method('enrol/mnet/enrol.php/course_enrolments');
86 $mnet_request->add_param($course->remoteid, 'int');
87 $mnet_request->send($mnet_peer);
88 $raw_all_enrolled_users = $mnet_request->response;
89 unset($mnet_request);
91 $all_enrolled_users = array();
92 if (!empty($raw_all_enrolled_users)) {
93 // Try to repair keying of remote users array, numeric usernames get lost in the fracas
94 foreach ($raw_all_enrolled_users as $username => $userdetails) {
95 if (empty($userdetails['username']) || !is_numeric($username)) {
96 //Not able to repair, or no need to repair
97 $all_enrolled_users[$username] = $userdetails;
98 } else {
99 $all_enrolled_users[$userdetails['username']] = $userdetails;
104 $select = '';
105 $all_enrolled_usernames = '';
106 $timemodified = array();
107 /// List all the users (homed on this server) who are enrolled on the course
108 /// This will include mnet-enrolled users, and those who have enrolled
109 /// themselves, etc.
110 if (is_array($all_enrolled_users) && count($all_enrolled_users)) {
111 foreach($all_enrolled_users as $username => $data) {
112 $all_enrolled_usernames .= "'$username', ";
114 $select = ' u.username IN (' .substr($all_enrolled_usernames, 0, -2) .') AND ';
115 } else {
116 $all_enrolled_users = array();
119 /// Synch our mnet_enrol_assignments with remote server
120 $sql = "
121 SELECT
122 u.id,
123 u.firstname,
124 u.lastname,
125 u.username,
126 u.email,
127 a.enroltype,
128 a.id as enrolid,
129 COALESCE(a.hostid, 0) as wehaverecord,
130 a.courseid
131 FROM
132 {$CFG->prefix}user u
133 JOIN
134 {$CFG->prefix}mnet_enrol_assignments a
136 a.userid = u.id AND a.courseid={$courseid}
137 WHERE
138 $select
139 u.deleted = 0 AND
140 u.confirmed = 1 AND
141 u.mnethostid = {$CFG->mnet_localhost_id}
142 ORDER BY
143 u.firstname ASC,
144 u.lastname ASC";
146 if (!$enrolledusers = get_records_sql($sql)) {
147 $enrolledusers = array();
150 foreach($enrolledusers as $user) {
152 $dataobj = new stdClass();
153 $dataobj->userid = $user->id;
154 $dataobj->hostid = $mnet_peer->id;
155 $dataobj->courseid = $courseid;
156 $dataobj->rolename = $all_enrolled_users[$user->username]['name'];
157 $dataobj->enroltype = $all_enrolled_users[$user->username]['enrol'];
159 if ($user->wehaverecord == 0) {
160 $dataobj->enroltime = $all_enrolled_users[$user->username]['timemodified'];
161 $dataobj->id = insert_record('mnet_enrol_assignments', $dataobj);
162 } elseif (array_key_exists($user->username, $all_enrolled_users)) {
163 $dataobj->id = $user->enrolid;
164 update_record('mnet_enrol_assignments', $dataobj);
165 } elseif (is_array($all_enrolled_users) && count($all_enrolled_users)) {
166 delete_record('mnet_enrol_assignments', 'id', $user->enrolid);
169 unset($enrolledusers);
171 // Read about our remote enrolments in 2 sets
172 // first, get the remote enrolments done via enrol/mnet $mnetenrolledusers
173 // second, get the remote enrolments done with other plugins $remtenrolledusers
174 // NOTE: both arrays are keyed on the userid!
175 $sql = "
176 SELECT
177 u.id,
178 u.firstname,
179 u.lastname,
180 a.rolename,
181 a.enroltype,
182 a.courseid
183 FROM
184 {$CFG->prefix}user u,
185 {$CFG->prefix}mnet_enrol_assignments a
186 WHERE
187 a.userid = u.id AND
188 a.courseid={$courseid} AND
189 a.enroltype = 'mnet' AND
190 u.deleted = 0 AND
191 u.confirmed = 1 AND
192 u.mnethostid = {$CFG->mnet_localhost_id}
193 ORDER BY
194 u.firstname ASC,
195 u.lastname ASC";
197 if (!$mnetenrolledusers = get_records_sql($sql)) {
198 $mnetenrolledusers = array();
200 $sql = "
201 SELECT
202 u.id,
203 u.firstname,
204 u.lastname,
205 a.rolename,
206 a.enroltype,
207 a.courseid
208 FROM
209 {$CFG->prefix}user u,
210 {$CFG->prefix}mnet_enrol_assignments a
211 WHERE
212 a.userid = u.id AND
213 a.courseid={$courseid} AND
214 a.enroltype != 'mnet' AND
215 u.deleted = 0 AND
216 u.confirmed = 1 AND
217 u.mnethostid = {$CFG->mnet_localhost_id}
218 ORDER BY
219 u.firstname ASC,
220 u.lastname ASC";
222 if (!$remtenrolledusers = get_records_sql($sql)) {
223 $remtenrolledusers = array();
226 $select = '';
227 $exclude = array_merge(array_keys($mnetenrolledusers), array_keys($remtenrolledusers));
228 $exclude[] = 0;
229 $select = 'AND u.username!=\'guest\' AND u.id NOT IN ('. join(',',$exclude) .') ';
230 unset($exclude);
232 $searchtext = trim($searchtext);
234 if ($searchtext !== '') { // Search for a subset of remaining users
235 $LIKE = sql_ilike();
236 $FULLNAME = sql_fullname();
238 $select .= " AND ($FULLNAME $LIKE '%$searchtext%' OR email $LIKE '%$searchtext%') ";
241 $sql = ('SELECT id, firstname, lastname, email
242 FROM '.$CFG->prefix.'user u
243 WHERE deleted = 0 AND confirmed = 1
244 AND mnethostid = '.$CFG->mnet_localhost_id.' '
245 .$select
246 .'ORDER BY lastname ASC, firstname ASC');
248 $availableusers = get_recordset_sql($sql, 0, MAX_USERS_PER_PAGE);
252 /// Print the page
254 /// get language strings
255 $str = get_strings(array('enrolmentplugins', 'configuration', 'users', 'administration'));
256 /// Get some language strings
258 $strpotentialusers = get_string('potentialusers', 'role');
259 $strexistingusers = get_string('existingusers', 'role');
260 $straction = get_string('assignroles', 'role');
261 $strroletoassign = get_string('roletoassign', 'role');
262 $strcurrentcontext = get_string('currentcontext', 'role');
263 $strsearch = get_string('search');
264 $strshowall = get_string('showall');
265 $strparticipants = get_string('participants');
266 $strsearchresults = get_string('searchresults');
268 admin_externalpage_print_header();
270 print_box('<strong>' . s($mnet_peer->name) . ' : '
271 . format_string($course->shortname) .' '. format_string($course->fullname)
272 . '</strong><br />'
273 . get_string("enrolcourseenrol_desc", "mnet"));
275 echo "<hr />";
277 include(dirname(__FILE__).'/enr_course_enrol.html');
279 if (!empty($errors)) {
280 $msg = '<p>';
281 foreach ($errors as $e) {
282 $msg .= $e.'<br />';
284 $msg .= '</p>';
285 notify($msg);
289 admin_externalpage_print_footer();