|
Moodle
2.2.1
http://www.collinsharper.com
|
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 defined('MOODLE_INTERNAL') || die(); 00028 00029 require_once("$CFG->libdir/formslib.php"); 00030 00031 class enrol_self_enrol_form extends moodleform { 00032 protected $instance; 00033 protected $toomany = false; 00034 00040 protected function get_form_identifier() { 00041 $formid = $this->_customdata->id.'_'.get_class($this); 00042 return $formid; 00043 } 00044 00045 public function definition() { 00046 global $DB; 00047 00048 $mform = $this->_form; 00049 $instance = $this->_customdata; 00050 $this->instance = $instance; 00051 $plugin = enrol_get_plugin('self'); 00052 00053 $heading = $plugin->get_instance_name($instance); 00054 $mform->addElement('header', 'selfheader', $heading); 00055 00056 if ($instance->customint3 > 0) { 00057 // max enrol limit specified 00058 $count = $DB->count_records('user_enrolments', array('enrolid'=>$instance->id)); 00059 if ($count >= $instance->customint3) { 00060 // bad luck, no more self enrolments here 00061 $this->toomany = true; 00062 $mform->addElement('static', 'notice', '', get_string('maxenrolledreached', 'enrol_self')); 00063 return; 00064 } 00065 } 00066 00067 if ($instance->password) { 00068 //change the id of self enrolment key input as there can be multiple self enrolment methods 00069 $mform->addElement('passwordunmask', 'enrolpassword', get_string('password', 'enrol_self'), 00070 array('id' => $instance->id."_enrolpassword")); 00071 } else { 00072 $mform->addElement('static', 'nokey', '', get_string('nopassword', 'enrol_self')); 00073 } 00074 00075 $this->add_action_buttons(false, get_string('enrolme', 'enrol_self')); 00076 00077 $mform->addElement('hidden', 'id'); 00078 $mform->setType('id', PARAM_INT); 00079 $mform->setDefault('id', $instance->courseid); 00080 00081 $mform->addElement('hidden', 'instance'); 00082 $mform->setType('instance', PARAM_INT); 00083 $mform->setDefault('instance', $instance->id); 00084 } 00085 00086 public function validation($data, $files) { 00087 global $DB, $CFG; 00088 00089 $errors = parent::validation($data, $files); 00090 $instance = $this->instance; 00091 00092 if ($this->toomany) { 00093 $errors['notice'] = get_string('error'); 00094 return $errors; 00095 } 00096 00097 if ($instance->password) { 00098 if ($data['enrolpassword'] !== $instance->password) { 00099 if ($instance->customint1) { 00100 $groups = $DB->get_records('groups', array('courseid'=>$instance->courseid), 'id ASC', 'id, enrolmentkey'); 00101 $found = false; 00102 foreach ($groups as $group) { 00103 if (empty($group->enrolmentkey)) { 00104 continue; 00105 } 00106 if ($group->enrolmentkey === $data['enrolpassword']) { 00107 $found = true; 00108 break; 00109 } 00110 } 00111 if (!$found) { 00112 // we can not hint because there are probably multiple passwords 00113 $errors['enrolpassword'] = get_string('passwordinvalid', 'enrol_self'); 00114 } 00115 00116 } else { 00117 $plugin = enrol_get_plugin('self'); 00118 if ($plugin->get_config('showhint')) { 00119 $textlib = textlib_get_instance(); 00120 $hint = $textlib->substr($instance->password, 0, 1); 00121 $errors['enrolpassword'] = get_string('passwordinvalidhint', 'enrol_self', $hint); 00122 } else { 00123 $errors['enrolpassword'] = get_string('passwordinvalid', 'enrol_self'); 00124 } 00125 } 00126 } 00127 } 00128 00129 return $errors; 00130 } 00131 }