|
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 00028 defined('MOODLE_INTERNAL') || die(); 00029 00030 require_once($CFG->libdir.'/formslib.php'); 00031 00032 class enrol_self_edit_form extends moodleform { 00033 00034 function definition() { 00035 $mform = $this->_form; 00036 00037 list($instance, $plugin, $context) = $this->_customdata; 00038 00039 $mform->addElement('header', 'header', get_string('pluginname', 'enrol_self')); 00040 00041 $mform->addElement('text', 'name', get_string('custominstancename', 'enrol')); 00042 00043 $options = array(ENROL_INSTANCE_ENABLED => get_string('yes'), 00044 ENROL_INSTANCE_DISABLED => get_string('no')); 00045 $mform->addElement('select', 'status', get_string('status', 'enrol_self'), $options); 00046 $mform->addHelpButton('status', 'status', 'enrol_self'); 00047 $mform->setDefault('status', $plugin->get_config('status')); 00048 00049 $mform->addElement('passwordunmask', 'password', get_string('password', 'enrol_self')); 00050 $mform->addHelpButton('password', 'password', 'enrol_self'); 00051 if (empty($instance->id) and $plugin->get_config('requirepassword')) { 00052 $mform->addRule('password', get_string('required'), 'required', null, 'client'); 00053 } 00054 00055 $options = array(1 => get_string('yes'), 00056 0 => get_string('no')); 00057 $mform->addElement('select', 'customint1', get_string('groupkey', 'enrol_self'), $options); 00058 $mform->addHelpButton('customint1', 'groupkey', 'enrol_self'); 00059 $mform->setDefault('customint1', $plugin->get_config('groupkey')); 00060 00061 if ($instance->id) { 00062 $roles = $this->extend_assignable_roles($context, $instance->roleid); 00063 } else { 00064 $roles = $this->extend_assignable_roles($context, $plugin->get_config('roleid')); 00065 } 00066 $mform->addElement('select', 'roleid', get_string('role', 'enrol_self'), $roles); 00067 $mform->setDefault('roleid', $plugin->get_config('roleid')); 00068 00069 $mform->addElement('duration', 'enrolperiod', get_string('enrolperiod', 'enrol_self'), array('optional' => true, 'defaultunit' => 86400)); 00070 $mform->setDefault('enrolperiod', $plugin->get_config('enrolperiod')); 00071 $mform->addHelpButton('enrolperiod', 'enrolperiod', 'enrol_self'); 00072 00073 $mform->addElement('date_selector', 'enrolstartdate', get_string('enrolstartdate', 'enrol_self'), array('optional' => true)); 00074 $mform->setDefault('enrolstartdate', 0); 00075 $mform->addHelpButton('enrolstartdate', 'enrolstartdate', 'enrol_self'); 00076 00077 $mform->addElement('date_selector', 'enrolenddate', get_string('enrolenddate', 'enrol_self'), array('optional' => true)); 00078 $mform->setDefault('enrolenddate', 0); 00079 $mform->addHelpButton('enrolenddate', 'enrolenddate', 'enrol_self'); 00080 00081 $options = array(0 => get_string('never'), 00082 1800 * 3600 * 24 => get_string('numdays', '', 1800), 00083 1000 * 3600 * 24 => get_string('numdays', '', 1000), 00084 365 * 3600 * 24 => get_string('numdays', '', 365), 00085 180 * 3600 * 24 => get_string('numdays', '', 180), 00086 150 * 3600 * 24 => get_string('numdays', '', 150), 00087 120 * 3600 * 24 => get_string('numdays', '', 120), 00088 90 * 3600 * 24 => get_string('numdays', '', 90), 00089 60 * 3600 * 24 => get_string('numdays', '', 60), 00090 30 * 3600 * 24 => get_string('numdays', '', 30), 00091 21 * 3600 * 24 => get_string('numdays', '', 21), 00092 14 * 3600 * 24 => get_string('numdays', '', 14), 00093 7 * 3600 * 24 => get_string('numdays', '', 7)); 00094 $mform->addElement('select', 'customint2', get_string('longtimenosee', 'enrol_self'), $options); 00095 $mform->setDefault('customint2', $plugin->get_config('longtimenosee')); 00096 $mform->addHelpButton('customint2', 'longtimenosee', 'enrol_self'); 00097 00098 $mform->addElement('text', 'customint3', get_string('maxenrolled', 'enrol_self')); 00099 $mform->setDefault('customint3', $plugin->get_config('maxenrolled')); 00100 $mform->addHelpButton('customint3', 'maxenrolled', 'enrol_self'); 00101 $mform->setType('customint3', PARAM_INT); 00102 00103 $mform->addElement('advcheckbox', 'customint4', get_string('sendcoursewelcomemessage', 'enrol_self')); 00104 $mform->setDefault('customint4', $plugin->get_config('sendcoursewelcomemessage')); 00105 $mform->addHelpButton('customint4', 'sendcoursewelcomemessage', 'enrol_self'); 00106 00107 $mform->addElement('textarea', 'customtext1', get_string('customwelcomemessage', 'enrol_self'), array('cols'=>'60', 'rows'=>'8')); 00108 00109 $mform->addElement('hidden', 'id'); 00110 $mform->setType('id', PARAM_INT); 00111 $mform->addElement('hidden', 'courseid'); 00112 $mform->setType('courseid', PARAM_INT); 00113 00114 $this->add_action_buttons(true, ($instance->id ? null : get_string('addinstance', 'enrol'))); 00115 00116 $this->set_data($instance); 00117 } 00118 00119 function validation($data, $files) { 00120 global $DB, $CFG; 00121 $errors = parent::validation($data, $files); 00122 00123 list($instance, $plugin, $context) = $this->_customdata; 00124 $checkpassword = false; 00125 00126 if ($instance->id) { 00127 if ($data['status'] == ENROL_INSTANCE_ENABLED) { 00128 if ($instance->password !== $data['password']) { 00129 $checkpassword = true; 00130 } 00131 } 00132 } else { 00133 if ($data['status'] == ENROL_INSTANCE_ENABLED) { 00134 $checkpassword = true; 00135 } 00136 } 00137 00138 if ($checkpassword) { 00139 $require = $plugin->get_config('requirepassword'); 00140 $policy = $plugin->get_config('usepasswordpolicy'); 00141 if ($require and trim($data['password']) === '') { 00142 $errors['password'] = get_string('required'); 00143 } else if ($policy) { 00144 $errmsg = '';//prevent eclipse warning 00145 if (!check_password_policy($data['password'], $errmsg)) { 00146 $errors['password'] = $errmsg; 00147 } 00148 } 00149 } 00150 00151 if ($data['status'] == ENROL_INSTANCE_ENABLED) { 00152 if (!empty($data['enrolenddate']) and $data['enrolenddate'] < $data['enrolstartdate']) { 00153 $errors['enrolenddate'] = get_string('enrolenddaterror', 'enrol_self'); 00154 } 00155 } 00156 00157 return $errors; 00158 } 00159 00167 function extend_assignable_roles($context, $defaultrole) { 00168 global $DB; 00169 $roles = get_assignable_roles($context); 00170 $sql = "SELECT r.id, r.name 00171 FROM {role} r 00172 WHERE r.id = $defaultrole"; 00173 $results = $DB->get_record_sql($sql); 00174 if (isset($results->name)) { 00175 $roles[$results->id] = $results->name; 00176 } 00177 return $roles; 00178 } 00179 }