Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/cohort/edit_form.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00027 if (!defined('MOODLE_INTERNAL')) {
00028     die('Direct access to this script is forbidden.');    
00029 }
00030 
00031 require_once($CFG->dirroot . '/lib/formslib.php');
00032 
00033 class cohort_edit_form extends moodleform {
00034 
00038     public function definition() {
00039 
00040         $mform = $this->_form;
00041         $editoroptions = $this->_customdata['editoroptions'];
00042         $cohort = $this->_customdata['data'];
00043 
00044         $mform->addElement('text', 'name', get_string('name', 'cohort'), 'maxlength="254" size="50"');
00045         $mform->addRule('name', get_string('required'), 'required', null, 'client');
00046         $mform->setType('name', PARAM_MULTILANG);
00047 
00048         $options = $this->get_category_options($cohort->contextid);
00049         $mform->addElement('select', 'contextid', get_string('context', 'role'), $options);
00050 
00051         $mform->addElement('text', 'idnumber', get_string('idnumber', 'cohort'), 'maxlength="254" size="50"');
00052         $mform->setType('name', PARAM_RAW);
00053 
00054         $mform->addElement('editor', 'description_editor', get_string('description', 'cohort'), null, $editoroptions);
00055         $mform->setType('description_editor', PARAM_RAW);
00056 
00057         $mform->addElement('hidden', 'id');
00058         $mform->setType('id', PARAM_INT);
00059 
00060         $this->add_action_buttons();
00061 
00062         $this->set_data($cohort);
00063     }
00064 
00065     public function validation($data, $files) {
00066         global $DB;
00067 
00068         $errors = parent::validation($data, $files);
00069 
00070         $idnumber = trim($data['idnumber']);
00071         if ($idnumber === '') {
00072             // fine, empty is ok
00073 
00074         } else if ($data['id']) {
00075             $current = $DB->get_record('cohort', array('id'=>$data['id']), '*', MUST_EXIST);
00076             if ($current->idnumber !== $idnumber) {
00077                 if ($DB->record_exists('cohort', array('idnumber'=>$idnumber))) {
00078                     $errors['idnumber'] = get_string('duplicateidnumber', 'cohort');
00079                 }
00080             }
00081 
00082         } else {
00083             if ($DB->record_exists('cohort', array('idnumber'=>$idnumber))) {
00084                 $errors['idnumber'] = get_string('duplicateidnumber', 'cohort');
00085             }
00086         }
00087 
00088         return $errors;
00089     }
00090 
00091     protected function get_category_options($currentcontextid) {
00092         $displaylist = array();
00093         $parentlist = array();
00094         make_categories_list($displaylist, $parentlist, 'moodle/cohort:manage');
00095         $options = array();
00096         $syscontext = get_context_instance(CONTEXT_SYSTEM);
00097         if (has_capability('moodle/cohort:manage', $syscontext)) {
00098             $options[$syscontext->id] = print_context_name($syscontext);
00099         }
00100         foreach ($displaylist as $cid=>$name) {
00101             $context = get_context_instance(CONTEXT_COURSECAT, $cid, MUST_EXIST);
00102             $options[$context->id] = $name;
00103         }
00104         // always add current - this is not likely, but if the logic gets changed it might be a problem
00105         if (!isset($options[$currentcontextid])) {
00106             $context = get_context_instance_by_id($currentcontextid, MUST_EXIST);
00107             $options[$context->id] = print_context_name($syscontext);
00108         }
00109         return $options;
00110     }
00111 }
00112 
 All Data Structures Namespaces Files Functions Variables Enumerations