PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://gitlab.com/unofficial-mirrors/moodle
PHP | 285 lines | 138 code | 48 blank | 99 comment | 23 complexity | 15b8daeca1e705a1bff3108e6f380eca MD5 | raw file
  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 ' . $DB->sql_compare_text('baseurl', 255) . ' = ' . $DB->sql_compare_text(':baseurl', 255) . ' AND
  155. course = :course AND name = :name AND toolproxyid IS NULL';
  156. if ($ltitype = $DB->get_record_sql($sql, $params, IGNORE_MULTIPLE)) {
  157. $this->set_mapping('ltitype', $data->id, $ltitype->id);
  158. return $ltitype->id;
  159. }
  160. return null;
  161. }
  162. /**
  163. * Process an lti config restore
  164. * @param mixed $data The data from backup XML file
  165. */
  166. protected function process_ltitypesconfig($data) {
  167. global $DB;
  168. $data = (object)$data;
  169. $data->typeid = $this->get_new_parentid('ltitype');
  170. // Only add configuration if the new lti_type was created.
  171. if ($data->typeid && $this->newltitype) {
  172. if ($data->name == 'servicesalt') {
  173. $data->value = uniqid('', true);
  174. }
  175. $DB->insert_record('lti_types_config', $data);
  176. }
  177. }
  178. /**
  179. * Process an lti config restore
  180. * @param mixed $data The data from backup XML file
  181. */
  182. protected function process_ltitypesconfigencrypted($data) {
  183. global $DB;
  184. $data = (object)$data;
  185. $data->typeid = $this->get_new_parentid('ltitype');
  186. // Only add configuration if the new lti_type was created.
  187. if ($data->typeid && $this->newltitype) {
  188. $data->value = $this->decrypt($data->value);
  189. if (!is_null($data->value)) {
  190. $DB->insert_record('lti_types_config', $data);
  191. }
  192. }
  193. }
  194. /**
  195. * Process a restore of LTI tool registration
  196. * This method is empty because we actually process registration as part of process_ltitype()
  197. * @param mixed $data The data from backup XML file
  198. */
  199. protected function process_ltitoolproxy($data) {
  200. }
  201. /**
  202. * Process an lti tool registration settings restore (only settings for the current activity)
  203. * @param mixed $data The data from backup XML file
  204. */
  205. protected function process_ltitoolsetting($data) {
  206. global $DB;
  207. $data = (object)$data;
  208. $data->toolproxyid = $this->get_new_parentid('ltitoolproxy');
  209. if (!$data->toolproxyid) {
  210. return;
  211. }
  212. $data->course = $this->get_courseid();
  213. $data->coursemoduleid = $this->task->get_moduleid();
  214. $DB->insert_record('lti_tool_settings', $data);
  215. }
  216. /**
  217. * Process a submission restore
  218. * @param mixed $data The data from backup XML file
  219. */
  220. protected function process_ltisubmission($data) {
  221. global $DB;
  222. $data = (object)$data;
  223. $oldid = $data->id;
  224. $data->ltiid = $this->get_new_parentid('lti');
  225. $data->datesubmitted = $this->apply_date_offset($data->datesubmitted);
  226. $data->dateupdated = $this->apply_date_offset($data->dateupdated);
  227. if ($data->userid > 0) {
  228. $data->userid = $this->get_mappingid('user', $data->userid);
  229. }
  230. $newitemid = $DB->insert_record('lti_submission', $data);
  231. $this->set_mapping('ltisubmission', $oldid, $newitemid);
  232. }
  233. protected function after_execute() {
  234. // Add lti related files, no need to match by itemname (just internally handled context).
  235. $this->add_related_files('mod_lti', 'intro', null);
  236. }
  237. }