restore_sloodle_stepslib.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. /**
  3. * Structure step to restore one choice activity
  4. */
  5. class restore_sloodle_activity_structure_step extends restore_activity_structure_step {
  6. protected function define_structure() {
  7. $paths = array();
  8. $userinfo = $this->get_setting_value('userinfo');
  9. $paths[] = new restore_path_element('sloodle', '/activity/sloodle');
  10. $paths[] = new restore_path_element('sloodle_currency_type', '/activity/sloodle/currency_types/currency_type');
  11. if ($userinfo) {
  12. $paths[] = new restore_path_element('sloodle_user', '/activity/sloodle/users/user');
  13. }
  14. $paths[] = new restore_path_element('sloodle_controller', '/activity/sloodle/controllers/controller');
  15. $paths[] = new restore_path_element('sloodle_tracker', '/activity/sloodle/trackers/tracker');
  16. $paths[] = new restore_path_element('sloodle_presenter', '/activity/sloodle/presenters/presenter');
  17. $paths[] = new restore_path_element('sloodle_presenter_entry', '/activity/sloodle/presenters/presenter/presenter_entries/presenter_entry');
  18. $paths[] = new restore_path_element('sloodle_distributor', '/activity/sloodle/distributors/distributor');
  19. $paths[] = new restore_path_element('sloodle_distributor_entry', '/activity/sloodle/distributor/distributors/distributor_entries/distributor_entry');
  20. $paths[] = new restore_path_element('sloodle_layout', '/activity/sloodle/controllers/controller/layouts/layout');
  21. $paths[] = new restore_path_element('sloodle_layout_entry', '/activity/sloodle/controllers/controller/layouts/layout/layout_entries/layout_entry');
  22. $paths[] = new restore_path_element('sloodle_layout_entry_config', '/activity/sloodle/controllers/controller/layouts/layout/layout_entries/layout_entry/layout_entry_configs/layout_entry_config');
  23. /*
  24. if ($userinfo) {
  25. $paths[] = new restore_path_element('choice_answer', '/activity/choice/answers/answer');
  26. }
  27. */
  28. // Return the paths wrapped into standard activity structure
  29. return $this->prepare_activity_structure($paths);
  30. }
  31. protected function process_sloodle($data) {
  32. global $DB;
  33. $data = (object)$data;
  34. $oldid = $data->id;
  35. $data->course = $this->get_courseid();
  36. $data->timecreated = $this->apply_date_offset($data->timecreated);
  37. $data->timemodified = $this->apply_date_offset($data->timemodified);
  38. // insert the choice record
  39. $newitemid = $DB->insert_record('sloodle', $data);
  40. // immediately after inserting "activity" record, call this
  41. $this->apply_activity_instance($newitemid);
  42. }
  43. protected function process_sloodle_controller($data) {
  44. global $DB;
  45. $data = (object)$data;
  46. $oldid = $data->id;
  47. $data->sloodleid = $this->get_new_parentid('sloodle');
  48. $newitemid = $DB->insert_record('sloodle_controller', $data);
  49. // immediately after inserting "activity" record, call this
  50. $this->set_mapping('sloodle_controller', $oldid, $newitemid);
  51. }
  52. protected function process_sloodle_tracker($data) {
  53. global $DB;
  54. $data = (object)$data;
  55. $oldid = $data->id;
  56. $data->sloodleid = $this->get_new_parentid('sloodle');
  57. $newitemid = $DB->insert_record('sloodle_tracker', $data);
  58. // immediately after inserting "activity" record, call this
  59. $this->set_mapping('sloodle_tracker', $oldid, $newitemid);
  60. }
  61. protected function process_sloodle_presenter($data) {
  62. global $DB;
  63. $data = (object)$data;
  64. $oldid = $data->id;
  65. $data->sloodleid = $this->get_new_parentid('sloodle');
  66. $newitemid = $DB->insert_record('sloodle_presenter', $data);
  67. // immediately after inserting "activity" record, call this
  68. $this->set_mapping('sloodle_presenter', $oldid, $newitemid);
  69. }
  70. protected function process_sloodle_presenter_entry($data) {
  71. global $DB;
  72. $data = (object)$data;
  73. $oldid = $data->id;
  74. $data->sloodleid = $this->get_new_parentid('sloodle');
  75. $newitemid = $DB->insert_record('sloodle_presenter_entry', $data);
  76. // immediately after inserting "activity" record, call this
  77. $this->set_mapping('sloodle_presenter_entry', $oldid, $newitemid);
  78. }
  79. protected function process_sloodle_distributor($data) {
  80. global $DB;
  81. $data = (object)$data;
  82. $oldid = $data->id;
  83. $data->sloodleid = $this->get_new_parentid('sloodle');
  84. $newitemid = $DB->insert_record('sloodle_distributor', $data);
  85. // immediately after inserting "activity" record, call this
  86. $this->set_mapping('sloodle_distributor', $oldid, $newitemid);
  87. }
  88. protected function process_sloodle_distributor_entry($data) {
  89. global $DB;
  90. $data = (object)$data;
  91. $oldid = $data->id;
  92. $data->sloodleid = $this->get_new_parentid('sloodle');
  93. $newitemid = $DB->insert_record('sloodle_distributor_entry', $data);
  94. // immediately after inserting "activity" record, call this
  95. $this->set_mapping('sloodle_distributor_entry', $oldid, $newitemid);
  96. }
  97. protected function process_sloodle_layout($data) {
  98. global $DB;
  99. $data = (object)$data;
  100. $oldid = $data->id;
  101. $data->course = $this->get_courseid();
  102. $data->controllerid = $this->task->get_moduleid();
  103. //$data->controllerid = $this->get_moduleid();
  104. //var_dump($this);
  105. //exit;
  106. //$data->controllerid = $this->get_new_parentid('sloodle'); // CHECK: Probably need to look for the course module ID instead
  107. $data->timeupdated = $this->apply_date_offset($data->timeupdated);
  108. $newitemid = $DB->insert_record('sloodle_layout', $data);
  109. $this->set_mapping('sloodle_layout', $oldid, $newitemid);
  110. }
  111. protected function process_sloodle_user($data) {
  112. global $DB;
  113. $data = (object)$data;
  114. $oldid = $data->id;
  115. $newitemid = 0;
  116. // If the avatar is already there, map the record ID to the existing user.
  117. if ( $existing = $DB->get_record('sloodle_users', array( 'uuid' => $data->uuid ) ) ) {
  118. $newitemid = $existing->id;
  119. } else {
  120. // If the Moodle user is already there, create an avatar for them.
  121. $data->userid = $this->get_mappingid('user', $data->userid);
  122. // Only import avatars if we know which user they are.
  123. if ($data->userid > 0) {
  124. $newitemid = $DB->insert_record('sloodle_users', $data);
  125. }
  126. }
  127. $this->set_mapping('sloodle_user', $oldid, $newitemid);
  128. }
  129. protected function process_sloodle_currency_type($data) {
  130. global $DB;
  131. $data = (object)$data;
  132. $oldid = $data->id;
  133. $newitemid = 0;
  134. // If the avatar is already there, map the record ID to the existing user.
  135. if ( $existing = $DB->get_record('sloodle_currency_types', array( 'name' => $data->name) ) ) {
  136. $newitemid = $existing->id;
  137. } else {
  138. $newitemid = $DB->insert_record('sloodle_currency_types', $data);
  139. }
  140. $this->set_mapping('sloodle_currency_types', $oldid, $newitemid);
  141. }
  142. protected function process_sloodle_layout_entry($data) {
  143. global $DB;
  144. $data = (object)$data;
  145. $oldid = $data->id;
  146. $data->layout = $this->get_new_parentid('sloodle_layout');
  147. $newitemid = $DB->insert_record('sloodle_layout_entry', $data);
  148. $this->set_mapping('sloodle_layout_entry', $oldid, $newitemid);
  149. }
  150. protected function process_sloodle_layout_entry_config($data) {
  151. global $DB;
  152. $data = (object)$data;
  153. $oldid = $data->id;
  154. $data->layout_entry = $this->get_new_parentid('sloodle_layout_entry');
  155. $newitemid = $DB->insert_record('sloodle_layout_entry_config', $data);
  156. $this->set_mapping('sloodle_layout_entry_config', $oldid, $newitemid);
  157. }
  158. protected function after_execute() {
  159. global $DB;
  160. // Add sloodle related files, no need to match by itemname (just internally handled context)
  161. $this->add_related_files('mod_sloodle', 'presenter', null);
  162. /*
  163. Presenter files have the context ID in the path.
  164. This needs to be replaced with the new context ID.
  165. We'll get the files directly from the files table and fix any context IDs.
  166. This is almost definitely the wrong way to do this, but I'm buggered if I know what the right one is.
  167. */
  168. $contextid = $this->task->get_contextid();
  169. $files = $DB->get_records('files', array('contextid'=>$contextid, 'component'=>'mod_sloodle', 'filearea'=>'presenter'));
  170. if (count($files) > 0) {
  171. foreach($files as $fr) {
  172. $fs = get_file_storage();
  173. if (!preg_match('#^(/)\d+(/.*)$#', $fr->filepath, $matches)) {
  174. continue;
  175. }
  176. $fr->filepath = $matches[1].$contextid.$matches[2];
  177. $fr->pathnamehash = $fs->get_pathname_hash($fr->contextid, $fr->component, $fr->filearea, $fr->itemid, $fr->filepath, $fr->filename);
  178. $DB->update_record('files', $fr);
  179. }
  180. }
  181. }
  182. }