|
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 class enrol_manual_plugin extends enrol_plugin { 00030 00031 public function roles_protected() { 00032 // users may tweak the roles later 00033 return false; 00034 } 00035 00036 public function allow_enrol(stdClass $instance) { 00037 // users with enrol cap may unenrol other users manually manually 00038 return true; 00039 } 00040 00041 public function allow_unenrol(stdClass $instance) { 00042 // users with unenrol cap may unenrol other users manually manually 00043 return true; 00044 } 00045 00046 public function allow_manage(stdClass $instance) { 00047 // users with manage cap may tweak period and status 00048 return true; 00049 } 00050 00058 public function get_manual_enrol_link($instance) { 00059 $name = $this->get_name(); 00060 if ($instance->enrol !== $name) { 00061 throw new coding_exception('invalid enrol instance!'); 00062 } 00063 00064 if (!enrol_is_enabled($name)) { 00065 return NULL; 00066 } 00067 00068 $context = get_context_instance(CONTEXT_COURSE, $instance->courseid, MUST_EXIST); 00069 00070 if (!has_capability('enrol/manual:manage', $context) or !has_capability('enrol/manual:enrol', $context) or !has_capability('enrol/manual:unenrol', $context)) { 00071 return NULL; 00072 } 00073 00074 return new moodle_url('/enrol/manual/manage.php', array('enrolid'=>$instance->id, 'id'=>$instance->courseid)); 00075 } 00076 00086 public function add_course_navigation($instancesnode, stdClass $instance) { 00087 if ($instance->enrol !== 'manual') { 00088 throw new coding_exception('Invalid enrol instance type!'); 00089 } 00090 00091 $context = get_context_instance(CONTEXT_COURSE, $instance->courseid); 00092 if (has_capability('enrol/manual:config', $context)) { 00093 $managelink = new moodle_url('/enrol/manual/edit.php', array('courseid'=>$instance->courseid)); 00094 $instancesnode->add($this->get_instance_name($instance), $managelink, navigation_node::TYPE_SETTING); 00095 } 00096 } 00097 00103 public function get_action_icons(stdClass $instance) { 00104 global $OUTPUT; 00105 00106 if ($instance->enrol !== 'manual') { 00107 throw new coding_exception('invalid enrol instance!'); 00108 } 00109 $context = get_context_instance(CONTEXT_COURSE, $instance->courseid); 00110 00111 $icons = array(); 00112 00113 if (has_capability('enrol/manual:manage', $context)) { 00114 $managelink = new moodle_url("/enrol/manual/manage.php", array('enrolid'=>$instance->id)); 00115 $icons[] = $OUTPUT->action_icon($managelink, new pix_icon('i/users', get_string('enrolusers', 'enrol_manual'), 'core', array('class'=>'iconsmall'))); 00116 } 00117 if (has_capability('enrol/manual:config', $context)) { 00118 $editlink = new moodle_url("/enrol/manual/edit.php", array('courseid'=>$instance->courseid)); 00119 $icons[] = $OUTPUT->action_icon($editlink, new pix_icon('i/edit', get_string('edit'), 'core', array('class'=>'icon'))); 00120 } 00121 00122 return $icons; 00123 } 00124 00130 public function get_newinstance_link($courseid) { 00131 global $DB; 00132 00133 $context = get_context_instance(CONTEXT_COURSE, $courseid, MUST_EXIST); 00134 00135 if (!has_capability('moodle/course:enrolconfig', $context) or !has_capability('enrol/manual:config', $context)) { 00136 return NULL; 00137 } 00138 00139 if ($DB->record_exists('enrol', array('courseid'=>$courseid, 'enrol'=>'manual'))) { 00140 return NULL; 00141 } 00142 00143 return new moodle_url('/enrol/manual/edit.php', array('courseid'=>$courseid)); 00144 } 00145 00151 public function add_default_instance($course) { 00152 $fields = array('status'=>$this->get_config('status'), 'enrolperiod'=>$this->get_config('enrolperiod', 0), 'roleid'=>$this->get_config('roleid', 0)); 00153 return $this->add_instance($course, $fields); 00154 } 00155 00162 public function add_instance($course, array $fields = NULL) { 00163 global $DB; 00164 00165 if ($DB->record_exists('enrol', array('courseid'=>$course->id, 'enrol'=>'manual'))) { 00166 // only one instance allowed, sorry 00167 return NULL; 00168 } 00169 00170 return parent::add_instance($course, $fields); 00171 } 00172 00185 public function get_manual_enrol_button(course_enrolment_manager $manager) { 00186 global $CFG; 00187 00188 $instance = null; 00189 $instances = array(); 00190 foreach ($manager->get_enrolment_instances() as $tempinstance) { 00191 if ($tempinstance->enrol == 'manual') { 00192 if ($instance === null) { 00193 $instance = $tempinstance; 00194 } 00195 $instances[] = array('id' => $tempinstance->id, 'name' => $this->get_instance_name($tempinstance)); 00196 } 00197 } 00198 if (empty($instance)) { 00199 return false; 00200 } 00201 00202 if (!$manuallink = $this->get_manual_enrol_link($instance)) { 00203 return false; 00204 } 00205 00206 $button = new enrol_user_button($manuallink, get_string('enrolusers', 'enrol_manual'), 'get'); 00207 $button->class .= ' enrol_manual_plugin'; 00208 00209 $startdate = $manager->get_course()->startdate; 00210 $startdateoptions = array(); 00211 $timeformat = get_string('strftimedatefullshort'); 00212 if ($startdate > 0) { 00213 $startdateoptions[2] = get_string('coursestart') . ' (' . userdate($startdate, $timeformat) . ')'; 00214 } 00215 $today = time(); 00216 $today = make_timestamp(date('Y', $today), date('m', $today), date('d', $today), 0, 0, 0); 00217 $startdateoptions[3] = get_string('today') . ' (' . userdate($today, $timeformat) . ')' ; 00218 00219 $modules = array('moodle-enrol_manual-quickenrolment', 'moodle-enrol_manual-quickenrolment-skin'); 00220 $arguments = array( 00221 'instances' => $instances, 00222 'courseid' => $instance->courseid, 00223 'ajaxurl' => '/enrol/manual/ajax.php', 00224 'url' => $manager->get_moodlepage()->url->out(false), 00225 'optionsStartDate' => $startdateoptions, 00226 'defaultRole' => $instance->roleid, 00227 'disableGradeHistory' => $CFG->disablegradehistory 00228 ); 00229 $function = 'M.enrol_manual.quickenrolment.init'; 00230 $button->require_yui_module($modules, $function, array($arguments)); 00231 $button->strings_for_js(array( 00232 'ajaxoneuserfound', 00233 'ajaxxusersfound', 00234 'ajaxnext25', 00235 'enrol', 00236 'enrolmentoptions', 00237 'enrolusers', 00238 'errajaxfailedenrol', 00239 'errajaxsearch', 00240 'none', 00241 'usersearch', 00242 'unlimitedduration', 00243 'startdatetoday', 00244 'durationdays', 00245 'enrolperiod', 00246 'finishenrollingusers', 00247 'recovergrades'), 'enrol'); 00248 $button->strings_for_js('assignroles', 'role'); 00249 $button->strings_for_js('startingfrom', 'moodle'); 00250 00251 return $button; 00252 } 00253 00261 public function get_user_enrolment_actions(course_enrolment_manager $manager, $ue) { 00262 $actions = array(); 00263 $context = $manager->get_context(); 00264 $instance = $ue->enrolmentinstance; 00265 $params = $manager->get_moodlepage()->url->params(); 00266 $params['ue'] = $ue->id; 00267 if ($this->allow_unenrol_user($instance, $ue) && has_capability("enrol/manual:unenrol", $context)) { 00268 $url = new moodle_url('/enrol/unenroluser.php', $params); 00269 $actions[] = new user_enrolment_action(new pix_icon('t/delete', ''), get_string('unenrol', 'enrol'), $url, array('class'=>'unenrollink', 'rel'=>$ue->id)); 00270 } 00271 if ($this->allow_manage($instance) && has_capability("enrol/manual:manage", $context)) { 00272 $url = new moodle_url('/enrol/manual/editenrolment.php', $params); 00273 $actions[] = new user_enrolment_action(new pix_icon('t/edit', ''), get_string('edit'), $url, array('class'=>'editenrollink', 'rel'=>$ue->id)); 00274 } 00275 return $actions; 00276 } 00277 00283 public function get_bulk_operations(course_enrolment_manager $manager) { 00284 global $CFG; 00285 require_once($CFG->dirroot.'/enrol/manual/locallib.php'); 00286 $bulkoperations = array( 00287 'editselectedusers' => new enrol_manual_editselectedusers_operation($manager, $this), 00288 'deleteselectedusers' => new enrol_manual_deleteselectedusers_operation($manager, $this) 00289 ); 00290 return $bulkoperations; 00291 } 00292 } 00293 00300 function enrol_manual_supports($feature) { 00301 switch($feature) { 00302 case ENROL_RESTORE_TYPE: return ENROL_RESTORE_EXACT; 00303 00304 default: return null; 00305 } 00306 }