restore_iassign_stepslib.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * Define all the restore steps that will be used by the restore_iassign_activity_task
  4. *
  5. * @author Patricia Alves Rodrigues
  6. * @author Leônidas O. Brandão
  7. * @version v 1.0 2012
  8. * @package mod_iassign_backup
  9. * @since 2012
  10. * @copyright iMatica (<a href="http://www.matematica.br">iMath</a>) - Computer Science Dep. of IME-USP (Brazil)
  11. *
  12. * <b>License</b>
  13. * - http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  14. *
  15. * @see restore_activity_structure_step
  16. */
  17. /**
  18. * Moodle core defines constant MOODLE_INTERNAL which shall be used to make sure that the script is included and not called directly.
  19. */
  20. defined('MOODLE_INTERNAL') || die();
  21. /**
  22. * Define the complete assignment structure for restore, with file and id annotations.
  23. * @see restore_activity_structure_step
  24. */
  25. class restore_iassign_activity_structure_step extends restore_activity_structure_step {
  26. /**
  27. * Define the structure of the restore workflow
  28. * @return void Adds support for the 'exercise' path that is common to all the activities.
  29. */
  30. protected function define_structure () {
  31. $paths = array();
  32. // To know if we are including userinfo
  33. $userinfo = $this->get_setting_value('userinfo');
  34. // Define each element separated
  35. $paths[] = new restore_path_element('iassign', '/activity/iassign');
  36. $paths[] = new restore_path_element('iassign_statement', '/activity/iassign/statements/statement');
  37. if($userinfo) {
  38. $iassign_submissions = new restore_path_element('iassign_submission', '/activity/iassign/statements/statement/iassign_submissions/iassign_submission');
  39. $paths[] = $iassign_submissions;
  40. $iassign_submission_comments = new restore_path_element('iassign_submission_comment', '/activity/iassign/statements/statement/iassign_submissions/iassign_submission/iassign_submission_comments/iassign_submission_comment');
  41. $paths[] = $iassign_submission_comments;
  42. }
  43. return $this->prepare_activity_structure($paths);
  44. }
  45. /**
  46. * Process an ia restore
  47. * @param object $data The data in object form
  48. */
  49. protected function process_iassign ($data) {
  50. global $DB;
  51. $data = (object) $data;
  52. $oldid = $data->id;
  53. $data->course = $this->get_courseid();
  54. $newitemid = $DB->insert_record('iassign', $data);
  55. $this->apply_activity_instance($newitemid);
  56. }
  57. /**
  58. * Process a iassign_statement restore.
  59. * @param object $data The data in object form
  60. */
  61. protected function process_iassign_statement ($data) {
  62. global $DB, $CFG;
  63. require_once($CFG->dirroot . '/mod/iassign/locallib.php');
  64. $data = (object) $data;
  65. $oldid = $data->id;
  66. $data->iassignid = $this->get_new_parentid('iassign');
  67. $newitemid = $DB->insert_record('iassign_statement', $data);
  68. $this->set_mapping('iassign_statement', $oldid, $newitemid, true); // Has related fileareas
  69. activity::add_calendar($newitemid);
  70. }
  71. /**
  72. * Process a iassign_submission restore.
  73. * @param object $data The data in object form
  74. */
  75. protected function process_iassign_submission ($data) {
  76. global $DB;
  77. $data = (object) $data;
  78. $oldid = $data->id;
  79. $data->iassign_statementid = $this->get_new_parentid('iassign_statement');
  80. $data->userid = $this->get_mappingid('user', $data->userid);
  81. $data->teacher = $this->get_mappingid('user', $data->teacher);
  82. $newitemid = $DB->insert_record('iassign_submission', $data);
  83. $this->set_mapping('iassign_submission', $oldid, $newitemid, true); // Has related fileareas
  84. }
  85. /**
  86. * Process a iassign_submission_comment restore.
  87. * @param object $data The data in object form
  88. */
  89. protected function process_iassign_submission_comment ($data) {
  90. global $DB;
  91. $data = (object) $data;
  92. $oldid = $data->id;
  93. $data->iassign_submissionid = $this->get_new_parentid('iassign_submission');
  94. $data->comment_authorid = $this->get_mappingid('user', $data->comment_authorid);
  95. $newitemid = $DB->insert_record('iassign_submission_comment', $data);
  96. $this->set_mapping('iassign_submission_comment', $oldid, $newitemid, true); // Has related fileareas
  97. }
  98. /**
  99. * Once the database tables have been fully restored, restore the files.
  100. */
  101. protected function after_execute () {
  102. global $CFG, $DB;
  103. $this->add_related_files('mod_iassign', 'exercise', null);
  104. $fs = get_file_storage();
  105. $iassigns = $DB->get_records('iassign', array('course' => $this->get_courseid()));
  106. foreach ($iassigns as $iassign) {
  107. $iassign_statements = $DB->get_records('iassign_statement', array('iassignid' => $iassign->id));
  108. foreach ($iassign_statements as $iassign_statement) {
  109. $files = $DB->get_records('files', array('component' => 'mod_iassign', 'filearea' => 'exercise', 'itemid' => $iassign_statement->file));
  110. if($files) {
  111. $filename = array();
  112. foreach ($files as $value) {
  113. if($value->filename != '.') {
  114. $filename = explode(".", $value->filename);
  115. }
  116. }
  117. $extension = "";
  118. if(count($filename) > 1)
  119. $extension = strtolower($filename[count($filename) - 1]);
  120. $iassign_ilms = $DB->get_records('iassign_ilm', array('parent' => 0, 'enable' => 1));
  121. foreach ($iassign_ilms as $iassign_ilm) {
  122. $extensions = explode(",", $iassign_ilm->extension);
  123. if(in_array($extension, $extensions))
  124. $iassign_statement->iassign_ilmid = $iassign_ilm->id;
  125. }
  126. $DB->update_record("iassign_statement", $iassign_statement);
  127. }
  128. }
  129. }
  130. }
  131. }