backup_iassign_stepslib.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /**
  3. * Define all the backup steps that will be used by the "backup_iassign_activity_task.class.php"
  4. * to make a copy of all iAssign activities (mod/iassign).
  5. *
  6. * After 2021 we stated to use the file associated to each activity with: {iassign_statement}.filesid={files}.id and {iassign_statement}.id={files}.itemid
  7. *
  8. * It builds the 'iassign.xml' with XML code to register block activities {iassign} and {iassign_statement} activities.
  9. *
  10. * From 2.0 version (2023/04/25) we use the trick to (artificially) register on {iassign_statement}
  11. * tags (<statement...>...</statement>) additional fields from {iassign_ilm}, with the name of
  12. * the used iLM (fields: name, version, type, extension, url). See 'define_structure()' with
  13. * these artificial field: 'ilm_name', 'version', 'type', 'extension', 'url'.
  14. *
  15. * These additional fields will be used by 'restore_iassign_stepslib.php' to search on the target
  16. * Moodle the better corresponding iLM.
  17. *
  18. * @author Patricia Alves Rodrigues
  19. * @author Leo^nidas de Oliveira Branda~o
  20. * @version v 2.0 2023 - new fields; comments on new association {iassign_statement}/{files} ; trick to recover iLM on target
  21. * @version v 1.0 2012
  22. * @package mod_iassign_backup
  23. * @since 2012
  24. * @cite iMath (http://www.matematica.br) - LInE (www.usp.br/line) - Computer Science Dep. of IME-USP (Brazil)
  25. *
  26. * <b>License</b>
  27. * - http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  28. *
  29. * @see backup_activity_structure_step
  30. */
  31. //DEBUG Initial
  32. //DEBUG To test 'backup_iassign_stepslib.php' alone through URL http://localhost/...(moodle).../mod/iassign/backup/moodle2/backup_iassign_stepslib.php //DDD
  33. //DEBUG Comment the line bellow and uncomment the next ones until //DEBUG final
  34. defined('MOODLE_INTERNAL') || die();
  35. //DDD define('SAW', 'dev_saw'); //DDD para Paprika3
  36. //DDD //
  37. define('SAW', 'saw'); //DDD para Paprika4
  38. /// Moodle core defines constant MOODLE_INTERNAL which shall be used to make sure that the script is included and not called directly.
  39. //DDD echo "backup_iassign_stepslib.php: inicio<br/>\n";
  40. //DDD require_once "/var/www/html/" . SAW . "/config.php"; //DDD
  41. //DDD require_once "/var/www/html/" . SAW . "/lib/filestorage/file_progress.php"; //DDD interface file_progress : no ./backup/util/plan/backup_structure_step.class.php
  42. //DDD require_once "/var/www/html/" . SAW . "/backup/util/interfaces/executable.class.php"; //DDD
  43. //DDD require_once "/var/www/html/" . SAW . "/backup/util/interfaces/loggable.class.php"; //DDD
  44. //DDD require_once "/var/www/html/" . SAW . "/backup/util/plan/base_step.class.php"; //DDD abstract class base_step implements executable, loggable
  45. //DDD require_once "/var/www/html/" . SAW . "/backup/util/plan/backup_step.class.php"; //DDD abstract class backup_step extends base_step
  46. //DDD require_once "/var/www/html/" . SAW . "/backup/util/plan/backup_execution_step.class.php"; //DDD abstract class backup_execution_step extends backup_step
  47. //DDD require_once "/var/www/html/" . SAW . "/backup/util/plan/backup_structure_step.class.php"; //DDD abstract class backup_structure_step extends backup_step
  48. //DDD require_once "/var/www/html/" . SAW . "/backup/moodle2/backup_stepslib.php"; //DDD // backup_activity_structure_step extends backup_execution_step
  49. //DDD require_once "/var/www/html/" . SAW . "/backup/util/structure/backup_nested_element.class.php"; //DDD
  50. //DDD require_once "/var/www/html/" . SAW . "/backup/moodle2/backup_stepslib.php"; //DDD
  51. //DEBUG final
  52. //DEBUG @see './mod/iassign/db/upgrade.php'
  53. //DEBUG @param: $filetype1 to special indentification (e.g. "student", "log", "sendemail")
  54. //DEBUG @return 1 in case of success; -1 in case do not overwrite
  55. function writeContentBIS ($filetype1, $pathbase, $outputFile, $msgToRegister) {
  56. global $CFG, $GROUP, $WRITEMSG, $OVERWRITE;
  57. $pathbase = $CFG->dirroot . "/mod/iassign/ilm_debug/"; // "/moodle/mod/iassign/ilm_debug/", $CFG->dirroot defined in "./lib/setup.php"
  58. $outputFile = $pathbase . $outputFile;
  59. if (!is_writable($pathbase)) { // TRUE se arquivo existe e pode ser escrito
  60. // Just ignore... You must change the write permission to 'www-data' on './mod/iassign/ilm_debug/'
  61. // print "Erro! Problema de acesso ao servidor! Por favor, avise ao administrador (<tt>$pathbase</tt> nao acessivel para escrita).<br/>"; // . $file_debug . "
  62. // exit(0);
  63. }
  64. // To write: verify if the file does not exists or have permission to overwrite
  65. if (is_file($outputFile)) { // already exist this file
  66. $fpointer = fopen($outputFile, "a"); // write - if executed, it clear the previou content at this file
  67. } // if (is_file($outputFile))
  68. else
  69. $fpointer = fopen($outputFile, "w"); // write - if executed, it clear the previou content at this file
  70. if (!$fpointer) {
  71. // $file_debug .= "Erro: nao foi possivel abrir o arquivo (" . $outputFile . ")!<br/>\n";
  72. // it was not possible to open the file '$completfilepath" . $file_name . "'!<br/>\n";
  73. // print "<br/>" . $file_debug . "<br/>\n";
  74. return 0;
  75. }
  76. else {
  77. fwrite($fpointer, $msgToRegister . "\n");
  78. fclose($fpointer);
  79. } // error
  80. return 1;
  81. } // function writeContentBIS($filetype1, $pathbase, $outputFile, $msgToRegister)
  82. // Define the complete choice structure for backup, with file and id annotations
  83. // @see backup_activity_structure_step
  84. class backup_iassign_activity_structure_step extends backup_activity_structure_step {
  85. // Considering MBZ backup file, this script will build each file ""iassign_x/iassign.xml" with the iAssign block, where "x" is the ={iassign}.id
  86. // The "iassign_x/iassign.xml" file will have several information under the tag "activity", e.g.
  87. // '<activity id="1301" moduleid="17037" modulename="iassign" contextid="42017">'
  88. // - id="1301" : is the {iassign}.id
  89. // - moduleid="17037" : is {course_modules}.id, which has the property {course_modules}.instance == {iassign}.id
  90. // - modulename="iassign": is {modules}.name == "iassign", that is {modules} with iAssign definition
  91. // - contextid="42017" : is {context}.id, which has the property {context}.id == {course_modules}.id
  92. // Inside the tag "iassign | statements | statement | iassign_submissions | iassign_submission" we have each student submission
  93. // with its contents under "answer" tag.
  94. // To create each item, it is necessary a "new backup_nested_element('name' [, array])"
  95. // For instance, to get {context}.id (AS contextid), {course_modules}.id, {course_modules}.instance, {context}.instanceid of one
  96. // particular iAssign block {iassign}.id such that {iassign}.id == {course_modules}.instance you must use (e.g. 17037):
  97. // SELECT {context}.id AS contextid, {course_modules}.id, {course_modules}.instance, {context}.instanceid FROM
  98. // {course_modules, {context WHERE {course_modules}.id = {context}.instanceid AND {course_modules}.course = 2 AND {course_modules}.id = 17037
  99. // Define the structure for the iassign activity
  100. // @return void Return the root element (choice), wrapped into standard activity structure
  101. protected function define_structure () { //DDD function define_structure ()
  102. // To know if we are including userinfo
  103. $userinfo = $this->get_setting_value('userinfo'); // ./backup/util/plan/base_step.class.php : $this->task->get_setting_value($name);
  104. // Each iAssign block of activities {iassign} : {course_modules}.instance={iassign}.id AND {course_modules}.course={iassign}.course
  105. // ./backup/util/structure/backup_nested_element.class.php : class backup_nested_element extends base_nested_element implements processable
  106. // Restored by "restore_iassign_stepslib.php!process_iassign($data)"
  107. $iassign = new backup_nested_element('iassign', array('id'), array('name',
  108. 'course', 'intro', 'introformat', 'activity_group', 'grade',
  109. 'timeavailable', 'timedue', 'preventlate', 'test', 'max_experiment'));
  110. // It creates the tag "<statements>...</statements>" to each {iassign} element
  111. $statements = new backup_nested_element('statements');
  112. // It creates the tag '<statement id="X">... </statement>' to each {iassign_statement} : {iassign_statement}.iassignid={iassign}.id
  113. // Restored by "restore_iassign_stepslib.php!process_iassign_statement($data)"
  114. // ATTENTION, we need additional fields from {iassign_ilm} 'ilm_name', 'version', 'type', 'extension' to restore each
  115. // {iassign_statement} with a correct field 'iassign_ilmid' (that depends on the iLM installed in the iAssign/Moodle destiny)
  116. $statement = new backup_nested_element('statement', array('id'), array('name',
  117. 'iassignid', 'type_iassign', 'proposition', 'author_name', 'author_modified_name',
  118. 'iassign_ilmid', 'file', 'grade', 'timecreated', 'timemodified', 'timeavailable',
  119. 'timedue', 'preventlate', 'test', 'special_param1', 'position', 'visible',
  120. 'max_experiment', 'dependency', 'automatic_evaluate', 'show_answer', 'store_all_submissions', 'filesid',
  121. 'ilm_name', 'version', 'type', 'extension', 'url'));
  122. // fields from {iassign_statement}
  123. $sql_fields_iassign_statement =
  124. " ias.id, ias.name, ias.iassignid, ias.type_iassign, ias.proposition, ias.author_name, ias.author_modified_name, " .
  125. "ias.iassign_ilmid, ias.file, ias.grade, ias.timecreated, ias.timemodified, ias.timeavailable, " .
  126. "ias.timedue, ias.preventlate, ias.test, ias.special_param1, ias.position, ias.visible, " .
  127. "ias.max_experiment, ias.dependency, ias.automatic_evaluate, ias.show_answer, ias.store_all_submissions, ias.filesid, ";
  128. // additional fields from {iassign_ilm}
  129. $sql_additional_fields_ilm = " ilm.name as ilm_name, ilm.version, ilm.type, ilm.extension, ilm.url ";
  130. // ./backup/util/plan/base_step.class.php : 102 : "return $this->task->get_setting_value($name);" / $this->task=./backup/util/plan/base_task.class.php:
  131. // ./backup/util/plan/base_task.class.php : 106 : public function get_setting_value($name): return $this->get_setting($name)->get_value(); / ./backup/moodle2/backup_activity_task.class.php!get_setting($name): return parent::get_setting($name)
  132. // ./backup/moodle2/backup_activity_task.class.php : 250 : public function get_setting($name): return parent::get_setting($name); / ./backup/util/plan/base_task.class.php
  133. // ./backup/util/plan/base_task.class.php : 97 : public function get_setting($name): return $this->plan->get_setting($name);
  134. // ./backup/util/plan/base_plan.class.php : 124 : public function get_setting($name): if (isset($this->settings[$name]))
  135. $iassign_submissions = new backup_nested_element('iassign_submissions');
  136. // Each student submission (to each {iassign_statement}) : {iassign_submission}.iassign_statementid = {iassign_statement}.id
  137. $iassign_submission = new backup_nested_element('iassign_submission', array('id'), array('iassign_statementid',
  138. 'userid', 'timecreated', 'timemodified', 'grade', 'teacher', 'answer', 'experiment', 'status', 'previous_grade'));
  139. //DEBUG foreach ($iassign_submission as $one_statement) $msg_ias .= " - " . $one_statement->id . "," . $one_statement->iassign_statementid .
  140. //DEBUG "," . $one_statement->userid . "," . $one_statement->timecreated . "\n";
  141. $iassign_submission_comments = new backup_nested_element('iassign_submission_comments');
  142. // Each comment associated with one {iassign_statement} of one student : {iassign_submission_comment}.iassign_submissionid = {iassign_submission}.id
  143. $iassign_submission_comment = new backup_nested_element('iassign_submission_comment', array('id'), array('iassign_submissionid',
  144. 'comment_authorid', 'timecreated', 'comment', 'return_status', 'receiver'));
  145. // Build the tree
  146. $iassign->add_child($statements);
  147. $statements->add_child($statement);
  148. $statement->add_child($iassign_submissions);
  149. $iassign_submissions->add_child($iassign_submission);
  150. $iassign_submission->add_child($iassign_submission_comments);
  151. $iassign_submission_comments->add_child($iassign_submission_comment);
  152. // ./backup/util/structure/backup_nested_element.class.php: Only elements having final elements can set source
  153. // Define sources: associate to iAssign main table {iassign} and use as foreign key the {course_modules}.instance
  154. // ./backup/backup.class.php: VAR_ACTIVITYID = -21 => $this->find_first_parent_by_name('id');
  155. // The "backup::VAR_ACTIVITYID" says to use the 'id' from {iassign} table as 'id' being backup
  156. $iassign->set_source_table('iassign', array('id' => backup::VAR_ACTIVITYID));
  157. // Select all {iassign_statement} activities with iassignid == {iassign}.id: all {iassign_statement}.* fields
  158. // Includes the additional {iassign_ilm} field in order to restore with the correct iLM in Moodle destination:
  159. // ilm_name, version, type, extension, url
  160. // @see ./backup/util/structure/backup_nested_element.class.php : public function set_source_sql($sql, $params)
  161. // $statement->set_source_sql('SELECT * FROM {iassign_statement} WHERE iassignid = ?', array(backup::VAR_PARENTID)); // The value of the first parent id found in the structure
  162. $sql_query = 'SELECT ' . $sql_fields_iassign_statement . $sql_additional_fields_ilm .
  163. ' FROM {iassign_statement} AS ias, {iassign_ilm} AS ilm' .
  164. ' WHERE ilm.id = ias.iassign_ilmid AND ias.iassignid = ?';
  165. $statement->set_source_sql($sql_query, array(backup::VAR_PARENTID)); // The value of the first parent id found in the structure
  166. if ($userinfo) {
  167. $iassign_submission->set_source_table('iassign_submission', array('iassign_statementid' => backup::VAR_PARENTID));
  168. $iassign_submission_comment->set_source_table('iassign_submission_comment', array('iassign_submissionid' => backup::VAR_PARENTID));
  169. }
  170. // Define id annotations: during processing, store them in the table {backup_ids_temp} (if existing...)
  171. $iassign_submission->annotate_ids('user', 'userid'); // {iassign_submission}.userid = {user}.id
  172. $iassign_submission->annotate_ids('user', 'teacher'); // {iassign_submission}.teacher = {user}.id
  173. $iassign_submission_comment->annotate_ids('user', 'comment_authorid'); // {iassign_submission_comment}.comment_authorid = {user}.id
  174. //DEBUG: See '/var/www/html/" . SAW . "/backup/util/structure/backup_nested_element.class.php ! public get_source_sql()"
  175. /*/D*/ $sql_query = $statement->get_source_sql(); //DDD $testa = new backup_iassign_activity_structure_step("",""); //DDD
  176. /*/D*/ $timeWrite = date('Y_m_d_H_i_s'); // Year_Month_Day_Hour_Minutes_Seconds
  177. /*/D*/ echo "backup_iassign_stepslib.php: writeContentBIS 'backup_1_" . $timeWrite . ".txt'<br/>\n";
  178. /*/D*/ $msgDstatement = "";
  179. /*/D*/ foreach ($statement as $itemS) { $msgDstatement .= "[";
  180. /*/D*/ foreach ($itemS as $item) $msgDstatement .= (string)$item . "; ";
  181. /*/D*/ $msgDstatement .= "]"; }
  182. /*/D*/ $msg = "backup_iassign_stepslib.php : backup::VAR_ACTIVITYID=" . backup::VAR_ACTIVITYID . ", set_source_sql(iassign_statement}\n" . $sql_query . "\n";
  183. /*/D*/ writeContentBIS("", "", "backup_1_" . $timeWrite . ".txt", $msg . $msgDstatement); //DDD
  184. // Define file annotations: to table {files} (associated with {iassign_statement})
  185. $iassign->annotate_files('mod_iassign', 'exercise', null); // This file area hasn't itemid
  186. return $this->prepare_activity_structure($iassign);
  187. } // protected function define_structure()
  188. } // class backup_iassign_activity_structure_step extends backup_activity_structure_step
  189. /* //DEBUG
  190. $testa = new backup_iassign_activity_structure_step("",""); //DDD
  191. $timeWrite = date('Y_m_d_H_i_s'); // Year_Month_Day_Hour_Minutes_Seconds
  192. $sql_query = "SELECT * FROM {iassign} WHERE id = 2";
  193. $iassign = $DB->get_records_sql($sql_query);
  194. $testa->define_structure(); // $iassign);
  195. $msg = "Teste";
  196. writeContentBIS("", "", "backup_" . $timeWrite . ".txt", $msg); //DDD
  197. */