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
);
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
44 $previoussearch = ($searchtext != '') or ($previoussearch) ?
1:0;
46 $baseurl = "remote_enrolment.php?courseid={$course->id}&host={$mnet_peer->id}";
47 if (!empty($userid)) {
48 $baseurl .= '&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
)) {
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) {
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
;
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;
99 $all_enrolled_users[$userdetails['username']] = $userdetails;
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
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 ';
116 $all_enrolled_users = array();
119 /// Synch our mnet_enrol_assignments with remote server
129 COALESCE(a.hostid, 0) as wehaverecord,
134 {$CFG->prefix}mnet_enrol_assignments a
136 a.userid = u.id AND a.courseid={$courseid}
141 u.mnethostid = {$CFG->mnet_localhost_id}
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!
184 {$CFG->prefix}user u,
185 {$CFG->prefix}mnet_enrol_assignments a
188 a.courseid={$courseid} AND
189 a.enroltype = 'mnet' AND
192 u.mnethostid = {$CFG->mnet_localhost_id}
197 if (!$mnetenrolledusers = get_records_sql($sql)) {
198 $mnetenrolledusers = array();
209 {$CFG->prefix}user u,
210 {$CFG->prefix}mnet_enrol_assignments a
213 a.courseid={$courseid} AND
214 a.enroltype != 'mnet' AND
217 u.mnethostid = {$CFG->mnet_localhost_id}
222 if (!$remtenrolledusers = get_records_sql($sql)) {
223 $remtenrolledusers = array();
227 $exclude = array_merge(array_keys($mnetenrolledusers), array_keys($remtenrolledusers));
229 $select = 'AND u.username!=\'guest\' AND u.id NOT IN ('. join(',',$exclude) .') ';
232 $searchtext = trim($searchtext);
234 if ($searchtext !== '') { // Search for a subset of remaining users
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
.' '
246 .'ORDER BY lastname ASC, firstname ASC');
248 $availableusers = get_recordset_sql($sql, 0, MAX_USERS_PER_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
)
273 . get_string("enrolcourseenrol_desc", "mnet"));
277 include(dirname(__FILE__
).'/enr_course_enrol.html');
279 if (!empty($errors)) {
281 foreach ($errors as $e) {
289 admin_externalpage_print_footer();