|
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 00032 class enrol_self_plugin extends enrol_plugin { 00033 00046 public function get_info_icons(array $instances) { 00047 $key = false; 00048 $nokey = false; 00049 foreach ($instances as $instance) { 00050 if ($instance->password or $instance->customint1) { 00051 $key = true; 00052 } else { 00053 $nokey = true; 00054 } 00055 } 00056 $icons = array(); 00057 if ($nokey) { 00058 $icons[] = new pix_icon('withoutkey', get_string('pluginname', 'enrol_self'), 'enrol_self'); 00059 } 00060 if ($key) { 00061 $icons[] = new pix_icon('withkey', get_string('pluginname', 'enrol_self'), 'enrol_self'); 00062 } 00063 return $icons; 00064 } 00065 00072 public function get_instance_name($instance) { 00073 global $DB; 00074 00075 if (empty($instance->name)) { 00076 if (!empty($instance->roleid) and $role = $DB->get_record('role', array('id'=>$instance->roleid))) { 00077 $role = ' (' . role_get_name($role, get_context_instance(CONTEXT_COURSE, $instance->courseid)) . ')'; 00078 } else { 00079 $role = ''; 00080 } 00081 $enrol = $this->get_name(); 00082 return get_string('pluginname', 'enrol_'.$enrol) . $role; 00083 } else { 00084 return format_string($instance->name); 00085 } 00086 } 00087 00088 public function roles_protected() { 00089 // users may tweak the roles later 00090 return false; 00091 } 00092 00093 public function allow_unenrol(stdClass $instance) { 00094 // users with unenrol cap may unenrol other users manually manually 00095 return true; 00096 } 00097 00098 public function allow_manage(stdClass $instance) { 00099 // users with manage cap may tweak period and status 00100 return true; 00101 } 00102 00103 public function show_enrolme_link(stdClass $instance) { 00104 return ($instance->status == ENROL_INSTANCE_ENABLED); 00105 } 00106 00113 public function add_course_navigation($instancesnode, stdClass $instance) { 00114 if ($instance->enrol !== 'self') { 00115 throw new coding_exception('Invalid enrol instance type!'); 00116 } 00117 00118 $context = get_context_instance(CONTEXT_COURSE, $instance->courseid); 00119 if (has_capability('enrol/self:config', $context)) { 00120 $managelink = new moodle_url('/enrol/self/edit.php', array('courseid'=>$instance->courseid, 'id'=>$instance->id)); 00121 $instancesnode->add($this->get_instance_name($instance), $managelink, navigation_node::TYPE_SETTING); 00122 } 00123 } 00124 00130 public function get_action_icons(stdClass $instance) { 00131 global $OUTPUT; 00132 00133 if ($instance->enrol !== 'self') { 00134 throw new coding_exception('invalid enrol instance!'); 00135 } 00136 $context = get_context_instance(CONTEXT_COURSE, $instance->courseid); 00137 00138 $icons = array(); 00139 00140 if (has_capability('enrol/self:config', $context)) { 00141 $editlink = new moodle_url("/enrol/self/edit.php", array('courseid'=>$instance->courseid, 'id'=>$instance->id)); 00142 $icons[] = $OUTPUT->action_icon($editlink, new pix_icon('i/edit', get_string('edit'), 'core', array('class'=>'icon'))); 00143 } 00144 00145 return $icons; 00146 } 00147 00153 public function get_newinstance_link($courseid) { 00154 $context = get_context_instance(CONTEXT_COURSE, $courseid, MUST_EXIST); 00155 00156 if (!has_capability('moodle/course:enrolconfig', $context) or !has_capability('enrol/self:config', $context)) { 00157 return NULL; 00158 } 00159 // multiple instances supported - different roles with different password 00160 return new moodle_url('/enrol/self/edit.php', array('courseid'=>$courseid)); 00161 } 00162 00170 public function enrol_page_hook(stdClass $instance) { 00171 global $CFG, $OUTPUT, $SESSION, $USER, $DB; 00172 00173 if (isguestuser()) { 00174 // can not enrol guest!! 00175 return null; 00176 } 00177 if ($DB->record_exists('user_enrolments', array('userid'=>$USER->id, 'enrolid'=>$instance->id))) { 00178 //TODO: maybe we should tell them they are already enrolled, but can not access the course 00179 return null; 00180 } 00181 00182 if ($instance->enrolstartdate != 0 and $instance->enrolstartdate > time()) { 00183 //TODO: inform that we can not enrol yet 00184 return null; 00185 } 00186 00187 if ($instance->enrolenddate != 0 and $instance->enrolenddate < time()) { 00188 //TODO: inform that enrolment is not possible any more 00189 return null; 00190 } 00191 00192 require_once("$CFG->dirroot/enrol/self/locallib.php"); 00193 require_once("$CFG->dirroot/group/lib.php"); 00194 00195 $form = new enrol_self_enrol_form(NULL, $instance); 00196 $instanceid = optional_param('instance', 0, PARAM_INT); 00197 00198 if ($instance->id == $instanceid) { 00199 if ($data = $form->get_data()) { 00200 $enrol = enrol_get_plugin('self'); 00201 $timestart = time(); 00202 if ($instance->enrolperiod) { 00203 $timeend = $timestart + $instance->enrolperiod; 00204 } else { 00205 $timeend = 0; 00206 } 00207 00208 $this->enrol_user($instance, $USER->id, $instance->roleid, $timestart, $timeend); 00209 add_to_log($instance->courseid, 'course', 'enrol', '../enrol/users.php?id='.$instance->courseid, $instance->courseid); //there should be userid somewhere! 00210 00211 if ($instance->password and $instance->customint1 and $data->enrolpassword !== $instance->password) { 00212 // it must be a group enrolment, let's assign group too 00213 $groups = $DB->get_records('groups', array('courseid'=>$instance->courseid), 'id', 'id, enrolmentkey'); 00214 foreach ($groups as $group) { 00215 if (empty($group->enrolmentkey)) { 00216 continue; 00217 } 00218 if ($group->enrolmentkey === $data->enrolpassword) { 00219 groups_add_member($group->id, $USER->id); 00220 break; 00221 } 00222 } 00223 } 00224 // send welcome 00225 if ($instance->customint4) { 00226 $this->email_welcome_message($instance, $USER); 00227 } 00228 } 00229 } 00230 00231 ob_start(); 00232 $form->display(); 00233 $output = ob_get_clean(); 00234 00235 return $OUTPUT->box($output); 00236 } 00237 00243 public function add_default_instance($course) { 00244 $fields = array('customint1' => $this->get_config('groupkey'), 00245 'customint2' => $this->get_config('longtimenosee'), 00246 'customint3' => $this->get_config('maxenrolled'), 00247 'customint4' => $this->get_config('sendcoursewelcomemessage'), 00248 'enrolperiod' => $this->get_config('enrolperiod', 0), 00249 'status' => $this->get_config('status'), 00250 'roleid' => $this->get_config('roleid', 0)); 00251 00252 if ($this->get_config('requirepassword')) { 00253 $fields['password'] = generate_password(20); 00254 } 00255 00256 return $this->add_instance($course, $fields); 00257 } 00258 00266 protected function email_welcome_message($instance, $user) { 00267 global $CFG, $DB; 00268 00269 $course = $DB->get_record('course', array('id'=>$instance->courseid), '*', MUST_EXIST); 00270 00271 $a = new stdClass(); 00272 $a->coursename = format_string($course->fullname); 00273 $a->profileurl = "$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id"; 00274 00275 if (trim($instance->customtext1) !== '') { 00276 $message = $instance->customtext1; 00277 $message = str_replace('{$a->coursename}', $a->coursename, $message); 00278 $message = str_replace('{$a->profileurl}', $a->profileurl, $message); 00279 } else { 00280 $message = get_string('welcometocoursetext', 'enrol_self', $a); 00281 } 00282 00283 $subject = get_string('welcometocourse', 'enrol_self', format_string($course->fullname)); 00284 00285 $context = get_context_instance(CONTEXT_COURSE, $course->id); 00286 $rusers = array(); 00287 if (!empty($CFG->coursecontact)) { 00288 $croles = explode(',', $CFG->coursecontact); 00289 $rusers = get_role_users($croles, $context, true, '', 'r.sortorder ASC, u.lastname ASC'); 00290 } 00291 if ($rusers) { 00292 $contact = reset($rusers); 00293 } else { 00294 $contact = get_admin(); 00295 } 00296 00297 //directly emailing welcome message rather than using messaging 00298 email_to_user($user, $contact, $subject, $message); 00299 } 00300 00305 public function cron() { 00306 global $DB; 00307 00308 if (!enrol_is_enabled('self')) { 00309 return; 00310 } 00311 00312 $plugin = enrol_get_plugin('self'); 00313 00314 $now = time(); 00315 00316 //note: the logic of self enrolment guarantees that user logged in at least once (=== u.lastaccess set) 00317 // and that user accessed course at least once too (=== user_lastaccess record exists) 00318 00319 // first deal with users that did not log in for a really long time 00320 $sql = "SELECT e.*, ue.userid 00321 FROM {user_enrolments} ue 00322 JOIN {enrol} e ON (e.id = ue.enrolid AND e.enrol = 'self' AND e.customint2 > 0) 00323 JOIN {user} u ON u.id = ue.userid 00324 WHERE :now - u.lastaccess > e.customint2"; 00325 $rs = $DB->get_recordset_sql($sql, array('now'=>$now)); 00326 foreach ($rs as $instance) { 00327 $userid = $instance->userid; 00328 unset($instance->userid); 00329 $plugin->unenrol_user($instance, $userid); 00330 mtrace("unenrolling user $userid from course $instance->courseid as they have did not log in for $instance->customint2 days"); 00331 } 00332 $rs->close(); 00333 00334 // now unenrol from course user did not visit for a long time 00335 $sql = "SELECT e.*, ue.userid 00336 FROM {user_enrolments} ue 00337 JOIN {enrol} e ON (e.id = ue.enrolid AND e.enrol = 'self' AND e.customint2 > 0) 00338 JOIN {user_lastaccess} ul ON (ul.userid = ue.userid AND ul.courseid = e.courseid) 00339 WHERE :now - ul.timeaccess > e.customint2"; 00340 $rs = $DB->get_recordset_sql($sql, array('now'=>$now)); 00341 foreach ($rs as $instance) { 00342 $userid = $instance->userid; 00343 unset($instance->userid); 00344 $plugin->unenrol_user($instance, $userid); 00345 mtrace("unenrolling user $userid from course $instance->courseid as they have did not access course for $instance->customint2 days"); 00346 } 00347 $rs->close(); 00348 00349 flush(); 00350 } 00351 00359 public function get_user_enrolment_actions(course_enrolment_manager $manager, $ue) { 00360 $actions = array(); 00361 $context = $manager->get_context(); 00362 $instance = $ue->enrolmentinstance; 00363 $params = $manager->get_moodlepage()->url->params(); 00364 $params['ue'] = $ue->id; 00365 if ($this->allow_unenrol($instance) && has_capability("enrol/self:unenrol", $context)) { 00366 $url = new moodle_url('/enrol/unenroluser.php', $params); 00367 $actions[] = new user_enrolment_action(new pix_icon('t/delete', ''), get_string('unenrol', 'enrol'), $url, array('class'=>'unenrollink', 'rel'=>$ue->id)); 00368 } 00369 if ($this->allow_manage($instance) && has_capability("enrol/self:manage", $context)) { 00370 $url = new moodle_url('/enrol/self/editenrolment.php', $params); 00371 $actions[] = new user_enrolment_action(new pix_icon('t/edit', ''), get_string('edit'), $url, array('class'=>'editenrollink', 'rel'=>$ue->id)); 00372 } 00373 return $actions; 00374 } 00375 } 00376 00383 function enrol_self_supports($feature) { 00384 switch($feature) { 00385 case ENROL_RESTORE_TYPE: return ENROL_RESTORE_EXACT; 00386 00387 default: return null; 00388 } 00389 }