[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 /** 18 * LTI enrolment plugin main library file. 19 * 20 * @package enrol_lti 21 * @copyright 2016 Mark Nelson <markn@moodle.com> 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 defined('MOODLE_INTERNAL') || die(); 26 27 /** 28 * LTI enrolment plugin class. 29 * 30 * @package enrol_lti 31 * @copyright 2016 Mark Nelson <markn@moodle.com> 32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 33 */ 34 class enrol_lti_plugin extends enrol_plugin { 35 36 /** 37 * Return true if we can add a new instance to this course. 38 * 39 * @param int $courseid 40 * @return boolean 41 */ 42 public function can_add_instance($courseid) { 43 $context = context_course::instance($courseid, MUST_EXIST); 44 return has_capability('moodle/course:enrolconfig', $context) && has_capability('enrol/lti:config', $context); 45 } 46 47 /** 48 * Is it possible to delete enrol instance via standard UI? 49 * 50 * @param object $instance 51 * @return bool 52 */ 53 public function can_delete_instance($instance) { 54 $context = context_course::instance($instance->courseid); 55 return has_capability('enrol/lti:config', $context); 56 } 57 58 /** 59 * Is it possible to hide/show enrol instance via standard UI? 60 * 61 * @param stdClass $instance 62 * @return bool 63 */ 64 public function can_hide_show_instance($instance) { 65 $context = context_course::instance($instance->courseid); 66 return has_capability('enrol/lti:config', $context); 67 } 68 69 /** 70 * Returns true if it's possible to unenrol users. 71 * 72 * @param stdClass $instance course enrol instance 73 * @return bool 74 */ 75 public function allow_unenrol(stdClass $instance) { 76 return true; 77 } 78 79 /** 80 * We are a good plugin and don't invent our own UI/validation code path. 81 * 82 * @return boolean 83 */ 84 public function use_standard_editing_ui() { 85 return true; 86 } 87 88 /** 89 * Add new instance of enrol plugin. 90 * 91 * @param object $course 92 * @param array $fields instance fields 93 * @return int id of new instance, null if can not be created 94 */ 95 public function add_instance($course, array $fields = null) { 96 global $DB; 97 98 $instanceid = parent::add_instance($course, $fields); 99 100 // Add additional data to our table. 101 $data = new stdClass(); 102 $data->enrolid = $instanceid; 103 $data->timecreated = time(); 104 $data->timemodified = $data->timecreated; 105 foreach ($fields as $field => $value) { 106 $data->$field = $value; 107 } 108 109 $DB->insert_record('enrol_lti_tools', $data); 110 111 return $instanceid; 112 } 113 114 /** 115 * Update instance of enrol plugin. 116 * 117 * @param stdClass $instance 118 * @param stdClass $data modified instance fields 119 * @return boolean 120 */ 121 public function update_instance($instance, $data) { 122 global $DB; 123 124 parent::update_instance($instance, $data); 125 126 // Remove the fields we don't want to override. 127 unset($data->id); 128 unset($data->timecreated); 129 unset($data->timemodified); 130 131 // Convert to an array we can loop over. 132 $fields = (array) $data; 133 134 // Update the data in our table. 135 $tool = new stdClass(); 136 $tool->id = $data->toolid; 137 $tool->timemodified = time(); 138 foreach ($fields as $field => $value) { 139 $tool->$field = $value; 140 } 141 142 return $DB->update_record('enrol_lti_tools', $tool); 143 } 144 145 /** 146 * Delete plugin specific information. 147 * 148 * @param stdClass $instance 149 * @return void 150 */ 151 public function delete_instance($instance) { 152 global $DB; 153 154 // Get the tool associated with this instance. 155 $tool = $DB->get_record('enrol_lti_tools', array('enrolid' => $instance->id), 'id', MUST_EXIST); 156 157 // Delete any users associated with this tool. 158 $DB->delete_records('enrol_lti_users', array('toolid' => $tool->id)); 159 160 // Delete the lti tool record. 161 $DB->delete_records('enrol_lti_tools', array('id' => $tool->id)); 162 163 // Time for the parent to do it's thang, yeow. 164 parent::delete_instance($instance); 165 } 166 167 /** 168 * Handles un-enrolling a user. 169 * 170 * @param stdClass $instance 171 * @param int $userid 172 * @return void 173 */ 174 public function unenrol_user(stdClass $instance, $userid) { 175 global $DB; 176 177 // Get the tool associated with this instance. Note - it may not exist if we have deleted 178 // the tool. This is fine because we have already cleaned the 'enrol_lti_users' table. 179 if ($tool = $DB->get_record('enrol_lti_tools', array('enrolid' => $instance->id), 'id')) { 180 // Need to remove the user from the users table. 181 $DB->delete_records('enrol_lti_users', array('userid' => $userid, 'toolid' => $tool->id)); 182 } 183 184 parent::unenrol_user($instance, $userid); 185 } 186 187 /** 188 * Add elements to the edit instance form. 189 * 190 * @param stdClass $instance 191 * @param MoodleQuickForm $mform 192 * @param context $context 193 * @return bool 194 */ 195 public function edit_instance_form($instance, MoodleQuickForm $mform, $context) { 196 global $DB; 197 198 $nameattribs = array('size' => '20', 'maxlength' => '255'); 199 $mform->addElement('text', 'name', get_string('custominstancename', 'enrol'), $nameattribs); 200 $mform->setType('name', PARAM_TEXT); 201 $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'server'); 202 203 $tools = array(); 204 $tools[$context->id] = get_string('course'); 205 $modinfo = get_fast_modinfo($instance->courseid); 206 $mods = $modinfo->get_cms(); 207 foreach ($mods as $mod) { 208 $tools[$mod->context->id] = format_string($mod->name); 209 } 210 211 $mform->addElement('select', 'contextid', get_string('tooltobeprovided', 'enrol_lti'), $tools); 212 $mform->setDefault('contextid', $context->id); 213 214 $mform->addElement('duration', 'enrolperiod', get_string('enrolperiod', 'enrol_lti'), 215 array('optional' => true, 'defaultunit' => DAYSECS)); 216 $mform->setDefault('enrolperiod', 0); 217 $mform->addHelpButton('enrolperiod', 'enrolperiod', 'enrol_lti'); 218 219 $mform->addElement('date_time_selector', 'enrolstartdate', get_string('enrolstartdate', 'enrol_lti'), 220 array('optional' => true)); 221 $mform->setDefault('enrolstartdate', 0); 222 $mform->addHelpButton('enrolstartdate', 'enrolstartdate', 'enrol_lti'); 223 224 $mform->addElement('date_time_selector', 'enrolenddate', get_string('enrolenddate', 'enrol_lti'), 225 array('optional' => true)); 226 $mform->setDefault('enrolenddate', 0); 227 $mform->addHelpButton('enrolenddate', 'enrolenddate', 'enrol_lti'); 228 229 $mform->addElement('text', 'maxenrolled', get_string('maxenrolled', 'enrol_lti')); 230 $mform->setDefault('maxenrolled', 0); 231 $mform->addHelpButton('maxenrolled', 'maxenrolled', 'enrol_lti'); 232 $mform->setType('maxenrolled', PARAM_INT); 233 234 $assignableroles = get_assignable_roles($context); 235 236 $mform->addElement('select', 'roleinstructor', get_string('roleinstructor', 'enrol_lti'), $assignableroles); 237 $mform->setDefault('roleinstructor', '3'); 238 $mform->addHelpButton('roleinstructor', 'roleinstructor', 'enrol_lti'); 239 240 $mform->addElement('select', 'rolelearner', get_string('rolelearner', 'enrol_lti'), $assignableroles); 241 $mform->setDefault('rolelearner', '5'); 242 $mform->addHelpButton('rolelearner', 'rolelearner', 'enrol_lti'); 243 244 $mform->addElement('header', 'remotesystem', get_string('remotesystem', 'enrol_lti')); 245 246 $mform->addElement('text', 'secret', get_string('secret', 'enrol_lti'), 'maxlength="64" size="25"'); 247 $mform->setType('secret', PARAM_ALPHANUM); 248 $mform->setDefault('secret', random_string(32)); 249 $mform->addHelpButton('secret', 'secret', 'enrol_lti'); 250 $mform->addRule('secret', get_string('required'), 'required'); 251 252 $mform->addElement('selectyesno', 'gradesync', get_string('gradesync', 'enrol_lti')); 253 $mform->setDefault('gradesync', 1); 254 $mform->addHelpButton('gradesync', 'gradesync', 'enrol_lti'); 255 256 $mform->addElement('selectyesno', 'gradesynccompletion', get_string('requirecompletion', 'enrol_lti')); 257 $mform->setDefault('gradesynccompletion', 0); 258 $mform->disabledIf('gradesynccompletion', 'gradesync', 0); 259 260 $mform->addElement('selectyesno', 'membersync', get_string('membersync', 'enrol_lti')); 261 $mform->setDefault('membersync', 1); 262 $mform->addHelpButton('membersync', 'membersync', 'enrol_lti'); 263 264 $options = array(); 265 $options[\enrol_lti\helper::MEMBER_SYNC_ENROL_AND_UNENROL] = get_string('membersyncmodeenrolandunenrol', 'enrol_lti'); 266 $options[\enrol_lti\helper::MEMBER_SYNC_ENROL_NEW] = get_string('membersyncmodeenrolnew', 'enrol_lti'); 267 $options[\enrol_lti\helper::MEMBER_SYNC_UNENROL_MISSING] = get_string('membersyncmodeunenrolmissing', 'enrol_lti'); 268 $mform->addElement('select', 'membersyncmode', get_string('membersyncmode', 'enrol_lti'), $options); 269 $mform->setDefault('membersyncmode', \enrol_lti\helper::MEMBER_SYNC_ENROL_AND_UNENROL); 270 $mform->addHelpButton('membersyncmode', 'membersyncmode', 'enrol_lti'); 271 $mform->disabledIf('membersyncmode', 'membersync', 0); 272 273 $mform->addElement('header', 'defaultheader', get_string('userdefaultvalues', 'enrol_lti')); 274 275 $emaildisplay = get_config('enrol_lti', 'emaildisplay'); 276 $choices = array( 277 0 => get_string('emaildisplayno'), 278 1 => get_string('emaildisplayyes'), 279 2 => get_string('emaildisplaycourse') 280 ); 281 $mform->addElement('select', 'maildisplay', get_string('emaildisplay'), $choices); 282 $mform->setDefault('maildisplay', $emaildisplay); 283 284 $city = get_config('enrol_lti', 'city'); 285 $mform->addElement('text', 'city', get_string('city'), 'maxlength="100" size="25"'); 286 $mform->setType('city', PARAM_TEXT); 287 $mform->setDefault('city', $city); 288 289 $country = get_config('enrol_lti', 'country'); 290 $countries = array('' => get_string('selectacountry') . '...') + get_string_manager()->get_list_of_countries(); 291 $mform->addElement('select', 'country', get_string('selectacountry'), $countries); 292 $mform->setDefault('country', $country); 293 $mform->setAdvanced('country'); 294 295 $timezone = get_config('enrol_lti', 'timezone'); 296 $choices = core_date::get_list_of_timezones(null, true); 297 $mform->addElement('select', 'timezone', get_string('timezone'), $choices); 298 $mform->setDefault('timezone', $timezone); 299 $mform->setAdvanced('timezone'); 300 301 $lang = get_config('enrol_lti', 'lang'); 302 $mform->addElement('select', 'lang', get_string('preferredlanguage'), get_string_manager()->get_list_of_translations()); 303 $mform->setDefault('lang', $lang); 304 $mform->setAdvanced('lang'); 305 306 $institution = get_config('enrol_lti', 'institution'); 307 $mform->addElement('text', 'institution', get_string('institution'), 'maxlength="40" size="25"'); 308 $mform->setType('institution', core_user::get_property_type('institution')); 309 $mform->setDefault('institution', $institution); 310 $mform->setAdvanced('institution'); 311 312 // Check if we are editing an instance. 313 if (!empty($instance->id)) { 314 // Get the details from the enrol_lti_tools table. 315 $ltitool = $DB->get_record('enrol_lti_tools', array('enrolid' => $instance->id), '*', MUST_EXIST); 316 317 $mform->addElement('hidden', 'toolid'); 318 $mform->setType('toolid', PARAM_INT); 319 $mform->setConstant('toolid', $ltitool->id); 320 321 $mform->setDefaults((array) $ltitool); 322 } 323 } 324 325 /** 326 * Perform custom validation of the data used to edit the instance. 327 * 328 * @param array $data array of ("fieldname"=>value) of submitted data 329 * @param array $files array of uploaded files "element_name"=>tmp_file_path 330 * @param object $instance The instance loaded from the DB 331 * @param context $context The context of the instance we are editing 332 * @return array of "element_name"=>"error_description" if there are errors, 333 * or an empty array if everything is OK. 334 * @return void 335 */ 336 public function edit_instance_validation($data, $files, $instance, $context) { 337 global $COURSE, $DB; 338 339 $errors = array(); 340 341 if (!empty($data['enrolenddate']) && $data['enrolenddate'] < $data['enrolstartdate']) { 342 $errors['enrolenddate'] = get_string('enrolenddateerror', 'enrol_lti'); 343 } 344 345 if (!empty($data['requirecompletion'])) { 346 $completion = new completion_info($COURSE); 347 $moodlecontext = $DB->get_record('context', array('id' => $data['contextid'])); 348 if ($moodlecontext->contextlevel == CONTEXT_MODULE) { 349 $cm = get_coursemodule_from_id(false, $moodlecontext->instanceid, 0, false, MUST_EXIST); 350 } else { 351 $cm = null; 352 } 353 354 if (!$completion->is_enabled($cm)) { 355 $errors['requirecompletion'] = get_string('errorcompletionenabled', 'enrol_lti'); 356 } 357 } 358 359 return $errors; 360 } 361 362 /** 363 * Gets an array of the user enrolment actions. 364 * 365 * @param course_enrolment_manager $manager 366 * @param stdClass $ue A user enrolment object 367 * @return array An array of user_enrolment_actions 368 */ 369 public function get_user_enrolment_actions(course_enrolment_manager $manager, $ue) { 370 $actions = array(); 371 $context = $manager->get_context(); 372 $instance = $ue->enrolmentinstance; 373 $params = $manager->get_moodlepage()->url->params(); 374 $params['ue'] = $ue->id; 375 if ($this->allow_unenrol_user($instance, $ue) && has_capability("enrol/lti:unenrol", $context)) { 376 $url = new moodle_url('/enrol/unenroluser.php', $params); 377 $actions[] = new user_enrolment_action(new pix_icon('t/delete', ''), get_string('unenrol', 'enrol'), $url, 378 array('class' => 'unenrollink', 'rel' => $ue->id)); 379 } 380 if ($this->allow_manage($instance) && has_capability("enrol/lti:manage", $context)) { 381 $url = new moodle_url('/enrol/editenrolment.php', $params); 382 $actions[] = new user_enrolment_action(new pix_icon('t/edit', ''), get_string('edit'), $url, 383 array('class' => 'editenrollink', 'rel' => $ue->id)); 384 } 385 return $actions; 386 } 387 388 /** 389 * Restore instance and map settings. 390 * 391 * @param restore_enrolments_structure_step $step 392 * @param stdClass $data 393 * @param stdClass $course 394 * @param int $oldid 395 */ 396 public function restore_instance(restore_enrolments_structure_step $step, stdClass $data, $course, $oldid) { 397 // We want to call the parent because we do not want to add an enrol_lti_tools row 398 // as that is done as part of the restore process. 399 $instanceid = parent::add_instance($course, (array)$data); 400 $step->set_mapping('enrol', $oldid, $instanceid); 401 } 402 } 403 404 /** 405 * Display the LTI link in the course administration menu. 406 * 407 * @param settings_navigation $navigation The settings navigation object 408 * @param stdClass $course The course 409 * @param stdclass $context Course context 410 */ 411 function enrol_lti_extend_navigation_course($navigation, $course, $context) { 412 // Check that the LTI plugin is enabled. 413 if (enrol_is_enabled('lti')) { 414 // Check that they can add an instance. 415 $ltiplugin = enrol_get_plugin('lti'); 416 if ($ltiplugin->can_add_instance($course->id)) { 417 $url = new moodle_url('/enrol/lti/index.php', array('courseid' => $course->id)); 418 $settingsnode = navigation_node::create(get_string('sharedexternaltools', 'enrol_lti'), $url, 419 navigation_node::TYPE_SETTING, null, null, new pix_icon('i/settings', '')); 420 421 $navigation->add_node($settingsnode); 422 } 423 } 424 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |