PageRenderTime 78ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/mod/lti/backup/moodle2/restore_lti_stepslib.php

https://bitbucket.org/jvomarques/moodle-esmarn
PHP | 284 lines | 137 code | 48 blank | 99 comment | 23 complexity | d49de739327bf7389a2abb65cd26d0b0 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, GPL-3.0
  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. // This file is part of BasicLTI4Moodle
  18. //
  19. // BasicLTI4Moodle is an IMS BasicLTI (Basic Learning Tools for Interoperability)
  20. // consumer for Moodle 1.9 and Moodle 2.0. BasicLTI is a IMS Standard that allows web
  21. // based learning tools to be easily integrated in LMS as native ones. The IMS BasicLTI
  22. // specification is part of the IMS standard Common Cartridge 1.1 Sakai and other main LMS
  23. // are already supporting or going to support BasicLTI. This project Implements the consumer
  24. // for Moodle. Moodle is a Free Open source Learning Management System by Martin Dougiamas.
  25. // BasicLTI4Moodle is a project iniciated and leaded by Ludo(Marc Alier) and Jordi Piguillem
  26. // at the GESSI research group at UPC.
  27. // SimpleLTI consumer for Moodle is an implementation of the early specification of LTI
  28. // by Charles Severance (Dr Chuck) htp://dr-chuck.com , developed by Jordi Piguillem in a
  29. // Google Summer of Code 2008 project co-mentored by Charles Severance and Marc Alier.
  30. //
  31. // BasicLTI4Moodle is copyright 2009 by Marc Alier Forment, Jordi Piguillem and Nikolas Galanis
  32. // of the Universitat Politecnica de Catalunya http://www.upc.edu
  33. // Contact info: Marc Alier Forment granludo @ gmail.com or marc.alier @ upc.edu.
  34. /**
  35. * This file contains all the restore steps that will be used
  36. * by the restore_lti_activity_task
  37. *
  38. * @package mod_lti
  39. * @copyright 2009 Marc Alier, Jordi Piguillem, Nikolas Galanis
  40. * marc.alier@upc.edu
  41. * @copyright 2009 Universitat Politecnica de Catalunya http://www.upc.edu
  42. * @author Marc Alier
  43. * @author Jordi Piguillem
  44. * @author Nikolas Galanis
  45. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  46. */
  47. defined('MOODLE_INTERNAL') || die;
  48. /**
  49. * Structure step to restore one lti activity
  50. */
  51. class restore_lti_activity_structure_step extends restore_activity_structure_step {
  52. /** @var bool */
  53. protected $newltitype = false;
  54. protected function define_structure() {
  55. $paths = array();
  56. // To know if we are including userinfo.
  57. $userinfo = $this->get_setting_value('userinfo');
  58. $lti = new restore_path_element('lti', '/activity/lti');
  59. $paths[] = $lti;
  60. $paths[] = new restore_path_element('ltitype', '/activity/lti/ltitype');
  61. $paths[] = new restore_path_element('ltitypesconfig', '/activity/lti/ltitype/ltitypesconfigs/ltitypesconfig');
  62. $paths[] = new restore_path_element('ltitypesconfigencrypted',
  63. '/activity/lti/ltitype/ltitypesconfigs/ltitypesconfigencrypted');
  64. $paths[] = new restore_path_element('ltitoolproxy', '/activity/lti/ltitype/ltitoolproxy');
  65. $paths[] = new restore_path_element('ltitoolsetting', '/activity/lti/ltitype/ltitoolproxy/ltitoolsettings/ltitoolsetting');
  66. if ($userinfo) {
  67. $submission = new restore_path_element('ltisubmission', '/activity/lti/ltisubmissions/ltisubmission');
  68. $paths[] = $submission;
  69. }
  70. // Add support for subplugin structures.
  71. $this->add_subplugin_structure('ltisource', $lti);
  72. $this->add_subplugin_structure('ltiservice', $lti);
  73. // Return the paths wrapped into standard activity structure.
  74. return $this->prepare_activity_structure($paths);
  75. }
  76. protected function process_lti($data) {
  77. global $DB;
  78. $data = (object)$data;
  79. $oldid = $data->id;
  80. $data->course = $this->get_courseid();
  81. $data->servicesalt = uniqid('', true);
  82. // Any changes to the list of dates that needs to be rolled should be same during course restore and course reset.
  83. // See MDL-9367.
  84. // Grade used to be a float (whole numbers only), restore as int.
  85. $data->grade = (int) $data->grade;
  86. $data->typeid = 0;
  87. // Try to decrypt resourcekey and password. Null if not possible (DB default).
  88. // Note these fields were originally encrypted on backup using {link @encrypted_final_element}.
  89. $data->resourcekey = isset($data->resourcekey) ? $this->decrypt($data->resourcekey) : null;
  90. $data->password = isset($data->password) ? $this->decrypt($data->password) : null;
  91. $newitemid = $DB->insert_record('lti', $data);
  92. // Immediately after inserting "activity" record, call this.
  93. $this->apply_activity_instance($newitemid);
  94. }
  95. /**
  96. * Process an lti type restore
  97. * @param mixed $data The data from backup XML file
  98. * @return void
  99. */
  100. protected function process_ltitype($data) {
  101. global $DB, $USER;
  102. $data = (object)$data;
  103. $oldid = $data->id;
  104. if (!empty($data->createdby)) {
  105. $data->createdby = $this->get_mappingid('user', $data->createdby) ?: $USER->id;
  106. }
  107. $courseid = $this->get_courseid();
  108. $data->course = ($this->get_mappingid('course', $data->course) == $courseid) ? $courseid : SITEID;
  109. // Try to find existing lti type with the same properties.
  110. $ltitypeid = $this->find_existing_lti_type($data);
  111. $this->newltitype = false;
  112. if (!$ltitypeid && $data->course == $courseid) {
  113. unset($data->toolproxyid); // Course tools can not use LTI2.
  114. $ltitypeid = $DB->insert_record('lti_types', $data);
  115. $this->newltitype = true;
  116. $this->set_mapping('ltitype', $oldid, $ltitypeid);
  117. }
  118. // Add the typeid entry back to LTI module.
  119. $DB->update_record('lti', ['id' => $this->get_new_parentid('lti'), 'typeid' => $ltitypeid]);
  120. }
  121. /**
  122. * Attempts to find existing record in lti_type
  123. * @param stdClass $data
  124. * @return int|null field lti_types.id or null if tool is not found
  125. */
  126. protected function find_existing_lti_type($data) {
  127. global $DB;
  128. if ($ltitypeid = $this->get_mappingid('ltitype', $data->id)) {
  129. return $ltitypeid;
  130. }
  131. $ltitype = null;
  132. $params = (array)$data;
  133. if ($this->task->is_samesite()) {
  134. // If we are restoring on the same site try to find lti type with the same id.
  135. $sql = 'id = :id AND course = :course';
  136. $sql .= ($data->toolproxyid) ? ' AND toolproxyid = :toolproxyid' : ' AND toolproxyid IS NULL';
  137. if ($DB->record_exists_select('lti_types', $sql, $params)) {
  138. $this->set_mapping('ltitype', $data->id, $data->id);
  139. if ($data->toolproxyid) {
  140. $this->set_mapping('ltitoolproxy', $data->toolproxyid, $data->toolproxyid);
  141. }
  142. return $data->id;
  143. }
  144. }
  145. if ($data->course != $this->get_courseid()) {
  146. // Site tools are not backed up and are not restored.
  147. return null;
  148. }
  149. // Now try to find the same type on the current site available in this course.
  150. // Compare only fields baseurl, course and name, if they are the same we assume it is the same tool.
  151. // LTI2 is not possible in the course so we add "lt.toolproxyid IS NULL" to the query.
  152. $sql = 'SELECT id
  153. FROM {lti_types}
  154. WHERE baseurl = :baseurl AND course = :course AND name = :name AND toolproxyid IS NULL';
  155. if ($ltitype = $DB->get_record_sql($sql, $params, IGNORE_MULTIPLE)) {
  156. $this->set_mapping('ltitype', $data->id, $ltitype->id);
  157. return $ltitype->id;
  158. }
  159. return null;
  160. }
  161. /**
  162. * Process an lti config restore
  163. * @param mixed $data The data from backup XML file
  164. */
  165. protected function process_ltitypesconfig($data) {
  166. global $DB;
  167. $data = (object)$data;
  168. $data->typeid = $this->get_new_parentid('ltitype');
  169. // Only add configuration if the new lti_type was created.
  170. if ($data->typeid && $this->newltitype) {
  171. if ($data->name == 'servicesalt') {
  172. $data->value = uniqid('', true);
  173. }
  174. $DB->insert_record('lti_types_config', $data);
  175. }
  176. }
  177. /**
  178. * Process an lti config restore
  179. * @param mixed $data The data from backup XML file
  180. */
  181. protected function process_ltitypesconfigencrypted($data) {
  182. global $DB;
  183. $data = (object)$data;
  184. $data->typeid = $this->get_new_parentid('ltitype');
  185. // Only add configuration if the new lti_type was created.
  186. if ($data->typeid && $this->newltitype) {
  187. $data->value = $this->decrypt($data->value);
  188. if (!is_null($data->value)) {
  189. $DB->insert_record('lti_types_config', $data);
  190. }
  191. }
  192. }
  193. /**
  194. * Process a restore of LTI tool registration
  195. * This method is empty because we actually process registration as part of process_ltitype()
  196. * @param mixed $data The data from backup XML file
  197. */
  198. protected function process_ltitoolproxy($data) {
  199. }
  200. /**
  201. * Process an lti tool registration settings restore (only settings for the current activity)
  202. * @param mixed $data The data from backup XML file
  203. */
  204. protected function process_ltitoolsetting($data) {
  205. global $DB;
  206. $data = (object)$data;
  207. $data->toolproxyid = $this->get_new_parentid('ltitoolproxy');
  208. if (!$data->toolproxyid) {
  209. return;
  210. }
  211. $data->course = $this->get_courseid();
  212. $data->coursemoduleid = $this->task->get_moduleid();
  213. $DB->insert_record('lti_tool_settings', $data);
  214. }
  215. /**
  216. * Process a submission restore
  217. * @param mixed $data The data from backup XML file
  218. */
  219. protected function process_ltisubmission($data) {
  220. global $DB;
  221. $data = (object)$data;
  222. $oldid = $data->id;
  223. $data->ltiid = $this->get_new_parentid('lti');
  224. $data->datesubmitted = $this->apply_date_offset($data->datesubmitted);
  225. $data->dateupdated = $this->apply_date_offset($data->dateupdated);
  226. if ($data->userid > 0) {
  227. $data->userid = $this->get_mappingid('user', $data->userid);
  228. }
  229. $newitemid = $DB->insert_record('lti_submission', $data);
  230. $this->set_mapping('ltisubmission', $oldid, $newitemid);
  231. }
  232. protected function after_execute() {
  233. // Add lti related files, no need to match by itemname (just internally handled context).
  234. $this->add_related_files('mod_lti', 'intro', null);
  235. }
  236. }