/** * Perform all necessary tasks to save a student enrolment. * @return bool true on success * @throws Exception * @uses $DB * @uses $USER * @uses events_trigger() */ function save() { global $DB, $USER; try { validation_helper::is_unique_userid_classid($this); } catch (Exception $e) { // already enrolled -- pretend we succeeded // error_log('student.class::add() - student already enrolled!'); // Note: this prevents Moodle Course enrolment when Moodle Course is // attached *after* initial student enrolment // forcing student to be unenroled then re-enroled to get enrolled // in the newly attached Moodle Course return true; } //set end time based on class duration if (empty($this->id) && empty($this->endtime) && !empty($this->classid)) { $studentclass = $this->pmclass; $duration = $studentclass->duration; if (!empty($duration)) { $this->endtime = $this->enrolmenttime + $duration; } else { // no class duration -> no end time $this->endtime = 0; } } if ($this->completestatusid != STUSTATUS_NOTCOMPLETE) { $this->update(); } else { parent::save(); } /// Enrol them into the Moodle class, if not already enrolled. if (empty($this->no_moodle_enrol) && ($moodlecourseid = moodle_get_course($this->classid))) { if ($mcourse = $this->_db->get_record('course', array('id' => $moodlecourseid))) { $plugin = enrol_get_plugin('elis'); $enrol = $plugin->get_or_create_instance($mcourse); $user = $this->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; } $context = context_course::instance($moodlecourseid); if (!is_enrolled($context, $muserid)) { $flag = false; if (empty($USER) || !isset($USER->id)) { $flag = true; if (!is_object($USER)) { $saveuser = $USER; $USER = new stdClass(); } else { $saveuser = clone $USER; } $USER->id = get_admin()->id; } $caughtex = null; try { $plugin->enrol_user($enrol, $muserid, $enrol->roleid, $this->enrolmenttime, $this->endtime ? $this->endtime : 0); } catch (Exception $e) { $caughtex = $e; } if ($flag) { $USER = $saveuser; } if ($caughtex) { throw $caughtex; } } } } else { $sturole = get_config('elisprogram_enrolrolesync', 'student_role'); // ELIS-3397: must still trigger events for notifications $sturole = get_config('elisprogram_enrolrolesync', 'student_role'); $ra = new stdClass(); $ra->roleid = !empty($sturole) ? $sturole : $DB->get_field('role', 'id', array('shortname' => 'student')); $ra->contextid = \local_elisprogram\context\pmclass::instance($this->classid)->id; $ra->userid = cm_get_moodleuserid($this->userid); $ra->component = 'enrol_elis'; $ra->timemodified = time(); $ra->modifierid = empty($USER->id) ? 0 : $USER->id; events_trigger('role_assigned', $ra); } return; }
/** * */ public function save() { $new = false; try { validation_helper::is_unique_userid_classid($this); } catch (Exception $e) { // already on waitlist return true; } if (!isset($this->id)) { $new = true; if (empty($this->position)) { $max = $this->_db->get_field(waitlist::TABLE, 'MAX(position)', array('classid' => $this->classid)); $this->position = $max + 1; } } parent::save(); $sendnotification = !empty(elis::$config->local_elisprogram->notify_addedtowaitlist_user) ? true : false; if ($new && $sendnotification === true) { $subject = get_string('waitlist', self::LANG_FILE); $pmclass = new pmclass($this->classid); $sparam = new stdClass(); $sparam->idnumber = $pmclass->idnumber; $message = get_string('added_to_waitlist_message', self::LANG_FILE, $sparam); $cuser = new user($this->userid); $cuser->load(); $from = get_admin(); notification::notify($message, $cuser, $from); //email_to_user($user, $from, $subject, $message); // *TBD* } }