Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/enrol/meta/addinstance_form.php
Go to the documentation of this file.
00001 <?php
00002 // This file is part of Moodle - http://moodle.org/
00003 //
00004 // Moodle is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // Moodle is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00016 
00026 defined('MOODLE_INTERNAL') || die();
00027 
00028 require_once("$CFG->libdir/formslib.php");
00029 
00030 class enrol_meta_addinstance_form extends moodleform {
00031     protected $course;
00032 
00033     function definition() {
00034         global $CFG, $DB;
00035 
00036         $mform  = $this->_form;
00037         $course = $this->_customdata;
00038         $this->course = $course;
00039 
00040         $existing = $DB->get_records('enrol', array('enrol'=>'meta', 'courseid'=>$course->id), '', 'customint1, id');
00041 
00042         // TODO: this has to be done via ajax or else it will fail very badly on large sites!
00043         $courses = array('' => get_string('choosedots'));
00044         $rs = $DB->get_recordset('course', array(), 'sortorder ASC', 'id, fullname, shortname, visible');
00045         foreach ($rs as $c) {
00046             if ($c->id == SITEID or $c->id == $course->id or isset($existing[$c->id])) {
00047                 continue;
00048             }
00049             $coursecontext = get_context_instance(CONTEXT_COURSE, $c->id);
00050             if (!$c->visible and !has_capability('moodle/course:viewhiddencourses', $coursecontext)) {
00051                 continue;
00052             }
00053             if (!has_capability('enrol/meta:selectaslinked', $coursecontext)) {
00054                 continue;
00055             }
00056             $courses[$c->id] = format_string($c->fullname). ' ['.format_string($c->shortname, true, array('context' => $coursecontext)).']';
00057         }
00058         $rs->close();
00059 
00060         $mform->addElement('header','general', get_string('pluginname', 'enrol_meta'));
00061 
00062         $mform->addElement('select', 'link', get_string('linkedcourse', 'enrol_meta'), $courses);
00063         $mform->addRule('link', get_string('required'), 'required', null, 'client');
00064 
00065         $mform->addElement('hidden', 'id', null);
00066         $mform->setType('id', PARAM_INT);
00067 
00068         $this->add_action_buttons(true, get_string('addinstance', 'enrol'));
00069 
00070         $this->set_data(array('id'=>$course->id));
00071     }
00072 
00073     function validation($data, $files) {
00074         global $DB, $CFG;
00075 
00076         // TODO: this is duplicated here because it may be necessary once we implement ajax course selection element
00077 
00078         $errors = parent::validation($data, $files);
00079         if (!$c = $DB->get_record('course', array('id'=>$data['link']))) {
00080             $errors['link'] = get_string('required');
00081         } else {
00082             $coursecontext = get_context_instance(CONTEXT_COURSE, $c->id);
00083             $existing = $DB->get_records('enrol', array('enrol'=>'meta', 'courseid'=>$this->course->id), '', 'customint1, id');
00084             if (!$c->visible and !has_capability('moodle/course:viewhiddencourses', $coursecontext)) {
00085                 $errors['link'] = get_string('error');
00086             } else if (!has_capability('enrol/meta:selectaslinked', $coursecontext)) {
00087                 $errors['link'] = get_string('error');
00088             } else if ($c->id == SITEID or $c->id == $this->course->id or isset($existing[$c->id])) {
00089                 $errors['link'] = get_string('error');
00090             }
00091         }
00092 
00093         return $errors;
00094     }
00095 }
00096 
 All Data Structures Namespaces Files Functions Variables Enumerations