PageRenderTime 24ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/hotpot/backuplib.php

https://github.com/cwaclawik/moodle
PHP | 337 lines | 239 code | 10 blank | 88 comment | 35 complexity | 58ece45473fea5a0b8f57d89741c2cca MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1
  1. <?PHP //$Id$
  2. //This php script contains all the stuff to backup/restore
  3. //quiz mods
  4. //-----------------------------------------------------------
  5. // This is the "graphical" structure of the hotpot mod:
  6. //-----------------------------------------------------------
  7. //
  8. // hotpot
  9. // (CL, pk->id,
  10. // fk->course, files)
  11. // |
  12. // +--------------+---------------+
  13. // | |
  14. // hotpot_attempts hotpot_questions
  15. // (UL, pk->id, (UL, pk->id,
  16. // fk->hotpot) fk->hotpot, text)
  17. // | | |
  18. // +-------------------+----------+ |
  19. // | | |
  20. // hotpot_details hotpot_responses |
  21. // (UL, pk->id, (UL, pk->id, |
  22. // fk->attempt) fk->attempt, question, |
  23. // correct, wrong, ignored) |
  24. // | |
  25. // +-------+-------+
  26. // |
  27. // hotpot_strings
  28. // (UL, pk->id)
  29. //
  30. // Meaning: pk->primary key field of the table
  31. // fk->foreign key to link with parent
  32. // nt->nested field (recursive data)
  33. // CL->course level info
  34. // UL->user level info
  35. // files->table may have files
  36. //
  37. //-----------------------------------------------------------
  38. function hotpot_backup_mods($bf, $preferences) {
  39. global $DB;
  40. $status = true;
  41. //Iterate over hotpot table
  42. $hotpots = $DB->get_records ("hotpot", array("course"=>$preferences->backup_course), "id");
  43. if ($hotpots) {
  44. foreach ($hotpots as $hotpot) {
  45. if (function_exists('backup_mod_selected')) {
  46. // Moodle >= 1.6
  47. $backup_mod_selected = backup_mod_selected($preferences, 'hotpot', $hotpot->id);
  48. } else {
  49. // Moodle <= 1.5
  50. $backup_mod_selected = true;
  51. }
  52. if ($backup_mod_selected) {
  53. $status = hotpot_backup_one_mod($bf, $preferences, $hotpot->id);
  54. }
  55. }
  56. }
  57. return $status;
  58. }
  59. function hotpot_backup_one_mod($bf, $preferences, $instance=0) {
  60. // $bf : resource id for b(ackup) f(ile)
  61. // $preferences : object containing switches and settings for this backup
  62. $level = 3;
  63. $status = true;
  64. $table = 'hotpot';
  65. $select = "course=? AND id=?";
  66. $params = array($preferences->backup_course, $instance);
  67. $records_tag = '';
  68. $records_tags = array();
  69. $record_tag = 'MOD';
  70. $record_tags = array('MODTYPE'=>'hotpot');
  71. $excluded_tags = array();
  72. $more_backup = '';
  73. if (function_exists('backup_userdata_selected')) {
  74. // Moodle >= 1.6
  75. $backup_userdata_selected = backup_userdata_selected($preferences, 'hotpot', $instance);
  76. } else {
  77. // Moodle <= 1.5
  78. $backup_userdata_selected = $preferences->mods['hotpot']->userinfo;
  79. }
  80. if ($backup_userdata_selected) {
  81. $more_backup .= '$GLOBALS["hotpot_backup_string_ids"] = array();';
  82. $more_backup .= '$status = hotpot_backup_attempts($bf, $record, $level, $status);';
  83. $more_backup .= '$status = hotpot_backup_questions($bf, $record, $level, $status);';
  84. $more_backup .= '$status = hotpot_backup_strings($bf, $record, $level, $status);';
  85. $more_backup .= 'unset($GLOBALS["hotpot_backup_string_ids"]);'; // tidy up
  86. }
  87. return hotpot_backup_records(
  88. $bf, $status, $level,
  89. $table, $select, $params,
  90. $records_tag, $records_tags,
  91. $record_tag, $record_tags,
  92. $excluded_tags, $more_backup
  93. );
  94. }
  95. function hotpot_backup_attempts($bf, &$parent, $level, $status) {
  96. // $parent is a reference to a hotpot record
  97. $table = 'hotpot_attempts';
  98. $select = "hotpot=?";
  99. $params = array($parent->id);
  100. $records_tag = 'ATTEMPT_DATA';
  101. $records_tags = array();
  102. $record_tag = 'ATTEMPT';
  103. $record_tags = array();
  104. $more_backup = '';
  105. $more_backup .= 'hotpot_backup_details($bf, $record, $level, $status);';
  106. $more_backup .= 'hotpot_backup_responses($bf, $record, $level, $status);';
  107. $excluded_tags = array('hotpot');
  108. return hotpot_backup_records(
  109. $bf, $status, $level,
  110. $table, $select, $params,
  111. $records_tag, $records_tags,
  112. $record_tag, $record_tags,
  113. $excluded_tags, $more_backup
  114. );
  115. }
  116. function hotpot_backup_details($bf, &$parent, $level, $status) {
  117. // $parent is a reference to an attempt record
  118. $table = 'hotpot_details';
  119. $select = "attempt=?";
  120. $params = array($parent->id);
  121. $records_tag = '';
  122. $records_tags = array();
  123. $record_tag = '';
  124. $record_tags = array();
  125. $more_backup = '';
  126. $excluded_tags = array('id','attempt');
  127. return hotpot_backup_records(
  128. $bf, $status, $level,
  129. $table, $select, $params,
  130. $records_tag, $records_tags,
  131. $record_tag, $record_tags,
  132. $excluded_tags, $more_backup
  133. );
  134. }
  135. function hotpot_backup_responses($bf, &$parent, $level, $status) {
  136. // $parent is a reference to an attempt record
  137. $table = 'hotpot_responses';
  138. $select = "attempt=?";
  139. $params = array($parent->id);
  140. $records_tag = 'RESPONSE_DATA';
  141. $records_tags = array();
  142. $record_tag = 'RESPONSE';
  143. $record_tags = array();
  144. $more_backup = 'hotpot_backup_string_ids($record, array("correct","wrong","ignored"));';
  145. $excluded_tags = array('id','attempt');
  146. return hotpot_backup_records(
  147. $bf, $status, $level,
  148. $table, $select, $params,
  149. $records_tag, $records_tags,
  150. $record_tag, $record_tags,
  151. $excluded_tags, $more_backup
  152. );
  153. }
  154. function hotpot_backup_questions($bf, &$parent, $level, $status) {
  155. // $parent is a reference to an hotpot record
  156. $table = 'hotpot_questions';
  157. $select = "hotpot=?";
  158. $params = array($parent->id);
  159. $records_tag = 'QUESTION_DATA';
  160. $records_tags = array();
  161. $record_tag = 'QUESTION';
  162. $record_tags = array();
  163. $more_backup = 'hotpot_backup_string_ids($record, array("text"));';
  164. $excluded_tags = array('hotpot');
  165. return hotpot_backup_records(
  166. $bf, $status, $level,
  167. $table, $select,
  168. $records_tag, $records_tags,
  169. $record_tag, $record_tags,
  170. $excluded_tags, $more_backup
  171. );
  172. }
  173. function hotpot_backup_string_ids(&$record, $fields) {
  174. // as the questions and responses tables are backed up
  175. // this function is called to store the ids of strings.
  176. // The string ids are used later by "hotpot_backup_strings"
  177. // $GLOBALS['hotpot_backup_string_ids'] was initialized in "hotpot_backup_mods"
  178. // store the ids of strings used in this $record's $fields
  179. foreach ($fields as $field) {
  180. if (empty($record->$field)) {
  181. // do nothing
  182. } else {
  183. $value = $record->$field;
  184. $ids = explode(',', "$value");
  185. foreach ($ids as $id) {
  186. if (empty($id)) {
  187. // do nothing
  188. } else {
  189. $GLOBALS['hotpot_backup_string_ids'][$id] = true;
  190. }
  191. }
  192. }
  193. }
  194. }
  195. function hotpot_backup_strings($bf, $record, $level, $status) {
  196. // This functions backups the strings used
  197. // in the question and responses for a single hotpot activity
  198. // The ids of the strings were stored by "hotpot_backup_string_ids"
  199. // $GLOBALS['hotpot_backup_string_ids'] was initialized in "hotpot_backup_mods"
  200. // retrieve $ids of strings to be backed up
  201. $ids = array_keys($GLOBALS['hotpot_backup_string_ids']);
  202. if (empty($ids)) {
  203. // no strings to backup
  204. } else {
  205. sort($ids);
  206. $ids = implode(',', $ids);
  207. $table = 'hotpot_strings';
  208. $select = "id IN ($ids)";
  209. $params = array();
  210. $records_tag = 'STRING_DATA';
  211. $records_tags = array();
  212. $record_tag = 'STRING';
  213. $record_tags = array();
  214. $more_backup = '';
  215. $excluded_tags = array('');
  216. $status = hotpot_backup_records(
  217. $bf, $status, $level,
  218. $table, $select, $params,
  219. $records_tag, $records_tags,
  220. $record_tag, $record_tags,
  221. $excluded_tags, $more_backup
  222. );
  223. }
  224. return $status;
  225. }
  226. function hotpot_backup_records(&$bf, $status, $level, $table, $select, $params, $records_tag, $records_tags, $record_tag, $record_tags, $excluded_tags, $more_backup) {
  227. // general purpose backup function
  228. // $bf : resource id of backup file
  229. // $status : current status of backup (true or false)
  230. // $level : current depth level in the backup XML tree
  231. // $table : table from which records will be selected and backed up
  232. // $select : SQL selection string
  233. // $records_tag : optional XML tag which starts a group of records (and descends a level)
  234. // $records_tags : optional XML tags to be inserted at the start of a group of records
  235. // $record_tag : optional XML tag which starts a record (and descends a level)
  236. // $record_tags : optional XML tags to be inserted at the start of a record
  237. // $excluded_tags : fields which will NOT be backed up from the records
  238. // $more_backup : optional PHP code to be eval(uated) for each record
  239. // If any of the "fwrite" statements fail,
  240. // no further "fwrite"s will be attempted
  241. // and the function returns "false".
  242. // Otherwise, the function returns "true".
  243. global $DB;
  244. if ($status && ($records = $DB->get_records_select($table, $select, $params, 'id'))) {
  245. // start a group of records
  246. if ($records_tag) {
  247. $status = $status && fwrite($bf, start_tag($records_tag, $level, true));
  248. $level++;
  249. foreach ($records_tags as $tag) {
  250. $status = $status && fwrite($bf, full_tag($tag, $level, false, $value));
  251. }
  252. }
  253. foreach ($records as $record) {
  254. // start a single record
  255. if ($record_tag) {
  256. $status = $status && fwrite($bf, start_tag($record_tag, $level, true));
  257. $level++;
  258. foreach ($record_tags as $tag=>$value) {
  259. $status = $status && fwrite($bf, full_tag($tag, $level, false, $value));
  260. }
  261. }
  262. // backup fields in this record
  263. $tags = get_object_vars($record);
  264. foreach ($tags as $tag=>$value) {
  265. if (!is_numeric($tag) && !in_array($tag, $excluded_tags)) {
  266. $status = $status && fwrite($bf, full_tag($tag, $level, false, $value));
  267. }
  268. }
  269. // backup related records, if required
  270. if ($more_backup) {
  271. eval($more_backup);
  272. }
  273. // end a single record
  274. if ($record_tag) {
  275. $level--;
  276. $status = $status && fwrite($bf, end_tag($record_tag, $level, true));
  277. }
  278. }
  279. // end a group of records
  280. if ($records_tag) {
  281. $level--;
  282. $status = $status && fwrite($bf, end_tag($records_tag, $level, true));
  283. }
  284. }
  285. return $status;
  286. }
  287. ////Return an array of info (name, value)
  288. function hotpot_check_backup_mods($course, $user_data=false, $backup_unique_code, $instances=null) {
  289. global $CFG, $DB;
  290. $info = array();
  291. if (isset($instances) && is_array($instances) && count($instances)) {
  292. foreach ($instances as $id => $instance) {
  293. $info += hotpot_check_backup_mods_instances($instance,$backup_unique_code);
  294. }
  295. } else {
  296. // the course data
  297. $info[0][0] = get_string('modulenameplural','hotpot');
  298. $info[0][1] = $DB->count_records('hotpot', array('course'=>$course));
  299. // the user_data, if requested
  300. if ($user_data) {
  301. $table = "{hotpot} h, {hotpot_attempts} a";
  302. $select = "h.course = ? AND h.id = a.hotpot";
  303. $params = array($course);
  304. $info[1][0] = get_string('attempts', 'quiz');
  305. $info[1][1] = $DB->count_records_sql("SELECT COUNT(*) FROM $table WHERE $select", $params);
  306. }
  307. }
  308. return $info;
  309. }
  310. ////Return an array of info (name, value)
  311. function hotpot_check_backup_mods_instances($instance,$backup_unique_code) {
  312. global $CFG, $DB;
  313. $info = array();
  314. // the course data
  315. $info[$instance->id.'0'][0] = '<b>'.$instance->name.'</b>';
  316. $info[$instance->id.'0'][1] = '';
  317. // the user_data, if requested
  318. if (!empty($instance->userdata)) {
  319. $table = "{hotpot_attempts} a";
  320. $select = "a.hotpot = ?";
  321. $params = array($instance->id);
  322. $info[$instance->id.'1'][0] = get_string('attempts', 'quiz');
  323. $info[$instance->id.'1'][1] = $DB->count_records_sql("SELECT COUNT(*) FROM $table WHERE $select", $params);
  324. }
  325. return $info;
  326. }
  327. ?>