PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/db/upgrade.php

https://github.com/aolley/moodle-mod_dialogue
PHP | 194 lines | 148 code | 20 blank | 26 comment | 6 complexity | e460b8cdcc81ae5f96cf765d9a657e5b 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. function xmldb_dialogue_upgrade($oldversion=0) {
  17. global $CFG, $DB, $OUTPUT;
  18. $dbman = $DB->get_manager(); // loads ddl manager and xmldb classes
  19. if ($oldversion < 2010123102) {
  20. /// Define field introformat to be added to dialogue
  21. $table = new xmldb_table('dialogue');
  22. $field = new xmldb_field('introformat');
  23. $field->set_attributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'intro');
  24. /// Conditionally launch add field introformat
  25. if (!$dbman->field_exists($table,$field)) {
  26. $dbman->add_field($table, $field);
  27. }
  28. /// Add field format on table dialogue_entries
  29. $table = new xmldb_table('dialogue_entries');
  30. $field = new xmldb_field('format', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'text');
  31. /// Conditionally launch add field format
  32. if (!$dbman->field_exists($table,$field)) {
  33. $dbman->add_field($table, $field);
  34. }
  35. /// dialogue savepoint reached
  36. upgrade_mod_savepoint(true, 2010123102, 'dialogue');
  37. }
  38. if ($oldversion < 2010123103) {
  39. /////////////////////////////////////
  40. /// new file storage upgrade code ///
  41. /////////////////////////////////////
  42. $fs = get_file_storage();
  43. $siteid = get_site()->id;
  44. $base = preg_quote($CFG->wwwroot,"/");
  45. $empty = $DB->sql_empty(); // silly oracle empty string handling workaround
  46. $fs = get_file_storage();
  47. $empty = $DB->sql_empty(); // silly oracle empty string handling workaround
  48. $sqlfrom = "FROM {dialogue_entries} e
  49. JOIN {dialogue_conversations} c ON c.id = e.conversationid
  50. JOIN {dialogue} d ON d.id = c.dialogueid
  51. JOIN {modules} m ON m.name = 'dialogue'
  52. JOIN {course_modules} cm ON (cm.module = m.id AND cm.instance = d.id) ";
  53. // WHERE e.attachment <> '$empty' AND e.attachment <> '1'";
  54. $count = $DB->count_records_sql("SELECT COUNT('x') $sqlfrom");
  55. $rs = $DB->get_recordset_sql("SELECT e.id, e.text, e.attachment, e.userid, e.recipientid, c.dialogueid, d.course, cm.id AS cmid $sqlfrom ORDER BY d.course, d.id, c.id");
  56. if ($rs->valid()) {
  57. $pbar = new progress_bar('migratedialoguefiles', 500, true);
  58. $i = 0;
  59. foreach ($rs as $entry) {
  60. $i++;
  61. upgrade_set_timeout(60); // set up timeout, may also abort execution
  62. $pbar->update($i, $count, "Migrating dialogue entries - $i/$count.");
  63. /// Migrate embedded message images
  64. $context = get_context_instance(CONTEXT_COURSE, $entry->course);
  65. $modcontext = get_context_instance(CONTEXT_MODULE, $entry->cmid);
  66. $filerecord = array('contextid'=>$modcontext->id, 'component'=>'mod_dialogue', 'filearea'=>'entry', 'itemid'=>$entry->id);
  67. $search="|$CFG->wwwroot/file.php(\?file=)?/$entry->course(/[^\s'\"&\?#]+)|";
  68. if (preg_match_all($search, $entry->text, $matches)) {
  69. $text = $entry->text;
  70. foreach ($matches[2] as $i=>$imagepath) {
  71. $path = "/$context->id/course/legacy/0" . $imagepath;
  72. if ($file = $fs->get_file_by_hash(sha1($path))) {
  73. try {
  74. $fs->create_file_from_storedfile($filerecord, $file);
  75. $text = str_replace($matches[0][$i], '@@PLUGINFILE@@'.$imagepath , $text);
  76. } catch (Exception $e) {
  77. }
  78. }
  79. }
  80. $DB->set_field('dialogue_entries', 'text', $content, array('id'=> $entry->id));
  81. }
  82. if ($entry->attachment) {
  83. $filepath = "$CFG->dataroot/$entry->course/$CFG->moddata/dialogue/$entry->dialogueid/$entry->id/$entry->attachment";
  84. if (!is_readable($filepath)) {
  85. //file missing??
  86. echo $OUTPUT->notification("File not readable, skipping: ".$filepath);
  87. $entry->attachment = '';
  88. $DB->update_record('dialogue_entries', $entry);
  89. continue;
  90. }
  91. $context = get_context_instance(CONTEXT_MODULE, $entry->cmid);
  92. $filename = clean_param($entry->attachment, PARAM_FILE);
  93. if ($filename === '') {
  94. echo $OUTPUT->notification("Unsupported entry filename, skipping: ".$filepath);
  95. $entry->attachment = '';
  96. $DB->update_record('dialogue_entries', $entry);
  97. continue;
  98. }
  99. if (!$fs->file_exists($context->id, 'mod_dialogue', 'attachment', $entry->id, '/', $filename)) {
  100. $file_record = array('contextid'=>$context->id, 'component'=>'mod_dialogue', 'filearea'=>'attachment', 'itemid'=>$entry->id, 'filepath'=>'/', 'filename'=>$filename, 'userid'=>$entry->userid);
  101. if ($fs->create_file_from_pathname($file_record, $filepath)) {
  102. $entry->attachment = '1';
  103. $DB->update_record('dialogue_entries', $entry);
  104. unlink($filepath);
  105. }
  106. }
  107. // remove dirs if empty
  108. @rmdir("$CFG->dataroot/$entry->course/$CFG->moddata/dialogue/$entry->dialogueid/$entry->id");
  109. @rmdir("$CFG->dataroot/$entry->course/$CFG->moddata/dialogue/$entry->dialogueid");
  110. @rmdir("$CFG->dataroot/$entry->course/$CFG->moddata/dialogue");
  111. }
  112. }
  113. }
  114. $rs->close();
  115. upgrade_mod_savepoint(true, 2010123103, 'dialogue');
  116. }
  117. if ($oldversion < 2010123104) {
  118. /// Add field format on table dialogue_entries
  119. $table = new xmldb_table('dialogue_entries');
  120. $field = new xmldb_field('format', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'text');
  121. /// Conditionally launch add field format
  122. if (!$dbman->field_exists($table,$field)) {
  123. $dbman->add_field($table, $field);
  124. }
  125. /// dialogue savepoint reached
  126. upgrade_mod_savepoint(true, 2010123104, 'dialogue');
  127. }
  128. if ($oldversion < 2010123105) {
  129. /// Define field trust to be added to dialogue_entries
  130. $table = new xmldb_table('dialogue_entries');
  131. $field = new xmldb_field('trust', XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'format');
  132. /// Conditionally launch add field trust
  133. if (!$dbman->field_exists($table, $field)) {
  134. $dbman->add_field($table, $field);
  135. }
  136. /// dialogue savepoint reached
  137. upgrade_mod_savepoint(true, 2010123105, 'dialogue');
  138. }
  139. if ($oldversion < 2010123106) {
  140. $trustmark = '#####TRUSTTEXT#####';
  141. $rs = $DB->get_recordset_sql("SELECT * FROM {dialogue_entries} WHERE text LIKE ?", array($trustmark.'%'));
  142. foreach ($rs as $entry) {
  143. if (strpos($entry->text, $trustmark) !== 0) {
  144. // probably lowercase in some DBs
  145. continue;
  146. }
  147. $entry->text = str_replace($trustmark, '', $entry->text);
  148. $entry->trust = 1;
  149. $DB->update_record('dialogue_entries', $entry);
  150. }
  151. $rs->close();
  152. /// dialogue savepoint reached
  153. upgrade_mod_savepoint(true, 2010123106, 'dialogue');
  154. }
  155. return true;
  156. }