MDL-31006 some more PHP54 notices
[moodle.git] / enrol / imsenterprise / lib.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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.
13 //
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/>.
17 /**
18  * IMS Enterprise file enrolment plugin.
19  *
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
24  * @subpackage imsenterprise
25  * @copyright  2010 Eugene Venter
26  * @author     Eugene Venter - based on code by Dan Stowell
27  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28  */
30 defined('MOODLE_INTERNAL') || die();
32 /*
34 Note for programmers:
36 This class uses regular expressions to mine the data file. The main reason is
37 that XML handling changes from PHP 4 to PHP 5, so this should work on both.
39 One drawback is that the pattern-matching doesn't (currently) handle XML
40 namespaces - it only copes with a <group> tag if it says <group>, and not
41 (for example) <ims:group>.
43 This should also be able to handle VERY LARGE FILES - so the entire IMS file is
44 NOT loaded into memory at once. It's handled line-by-line, 'forgetting' tags as
45 soon as they are processed.
47 N.B. The "sourcedid" ID code is translated to Moodle's "idnumber" field, both
48 for users and for courses.
50 */
52 require_once($CFG->dirroot.'/group/lib.php');
55 class enrol_imsenterprise_plugin extends enrol_plugin {
57 var $log;
60 /**
61 * Read in an IMS Enterprise file.
62 * Originally designed to handle v1.1 files but should be able to handle
63 * earlier types as well, I believe.
64 *
65 */
66 function cron() {
67     global $CFG;
69     // Get configs
70     $imsfilelocation    = $this->get_config('imsfilelocation');
71     $logtolocation      = $this->get_config('logtolocation');
72     $mailadmins         = $this->get_config('mailadmins');
73     $prev_time          = $this->get_config('prev_time');
74     $prev_md5           = $this->get_config('prev_md5');
75     $prev_path          = $this->get_config('prev_path');
77     if (empty($imsfilelocation)) {
78         // $filename = "$CFG->dirroot/enrol/imsenterprise/example.xml";  // Default location
79         $filename = "$CFG->dataroot/1/imsenterprise-enrol.xml";  // Default location
80     } else {
81         $filename = $imsfilelocation;
82     }
84     $this->logfp = false; // File pointer for writing log data to
85     if(!empty($logtolocation)) {
86         $this->logfp = fopen($logtolocation, 'a');
87     }
89     if ( file_exists($filename) ) {
90         @set_time_limit(0);
91         $starttime = time();
93         $this->log_line('----------------------------------------------------------------------');
94         $this->log_line("IMS Enterprise enrol cron process launched at " . userdate(time()));
95         $this->log_line('Found file '.$filename);
96         $this->xmlcache = '';
98         // Make sure we understand how to map the IMS-E roles to Moodle roles
99         $this->load_role_mappings();
101         $md5 = md5_file($filename); // NB We'll write this value back to the database at the end of the cron
102         $filemtime = filemtime($filename);
104         // Decide if we want to process the file (based on filepath, modification time, and MD5 hash)
105         // This is so we avoid wasting the server's efforts processing a file unnecessarily
106         if(empty($prev_path)  || ($filename != $prev_path)) {
107             $fileisnew = true;
108         } elseif(isset($prev_time) && ($filemtime <= $prev_time)) {
109             $fileisnew = false;
110             $this->log_line('File modification time is not more recent than last update - skipping processing.');
111         } elseif(isset($prev_md5) && ($md5 == $prev_md5)) {
112             $fileisnew = false;
113             $this->log_line('File MD5 hash is same as on last update - skipping processing.');
114         } else {
115             $fileisnew = true; // Let's process it!
116         }
118         if($fileisnew) {
120             $listoftags = array('group', 'person', 'member', 'membership', 'comments', 'properties'); // The list of tags which should trigger action (even if only cache trimming)
121             $this->continueprocessing = true; // The <properties> tag is allowed to halt processing if we're demanding a matching target
123             // FIRST PASS: Run through the file and process the group/person entries
124             if (($fh = fopen($filename, "r")) != false) {
126                 $line = 0;
127                 while ((!feof($fh)) && $this->continueprocessing) {
129                     $line++;
130                     $curline = fgets($fh);
131                     $this->xmlcache .= $curline; // Add a line onto the XML cache
133                     while (true) {
134                       // If we've got a full tag (i.e. the most recent line has closed the tag) then process-it-and-forget-it.
135                       // Must always make sure to remove tags from cache so they don't clog up our memory
136                       if($tagcontents = $this->full_tag_found_in_cache('group', $curline)) {
137                           $this->process_group_tag($tagcontents);
138                           $this->remove_tag_from_cache('group');
139                       } elseif($tagcontents = $this->full_tag_found_in_cache('person', $curline)) {
140                           $this->process_person_tag($tagcontents);
141                           $this->remove_tag_from_cache('person');
142                       } elseif($tagcontents = $this->full_tag_found_in_cache('membership', $curline)) {
143                           $this->process_membership_tag($tagcontents);
144                           $this->remove_tag_from_cache('membership');
145                       } elseif($tagcontents = $this->full_tag_found_in_cache('comments', $curline)) {
146                           $this->remove_tag_from_cache('comments');
147                       } elseif($tagcontents = $this->full_tag_found_in_cache('properties', $curline)) {
148                           $this->process_properties_tag($tagcontents);
149                           $this->remove_tag_from_cache('properties');
150                       } else {
151                           break;
152                       }
153                     } // End of while-tags-are-detected
154                 } // end of while loop
155                 fclose($fh);
156                 fix_course_sortorder();
157             } // end of if(file_open) for first pass
159             /*
162             SECOND PASS REMOVED
163             Since the IMS specification v1.1 insists that "memberships" should come last,
164             and since vendors seem to have done this anyway (even with 1.0),
165             we can sensibly perform the import in one fell swoop.
168             // SECOND PASS: Now go through the file and process the membership entries
169             $this->xmlcache = '';
170             if (($fh = fopen($filename, "r")) != false) {
171                 $line = 0;
172                 while ((!feof($fh)) && $this->continueprocessing) {
173                     $line++;
174                     $curline = fgets($fh);
175                     $this->xmlcache .= $curline; // Add a line onto the XML cache
177                     while(true){
178                   // Must always make sure to remove tags from cache so they don't clog up our memory
179                   if($tagcontents = $this->full_tag_found_in_cache('group', $curline)){
180                           $this->remove_tag_from_cache('group');
181                       }elseif($tagcontents = $this->full_tag_found_in_cache('person', $curline)){
182                           $this->remove_tag_from_cache('person');
183                       }elseif($tagcontents = $this->full_tag_found_in_cache('membership', $curline)){
184                           $this->process_membership_tag($tagcontents);
185                           $this->remove_tag_from_cache('membership');
186                       }elseif($tagcontents = $this->full_tag_found_in_cache('comments', $curline)){
187                           $this->remove_tag_from_cache('comments');
188                       }elseif($tagcontents = $this->full_tag_found_in_cache('properties', $curline)){
189                           $this->remove_tag_from_cache('properties');
190                       }else{
191                     break;
192                   }
193                 }
194                 } // end of while loop
195                 fclose($fh);
196             } // end of if(file_open) for second pass
199            */
201             $timeelapsed = time() - $starttime;
202             $this->log_line('Process has completed. Time taken: '.$timeelapsed.' seconds.');
205         } // END of "if file is new"
208         // These variables are stored so we can compare them against the IMS file, next time round.
209         $this->set_config('prev_time', $filemtime);
210         $this->set_config('prev_md5',  $md5);
211         $this->set_config('prev_path', $filename);
215     }else{ // end of if(file_exists)
216         $this->log_line('File not found: '.$filename);
217     }
219     if (!empty($mailadmins)) {
220         $msg = "An IMS enrolment has been carried out within Moodle.\nTime taken: $timeelapsed seconds.\n\n";
221         if(!empty($logtolocation)){
222             if($this->logfp){
223                 $msg .= "Log data has been written to:\n";
224                 $msg .= "$logtolocation\n";
225                 $msg .= "(Log file size: ".ceil(filesize($logtolocation)/1024)."Kb)\n\n";
226             }else{
227                 $msg .= "The log file appears not to have been successfully written.\nCheck that the file is writeable by the server:\n";
228                 $msg .= "$logtolocation\n\n";
229             }
230         }else{
231             $msg .= "Logging is currently not active.";
232         }
234         $eventdata = new stdClass();
235         $eventdata->modulename        = 'moodle';
236         $eventdata->component         = 'imsenterprise';
237         $eventdata->name              = 'imsenterprise_enrolment';
238         $eventdata->userfrom          = get_admin();
239         $eventdata->userto            = get_admin();
240         $eventdata->subject           = "Moodle IMS Enterprise enrolment notification";
241         $eventdata->fullmessage       = $msg;
242         $eventdata->fullmessageformat = FORMAT_PLAIN;
243         $eventdata->fullmessagehtml   = '';
244         $eventdata->smallmessage      = '';
245         message_send($eventdata);
247         $this->log_line('Notification email sent to administrator.');
249     }
251     if($this->logfp){
252       fclose($this->logfp);
253     }
256 } // end of cron() function
258 /**
259 * Check if a complete tag is found in the cached data, which usually happens
260 * when the end of the tag has only just been loaded into the cache.
261 * Returns either false, or the contents of the tag (including start and end).
262 * @param string $tagname Name of tag to look for
263 * @param string $latestline The very last line in the cache (used for speeding up the match)
264 */
265 function full_tag_found_in_cache($tagname, $latestline){ // Return entire element if found. Otherwise return false.
266     if(strpos(strtolower($latestline), '</'.strtolower($tagname).'>')===false){
267         return false;
268     }elseif(preg_match('{(<'.$tagname.'\b.*?>.*?</'.$tagname.'>)}is', $this->xmlcache, $matches)){
269         return $matches[1];
270     }else return false;
273 /**
274 * Remove complete tag from the cached data (including all its contents) - so
275 * that the cache doesn't grow to unmanageable size
276 * @param string $tagname Name of tag to look for
277 */
278 function remove_tag_from_cache($tagname){ // Trim the cache so we're not in danger of running out of memory.
279     ///echo "<p>remove_tag_from_cache: $tagname</p>";  flush();  ob_flush();
280     //  echo "<p>remove_tag_from_cache:<br />".htmlspecialchars($this->xmlcache);
281     $this->xmlcache = trim(preg_replace('{<'.$tagname.'\b.*?>.*?</'.$tagname.'>}is', '', $this->xmlcache, 1)); // "1" so that we replace only the FIRST instance
282     //  echo "<br />".htmlspecialchars($this->xmlcache)."</p>";
285 /**
286 * Very simple convenience function to return the "recstatus" found in person/group/role tags.
287 * 1=Add, 2=Update, 3=Delete, as specified by IMS, and we also use 0 to indicate "unspecified".
288 * @param string $tagdata the tag XML data
289 * @param string $tagname the name of the tag we're interested in
290 */
291 function get_recstatus($tagdata, $tagname){
292     if(preg_match('{<'.$tagname.'\b[^>]*recstatus\s*=\s*["\'](\d)["\']}is', $tagdata, $matches)){
293         // echo "<p>get_recstatus($tagname) found status of $matches[1]</p>";
294         return intval($matches[1]);
295     }else{
296         // echo "<p>get_recstatus($tagname) found nothing</p>";
297         return 0; // Unspecified
298     }
301 /**
302 * Process the group tag. This defines a Moodle course.
303 * @param string $tagconents The raw contents of the XML element
304 */
305 function process_group_tag($tagcontents) {
306     global $DB;
308     // Get configs
309     $truncatecoursecodes    = $this->get_config('truncatecoursecodes');
310     $createnewcourses       = $this->get_config('createnewcourses');
311     $createnewcategories    = $this->get_config('createnewcategories');
313     // Process tag contents
314     $group = new stdClass();
315     if (preg_match('{<sourcedid>.*?<id>(.+?)</id>.*?</sourcedid>}is', $tagcontents, $matches)) {
316         $group->coursecode = trim($matches[1]);
317     }
318     if (preg_match('{<description>.*?<long>(.*?)</long>.*?</description>}is', $tagcontents, $matches)){
319         $group->description = trim($matches[1]);
320     }
321     if (preg_match('{<description>.*?<short>(.*?)</short>.*?</description>}is', $tagcontents, $matches)) {
322         $group->shortName = trim($matches[1]);
323     }
324     if (preg_match('{<org>.*?<orgunit>(.*?)</orgunit>.*?</org>}is', $tagcontents, $matches)) {
325         $group->category = trim($matches[1]);
326     }
328     $recstatus = ($this->get_recstatus($tagcontents, 'group'));
329     //echo "<p>get_recstatus for this group returned $recstatus</p>";
331     if (!(strlen($group->coursecode)>0)) {
332         $this->log_line('Error at line '.$line.': Unable to find course code in \'group\' element.');
333     } else {
334         // First, truncate the course code if desired
335         if (intval($truncatecoursecodes)>0) {
336             $group->coursecode = ($truncatecoursecodes > 0)
337                      ? substr($group->coursecode, 0, intval($truncatecoursecodes))
338                      : $group->coursecode;
339         }
341         /* -----------Course aliasing is DEACTIVATED until a more general method is in place---------------
343        // Second, look in the course alias table to see if the code should be translated to something else
344         if($aliases = $DB->get_field('enrol_coursealias', 'toids', array('fromid'=>$group->coursecode))){
345             $this->log_line("Found alias of course code: Translated $group->coursecode to $aliases");
346             // Alias is allowed to be a comma-separated list, so let's split it
347             $group->coursecode = explode(',', $aliases);
348         }
349        */
351         // For compatibility with the (currently inactive) course aliasing, we need this to be an array
352         $group->coursecode = array($group->coursecode);
354         // Third, check if the course(s) exist
355         foreach ($group->coursecode as $coursecode) {
356             $coursecode = trim($coursecode);
357             if (!$DB->get_field('course', 'id', array('idnumber'=>$coursecode))) {
358                 if (!$createnewcourses) {
359                     $this->log_line("Course $coursecode not found in Moodle's course idnumbers.");
360                 } else {
361                     // Set shortname to description or description to shortname if one is set but not the other.
362                     $nodescription = !isset($group->description);
363                     $noshortname = !isset($group->shortName);
364                     if ( $nodescription && $noshortname) {
365                         // If neither short nor long description are set let if fail
366                         $this->log_line("Neither long nor short name are set for $coursecode");
367                     } else if ($nodescription) {
368                         // If short and ID exist, then give the long short's value, then give short the ID's value
369                         $group->description = $group->shortName;
370                         $group->shortName = $coursecode;
371                     } else if ($noshortname) {
372                         // If long and ID exist, then map long to long, then give short the ID's value.
373                         $group->shortName = $coursecode;
374                     }
375                     // Create the (hidden) course(s) if not found
376                     $courseconfig = get_config('moodlecourse'); // Load Moodle Course shell defaults
377                     $course = new stdClass();
378                     $course->fullname = $group->description;
379                     $course->shortname = $group->shortName;;
380                     $course->idnumber = $coursecode;
381                     $course->format = $courseconfig->format;
382                     $course->visible = $courseconfig->visible;
383                     $course->numsections = $courseconfig->numsections;
384                     $course->hiddensections = $courseconfig->hiddensections;
385                     $course->newsitems = $courseconfig->newsitems;
386                     $course->showgrades = $courseconfig->showgrades;
387                     $course->showreports = $courseconfig->showreports;
388                     $course->maxbytes = $courseconfig->maxbytes;
389                     $course->groupmode = $courseconfig->groupmode;
390                     $course->groupmodeforce = $courseconfig->groupmodeforce;
391                     $course->enablecompletion = $courseconfig->enablecompletion;
392                     $course->completionstartonenrol = $courseconfig->completionstartonenrol;
393                     // Insert default names for teachers/students, from the current language
395                     // Handle course categorisation (taken from the group.org.orgunit field if present)
396                     if (strlen($group->category)>0) {
397                         // If the category is defined and exists in Moodle, we want to store it in that one
398                         if ($catid = $DB->get_field('course_categories', 'id', array('name'=>$group->category))) {
399                             $course->category = $catid;
400                         } else if ($createnewcategories) {
401                             // Else if we're allowed to create new categories, let's create this one
402                             $newcat = new stdClass();
403                             $newcat->name = $group->category;
404                             $newcat->visible = 0;
405                             $catid = $DB->insert_record('course_categories', $newcat);
406                             $course->category = $catid;
407                             $this->log_line("Created new (hidden) category, #$catid: $newcat->name");
408                         } else {
409                             // If not found and not allowed to create, stick with default
410                             $this->log_line('Category '.$group->category.' not found in Moodle database, so using default category instead.');
411                             $course->category = 1;
412                         }
413                     } else {
414                         $course->category = 1;
415                     }
416                     $course->timecreated = time();
417                     $course->startdate = time();
418                     // Choose a sort order that puts us at the start of the list!
419                     $course->sortorder = 0;
421                     $courseid = $DB->insert_record('course', $course);
423                     // Setup default enrolment plugins
424                     $course->id = $courseid;
425                     enrol_course_updated(true, $course, null);
427                     // Setup the blocks
428                     $course = $DB->get_record('course', array('id' => $courseid));
429                     blocks_add_default_course_blocks($course);
431                     $section = new stdClass();
432                     $section->course = $course->id;   // Create a default section.
433                     $section->section = 0;
434                     $section->summaryformat = FORMAT_HTML;
435                     $section->id = $DB->insert_record("course_sections", $section);
437                     add_to_log(SITEID, "course", "new", "view.php?id=$course->id", "$course->fullname (ID $course->id)");
439                     $this->log_line("Created course $coursecode in Moodle (Moodle ID is $course->id)");
440                 }
441             } else if ($recstatus==3 && ($courseid = $DB->get_field('course', 'id', array('idnumber'=>$coursecode)))) {
442                 // If course does exist, but recstatus==3 (delete), then set the course as hidden
443                 $DB->set_field('course', 'visible', '0', array('id'=>$courseid));
444             }
445         } // End of foreach(coursecode)
446     }
447 } // End process_group_tag()
449 /**
450 * Process the person tag. This defines a Moodle user.
451 * @param string $tagconents The raw contents of the XML element
452 */
453 function process_person_tag($tagcontents){
454     global $CFG, $DB;
456     // Get plugin configs
457     $imssourcedidfallback   = $this->get_config('imssourcedidfallback');
458     $fixcaseusernames       = $this->get_config('fixcaseusernames');
459     $fixcasepersonalnames   = $this->get_config('fixcasepersonalnames');
460     $imsdeleteusers         = $this->get_config('imsdeleteusers');
461     $createnewusers         = $this->get_config('createnewusers');
463     $person = new stdClass();
464     if(preg_match('{<sourcedid>.*?<id>(.+?)</id>.*?</sourcedid>}is', $tagcontents, $matches)){
465         $person->idnumber = trim($matches[1]);
466     }
467     if(preg_match('{<name>.*?<n>.*?<given>(.+?)</given>.*?</n>.*?</name>}is', $tagcontents, $matches)){
468         $person->firstname = trim($matches[1]);
469     }
470     if(preg_match('{<name>.*?<n>.*?<family>(.+?)</family>.*?</n>.*?</name>}is', $tagcontents, $matches)){
471         $person->lastname = trim($matches[1]);
472     }
473     if(preg_match('{<userid>(.*?)</userid>}is', $tagcontents, $matches)){
474         $person->username = trim($matches[1]);
475     }
476     if($imssourcedidfallback && trim($person->username)==''){
477       // This is the point where we can fall back to useing the "sourcedid" if "userid" is not supplied
478       // NB We don't use an "elseif" because the tag may be supplied-but-empty
479         $person->username = $person->idnumber;
480     }
481     if(preg_match('{<email>(.*?)</email>}is', $tagcontents, $matches)){
482         $person->email = trim($matches[1]);
483     }
484     if(preg_match('{<url>(.*?)</url>}is', $tagcontents, $matches)){
485         $person->url = trim($matches[1]);
486     }
487     if(preg_match('{<adr>.*?<locality>(.+?)</locality>.*?</adr>}is', $tagcontents, $matches)){
488         $person->city = trim($matches[1]);
489     }
490     if(preg_match('{<adr>.*?<country>(.+?)</country>.*?</adr>}is', $tagcontents, $matches)){
491         $person->country = trim($matches[1]);
492     }
494     // Fix case of some of the fields if required
495     if($fixcaseusernames && isset($person->username)){
496         $person->username = strtolower($person->username);
497     }
498     if($fixcasepersonalnames){
499         if(isset($person->firstname)){
500             $person->firstname = ucwords(strtolower($person->firstname));
501         }
502         if(isset($person->lastname)){
503             $person->lastname = ucwords(strtolower($person->lastname));
504         }
505     }
507     $recstatus = ($this->get_recstatus($tagcontents, 'person'));
510     // Now if the recstatus is 3, we should delete the user if-and-only-if the setting for delete users is turned on
511     // In the "users" table we can do this by setting deleted=1
512     if($recstatus==3){
514         if($imsdeleteusers){ // If we're allowed to delete user records
515             // Make sure their "deleted" field is set to one
516             $DB->set_field('user', 'deleted', 1, array('username'=>$person->username));
517             $this->log_line("Marked user record for user '$person->username' (ID number $person->idnumber) as deleted.");
518         }else{
519             $this->log_line("Ignoring deletion request for user '$person->username' (ID number $person->idnumber).");
520         }
522     }else{ // Add or update record
525         // If the user exists (matching sourcedid) then we don't need to do anything.
526         if(!$DB->get_field('user', 'id', array('idnumber'=>$person->idnumber)) && $createnewusers){
527             // If they don't exist and haven't a defined username, we log this as a potential problem.
528             if((!isset($person->username)) || (strlen($person->username)==0)){
529                 $this->log_line("Cannot create new user for ID # $person->idnumber - no username listed in IMS data for this person.");
530             } else if ($DB->get_field('user', 'id', array('username'=>$person->username))){
531                 // If their idnumber is not registered but their user ID is, then add their idnumber to their record
532                 $DB->set_field('user', 'idnumber', $person->idnumber, array('username'=>$person->username));
533             } else {
535             // If they don't exist and they have a defined username, and $createnewusers == true, we create them.
536             $person->lang = 'manual'; //TODO: this needs more work due tu multiauth changes
537             $person->auth = $CFG->auth;
538             $person->confirmed = 1;
539             $person->timemodified = time();
540             $person->mnethostid = $CFG->mnet_localhost_id;
541             $id = $DB->insert_record('user', $person);
542     /*
543     Photo processing is deactivated until we hear from Moodle dev forum about modification to gdlib.
545                                  //Antoni Mas. 07/12/2005. If a photo URL is specified then we might want to load
546                                  // it into the user's profile. Beware that this may cause a heavy overhead on the server.
547                                  if($CFG->enrol_processphoto){
548                                    if(preg_match('{<photo>.*?<extref>(.*?)</extref>.*?</photo>}is', $tagcontents, $matches)){
549                                      $person->urlphoto = trim($matches[1]);
550                                    }
551                                    //Habilitam el flag que ens indica que el personatge t foto prpia.
552                                    $person->picture = 1;
553                                    //Llibreria creada per nosaltres mateixos.
554                                    require_once($CFG->dirroot.'/lib/gdlib.php');
555                                    if ($usernew->picture = save_profile_image($id, $person->urlphoto,'user')) { TODO: use process_new_icon() instead
556                                      $DB->set_field('user', 'picture', $usernew->picture, array('id'=>$id));  /// Note picture in DB
557                                    }
558                                  }
559     */
560                 $this->log_line("Created user record for user '$person->username' (ID number $person->idnumber).");
561             }
562         } elseif ($createnewusers) {
563             $this->log_line("User record already exists for user '$person->username' (ID number $person->idnumber).");
565             // Make sure their "deleted" field is set to zero.
566             $DB->set_field('user', 'deleted', 0, array('idnumber'=>$person->idnumber));
567         }else{
568             $this->log_line("No user record found for '$person->username' (ID number $person->idnumber).");
569         }
571     } // End of are-we-deleting-or-adding
573 } // End process_person_tag()
575 /**
576 * Process the membership tag. This defines whether the specified Moodle users
577 * should be added/removed as teachers/students.
578 * @param string $tagconents The raw contents of the XML element
579 */
580 function process_membership_tag($tagcontents){
581     global $DB;
583     // Get plugin configs
584     $truncatecoursecodes = $this->get_config('truncatecoursecodes');
585     $imscapitafix = $this->get_config('imscapitafix');
587     $memberstally = 0;
588     $membersuntally = 0;
590     // In order to reduce the number of db queries required, group name/id associations are cached in this array:
591     $groupids = array();
593     $ship = new stdClass();
595     if(preg_match('{<sourcedid>.*?<id>(.+?)</id>.*?</sourcedid>}is', $tagcontents, $matches)){
596         $ship->coursecode = ($truncatecoursecodes > 0)
597                                  ? substr(trim($matches[1]), 0, intval($truncatecoursecodes))
598                                  : trim($matches[1]);
599         $ship->courseid = $DB->get_field('course', 'id', array('idnumber'=>$ship->coursecode));
600     }
601     if($ship->courseid && preg_match_all('{<member>(.*?)</member>}is', $tagcontents, $membermatches, PREG_SET_ORDER)){
602         $courseobj = new stdClass();
603         $courseobj->id = $ship->courseid;
605         foreach($membermatches as $mmatch){
606             $member = new stdClass();
607             $memberstoreobj = new stdClass();
608             if(preg_match('{<sourcedid>.*?<id>(.+?)</id>.*?</sourcedid>}is', $mmatch[1], $matches)){
609                 $member->idnumber = trim($matches[1]);
610             }
611             if(preg_match('{<role\s+roletype=["\'](.+?)["\'].*?>}is', $mmatch[1], $matches)){
612                 $member->roletype = trim($matches[1]); // 01 means Student, 02 means Instructor, 3 means ContentDeveloper, and there are more besides
613             } elseif($imscapitafix && preg_match('{<roletype>(.+?)</roletype>}is', $mmatch[1], $matches)){
614                 // The XML that comes out of Capita Student Records seems to contain a misinterpretation of the IMS specification!
615                 $member->roletype = trim($matches[1]); // 01 means Student, 02 means Instructor, 3 means ContentDeveloper, and there are more besides
616             }
617             if(preg_match('{<role\b.*?<status>(.+?)</status>.*?</role>}is', $mmatch[1], $matches)){
618                 $member->status = trim($matches[1]); // 1 means active, 0 means inactive - treat this as enrol vs unenrol
619             }
621             $recstatus = ($this->get_recstatus($mmatch[1], 'role'));
622             if($recstatus==3){
623               $member->status = 0; // See above - recstatus of 3 (==delete) is treated the same as status of 0
624               //echo "<p>process_membership_tag: unenrolling member due to recstatus of 3</p>";
625             }
627             $timeframe = new stdClass();
628             $timeframe->begin = 0;
629             $timeframe->end = 0;
630             if(preg_match('{<role\b.*?<timeframe>(.+?)</timeframe>.*?</role>}is', $mmatch[1], $matches)){
631                 $timeframe = $this->decode_timeframe($matches[1]);
632             }
633             if(preg_match('{<role\b.*?<extension>.*?<cohort>(.+?)</cohort>.*?</extension>.*?</role>}is', $mmatch[1], $matches)){
634                 $member->groupname = trim($matches[1]);
635                 // The actual processing (ensuring a group record exists, etc) occurs below, in the enrol-a-student clause
636             }
638             $rolecontext = get_context_instance(CONTEXT_COURSE, $ship->courseid);
639             $rolecontext = $rolecontext->id; // All we really want is the ID
640 //$this->log_line("Context instance for course $ship->courseid is...");
641 //print_r($rolecontext);
643             // Add or remove this student or teacher to the course...
644             $memberstoreobj->userid = $DB->get_field('user', 'id', array('idnumber'=>$member->idnumber));
645             $memberstoreobj->enrol = 'imsenterprise';
646             $memberstoreobj->course = $ship->courseid;
647             $memberstoreobj->time = time();
648             $memberstoreobj->timemodified = time();
649             if($memberstoreobj->userid){
651                 // Decide the "real" role (i.e. the Moodle role) that this user should be assigned to.
652                 // Zero means this roletype is supposed to be skipped.
653                 $moodleroleid = $this->rolemappings[$member->roletype];
654                 if(!$moodleroleid) {
655                     $this->log_line("SKIPPING role $member->roletype for $memberstoreobj->userid ($member->idnumber) in course $memberstoreobj->course");
656                     continue;
657                 }
659                 if(intval($member->status) == 1) {
660                     // Enrol the member
662                     $einstance = $DB->get_record('enrol',
663                                     array('courseid' => $courseobj->id, 'enrol' => $memberstoreobj->enrol));
664                     if (empty($einstance)) {
665                         // Only add an enrol instance to the course if non-existent
666                         $enrolid = $this->add_instance($courseobj);
667                         $einstance = $DB->get_record('enrol', array('id' => $enrolid));
668                     }
670                     $this->enrol_user($einstance, $memberstoreobj->userid, $moodleroleid, $timeframe->begin, $timeframe->end);
672                     $this->log_line("Enrolled user #$memberstoreobj->userid ($member->idnumber) to role $member->roletype in course $memberstoreobj->course");
673                     $memberstally++;
675                     // At this point we can also ensure the group membership is recorded if present
676                     if(isset($member->groupname)){
677                         // Create the group if it doesn't exist - either way, make sure we know the group ID
678                         if(isset($groupids[$member->groupname])) {
679                             $member->groupid = $groupids[$member->groupname]; // Recall the group ID from cache if available
680                         } else {
681                             if($groupid = $DB->get_field('groups', 'id', array('courseid'=>$ship->courseid, 'name'=>$member->groupname))){
682                                 $member->groupid = $groupid;
683                                 $groupids[$member->groupname] = $groupid; // Store ID in cache
684                             } else {
685                                 // Attempt to create the group
686                                 $group = new stdClass();
687                                 $group->name = $member->groupname;
688                                 $group->courseid = $ship->courseid;
689                                 $group->timecreated = time();
690                                 $group->timemodified = time();
691                                 $groupid = $DB->insert_record('groups', $group);
692                                 $this->log_line('Added a new group for this course: '.$group->name);
693                                 $groupids[$member->groupname] = $groupid; // Store ID in cache
694                                 $member->groupid = $groupid;
695                             }
696                         }
697                         // Add the user-to-group association if it doesn't already exist
698                         if($member->groupid) {
699                             groups_add_member($member->groupid, $memberstoreobj->userid);
700                         }
701                     } // End of group-enrolment (from member.role.extension.cohort tag)
703                 } elseif ($this->get_config('imsunenrol')) {
704                     // Unenrol member
706                     $einstances = $DB->get_records('enrol',
707                                     array('enrol' => $memberstoreobj->enrol, 'courseid' => $courseobj->id));
708                     foreach ($einstances as $einstance) {
709                         // Unenrol the user from all imsenterprise enrolment instances
710                         $this->unenrol_user($einstance, $memberstoreobj->userid);
711                     }
713                     $membersuntally++;
714                     $this->log_line("Unenrolled $member->idnumber from role $moodleroleid in course");
715                 }
717             }
718         }
719         $this->log_line("Added $memberstally users to course $ship->coursecode");
720         if($membersuntally > 0){
721             $this->log_line("Removed $membersuntally users from course $ship->coursecode");
722         }
723     }
724 } // End process_membership_tag()
726 /**
727 * Process the properties tag. The only data from this element
728 * that is relevant is whether a <target> is specified.
729 * @param string $tagconents The raw contents of the XML element
730 */
731 function process_properties_tag($tagcontents){
732     $imsrestricttarget = $this->get_config('imsrestricttarget');
734     if ($imsrestricttarget) {
735         if(!(preg_match('{<target>'.preg_quote($imsrestricttarget).'</target>}is', $tagcontents, $matches))){
736             $this->log_line("Skipping processing: required target \"$imsrestricttarget\" not specified in this data.");
737             $this->continueprocessing = false;
738         }
739     }
742 /**
743 * Store logging information. This does two things: uses the {@link mtrace()}
744 * function to print info to screen/STDOUT, and also writes log to a text file
745 * if a path has been specified.
746 * @param string $string Text to write (newline will be added automatically)
747 */
748 function log_line($string){
749     mtrace($string);
750     if($this->logfp) {
751         fwrite($this->logfp, $string . "\n");
752     }
755 /**
756 * Process the INNER contents of a <timeframe> tag, to return beginning/ending dates.
757 */
758 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
759     $ret = new stdClass();
760     $ret->begin = $ret->end = 0;
761     // Explanatory note: The matching will ONLY match if the attribute restrict="1"
762     // because otherwise the time markers should be ignored (participation should be
763     // allowed outside the period)
764     if(preg_match('{<begin\s+restrict="1">(\d\d\d\d)-(\d\d)-(\d\d)</begin>}is', $string, $matches)){
765         $ret->begin = mktime(0,0,0, $matches[2], $matches[3], $matches[1]);
766     }
767     if(preg_match('{<end\s+restrict="1">(\d\d\d\d)-(\d\d)-(\d\d)</end>}is', $string, $matches)){
768         $ret->end = mktime(0,0,0, $matches[2], $matches[3], $matches[1]);
769     }
770     return $ret;
771 } // End decode_timeframe
773 /**
774 * Load the role mappings (from the config), so we can easily refer to
775 * how an IMS-E role corresponds to a Moodle role
776 */
777 function load_role_mappings() {
778     require_once('locallib.php');
779     global $DB;
781     $imsroles = new imsenterprise_roles();
782     $imsroles = $imsroles->get_imsroles();
784     $this->rolemappings = array();
785     foreach($imsroles as $imsrolenum=>$imsrolename) {
786         $this->rolemappings[$imsrolenum] = $this->rolemappings[$imsrolename] = $this->get_config('imsrolemap' . $imsrolenum);
787     }
790 } // end of class