PageRenderTime 26ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/hotpot/backuplib.php

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