Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / admin / mnet / enr_course_enrol.php
blob3cbd93db4a45c4f19a01b08b5faa730cbf28ec9b
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 $all_enrolled_users = $mnet_request->response;
90 unset($mnet_request);
92 $select = '';
93 $all_enrolled_usernames = '';
94 $timemodified = array();
95 /// List all the users (homed on this server) who are enrolled on the course
96 /// This will include mnet-enrolled users, and those who have enrolled
97 /// themselves, etc.
98 if (is_array($all_enrolled_users) && count($all_enrolled_users)) {
99 foreach($all_enrolled_users as $username => $data) {
100 $all_enrolled_usernames .= "'$username', ";
102 $select = ' u.username IN (' .substr($all_enrolled_usernames, 0, -2) .') AND ';
103 } else {
104 $all_enrolled_users = array();
107 /// Synch our mnet_enrol_assignments with remote server
108 $sql = "
109 SELECT
110 u.id,
111 u.firstname,
112 u.lastname,
113 u.username,
114 u.email,
115 a.enroltype,
116 a.id as enrolid,
117 COALESCE(a.hostid, 0) as wehaverecord,
118 a.courseid
119 FROM
120 {$CFG->prefix}user u
121 LEFT JOIN
122 {$CFG->prefix}mnet_enrol_assignments a
124 a.userid = u.id AND a.courseid={$courseid}
125 WHERE
126 $select
127 u.deleted = 0 AND
128 u.confirmed = 1 AND
129 u.mnethostid = {$CFG->mnet_localhost_id}
130 ORDER BY
131 u.firstname ASC,
132 u.lastname ASC";
134 if (!$enrolledusers = get_records_sql($sql)) {
135 $enrolledusers = array();
138 foreach($enrolledusers as $user) {
140 $dataobj = new stdClass();
141 $dataobj->userid = $user->id;
142 $dataobj->hostid = $mnet_peer->id;
143 $dataobj->courseid = $courseid;
144 $dataobj->rolename = $all_enrolled_users[$user->username]['name'];
145 $dataobj->enroltype = $all_enrolled_users[$user->username]['enrol'];
147 if ($user->wehaverecord == 0) {
148 $dataobj->enroltime = $all_enrolled_users[$user->username]['timemodified'];
149 $dataobj->id = insert_record('mnet_enrol_assignments', $dataobj);
150 } elseif (array_key_exists($user->username, $all_enrolled_users)) {
151 $dataobj->id = $user->enrolid;
152 update_record('mnet_enrol_assignments', $dataobj);
153 } elseif (is_array($all_enrolled_users) && count($all_enrolled_users)) {
154 delete_record('mnet_enrol_assignments', 'id', $user->enrolid);
157 unset($enrolledusers);
159 // Read about our remote enrolments in 2 sets
160 // first, get the remote enrolments done via enrol/mnet $mnetenrolledusers
161 // second, get the remote enrolments done with other plugins $remtenrolledusers
162 // NOTE: both arrays are keyed on the userid!
163 $sql = "
164 SELECT
165 u.id,
166 u.firstname,
167 u.lastname,
168 a.rolename,
169 a.enroltype,
170 a.courseid
171 FROM
172 {$CFG->prefix}user u,
173 {$CFG->prefix}mnet_enrol_assignments a
174 WHERE
175 a.userid = u.id AND
176 a.courseid={$courseid} AND
177 a.enroltype = 'mnet' AND
178 u.deleted = 0 AND
179 u.confirmed = 1 AND
180 u.mnethostid = {$CFG->mnet_localhost_id}
181 ORDER BY
182 u.firstname ASC,
183 u.lastname ASC";
185 if (!$mnetenrolledusers = get_records_sql($sql)) {
186 $mnetenrolledusers = array();
188 $sql = "
189 SELECT
190 u.id,
191 u.firstname,
192 u.lastname,
193 a.rolename,
194 a.enroltype,
195 a.courseid
196 FROM
197 {$CFG->prefix}user u,
198 {$CFG->prefix}mnet_enrol_assignments a
199 WHERE
200 a.userid = u.id AND
201 a.courseid={$courseid} AND
202 a.enroltype != 'mnet' AND
203 u.deleted = 0 AND
204 u.confirmed = 1 AND
205 u.mnethostid = {$CFG->mnet_localhost_id}
206 ORDER BY
207 u.firstname ASC,
208 u.lastname ASC";
210 if (!$remtenrolledusers = get_records_sql($sql)) {
211 $remtenrolledusers = array();
214 $select = '';
215 $exclude = array_merge(array_keys($mnetenrolledusers), array_keys($remtenrolledusers));
216 $exclude[] = 0;
217 $select = 'AND u.username!=\'guest\' AND u.id NOT IN ('. join(',',$exclude) .') ';
218 unset($exclude);
220 $searchtext = trim($searchtext);
222 if ($searchtext !== '') { // Search for a subset of remaining users
223 $LIKE = sql_ilike();
224 $FULLNAME = sql_fullname();
226 $select .= " AND ($FULLNAME $LIKE '%$searchtext%' OR email $LIKE '%$searchtext%') ";
229 $sql = ('SELECT id, firstname, lastname, email
230 FROM '.$CFG->prefix.'user u
231 WHERE deleted = 0 AND confirmed = 1
232 AND mnethostid = '.$CFG->mnet_localhost_id.' '
233 .$select
234 .'ORDER BY lastname ASC, firstname ASC');
236 $availableusers = get_recordset_sql($sql, 0, MAX_USERS_PER_PAGE);
240 /// Print the page
242 /// get language strings
243 $str = get_strings(array('enrolmentplugins', 'configuration', 'users', 'administration'));
244 /// Get some language strings
246 $strpotentialusers = get_string('potentialusers', 'role');
247 $strexistingusers = get_string('existingusers', 'role');
248 $straction = get_string('assignroles', 'role');
249 $strroletoassign = get_string('roletoassign', 'role');
250 $strcurrentcontext = get_string('currentcontext', 'role');
251 $strsearch = get_string('search');
252 $strshowall = get_string('showall');
253 $strparticipants = get_string('participants');
254 $strsearchresults = get_string('searchresults');
256 admin_externalpage_print_header();
258 print_box('<strong>' . s($mnet_peer->name) . ' : '
259 . format_string($course->shortname) .' '. format_string($course->fullname)
260 . '</strong><br />'
261 . get_string("enrolcourseenrol_desc", "mnet"));
263 echo "<hr />";
265 include(dirname(__FILE__).'/enr_course_enrol.html');
267 if (!empty($errors)) {
268 $msg = '<p>';
269 foreach ($errors as $e) {
270 $msg .= $e.'<br />';
272 $msg .= '</p>';
273 notify($msg);
277 admin_externalpage_print_footer();