Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added backup and restore features

  • Loading branch information...
commit dde32eea69b62d8f083fa3cf25cf8f3c340a41c2 1 parent 16315dd
bobopinna authored

Showing 2 changed files with 473 additions and 0 deletions. Show diff stats Hide diff stats

  1. +208 0 mod/scorm/backuplib.php
  2. +265 0 mod/scorm/restorelib.php
208 mod/scorm/backuplib.php
... ... @@ -0,0 +1,208 @@
  1 +<?PHP //$Id$
  2 + //This php script contains all the stuff to backup/restore
  3 + //scorm mods
  4 +
  5 + //This is the "graphical" structure of the scorm mod:
  6 + //
  7 + // scorm
  8 + // (CL,pk->id)--------------------
  9 + // | |
  10 + // | |
  11 + // | |
  12 + // scorm_scoes |
  13 + // (UL,pk->id, fk->scorm) |
  14 + // | |
  15 + // | |
  16 + // | |
  17 + // scorm_sco_users |
  18 + // (UL,pk->id, fk->scormid, fk->scoid)----
  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 scorm_backup_mods($bf,$preferences) {
  30 +
  31 + global $CFG;
  32 +
  33 + $status = true;
  34 +
  35 + //Iterate over scorm table
  36 + $scorms = get_records ("scorm","course",$preferences->backup_course,"id");
  37 + if ($scorms) {
  38 + foreach ($scorms as $scorm) {
  39 + //Start mod
  40 + fwrite ($bf,start_tag("MOD",3,true));
  41 + //Print scorm data
  42 + fwrite ($bf,full_tag("ID",4,false,$scorm->id));
  43 + fwrite ($bf,full_tag("MODTYPE",4,false,"scorm"));
  44 + fwrite ($bf,full_tag("NAME",4,false,$scorm->name));
  45 + fwrite ($bf,full_tag("REFERENCE",4,false,$scorm->reference));
  46 + fwrite ($bf,full_tag("DATADIR",4,false,$scorm->datadir));
  47 + fwrite ($bf,full_tag("LAUNCH",4,false,$scorm->launch));
  48 + fwrite ($bf,full_tag("SUMMARY",4,false,$scorm->summary));
  49 + fwrite ($bf,full_tag("AUTO",4,false,$scorm->auto));
  50 + fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$scorm->timemodified));
  51 + $status = backup_scorm_scoes($bf,$preferences,$scorm->id);
  52 + //if we've selected to backup users info, then execute backup_scorm_sco_users
  53 + if ($status) {
  54 + if ($preferences->mods["scorm"]->userinfo) {
  55 + $status = backup_scorm_sco_users($bf,$preferences,$scorm->id);
  56 + }
  57 + }
  58 + //End mod
  59 + $status =fwrite ($bf,end_tag("MOD",3,true));
  60 + }
  61 + //backup scorm files
  62 + if ($status) {
  63 + $status = backup_scorm_files($bf,$preferences);
  64 + }
  65 + }
  66 + return $status;
  67 + }
  68 +
  69 + //Backup scorm_scoes contents (executed from scorm_backup_mods)
  70 + function backup_scorm_scoes ($bf,$preferences,$scorm) {
  71 +
  72 + global $CFG;
  73 +
  74 + $status = true;
  75 +
  76 + $scorm_scoes = get_records("scorm_scoes","scorm",$scorm,"id");
  77 + //If there is submissions
  78 + if ($scorm_scoes) {
  79 + //Write start tag
  80 + $status =fwrite ($bf,start_tag("SCOES",4,true));
  81 + //Iterate over each sco
  82 + foreach ($scorm_scoes as $sco) {
  83 + //Start sco
  84 + $status =fwrite ($bf,start_tag("SCO",5,true));
  85 + //Print submission contents
  86 + fwrite ($bf,full_tag("ID",6,false,$sco->id));
  87 + fwrite ($bf,full_tag("PARENT",6,false,$sco->parent));
  88 + fwrite ($bf,full_tag("IDENTIFIER",6,false,$sco->identifier));
  89 + fwrite ($bf,full_tag("LAUNCH",6,false,$sco->launch));
  90 + fwrite ($bf,full_tag("TYPE",6,false,$sco->type));
  91 + fwrite ($bf,full_tag("TITLE",6,false,$sco->title));
  92 + fwrite ($bf,full_tag("NEXT",6,false,$sco->next));
  93 + fwrite ($bf,full_tag("PREVIOUS",6,false,$sco->previous));
  94 + //End sco
  95 + $status =fwrite ($bf,end_tag("SCO",5,true));
  96 + }
  97 + //Write end tag
  98 + $status =fwrite ($bf,end_tag("SCOES",4,true));
  99 + }
  100 + return $status;
  101 + }
  102 +
  103 + //Backup scorm_sco_users contents (executed from scorm_backup_mods)
  104 + function backup_scorm_sco_users ($bf,$preferences,$scorm) {
  105 +
  106 + global $CFG;
  107 +
  108 + $status = true;
  109 +
  110 + $scorm_sco_users = get_records("scorm_sco_users","scormid",$scorm,"id");
  111 + //If there is submissions
  112 + if ($scorm_sco_users) {
  113 + //Write start tag
  114 + $status =fwrite ($bf,start_tag("SCO_USERS",4,true));
  115 + //Iterate over each sco
  116 + foreach ($scorm_sco_users as $sco_user) {
  117 + //Start sco
  118 + $status =fwrite ($bf,start_tag("SCO_USER",5,true));
  119 + //Print submission contents
  120 + fwrite ($bf,full_tag("ID",6,false,$sco_user->id));
  121 + fwrite ($bf,full_tag("USERID",6,false,$sco_user->userid));
  122 + fwrite ($bf,full_tag("SCOID",6,false,$sco_user->scoid));
  123 + fwrite ($bf,full_tag("CMI_CORE_LESSON_LOCATION",6,false,$sco_user->cmi_core_lesson_location));
  124 + fwrite ($bf,full_tag("CMI_CORE_LESSON_STATUS",6,false,$sco_user->cmi_core_lesson_status));
  125 + fwrite ($bf,full_tag("CMI_CORE_EXIT",6,false,$sco_user->cmi_core_exit));
  126 + fwrite ($bf,full_tag("CMI_CORE_TOTAL_TIME",6,false,$sco_user->cmi_core_total_time));
  127 + fwrite ($bf,full_tag("CMI_CORE_SESSION_TIME",6,false,$sco_user->cmi_core_session_time));
  128 + fwrite ($bf,full_tag("CMI_CORE_SCORE_RAW",6,false,$sco_user->cmi_core_score_raw));
  129 + fwrite ($bf,full_tag("CMI_SUSPEND_DATA",6,false,$sco_user->cmi_suspend_data));
  130 + fwrite ($bf,full_tag("CMI_LAUNCH_DATA",6,false,$sco_user->cmi_launch_data));
  131 + //End sco
  132 + $status =fwrite ($bf,end_tag("SCO_USER",5,true));
  133 + }
  134 + //Write end tag
  135 + $status =fwrite ($bf,end_tag("SCO_USERS",4,true));
  136 + }
  137 + return $status;
  138 + }
  139 +
  140 + ////Return an array of info (name,value)
  141 + function scorm_check_backup_mods($course,$user_data=false,$backup_unique_code) {
  142 + //First the course data
  143 + $info[0][0] = get_string("modulenameplural","scorm");
  144 + if ($ids = scorm_ids ($course)) {
  145 + $info[0][1] = count($ids);
  146 + } else {
  147 + $info[0][1] = 0;
  148 + }
  149 +
  150 + //Now, if requested, the user_data
  151 + if ($user_data) {
  152 + $info[1][0] = get_string("scoes","scorm");
  153 + if ($ids = scorm_sco_users_ids_by_course ($course)) {
  154 + $info[1][1] = count($ids);
  155 + } else {
  156 + $info[1][1] = 0;
  157 + }
  158 + }
  159 + return $info;
  160 + }
  161 +
  162 + //Backup scorm package files
  163 + function backup_scorm_files($bf,$preferences) {
  164 +
  165 + global $CFG;
  166 +
  167 + $status = true;
  168 +
  169 + //First we check to moddata exists and create it as necessary
  170 + //in temp/backup/$backup_code dir
  171 + $status = check_and_create_moddata_dir($preferences->backup_unique_code);
  172 + //Now copy the scorm dir
  173 + if ($status) {
  174 + if (is_dir($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/scorm")) {
  175 + $status = backup_copy_file($CFG->dataroot."/".$preferences->backup_course."/".$CFG->moddata."/scorm",
  176 + $CFG->dataroot."/temp/backup/".$preferences->backup_unique_code."/moddata/scorm");
  177 + }
  178 + }
  179 +
  180 + return $status;
  181 +
  182 + }
  183 +
  184 +
  185 + // INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE
  186 +
  187 + //Returns an array of scorms id
  188 + function scorm_ids ($course) {
  189 +
  190 + global $CFG;
  191 +
  192 + return get_records_sql ("SELECT a.id, a.course
  193 + FROM {$CFG->prefix}scorm a
  194 + WHERE a.course = '$course'");
  195 + }
  196 +
  197 + //Returns an array of scorm_scoes id
  198 + function scorm_sco_users_ids_by_course ($course) {
  199 +
  200 + global $CFG;
  201 +
  202 + return get_records_sql ("SELECT s.id , s.scormid
  203 + FROM {$CFG->prefix}scorm_sco_users s,
  204 + {$CFG->prefix}scorm a
  205 + WHERE a.course = '$course' AND
  206 + s.scormid = a.id");
  207 + }
  208 +?>
265 mod/scorm/restorelib.php
... ... @@ -0,0 +1,265 @@
  1 +<?PHP //$Id$
  2 + //This php script contains all the stuff to backup/restore
  3 + //reservation mods
  4 +
  5 + //This is the "graphical" structure of the scorm mod:
  6 + //
  7 + // scorm
  8 + // (CL,pk->id)--------------------
  9 + // | |
  10 + // | |
  11 + // | |
  12 + // scorm_scoes |
  13 + // (UL,pk->id, fk->scorm) |
  14 + // | |
  15 + // | |
  16 + // | |
  17 + // scorm_sco_users |
  18 + // (UL,pk->id, fk->scormid, fk->scoid)----
  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 + //This function executes all the restore procedure about this mod
  30 + function scorm_restore_mods($mod,$restore) {
  31 +
  32 + global $CFG;
  33 +
  34 + $status = true;
  35 +
  36 + //Get record from backup_ids
  37 + $data = backup_getid($restore->backup_unique_code,$mod->modtype,$mod->id);
  38 +
  39 + if ($data) {
  40 + //Now get completed xmlized object
  41 + $info = $data->info;
  42 + //traverse_xmlize($info); //Debug
  43 + //print_object ($GLOBALS['traverse_array']); //Debug
  44 + //$GLOBALS['traverse_array']=""; //Debug
  45 +
  46 + //Now, build the SCORM record structure
  47 + $scorm->course = $restore->course_id;
  48 + $scorm->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
  49 + $scorm->reference = backup_todb($info['MOD']['#']['REFERENCE']['0']['#']);
  50 + $scorm->datadir = backup_todb($info['MOD']['#']['DATADIR']['0']['#']);
  51 + $scorm->launch = backup_todb($info['MOD']['#']['LAUNCH']['0']['#']);
  52 + $scorm->summary = backup_todb($info['MOD']['#']['SUMMARY']['0']['#']);
  53 + $scorm->auto = backup_todb($info['MOD']['#']['AUTO']['0']['#']);
  54 + $scorm->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
  55 +
  56 + //The structure is equal to the db, so insert the scorm
  57 + $newid = insert_record ("scorm",$scorm);
  58 + //Do some output
  59 + echo "<ul><li>".get_string("modulename","scorm")." \"".$scorm->name."\"<br>";
  60 + backup_flush(300);
  61 +
  62 + if ($newid) {
  63 + //We have the newid, update backup_ids
  64 + backup_putid($restore->backup_unique_code,$mod->modtype,
  65 + $mod->id, $newid);
  66 +
  67 + //Now copy moddata associated files
  68 + $status = scorm_restore_files ($scorm->datadir, $restore);
  69 +
  70 + if ($status)
  71 + $status = scorm_scoes_restore_mods ($newid,$info,$restore);
  72 +
  73 + //Now check if want to restore user data and do it.
  74 + if ($restore->mods['scorm']->userinfo) {
  75 + //Restore scorm_scoes
  76 + if ($status)
  77 + $status = scorm_sco_users_restore_mods ($newid,$info,$restore);
  78 + }
  79 + } else {
  80 + $status = false;
  81 + }
  82 +
  83 + //Finalize ul
  84 + echo "</ul>";
  85 +
  86 + } else {
  87 + $status = false;
  88 + }
  89 +
  90 + return $status;
  91 + }
  92 +
  93 + //This function restores the scorm_scoes
  94 + function scorm_scoes_restore_mods($scorm_id,$info,$restore) {
  95 +
  96 + global $CFG;
  97 +
  98 + $status = true;
  99 +
  100 + //Get the sco array
  101 + $scoes = $info['MOD']['#']['SCOES']['0']['#']['SCO'];
  102 +
  103 + //Iterate over scoes
  104 + for($i = 0; $i < sizeof($scoes); $i++) {
  105 + $sub_info = $scoes[$i];
  106 +
  107 + //We'll need this later!!
  108 + $oldid = backup_todb($sub_info['#']['ID']['0']['#']);
  109 +
  110 + //Now, build the scorm_scoes record structure
  111 + $sco->scorm = $scorm_id;
  112 + $sco->parent = backup_todb($sub_info['#']['PARENT']['0']['#']);
  113 + $sco->identifier = backup_todb($sub_info['#']['IDENTIFIER']['0']['#']);
  114 + $sco->launch = backup_todb($sub_info['#']['LAUNCH']['0']['#']);
  115 + $sco->type = backup_todb($sub_info['#']['TYPE']['0']['#']);
  116 + $sco->title = backup_todb($sub_info['#']['TITLE']['0']['#']);
  117 + $sco->next = backup_todb($sub_info['#']['NEXT']['0']['#']);
  118 + $sco->previous = backup_todb($sub_info['#']['PREVIOUS']['0']['#']);
  119 +
  120 + //The structure is equal to the db, so insert the scorm_scoes
  121 + $newid = insert_record ("scorm_scoes",$sco);
  122 +
  123 + //Do some output
  124 + if (($i+1) % 50 == 0) {
  125 + echo ".";
  126 + if (($i+1) % 1000 == 0) {
  127 + echo "<br>";
  128 + }
  129 + backup_flush(300);
  130 + }
  131 +
  132 + if ($newid) {
  133 + //We have the newid, update backup_ids
  134 + backup_putid($restore->backup_unique_code,"scorm_scoes", $oldid, $newid);
  135 + } else {
  136 + $status = false;
  137 + }
  138 + }
  139 +
  140 + return $status;
  141 + }
  142 +
  143 + //This function restores the scorm_sco_users
  144 + function scorm_sco_users_restore_mods($scorm_id,$info,$restore) {
  145 +
  146 + global $CFG;
  147 +
  148 + $status = true;
  149 + $sco_users = NULL;
  150 +
  151 + //Get the sco array
  152 + if (!empty($info['MOD']['#']['SCO_USERS']['0']['#']['SCO_USER']))
  153 + $sco_users = $info['MOD']['#']['SCO_USERS']['0']['#']['SCO_USER'];
  154 +
  155 + //Iterate over sco_users
  156 + for($i = 0; $i < sizeof($sco_users); $i++) {
  157 + $sub_info = $sco_users[$i];
  158 +
  159 + //We'll need this later!!
  160 + $oldid = backup_todb($sub_info['#']['ID']['0']['#']);
  161 + $oldscoid = backup_todb($sub_info['#']['SCOID']['0']['#']);
  162 + $olduserid = backup_todb($sub_info['#']['USERID']['0']['#']);
  163 +
  164 + //Now, build the scorm_sco_users record structure
  165 + $sco_user->scormid = $scorm_id;
  166 + $sco_user->userid = backup_todb($sub_info['#']['USERID']['0']['#']);
  167 + $sco_user->scoid = backup_todb($sub_info['#']['SCOID']['0']['#']);
  168 + $sco_user->cmi_core_lesson_location = backup_todb($sub_info['#']['CMI_CORE_LESSON_LOCATION']['0']['#']);
  169 + $sco_user->cmi_core_lesson_status = backup_todb($sub_info['#']['CMI_CORE_LESSON_STATUS']['0']['#']);
  170 + $sco_user->cmi_core_exit = backup_todb($sub_info['#']['CMI_CORE_EXIT']['0']['#']);
  171 + $sco_user->cmi_core_total_time = backup_todb($sub_info['#']['CMI_CORE_TOTAL_TIME']['0']['#']);
  172 + $sco_user->cmi_core_session_time = backup_todb($sub_info['#']['CMI_CORE_SESSION_TIME']['0']['#']);
  173 + $sco_user->cmi_core_score_raw = backup_todb($sub_info['#']['CMI_CORE_SCORE_RAW']['0']['#']);
  174 + $sco_user->cmi_suspend_data = backup_todb($sub_info['#']['CMI_SUSPEND_DATA']['0']['#']);
  175 + $sco_user->cmi_launch_data = backup_todb($sub_info['#']['CMI_LAUNCH_DATA']['0']['#']);
  176 +
  177 + //We have to recode the userid field
  178 + $user = backup_getid($restore->backup_unique_code,"user",$sco_user->userid);
  179 + if ($user) {
  180 + $sco_user->userid = $user->new_id;
  181 + }
  182 +
  183 + //We have to recode the scoid field
  184 + $sco = backup_getid($restore->backup_unique_code,"scorm_scoes",$sco_user->scoid);
  185 + if ($sco) {
  186 + $sco_user->scoid = $sco->new_id;
  187 + }
  188 +
  189 + //The structure is equal to the db, so insert the scorm_sco_users
  190 + $newid = insert_record ("scorm_sco_users",$sco_user);
  191 +
  192 + //Do some output
  193 + if (($i+1) % 50 == 0) {
  194 + echo ".";
  195 + if (($i+1) % 1000 == 0) {
  196 + echo "<br>";
  197 + }
  198 + backup_flush(300);
  199 + }
  200 +
  201 + }
  202 +
  203 + return $status;
  204 + }
  205 +
  206 + //This function copies the scorm related info from backup temp dir to course moddata folder,
  207 + //creating it if needed
  208 + function scorm_restore_files ($packagedir, $restore) {
  209 +
  210 + global $CFG;
  211 +
  212 + $status = true;
  213 + $todo = false;
  214 + $moddata_path = "";
  215 + $scorm_path = "";
  216 + $temp_path = "";
  217 +
  218 + //First, we check to "course_id" exists and create is as necessary
  219 + //in CFG->dataroot
  220 + $dest_dir = $CFG->dataroot."/".$restore->course_id;
  221 + $status = check_dir_exists($dest_dir,true);
  222 +
  223 + //First, locate course's moddata directory
  224 + $moddata_path = $CFG->dataroot."/".$restore->course_id."/".$CFG->moddata;
  225 +
  226 + //Check it exists and create it
  227 + $status = check_dir_exists($moddata_path,true);
  228 +
  229 + //Now, locate scorm directory
  230 + if ($status) {
  231 + $scorm_path = $moddata_path."/scorm";
  232 + //Check it exists and create it
  233 + $status = check_dir_exists($scorm_path,true);
  234 + }
  235 +
  236 + //Now locate the temp dir we are restoring from
  237 + if ($status) {
  238 + $temp_path = $CFG->dataroot."/temp/backup/".$restore->backup_unique_code.
  239 + "/moddata/scorm/".$packagedir;
  240 + //Check it exists
  241 + if (is_dir($temp_path)) {
  242 + $todo = true;
  243 + }
  244 + }
  245 +
  246 + //If todo, we create the neccesary dirs in course moddata/scorm
  247 + if ($status and $todo) {
  248 + //Make scorm package directory path
  249 + $this_scorm_path = $scorm_path."/".$packagedir;
  250 +
  251 + $status = backup_copy_file($temp_path, $this_scorm_path);
  252 + }
  253 +
  254 + return $status;
  255 + }
  256 +
  257 + //This function returns a log record with all the necessay transformations
  258 + //done. It's used by restore_log_module() to restore modules log.
  259 + function scorm_restore_logs($restore,$log) {
  260 +
  261 + $status = true;
  262 +
  263 + return $status;
  264 + }
  265 +?>

0 comments on commit dde32ee

Please sign in to comment.
Something went wrong with that request. Please try again.