/mod/attforblock/db/upgrade.php

https://github.com/jarednipper/HSU-common-code · PHP · 221 lines · 142 code · 53 blank · 26 comment · 39 complexity · 61f5b61194e92f19fee625a045706ad2 MD5 · raw file

  1. <?php //$Id: upgrade.php,v 1.1.2.2 2009/02/23 19:22:42 dlnsk Exp $
  2. // This file keeps track of upgrades to
  3. // the forum module
  4. //
  5. // Sometimes, changes between versions involve
  6. // alterations to database structures and other
  7. // major things that may break installations.
  8. //
  9. // The upgrade function in this file will attempt
  10. // to perform all the necessary actions to upgrade
  11. // your older installtion to the current version.
  12. //
  13. // If there's something it cannot do itself, it
  14. // will tell you what you need to do.
  15. //
  16. // The commands in here will all be database-neutral,
  17. // using the functions defined in lib/ddllib.php
  18. function xmldb_attforblock_upgrade($oldversion=0) {
  19. global $CFG, $THEME, $db;
  20. $result = true;
  21. /// And upgrade begins here. For each one, you'll need one
  22. /// block of code similar to the next one. Please, delete
  23. /// this comment lines once this file start handling proper
  24. /// upgrade code.
  25. if ($result && $oldversion < 2008021904) { //New version in version.php
  26. global $USER;
  27. if ($sessions = get_records('attendance_sessions', 'takenby', 0)) {
  28. foreach ($sessions as $sess) {
  29. if (count_records('attendance_log', 'attsid', $sess->id) > 0) {
  30. $sess->takenby = $USER->id;
  31. $sess->timetaken = $sess->timemodified ? $sess->timemodified : time();
  32. $sess->description = addslashes($sess->description);
  33. $result = update_record('attendance_sessions', $sess) and $result;
  34. }
  35. }
  36. }
  37. }
  38. if ($oldversion < 2008102401 and $result) {
  39. $table = new XMLDBTable('attforblock');
  40. $field = new XMLDBField('grade');
  41. $field->setAttributes(XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, '100', 'name');
  42. $result = $result && add_field($table, $field);
  43. $table = new XMLDBTable('attendance_sessions');
  44. $field = new XMLDBField('courseid');
  45. $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'id');
  46. $result = $result && change_field_unsigned($table, $field);
  47. // $field = new XMLDBField('creator');
  48. // $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'courseid');
  49. // $result = $result && change_field_unsigned($table, $field);
  50. $field = new XMLDBField('sessdate');
  51. $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'creator');
  52. $result = $result && change_field_unsigned($table, $field);
  53. $field = new XMLDBField('duration');
  54. $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'sessdate');
  55. $result = $result && add_field($table, $field);
  56. $field = new XMLDBField('timetaken');
  57. $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'takenby');
  58. $result = $result && change_field_unsigned($table, $field);
  59. $result = $result && rename_field($table, $field, 'lasttaken');
  60. $field = new XMLDBField('takenby');
  61. $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'lasttaken');
  62. $result = $result && change_field_unsigned($table, $field);
  63. $result = $result && rename_field($table, $field, 'lasttakenby');
  64. $field = new XMLDBField('timemodified');
  65. $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, null, null, null, null, null, 'lasttaken');
  66. $result = $result && change_field_unsigned($table, $field);
  67. $table = new XMLDBTable('attendance_log');
  68. $field = new XMLDBField('attsid');
  69. $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'id');
  70. $result = $result && change_field_unsigned($table, $field);
  71. $field = new XMLDBField('studentid');
  72. $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'attsid');
  73. $result = $result && change_field_unsigned($table, $field);
  74. $field = new XMLDBField('statusid');
  75. $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'status');
  76. $result = $result && add_field($table, $field);
  77. $field = new XMLDBField('statusset');
  78. $field->setAttributes(XMLDB_TYPE_CHAR, '100', null, null, null, null, null, null, 'statusid');
  79. $result = $result && add_field($table, $field);
  80. $field = new XMLDBField('timetaken');
  81. $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'statusid');
  82. $result = $result && add_field($table, $field);
  83. $field = new XMLDBField('takenby');
  84. $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'timetaken');
  85. $result = $result && add_field($table, $field);
  86. //Indexes
  87. $index = new XMLDBIndex('statusid');
  88. $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('statusid'));
  89. $result = $result && add_index($table, $index);
  90. $index = new XMLDBIndex('attsid');
  91. $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('attsid'));
  92. $result = $result && drop_index($table, $index);
  93. $field = new XMLDBField('attsid'); //Rename field
  94. $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'id');
  95. $result = $result && rename_field($table, $field, 'sessionid');
  96. $index = new XMLDBIndex('sessionid');
  97. $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('sessionid'));
  98. $result = $result && add_index($table, $index);
  99. $table = new XMLDBTable('attendance_settings');
  100. $field = new XMLDBField('courseid');
  101. $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'id');
  102. $result = $result && change_field_unsigned($table, $field);
  103. $field = new XMLDBField('visible');
  104. $field->setAttributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '1', 'grade');
  105. $result = $result && add_field($table, $field);
  106. $field = new XMLDBField('deleted');
  107. $field->setAttributes(XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'visible');
  108. $result = $result && add_field($table, $field);
  109. //Indexes
  110. $index = new XMLDBIndex('visible');
  111. $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('visible'));
  112. $result = $result && add_index($table, $index);
  113. $index = new XMLDBIndex('deleted');
  114. $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('deleted'));
  115. $result = $result && add_index($table, $index);
  116. $result = $result && rename_table($table, 'attendance_statuses');
  117. }
  118. if ($oldversion < 2008102406 and $result) {
  119. if ($courses = get_records_sql("SELECT courseid FROM {$CFG->prefix}attendance_sessions GROUP BY courseid")) {
  120. foreach ($courses as $c) {
  121. //Adding own status for course (now it must have own)
  122. if (!count_records('attendance_statuses', 'courseid', $c->courseid)) {
  123. $statuses = get_records('attendance_statuses', 'courseid', 0);
  124. foreach($statuses as $stat) {
  125. $rec = $stat;
  126. $rec->courseid = $c->courseid;
  127. insert_record('attendance_statuses', $rec);
  128. }
  129. }
  130. $statuses = get_records('attendance_statuses', 'courseid', $c->courseid);
  131. $statlist = implode(',', array_keys($statuses));
  132. $sess = get_records_select_menu('attendance_sessions', "courseid = $c->courseid AND lasttakenby > 0");
  133. $sesslist = implode(',', array_keys($sess));
  134. foreach($statuses as $stat) {
  135. execute_sql("UPDATE {$CFG->prefix}attendance_log
  136. SET statusid = {$stat->id}, statusset = '$statlist'
  137. WHERE sessionid IN ($sesslist) AND status = '$stat->status'");
  138. }
  139. $sessions = get_records_list('attendance_sessions', 'id', $sesslist);
  140. foreach($sessions as $sess) {
  141. execute_sql("UPDATE {$CFG->prefix}attendance_log
  142. SET timetaken = {$sess->lasttaken},
  143. takenby = {$sess->lasttakenby}
  144. WHERE sessionid = {$sess->id}");
  145. }
  146. }
  147. }
  148. }
  149. if ($oldversion < 2008102409 and $result) {
  150. $table = new XMLDBTable('attendance_statuses');
  151. $field = new XMLDBField('status');
  152. $result = $result && drop_field($table, $field);
  153. $index = new XMLDBIndex('status');
  154. $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('status'));
  155. $result = $result && drop_index($table, $index);
  156. $table = new XMLDBTable('attendance_log');
  157. $field = new XMLDBField('status');
  158. $result = $result && drop_field($table, $field);
  159. $index = new XMLDBIndex('status');
  160. $index->setAttributes(XMLDB_INDEX_NOTUNIQUE, array('status'));
  161. $result = $result && drop_index($table, $index);
  162. $table = new XMLDBTable('attendance_sessions');
  163. $field = new XMLDBField('creator');
  164. $result = $result && drop_field($table, $field);
  165. }
  166. return $result;
  167. }
  168. ?>