[ Index ]

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

title

Body

[close]

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

   1  <?php //Id:$
   2  
   3  //This php script contains all the stuff to backup/restore data mod
   4  
   5      //This is the "graphical" structure of the data mod:
   6      //
   7      //                     data
   8      //                    (CL,pk->id)
   9      //                        |
  10      //                        |
  11      //                        |
  12      //      ---------------------------------------------------------------------------------
  13      //      |                                                                               |
  14      //data_records (UL,pk->id, fk->data)                                      data_fields (pk->id, fk->data)
  15      //               |                                                                      |
  16      //               |                                                                      |
  17      //     -----------------------------------------------------------------------------    |
  18      //     |                                  |                                        |    |
  19      //data_ratings(fk->recordid, pk->id) data_comments (fk->recordid, pk->id)          |    |
  20      //                                                                  data_content(pk->id, fk->recordid, fk->fieldid)
  21      //
  22      //
  23      //
  24      // Meaning: pk->primary key field of the table
  25      //          fk->foreign key to link with parent
  26      //          nt->nested field (recursive data)
  27      //          CL->course level info
  28      //          UL->user level info
  29      //          files->table may have files)
  30      //
  31      //-----------------------------------------------------------
  32  
  33      //Backup data files because we've selected to backup user info
  34      //and files are user info's level
  35  
  36  
  37      //Return a content encoded to support interactivities linking. Every module
  38  
  39  function data_backup_mods($bf,$preferences) {
  40      global $CFG;
  41  
  42      $status = true;
  43  
  44      // iterate
  45      if ($datas = get_records('data','course',$preferences->backup_course,"id")) {
  46          foreach ($datas as $data) {
  47             if (function_exists('backup_mod_selected')) {
  48                      // Moodle 1.6
  49                      $backup_mod_selected = backup_mod_selected($preferences, 'data', $data->id);
  50              } else {
  51                      // Moodle 1.5
  52                  $backup_mod_selected = true;
  53              }
  54              if ($backup_mod_selected) {
  55                  $status = data_backup_one_mod($bf,$preferences,$data);
  56                  // backup files happens in backup_one_mod now too.
  57              }
  58          }
  59      }
  60      return $status;
  61  }
  62  
  63  function data_backup_one_mod($bf,$preferences,$data) {
  64      global $CFG;
  65  
  66      if (is_numeric($data)) { // backwards compatibility
  67          $data = get_record('data','id',$data);
  68      }
  69      $instanceid = $data->id;
  70  
  71      $status = true;
  72  
  73  
  74      fwrite ($bf,start_tag("MOD",3,true));
  75      //Print data data
  76      fwrite ($bf,full_tag("ID",4,false,$data->id));
  77      fwrite ($bf,full_tag("MODTYPE",4,false,"data"));
  78      fwrite ($bf,full_tag("NAME",4,false,$data->name));
  79      fwrite ($bf,full_tag("INTRO",4,false,$data->intro));
  80      fwrite ($bf,full_tag("COMMENTS",4,false,$data->comments));
  81      fwrite ($bf,full_tag("TIMEAVAILABLEFROM",4,false,$data->timeavailablefrom));
  82      fwrite ($bf,full_tag("TIMEAVAILABLETO",4,false,$data->timeavailableto));
  83      fwrite ($bf,full_tag("TIMEVIEWFROM",4,false,$data->timeviewfrom));
  84      fwrite ($bf,full_tag("TIMEVIEWTO",4,false,$data->timeviewto));
  85      fwrite ($bf,full_tag("REQUIREDENTRIES",4,false,$data->requiredentries));
  86      fwrite ($bf,full_tag("REQUIREDENTRIESTOVIEW",4,false,$data->requiredentriestoview));
  87      fwrite ($bf,full_tag("MAXENTRIES",4,false,$data->maxentries));
  88      fwrite ($bf,full_tag("RSSARTICLES",4,false,$data->rssarticles));
  89      fwrite ($bf,full_tag("SINGLETEMPLATE",4,false,$data->singletemplate));
  90      fwrite ($bf,full_tag("LISTTEMPLATE",4,false,$data->listtemplate));
  91      fwrite ($bf,full_tag("LISTTEMPLATEHEADER",4,false,$data->listtemplateheader));
  92      fwrite ($bf,full_tag("LISTTEMPLATEFOOTER",4,false,$data->listtemplatefooter));
  93      fwrite ($bf,full_tag("ADDTEMPLATE",4,false,$data->addtemplate));
  94      fwrite ($bf,full_tag("RSSTEMPLATE",4,false,$data->rsstemplate));
  95      fwrite ($bf,full_tag("RSSTITLETEMPLATE",4,false,$data->rsstitletemplate));
  96      fwrite ($bf,full_tag("CSSTEMPLATE",4,false,$data->csstemplate));
  97      fwrite ($bf,full_tag("JSTEMPLATE",4,false,$data->jstemplate));
  98      fwrite ($bf,full_tag("APPROVAL",4,false,$data->approval));
  99      fwrite ($bf,full_tag("SCALE",4,false,$data->scale));
 100      fwrite ($bf,full_tag("ASSESSED",4,false,$data->assessed));
 101      fwrite ($bf,full_tag("DEFAULTSORT",4,false,$data->defaultsort));
 102      fwrite ($bf,full_tag("DEFAULTSORTDIR",4,false,$data->defaultsortdir));
 103      fwrite ($bf,full_tag("EDITANY",4,false,$data->editany));
 104      fwrite ($bf,full_tag("NOTIFICATION",4,false,$data->notification));
 105  
 106      // if we've selected to backup users info, then call any other functions we need
 107      // including backing up individual files
 108  
 109      $status = backup_data_fields($bf,$preferences,$data->id);
 110  
 111      if (backup_userdata_selected($preferences,'data',$data->id)) {
 112          //$status = backup_someuserdata_for_this_instance();
 113          //$status = backup_somefiles_for_this_instance();
 114          // ... etc
 115  
 116          $status = backup_data_records($bf,$preferences,$data->id);
 117          if ($status) {
 118              $status = backup_data_files_instance($bf,$preferences,$data->id);    //recursive copy
 119          }
 120      }
 121      fwrite ($bf,end_tag("MOD",3,true));
 122      return $status;
 123  
 124  }
 125  
 126  
 127  function backup_data_fields($bf,$preferences,$dataid){
 128      global $CFG;
 129      $status = true;
 130  
 131      $data_fields = get_records("data_fields","dataid",$dataid);
 132  
 133          //If there is submissions
 134          if ($data_fields) {
 135              //Write start tag
 136              $status =fwrite ($bf,start_tag("FIELDS",4,true));
 137              //Iterate over each submission
 138              foreach ($data_fields as $fie_sub) {
 139                  //Start submission
 140                  $status =fwrite ($bf,start_tag("FIELD",5,true));
 141                  //Print submission contents
 142                  fwrite ($bf,full_tag("ID",6,false,$fie_sub->id));
 143                  fwrite ($bf,full_tag("DATAID",6,false,$fie_sub->dataid));
 144                  fwrite ($bf,full_tag("TYPE",6,false,$fie_sub->type));
 145                  fwrite ($bf,full_tag("NAME",6,false,$fie_sub->name));
 146                  fwrite ($bf,full_tag("DESCRIPTION",6,false,$fie_sub->description));
 147                  fwrite ($bf,full_tag("PARAM1",6,false,$fie_sub->param1));
 148                  fwrite ($bf,full_tag("PARAM2",6,false,$fie_sub->param2));
 149                  fwrite ($bf,full_tag("PARAM3",6,false,$fie_sub->param3));
 150                  fwrite ($bf,full_tag("PARAM4",6,false,$fie_sub->param4));
 151                  fwrite ($bf,full_tag("PARAM5",6,false,$fie_sub->param5));
 152                  fwrite ($bf,full_tag("PARAM6",6,false,$fie_sub->param6));
 153                  fwrite ($bf,full_tag("PARAM7",6,false,$fie_sub->param7));
 154                  fwrite ($bf,full_tag("PARAM8",6,false,$fie_sub->param8));
 155                  fwrite ($bf,full_tag("PARAM9",6,false,$fie_sub->param9));
 156                  fwrite ($bf,full_tag("PARAM10",6,false,$fie_sub->param10));
 157  
 158                  //End submission
 159                  $status =fwrite ($bf,end_tag("FIELD",5,true));
 160              }
 161              //Write end tag
 162              $status =fwrite ($bf,end_tag("FIELDS",4,true));
 163          }
 164          return $status;
 165  }
 166  
 167  function backup_data_content($bf,$preferences,$recordid){
 168      global $CFG;
 169      $status = true;
 170  
 171      $data_contents = get_records("data_content","recordid",$recordid);
 172  
 173          //If there is submissions
 174          if ($data_contents) {
 175              //Write start tag
 176              $status =fwrite ($bf,start_tag("CONTENTS",6,true));
 177              //Iterate over each submission
 178              foreach ($data_contents as $cnt_sub) {
 179                  //Start submission
 180                  $status =fwrite ($bf,start_tag("CONTENT",7,true));
 181                  //Print submission contents
 182                  fwrite ($bf,full_tag("ID",8,false,$cnt_sub->id));
 183                  fwrite ($bf,full_tag("RECORDID",8,false,$cnt_sub->recordid));
 184                  fwrite ($bf,full_tag("FIELDID",8,false,$cnt_sub->fieldid));
 185                  fwrite ($bf,full_tag("CONTENT",8,false,$cnt_sub->content));
 186                  fwrite ($bf,full_tag("CONTENT1",8,false,$cnt_sub->content1));
 187                  fwrite ($bf,full_tag("CONTENT2",8,false,$cnt_sub->content2));
 188                  fwrite ($bf,full_tag("CONTENT3",8,false,$cnt_sub->content3));
 189                  fwrite ($bf,full_tag("CONTENT4",8,false,$cnt_sub->content4));
 190                  //End submission
 191                  $status =fwrite ($bf,end_tag("CONTENT",7,true));
 192              }
 193              //Write end tag
 194              $status =fwrite ($bf,end_tag("CONTENTS",6,true));
 195          }
 196          return $status;
 197  
 198  }
 199  function backup_data_ratings($bf,$preferences,$recordid){
 200      global $CFG;
 201      $status = true;
 202      $data_ratings = get_records("data_ratings","recordid",$recordid);
 203  
 204      //If there is submissions
 205      if ($data_ratings) {
 206          //Write start tag
 207          $status =fwrite ($bf,start_tag("RATINGS",6,true));
 208          //Iterate over each submission
 209          foreach ($data_ratings as $rat_sub) {
 210              //Start submission
 211              $status =fwrite ($bf,start_tag("RATING",7,true));
 212              //Print submission contents
 213              fwrite ($bf,full_tag("ID",8,false,$rat_sub->id));
 214              fwrite ($bf,full_tag("RECORDID",8,false,$rat_sub->recordid));
 215              fwrite ($bf,full_tag("USERID",8,false,$rat_sub->userid));
 216              fwrite ($bf,full_tag("RATING",8,false,$rat_sub->rating));
 217              //End submission
 218              $status =fwrite ($bf,end_tag("RATING",7,true));
 219          }
 220              //Write end tag
 221          $status =fwrite ($bf,end_tag("RATINGS",6,true));
 222  
 223      }
 224  
 225      return $status;
 226  }
 227  function backup_data_comments($bf,$preferences,$recordid){
 228      global $CFG;
 229      $status = true;
 230      $data_comments = get_records("data_comments","recordid",$recordid);
 231  
 232      //If there is submissions
 233      if ($data_comments) {
 234          //Write start tag
 235          $status =fwrite ($bf,start_tag("COMMENTS",6,true));
 236              //Iterate over each submission
 237          foreach ($data_comments as $com_sub) {
 238              //Start submission
 239              $status =fwrite ($bf,start_tag("COMMENT",7,true));
 240              //Print submission contents
 241              fwrite ($bf,full_tag("ID",8,false,$com_sub->id));
 242              fwrite ($bf,full_tag("RECORDID",8,false,$com_sub->recordid));
 243              fwrite ($bf,full_tag("USERID",8,false,$com_sub->userid));
 244              fwrite ($bf,full_tag("CONTENT",8,false,$com_sub->content));
 245              fwrite ($bf,full_tag("CREATED",8,false,$com_sub->created));
 246              fwrite ($bf,full_tag("MODIFIED",8,false,$com_sub->modified));
 247              //End submission
 248              $status =fwrite ($bf,end_tag("COMMENT",7,true));
 249          }
 250          //Write end tag
 251          $status =fwrite ($bf,end_tag("COMMENTS",6,true));
 252      }
 253      return $status;
 254  }
 255  
 256  
 257  function backup_data_files_instance($bf,$preferences,$instanceid) {
 258  
 259      global $CFG;
 260      $status = true;
 261  
 262          //First we check to moddata exists and create it as necessary
 263          //in temp/backup/$backup_code  dir
 264      $status = check_and_create_moddata_dir($preferences->backup_unique_code);
 265      $status = check_dir_exists($CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/moddata/data/",true);
 266          //Now copy the data dir
 267      if ($status) {
 268              //Only if it exists !! Thanks to Daniel Miksik.
 269          if (is_dir($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/data/".$instanceid)) {
 270              $status = backup_copy_file($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/data/".$instanceid,
 271                                             $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/moddata/data/".$instanceid);
 272          }
 273      }
 274      return $status;
 275  }
 276  
 277  function backup_data_records($bf,$preferences,$dataid){
 278  
 279      global $CFG;
 280      $status = true;
 281  
 282      $data_records = get_records("data_records","dataid",$dataid);
 283          //If there is submissions
 284          if ($data_records) {
 285              //Write start tag
 286              $status =fwrite ($bf,start_tag("RECORDS",4,true));
 287              //Iterate over each submission
 288              foreach ($data_records as $rec_sub) {
 289                  //Start submission
 290                  $status =fwrite ($bf,start_tag("RECORD",5,true));
 291                  //Print submission contents
 292                  fwrite ($bf,full_tag("ID",6,false,$rec_sub->id));
 293                  fwrite ($bf,full_tag("USERID",6,false,$rec_sub->userid));
 294                  fwrite ($bf,full_tag("GROUPID",6,false,$rec_sub->groupid));
 295                  fwrite ($bf,full_tag("DATAID",6,false,$rec_sub->dataid));
 296                  fwrite ($bf,full_tag("TIMECREATED",6,false,$rec_sub->timecreated));
 297                  fwrite ($bf,full_tag("TIMEMODIFIED",6,false,$rec_sub->timemodified));
 298                  fwrite ($bf,full_tag("APPROVED",6,false,$rec_sub->approved));
 299                  //End submission
 300  
 301                  backup_data_content($bf,$preferences,$rec_sub->id);
 302                  backup_data_ratings($bf,$preferences,$rec_sub->id);
 303                  backup_data_comments($bf,$preferences,$rec_sub->id);
 304  
 305                  $status =fwrite ($bf,end_tag("RECORD",5,true));
 306              }
 307              //Write end tag
 308              $status =fwrite ($bf,end_tag("RECORDS",4,true));
 309          }
 310          return $status;
 311  
 312  }
 313  
 314  function backup_data_files($bf,$preferences) {
 315  
 316      global $CFG;
 317  
 318      $status = true;
 319  
 320          //First we check to moddata exists and create it as necessary
 321          //in temp/backup/$backup_code  dir
 322      $status = check_and_create_moddata_dir($preferences->backup_unique_code);
 323          //Now copy the data dir
 324      if ($status) {
 325              //Only if it exists !! Thanks to Daniel Miksik.
 326          if (is_dir($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/data")) {
 327              $status = backup_copy_file($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/data",
 328                                                 $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/moddata/data");
 329          }
 330      }
 331  
 332      return $status;
 333  }
 334  
 335  function backup_data_file_instance($bf,$preferences,$instanceid) {
 336  
 337      global $CFG;
 338      $status = true;
 339  
 340          //First we check to moddata exists and create it as necessary
 341          //in temp/backup/$backup_code  dir
 342      $status = check_and_create_moddata_dir($preferences->backup_unique_code);
 343      $status = check_dir_exists($CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/moddata/data/",true);
 344          //Now copy the data dir
 345      if ($status) {
 346              //Only if it exists !! Thanks to Daniel Miksik.
 347          if (is_dir($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/data/".$instanceid)) {
 348              $status = backup_copy_file($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/data/".$instanceid,
 349                                             $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/moddata/data/".$instanceid);
 350          }
 351      }
 352      return $status;
 353  }
 354  
 355  function data_check_backup_mods_instances($instance,$backup_unique_code) {
 356      $info[$instance->id.'0'][0] = '<b>'.$instance->name.'</b>';
 357      $info[$instance->id.'0'][1] = '';
 358      if (!empty($instance->userdata)) {
 359          // any other needed stuff
 360      }
 361      return $info;
 362  }
 363  
 364  function data_check_backup_mods($course,$user_data=false,$backup_unique_code,$instances=null) {
 365      if (!empty($instances) && is_array($instances) && count($instances)) {
 366          $info = array();
 367          foreach ($instances as $id => $instance) {
 368              $info += data_check_backup_mods_instances($instance,$backup_unique_code);
 369          }
 370          return $info;
 371      }
 372  
 373      // otherwise continue as normal
 374      //First the course data
 375      $info[0][0] = get_string("modulenameplural","data");
 376      if ($ids = data_ids ($course)) {
 377          $info[0][1] = count($ids);
 378      } else {
 379          $info[0][1] = 0;
 380      }
 381  
 382      //Now, if requested, the user_data
 383      if ($user_data) {
 384          // any other needed stuff
 385      }
 386      return $info;
 387  
 388  }
 389  
 390  /**
 391   * Returns a content encoded to support interactivities linking. Every module
 392   * should have its own. They are called automatically from the backup procedure.
 393   *
 394   * @param string $content content to be encoded
 395   * @param object $preferences backup preferences in use
 396   * @return string the content encoded
 397   */
 398  function data_encode_content_links ($content,$preferences) {
 399  
 400      global $CFG;
 401  
 402      $base = preg_quote($CFG->wwwroot,"/");
 403  
 404  /// Link to one "record" of the database
 405      $search="/(".$base."\/mod\/data\/view.php\?d\=)([0-9]+)\&rid\=([0-9]+)/";
 406      $result= preg_replace($search,'$@DATAVIEWRECORD*$2*$3@$',$content);
 407  
 408  /// Link to the list of databases
 409      $search="/(".$base."\/mod\/data\/index.php\?id\=)([0-9]+)/";
 410      $result= preg_replace($search,'$@DATAINDEX*$2@$',$result);
 411  
 412  /// Link to database view by moduleid
 413      $search="/(".$base."\/mod\/data\/view.php\?id\=)([0-9]+)/";
 414      $result= preg_replace($search,'$@DATAVIEWBYID*$2@$',$result);
 415  
 416  /// Link to database view by databaseid
 417      $search="/(".$base."\/mod\/data\/view.php\?d\=)([0-9]+)/";
 418      $result= preg_replace($search,'$@DATAVIEWBYD*$2@$',$result);
 419  
 420      return $result;
 421  }
 422  
 423  function data_ids($course) {
 424      // stub function, return number of modules
 425      return 1;
 426  }


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