Exemplo n.º 1
0
 /**
  * Enrol the instructors associated with the class into the attached Moodle
  * course.
  *
  * @param none
  * @return bool True on success, False otherwise.
  */
 function data_enrol_instructors()
 {
     if (empty($this->classid) || empty($this->moodlecourseid) || !empty($this->siteconfig) && !file_exists($this->siteconfig)) {
         return false;
     }
     $instructors = instructor::find(new field_filter('classid', $this->classid));
     if (elis::$config->local_elisprogram->default_instructor_role && $instructors->valid()) {
         /// At this point we must switch over the other Moodle site's DB config, if needed
         if (!empty($this->siteconfig)) {
             // TBD: implement this in the future if needed in v2
             //$cfgbak = moodle_load_config($this->siteconfig);
         }
         /// This has to be put here in case we have a site config reload.
         $CFG = $GLOBALS['CFG'];
         if (!($context = context_course::instance($this->moodlecourseid))) {
             return false;
         }
         $plugin = enrol_get_plugin('elis');
         $enrol = $plugin->get_or_create_instance($this->_db->get_record('course', array('id' => $this->moodlecourseid)));
         foreach ($instructors as $instructor) {
             /// Make sure that a Moodle account exists for this user already.
             $user = $instructor->users;
             if (!($muser = $user->get_moodleuser())) {
                 if (!($muserid = $user->synchronize_moodle_user(true, true))) {
                     throw new Exception(get_string('errorsynchronizeuser', self::LANG_FILE));
                 }
             } else {
                 $muserid = $muser->id;
             }
             if (!is_enrolled($context, $muserid)) {
                 $plugin->enrol_user($enrol, $muserid, elis::$config->local_elisprogram->default_instructor_role, 0, 0);
             }
         }
         /// Reset $CFG object.
         if (!empty($this->siteconfig)) {
             // TBD: implement this in the future if needed in v2
             //moodle_load_config($cfgbak->dirroot . '/config.php');
         }
     }
     $instructors->close();
     return true;
 }