PageRenderTime 64ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/enrol/lti/lib.php

http://github.com/moodle/moodle
PHP | 417 lines | 212 code | 67 blank | 138 comment | 13 complexity | 6ab80b5e49d057d273c8297946af9e27 MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  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. * LTI enrolment plugin main library file.
  18. *
  19. * @package enrol_lti
  20. * @copyright 2016 Mark Nelson <markn@moodle.com>
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. use enrol_lti\data_connector;
  24. use IMSGlobal\LTI\ToolProvider\ToolConsumer;
  25. defined('MOODLE_INTERNAL') || die();
  26. /**
  27. * LTI enrolment plugin class.
  28. *
  29. * @package enrol_lti
  30. * @copyright 2016 Mark Nelson <markn@moodle.com>
  31. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32. */
  33. class enrol_lti_plugin extends enrol_plugin {
  34. /**
  35. * Return true if we can add a new instance to this course.
  36. *
  37. * @param int $courseid
  38. * @return boolean
  39. */
  40. public function can_add_instance($courseid) {
  41. $context = context_course::instance($courseid, MUST_EXIST);
  42. return has_capability('moodle/course:enrolconfig', $context) && has_capability('enrol/lti:config', $context);
  43. }
  44. /**
  45. * Is it possible to delete enrol instance via standard UI?
  46. *
  47. * @param object $instance
  48. * @return bool
  49. */
  50. public function can_delete_instance($instance) {
  51. $context = context_course::instance($instance->courseid);
  52. return has_capability('enrol/lti:config', $context);
  53. }
  54. /**
  55. * Is it possible to hide/show enrol instance via standard UI?
  56. *
  57. * @param stdClass $instance
  58. * @return bool
  59. */
  60. public function can_hide_show_instance($instance) {
  61. $context = context_course::instance($instance->courseid);
  62. return has_capability('enrol/lti:config', $context);
  63. }
  64. /**
  65. * Returns true if it's possible to unenrol users.
  66. *
  67. * @param stdClass $instance course enrol instance
  68. * @return bool
  69. */
  70. public function allow_unenrol(stdClass $instance) {
  71. return true;
  72. }
  73. /**
  74. * We are a good plugin and don't invent our own UI/validation code path.
  75. *
  76. * @return boolean
  77. */
  78. public function use_standard_editing_ui() {
  79. return true;
  80. }
  81. /**
  82. * Add new instance of enrol plugin.
  83. *
  84. * @param object $course
  85. * @param array $fields instance fields
  86. * @return int id of new instance, null if can not be created
  87. */
  88. public function add_instance($course, array $fields = null) {
  89. global $DB;
  90. $instanceid = parent::add_instance($course, $fields);
  91. // Add additional data to our table.
  92. $data = new stdClass();
  93. $data->enrolid = $instanceid;
  94. $data->timecreated = time();
  95. $data->timemodified = $data->timecreated;
  96. foreach ($fields as $field => $value) {
  97. $data->$field = $value;
  98. }
  99. $DB->insert_record('enrol_lti_tools', $data);
  100. return $instanceid;
  101. }
  102. /**
  103. * Update instance of enrol plugin.
  104. *
  105. * @param stdClass $instance
  106. * @param stdClass $data modified instance fields
  107. * @return boolean
  108. */
  109. public function update_instance($instance, $data) {
  110. global $DB;
  111. parent::update_instance($instance, $data);
  112. // Remove the fields we don't want to override.
  113. unset($data->id);
  114. unset($data->timecreated);
  115. unset($data->timemodified);
  116. // Convert to an array we can loop over.
  117. $fields = (array) $data;
  118. // Update the data in our table.
  119. $tool = new stdClass();
  120. $tool->id = $data->toolid;
  121. $tool->timemodified = time();
  122. foreach ($fields as $field => $value) {
  123. $tool->$field = $value;
  124. }
  125. return $DB->update_record('enrol_lti_tools', $tool);
  126. }
  127. /**
  128. * Delete plugin specific information.
  129. *
  130. * @param stdClass $instance
  131. * @return void
  132. */
  133. public function delete_instance($instance) {
  134. global $DB;
  135. // Get the tool associated with this instance.
  136. $tool = $DB->get_record('enrol_lti_tools', array('enrolid' => $instance->id), 'id', MUST_EXIST);
  137. // Delete any users associated with this tool.
  138. $DB->delete_records('enrol_lti_users', array('toolid' => $tool->id));
  139. // Get tool and consumer mappings.
  140. $rsmapping = $DB->get_recordset('enrol_lti_tool_consumer_map', array('toolid' => $tool->id));
  141. // Delete consumers that are linked to this tool and their related data.
  142. $dataconnector = new data_connector();
  143. foreach ($rsmapping as $mapping) {
  144. $consumer = new ToolConsumer(null, $dataconnector);
  145. $consumer->setRecordId($mapping->consumerid);
  146. $dataconnector->deleteToolConsumer($consumer);
  147. }
  148. $rsmapping->close();
  149. // Delete mapping records.
  150. $DB->delete_records('enrol_lti_tool_consumer_map', array('toolid' => $tool->id));
  151. // Delete the lti tool record.
  152. $DB->delete_records('enrol_lti_tools', array('id' => $tool->id));
  153. // Time for the parent to do it's thang, yeow.
  154. parent::delete_instance($instance);
  155. }
  156. /**
  157. * Handles un-enrolling a user.
  158. *
  159. * @param stdClass $instance
  160. * @param int $userid
  161. * @return void
  162. */
  163. public function unenrol_user(stdClass $instance, $userid) {
  164. global $DB;
  165. // Get the tool associated with this instance. Note - it may not exist if we have deleted
  166. // the tool. This is fine because we have already cleaned the 'enrol_lti_users' table.
  167. if ($tool = $DB->get_record('enrol_lti_tools', array('enrolid' => $instance->id), 'id')) {
  168. // Need to remove the user from the users table.
  169. $DB->delete_records('enrol_lti_users', array('userid' => $userid, 'toolid' => $tool->id));
  170. }
  171. parent::unenrol_user($instance, $userid);
  172. }
  173. /**
  174. * Add elements to the edit instance form.
  175. *
  176. * @param stdClass $instance
  177. * @param MoodleQuickForm $mform
  178. * @param context $context
  179. * @return bool
  180. */
  181. public function edit_instance_form($instance, MoodleQuickForm $mform, $context) {
  182. global $DB;
  183. $nameattribs = array('size' => '20', 'maxlength' => '255');
  184. $mform->addElement('text', 'name', get_string('custominstancename', 'enrol'), $nameattribs);
  185. $mform->setType('name', PARAM_TEXT);
  186. $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'server');
  187. $tools = array();
  188. $tools[$context->id] = get_string('course');
  189. $modinfo = get_fast_modinfo($instance->courseid);
  190. $mods = $modinfo->get_cms();
  191. foreach ($mods as $mod) {
  192. $tools[$mod->context->id] = format_string($mod->name);
  193. }
  194. $mform->addElement('select', 'contextid', get_string('tooltobeprovided', 'enrol_lti'), $tools);
  195. $mform->setDefault('contextid', $context->id);
  196. $mform->addElement('duration', 'enrolperiod', get_string('enrolperiod', 'enrol_lti'),
  197. array('optional' => true, 'defaultunit' => DAYSECS));
  198. $mform->setDefault('enrolperiod', 0);
  199. $mform->addHelpButton('enrolperiod', 'enrolperiod', 'enrol_lti');
  200. $mform->addElement('date_time_selector', 'enrolstartdate', get_string('enrolstartdate', 'enrol_lti'),
  201. array('optional' => true));
  202. $mform->setDefault('enrolstartdate', 0);
  203. $mform->addHelpButton('enrolstartdate', 'enrolstartdate', 'enrol_lti');
  204. $mform->addElement('date_time_selector', 'enrolenddate', get_string('enrolenddate', 'enrol_lti'),
  205. array('optional' => true));
  206. $mform->setDefault('enrolenddate', 0);
  207. $mform->addHelpButton('enrolenddate', 'enrolenddate', 'enrol_lti');
  208. $mform->addElement('text', 'maxenrolled', get_string('maxenrolled', 'enrol_lti'));
  209. $mform->setDefault('maxenrolled', 0);
  210. $mform->addHelpButton('maxenrolled', 'maxenrolled', 'enrol_lti');
  211. $mform->setType('maxenrolled', PARAM_INT);
  212. $assignableroles = get_assignable_roles($context);
  213. $mform->addElement('select', 'roleinstructor', get_string('roleinstructor', 'enrol_lti'), $assignableroles);
  214. $mform->setDefault('roleinstructor', '3');
  215. $mform->addHelpButton('roleinstructor', 'roleinstructor', 'enrol_lti');
  216. $mform->addElement('select', 'rolelearner', get_string('rolelearner', 'enrol_lti'), $assignableroles);
  217. $mform->setDefault('rolelearner', '5');
  218. $mform->addHelpButton('rolelearner', 'rolelearner', 'enrol_lti');
  219. $mform->addElement('header', 'remotesystem', get_string('remotesystem', 'enrol_lti'));
  220. $mform->addElement('text', 'secret', get_string('secret', 'enrol_lti'), 'maxlength="64" size="25"');
  221. $mform->setType('secret', PARAM_ALPHANUM);
  222. $mform->setDefault('secret', random_string(32));
  223. $mform->addHelpButton('secret', 'secret', 'enrol_lti');
  224. $mform->addRule('secret', get_string('required'), 'required');
  225. $mform->addElement('selectyesno', 'gradesync', get_string('gradesync', 'enrol_lti'));
  226. $mform->setDefault('gradesync', 1);
  227. $mform->addHelpButton('gradesync', 'gradesync', 'enrol_lti');
  228. $mform->addElement('selectyesno', 'gradesynccompletion', get_string('requirecompletion', 'enrol_lti'));
  229. $mform->setDefault('gradesynccompletion', 0);
  230. $mform->disabledIf('gradesynccompletion', 'gradesync', 0);
  231. $mform->addElement('selectyesno', 'membersync', get_string('membersync', 'enrol_lti'));
  232. $mform->setDefault('membersync', 1);
  233. $mform->addHelpButton('membersync', 'membersync', 'enrol_lti');
  234. $options = array();
  235. $options[\enrol_lti\helper::MEMBER_SYNC_ENROL_AND_UNENROL] = get_string('membersyncmodeenrolandunenrol', 'enrol_lti');
  236. $options[\enrol_lti\helper::MEMBER_SYNC_ENROL_NEW] = get_string('membersyncmodeenrolnew', 'enrol_lti');
  237. $options[\enrol_lti\helper::MEMBER_SYNC_UNENROL_MISSING] = get_string('membersyncmodeunenrolmissing', 'enrol_lti');
  238. $mform->addElement('select', 'membersyncmode', get_string('membersyncmode', 'enrol_lti'), $options);
  239. $mform->setDefault('membersyncmode', \enrol_lti\helper::MEMBER_SYNC_ENROL_AND_UNENROL);
  240. $mform->addHelpButton('membersyncmode', 'membersyncmode', 'enrol_lti');
  241. $mform->disabledIf('membersyncmode', 'membersync', 0);
  242. $mform->addElement('header', 'defaultheader', get_string('userdefaultvalues', 'enrol_lti'));
  243. $emaildisplay = get_config('enrol_lti', 'emaildisplay');
  244. $choices = array(
  245. 0 => get_string('emaildisplayno'),
  246. 1 => get_string('emaildisplayyes'),
  247. 2 => get_string('emaildisplaycourse')
  248. );
  249. $mform->addElement('select', 'maildisplay', get_string('emaildisplay'), $choices);
  250. $mform->setDefault('maildisplay', $emaildisplay);
  251. $mform->addHelpButton('maildisplay', 'emaildisplay');
  252. $city = get_config('enrol_lti', 'city');
  253. $mform->addElement('text', 'city', get_string('city'), 'maxlength="100" size="25"');
  254. $mform->setType('city', PARAM_TEXT);
  255. $mform->setDefault('city', $city);
  256. $country = get_config('enrol_lti', 'country');
  257. $countries = array('' => get_string('selectacountry') . '...') + get_string_manager()->get_list_of_countries();
  258. $mform->addElement('select', 'country', get_string('selectacountry'), $countries);
  259. $mform->setDefault('country', $country);
  260. $mform->setAdvanced('country');
  261. $timezone = get_config('enrol_lti', 'timezone');
  262. $choices = core_date::get_list_of_timezones(null, true);
  263. $mform->addElement('select', 'timezone', get_string('timezone'), $choices);
  264. $mform->setDefault('timezone', $timezone);
  265. $mform->setAdvanced('timezone');
  266. $lang = get_config('enrol_lti', 'lang');
  267. $mform->addElement('select', 'lang', get_string('preferredlanguage'), get_string_manager()->get_list_of_translations());
  268. $mform->setDefault('lang', $lang);
  269. $mform->setAdvanced('lang');
  270. $institution = get_config('enrol_lti', 'institution');
  271. $mform->addElement('text', 'institution', get_string('institution'), 'maxlength="40" size="25"');
  272. $mform->setType('institution', core_user::get_property_type('institution'));
  273. $mform->setDefault('institution', $institution);
  274. $mform->setAdvanced('institution');
  275. // Check if we are editing an instance.
  276. if (!empty($instance->id)) {
  277. // Get the details from the enrol_lti_tools table.
  278. $ltitool = $DB->get_record('enrol_lti_tools', array('enrolid' => $instance->id), '*', MUST_EXIST);
  279. $mform->addElement('hidden', 'toolid');
  280. $mform->setType('toolid', PARAM_INT);
  281. $mform->setConstant('toolid', $ltitool->id);
  282. $mform->setDefaults((array) $ltitool);
  283. }
  284. }
  285. /**
  286. * Perform custom validation of the data used to edit the instance.
  287. *
  288. * @param array $data array of ("fieldname"=>value) of submitted data
  289. * @param array $files array of uploaded files "element_name"=>tmp_file_path
  290. * @param object $instance The instance loaded from the DB
  291. * @param context $context The context of the instance we are editing
  292. * @return array of "element_name"=>"error_description" if there are errors,
  293. * or an empty array if everything is OK.
  294. * @return void
  295. */
  296. public function edit_instance_validation($data, $files, $instance, $context) {
  297. global $COURSE, $DB;
  298. $errors = array();
  299. if (!empty($data['enrolenddate']) && $data['enrolenddate'] < $data['enrolstartdate']) {
  300. $errors['enrolenddate'] = get_string('enrolenddateerror', 'enrol_lti');
  301. }
  302. if (!empty($data['requirecompletion'])) {
  303. $completion = new completion_info($COURSE);
  304. $moodlecontext = $DB->get_record('context', array('id' => $data['contextid']));
  305. if ($moodlecontext->contextlevel == CONTEXT_MODULE) {
  306. $cm = get_coursemodule_from_id(false, $moodlecontext->instanceid, 0, false, MUST_EXIST);
  307. } else {
  308. $cm = null;
  309. }
  310. if (!$completion->is_enabled($cm)) {
  311. $errors['requirecompletion'] = get_string('errorcompletionenabled', 'enrol_lti');
  312. }
  313. }
  314. return $errors;
  315. }
  316. /**
  317. * Restore instance and map settings.
  318. *
  319. * @param restore_enrolments_structure_step $step
  320. * @param stdClass $data
  321. * @param stdClass $course
  322. * @param int $oldid
  323. */
  324. public function restore_instance(restore_enrolments_structure_step $step, stdClass $data, $course, $oldid) {
  325. // We want to call the parent because we do not want to add an enrol_lti_tools row
  326. // as that is done as part of the restore process.
  327. $instanceid = parent::add_instance($course, (array)$data);
  328. $step->set_mapping('enrol', $oldid, $instanceid);
  329. }
  330. }
  331. /**
  332. * Display the LTI link in the course administration menu.
  333. *
  334. * @param settings_navigation $navigation The settings navigation object
  335. * @param stdClass $course The course
  336. * @param stdclass $context Course context
  337. */
  338. function enrol_lti_extend_navigation_course($navigation, $course, $context) {
  339. // Check that the LTI plugin is enabled.
  340. if (enrol_is_enabled('lti')) {
  341. // Check that they can add an instance.
  342. $ltiplugin = enrol_get_plugin('lti');
  343. if ($ltiplugin->can_add_instance($course->id)) {
  344. $url = new moodle_url('/enrol/lti/index.php', array('courseid' => $course->id));
  345. $settingsnode = navigation_node::create(get_string('sharedexternaltools', 'enrol_lti'), $url,
  346. navigation_node::TYPE_SETTING, null, null, new pix_icon('i/settings', ''));
  347. $navigation->add_node($settingsnode);
  348. }
  349. }
  350. }