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']['#']);
95 $database->defaultsort
= backup_todb($info['MOD']['#']['DEFAULTSORT']['0']['#']);
96 $database->defaultsortdir
= backup_todb($info['MOD']['#']['DEFAULTSORTDIR']['0']['#']);
97 $database->editany
= backup_todb($info['MOD']['#']['EDITANY']['0']['#']);
98 $database->notification
= backup_todb($info['MOD']['#']['NOTIFICATION']['0']['#']);
100 $newid = insert_record ('data', $database);
103 if (!defined('RESTORE_SILENTLY')) {
104 echo "<li>".get_string("modulename","data")." \"".format_string(stripslashes($database->name
),true)."\"</li>";
108 //We have the newid, update backup_ids
109 backup_putid($restore->backup_unique_code
,$mod->modtype
,
111 //Now check if want to restore user data and do it.
112 if (function_exists('restore_userdata_selected')) {
114 $restore_userdata_selected = restore_userdata_selected($restore, 'data', $mod->id
);
117 $restore_userdata_selected = $restore->mods
['data']->userinfo
;
120 //Restore data_fields first!!! need to hold an array of [oldid]=>newid due to double dependencies
121 $status = $status and data_fields_restore_mods ($mod->id
, $newid, $info, $restore);
123 if ($restore_userdata_selected) {
124 $status = $status and data_records_restore_mods ($mod->id
, $newid, $info, $restore);
127 // If the backup contained $data->participants, $data->assesspublic
128 // and $data->groupmode, we need to convert the data to use Roles.
129 // It means the backup was made pre Moodle 1.7. We check the
130 // backup_version to make sure.
131 if (isset($database->participants
) && isset($database->assesspublic
)) {
133 if (!$teacherroles = get_roles_with_capability('moodle/legacy:teacher', CAP_ALLOW
)) {
134 notice('Default teacher role was not found. Roles and permissions '.
135 'for your database modules will have to be manually set.');
137 if (!$studentroles = get_roles_with_capability('moodle/legacy:student', CAP_ALLOW
)) {
138 notice('Default student role was not found. Roles and permissions '.
139 'for all your database modules will have to be manually set.');
141 if (!$guestroles = get_roles_with_capability('moodle/legacy:guest', CAP_ALLOW
)) {
142 notice('Default guest role was not found. Roles and permissions '.
143 'for all your database modules will have to be manually set.');
145 require_once($CFG->dirroot
.'/mod/data/lib.php');
146 data_convert_to_roles($database, $teacherroles, $studentroles,
147 $restore->mods
['data']->instances
[$mod->id
]->restored_as_course_module
);
160 function data_fields_restore_mods ($old_data_id, $new_data_id, $info, $restore) {
162 global $CFG, $fieldids;
165 $fields = $info['MOD']['#']['FIELDS']['0']['#']['FIELD'];
167 for ($i = 0; $i < sizeof($fields); $i++
) {
169 $fie_info = $fields[$i];
170 $oldid = backup_todb($fie_info['#']['ID']['0']['#']);
172 $field -> dataid
= $new_data_id;
173 $field -> type
= backup_todb($fie_info['#']['TYPE']['0']['#']);
174 $field -> name
= backup_todb($fie_info['#']['NAME']['0']['#']);
175 $field -> description
= backup_todb($fie_info['#']['DESCRIPTION']['0']['#']);
176 $field -> param1
= backup_todb($fie_info['#']['PARAM1']['0']['#']);
177 $field -> param2
= backup_todb($fie_info['#']['PARAM2']['0']['#']);
178 $field -> param3
= backup_todb($fie_info['#']['PARAM3']['0']['#']);
179 $field -> param4
= backup_todb($fie_info['#']['PARAM4']['0']['#']);
180 $field -> param5
= backup_todb($fie_info['#']['PARAM5']['0']['#']);
181 $field -> param6
= backup_todb($fie_info['#']['PARAM6']['0']['#']);
182 $field -> param7
= backup_todb($fie_info['#']['PARAM7']['0']['#']);
183 $field -> param8
= backup_todb($fie_info['#']['PARAM8']['0']['#']);
184 $field -> param9
= backup_todb($fie_info['#']['PARAM9']['0']['#']);
185 $field -> param10
= backup_todb($fie_info['#']['PARAM10']['0']['#']);
187 $newid = insert_record ("data_fields",$field);
189 $fieldids[$oldid] = $newid; //so we can use them in sub tables that depends on both fieldid and recordid
192 if (($i+
1) %
50 == 0) {
193 if (!defined('RESTORE_SILENTLY')) {
195 if (($i+
1) %
1000 == 0) {
203 //We have the newid, update backup_ids
204 $status = backup_putid($restore->backup_unique_code
,"data_fields",$oldid, $newid);
214 function data_records_restore_mods ($old_data_id, $new_data_id, $info, $restore) {
216 global $CFG, $fieldids;
220 $records = $info['MOD']['#']['RECORDS']['0']['#']['RECORD'];
222 for ($i = 0; $i < sizeof($records); $i++
) {
224 $rec_info = $records[$i];
225 $oldid = backup_todb($rec_info['#']['ID']['0']['#']);
227 $record = new object();
228 $record -> dataid
= $new_data_id;
229 $record -> userid
= backup_todb($rec_info['#']['USERID']['0']['#']);
230 $record -> groupid
= backup_todb($rec_info['#']['GROUPID']['0']['#']);
231 $record -> timecreated
= backup_todb($rec_info['#']['TIMECREATED']['0']['#']);
232 $record -> timemodified
= backup_todb($rec_info['#']['TIMEMODIFIED']['0']['#']);
233 $record -> approved
= backup_todb($rec_info['#']['APPROVED']['0']['#']);
234 $user = backup_getid($restore->backup_unique_code
,"user",$record->userid
);
235 $group= backup_getid($restore->backup_unique_code
,"groups",$record->groupid
);
238 $record->userid
= $user->new_id
;
241 $record->groupid
= $group->new_id
;
244 $newid = insert_record ("data_records",$record);
247 if (($i+
1) %
50 == 0) {
248 if (!defined('RESTORE_SILENTLY')) {
250 if (($i+
1) %
1000 == 0) {
258 //We have the newid, update backup_ids
259 $status = $status and backup_putid($restore->backup_unique_code
,"data_records",$oldid, $newid);
261 $status = $status and data_content_restore_mods ($oldid, $newid, $old_data_id, $new_data_id, $rec_info, $restore);
262 $status = $status and data_ratings_restore_mods ($oldid, $newid, $info, $rec_info);
263 $status = $status and data_comments_restore_mods ($oldid, $newid, $info, $rec_info);
272 function data_content_restore_mods ($old_record_id, $new_record_id, $old_data_id, $new_data_id, $recinfo, $restore) {
274 global $CFG, $fieldids;
278 $contents = $recinfo['#']['CONTENTS']['0']['#']['CONTENT'];
280 for ($i = 0; $i < sizeof($contents); $i++
) {
282 $con_info = $contents[$i];
283 $oldid = backup_todb($con_info['#']['ID']['0']['#']);
284 $oldfieldid = backup_todb($con_info['#']['FIELDID']['0']['#']);
285 $oldrecordid = backup_todb($con_info['#']['RECORDID']['0']['#']);
287 $content -> recordid
= $new_record_id;
288 $content -> fieldid
= $fieldids[$oldfieldid];
289 $content -> content
= backup_todb($con_info['#']['CONTENT']['0']['#']);
290 $content -> content1
= backup_todb($con_info['#']['CONTENT1']['0']['#']);
291 $content -> content2
= backup_todb($con_info['#']['CONTENT2']['0']['#']);
292 $content -> content3
= backup_todb($con_info['#']['CONTENT3']['0']['#']);
293 $content -> content4
= backup_todb($con_info['#']['CONTENT4']['0']['#']);
294 $newid = insert_record ("data_content",$content);
297 if (($i+
1) %
50 == 0) {
298 if (!defined('RESTORE_SILENTLY')) {
300 if (($i+
1) %
1000 == 0) {
308 //We have the newid, update backup_ids
310 $status = $status and data_restore_files ($old_data_id, $new_data_id, $oldfieldid, $content->fieldid
, $oldrecordid, $content->recordid
, $recinfo, $restore);
311 $status = $status and backup_putid($restore->backup_unique_code
,"data_content",$oldid, $newid);
320 function data_restore_files ($old_data_id, $new_data_id, $old_field_id, $new_field_id, $old_record_id, $new_record_id, $recinfo, $restore) {
330 //First, we check to "course_id" exists and create is as necessary
332 $dest_dir = $CFG->dataroot
."/".$restore->course_id
;
333 $status = check_dir_exists($dest_dir,true);
335 //Now, locate course's moddata directory
336 $moddata_path = $CFG->dataroot
."/".$restore->course_id
."/".$CFG->moddata
;
338 //Check it exists and create it
339 $status = check_dir_exists($moddata_path,true);
341 //Now, locate data directory
343 $data_path = $moddata_path."/data";
344 //Check it exists and create it
345 $status = check_dir_exists($data_path,true);
348 //Now locate the temp dir we are gong to restore
350 $temp_path = $CFG->dataroot
."/temp/backup/".$restore->backup_unique_code
.
351 "/moddata/data/".$old_data_id."/".$old_field_id."/".$old_record_id;
352 $todo = check_dir_exists($temp_path);
355 //If todo, we create the neccesary dirs in course moddata/data
356 if ($status and $todo) {
358 $this_data_path = $data_path."/".$new_data_id;
360 $status = check_dir_exists($this_data_path,true);
362 $this_field_path = $this_data_path."/".$new_field_id;
363 $status = check_dir_exists($this_field_path,true);
364 $this_record_path = $this_field_path = $this_field_path."/".$new_record_id;
365 $status = check_dir_exists($this_record_path,true);
366 //And now, copy temp_path to user_data_path
368 $status = @backup_copy_file
($temp_path, $this_record_path);
374 function data_ratings_restore_mods ($oldid, $newid, $info, $rec_info) {
380 $ratings= $rec_info['#']['RATINGS']['0']['#']['RATING'];
382 if (empty($ratings)) { // no ratings to restore
385 for ($i = 0; $i < sizeof($ratings); $i++
) {
387 $rat_info = $ratings[$i];
389 $rating -> recordid
= $newid;
390 $rating -> userid
= backup_todb($rat_info['#']['USERID']['0']['#']);
391 $rating -> rating
= backup_todb($rat_info['#']['RATING']['0']['#']);
393 if (! insert_record ("data_ratings",$rating)) {
400 function data_comments_restore_mods ($oldid, $newid, $info, $rec_info) {
406 $comments= $rec_info['#']['COMMENTS']['0']['#']['COMMENT'];
408 if (empty($comments)) { // no comments to restore
412 for ($i = 0; $i < sizeof($comments); $i++
) {
414 $com_info = $comments[$i];
416 $comment -> recordid
= $newid;
417 $comment -> userid
= backup_todb($com_info['#']['USERID']['0']['#']);
418 $comment -> content
= backup_todb($com_info['#']['CONTENT']['0']['#']);
419 $comment -> created
= backup_todb($com_info['#']['CREATED']['0']['#']);
420 $comment -> modified
= backup_todb($com_info['#']['MODIFIED']['0']['#']);
421 if (! insert_record ("data_comments",$comment)) {