PageRenderTime 60ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/mod/game/backuplib.php

https://github.com/galitush2005/RTL-BIDI-Hebrew-Moodle-Plugins
PHP | 275 lines | 244 code | 7 blank | 24 comment | 7 complexity | 7b47060f317567aab64f35d13b4e44fb MD5 | raw file
  1. <?php //$Id: backuplib.php,v 1.3 2008/10/11 19:12:35 bdaloukas Exp $
  2. //This php script contains all the stuff to backup games
  3. //This version uses only the table game
  4. //This is the "graphical" structure of the game mod:
  5. //To see, put your terminal to 160cc
  6. //
  7. // game
  8. // (CL,pk->id)
  9. // |
  10. // Meaning: pk->primary key field of the table
  11. // fk->foreign key to link with parent
  12. // nt->nested field (recursive data)
  13. // SL->site level info
  14. // CL->course level info
  15. // UL->user level info
  16. // files->table may have files
  17. //
  18. //-----------------------------------------------------------
  19. // When we backup a game we also need to backup the questions and possibly
  20. // the data about student interaction with the questions. The functions to do
  21. // that are included with the following library
  22. /*
  23. * Insert necessary category ids to backup_ids table. Called during backup_check.html
  24. */
  25. function game_insert_category_and_question_ids($course, $backup_unique_code, $instances = null) {
  26. global $CFG;
  27. // Insert categories used by games
  28. $sql = "SELECT DISTINCT g.questioncategoryid as id
  29. FROM {$CFG->prefix}game g
  30. WHERE g.course=$course";
  31. if( ($recs = get_records_sql( $sql)) == false){
  32. return true;
  33. }
  34. $status = true;
  35. $table = 'question_categories';
  36. foreach( $recs as $rec){
  37. if( $rec->id == 0){
  38. continue;
  39. }
  40. $select = "backup_code='$backup_unique_code' AND table_name='$table' AND old_id = '$rec->id'";
  41. if( ($found = get_record_select( 'backup_ids', $select)) != false){
  42. continue;
  43. }
  44. unset( $newrec);
  45. $newrec->backup_code = $backup_unique_code;
  46. $newrec->table_name = $table;
  47. $newrec->old_id = $rec->id;
  48. $newrec->info = '';
  49. if (!insert_record( 'backup_ids', $newrec)){
  50. print_object( $newrec);
  51. error("game_insert_category_and_question_ids: Can't insert to backup_ids");
  52. }
  53. }
  54. return $status;
  55. }
  56. function game_insert_glossaries_ids($course, $backup_unique_code, $instances = null) {
  57. global $CFG;
  58. // Insert categories used by games
  59. $status = execute_sql("INSERT INTO {$CFG->prefix}backup_ids
  60. (backup_code, table_name, old_id, info)
  61. SELECT DISTINCT $backup_unique_code, 'glossary', g.glossaryid, ''
  62. FROM {$CFG->prefix}game g
  63. WHERE g.course=$course", false);
  64. return $status;
  65. }
  66. //STEP 2. Backup games and associated structures
  67. // (course dependent)
  68. function game_backup_one_mod($bf,$preferences,$game) {
  69. global $CFG;
  70. $status = true;
  71. if (is_numeric($game)) {
  72. $game = get_record('game','id',$game);
  73. }
  74. //Start mod
  75. fwrite ($bf,start_tag("MOD",3,true));
  76. //Print game data
  77. $game->modtype = 'game';
  78. game_backup_record( $bf, $game, 4);
  79. $recs = get_records_select( 'game_snakes_database');
  80. game_backup_table( $bf, $recs, 'GAME_SNAKES_DATABASE',5, 'GAME_SNAKES_DATABASE_RECORD', 6);
  81. $recs = get_records_select( 'game_bookquiz_questions', "gameid=$game->id");
  82. game_backup_table( $bf, $recs, 'GAME_BOOKQUIZ_QUESTIONS',5, 'GAME_BOOKQUIZ_QUESTION', 6);
  83. $recs = get_records_select( 'game_grades', "gameid=$game->id");
  84. game_backup_table( $bf, $recs, 'GAME_GRADES',5, 'GAME_GRADE', 6);
  85. $sql = "SELECT DISTINCT g.questioncategoryid as id,qc.stamp FROM ".
  86. " {$CFG->prefix}game g,{$CFG->prefix}question_categories qc ".
  87. " WHERE g.questioncategoryid = qc.id";
  88. $recs = get_records_sql( $sql);
  89. game_backup_table( $bf, $recs, 'QUESTION_CATEGORIES',5, 'QUESTION_CATEGORY', 6);
  90. game_backup_attempts( $bf,$preferences,$game->id);
  91. //End mod
  92. $status = fwrite ($bf,end_tag("MOD",3,true));
  93. return $status;
  94. }
  95. function game_backup_table( $bf,$recs, $tags, $levels, $tag, $level)
  96. {
  97. $status = true;
  98. //If there are records
  99. if ($recs != false) {
  100. //Write start tag
  101. $status = fwrite ($bf,start_tag( $tags, $levels, true));
  102. //Iterate over each attempt
  103. foreach ($recs as $rec) {
  104. //Start game_snakes_database
  105. $status = fwrite ($bf, start_tag( $tag, $level, true));
  106. game_backup_record( $bf, $rec, $level);
  107. $status = fwrite ($bf, end_tag( $tag, $level, true));
  108. }
  109. $status = fwrite ($bf,end_tag( $tags, $levels, true));
  110. }
  111. return $status;
  112. }
  113. function game_backup_record( $bf, $rec, $level){
  114. foreach( $rec as $field => $value){
  115. fwrite ($bf,full_tag( strtoupper( $field), $level, false, $rec->$field));
  116. }
  117. }
  118. function game_backup_attempts( $bf,$preferences,$gameid)
  119. {
  120. $status = true;
  121. $attempts = get_records("game_attempts","gameid",$gameid,"id");
  122. //If there are attempts
  123. if ($attempts) {
  124. //Write start tag
  125. $status = fwrite ($bf,start_tag("GAME_ATTEMPTS",5,true));
  126. //Iterate over each attempt
  127. foreach ($attempts as $attempt) {
  128. //Start game_attempt
  129. $status = fwrite ($bf,start_tag("GAME_ATTEMPT",6,true));
  130. game_backup_record( $bf, $attempt, 7);
  131. ////////////game_queries
  132. $queries = get_records("game_queries","attemptid",$attempt->id,"id");
  133. if( $queries != false){
  134. //Write start tag
  135. $status = fwrite ($bf,start_tag("GAME_QUERIES",8,true));
  136. foreach ($queries as $query) {
  137. //Start game_query
  138. $status = fwrite ($bf,start_tag("GAME_QUERY",9,true));
  139. game_backup_record( $bf, $query, 10);
  140. $status = fwrite ($bf,end_tag("GAME_QUERY",9,true));
  141. }
  142. $status = fwrite ($bf,end_tag("GAME_QUERIES",8,true));
  143. }
  144. $names = array( 'game_hangman', 'game_cross', 'game_cryptex', 'game_millionaire', 'game_sudoku', 'game_snakes', 'game_hiddenpicture');
  145. game_backup_game( $bf, $attempt->id, $names);
  146. //End question instance
  147. $status = fwrite ($bf,end_tag("GAME_ATTEMPT",6,true));
  148. }
  149. //Write end tag
  150. $status = fwrite ($bf,end_tag("GAME_ATTEMPTS",5,true));
  151. }
  152. return $status;
  153. }
  154. function game_backup_game( $bf, $attemptid, $names)
  155. {
  156. foreach( $names as $name){
  157. if( ($rec = get_record_select( $name, "id=$attemptid")) == false){
  158. continue;
  159. }
  160. $uppername = strtoupper( $name);
  161. $status = fwrite ($bf,start_tag( $uppername,7,true));
  162. game_backup_record( $bf, $rec, 8);
  163. $status = fwrite ($bf,end_tag( $uppername,7,true));
  164. }
  165. }
  166. function game_backup_mods($bf,$preferences) {
  167. global $CFG;
  168. $status = true;
  169. //Iterate over game table
  170. $games = get_records ("game","course",$preferences->backup_course,"id");
  171. if ($games) {
  172. foreach ($games as $game) {
  173. if (backup_mod_selected($preferences,'game',$game->id)) {
  174. $status = game_backup_one_mod( $bf,$preferences,$game);
  175. }
  176. }
  177. }
  178. return $status;
  179. }
  180. ////Return an array of info (name,value)
  181. /// $instances is an array with key = instanceid, value = object (name,id,userdata)
  182. function game_check_backup_mods($course,$user_data= false,$backup_unique_code,$instances=null) {
  183. //this function selects all the questions / categories to be backed up.
  184. game_insert_category_and_question_ids($course, $backup_unique_code, $instances);
  185. game_insert_glossaries_ids($course, $backup_unique_code, $instances);
  186. if (!empty($instances) && is_array($instances) && count($instances)) {
  187. $info = array();
  188. foreach ($instances as $id => $instance) {
  189. $info += game_check_backup_mods_instances($instance,$backup_unique_code);
  190. }
  191. return $info;
  192. }
  193. //First the course data
  194. $info[0][0] = get_string("modulenameplural","game");
  195. if ($ids = game_ids ($course)) {
  196. $info[0][1] = count($ids);
  197. } else {
  198. $info[0][1] = 0;
  199. }
  200. return $info;
  201. }
  202. function game_check_backup_mods_instances($instance,$backup_unique_code) {
  203. // the keys in this array need to be unique as they get merged...
  204. $info[$instance->id.'0'][0] = '<b>'.$instance->name.'</b>';
  205. $info[$instance->id.'0'][1] = '';
  206. return $info;
  207. }
  208. // INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE
  209. //Returns an array of quiz id
  210. function game_ids ($course) {
  211. global $CFG;
  212. return get_records_sql ("SELECT a.id, a.course
  213. FROM {$CFG->prefix}game a
  214. WHERE a.course = '$course'");
  215. }
  216. ?>