2 //This php script contains all the stuff to backup/restore
5 //This is the "graphical" structure of the chat mod:
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 backup procedure about this mod
25 function chat_backup_mods($bf,$preferences) {
31 //Iterate over chat table
32 $chats = get_records ("chat","course",$preferences->backup_course
,"id");
34 foreach ($chats as $chat) {
35 if (backup_mod_selected($preferences,'chat',$chat->id
)) {
36 $status = chat_backup_one_mod($bf,$preferences,$chat);
43 function chat_backup_one_mod($bf,$preferences,$chat) {
47 if (is_numeric($chat)) {
48 $chat = get_record('chat','id',$chat);
54 fwrite ($bf,start_tag("MOD",3,true));
56 fwrite ($bf,full_tag("ID",4,false,$chat->id
));
57 fwrite ($bf,full_tag("MODTYPE",4,false,"chat"));
58 fwrite ($bf,full_tag("NAME",4,false,$chat->name
));
59 fwrite ($bf,full_tag("INTRO",4,false,$chat->intro
));
60 fwrite ($bf,full_tag("KEEPDAYS",4,false,$chat->keepdays
));
61 fwrite ($bf,full_tag("STUDENTLOGS",4,false,$chat->studentlogs
));
62 fwrite ($bf,full_tag("SCHEDULE",4,false,$chat->schedule
));
63 fwrite ($bf,full_tag("CHATTIME",4,false,$chat->chattime
));
64 fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$chat->timemodified
));
65 //if we've selected to backup users info, then execute backup_chat_messages
66 if (backup_userdata_selected($preferences,'chat',$chat->id
)) {
67 $status = backup_chat_messages($bf,$preferences,$chat->id
);
70 $status =fwrite ($bf,end_tag("MOD",3,true));
75 //Backup chat_messages contents (executed from chat_backup_mods)
76 function backup_chat_messages ($bf,$preferences,$chat) {
82 $chat_messages = get_records("chat_messages","chatid",$chat,"id");
83 //If there is messages
86 $status =fwrite ($bf,start_tag("MESSAGES",4,true));
87 //Iterate over each message
88 foreach ($chat_messages as $cha_mes) {
90 $status =fwrite ($bf,start_tag("MESSAGE",5,true));
91 //Print message contents
92 fwrite ($bf,full_tag("ID",6,false,$cha_mes->id
));
93 fwrite ($bf,full_tag("USERID",6,false,$cha_mes->userid
));
94 fwrite ($bf,full_tag("GROUPID",6,false,$cha_mes->groupid
));
95 fwrite ($bf,full_tag("SYSTEM",6,false,$cha_mes->system
));
96 fwrite ($bf,full_tag("MESSAGE_TEXT",6,false,$cha_mes->message
));
97 fwrite ($bf,full_tag("TIMESTAMP",6,false,$cha_mes->timestamp
));
99 $status =fwrite ($bf,end_tag("MESSAGE",5,true));
102 $status =fwrite ($bf,end_tag("MESSAGES",4,true));
107 //Return an array of info (name,value)
108 function chat_check_backup_mods($course,$user_data=false,$backup_unique_code,$instances=null) {
110 if (!empty($instances) && is_array($instances) && count($instances)) {
112 foreach ($instances as $id => $instance) {
113 $info +
= chat_check_backup_mods_instances($instance,$backup_unique_code);
117 //First the course data
118 $info[0][0] = get_string("modulenameplural","chat");
119 if ($ids = chat_ids ($course)) {
120 $info[0][1] = count($ids);
125 //Now, if requested, the user_data
127 $info[1][0] = get_string("messages","chat");
128 if ($ids = chat_message_ids_by_course ($course)) {
129 $info[1][1] = count($ids);
137 //Return an array of info (name,value)
138 function chat_check_backup_mods_instances($instance,$backup_unique_code) {
139 //First the course data
140 $info[$instance->id
.'0'][0] = '<b>'.$instance->name
.'</b>';
141 $info[$instance->id
.'0'][1] = '';
143 //Now, if requested, the user_data
144 if (!empty($instance->userdata
)) {
145 $info[$instance->id
.'1'][0] = get_string("messages","chat");
146 if ($ids = chat_message_ids_by_instance ($instance->id
)) {
147 $info[$instance->id
.'1'][1] = count($ids);
149 $info[$instance->id
.'1'][1] = 0;
155 //Return a content encoded to support interactivities linking. Every module
156 //should have its own. They are called automatically from the backup procedure.
157 function chat_encode_content_links ($content,$preferences) {
161 $base = preg_quote($CFG->wwwroot
,"/");
163 //Link to the list of chats
164 $buscar="/(".$base."\/mod\/chat\/index.php\?id\=)([0-9]+)/";
165 $result= preg_replace($buscar,'$@CHATINDEX*$2@$',$content);
167 //Link to chat view by moduleid
168 $buscar="/(".$base."\/mod\/chat\/view.php\?id\=)([0-9]+)/";
169 $result= preg_replace($buscar,'$@CHATVIEWBYID*$2@$',$result);
174 // INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE
176 //Returns an array of chats id
177 function chat_ids ($course) {
181 return get_records_sql ("SELECT c.id, c.course
182 FROM {$CFG->prefix}chat c
183 WHERE c.course = '$course'");
186 //Returns an array of assignment_submissions id
187 function chat_message_ids_by_course ($course) {
191 return get_records_sql ("SELECT m.id , m.chatid
192 FROM {$CFG->prefix}chat_messages m,
194 WHERE c.course = '$course' AND
198 //Returns an array of chat id
199 function chat_message_ids_by_instance ($instanceid) {
203 return get_records_sql ("SELECT m.id , m.chatid
204 FROM {$CFG->prefix}chat_messages m
205 WHERE m.chatid = $instanceid");