From a379029c54134679d1f7bf465be2e8d024a0aa98 Mon Sep 17 00:00:00 2001 From: Petr Skoda Date: Fri, 23 Apr 2010 09:10:05 +0000 Subject: [PATCH] MDL-21781 cohort id duplicates detection --- cohort/edit_form.php | 26 +++++++++++++++++--------- lang/en/cohort.php | 5 +++-- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/cohort/edit_form.php b/cohort/edit_form.php index c62322ca640..e286a23f45d 100644 --- a/cohort/edit_form.php +++ b/cohort/edit_form.php @@ -24,14 +24,14 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ - -require_once($CFG->dirroot.'/lib/formslib.php'); +require_once($CFG->dirroot . '/lib/formslib.php'); class cohort_edit_form extends moodleform { - // Define the form - function definition () { - global $USER, $CFG, $COURSE; + /** + * Define the form + */ + function definition() { $mform = $this->_form; $editoroptions = $this->_customdata['editoroptions']; @@ -46,10 +46,10 @@ class cohort_edit_form extends moodleform { $mform->addElement('editor', 'description_editor', get_string('description', 'cohort'), null, $editoroptions); $mform->setType('description_editor', PARAM_RAW); - $mform->addElement('hidden','id'); + $mform->addElement('hidden', 'id'); $mform->setType('id', PARAM_INT); - $mform->addElement('hidden','contextid'); + $mform->addElement('hidden', 'contextid'); $mform->setType('contextid', PARAM_INT); $this->add_action_buttons(); @@ -59,11 +59,19 @@ class cohort_edit_form extends moodleform { global $DB; $errors = parent::validation($data, $files); - $textlib = textlib_get_instance(); $idnumber = trim($data['idnumber']); if ($data['id']) { - //TODO: validate there are no idnumber + $current = $DB->get_record('cohort', array('id'=>$data['id']), '*', MUST_EXIST); + if ($current->idnumber !== $idnumber) { + if ($DB->record_exists('cohort', array('idnumber'=>$idnumber))) { + $errors['idnumber'] = get_string('duplicateidnumber', 'cohort'); + } + } + } else { + if ($DB->record_exists('cohort', array('idnumber'=>$idnumber))) { + $errors['idnumber'] = get_string('duplicateidnumber', 'cohort'); + } } return $errors; diff --git a/lang/en/cohort.php b/lang/en/cohort.php index 78a27720048..e673550c1fc 100644 --- a/lang/en/cohort.php +++ b/lang/en/cohort.php @@ -37,10 +37,11 @@ $string['component'] = 'Source'; $string['currentusers'] = 'Current users'; $string['currentusersmatching'] = 'Current users matching'; $string['delcohort'] = 'Delete cohort'; -$string['delconfirm'] = 'Do you really want to delete cohort $a?'; +$string['delconfirm'] = 'Do you really want to delete cohort \'{$a}\'?'; $string['description'] = 'Description'; +$string['duplicateidnumber'] = 'Cohort with the same ID number already exists'; $string['editcohort'] = 'Edit cohort'; -$string['idnumber'] = 'ID number'; +$string['idnumber'] = 'Cohort ID'; $string['memberscount'] = 'Cohort size'; $string['name'] = 'Name'; $string['nocomponent'] = 'Created manually';