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) {
30 //Iterate over chat table
31 $chats = $DB->get_records ("chat", array("course"=>$preferences->backup_course), "id");
33 foreach ($chats as $chat) {
34 if (backup_mod_selected($preferences,'chat',$chat->id)) {
35 $status = chat_backup_one_mod($bf,$preferences,$chat);
42 function chat_backup_one_mod($bf,$preferences,$chat) {
45 if (is_numeric($chat)) {
46 $chat = $DB->get_record('chat', array('id'=>$chat));
52 fwrite ($bf,start_tag("MOD",3,true));
54 fwrite ($bf,full_tag("ID",4,false,$chat->id));
55 fwrite ($bf,full_tag("MODTYPE",4,false,"chat"));
56 fwrite ($bf,full_tag("NAME",4,false,$chat->name));
57 fwrite ($bf,full_tag("INTRO",4,false,$chat->intro));
58 fwrite ($bf,full_tag("KEEPDAYS",4,false,$chat->keepdays));
59 fwrite ($bf,full_tag("STUDENTLOGS",4,false,$chat->studentlogs));
60 fwrite ($bf,full_tag("SCHEDULE",4,false,$chat->schedule));
61 fwrite ($bf,full_tag("CHATTIME",4,false,$chat->chattime));
62 fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$chat->timemodified));
63 //if we've selected to backup users info, then execute backup_chat_messages
64 if (backup_userdata_selected($preferences,'chat',$chat->id)) {
65 $status = backup_chat_messages($bf,$preferences,$chat->id);
68 $status =fwrite ($bf,end_tag("MOD",3,true));
73 //Backup chat_messages contents (executed from chat_backup_mods)
74 function backup_chat_messages ($bf,$preferences,$chat) {
79 $chat_messages = $DB->get_records("chat_messages", array("chatid"=>$chat), "id");
80 //If there is messages
83 $status =fwrite ($bf,start_tag("MESSAGES",4,true));
84 //Iterate over each message
85 foreach ($chat_messages as $cha_mes) {
87 $status =fwrite ($bf,start_tag("MESSAGE",5,true));
88 //Print message contents
89 fwrite ($bf,full_tag("ID",6,false,$cha_mes->id));
90 fwrite ($bf,full_tag("USERID",6,false,$cha_mes->userid));
91 fwrite ($bf,full_tag("GROUPID",6,false,$cha_mes->groupid));
92 fwrite ($bf,full_tag("SYSTEM",6,false,$cha_mes->system));
93 fwrite ($bf,full_tag("MESSAGE_TEXT",6,false,$cha_mes->message));
94 fwrite ($bf,full_tag("TIMESTAMP",6,false,$cha_mes->timestamp));
96 $status =fwrite ($bf,end_tag("MESSAGE",5,true));
99 $status =fwrite ($bf,end_tag("MESSAGES",4,true));
104 //Return an array of info (name,value)
105 function chat_check_backup_mods($course,$user_data=false,$backup_unique_code,$instances=null) {
107 if (!empty($instances) && is_array($instances) && count($instances)) {
109 foreach ($instances as $id => $instance) {
110 $info += chat_check_backup_mods_instances($instance,$backup_unique_code);
114 //First the course data
115 $info[0][0] = get_string("modulenameplural","chat");
116 if ($ids = chat_ids ($course)) {
117 $info[0][1] = count($ids);
122 //Now, if requested, the user_data
124 $info[1][0] = get_string("messages","chat");
125 if ($ids = chat_message_ids_by_course ($course)) {
126 $info[1][1] = count($ids);
134 //Return an array of info (name,value)
135 function chat_check_backup_mods_instances($instance,$backup_unique_code) {
136 //First the course data
137 $info[$instance->id.'0'][0] = '<b>'.$instance->name.'</b>';
138 $info[$instance->id.'0'][1] = '';
140 //Now, if requested, the user_data
141 if (!empty($instance->userdata)) {
142 $info[$instance->id.'1'][0] = get_string("messages","chat");
143 if ($ids = chat_message_ids_by_instance ($instance->id)) {
144 $info[$instance->id.'1'][1] = count($ids);
146 $info[$instance->id.'1'][1] = 0;
152 //Return a content encoded to support interactivities linking. Every module
153 //should have its own. They are called automatically from the backup procedure.
154 function chat_encode_content_links ($content,$preferences) {
158 $base = preg_quote($CFG->wwwroot,"/");
160 //Link to the list of chats
161 $buscar="/(".$base."\/mod\/chat\/index.php\?id\=)([0-9]+)/";
162 $result= preg_replace($buscar,'$@CHATINDEX*$2@$',$content);
164 //Link to chat view by moduleid
165 $buscar="/(".$base."\/mod\/chat\/view.php\?id\=)([0-9]+)/";
166 $result= preg_replace($buscar,'$@CHATVIEWBYID*$2@$',$result);
171 // INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE
173 //Returns an array of chats id
174 function chat_ids ($course) {
177 return $DB->get_records_sql("SELECT c.id, c.course
179 WHERE c.course = ?", array($course));
182 //Returns an array of assignment_submissions id
183 function chat_message_ids_by_course ($course) {
186 return $DB->get_records_sql("SELECT m.id , m.chatid
187 FROM {chat_messages} m, {chat} c
188 WHERE c.course = ? AND
189 m.chatid = c.id", array($course));
192 //Returns an array of chat id
193 function chat_message_ids_by_instance ($instanceid) {
196 return $DB->get_records_sql("SELECT m.id , m.chatid
197 FROM {chat_messages} m
198 WHERE m.chatid = ?", array($instanceid));