Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / mod / chat / restorelib.php
blob883ff4289b9693d6cfb7e7d8f747b3d0ec7075f1
1 <?php //$Id$
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)
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)
22 //-----------------------------------------------------------
24 //This function executes all the restore procedure about this mod
25 function chat_restore_mods($mod,$restore) {
27 global $CFG;
29 $status = true;
31 //Get record from backup_ids
32 $data = backup_getid($restore->backup_unique_code,$mod->modtype,$mod->id);
34 if ($data) {
35 //Now get completed xmlized object
36 $info = $data->info;
38 //traverse_xmlize($info); //Debug
39 //print_object ($GLOBALS['traverse_array']); //Debug
40 //$GLOBALS['traverse_array']=""; //Debug
41 // if necessary, write to restorelog and adjust date/time fields
42 if ($restore->course_startdateoffset) {
43 restore_log_date_changes('Chat', $restore, $info['MOD']['#'], array('CHATTIME'));
45 //Now, build the CHAT record structure
46 $chat->course = $restore->course_id;
47 $chat->name = backup_todb($info['MOD']['#']['NAME']['0']['#']);
48 $chat->intro = backup_todb($info['MOD']['#']['INTRO']['0']['#']);
49 $chat->keepdays = backup_todb($info['MOD']['#']['KEEPDAYS']['0']['#']);
50 $chat->studentlogs = backup_todb($info['MOD']['#']['STUDENTLOGS']['0']['#']);
51 $chat->schedule = backup_todb($info['MOD']['#']['SCHEDULE']['0']['#']);
52 $chat->chattime = backup_todb($info['MOD']['#']['CHATTIME']['0']['#']);
53 $chat->timemodified = backup_todb($info['MOD']['#']['TIMEMODIFIED']['0']['#']);
55 //The structure is equal to the db, so insert the chat
56 $newid = insert_record ("chat",$chat);
58 //Do some output
59 if (!defined('RESTORE_SILENTLY')) {
60 echo "<li>".get_string("modulename","chat")." \"".format_string(stripslashes($chat->name),true)."\"</li>";
62 backup_flush(300);
64 if ($newid) {
65 //We have the newid, update backup_ids
66 backup_putid($restore->backup_unique_code,$mod->modtype,
67 $mod->id, $newid);
68 //Now check if want to restore user data and do it.
69 if (restore_userdata_selected($restore,'chat',$mod->id)) {
70 //Restore chat_messages
71 $status = chat_messages_restore_mods ($mod->id, $newid,$info,$restore);
73 } else {
74 $status = false;
76 } else {
77 $status = false;
80 return $status;
83 //This function restores the chat_messages
84 function chat_messages_restore_mods($old_chat_id, $new_chat_id,$info,$restore) {
86 global $CFG;
88 $status = true;
90 //Get the messages array
91 $messages = $info['MOD']['#']['MESSAGES']['0']['#']['MESSAGE'];
93 //Iterate over messages
94 for($i = 0; $i < sizeof($messages); $i++) {
95 $mes_info = $messages[$i];
96 //traverse_xmlize($mes_info); //Debug
97 //print_object ($GLOBALS['traverse_array']); //Debug
98 //$GLOBALS['traverse_array']=""; //Debug
100 //We'll need this later!!
101 $oldid = backup_todb($mes_info['#']['ID']['0']['#']);
102 $olduserid = backup_todb($mes_info['#']['USERID']['0']['#']);
104 //Now, build the CHAT_MESSAGES record structure
105 $message = new object();
106 $message->chatid = $new_chat_id;
107 $message->userid = backup_todb($mes_info['#']['USERID']['0']['#']);
108 $message->groupid = backup_todb($mes_info['#']['GROUPID']['0']['#']);
109 $message->system = backup_todb($mes_info['#']['SYSTEM']['0']['#']);
110 $message->message = backup_todb($mes_info['#']['MESSAGE_TEXT']['0']['#']);
111 $message->timestamp = backup_todb($mes_info['#']['TIMESTAMP']['0']['#']);
113 //We have to recode the userid field
114 $user = backup_getid($restore->backup_unique_code,"user",$message->userid);
115 if ($user) {
116 $message->userid = $user->new_id;
119 //We have to recode the groupid field
120 $group = restore_group_getid($restore, $message->groupid);
121 if ($group) {
122 $message->groupid = $group->new_id;
125 //The structure is equal to the db, so insert the chat_message
126 $newid = insert_record ("chat_messages",$message);
128 //Do some output
129 if (($i+1) % 50 == 0) {
130 if (!defined('RESTORE_SILENTLY')) {
131 echo ".";
132 if (($i+1) % 1000 == 0) {
133 echo "<br />";
136 backup_flush(300);
139 return $status;
142 //Return a content decoded to support interactivities linking. Every module
143 //should have its own. They are called automatically from
144 //chat_decode_content_links_caller() function in each module
145 //in the restore process
146 function chat_decode_content_links ($content,$restore) {
148 global $CFG;
150 $result = $content;
152 //Link to the list of chats
154 $searchstring='/\$@(CHATINDEX)\*([0-9]+)@\$/';
155 //We look for it
156 preg_match_all($searchstring,$content,$foundset);
157 //If found, then we are going to look for its new id (in backup tables)
158 if ($foundset[0]) {
159 //print_object($foundset); //Debug
160 //Iterate over foundset[2]. They are the old_ids
161 foreach($foundset[2] as $old_id) {
162 //We get the needed variables here (course id)
163 $rec = backup_getid($restore->backup_unique_code,"course",$old_id);
164 //Personalize the searchstring
165 $searchstring='/\$@(CHATINDEX)\*('.$old_id.')@\$/';
166 //If it is a link to this course, update the link to its new location
167 if($rec->new_id) {
168 //Now replace it
169 $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/chat/index.php?id='.$rec->new_id,$result);
170 } else {
171 //It's a foreign link so leave it as original
172 $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/chat/index.php?id='.$old_id,$result);
177 //Link to chat view by moduleid
179 $searchstring='/\$@(CHATVIEWBYID)\*([0-9]+)@\$/';
180 //We look for it
181 preg_match_all($searchstring,$result,$foundset);
182 //If found, then we are going to look for its new id (in backup tables)
183 if ($foundset[0]) {
184 //print_object($foundset); //Debug
185 //Iterate over foundset[2]. They are the old_ids
186 foreach($foundset[2] as $old_id) {
187 //We get the needed variables here (course_modules id)
188 $rec = backup_getid($restore->backup_unique_code,"course_modules",$old_id);
189 //Personalize the searchstring
190 $searchstring='/\$@(CHATVIEWBYID)\*('.$old_id.')@\$/';
191 //If it is a link to this course, update the link to its new location
192 if($rec->new_id) {
193 //Now replace it
194 $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/chat/view.php?id='.$rec->new_id,$result);
195 } else {
196 //It's a foreign link so leave it as original
197 $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/chat/view.php?id='.$old_id,$result);
202 return $result;
205 //This function makes all the necessary calls to xxxx_decode_content_links()
206 //function in each module, passing them the desired contents to be decoded
207 //from backup format to destination site/course in order to mantain inter-activities
208 //working in the backup/restore process. It's called from restore_decode_content_links()
209 //function in restore process
210 function chat_decode_content_links_caller($restore) {
211 global $CFG;
212 $status = true;
214 if ($chats = get_records_sql ("SELECT c.id, c.intro
215 FROM {$CFG->prefix}chat c
216 WHERE c.course = $restore->course_id")) {
217 //Iterate over each chat->intro
218 $i = 0; //Counter to send some output to the browser to avoid timeouts
219 foreach ($chats as $chat) {
220 //Increment counter
221 $i++;
222 $content = $chat->intro;
223 $result = restore_decode_content_links_worker($content,$restore);
224 if ($result != $content) {
225 //Update record
226 $chat->intro = addslashes($result);
227 $status = update_record("chat",$chat);
228 if (debugging()) {
229 if (!defined('RESTORE_SILENTLY')) {
230 echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
234 //Do some output
235 if (($i+1) % 5 == 0) {
236 if (!defined('RESTORE_SILENTLY')) {
237 echo ".";
238 if (($i+1) % 100 == 0) {
239 echo "<br />";
242 backup_flush(300);
247 return $status;
250 //This function returns a log record with all the necessay transformations
251 //done. It's used by restore_log_module() to restore modules log.
252 function chat_restore_logs($restore,$log) {
254 $status = false;
256 //Depending of the action, we recode different things
257 switch ($log->action) {
258 case "add":
259 if ($log->cmid) {
260 //Get the new_id of the module (to recode the info field)
261 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
262 if ($mod) {
263 $log->url = "view.php?id=".$log->cmid;
264 $log->info = $mod->new_id;
265 $status = true;
268 break;
269 case "update":
270 if ($log->cmid) {
271 //Get the new_id of the module (to recode the info field)
272 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
273 if ($mod) {
274 $log->url = "view.php?id=".$log->cmid;
275 $log->info = $mod->new_id;
276 $status = true;
279 break;
280 case "talk":
281 if ($log->cmid) {
282 //Get the new_id of the module (to recode the info field)
283 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
284 if ($mod) {
285 $log->url = "view.php?id=".$log->cmid;
286 $log->info = $mod->new_id;
287 $status = true;
290 break;
291 case "view":
292 if ($log->cmid) {
293 //Get the new_id of the module (to recode the info field)
294 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
295 if ($mod) {
296 $log->url = "view.php?id=".$log->cmid;
297 $log->info = $mod->new_id;
298 $status = true;
301 break;
302 case "view all":
303 $log->url = "index.php?id=".$log->course;
304 $status = true;
305 break;
306 case "report":
307 if ($log->cmid) {
308 //Get the new_id of the module (to recode the info field)
309 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
310 if ($mod) {
311 $log->url = "report.php?id=".$log->cmid;
312 $log->info = $mod->new_id;
313 $status = true;
316 break;
317 default:
318 if (!defined('RESTORE_SILENTLY')) {
319 echo "action (".$log->module."-".$log->action.") unknown. Not restored<br />"; //Debug
321 break;
324 if ($status) {
325 $status = $log;
327 return $status;