PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/hotpot/db/upgrade.php

https://github.com/bobpuffer/1.9.12-LAE1.3
PHP | 328 lines | 284 code | 28 blank | 16 comment | 80 complexity | aee2e5c20a5f4f7d1d70955f1710bc59 MD5 | raw file
  1. <?php //$Id: upgrade.php 68 2009-07-31 18:23:01Z dlandau $
  2. // This file keeps track of upgrades to the hotpot module
  3. function xmldb_hotpot_upgrade($oldversion=0) {
  4. global $CFG, $THEME, $db;
  5. $result = true;
  6. //===== 1.9.0 upgrade line ======//
  7. if ($result && $oldversion < 2007101512) {
  8. // save and disable setting to display debugging messages
  9. $debug = $db->debug;
  10. $db->debug = false;
  11. notify('Fixing hotpot grades, this may take a while if there are many hotpots...', 'notifysuccess');
  12. hotpot_fix_grades();
  13. // restore $db->debug
  14. $db->debug = $debug;
  15. }
  16. // update hotpot grades from sites earlier than Moodle 1.9, 27th March 2008
  17. if ($result && $oldversion < 2007101513) {
  18. // ensure "hotpot_update_grades" function is available
  19. require_once $CFG->dirroot.'/mod/hotpot/lib.php';
  20. // save and disable setting to display debugging messages
  21. $debug = $db->debug;
  22. $db->debug = false;
  23. notify('Processing hotpot grades, this may take a while if there are many hotpots...', 'notifysuccess');
  24. hotpot_update_grades();
  25. // restore $db->debug
  26. $db->debug = $debug;
  27. }
  28. return $result;
  29. }
  30. function hotpot_fix_grades($print=true, $usehotpotname=1) {
  31. // if hotpot name and grade are different ...
  32. // $usehotpotname=0: set hotpot name equal to grade name
  33. // $usehotpotname=1: set grade name equal to hotpot name
  34. global $CFG, $db;
  35. require_once($CFG->dirroot.'/lib/gradelib.php');
  36. if (! $module = get_record('modules', 'name', 'hotpot')) {
  37. if ($print) {
  38. print_error('error_nohotpot', 'hotpot');
  39. } else {
  40. debugging(get_string('error_nohotpot', 'hotpot'), DEBUG_DEVELOPER);
  41. }
  42. }
  43. if (! $hotpots = get_records('hotpot')) {
  44. $hotpots = array();
  45. }
  46. if(! $gradeitems = get_records_select('grade_items', "itemtype='mod' AND itemmodule='hotpot'")) {
  47. $gradeitems = array();
  48. }
  49. $success = '<font color="green">OK</font>'."\n";
  50. $failure = '<font color="red">FAILED</font>'."\n";
  51. $not = '<font color="red">NOT</font>'."\n";
  52. $new = get_string('newvalue', 'hotpot');
  53. $old = get_string('oldvalue', 'hotpot');
  54. $hotpots_no_grade = array(); // hotpots without a grade item
  55. $hotpots_no_weighting = array(); // hotpots with zero grade limit/weighting
  56. $gradeitems_wrong_name = array(); // grade items that have a different name from their hotpot
  57. $gradeitems_no_hotpot = array(); // grade items without a hotpot
  58. $gradeitems_no_idnumber = array(); // grade items without an idnumber (= course_modules id)
  59. foreach (array_keys($gradeitems) as $id) {
  60. $hotpotid = $gradeitems[$id]->iteminstance;
  61. if (array_key_exists($hotpotid, $hotpots)) {
  62. $hotpots[$hotpotid]->gradeitem = &$gradeitems[$id];
  63. if (empty($gradeitems[$id]->idnumber)) {
  64. $gradeitems_no_idnumber[$id] = &$gradeitems[$id];
  65. }
  66. if ($gradeitems[$id]->itemname != $hotpots[$hotpotid]->name) {
  67. $gradeitems_wrong_name[$id] = &$gradeitems[$id];
  68. }
  69. } else {
  70. $gradeitems_no_hotpot[$id] = &$gradeitems[$id];
  71. }
  72. }
  73. foreach ($hotpots as $id=>$hotpot) {
  74. if ($hotpot->grade==0) {
  75. // no grade item required, because grade is always 0
  76. // transfer this hotpot to "no_weighting" array
  77. $hotpots_no_weighting[$id] = &$hotpots[$id];
  78. if (isset($hotpot->gradeitem)) {
  79. // grade item not required
  80. $gradeitemid = $hotpot->gradeitem->id;
  81. $gradeitems_no_hotpot[$gradeitemid] = &$gradeitems[$gradeitemid];
  82. unset($hotpots[$id]->gradeitem);
  83. }
  84. } else {
  85. if (empty($hotpot->gradeitem)) {
  86. // grade item required, but missing
  87. $hotpots_no_grade[$id] = &$hotpots[$id];
  88. }
  89. }
  90. }
  91. $output = '';
  92. $start_list = false;
  93. $count_idnumber_updated = 0;
  94. $count_idnumber_notupdated = 0;
  95. foreach ($gradeitems_no_idnumber as $id=>$gradeitem) {
  96. $idnumber = get_field('course_modules', 'idnumber', 'module', $module->id, 'instance', $gradeitem->iteminstance);
  97. if (! $idnumber) {
  98. unset($gradeitems_no_idnumber[$id]);
  99. continue;
  100. }
  101. if (! $start_list) {
  102. $start_list = true;
  103. if ($print) {
  104. print '<ul>'."\n";
  105. }
  106. }
  107. if ($print) {
  108. $a = 'grade_item(id='.$id.').idnumber: '.$new.'='.$idnumber;
  109. print '<li>'.get_string('updatinga', '', $a).' ... ';
  110. }
  111. if (set_field('grade_items', 'idnumber', addslashes($idnumber), 'id', $id)) {
  112. $count_idnumber_updated++;
  113. if ($print) {
  114. print $success;
  115. }
  116. } else {
  117. $count_idnumber_notupdated++;
  118. if ($print) {
  119. print $failure;
  120. }
  121. }
  122. if ($print) {
  123. print '</li>'."\n";
  124. }
  125. }
  126. if ($start_list) {
  127. if ($print) {
  128. print '</ul>'."\n";
  129. }
  130. }
  131. $start_list = false;
  132. $count_name_updated = 0;
  133. $count_name_notupdated = 0;
  134. foreach ($gradeitems_wrong_name as $id=>$gradeitem) {
  135. $gradename = $gradeitem->itemname;
  136. $hotpotid = $gradeitem->iteminstance;
  137. $hotpotname = $hotpots[$hotpotid]->name;
  138. if (! $start_list) {
  139. $start_list = true;
  140. if ($print) {
  141. print '<ul>'."\n";
  142. }
  143. }
  144. if ($usehotpotname) {
  145. if ($print) {
  146. $a = 'grade_item(id='.$id.').name: '.$old.'='.$gradename.' '.$new.'='.$hotpotname;
  147. print '<li>'.get_string('updatinga', '', $a).' ... ';
  148. }
  149. $set_field = set_field('grade_items', 'itemname', addslashes($hotpotname), 'id', $id);
  150. } else {
  151. if ($print) {
  152. $a = 'hotpot(id='.$hotpotid.').name: '.$old.'='.$hotpotname.' '.$new.'='.$gradename;
  153. print '<li>'.get_string('updatinga', '', $a).' ... ';
  154. }
  155. $set_field = set_field('hotpot', 'name', addslashes($gradename), 'id', $hotpotid);
  156. }
  157. if ($set_field) {
  158. $count_name_updated++;
  159. if ($print) {
  160. print $success;
  161. }
  162. } else {
  163. $count_name_notupdated++;
  164. if ($print) {
  165. print $failure;
  166. }
  167. }
  168. if ($print) {
  169. print '</li>'."\n";
  170. }
  171. }
  172. if ($start_list) {
  173. if ($print) {
  174. print '</ul>'."\n";
  175. }
  176. }
  177. $start_list = false;
  178. $count_deleted = 0;
  179. $count_notdeleted = 0;
  180. if ($ids = implode(',', array_keys($gradeitems_no_hotpot))) {
  181. $count = count($gradeitems_no_hotpot);
  182. if (! $start_list) {
  183. $start_list = true;
  184. if ($print) {
  185. print '<ul>'."\n";
  186. }
  187. }
  188. if ($print) {
  189. print '<li>deleting '.$count.' grade items with no hotpots ... ';
  190. }
  191. if (delete_records_select('grade_items', "id in ($ids)")) {
  192. $count_deleted = $count;
  193. if ($print) {
  194. print $success;
  195. }
  196. } else {
  197. $count_notdeleted = $count;
  198. if ($print) {
  199. print $failure;
  200. }
  201. }
  202. if ($print) {
  203. print '</li>'."\n";
  204. }
  205. }
  206. if ($start_list) {
  207. if ($print) {
  208. print '</ul>'."\n";
  209. }
  210. }
  211. $start_list = false;
  212. $count_added = 0;
  213. $count_notadded = 0;
  214. foreach ($hotpots_no_grade as $hotpotid=>$hotpot) {
  215. $params = array(
  216. 'itemname' => $hotpot->name
  217. );
  218. if ($coursemoduleid = get_field('course_modules', 'id', 'module', $module->id, 'instance', $hotpotid)) {
  219. $params['idnumber'] = $coursemoduleid;
  220. }
  221. if ($hotpot->grade>0) {
  222. $params['gradetype'] = GRADE_TYPE_VALUE;
  223. $params['grademax'] = $hotpot->grade/100;
  224. $params['grademin'] = 0;
  225. } else {
  226. // no grade item needed - shouldn't happen
  227. $params['gradetype'] = GRADE_TYPE_NONE;
  228. }
  229. if (! $start_list) {
  230. $start_list = true;
  231. if ($print) {
  232. print '<ul>'."\n";
  233. }
  234. }
  235. if ($print) {
  236. print '<li>adding grade item for hotpot (id='.$hotpot->id.' name='.$hotpot->name.') ... ';
  237. }
  238. if (grade_update('mod/hotpot', $hotpot->course, 'mod', 'hotpot', $hotpotid, 0, null, $params)==GRADE_UPDATE_OK) {
  239. $count_added++;
  240. if ($print) {
  241. print $success;
  242. }
  243. } else {
  244. $count_notadded++;
  245. if ($print) {
  246. print $failure;
  247. }
  248. }
  249. if ($print) {
  250. print '</li>'."\n";
  251. }
  252. }
  253. if ($start_list) {
  254. if ($print) {
  255. print '</ul>'."\n";
  256. }
  257. }
  258. if ($print) {
  259. print "<ul>\n";
  260. print " <li>".count($hotpots)." HotPots were found</li>\n";
  261. if ($count = count($hotpots_no_weighting)) {
  262. print " <li>$count hotpot(s) have zero grade limit</li>\n";
  263. }
  264. print " <li>".count($gradeitems)." grade items were found</li>\n";
  265. if ($count = count($gradeitems_no_idnumber)) {
  266. if ($count_idnumber_updated) {
  267. print " <li>$count_idnumber_updated / $count grade item idnumber(s) were successfully updated</li>\n";
  268. }
  269. if ($count_idnumber_notupdated) {
  270. print " <li>$count_idnumber_notupdated / $count grade item idnumber(s) could $not be updated !!</li>\n";
  271. }
  272. }
  273. if ($count = count($gradeitems_wrong_name)) {
  274. if ($count_name_updated) {
  275. print " <li>$count_name_updated / $count grade item name(s) were successfully updated</li>\n";
  276. }
  277. if ($count_name_notupdated) {
  278. print " <li>$count_name_notupdated / $count grade item name(s) could $not be updated !!</li>\n";
  279. }
  280. }
  281. if ($count = count($gradeitems_no_hotpot)) {
  282. if ($count_deleted) {
  283. print " <li>$count_deleted / $count grade item(s) were successfully deleted</li>\n";
  284. }
  285. if ($count_notdeleted) {
  286. print " <li>$count_notdeleted / $count grade item(s) could $not be deleted !!</li>\n";
  287. }
  288. }
  289. if ($count = count($hotpots_no_grade)) {
  290. if ($count_added) {
  291. print " <li>$count_added / $count grade item(s) were successfully added</li>\n";
  292. }
  293. if ($count_notadded) {
  294. print " <li>$count_notadded / $count grade item(s) could $not be added !!</li>\n";
  295. }
  296. }
  297. print "</ul>\n";
  298. }
  299. }
  300. ?>