Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / enrol / mnet / enrol.php
blob4cffd613de538f837f888e325994b5489da54532
1 <?php
2 // The following flags are set in the configuration
3 // $config->allow_allcourses: expose all courses to external enrolment
4 // $config->allowed_categories: serialised array of courses allowed
5 // $config->allowed_courses: serialised array of courses allowed
7 class enrolment_plugin_mnet {
9 /// Override the base config_form() function
10 function config_form($frm) {
11 global $CFG;
13 $vars = array('enrol_mnet_allow_allcourses',
14 'enrol_mnet_allowed_categories',
15 'enrol_mnet_allowed_courses');
17 foreach ($vars as $var) {
18 if (!isset($frm->$var)) {
19 $frm->$var = '';
23 $mnethosts = $this->list_remote_servers();
25 include ("$CFG->dirroot/enrol/mnet/config.html");
29 /// Override the base process_config() function
30 function process_config($config) {
32 if (!isset($config->enrol_mnet_allow_allcourses)) {
33 $config->enrol_mnet_allow_allcourses = false;
35 set_config('enrol_mnet_allow_allcourses', $config->enrol_mnet_allow_allcourses);
37 if (!isset($config->enrol_mnet_allowed_categories)) {
38 $config->enrol_mnet_allowed_categories = '';
40 set_config('enrol_mnet_allowed_categories', $config->enrol_mnet_allowed_categories);
42 if (!isset($config->enrol_mnet_allowed_courses)) {
43 $config->enrol_mnet_allowed_courses = '';
45 set_config('enrol_mnet_allowed_courses', $config->enrol_mnet_allowed_courses);
47 return true;
51 /// Override the get_access_icons() function
52 function get_access_icons($course) {
55 /**
56 * Override the base cron() function
58 //function cron() {
60 //} // end of cron()
64 /***
65 *** MNET functions
66 ***
67 ***/
68 function mnet_publishes() {
70 $enrol = array();
71 $enrol['name'] = 'mnet_enrol'; // Name & Description go in lang file
72 $enrol['apiversion'] = 1;
73 $enrol['methods'] = array('available_courses','user_enrolments', 'enrol_user', 'unenrol_user', 'course_enrolments' );
75 return array($enrol);
78 /**
79 * Does Foo
81 * @param string $username The username
82 * @param int $mnethostid The id of the remote mnethost
83 * @return bool Whether the user can login from the remote host
85 function available_courses() {
86 global $CFG;
88 if (!empty($CFG->enrol_mnet_allow_allcourses)) {
90 $query =
91 "SELECT
92 co.id AS remoteid,
93 ca.id AS cat_id,
94 ca.name AS cat_name,
95 ca.description AS cat_description,
96 co.sortorder,
97 co.fullname,
98 co.shortname,
99 co.idnumber,
100 co.summary,
101 co.startdate,
102 co.cost,
103 co.currency,
104 co.defaultrole AS defaultroleid,
105 r.name AS defaultrolename
106 FROM
107 {$CFG->prefix}course_categories ca
108 JOIN
109 {$CFG->prefix}course co ON
110 ca.id = co.category
111 LEFT JOIN
112 {$CFG->prefix}role r ON
113 r.id = co.defaultrole
114 WHERE
115 co.visible = '1' AND
116 co.enrollable = '1'
117 ORDER BY
118 sortorder ASC
121 return get_records_sql($query);
123 } elseif (!empty($CFG->enrol_mnet_allowed_categories)) {
125 $cats = preg_split('/\s*,\s*/', $CFG->enrol_mnet_allowed_categories);
126 for ($n=0;$n < count($cats); $n++) {
127 $cats[$n] = " ca.path LIKE '%/" . (int)$cats[$n] . "/%' ";
129 $cats = join(' OR ', $cats);
131 $query =
132 "SELECT
133 id, name
134 FROM
135 {$CFG->prefix}course_categories ca
136 WHERE
137 ca.id IN ({$CFG->enrol_mnet_allowed_categories})
138 OR ( $cats )
139 ORDER BY
140 path ASC,
141 depth ASC
143 unset($cats);
145 $rs = get_records_sql($query);
147 if (!empty($rs)) {
148 $cats = array_keys($rs);
150 $where = ' AND ( ca.id IN (' . join(',', $cats) . ') ';
153 if (!empty($CFG->enrol_mnet_allowed_courses)) {
154 $where .= " OR co.id in ('{$CFG->enrol_mnet_allowed_courses}') ";
157 $where .= ')';
159 $query =
160 "SELECT
161 co.id as remoteid,
162 ca.id as cat_id,
163 ca.name as cat_name,
164 ca.description as cat_description,
165 co.sortorder,
166 co.fullname,
167 co.shortname,
168 co.idnumber,
169 co.summary,
170 co.startdate,
171 co.cost,
172 co.currency,
173 co.defaultrole as defaultroleid,
174 r.name
175 FROM
176 {$CFG->prefix}course_categories ca
177 JOIN
178 {$CFG->prefix}course co ON
179 ca.id = co.category
180 LEFT JOIN
181 {$CFG->prefix}role r ON
182 r.id = co.defaultrole
183 WHERE
184 co.visible = '1' AND
185 co.enrollable = '1' $where
186 ORDER BY
187 sortorder ASC
190 return get_records_sql($query);
192 } elseif (!empty($CFG->enrol_mnet_allowed_courses)) {
194 $query =
195 "SELECT
196 co.id as remoteid,
197 ca.id as cat_id,
198 ca.name as cat_name,
199 ca.description as cat_description,
200 co.sortorder,
201 co.fullname,
202 co.shortname,
203 co.idnumber,
204 co.summary,
205 co.startdate,
206 co.cost,
207 co.currency,
208 co.defaultrole as defaultroleid,
209 r.name
210 FROM
211 {$CFG->prefix}course_categories ca
212 JOIN
213 {$CFG->prefix}course co ON
214 ca.id = co.category
215 LEFT JOIN
216 {$CFG->prefix}role r ON
217 r.id = co.defaultrole
218 WHERE
219 co.visible = '1' AND
220 co.enrollable = '1' AND
221 co.id in ({$CFG->enrol_mnet_allowed_courses})
222 ORDER BY
223 sortorder ASC
226 return get_records_sql($query);
230 return array();
236 function user_enrolments($userid) {
237 return array();
241 * Get a list of users from the client server who are enrolled in a course
243 * @param int $courseid The Course ID
244 * @param string $roles Comma-separated list of role shortnames
245 * @return array Array of usernames who are homed on the
246 * client machine
248 function course_enrolments($courseid, $roles = '') {
249 global $MNET_REMOTE_CLIENT, $CFG;
251 if (! $course = get_record('course', 'id', $courseid) ) {
252 return 'no course';
253 //error("That's an invalid course id");
256 $context = get_context_instance(CONTEXT_COURSE, $courseid);
258 $sql = "
259 SELECT
260 u.id,
261 u.username,
262 a.enrol,
263 a.timemodified,
264 r.name,
265 r.shortname
266 FROM
267 {$CFG->prefix}role_assignments a,
268 {$CFG->prefix}role r,
269 {$CFG->prefix}user u
270 WHERE
271 a.contextid = '{$context->id}' AND
272 a.roleid = r.id AND
273 a.userid = u.id AND
274 u.mnethostid = '{$MNET_REMOTE_CLIENT->id}'
277 if(!empty($roles)) {
278 // $default_role = get_default_course_role($course); ???
279 $sql .= " AND
280 a.roleid in ('".str_replace(',', "', '", $roles)."')";
283 $enrolments = get_records_sql($sql);
285 $returnarray = array();
286 foreach($enrolments as $user) {
287 $returnarray[$user->username] = array('enrol' => $user->enrol,
288 'timemodified' => $user->timemodified,
289 'shortname' => $user->shortname,
290 'name' => $user->name);
292 return $returnarray;
296 * Enrols user to course with the default role
298 * @param string $username The username of the remote use
299 * @param int $courseid The id of the local course
300 * @return bool Whether the enrolment has been successful
302 function enrol_user($user, $courseid) {
303 global $MNET, $MNET_REMOTE_CLIENT;
305 $userrecord = get_record('user','username',addslashes($user['username']), 'mnethostid',$MNET_REMOTE_CLIENT->id);
307 if ($userrecord == false) {
308 $userrecord = new stdClass();
309 $userrecord->username = addslashes($user['username']);
310 $userrecord->email = addslashes($user['email']);
311 $userrecord->firstname = addslashes($user['firstname']);
312 $userrecord->lastname = addslashes($user['lastname']);
313 $userrecord->mnethostid = $MNET_REMOTE_CLIENT->id;
315 if ($userrecord->id = insert_record('user', $userrecord)) {
316 $userrecord = get_record('user','id', $userrecord->id);
317 } else {
318 // TODO: Error out
319 return false;
323 if (! $course = get_record('course', 'id', $courseid) ) {
324 // TODO: Error out
325 return false;
328 $courses = $this->available_courses();
330 if (!empty($courses[$courseid])) {
331 if (enrol_into_course($course, $userrecord, 'mnet')) {
332 return true;
335 return false;
339 * Unenrol a user from a course
341 * @param string $username The username
342 * @param int $courseid The id of the local course
343 * @return bool Whether the user can login from the remote host
345 function unenrol_user($user, $courseid) {
346 global $MNET_REMOTE_CLIENT;
348 $userrecord = get_record('user','username',$user['username'], 'mnethostid',$MNET_REMOTE_CLIENT->id);
350 if ($userrecord == false) {
351 // TODO: Error out
354 if (! $course = get_record('course', 'id', $courseid) ) {
355 // TODO: Error out
358 if (! $context = get_context_instance(CONTEXT_COURSE, $course->id)) {
359 // TODO: Error out (Invalid context)
362 // Are we a *real* user or the shady MNET Daemon?
363 // require_capability('moodle/role:assign', $context, NULL, false);
365 if (! role_unassign(0, $userrecord->id, 0, $context->id)) {
366 error("An error occurred while trying to unenrol that person.");
369 return true;
372 /***
373 *** Client RPC behaviour
376 ***/
379 * Lists remote servers we use 'enrol' services from.
381 * @return array
383 function list_remote_servers() {
384 global $CFG;
386 $sql = "
387 SELECT DISTINCT
388 h.id,
389 h.name
390 FROM
391 {$CFG->prefix}mnet_host h,
392 {$CFG->prefix}mnet_host2service h2s,
393 {$CFG->prefix}mnet_service s
394 WHERE
395 h.id = h2s.hostid AND
396 h2s.serviceid = s.id AND
397 s.name = 'mnet_enrol' AND
398 h2s.subscribe = 1";
400 $res = get_records_sql($sql);
401 if (is_array($res)) {
402 return $res;
403 } else {
404 return array();
409 * Does Foo
411 * @param int $mnethostid The id of the remote mnethost
412 * @return array Whether the user can login from the remote host
414 function fetch_remote_courses($mnethostid) {
415 global $CFG;
416 global $USER;
417 global $MNET;
418 require_once $CFG->dirroot . '/mnet/xmlrpc/client.php';
420 // get the Service Provider info
421 $mnet_sp = new mnet_peer();
422 $mnet_sp->set_id($mnethostid);
424 // set up the RPC request
425 $mnetrequest = new mnet_xmlrpc_client();
426 $mnetrequest->set_method('enrol/mnet/enrol.php/available_courses');
428 // TODO: cache for a while (10 minutes?)
430 // Thunderbirds are go! Do RPC call and store response
431 if ($mnetrequest->send($mnet_sp) === true) {
432 $courses = $mnetrequest->response;
434 // get the cached courses key'd on remote id - only need remoteid and id fields
435 $cachedcourses = get_records('mnet_enrol_course',
436 'hostid', $mnethostid,
437 'remoteid', 'remoteid, id' );
439 // Update cache and transform $courses into objects
440 // in-place for the benefit of our caller...
441 for ($n=0;$n<count($courses);$n++) {
443 $course = &$courses[$n];
445 // add/update cached data in mnet_enrol_courses
446 // sanitise data
447 $course = (object)$course;
448 $course->remoteid = (int)$course->remoteid;
449 $course->hostid = $mnethostid;
450 $course->cat_id = (int)$course->cat_id;
451 $course->sortorder = (int)$course->sortorder ;
452 $course->startdate = (int)$course->startdate;
453 $course->cost = (int)$course->cost;
454 $course->defaultroleid = (int)$course->defaultroleid;
456 // sanitise strings for DB NOTE - these are not sane
457 // for printing, so we'll use a different object
458 $dbcourse = clone($course);
459 $dbcourse->cat_name = addslashes($dbcourse->cat_name);
460 $dbcourse->cat_description = addslashes($dbcourse->cat_description);
461 $dbcourse->fullname = addslashes($dbcourse->fullname);
462 $dbcourse->shortname = addslashes($dbcourse->shortname);
463 $dbcourse->idnumber = addslashes($dbcourse->idnumber);
464 $dbcourse->summary = addslashes($dbcourse->summary);
465 $dbcourse->currency = addslashes($dbcourse->currency);
466 $dbcourse->defaultrolename = addslashes($dbcourse->defaultrolename);
468 // insert or update
469 if (empty($cachedcourses[$course->remoteid])) {
470 $course->id = insert_record('mnet_enrol_course', $dbcourse);
471 } else {
472 $course->id = $cachedcourses[$course->remoteid]->id;
473 $cachedcourses[$course->remoteid]->seen=true;
474 update_record('mnet_enrol_course', $dbcourse);
476 // free tmp obj
477 unset($dbcourse);
480 // prune stale data from cache
481 if (!empty($cachedcourses)) {
482 $stale = array();
483 foreach ($cachedcourses as $id => $cc) {
484 // TODO: maybe set a deleted flag instead
485 if (empty($cc->seen)) {
486 $stale[] = $cc->id;
489 if (!empty($stale)) {
490 delete_records_select('mnet_enrol_course', 'id IN ('.join(',',$stale).')');
494 return $courses;
495 } else {
496 foreach ($mnetrequest->error as $errormessage) {
497 list($code, $errormessage) = array_map('trim',explode(':', $errormessage, 2));
498 $message .= "ERROR $code:<br/>$errormessage<br/>";
500 error("RPC enrol/mnet/available_courses:<br/>$message");
502 return false;
506 * Does Foo
508 * @param int $mnethostid The id of the remote mnethost
509 * @return array Whether the user can login from the remote host
511 function req_enrol_user($userid, $courseid) {
512 global $CFG;
513 global $USER;
514 global $MNET;
515 require_once $CFG->dirroot . '/mnet/xmlrpc/client.php';
517 // Prepare a basic user record
518 // in case the remote host doesn't have it
519 $user = get_record('user', 'id', $userid, '','','','', 'username, email, firstname, lastname');
520 $user = (array)$user;
522 $course = get_record('mnet_enrol_course', 'id', $courseid);
524 // get the Service Provider info
525 $mnet_sp = new mnet_peer();
526 $mnet_sp->set_id($course->hostid);
528 // set up the RPC request
529 $mnetrequest = new mnet_xmlrpc_client();
530 $mnetrequest->set_method('enrol/mnet/enrol.php/enrol_user');
531 $mnetrequest->add_param($user);
532 $mnetrequest->add_param($course->remoteid);
534 // Thunderbirds are go! Do RPC call and store response
535 if ($mnetrequest->send($mnet_sp) === true) {
536 if ($mnetrequest->response == true) {
537 // now store it in the mnet_enrol_assignments table
538 $assignment = new StdClass;
539 $assignment->userid = $userid;
540 $assignment->hostid = $course->hostid;
541 $assignment->courseid = $course->id;
542 $assignment->enroltype = 'mnet';
543 // TODO: other fields
544 if (insert_record('mnet_enrol_assignments', $assignment)) {
545 return true;
550 return false;
554 * Does Foo
556 * @param int $mnethostid The id of the remote mnethost
557 * @return array Whether the user can login from the remote host
559 function req_unenrol_user($userid, $courseid) {
560 global $CFG;
561 global $USER;
562 global $MNET;
563 require_once $CFG->dirroot . '/mnet/xmlrpc/client.php';
565 // in case the remote host doesn't have it
566 $user = get_record('user', 'id', $userid, '','','','', 'username, email');
567 $user = $user->username;
569 $course = get_record('mnet_enrol_course', 'id', $courseid);
571 // get the Service Provider info
572 $mnet_sp = new mnet_peer();
573 $mnet_sp->set_id($course->hostid);
575 // set up the RPC request
576 $mnetrequest = new mnet_xmlrpc_client();
577 $mnetrequest->set_method('enrol/mnet/enrol.php/unenrol_user');
578 $mnetrequest->add_param($user);
579 $mnetrequest->add_param($course->remoteid);
581 // TODO - prevent removal of enrolments that are not of
582 // type mnet...
585 // Thunderbirds are go! Do RPC call and store response
586 if ($mnetrequest->send($mnet_sp) === true) {
587 if ($mnetrequest->response == true) {
588 // remove enrolment cached in mnet_enrol_assignments
589 delete_records_select('mnet_enrol_assignments',
590 "userid={$userid} AND courseid={$course->id}");
592 return true;
595 return false;
598 } // end of class