2 //This php script contains all the stuff to backup/restore data mod
4 //This is the "graphical" structure of the data mod:
11 // ---------------------------------------------------------------------------------
13 //data_records (UL,pk->id, fk->data) data_fields (pk->id, fk->data)
16 // ----------------------------------------------------------------------------- |
18 //data_ratings(fk->recordid, pk->id) data_comments (fk->recordid, pk->id) | |
19 // data_content(pk->id, fk->recordid, fk->fieldid)
23 // Meaning: pk->primary key field of the table
24 // fk->foreign key to link with parent
25 // nt->nested field (recursive data)
26 // CL->course level info
27 // UL->user level info
28 // files->table may have files)
30 //-----------------------------------------------------------
32 //Backup data files because we've selected to backup user info
33 //and files are user info's level
35 $fieldids = array(); //array in the format of $fieldids[$oldid]=$newid. This is needed because of double dependencies of multiple tables.
38 //Return a content encoded to support interactivities linking. Every module
39 function data_restore_mods($mod,$restore) {
45 $data = backup_getid($restore->backup_unique_code
,$mod->modtype
,$mod->id
);
48 //Now get completed xmlized object
50 // if necessary, write to restorelog and adjust date/time fields
51 if ($restore->course_startdateoffset
) {
52 restore_log_date_changes('Database', $restore, $info['MOD']['#'], array('TIMEAVAILABLEFROM', 'TIMEAVAILABLETO','TIMEVIEWFROM', 'TIMEVIEWTO'));
54 //traverse_xmlize($info); //Debug
55 //print_object ($GLOBALS['traverse_array']); //Debug
56 //$GLOBALS['traverse_array']=""; //Debug
58 $database->course
= $restore->course_id
;
60 $database->name
= backup_todb($info['MOD']['#']['NAME']['0']['#']);
61 $database->intro
= backup_todb($info['MOD']['#']['INTRO']['0']['#']);
62 // Only relevant for restoring backups from 1.6 in a 1.7 install.
63 if (isset($info['MOD']['#']['RATINGS']['0']['#'])) {
64 $database->ratings
= backup_todb($info['MOD']['#']['RATINGS']['0']['#']);
66 $database->comments
= backup_todb($info['MOD']['#']['COMMENTS']['0']['#']);
67 $database->timeavailablefrom
= backup_todb($info['MOD']['#']['TIMEAVAILABLEFROM']['0']['#']);
68 $database->timeavailableto
= backup_todb($info['MOD']['#']['TIMEAVAILABLETO']['0']['#']);
69 $database->timeviewfrom
= backup_todb($info['MOD']['#']['TIMEVIEWFROM']['0']['#']);
70 $database->timeviewto
= backup_todb($info['MOD']['#']['TIMEVIEWTO']['0']['#']);
71 // Only relevant for restoring backups from 1.6 in a 1.7 install.
72 if (isset($info['MOD']['#']['PARTICIPANTS']['0']['#'])) {
73 $database->participants
= backup_todb($info['MOD']['#']['PARTICIPANTS']['0']['#']);
75 $database->requiredentries
= backup_todb($info['MOD']['#']['REQUIREDENTRIES']['0']['#']);
76 $database->requiredentriestoview
= backup_todb($info['MOD']['#']['REQUIREDENTRIESTOVIEW']['0']['#']);
77 $database->maxentries
= backup_todb($info['MOD']['#']['MAXENTRIES']['0']['#']);
78 $database->rssarticles
= backup_todb($info['MOD']['#']['RSSARTICLES']['0']['#']);
79 $database->singletemplate
= backup_todb($info['MOD']['#']['SINGLETEMPLATE']['0']['#']);
80 $database->listtemplate
= backup_todb($info['MOD']['#']['LISTTEMPLATE']['0']['#']);
81 $database->listtemplateheader
= backup_todb($info['MOD']['#']['LISTTEMPLATEHEADER']['0']['#']);
82 $database->listtemplatefooter
= backup_todb($info['MOD']['#']['LISTTEMPLATEFOOTER']['0']['#']);
83 $database->addtemplate
= backup_todb($info['MOD']['#']['ADDTEMPLATE']['0']['#']);
84 $database->rsstemplate
= backup_todb($info['MOD']['#']['RSSTEMPLATE']['0']['#']);
85 $database->rsstitletemplate
= backup_todb($info['MOD']['#']['RSSTITLETEMPLATE']['0']['#']);
86 $database->csstemplate
= backup_todb($info['MOD']['#']['CSSTEMPLATE']['0']['#']);
87 $database->jstemplate
= backup_todb($info['MOD']['#']['JSTEMPLATE']['0']['#']);
88 $database->approval
= backup_todb($info['MOD']['#']['APPROVAL']['0']['#']);
89 $database->scale
= backup_todb($info['MOD']['#']['SCALE']['0']['#']);
90 $database->assessed
= backup_todb($info['MOD']['#']['ASSESSED']['0']['#']);
91 // Only relevant for restoring backups from 1.6 in a 1.7 install.
92 if (isset($info['MOD']['#']['ASSESSPUBLIC']['0']['#'])) {
93 $database->assesspublic
= backup_todb($info['MOD']['#']['ASSESSPUBLIC']['0']['#']);
96 $newid = insert_record ('data', $database);
99 if (!defined('RESTORE_SILENTLY')) {
100 echo "<li>".get_string("modulename","data")." \"".format_string(stripslashes($database->name
),true)."\"</li>";
104 //We have the newid, update backup_ids
105 backup_putid($restore->backup_unique_code
,$mod->modtype
,
107 //Now check if want to restore user data and do it.
108 if (function_exists('restore_userdata_selected')) {
110 $restore_userdata_selected = restore_userdata_selected($restore, 'data', $mod->id
);
113 $restore_userdata_selected = $restore->mods
['data']->userinfo
;
116 //Restore data_fields first!!! need to hold an array of [oldid]=>newid due to double dependencies
117 $status = $status and data_fields_restore_mods ($mod->id
, $newid, $info, $restore);
119 if ($restore_userdata_selected) {
120 $status = $status and data_records_restore_mods ($mod->id
, $newid, $info, $restore);
123 // If the backup contained $data->participants, $data->assesspublic
124 // and $data->groupmode, we need to convert the data to use Roles.
125 // It means the backup was made pre Moodle 1.7. We check the
126 // backup_version to make sure.
127 if (isset($database->participants
) && isset($database->assesspublic
)) {
129 if (!$teacherroles = get_roles_with_capability('moodle/legacy:teacher', CAP_ALLOW
)) {
130 notice('Default teacher role was not found. Roles and permissions '.
131 'for your database modules will have to be manually set.');
133 if (!$studentroles = get_roles_with_capability('moodle/legacy:student', CAP_ALLOW
)) {
134 notice('Default student role was not found. Roles and permissions '.
135 'for all your database modules will have to be manually set.');
137 if (!$guestroles = get_roles_with_capability('moodle/legacy:guest', CAP_ALLOW
)) {
138 notice('Default guest role was not found. Roles and permissions '.
139 'for all your database modules will have to be manually set.');
141 require_once($CFG->dirroot
.'/mod/data/lib.php');
142 data_convert_to_roles($database, $teacherroles, $studentroles,
143 $restore->mods
['data']->instances
[$mod->id
]->restored_as_course_module
);
156 function data_fields_restore_mods ($old_data_id, $new_data_id, $info, $restore) {
158 global $CFG, $fieldids;
161 $fields = $info['MOD']['#']['FIELDS']['0']['#']['FIELD'];
163 for ($i = 0; $i < sizeof($fields); $i++
) {
165 $fie_info = $fields[$i];
166 $oldid = backup_todb($fie_info['#']['ID']['0']['#']);
168 $field -> dataid
= $new_data_id;
169 $field -> type
= backup_todb($fie_info['#']['TYPE']['0']['#']);
170 $field -> name
= backup_todb($fie_info['#']['NAME']['0']['#']);
171 $field -> description
= backup_todb($fie_info['#']['DESCRIPTION']['0']['#']);
172 $field -> param1
= backup_todb($fie_info['#']['PARAM1']['0']['#']);
173 $field -> param2
= backup_todb($fie_info['#']['PARAM2']['0']['#']);
174 $field -> param3
= backup_todb($fie_info['#']['PARAM3']['0']['#']);
175 $field -> param4
= backup_todb($fie_info['#']['PARAM4']['0']['#']);
176 $field -> param5
= backup_todb($fie_info['#']['PARAM5']['0']['#']);
177 $field -> param6
= backup_todb($fie_info['#']['PARAM6']['0']['#']);
178 $field -> param7
= backup_todb($fie_info['#']['PARAM7']['0']['#']);
179 $field -> param8
= backup_todb($fie_info['#']['PARAM8']['0']['#']);
180 $field -> param9
= backup_todb($fie_info['#']['PARAM9']['0']['#']);
181 $field -> param10
= backup_todb($fie_info['#']['PARAM10']['0']['#']);
183 $newid = insert_record ("data_fields",$field);
185 $fieldids[$oldid] = $newid; //so we can use them in sub tables that depends on both fieldid and recordid
188 if (($i+
1) %
50 == 0) {
189 if (!defined('RESTORE_SILENTLY')) {
191 if (($i+
1) %
1000 == 0) {
199 //We have the newid, update backup_ids
200 $status = backup_putid($restore->backup_unique_code
,"data_fields",$oldid, $newid);
210 function data_records_restore_mods ($old_data_id, $new_data_id, $info, $restore) {
212 global $CFG, $fieldids;
216 $records = $info['MOD']['#']['RECORDS']['0']['#']['RECORD'];
218 for ($i = 0; $i < sizeof($records); $i++
) {
220 $rec_info = $records[$i];
221 $oldid = backup_todb($rec_info['#']['ID']['0']['#']);
223 $record = new object();
224 $record -> dataid
= $new_data_id;
225 $record -> userid
= backup_todb($rec_info['#']['USERID']['0']['#']);
226 $record -> groupid
= backup_todb($rec_info['#']['GROUPID']['0']['#']);
227 $record -> timecreated
= backup_todb($rec_info['#']['TIMECREATED']['0']['#']);
228 $record -> timemodified
= backup_todb($rec_info['#']['TIMEMODIFIED']['0']['#']);
229 $record -> approved
= backup_todb($rec_info['#']['APPROVED']['0']['#']);
230 $user = backup_getid($restore->backup_unique_code
,"user",$record->userid
);
231 $group= backup_getid($restore->backup_unique_code
,"groups",$record->groupid
);
234 $record->userid
= $user->new_id
;
237 $record->groupid
= $group->new_id
;
240 $newid = insert_record ("data_records",$record);
243 if (($i+
1) %
50 == 0) {
244 if (!defined('RESTORE_SILENTLY')) {
246 if (($i+
1) %
1000 == 0) {
254 //We have the newid, update backup_ids
255 $status = $status and backup_putid($restore->backup_unique_code
,"data_records",$oldid, $newid);
257 $status = $status and data_content_restore_mods ($oldid, $newid, $old_data_id, $new_data_id, $rec_info, $restore);
258 $status = $status and data_ratings_restore_mods ($oldid, $newid, $info, $rec_info);
259 $status = $status and data_comments_restore_mods ($oldid, $newid, $info, $rec_info);
268 function data_content_restore_mods ($old_record_id, $new_record_id, $old_data_id, $new_data_id, $recinfo, $restore) {
270 global $CFG, $fieldids;
274 $contents = $recinfo['#']['CONTENTS']['0']['#']['CONTENT'];
276 for ($i = 0; $i < sizeof($contents); $i++
) {
278 $con_info = $contents[$i];
279 $oldid = backup_todb($con_info['#']['ID']['0']['#']);
280 $oldfieldid = backup_todb($con_info['#']['FIELDID']['0']['#']);
281 $oldrecordid = backup_todb($con_info['#']['RECORDID']['0']['#']);
283 $content -> recordid
= $new_record_id;
284 $content -> fieldid
= $fieldids[$oldfieldid];
285 $content -> content
= backup_todb($con_info['#']['CONTENT']['0']['#']);
286 $content -> content1
= backup_todb($con_info['#']['CONTENT1']['0']['#']);
287 $content -> content2
= backup_todb($con_info['#']['CONTENT2']['0']['#']);
288 $content -> content3
= backup_todb($con_info['#']['CONTENT3']['0']['#']);
289 $content -> content4
= backup_todb($con_info['#']['CONTENT4']['0']['#']);
290 $newid = insert_record ("data_content",$content);
293 if (($i+
1) %
50 == 0) {
294 if (!defined('RESTORE_SILENTLY')) {
296 if (($i+
1) %
1000 == 0) {
304 //We have the newid, update backup_ids
306 $status = $status and data_restore_files ($old_data_id, $new_data_id, $oldfieldid, $content->fieldid
, $oldrecordid, $content->recordid
, $recinfo, $restore);
307 $status = $status and backup_putid($restore->backup_unique_code
,"data_content",$oldid, $newid);
316 function data_restore_files ($old_data_id, $new_data_id, $old_field_id, $new_field_id, $old_record_id, $new_record_id, $recinfo, $restore) {
326 //First, we check to "course_id" exists and create is as necessary
328 $dest_dir = $CFG->dataroot
."/".$restore->course_id
;
329 $status = check_dir_exists($dest_dir,true);
331 //Now, locate course's moddata directory
332 $moddata_path = $CFG->dataroot
."/".$restore->course_id
."/".$CFG->moddata
;
334 //Check it exists and create it
335 $status = check_dir_exists($moddata_path,true);
337 //Now, locate data directory
339 $data_path = $moddata_path."/data";
340 //Check it exists and create it
341 $status = check_dir_exists($data_path,true);
344 //Now locate the temp dir we are gong to restore
346 $temp_path = $CFG->dataroot
."/temp/backup/".$restore->backup_unique_code
.
347 "/moddata/data/".$old_data_id."/".$old_field_id."/".$old_record_id;
348 $todo = check_dir_exists($temp_path);
351 //If todo, we create the neccesary dirs in course moddata/data
352 if ($status and $todo) {
354 $this_data_path = $data_path."/".$new_data_id;
356 $status = check_dir_exists($this_data_path,true);
358 $this_field_path = $this_data_path."/".$new_field_id;
359 $status = check_dir_exists($this_field_path,true);
360 $this_record_path = $this_field_path = $this_field_path."/".$new_record_id;
361 $status = check_dir_exists($this_record_path,true);
362 //And now, copy temp_path to user_data_path
364 $status = @backup_copy_file
($temp_path, $this_record_path);
370 function data_ratings_restore_mods ($oldid, $newid, $info, $rec_info) {
376 $ratings= $rec_info['#']['RATINGS']['0']['#']['RATING'];
378 if (empty($ratings)) { // no ratings to restore
381 for ($i = 0; $i < sizeof($ratings); $i++
) {
383 $rat_info = $ratings[$i];
385 $rating -> recordid
= $newid;
386 $rating -> userid
= backup_todb($rat_info['#']['USERID']['0']['#']);
387 $rating -> rating
= backup_todb($rat_info['#']['RATING']['0']['#']);
389 if (! insert_record ("data_ratings",$rating)) {
396 function data_comments_restore_mods ($oldid, $newid, $info, $rec_info) {
402 $comments= $rec_info['#']['COMMENTS']['0']['#']['COMMENT'];
404 if (empty($comments)) { // no comments to restore
408 for ($i = 0; $i < sizeof($comments); $i++
) {
410 $com_info = $comments[$i];
412 $comment -> recordid
= $newid;
413 $comment -> userid
= backup_todb($com_info['#']['USERID']['0']['#']);
414 $comment -> content
= backup_todb($com_info['#']['CONTENT']['0']['#']);
415 $comment -> created
= backup_todb($com_info['#']['CREATED']['0']['#']);
416 $comment -> modified
= backup_todb($com_info['#']['MODIFIED']['0']['#']);
417 if (! insert_record ("data_comments",$comment)) {