[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/mod/choice/ -> backuplib.php (source)

   1  <?php //$Id: backuplib.php,v 1.11 2006/02/08 23:46:21 danmarsden Exp $
   2      //This php script contains all the stuff to backup/restore
   3      //choice mods
   4  
   5      //This is the "graphical" structure of the choice mod:
   6      //
   7      //                      choice                                      
   8      //                    (CL,pk->id)----------|
   9      //                        |                |
  10      //                        |                |
  11      //                        |                |
  12      //                  choice_options         |
  13      //             (UL,pk->id, fk->choiceid)   |  
  14      //                        |                |
  15      //                        |                |
  16      //                        |                |
  17      //                   choice_answers        |
  18      //        (UL,pk->id, fk->choiceid, fk->optionid)     
  19      //
  20      // Meaning: pk->primary key field of the table
  21      //          fk->foreign key to link with parent
  22      //          nt->nested field (recursive data)
  23      //          CL->course level info
  24      //          UL->user level info
  25      //          files->table may have files)
  26      //
  27      //-----------------------------------------------------------
  28  
  29      function choice_backup_mods($bf,$preferences) {
  30          
  31          global $CFG;
  32  
  33          $status = true;
  34  
  35          //Iterate over choice table
  36          $choices = get_records ("choice","course",$preferences->backup_course,"id");
  37          if ($choices) {
  38              foreach ($choices as $choice) {
  39                  if (backup_mod_selected($preferences,'choice',$choice->id)) {
  40                      $status = choice_backup_one_mod($bf,$preferences,$choice);
  41                  }
  42              }
  43          }
  44          return $status;
  45      }
  46  
  47      function choice_backup_one_mod($bf,$preferences,$choice) {
  48  
  49          global $CFG;
  50      
  51          if (is_numeric($choice)) {
  52              $choice = get_record('choice','id',$choice);
  53          }
  54      
  55          $status = true;
  56  
  57          //Start mod
  58          fwrite ($bf,start_tag("MOD",3,true));
  59          //Print choice data
  60          fwrite ($bf,full_tag("ID",4,false,$choice->id));
  61          fwrite ($bf,full_tag("MODTYPE",4,false,"choice"));
  62          fwrite ($bf,full_tag("NAME",4,false,$choice->name));
  63          fwrite ($bf,full_tag("TEXT",4,false,$choice->text));
  64          fwrite ($bf,full_tag("FORMAT",4,false,$choice->format));
  65          fwrite ($bf,full_tag("PUBLISH",4,false,$choice->publish));
  66          fwrite ($bf,full_tag("SHOWRESULTS",4,false,$choice->showresults));
  67          fwrite ($bf,full_tag("DISPLAY",4,false,$choice->display));
  68          fwrite ($bf,full_tag("ALLOWUPDATE",4,false,$choice->allowupdate));
  69          fwrite ($bf,full_tag("SHOWUNANSWERED",4,false,$choice->showunanswered));
  70          fwrite ($bf,full_tag("LIMITANSWERS",4,false,$choice->limitanswers));
  71          fwrite ($bf,full_tag("TIMEOPEN",4,false,$choice->timeopen));
  72          fwrite ($bf,full_tag("TIMECLOSE",4,false,$choice->timeclose));
  73          fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$choice->timemodified));
  74  
  75          //Now backup choice_options
  76          $status = backup_choice_options($bf,$preferences,$choice->id);
  77  
  78          //if we've selected to backup users info, then execute backup_choice_answers
  79          if (backup_userdata_selected($preferences,'choice',$choice->id)) {
  80              $status = backup_choice_answers($bf,$preferences,$choice->id);
  81          }
  82          //End mod
  83          $status =fwrite ($bf,end_tag("MOD",3,true));
  84  
  85          return $status;
  86      }
  87  
  88      //Backup choice_answers contents (executed from choice_backup_mods)
  89      function backup_choice_answers ($bf,$preferences,$choice) {
  90  
  91          global $CFG;
  92  
  93          $status = true;
  94  
  95          $choice_answers = get_records("choice_answers","choiceid",$choice,"id");
  96          //If there is answers
  97          if ($choice_answers) {
  98              //Write start tag
  99              $status =fwrite ($bf,start_tag("ANSWERS",4,true));
 100              //Iterate over each answer
 101              foreach ($choice_answers as $cho_ans) {
 102                  //Start answer
 103                  $status =fwrite ($bf,start_tag("ANSWER",5,true));
 104                  //Print answer contents
 105                  fwrite ($bf,full_tag("ID",6,false,$cho_ans->id));
 106                  fwrite ($bf,full_tag("USERID",6,false,$cho_ans->userid));
 107                  fwrite ($bf,full_tag("OPTIONID",6,false,$cho_ans->optionid));
 108                  fwrite ($bf,full_tag("TIMEMODIFIED",6,false,$cho_ans->timemodified));
 109                  //End answer
 110                  $status =fwrite ($bf,end_tag("ANSWER",5,true));
 111              }
 112              //Write end tag
 113              $status =fwrite ($bf,end_tag("ANSWERS",4,true));
 114          }
 115          return $status;
 116      }
 117  
 118  
 119      //backup choice_options contents (executed from choice_backup_mods)
 120      function backup_choice_options ($bf,$preferences,$choice) {
 121  
 122          global $CFG;
 123  
 124          $status = true;
 125          
 126          $choice_options = get_records("choice_options","choiceid",$choice,"id");
 127          //If there is options
 128          if ($choice_options) {            
 129              //Write start tag
 130              $status =fwrite ($bf,start_tag("OPTIONS",4,true));
 131              //Iterate over each answer
 132              foreach ($choice_options as $cho_opt) {
 133                  //Start option
 134                  $status =fwrite ($bf,start_tag("OPTION",5,true));
 135                  //Print option contents
 136                  fwrite ($bf,full_tag("ID",6,false,$cho_opt->id));
 137                  fwrite ($bf,full_tag("TEXT",6,false,$cho_opt->text));
 138                  fwrite ($bf,full_tag("MAXANSWERS",6,false,$cho_opt->maxanswers));
 139                  fwrite ($bf,full_tag("TIMEMODIFIED",6,false,$cho_opt->timemodified));
 140                  //End answer
 141                  $status =fwrite ($bf,end_tag("OPTION",5,true));
 142              }
 143              //Write end tag
 144              $status =fwrite ($bf,end_tag("OPTIONS",4,true));
 145          }
 146          return $status;
 147      }
 148     
 149     ////Return an array of info (name,value)
 150     function choice_check_backup_mods($course,$user_data=false,$backup_unique_code,$instances=null) {
 151  
 152          if (!empty($instances) && is_array($instances) && count($instances)) {
 153              $info = array();
 154              foreach ($instances as $id => $instance) {
 155                  $info += choice_check_backup_mods_instances($instance,$backup_unique_code);
 156              }
 157              return $info;
 158          }
 159          //First the course data
 160          $info[0][0] = get_string("modulenameplural","choice");
 161          if ($ids = choice_ids ($course)) {
 162              $info[0][1] = count($ids);
 163          } else {
 164              $info[0][1] = 0;
 165          }
 166  
 167          //Now, if requested, the user_data
 168          if ($user_data) {
 169              $info[1][0] = get_string("responses","choice");
 170              if ($ids = choice_answer_ids_by_course ($course)) {
 171                  $info[1][1] = count($ids);
 172              } else {
 173                  $info[1][1] = 0;
 174              }
 175          }
 176          return $info;
 177      }
 178  
 179     ////Return an array of info (name,value)
 180     function choice_check_backup_mods_instances($instance,$backup_unique_code) {
 181          //First the course data
 182          $info[$instance->id.'0'][0] = '<b>'.$instance->name.'</b>';
 183          $info[$instance->id.'0'][1] = '';
 184  
 185          //Now, if requested, the user_data
 186          if (!empty($instance->userdata)) {
 187              $info[$instance->id.'1'][0] = get_string("responses","choice");
 188              if ($ids = choice_answer_ids_by_instance ($instance->id)) {
 189                  $info[$instance->id.'1'][1] = count($ids);
 190              } else {
 191                  $info[$instance->id.'1'][1] = 0;
 192              }
 193          }
 194          return $info;
 195      }
 196  
 197      //Return a content encoded to support interactivities linking. Every module
 198      //should have its own. They are called automatically from the backup procedure.
 199      function choice_encode_content_links ($content,$preferences) {
 200  
 201          global $CFG;
 202  
 203          $base = preg_quote($CFG->wwwroot,"/");
 204  
 205          //Link to the list of choices
 206          $buscar="/(".$base."\/mod\/choice\/index.php\?id\=)([0-9]+)/";
 207          $result= preg_replace($buscar,'$@CHOICEINDEX*$2@$',$content);
 208  
 209          //Link to choice view by moduleid
 210          $buscar="/(".$base."\/mod\/choice\/view.php\?id\=)([0-9]+)/";
 211          $result= preg_replace($buscar,'$@CHOICEVIEWBYID*$2@$',$result);
 212  
 213          return $result;
 214      }
 215  
 216      // INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE
 217  
 218      //Returns an array of choices id
 219      function choice_ids ($course) {
 220  
 221          global $CFG;
 222  
 223          return get_records_sql ("SELECT a.id, a.course
 224                                   FROM {$CFG->prefix}choice a
 225                                   WHERE a.course = '$course'");
 226      }
 227     
 228      //Returns an array of choice_answers id
 229      function choice_answer_ids_by_course ($course) {
 230  
 231          global $CFG;
 232  
 233          return get_records_sql ("SELECT s.id , s.choiceid
 234                                   FROM {$CFG->prefix}choice_answers s,
 235                                        {$CFG->prefix}choice a
 236                                   WHERE a.course = '$course' AND
 237                                         s.choiceid = a.id");
 238      }
 239  
 240      //Returns an array of choice_answers id
 241      function choice_answer_ids_by_instance ($instanceid) {
 242  
 243          global $CFG;
 244  
 245          return get_records_sql ("SELECT s.id , s.choiceid
 246                                   FROM {$CFG->prefix}choice_answers s
 247                                   WHERE s.choiceid = $instanceid");
 248      }
 249  ?>


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7