adding some strings
[moodle-linuxchix.git] / mod / chat / restorelib.php
blob67fe78451b848cf6e085edb7cbb970b5dd0b4e67
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->chatid = $new_chat_id;
106 $message->userid = backup_todb($mes_info['#']['USERID']['0']['#']);
107 $message->groupid = backup_todb($mes_info['#']['GROUPID']['0']['#']);
108 $message->system = backup_todb($mes_info['#']['SYSTEM']['0']['#']);
109 $message->message = backup_todb($mes_info['#']['MESSAGE_TEXT']['0']['#']);
110 $message->timestamp = backup_todb($mes_info['#']['TIMESTAMP']['0']['#']);
112 //We have to recode the userid field
113 $user = backup_getid($restore->backup_unique_code,"user",$message->userid);
114 if ($user) {
115 $message->userid = $user->new_id;
118 //We have to recode the groupid field
119 $group = backup_getid($restore->backup_unique_code, 'groups', $message->groupid);
120 if ($group) {
121 $message->groupid = $group->new_id;
124 //The structure is equal to the db, so insert the chat_message
125 $newid = insert_record ("chat_messages",$message);
127 //Do some output
128 if (($i+1) % 50 == 0) {
129 if (!defined('RESTORE_SILENTLY')) {
130 echo ".";
131 if (($i+1) % 1000 == 0) {
132 echo "<br />";
135 backup_flush(300);
138 return $status;
141 //Return a content decoded to support interactivities linking. Every module
142 //should have its own. They are called automatically from
143 //chat_decode_content_links_caller() function in each module
144 //in the restore process
145 function chat_decode_content_links ($content,$restore) {
147 global $CFG;
149 $result = $content;
151 //Link to the list of chats
153 $searchstring='/\$@(CHATINDEX)\*([0-9]+)@\$/';
154 //We look for it
155 preg_match_all($searchstring,$content,$foundset);
156 //If found, then we are going to look for its new id (in backup tables)
157 if ($foundset[0]) {
158 //print_object($foundset); //Debug
159 //Iterate over foundset[2]. They are the old_ids
160 foreach($foundset[2] as $old_id) {
161 //We get the needed variables here (course id)
162 $rec = backup_getid($restore->backup_unique_code,"course",$old_id);
163 //Personalize the searchstring
164 $searchstring='/\$@(CHATINDEX)\*('.$old_id.')@\$/';
165 //If it is a link to this course, update the link to its new location
166 if($rec->new_id) {
167 //Now replace it
168 $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/chat/index.php?id='.$rec->new_id,$result);
169 } else {
170 //It's a foreign link so leave it as original
171 $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/chat/index.php?id='.$old_id,$result);
176 //Link to chat view by moduleid
178 $searchstring='/\$@(CHATVIEWBYID)\*([0-9]+)@\$/';
179 //We look for it
180 preg_match_all($searchstring,$result,$foundset);
181 //If found, then we are going to look for its new id (in backup tables)
182 if ($foundset[0]) {
183 //print_object($foundset); //Debug
184 //Iterate over foundset[2]. They are the old_ids
185 foreach($foundset[2] as $old_id) {
186 //We get the needed variables here (course_modules id)
187 $rec = backup_getid($restore->backup_unique_code,"course_modules",$old_id);
188 //Personalize the searchstring
189 $searchstring='/\$@(CHATVIEWBYID)\*('.$old_id.')@\$/';
190 //If it is a link to this course, update the link to its new location
191 if($rec->new_id) {
192 //Now replace it
193 $result= preg_replace($searchstring,$CFG->wwwroot.'/mod/chat/view.php?id='.$rec->new_id,$result);
194 } else {
195 //It's a foreign link so leave it as original
196 $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/chat/view.php?id='.$old_id,$result);
201 return $result;
204 //This function makes all the necessary calls to xxxx_decode_content_links()
205 //function in each module, passing them the desired contents to be decoded
206 //from backup format to destination site/course in order to mantain inter-activities
207 //working in the backup/restore process. It's called from restore_decode_content_links()
208 //function in restore process
209 function chat_decode_content_links_caller($restore) {
210 global $CFG;
211 $status = true;
213 if ($chats = get_records_sql ("SELECT c.id, c.intro
214 FROM {$CFG->prefix}chat c
215 WHERE c.course = $restore->course_id")) {
216 //Iterate over each chat->intro
217 $i = 0; //Counter to send some output to the browser to avoid timeouts
218 foreach ($chats as $chat) {
219 //Increment counter
220 $i++;
221 $content = $chat->intro;
222 $result = restore_decode_content_links_worker($content,$restore);
223 if ($result != $content) {
224 //Update record
225 $chat->intro = addslashes($result);
226 $status = update_record("chat",$chat);
227 if (debugging()) {
228 if (!defined('RESTORE_SILENTLY')) {
229 echo '<br /><hr />'.s($content).'<br />changed to<br />'.s($result).'<hr /><br />';
233 //Do some output
234 if (($i+1) % 5 == 0) {
235 if (!defined('RESTORE_SILENTLY')) {
236 echo ".";
237 if (($i+1) % 100 == 0) {
238 echo "<br />";
241 backup_flush(300);
246 return $status;
249 //This function returns a log record with all the necessay transformations
250 //done. It's used by restore_log_module() to restore modules log.
251 function chat_restore_logs($restore,$log) {
253 $status = false;
255 //Depending of the action, we recode different things
256 switch ($log->action) {
257 case "add":
258 if ($log->cmid) {
259 //Get the new_id of the module (to recode the info field)
260 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
261 if ($mod) {
262 $log->url = "view.php?id=".$log->cmid;
263 $log->info = $mod->new_id;
264 $status = true;
267 break;
268 case "update":
269 if ($log->cmid) {
270 //Get the new_id of the module (to recode the info field)
271 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
272 if ($mod) {
273 $log->url = "view.php?id=".$log->cmid;
274 $log->info = $mod->new_id;
275 $status = true;
278 break;
279 case "talk":
280 if ($log->cmid) {
281 //Get the new_id of the module (to recode the info field)
282 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
283 if ($mod) {
284 $log->url = "view.php?id=".$log->cmid;
285 $log->info = $mod->new_id;
286 $status = true;
289 break;
290 case "view":
291 if ($log->cmid) {
292 //Get the new_id of the module (to recode the info field)
293 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
294 if ($mod) {
295 $log->url = "view.php?id=".$log->cmid;
296 $log->info = $mod->new_id;
297 $status = true;
300 break;
301 case "view all":
302 $log->url = "index.php?id=".$log->course;
303 $status = true;
304 break;
305 case "report":
306 if ($log->cmid) {
307 //Get the new_id of the module (to recode the info field)
308 $mod = backup_getid($restore->backup_unique_code,$log->module,$log->info);
309 if ($mod) {
310 $log->url = "report.php?id=".$log->cmid;
311 $log->info = $mod->new_id;
312 $status = true;
315 break;
316 default:
317 if (!defined('RESTORE_SILENTLY')) {
318 echo "action (".$log->module."-".$log->action.") unknown. Not restored<br />"; //Debug
320 break;
323 if ($status) {
324 $status = $log;
326 return $status;