MDL-56282 enrol_meta: Fix error when editing existing instances

We had a few problems here.

1/ The auto complete element was displaying the picker though the
   field was frozen.
2/ Checking for existing enrolment including the courses chosen was
   not checking if the courses found were in the instance we edit.
3/ Upon creation we support multiple courses but when editing we
   should not expect more than the course in the instance being edited.
This commit is contained in:
Frédéric Massart 2016-10-06 11:48:22 +02:00 committed by Damyon Wiese
parent 09a05b798b
commit 77bec234e0
2 changed files with 18 additions and 6 deletions

View File

@ -332,7 +332,7 @@ class enrol_meta_plugin extends enrol_plugin {
$options = array(
'requiredcapabilities' => array('enrol/meta:selectaslinked'),
'multiple' => true,
'multiple' => empty($instance->id), // We only accept multiple values on creation.
'exclude' => $excludelist
);
$mform->addElement('course', 'customint1', get_string('linkedcourse', 'enrol_meta'), $options);
@ -362,15 +362,24 @@ class enrol_meta_plugin extends enrol_plugin {
$c = false;
if (!empty($data['customint1'])) {
foreach ($data['customint1'] as $courseid) {
$courses = is_array($data['customint1']) ? $data['customint1'] : [$data['customint1']];
foreach ($courses as $courseid) {
$c = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
$coursecontext = context_course::instance($c->id);
$existing = $DB->get_records('enrol', array('enrol' => 'meta', 'courseid' => $thiscourseid), '', 'customint1, id');
$sqlexists = 'enrol = :meta AND courseid = :currentcourseid AND customint1 = :courseid AND id != :id';
$existing = $DB->record_exists_select('enrol', $sqlexists, [
'meta' => 'meta',
'currentcourseid' => $thiscourseid,
'courseid' => $c->id,
'id' => $instance->id
]);
if (!$c->visible and !has_capability('moodle/course:viewhiddencourses', $coursecontext)) {
$errors['customint1'] = get_string('error');
} else if (!has_capability('enrol/meta:selectaslinked', $coursecontext)) {
$errors['customint1'] = get_string('error');
} else if ($c->id == SITEID or $c->id == $thiscourseid or isset($existing[$c->id])) {
} else if ($c->id == SITEID or $c->id == $thiscourseid or $existing) {
$errors['customint1'] = get_string('error');
}
}

View File

@ -121,8 +121,11 @@ class MoodleQuickForm_autocomplete extends MoodleQuickForm_select {
// Enhance the select with javascript.
$this->_generateId();
$id = $this->getAttribute('id');
$PAGE->requires->js_call_amd('core/form-autocomplete', 'enhance', $params = array('#' . $id, $this->tags, $this->ajax,
$this->placeholder, $this->casesensitive, $this->showsuggestions, $this->noselectionstring));
if (!$this->isFrozen()) {
$PAGE->requires->js_call_amd('core/form-autocomplete', 'enhance', $params = array('#' . $id, $this->tags, $this->ajax,
$this->placeholder, $this->casesensitive, $this->showsuggestions, $this->noselectionstring));
}
return parent::toHTML();
}