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 $all_enrolled_users = $mnet_request->response
;
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
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 ';
104 $all_enrolled_users = array();
107 /// Synch our mnet_enrol_assignments with remote server
117 COALESCE(a.hostid, 0) as wehaverecord,
122 {$CFG->prefix}mnet_enrol_assignments a
124 a.userid = u.id AND a.courseid={$courseid}
129 u.mnethostid = {$CFG->mnet_localhost_id}
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!
172 {$CFG->prefix}user u,
173 {$CFG->prefix}mnet_enrol_assignments a
176 a.courseid={$courseid} AND
177 a.enroltype = 'mnet' AND
180 u.mnethostid = {$CFG->mnet_localhost_id}
185 if (!$mnetenrolledusers = get_records_sql($sql)) {
186 $mnetenrolledusers = array();
197 {$CFG->prefix}user u,
198 {$CFG->prefix}mnet_enrol_assignments a
201 a.courseid={$courseid} AND
202 a.enroltype != 'mnet' AND
205 u.mnethostid = {$CFG->mnet_localhost_id}
210 if (!$remtenrolledusers = get_records_sql($sql)) {
211 $remtenrolledusers = array();
215 $exclude = array_merge(array_keys($mnetenrolledusers), array_keys($remtenrolledusers));
217 $select = 'AND u.username!=\'guest\' AND u.id NOT IN ('. join(',',$exclude) .') ';
220 $searchtext = trim($searchtext);
222 if ($searchtext !== '') { // Search for a subset of remaining users
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
.' '
234 .'ORDER BY lastname ASC, firstname ASC');
236 $availableusers = get_recordset_sql($sql, 0, MAX_USERS_PER_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
)
261 . get_string("enrolcourseenrol_desc", "mnet"));
265 include(dirname(__FILE__
).'/enr_course_enrol.html');
267 if (!empty($errors)) {
269 foreach ($errors as $e) {
277 admin_externalpage_print_footer();