Merge branch 'MDL-32570-master' of git://github.com/danpoltawski/moodle
authorDamyon Wiese <damyon@moodle.com>
Tue, 8 Oct 2013 05:47:49 +0000 (13:47 +0800)
committerDamyon Wiese <damyon@moodle.com>
Tue, 8 Oct 2013 05:47:49 +0000 (13:47 +0800)
1  2 
enrol/imsenterprise/lib.php

@@@ -85,7 -85,6 +85,7 @@@ function cron() 
          $this->logfp = fopen($logtolocation, 'a');
      }
  
 +    $fileisnew = false;
      if ( file_exists($filename) ) {
          @set_time_limit(0);
          $starttime = time();
          if(empty($prev_path)  || ($filename != $prev_path)) {
              $fileisnew = true;
          } elseif(isset($prev_time) && ($filemtime <= $prev_time)) {
 -            $fileisnew = false;
              $this->log_line('File modification time is not more recent than last update - skipping processing.');
          } elseif(isset($prev_md5) && ($md5 == $prev_md5)) {
 -            $fileisnew = false;
              $this->log_line('File MD5 hash is same as on last update - skipping processing.');
          } else {
              $fileisnew = true; // Let's process it!
          $this->log_line('File not found: '.$filename);
      }
  
 -    if (!empty($mailadmins)) {
 +    if (!empty($mailadmins) && $fileisnew) {
          $msg = "An IMS enrolment has been carried out within Moodle.\nTime taken: $timeelapsed seconds.\n\n";
          if(!empty($logtolocation)){
              if($this->logfp){
  
          $eventdata = new stdClass();
          $eventdata->modulename        = 'moodle';
 -        $eventdata->component         = 'imsenterprise';
 +        $eventdata->component         = 'enrol_imsenterprise';
          $eventdata->name              = 'imsenterprise_enrolment';
          $eventdata->userfrom          = get_admin();
          $eventdata->userto            = get_admin();
@@@ -399,7 -400,7 +399,7 @@@ function process_group_tag($tagcontents
                      // Insert default names for teachers/students, from the current language
  
                      // Handle course categorisation (taken from the group.org.orgunit field if present)
 -                    if (strlen($group->category)>0) {
 +                    if (!empty($group->category)) {
                          // If the category is defined and exists in Moodle, we want to store it in that one
                          if ($catid = $DB->get_field('course_categories', 'id', array('name'=>$group->category))) {
                              $course->category = $catid;
                          } else {
                              // If not found and not allowed to create, stick with default
                              $this->log_line('Category '.$group->category.' not found in Moodle database, so using default category instead.');
-                             $course->category = 1;
+                             $course->category = $this->get_default_category_id();
                          }
                      } else {
-                         $course->category = 1;
+                         $course->category = $this->get_default_category_id();
                      }
                      $course->timecreated = time();
                      $course->startdate = time();
@@@ -833,6 -834,27 +833,27 @@@ function load_role_mappings() 
      }
  
  
+     /**
+      * Get the default category id (often known as 'Miscellaneous'),
+      * statically cached to avoid multiple DB lookups on big imports.
+      *
+      * @return int id of default category.
+      */
+     private function get_default_category_id() {
+         global $CFG;
+         require_once($CFG->libdir.'/coursecatlib.php');
+         static $defaultcategoryid = null;
+         if ($defaultcategoryid === null) {
+             $category = coursecat::get_default();
+             $defaultcategoryid = $category->id;
+         }
+         return $defaultcategoryid;
+     }
  } // end of class