2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * IMS Enterprise file enrolment plugin.
20 * This plugin lets the user specify an IMS Enterprise file to be processed.
21 * The IMS Enterprise file is mainly parsed on a regular cron,
22 * but can also be imported via the UI (Admin Settings).
23 * @package enrol_imsenterprise
24 * @copyright 2010 Eugene Venter
25 * @author Eugene Venter - based on code by Dan Stowell
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 This class uses regular expressions to mine the data file. The main reason is
34 that XML handling changes from PHP 4 to PHP 5, so this should work on both.
36 One drawback is that the pattern-matching doesn't (currently) handle XML
37 namespaces - it only copes with a <group> tag if it says <group>, and not
38 (for example) <ims:group>.
40 This should also be able to handle VERY LARGE FILES - so the entire IMS file is
41 NOT loaded into memory at once. It's handled line-by-line, 'forgetting' tags as
42 soon as they are processed.
44 N.B. The "sourcedid" ID code is translated to Moodle's "idnumber" field, both
45 for users and for courses.
49 require_once($CFG->dirroot.'/group/lib.php');
52 class enrol_imsenterprise_plugin extends enrol_plugin {
58 * Read in an IMS Enterprise file.
59 * Originally designed to handle v1.1 files but should be able to handle
60 * earlier types as well, I believe.
67 $imsfilelocation = $this->get_config('imsfilelocation');
68 $logtolocation = $this->get_config('logtolocation');
69 $mailadmins = $this->get_config('mailadmins');
70 $prev_time = $this->get_config('prev_time');
71 $prev_md5 = $this->get_config('prev_md5');
72 $prev_path = $this->get_config('prev_path');
74 if (empty($imsfilelocation)) {
75 // $filename = "$CFG->dirroot/enrol/imsenterprise/example.xml"; // Default location
76 $filename = "$CFG->dataroot/1/imsenterprise-enrol.xml"; // Default location
78 $filename = $imsfilelocation;
81 $this->logfp = false; // File pointer for writing log data to
82 if(!empty($logtolocation)) {
83 $this->logfp = fopen($logtolocation, 'a');
86 if ( file_exists($filename) ) {
90 $this->log_line('----------------------------------------------------------------------');
91 $this->log_line("IMS Enterprise enrol cron process launched at " . userdate(time()));
92 $this->log_line('Found file '.$filename);
95 // Make sure we understand how to map the IMS-E roles to Moodle roles
96 $this->load_role_mappings();
98 $md5 = md5_file($filename); // NB We'll write this value back to the database at the end of the cron
99 $filemtime = filemtime($filename);
101 // Decide if we want to process the file (based on filepath, modification time, and MD5 hash)
102 // This is so we avoid wasting the server's efforts processing a file unnecessarily
103 if(empty($prev_path) || ($filename != $prev_path)) {
105 } elseif(isset($prev_time) && ($filemtime <= $prev_time)) {
107 $this->log_line('File modification time is not more recent than last update - skipping processing.');
108 } elseif(isset($prev_md5) && ($md5 == $prev_md5)) {
110 $this->log_line('File MD5 hash is same as on last update - skipping processing.');
112 $fileisnew = true; // Let's process it!
117 $listoftags = array('group', 'person', 'member', 'membership', 'comments', 'properties'); // The list of tags which should trigger action (even if only cache trimming)
118 $this->continueprocessing = true; // The <properties> tag is allowed to halt processing if we're demanding a matching target
120 // FIRST PASS: Run through the file and process the group/person entries
121 if (($fh = fopen($filename, "r")) != false) {
124 while ((!feof($fh)) && $this->continueprocessing) {
127 $curline = fgets($fh);
128 $this->xmlcache .= $curline; // Add a line onto the XML cache
131 // If we've got a full tag (i.e. the most recent line has closed the tag) then process-it-and-forget-it.
132 // Must always make sure to remove tags from cache so they don't clog up our memory
133 if($tagcontents = $this->full_tag_found_in_cache('group', $curline)) {
134 $this->process_group_tag($tagcontents);
135 $this->remove_tag_from_cache('group');
136 } elseif($tagcontents = $this->full_tag_found_in_cache('person', $curline)) {
137 $this->process_person_tag($tagcontents);
138 $this->remove_tag_from_cache('person');
139 } elseif($tagcontents = $this->full_tag_found_in_cache('membership', $curline)) {
140 $this->process_membership_tag($tagcontents);
141 $this->remove_tag_from_cache('membership');
142 } elseif($tagcontents = $this->full_tag_found_in_cache('comments', $curline)) {
143 $this->remove_tag_from_cache('comments');
144 } elseif($tagcontents = $this->full_tag_found_in_cache('properties', $curline)) {
145 $this->process_properties_tag($tagcontents);
146 $this->remove_tag_from_cache('properties');
150 } // End of while-tags-are-detected
151 } // end of while loop
153 fix_course_sortorder();
154 } // end of if(file_open) for first pass
160 Since the IMS specification v1.1 insists that "memberships" should come last,
161 and since vendors seem to have done this anyway (even with 1.0),
162 we can sensibly perform the import in one fell swoop.
165 // SECOND PASS: Now go through the file and process the membership entries
166 $this->xmlcache = '';
167 if (($fh = fopen($filename, "r")) != false) {
169 while ((!feof($fh)) && $this->continueprocessing) {
171 $curline = fgets($fh);
172 $this->xmlcache .= $curline; // Add a line onto the XML cache
175 // Must always make sure to remove tags from cache so they don't clog up our memory
176 if($tagcontents = $this->full_tag_found_in_cache('group', $curline)){
177 $this->remove_tag_from_cache('group');
178 }elseif($tagcontents = $this->full_tag_found_in_cache('person', $curline)){
179 $this->remove_tag_from_cache('person');
180 }elseif($tagcontents = $this->full_tag_found_in_cache('membership', $curline)){
181 $this->process_membership_tag($tagcontents);
182 $this->remove_tag_from_cache('membership');
183 }elseif($tagcontents = $this->full_tag_found_in_cache('comments', $curline)){
184 $this->remove_tag_from_cache('comments');
185 }elseif($tagcontents = $this->full_tag_found_in_cache('properties', $curline)){
186 $this->remove_tag_from_cache('properties');
191 } // end of while loop
193 } // end of if(file_open) for second pass
198 $timeelapsed = time() - $starttime;
199 $this->log_line('Process has completed. Time taken: '.$timeelapsed.' seconds.');
202 } // END of "if file is new"
205 // These variables are stored so we can compare them against the IMS file, next time round.
206 $this->set_config('prev_time', $filemtime);
207 $this->set_config('prev_md5', $md5);
208 $this->set_config('prev_path', $filename);
212 }else{ // end of if(file_exists)
213 $this->log_line('File not found: '.$filename);
216 if (!empty($mailadmins)) {
217 $msg = "An IMS enrolment has been carried out within Moodle.\nTime taken: $timeelapsed seconds.\n\n";
218 if(!empty($logtolocation)){
220 $msg .= "Log data has been written to:\n";
221 $msg .= "$logtolocation\n";
222 $msg .= "(Log file size: ".ceil(filesize($logtolocation)/1024)."Kb)\n\n";
224 $msg .= "The log file appears not to have been successfully written.\nCheck that the file is writeable by the server:\n";
225 $msg .= "$logtolocation\n\n";
228 $msg .= "Logging is currently not active.";
231 $eventdata = new object();
232 $eventdata->modulename = 'moodle';
233 $eventdata->component = 'imsenterprise';
234 $eventdata->name = 'imsenterprise_enrolment';
235 $eventdata->userfrom = get_admin();
236 $eventdata->userto = get_admin();
237 $eventdata->subject = "Moodle IMS Enterprise enrolment notification";
238 $eventdata->fullmessage = $msg;
239 $eventdata->fullmessageformat = FORMAT_PLAIN;
240 $eventdata->fullmessagehtml = '';
241 $eventdata->smallmessage = '';
242 message_send($eventdata);
244 $this->log_line('Notification email sent to administrator.');
249 fclose($this->logfp);
253 } // end of cron() function
256 * Check if a complete tag is found in the cached data, which usually happens
257 * when the end of the tag has only just been loaded into the cache.
258 * Returns either false, or the contents of the tag (including start and end).
259 * @param string $tagname Name of tag to look for
260 * @param string $latestline The very last line in the cache (used for speeding up the match)
262 function full_tag_found_in_cache($tagname, $latestline){ // Return entire element if found. Otherwise return false.
263 if(strpos(strtolower($latestline), '</'.strtolower($tagname).'>')===false){
265 }elseif(preg_match('{(<'.$tagname.'\b.*?>.*?</'.$tagname.'>)}is', $this->xmlcache, $matches)){
271 * Remove complete tag from the cached data (including all its contents) - so
272 * that the cache doesn't grow to unmanageable size
273 * @param string $tagname Name of tag to look for
275 function remove_tag_from_cache($tagname){ // Trim the cache so we're not in danger of running out of memory.
276 ///echo "<p>remove_tag_from_cache: $tagname</p>"; flush(); ob_flush();
277 // echo "<p>remove_tag_from_cache:<br />".htmlspecialchars($this->xmlcache);
278 $this->xmlcache = trim(preg_replace('{<'.$tagname.'\b.*?>.*?</'.$tagname.'>}is', '', $this->xmlcache, 1)); // "1" so that we replace only the FIRST instance
279 // echo "<br />".htmlspecialchars($this->xmlcache)."</p>";
283 * Very simple convenience function to return the "recstatus" found in person/group/role tags.
284 * 1=Add, 2=Update, 3=Delete, as specified by IMS, and we also use 0 to indicate "unspecified".
285 * @param string $tagdata the tag XML data
286 * @param string $tagname the name of the tag we're interested in
288 function get_recstatus($tagdata, $tagname){
289 if(preg_match('{<'.$tagname.'\b[^>]*recstatus\s*=\s*["\'](\d)["\']}is', $tagdata, $matches)){
290 // echo "<p>get_recstatus($tagname) found status of $matches[1]</p>";
291 return intval($matches[1]);
293 // echo "<p>get_recstatus($tagname) found nothing</p>";
294 return 0; // Unspecified
299 * Process the group tag. This defines a Moodle course.
300 * @param string $tagconents The raw contents of the XML element
302 function process_group_tag($tagcontents){
306 $truncatecoursecodes = $this->get_config('truncatecoursecodes');
307 $createnewcourses = $this->get_config('createnewcourses');
308 $createnewcategories = $this->get_config('createnewcategories');
310 // Process tag contents
312 if(preg_match('{<sourcedid>.*?<id>(.+?)</id>.*?</sourcedid>}is', $tagcontents, $matches)){
313 $group->coursecode = trim($matches[1]);
315 if(preg_match('{<description>.*?<short>(.*?)</short>.*?</description>}is', $tagcontents, $matches)){
316 $group->description = trim($matches[1]);
318 if(preg_match('{<org>.*?<orgunit>(.*?)</orgunit>.*?</org>}is', $tagcontents, $matches)){
319 $group->category = trim($matches[1]);
322 $recstatus = ($this->get_recstatus($tagcontents, 'group'));
323 //echo "<p>get_recstatus for this group returned $recstatus</p>";
325 if(!(strlen($group->coursecode)>0)){
326 $this->log_line('Error at line '.$line.': Unable to find course code in \'group\' element.');
328 // First, truncate the course code if desired
329 if(intval($truncatecoursecodes)>0){
330 $group->coursecode = ($truncatecoursecodes > 0)
331 ? substr($group->coursecode, 0, intval($truncatecoursecodes))
332 : $group->coursecode;
335 /* -----------Course aliasing is DEACTIVATED until a more general method is in place---------------
337 // Second, look in the course alias table to see if the code should be translated to something else
338 if($aliases = $DB->get_field('enrol_coursealias', 'toids', array('fromid'=>$group->coursecode))){
339 $this->log_line("Found alias of course code: Translated $group->coursecode to $aliases");
340 // Alias is allowed to be a comma-separated list, so let's split it
341 $group->coursecode = explode(',', $aliases);
345 // For compatibility with the (currently inactive) course aliasing, we need this to be an array
346 $group->coursecode = array($group->coursecode);
348 // Third, check if the course(s) exist
349 foreach($group->coursecode as $coursecode){
350 $coursecode = trim($coursecode);
351 if(!$DB->get_field('course', 'id', array('idnumber'=>$coursecode))) {
352 if(!$createnewcourses) {
353 $this->log_line("Course $coursecode not found in Moodle's course idnumbers.");
355 // Create the (hidden) course(s) if not found
356 $course = new object();
357 $course->fullname = $group->description;
358 $course->shortname = $coursecode;
359 $course->idnumber = $coursecode;
360 $course->format = 'topics';
361 $course->visible = 0;
362 // Insert default names for teachers/students, from the current language
365 // Handle course categorisation (taken from the group.org.orgunit field if present)
366 if(strlen($group->category)>0){
367 // If the category is defined and exists in Moodle, we want to store it in that one
368 if($catid = $DB->get_field('course_categories', 'id', array('name'=>$group->category))){
369 $course->category = $catid;
370 } elseif($createnewcategories) {
371 // Else if we're allowed to create new categories, let's create this one
372 $newcat->name = $group->category;
373 $newcat->visible = 0;
374 if($catid = $DB->insert_record('course_categories', $newcat)){
375 $course->category = $catid;
376 $this->log_line("Created new (hidden) category, #$catid: $newcat->name");
378 $this->log_line('Failed to create new category: '.$newcat->name);
381 // If not found and not allowed to create, stick with default
382 $this->log_line('Category '.$group->category.' not found in Moodle database, so using default category instead.');
383 $course->category = 1;
386 $course->category = 1;
388 $course->timecreated = time();
389 $course->startdate = time();
390 $course->numsections = 1;
391 // Choose a sort order that puts us at the start of the list!
392 $course->sortorder = 0;
394 if ($courseid = $DB->insert_record('course', $course)) {
397 $course = $DB->get_record('course', array('id' => $courseid));
398 blocks_add_default_course_blocks($course);
400 $section = new object();
401 $section->course = $course->id; // Create a default section.
402 $section->section = 0;
403 $section->summaryformat = FORMAT_HTML;
404 $section->id = $DB->insert_record("course_sections", $section);
406 add_to_log(SITEID, "course", "new", "view.php?id=$course->id", "$course->fullname (ID $course->id)");
408 $this->log_line("Created course $coursecode in Moodle (Moodle ID is $course->id)");
410 $this->log_line('Failed to create course '.$coursecode.' in Moodle');
413 }elseif($recstatus==3 && ($courseid = $DB->get_field('course', 'id', array('idnumber'=>$coursecode)))){
414 // If course does exist, but recstatus==3 (delete), then set the course as hidden
415 $DB->set_field('course', 'visible', '0', array('id'=>$courseid));
417 } // End of foreach(coursecode)
419 } // End process_group_tag()
422 * Process the person tag. This defines a Moodle user.
423 * @param string $tagconents The raw contents of the XML element
425 function process_person_tag($tagcontents){
428 // Get plugin configs
429 $imssourcedidfallback = $this->get_config('imssourcedidfallback');
430 $fixcaseusernames = $this->get_config('fixcaseusernames');
431 $fixcasepersonalnames = $this->get_config('fixcasepersonalnames');
432 $imsdeleteusers = $this->get_config('imsdeleteusers');
433 $createnewusers = $this->get_config('createnewusers');
435 if(preg_match('{<sourcedid>.*?<id>(.+?)</id>.*?</sourcedid>}is', $tagcontents, $matches)){
436 $person->idnumber = trim($matches[1]);
438 if(preg_match('{<name>.*?<n>.*?<given>(.+?)</given>.*?</n>.*?</name>}is', $tagcontents, $matches)){
439 $person->firstname = trim($matches[1]);
441 if(preg_match('{<name>.*?<n>.*?<family>(.+?)</family>.*?</n>.*?</name>}is', $tagcontents, $matches)){
442 $person->lastname = trim($matches[1]);
444 if(preg_match('{<userid>(.*?)</userid>}is', $tagcontents, $matches)){
445 $person->username = trim($matches[1]);
447 if($imssourcedidfallback && trim($person->username)==''){
448 // This is the point where we can fall back to useing the "sourcedid" if "userid" is not supplied
449 // NB We don't use an "elseif" because the tag may be supplied-but-empty
450 $person->username = $person->idnumber;
452 if(preg_match('{<email>(.*?)</email>}is', $tagcontents, $matches)){
453 $person->email = trim($matches[1]);
455 if(preg_match('{<url>(.*?)</url>}is', $tagcontents, $matches)){
456 $person->url = trim($matches[1]);
458 if(preg_match('{<adr>.*?<locality>(.+?)</locality>.*?</adr>}is', $tagcontents, $matches)){
459 $person->city = trim($matches[1]);
461 if(preg_match('{<adr>.*?<country>(.+?)</country>.*?</adr>}is', $tagcontents, $matches)){
462 $person->country = trim($matches[1]);
465 // Fix case of some of the fields if required
466 if($fixcaseusernames && isset($person->username)){
467 $person->username = strtolower($person->username);
469 if($fixcasepersonalnames){
470 if(isset($person->firstname)){
471 $person->firstname = ucwords(strtolower($person->firstname));
473 if(isset($person->lastname)){
474 $person->lastname = ucwords(strtolower($person->lastname));
478 $recstatus = ($this->get_recstatus($tagcontents, 'person'));
481 // Now if the recstatus is 3, we should delete the user if-and-only-if the setting for delete users is turned on
482 // In the "users" table we can do this by setting deleted=1
485 if($imsdeleteusers){ // If we're allowed to delete user records
486 // Make sure their "deleted" field is set to one
487 $DB->set_field('user', 'deleted', 1, array('username'=>$person->username));
488 $this->log_line("Marked user record for user '$person->username' (ID number $person->idnumber) as deleted.");
490 $this->log_line("Ignoring deletion request for user '$person->username' (ID number $person->idnumber).");
493 }else{ // Add or update record
496 // If the user exists (matching sourcedid) then we don't need to do anything.
497 if(!$DB->get_field('user', 'id', array('idnumber'=>$person->idnumber)) && $createnewusers){
498 // If they don't exist and haven't a defined username, we log this as a potential problem.
499 if((!isset($person->username)) || (strlen($person->username)==0)){
500 $this->log_line("Cannot create new user for ID # $person->idnumber - no username listed in IMS data for this person.");
501 } else if ($DB->get_field('user', 'id', array('username'=>$person->username))){
502 // If their idnumber is not registered but their user ID is, then add their idnumber to their record
503 $DB->set_field('user', 'idnumber', $person->idnumber, array('username'=>$person->username));
506 // If they don't exist and they have a defined username, and $createnewusers == true, we create them.
507 $person->lang = 'manual'; //TODO: this needs more work due tu multiauth changes
508 $person->auth = $CFG->auth;
509 $person->confirmed = 1;
510 $person->timemodified = time();
511 $person->mnethostid = $CFG->mnet_localhost_id;
512 if($id = $DB->insert_record('user', $person)){
514 Photo processing is deactivated until we hear from Moodle dev forum about modification to gdlib.
516 //Antoni Mas. 07/12/2005. If a photo URL is specified then we might want to load
517 // it into the user's profile. Beware that this may cause a heavy overhead on the server.
518 if($CFG->enrol_processphoto){
519 if(preg_match('{<photo>.*?<extref>(.*?)</extref>.*?</photo>}is', $tagcontents, $matches)){
520 $person->urlphoto = trim($matches[1]);
522 //Habilitam el flag que ens indica que el personatge t foto prpia.
523 $person->picture = 1;
524 //Llibreria creada per nosaltres mateixos.
525 require_once($CFG->dirroot.'/lib/gdlib.php');
526 if ($usernew->picture = save_profile_image($id, $person->urlphoto,'user')) { TODO: use process_new_icon() instead
527 $DB->set_field('user', 'picture', $usernew->picture, array('id'=>$id)); /// Note picture in DB
531 $this->log_line("Created user record for user '$person->username' (ID number $person->idnumber).");
533 $this->log_line("Database error while trying to create user record for user '$person->username' (ID number $person->idnumber).");
536 } elseif ($createnewusers) {
537 $this->log_line("User record already exists for user '$person->username' (ID number $person->idnumber).");
539 // Make sure their "deleted" field is set to zero.
540 $DB->set_field('user', 'deleted', 0, array('idnumber'=>$person->idnumber));
542 $this->log_line("No user record found for '$person->username' (ID number $person->idnumber).");
545 } // End of are-we-deleting-or-adding
547 } // End process_person_tag()
550 * Process the membership tag. This defines whether the specified Moodle users
551 * should be added/removed as teachers/students.
552 * @param string $tagconents The raw contents of the XML element
554 function process_membership_tag($tagcontents){
557 // Get plugin configs
558 $truncatecoursecodes = $this->get_config('truncatecoursecodes');
559 $imscapitafix = $this->get_config('imscapitafix');
564 // In order to reduce the number of db queries required, group name/id associations are cached in this array:
567 if(preg_match('{<sourcedid>.*?<id>(.+?)</id>.*?</sourcedid>}is', $tagcontents, $matches)){
568 $ship->coursecode = ($truncatecoursecodes > 0)
569 ? substr(trim($matches[1]), 0, intval($truncatecoursecodes))
571 $ship->courseid = $DB->get_field('course', 'id', array('idnumber'=>$ship->coursecode));
573 if($ship->courseid && preg_match_all('{<member>(.*?)</member>}is', $tagcontents, $membermatches, PREG_SET_ORDER)){
574 $courseobj = new stdClass();
575 $courseobj->id = $ship->courseid;
577 foreach($membermatches as $mmatch){
579 unset($memberstoreobj);
580 if(preg_match('{<sourcedid>.*?<id>(.+?)</id>.*?</sourcedid>}is', $mmatch[1], $matches)){
581 $member->idnumber = trim($matches[1]);
583 if(preg_match('{<role\s+roletype=["\'](.+?)["\'].*?>}is', $mmatch[1], $matches)){
584 $member->roletype = trim($matches[1]); // 01 means Student, 02 means Instructor, 3 means ContentDeveloper, and there are more besides
585 } elseif($imscapitafix && preg_match('{<roletype>(.+?)</roletype>}is', $mmatch[1], $matches)){
586 // The XML that comes out of Capita Student Records seems to contain a misinterpretation of the IMS specification!
587 $member->roletype = trim($matches[1]); // 01 means Student, 02 means Instructor, 3 means ContentDeveloper, and there are more besides
589 if(preg_match('{<role\b.*?<status>(.+?)</status>.*?</role>}is', $mmatch[1], $matches)){
590 $member->status = trim($matches[1]); // 1 means active, 0 means inactive - treat this as enrol vs unenrol
593 $recstatus = ($this->get_recstatus($mmatch[1], 'role'));
595 $member->status = 0; // See above - recstatus of 3 (==delete) is treated the same as status of 0
596 //echo "<p>process_membership_tag: unenrolling member due to recstatus of 3</p>";
599 $timeframe->begin = 0;
601 if(preg_match('{<role\b.*?<timeframe>(.+?)</timeframe>.*?</role>}is', $mmatch[1], $matches)){
602 $timeframe = $this->decode_timeframe($matches[1]);
604 if(preg_match('{<role\b.*?<extension>.*?<cohort>(.+?)</cohort>.*?</extension>.*?</role>}is', $mmatch[1], $matches)){
605 $member->groupname = trim($matches[1]);
606 // The actual processing (ensuring a group record exists, etc) occurs below, in the enrol-a-student clause
609 $rolecontext = get_context_instance(CONTEXT_COURSE, $ship->courseid);
610 $rolecontext = $rolecontext->id; // All we really want is the ID
611 //$this->log_line("Context instance for course $ship->courseid is...");
612 //print_r($rolecontext);
614 // Add or remove this student or teacher to the course...
615 $memberstoreobj->userid = $DB->get_field('user', 'id', array('idnumber'=>$member->idnumber));
616 $memberstoreobj->enrol = 'imsenterprise';
617 $memberstoreobj->course = $ship->courseid;
618 $memberstoreobj->time = time();
619 $memberstoreobj->timemodified = time();
620 if($memberstoreobj->userid){
622 // Decide the "real" role (i.e. the Moodle role) that this user should be assigned to.
623 // Zero means this roletype is supposed to be skipped.
624 $moodleroleid = $this->rolemappings[$member->roletype];
626 $this->log_line("SKIPPING role $member->roletype for $memberstoreobj->userid ($member->idnumber) in course $memberstoreobj->course");
630 if(intval($member->status) == 1) {
633 $einstance = $DB->get_record('enrol',
634 array('courseid' => $courseobj->id, 'enrol' => $memberstoreobj->enrol));
635 if (empty($einstance)) {
636 // Only add an enrol instance to the course if non-existent
637 $enrolid = $this->add_instance($courseobj);
638 $einstance = $DB->get_record('enrol', array('id' => $enrolid));
641 $this->enrol_user($einstance, $memberstoreobj->userid, $moodleroleid, $timeframe->begin, $timeframe->end);
643 $this->log_line("Enrolled user #$memberstoreobj->userid ($member->idnumber) to role $member->roletype in course $memberstoreobj->course");
646 // At this point we can also ensure the group membership is recorded if present
647 if(isset($member->groupname)){
648 // Create the group if it doesn't exist - either way, make sure we know the group ID
649 if(isset($groupids[$member->groupname])) {
650 $member->groupid = $groupids[$member->groupname]; // Recall the group ID from cache if available
652 if($groupid = $DB->get_field('groups', 'id', 'name', $member->groupname, array('courseid'=>$ship->courseid))){
653 $member->groupid = $groupid;
654 $groupids[$member->groupname] = $groupid; // Store ID in cache
656 // Attempt to create the group
657 $group->name = $member->groupname;
658 $group->courseid = $ship->courseid;
659 $group->timecreated = time();
660 $group->timemodified = time();
661 $groupid = $DB->insert_record('groups', $group);
662 $this->log_line('Added a new group for this course: '.$group->name);
663 $groupids[$member->groupname] = $groupid; // Store ID in cache
664 $member->groupid = $groupid;
667 // Add the user-to-group association if it doesn't already exist
668 if($member->groupid) {
669 groups_add_member($member->groupid, $memberstoreobj->userid);
671 } // End of group-enrolment (from member.role.extension.cohort tag)
673 } elseif ($this->get_config('imsunenrol')) {
676 $einstances = $DB->get_records('enrol',
677 array('enrol' => $memberstoreobj->enrol, 'courseid' => $courseobj->id));
678 foreach ($einstances as $einstance) {
679 // Unenrol the user from all imsenterprise enrolment instances
680 $this->unenrol_user($einstance, $memberstoreobj->userid);
684 $this->log_line("Unenrolled $member->idnumber from role $moodleroleid in course");
689 $this->log_line("Added $memberstally users to course $ship->coursecode");
690 if($membersuntally > 0){
691 $this->log_line("Removed $membersuntally users from course $ship->coursecode");
694 } // End process_membership_tag()
697 * Process the properties tag. The only data from this element
698 * that is relevant is whether a <target> is specified.
699 * @param string $tagconents The raw contents of the XML element
701 function process_properties_tag($tagcontents){
702 $imsrestricttarget = $this->get_config('imsrestricttarget');
704 if ($imsrestricttarget) {
705 if(!(preg_match('{<target>'.preg_quote($imsrestricttarget).'</target>}is', $tagcontents, $matches))){
706 $this->log_line("Skipping processing: required target \"$imsrestricttarget\" not specified in this data.");
707 $this->continueprocessing = false;
713 * Store logging information. This does two things: uses the {@link mtrace()}
714 * function to print info to screen/STDOUT, and also writes log to a text file
715 * if a path has been specified.
716 * @param string $string Text to write (newline will be added automatically)
718 function log_line($string){
721 fwrite($this->logfp, $string . "\n");
726 * Process the INNER contents of a <timeframe> tag, to return beginning/ending dates.
728 function decode_timeframe($string){ // Pass me the INNER CONTENTS of a <timeframe> tag - beginning and/or ending is returned, in unix time, zero indicating not specified
729 $ret->begin = $ret->end = 0;
730 // Explanatory note: The matching will ONLY match if the attribute restrict="1"
731 // because otherwise the time markers should be ignored (participation should be
732 // allowed outside the period)
733 if(preg_match('{<begin\s+restrict="1">(\d\d\d\d)-(\d\d)-(\d\d)</begin>}is', $string, $matches)){
734 $ret->begin = mktime(0,0,0, $matches[2], $matches[3], $matches[1]);
736 if(preg_match('{<end\s+restrict="1">(\d\d\d\d)-(\d\d)-(\d\d)</end>}is', $string, $matches)){
737 $ret->end = mktime(0,0,0, $matches[2], $matches[3], $matches[1]);
740 } // End decode_timeframe
743 * Load the role mappings (from the config), so we can easily refer to
744 * how an IMS-E role corresponds to a Moodle role
746 function load_role_mappings() {
747 require_once('locallib.php');
750 $imsroles = new imsenterprise_roles();
751 $imsroles = $imsroles->get_imsroles();
753 $this->rolemappings = array();
754 foreach($imsroles as $imsrolenum=>$imsrolename) {
755 $this->rolemappings[$imsrolenum] = $this->rolemappings[$imsrolename]
756 = $DB->get_field('config_plugins', 'value', array('name'=>'imsrolemap' . $imsrolenum));