MDL-20700 coding style cleanup - cvs keywords removed, closign php tag removed, trail...
[moodle.git] / mod / chat / restorelib.php
1 <?php
2     //This php script contains all the stuff to backup/restore
3     //chat mods
5     //This is the "graphical" structure of the chat mod:
6     //
7     //                       chat
8     //                    (CL,pk->id)
9     //                        |
10     //                        |
11     //                        |
12     //                    chat_messages
13     //                (UL,pk->id, fk->chatid)
14     //
15     // Meaning: pk->primary key field of the table
16     //          fk->foreign key to link with parent
17     //          nt->nested field (recursive data)
18     //          CL->course level info
19     //          UL->user level info
20     //          files->table may have files)
21     //
22     //-----------------------------------------------------------
24     //This function executes all the restore procedure about this mod
25     function chat_restore_mods($mod,$restore) {
26         global $CFG, $DB;
28         $status = true;
30         //Get record from backup_ids
31         $data = backup_getid($restore->backup_unique_code,$mod->modtype,$mod->id);
33         if ($data) {
34             //Now get completed xmlized object
35             $info = $data->info;
37             //traverse_xmlize($info);                                                                     //Debug
38             //print_object ($GLOBALS['traverse_array']);                                                  //Debug
39             //$GLOBALS['traverse_array']="";                                                              //Debug
40             // if necessary, write to restorelog and adjust date/time fields
41             if ($restore->course_startdateoffset) {
42                 restore_log_date_changes('Chat', $restore, $info['MOD']['#'], array('CHATTIME'));
43             }
44             //Now, build the CHAT record structure
45             $chat->course = $restore->course_id;
46             $chat->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
47             $chat->intro = backup_todb($info['MOD']['#']['INTRO']['0']['#']);
48             $chat->keepdays = backup_todb($info['MOD']['#']['KEEPDAYS']['0']['#']);
49             $chat->studentlogs = backup_todb($info['MOD']['#']['STUDENTLOGS']['0']['#']);
50             $chat->schedule = backup_todb($info['MOD']['#']['SCHEDULE']['0']['#']);
51             $chat->chattime = backup_todb($info['MOD']['#']['CHATTIME']['0']['#']);
52             $chat->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
54             //The structure is equal to the db, so insert the chat
55             $newid = $DB->insert_record ("chat",$chat);
57             //Do some output
58             if (!defined('RESTORE_SILENTLY')) {
59                 echo "<li>".get_string("modulename","chat")." \"".format_string($chat->name,true)."\"</li>";
60             }
61             backup_flush(300);
63             if ($newid) {
64                 //We have the newid, update backup_ids
65                 backup_putid($restore->backup_unique_code,$mod->modtype,
66                              $mod->id, $newid);
67                 //Now check if want to restore user data and do it.
68                 if (restore_userdata_selected($restore,'chat',$mod->id)) {
69                     //Restore chat_messages
70                     $status = chat_messages_restore_mods ($mod->id, $newid,$info,$restore);
71                 }
72             } else {
73                 $status = false;
74             }
75         } else {
76             $status = false;
77         }
79         return $status;
80     }
82     //This function restores the chat_messages
83     function chat_messages_restore_mods($old_chat_id, $new_chat_id,$info,$restore) {
84         global $CFG, $DB;
86         $status = true;
88         //Get the messages array
89         $messages = $info['MOD']['#']['MESSAGES']['0']['#']['MESSAGE'];
91         //Iterate over messages
92         for($i = 0; $i < sizeof($messages); $i++) {
93             $mes_info = $messages[$i];
94             //traverse_xmlize($mes_info);                                                                 //Debug
95             //print_object ($GLOBALS['traverse_array']);                                                  //Debug
96             //$GLOBALS['traverse_array']="";                                                              //Debug
98             //We'll need this later!!
99             $oldid = backup_todb($mes_info['#']['ID']['0']['#']);
100             $olduserid = backup_todb($mes_info['#']['USERID']['0']['#']);
102             //Now, build the CHAT_MESSAGES record structure
103             $message = new object();
104             $message->chatid = $new_chat_id;
105             $message->userid = backup_todb($mes_info['#']['USERID']['0']['#']);
106             $message->groupid = backup_todb($mes_info['#']['GROUPID']['0']['#']);
107             $message->system = backup_todb($mes_info['#']['SYSTEM']['0']['#']);
108             $message->message = backup_todb($mes_info['#']['MESSAGE_TEXT']['0']['#']);
109             $message->timestamp = backup_todb($mes_info['#']['TIMESTAMP']['0']['#']);
111             //We have to recode the userid field
112             $user = backup_getid($restore->backup_unique_code,"user",$message->userid);
113             if ($user) {
114                 $message->userid = $user->new_id;
115             }
117             //We have to recode the groupid field
118             $group = restore_group_getid($restore, $message->groupid);
119             if ($group) {
120                 $message->groupid = $group->new_id;
121             }
123             //The structure is equal to the db, so insert the chat_message
124             $newid = $DB->insert_record ("chat_messages",$message);
126             //Do some output
127             if (($i+1) % 50 == 0) {
128                 if (!defined('RESTORE_SILENTLY')) {
129                     echo ".";
130                     if (($i+1) % 1000 == 0) {
131                         echo "<br />";
132                     }
133                 }
134                 backup_flush(300);
135             }
136         }
137         return $status;
138     }
140     //Return a content decoded to support interactivities linking. Every module
141     //should have its own. They are called automatically from
142     //chat_decode_content_links_caller() function in each module
143     //in the restore process
144     function chat_decode_content_links ($content,$restore) {
146         global $CFG;
148         $result = $content;
150         //Link to the list of chats
152         $searchstring='/\$@(CHATINDEX)\*([0-9]+)@\$/';
153         //We look for it
154         preg_match_all($searchstring,$content,$foundset);
155         //If found, then we are going to look for its new id (in backup tables)
156         if ($foundset[0]) {
157             //print_object($foundset);                                     //Debug
158             //Iterate over foundset[2]. They are the old_ids
159             foreach($foundset[2] as $old_id) {
160                 //We get the needed variables here (course id)
161                 $rec = backup_getid($restore->backup_unique_code,"course",$old_id);
162                 //Personalize the searchstring
163                 $searchstring='/\$@(CHATINDEX)\*('.$old_id.')@\$/';
164                 //If it is a link to this course, update the link to its new location
165                 if($rec->new_id) {
166                     //Now replace it
167                     $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/chat/index.php?id='.$rec->new_id,$result);
168                 } else {
169                     //It's a foreign link so leave it as original
170                     $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/chat/index.php?id='.$old_id,$result);
171                 }
172             }
173         }
175         //Link to chat view by moduleid
177         $searchstring='/\$@(CHATVIEWBYID)\*([0-9]+)@\$/';
178         //We look for it
179         preg_match_all($searchstring,$result,$foundset);
180         //If found, then we are going to look for its new id (in backup tables)
181         if ($foundset[0]) {
182             //print_object($foundset);                                     //Debug
183             //Iterate over foundset[2]. They are the old_ids
184             foreach($foundset[2] as $old_id) {
185                 //We get the needed variables here (course_modules id)
186                 $rec = backup_getid($restore->backup_unique_code,"course_modules",$old_id);
187                 //Personalize the searchstring
188                 $searchstring='/\$@(CHATVIEWBYID)\*('.$old_id.')@\$/';
189                 //If it is a link to this course, update the link to its new location
190                 if($rec->new_id) {
191                     //Now replace it
192                     $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/chat/view.php?id='.$rec->new_id,$result);
193                 } else {
194                     //It's a foreign link so leave it as original
195                     $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/chat/view.php?id='.$old_id,$result);
196                 }
197             }
198         }
200         return $result;
201     }
203     //This function makes all the necessary calls to xxxx_decode_content_links()
204     //function in each module, passing them the desired contents to be decoded
205     //from backup format to destination site/course in order to mantain inter-activities
206     //working in the backup/restore process. It's called from restore_decode_content_links()
207     //function in restore process
208     function chat_decode_content_links_caller($restore) {
209         global $CFG, $DB;
210         $status = true;
212         if ($chats = $DB->get_records('chat', array('course'=>$restore->course_id), '', "id,intro")) {
213                                                //Iterate over each chat->intro
214             $i = 0;   //Counter to send some output to the browser to avoid timeouts
215             foreach ($chats as $chat) {
216                 //Increment counter
217                 $i++;
218                 $content = $chat->intro;
219                 $result = restore_decode_content_links_worker($content,$restore);
220                 if ($result != $content) {
221                     //Update record
222                     $chat->intro = $result;
223                     $status = $DB->update_record("chat",$chat);
224                     if (debugging()) {
225                         if (!defined('RESTORE_SILENTLY')) {
226                             echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
227                         }
228                     }
229                 }
230                 //Do some output
231                 if (($i+1) % 5 == 0) {
232                     if (!defined('RESTORE_SILENTLY')) {
233                         echo ".";
234                         if (($i+1) % 100 == 0) {
235                             echo "<br />";
236                         }
237                     }
238                     backup_flush(300);
239                 }
240             }
241         }
243         return $status;
244     }
246     //This function returns a log record with all the necessay transformations
247     //done. It's used by restore_log_module() to restore modules log.
248     function chat_restore_logs($restore,$log) {
249         $status = false;
251         //Depending of the action, we recode different things
252         switch ($log->action) {
253         case "add":
254             if ($log->cmid) {
255                 //Get the new_id of the module (to recode the info field)
256                 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
257                 if ($mod) {
258                     $log->url = "view.php?id=".$log->cmid;
259                     $log->info = $mod->new_id;
260                     $status = true;
261                 }
262             }
263             break;
264         case "update":
265             if ($log->cmid) {
266                 //Get the new_id of the module (to recode the info field)
267                 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
268                 if ($mod) {
269                     $log->url = "view.php?id=".$log->cmid;
270                     $log->info = $mod->new_id;
271                     $status = true;
272                 }
273             }
274             break;
275         case "talk":
276             if ($log->cmid) {
277                 //Get the new_id of the module (to recode the info field)
278                 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
279                 if ($mod) {
280                     $log->url = "view.php?id=".$log->cmid;
281                     $log->info = $mod->new_id;
282                     $status = true;
283                 }
284             }
285             break;
286         case "view":
287             if ($log->cmid) {
288                 //Get the new_id of the module (to recode the info field)
289                 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
290                 if ($mod) {
291                     $log->url = "view.php?id=".$log->cmid;
292                     $log->info = $mod->new_id;
293                     $status = true;
294                 }
295             }
296             break;
297         case "view all":
298             $log->url = "index.php?id=".$log->course;
299             $status = true;
300             break;
301         case "report":
302             if ($log->cmid) {
303                 //Get the new_id of the module (to recode the info field)
304                 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
305                 if ($mod) {
306                     $log->url = "report.php?id=".$log->cmid;
307                     $log->info = $mod->new_id;
308                     $status = true;
309                 }
310             }
311             break;
312         default:
313             if (!defined('RESTORE_SILENTLY')) {
314                 echo "action (".$log->module."-".$log->action.") unknown. Not restored<br />";                 //Debug
315             }
316             break;
317         }
319         if ($status) {
320             $status = $log;
321         }
322         return $status;
323     }