MDL-21781 protect cohorts created by external systesm - we can not edit them with...
[moodle.git] / cohort / edit_form.php
index c62322c..c81c0c5 100644 (file)
  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
 
  * @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 {
 
 
 class cohort_edit_form extends moodleform {
 
-    // Define the form
-    function definition () {
-        global $USER, $CFG, $COURSE;
+    /**
+     * Define the cohort edit form
+     */
+    public function definition() {
 
         $mform = $this->_form;
         $editoroptions = $this->_customdata['editoroptions'];
 
         $mform = $this->_form;
         $editoroptions = $this->_customdata['editoroptions'];
+        $cohort = $this->_customdata['data'];
 
         $mform->addElement('text', 'name', get_string('name', 'cohort'), 'maxlength="254" size="50"');
         $mform->addRule('name', get_string('required'), 'required', null, 'client');
         $mform->setType('name', PARAM_MULTILANG);
 
 
         $mform->addElement('text', 'name', get_string('name', 'cohort'), 'maxlength="254" size="50"');
         $mform->addRule('name', get_string('required'), 'required', null, 'client');
         $mform->setType('name', PARAM_MULTILANG);
 
+        $options = $this->get_category_options($cohort->contextid);
+        $mform->addElement('select', 'contextid', get_string('context', 'role'), $options);
+
         $mform->addElement('text', 'idnumber', get_string('idnumber', 'cohort'), 'maxlength="254" size="50"');
         $mform->setType('name', PARAM_RAW);
 
         $mform->addElement('editor', 'description_editor', get_string('description', 'cohort'), null, $editoroptions);
         $mform->setType('description_editor', PARAM_RAW);
 
         $mform->addElement('text', 'idnumber', get_string('idnumber', 'cohort'), 'maxlength="254" size="50"');
         $mform->setType('name', PARAM_RAW);
 
         $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->setType('id', PARAM_INT);
 
-        $mform->addElement('hidden','contextid');
-        $mform->setType('contextid', PARAM_INT);
-
         $this->add_action_buttons();
         $this->add_action_buttons();
+
+        $this->set_data($cohort);
     }
 
     }
 
-    function validation($data, $files) {
+    public function validation($data, $files) {
         global $DB;
 
         $errors = parent::validation($data, $files);
         global $DB;
 
         $errors = parent::validation($data, $files);
-        $textlib = textlib_get_instance();
 
         $idnumber = trim($data['idnumber']);
         if ($data['id']) {
 
         $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;
     }
         }
 
         return $errors;
     }
+
+    protected function get_category_options($currentcontextid) {
+        $displaylist = array();
+        $parentlist = array();
+        make_categories_list($displaylist, $parentlist, 'moodle/cohort:manage');
+        $options = array();
+        $syscontext = get_context_instance(CONTEXT_SYSTEM);
+        if (has_capability('moodle/cohort:manage', $syscontext)) {
+            $options[$syscontext->id] = print_context_name($syscontext);
+        }
+        foreach ($displaylist as $cid=>$name) {
+            $context = get_context_instance(CONTEXT_COURSECAT, $cid, MUST_EXIST);
+            $options[$context->id] = $name;
+        }
+        // always add current - this is not likely, but if the logic get's changed it might be a problem
+        if (!isset($options[$currentcontextid])) {
+            $context = get_context_instance_by_id($currentcontextid, MUST_EXIST);
+            $options[$context->id] = print_context_name($syscontext);
+        }
+        return $options;
+    }
 }
     
\ No newline at end of file
 }
     
\ No newline at end of file