|
Moodle
2.2.1
http://www.collinsharper.com
|
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 00029 defined('MOODLE_INTERNAL') || die(); 00030 00031 class enrol_guest_plugin extends enrol_plugin { 00032 00045 public function get_info_icons(array $instances) { 00046 foreach ($instances as $instance) { 00047 if ($instance->password !== '') { 00048 return array(new pix_icon('withpassword', get_string('pluginname', 'enrol_guest'), 'enrol_guest')); 00049 } else { 00050 return array(new pix_icon('withoutpassword', get_string('pluginname', 'enrol_guest'), 'enrol_guest')); 00051 } 00052 } 00053 } 00054 00055 public function enrol_user(stdClass $instance, $userid, $roleid = NULL, $timestart = 0, $timeend = 0, $status = NULL) { 00056 // no real enrolments here! 00057 return; 00058 } 00059 00060 public function unenrol_user(stdClass $instance, $userid) { 00061 // nothing to do, we never enrol here! 00062 return; 00063 } 00064 00072 public function try_guestaccess(stdClass $instance) { 00073 global $USER, $CFG; 00074 00075 $allow = false; 00076 00077 if ($instance->password === '') { 00078 $allow = true; 00079 00080 } else if (isset($USER->enrol_guest_passwords[$instance->id])) { // this is a hack, ideally we should not add stuff to $USER... 00081 if ($USER->enrol_guest_passwords[$instance->id] === $instance->password) { 00082 $allow = true; 00083 } 00084 } 00085 00086 if ($allow) { 00087 // Temporarily assign them some guest role for this context 00088 $context = get_context_instance(CONTEXT_COURSE, $instance->courseid); 00089 load_temp_course_role($context, $CFG->guestroleid); 00090 return ENROL_MAX_TIMESTAMP; 00091 } 00092 00093 return false; 00094 } 00095 00101 public function get_newinstance_link($courseid) { 00102 global $DB; 00103 00104 $context = get_context_instance(CONTEXT_COURSE, $courseid, MUST_EXIST); 00105 00106 if (!has_capability('moodle/course:enrolconfig', $context) or !has_capability('enrol/guest:config', $context)) { 00107 return NULL; 00108 } 00109 00110 if ($DB->record_exists('enrol', array('courseid'=>$courseid, 'enrol'=>'guest'))) { 00111 return NULL; 00112 } 00113 00114 return new moodle_url('/enrol/guest/addinstance.php', array('sesskey'=>sesskey(), 'id'=>$courseid)); 00115 } 00116 00124 public function enrol_page_hook(stdClass $instance) { 00125 global $CFG, $OUTPUT, $SESSION, $USER; 00126 00127 if ($instance->password === '') { 00128 return null; 00129 } 00130 00131 if (isset($USER->enrol['tempguest'][$instance->courseid]) and $USER->enrol['tempguest'][$instance->courseid] > time()) { 00132 // no need to show the guest access when user can already enter course as guest 00133 return null; 00134 } 00135 00136 require_once("$CFG->dirroot/enrol/guest/locallib.php"); 00137 $form = new enrol_guest_enrol_form(NULL, $instance); 00138 $instanceid = optional_param('instance', 0, PARAM_INT); 00139 00140 if ($instance->id == $instanceid) { 00141 if ($data = $form->get_data()) { 00142 // add guest role 00143 $context = get_context_instance(CONTEXT_COURSE, $instance->courseid); 00144 $USER->enrol_guest_passwords[$instance->id] = $data->guestpassword; // this is a hack, ideally we should not add stuff to $USER... 00145 if (isset($USER->enrol['tempguest'][$instance->courseid])) { 00146 remove_temp_course_roles($context); 00147 } 00148 load_temp_course_role($context, $CFG->guestroleid); 00149 $USER->enrol['tempguest'][$instance->courseid] = ENROL_MAX_TIMESTAMP; 00150 00151 // go to the originally requested page 00152 if (!empty($SESSION->wantsurl)) { 00153 $destination = $SESSION->wantsurl; 00154 unset($SESSION->wantsurl); 00155 } else { 00156 $destination = "$CFG->wwwroot/course/view.php?id=$instance->courseid"; 00157 } 00158 redirect($destination); 00159 } 00160 } 00161 00162 ob_start(); 00163 $form->display(); 00164 $output = ob_get_clean(); 00165 00166 return $OUTPUT->box($output, 'generalbox'); 00167 } 00168 00178 public function course_edit_form($instance, MoodleQuickForm $mform, $data, $context) { 00179 00180 $i = isset($instance->id) ? $instance->id : 0; 00181 00182 if (!$i and !$this->get_config('defaultenrol')) { 00183 return; 00184 } 00185 00186 $header = $this->get_instance_name($instance); 00187 $config = has_capability('enrol/guest:config', $context); 00188 00189 $mform->addElement('header', 'enrol_guest_header_'.$i, $header); 00190 00191 00192 $options = array(ENROL_INSTANCE_ENABLED => get_string('yes'), 00193 ENROL_INSTANCE_DISABLED => get_string('no')); 00194 $mform->addElement('select', 'enrol_guest_status_'.$i, get_string('status', 'enrol_guest'), $options); 00195 $mform->addHelpButton('enrol_guest_status_'.$i, 'status', 'enrol_guest'); 00196 $mform->setDefault('enrol_guest_status_'.$i, $this->get_config('status')); 00197 $mform->setAdvanced('enrol_guest_status_'.$i, $this->get_config('status_adv')); 00198 if (!$config) { 00199 $mform->hardFreeze('enrol_guest_status_'.$i); 00200 } 00201 00202 $mform->addElement('passwordunmask', 'enrol_guest_password_'.$i, get_string('password', 'enrol_guest')); 00203 $mform->addHelpButton('enrol_guest_password_'.$i, 'password', 'enrol_guest'); 00204 if (!$config) { 00205 $mform->hardFreeze('enrol_guest_password_'.$i); 00206 } else { 00207 $mform->disabledIf('enrol_guest_password_'.$i, 'enrol_guest_status_'.$i, 'noteq', ENROL_INSTANCE_ENABLED); 00208 } 00209 00210 00211 // now add all values from enrol table 00212 if ($instance) { 00213 foreach($instance as $key=>$val) { 00214 $data->{'enrol_guest_'.$key.'_'.$i} = $val; 00215 } 00216 } 00217 } 00218 00227 public function course_edit_validation($instance, array $data, $context) { 00228 $errors = array(); 00229 00230 if (!has_capability('enrol/guest:config', $context)) { 00231 // we are going to ignore the data later anyway, they would nto be able to fix the form anyway 00232 return $errors; 00233 } 00234 00235 $i = isset($instance->id) ? $instance->id : 0; 00236 00237 if (!isset($data['enrol_guest_status_'.$i])) { 00238 return $errors; 00239 } 00240 00241 $password = empty($data['enrol_guest_password_'.$i]) ? '' : $data['enrol_guest_password_'.$i]; 00242 $checkpassword = false; 00243 00244 if ($instance) { 00245 if ($data['enrol_guest_status_'.$i] == ENROL_INSTANCE_ENABLED) { 00246 if ($instance->password !== $password) { 00247 $checkpassword = true; 00248 } 00249 } 00250 } else { 00251 if ($data['enrol_guest_status_'.$i] == ENROL_INSTANCE_ENABLED) { 00252 $checkpassword = true; 00253 } 00254 } 00255 00256 if ($checkpassword) { 00257 $require = $this->get_config('requirepassword'); 00258 $policy = $this->get_config('usepasswordpolicy'); 00259 if ($require and empty($password)) { 00260 $errors['enrol_guest_password_'.$i] = get_string('required'); 00261 } else if ($policy) { 00262 $errmsg = '';//prevent eclipse warning 00263 if (!check_password_policy($password, $errmsg)) { 00264 $errors['enrol_guest_password_'.$i] = $errmsg; 00265 } 00266 } 00267 } 00268 00269 return $errors; 00270 } 00271 00280 public function course_updated($inserted, $course, $data) { 00281 global $DB; 00282 00283 $context = get_context_instance(CONTEXT_COURSE, $course->id); 00284 00285 if (has_capability('enrol/guest:config', $context)) { 00286 if ($inserted) { 00287 if (isset($data->enrol_guest_status_0)) { 00288 $fields = array('status'=>$data->enrol_guest_status_0); 00289 if ($fields['status'] == ENROL_INSTANCE_ENABLED) { 00290 $fields['password'] = $data->enrol_guest_password_0; 00291 } else { 00292 if ($this->get_config('requirepassword')) { 00293 $fields['password'] = generate_password(20); 00294 } 00295 } 00296 $this->add_instance($course, $fields); 00297 } else { 00298 if ($this->get_config('defaultenrol')) { 00299 $this->add_default_instance($course); 00300 } 00301 } 00302 } else { 00303 $instances = $DB->get_records('enrol', array('courseid'=>$course->id, 'enrol'=>'guest')); 00304 foreach ($instances as $instance) { 00305 $i = $instance->id; 00306 00307 if (isset($data->{'enrol_guest_status_'.$i})) { 00308 $reset = ($instance->status != $data->{'enrol_guest_status_'.$i}); 00309 00310 $instance->status = $data->{'enrol_guest_status_'.$i}; 00311 $instance->timemodified = time(); 00312 if ($instance->status == ENROL_INSTANCE_ENABLED) { 00313 if ($instance->password !== $data->{'enrol_guest_password_'.$i}) { 00314 $reset = true; 00315 } 00316 $instance->password = $data->{'enrol_guest_password_'.$i}; 00317 } 00318 $DB->update_record('enrol', $instance); 00319 00320 if ($reset) { 00321 $context->mark_dirty(); 00322 } 00323 } 00324 } 00325 } 00326 00327 } else { 00328 if ($inserted) { 00329 if ($this->get_config('defaultenrol')) { 00330 $this->add_default_instance($course); 00331 } 00332 } else { 00333 // bad luck, user can not change anything 00334 } 00335 } 00336 } 00337 00344 public function add_instance($course, array $fields = NULL) { 00345 $fields = (array)$fields; 00346 00347 if (!isset($fields['password'])) { 00348 $fields['password'] = ''; 00349 } 00350 00351 return parent::add_instance($course, $fields); 00352 } 00353 00359 public function add_default_instance($course) { 00360 $fields = array('status'=>$this->get_config('status')); 00361 00362 if ($this->get_config('requirepassword')) { 00363 $fields['password'] = generate_password(20); 00364 } 00365 00366 return $this->add_instance($course, $fields); 00367 } 00368 00369 } 00370 00377 function enrol_guest_supports($feature) { 00378 switch($feature) { 00379 case ENROL_RESTORE_TYPE: return ENROL_RESTORE_NOUSERS; 00380 00381 default: return null; 00382 } 00383 }