/** * Returns the user who is responsible for self enrolments in given instance. * * Usually it is the first editing teacher - the person with "highest authority" * as defined by sort_by_roleassignment_authority() having 'enrol/self:manage' * capability. * * @param int $instanceid enrolment instance id * @return stdClass user record */ protected function get_enroller($instanceid) { global $DB; if ($this->lasternollerinstanceid == $instanceid and $this->lasternoller) { return $this->lasternoller; } $instance = $DB->get_record('enrol', array('id' => $instanceid, 'enrol' => $this->get_name()), '*', MUST_EXIST); $context = context_course::instance($instance->courseid); if ($users = get_enrolled_users($context, 'enrol/self:manage')) { $users = sort_by_roleassignment_authority($users, $context); $this->lasternoller = reset($users); unset($users); } else { $this->lasternoller = parent::get_enroller($instanceid); } $this->lasternollerinstanceid = $instanceid; return $this->lasternoller; }