restore_sloodle_activity_task.class.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. require_once($CFG->dirroot . '/mod/sloodle/backup/moodle2/restore_sloodle_stepslib.php');
  3. /**
  4. * sloodle restore task that provides all the settings and steps to perform one
  5. * complete restore of the activity
  6. */
  7. class restore_sloodle_activity_task extends restore_activity_task {
  8. /**
  9. * Define (add) particular settings this activity can have
  10. */
  11. protected function define_my_settings() {
  12. // No particular settings for this activity
  13. }
  14. /**
  15. * Define (add) particular steps this activity can have
  16. */
  17. protected function define_my_steps() {
  18. // Sloodle only has one structure step
  19. $this->add_step(new restore_sloodle_activity_structure_step('sloodle_structure', 'sloodle.xml'));
  20. }
  21. /*
  22. Hack alert, Edmund Edgar, 2012-06-23:
  23. Presenter URLs need to be mapped back to the new site and context.
  24. This is probably doable with these decode_rules, but I don't understand how they work
  25. ...so I'm using after_restore instead.
  26. If anyone understands this stuff, please go ahead and rewrite it to do it right.
  27. */
  28. /**
  29. * Define the contents in the activity that must be
  30. * processed by the link decoder
  31. */
  32. static public function define_decode_contents() {
  33. $contents = array();
  34. //$contents[] = new restore_decode_content('sloodle_presenter_entry', array('source'), 'sloodle_presenter_entry');
  35. return $contents;
  36. }
  37. /**
  38. * Define the decoding rules for links belonging
  39. * to the activity to be executed by the link decoder
  40. */
  41. static public function define_decode_rules() {
  42. $rules = array();
  43. /*
  44. $rules[] = new restore_decode_rule('CHOICEVIEWBYID', '/mod/choice/view.php?id=$1', 'course_module');
  45. $rules[] = new restore_decode_rule('CHOICEINDEX', '/mod/choice/index.php?id=$1', 'course');
  46. */
  47. return $rules;
  48. }
  49. /**
  50. * Define the restore log rules that will be applied
  51. * by the {@link restore_logs_processor} when restoring
  52. * choice logs. It must return one array
  53. * of {@link restore_log_rule} objects
  54. */
  55. static public function define_restore_log_rules() {
  56. $rules = array();
  57. /*
  58. $rules[] = new restore_log_rule('choice', 'add', 'view.php?id={course_module}', '{choice}');
  59. $rules[] = new restore_log_rule('choice', 'update', 'view.php?id={course_module}', '{choice}');
  60. $rules[] = new restore_log_rule('choice', 'view', 'view.php?id={course_module}', '{choice}');
  61. $rules[] = new restore_log_rule('choice', 'choose', 'view.php?id={course_module}', '{choice}');
  62. $rules[] = new restore_log_rule('choice', 'choose again', 'view.php?id={course_module}', '{choice}');
  63. $rules[] = new restore_log_rule('choice', 'report', 'report.php?id={course_module}', '{choice}');
  64. */
  65. return $rules;
  66. }
  67. /**
  68. * Define the restore log rules that will be applied
  69. * by the {@link restore_logs_processor} when restoring
  70. * course logs. It must return one array
  71. * of {@link restore_log_rule} objects
  72. *
  73. * Note this rules are applied when restoring course logs
  74. * by the restore final task, but are defined here at
  75. * activity level. All them are rules not linked to any module instance (cmid = 0)
  76. */
  77. static public function define_restore_log_rules_for_course() {
  78. $rules = array();
  79. /*
  80. // Fix old wrong uses (missing extension)
  81. $rules[] = new restore_log_rule('choice', 'view all', 'index?id={course}', null,
  82. null, null, 'index.php?id={course}');
  83. $rules[] = new restore_log_rule('choice', 'view all', 'index.php?id={course}', null);
  84. */
  85. return $rules;
  86. }
  87. public function after_restore() {
  88. // Get a list of inserted layout entries
  89. global $DB;
  90. global $CFG;
  91. // Layout entry config may refer to an external course module, eg chatroom.
  92. // If we've done a full course backup, we should be able to recover it.
  93. // If we can't recover it, we'll set it to 0/null, and hope the UI can deal with that sensibly.
  94. $layouts = $DB->get_records('sloodle_layout', array('controllerid' => $this->get_moduleid()));
  95. if (count($layouts)) {
  96. foreach($layouts as $layout) {
  97. $layout_entries = $DB->get_records('sloodle_layout_entry', array('layout' => $layout->id));
  98. if (count($layout_entries)) {
  99. foreach($layout_entries as $le) {
  100. $layout_entry_configs = $DB->get_records('sloodle_layout_entry_config', array('layout_entry' => $le->id, 'name'=>'sloodlemoduleid'));
  101. foreach($layout_entry_configs as $lec) {
  102. if ($mapcm = restore_structure_step::get_mapping('course_module', $lec->value)) {
  103. $lec->value = $mapcm->newitemid;
  104. $DB->update_record('sloodle_layout_entry_config', $lec);
  105. }
  106. }
  107. $layout_entry_configs = $DB->get_records('sloodle_layout_entry_config', array('layout_entry' => $le->id, 'name'=>'sloodlecurrencyid'));
  108. foreach($layout_entry_configs as $lec) {
  109. if ($mapcm = restore_structure_step::get_mapping('sloodle_currency_types', $lec->value)) {
  110. $lec->value = $mapcm->newitemid;
  111. $DB->update_record('sloodle_layout_entry_config', $lec);
  112. }
  113. }
  114. }
  115. }
  116. }
  117. }
  118. /*
  119. This should probably really be done with decode_rules - see hack alert above.
  120. */
  121. $moduleid = $this->get_moduleid();
  122. $sloodleid = $DB->get_field('course_modules', 'instance', array('id'=>$moduleid));
  123. $contextid = $this->get_contextid();
  124. $presenter_entries = $DB->get_records('sloodle_presenter_entry', array('sloodleid' => $sloodleid));
  125. if (count($presenter_entries) > 0) {
  126. foreach($presenter_entries as $pe) {
  127. $source = $pe->source;
  128. $base = preg_quote('$@SITEROOT@$', '#');
  129. $contextidplaceholder = preg_quote('$@CONTEXTID@$', '#');
  130. if (preg_match('#^'.$base.'(/pluginfile.php/)'.$contextidplaceholder.'(/mod_sloodle/presenter/.*)#', $source, $matches)) {
  131. $pe->source = $CFG->wwwroot.$matches[1].$contextid.$matches[2];
  132. $DB->update_record('sloodle_presenter_entry', $pe);
  133. }
  134. }
  135. }
  136. //$file_entries = $DB->get_records('mdl_files', array('contextid'=>$contextid, 'component'=>'mod_sloodle', 'filearea'=>'presenter'));
  137. }
  138. }