MDL-10240:
[moodle-linuxchix.git] / mod / chat / backuplib.php
blobc39b391682406923398051a7c93a267f356b6bc2
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 backup procedure about this mod
25 function chat_backup_mods($bf,$preferences) {
27 global $CFG;
29 $status = true;
31 //Iterate over chat table
32 $chats = get_records ("chat","course",$preferences->backup_course,"id");
33 if ($chats) {
34 foreach ($chats as $chat) {
35 if (backup_mod_selected($preferences,'chat',$chat->id)) {
36 $status = chat_backup_one_mod($bf,$preferences,$chat);
40 return $status;
43 function chat_backup_one_mod($bf,$preferences,$chat) {
45 global $CFG;
47 if (is_numeric($chat)) {
48 $chat = get_record('chat','id',$chat);
51 $status = true;
53 //Start mod
54 fwrite ($bf,start_tag("MOD",3,true));
55 //Print chat data
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);
69 //End mod
70 $status =fwrite ($bf,end_tag("MOD",3,true));
72 return $status;
75 //Backup chat_messages contents (executed from chat_backup_mods)
76 function backup_chat_messages ($bf,$preferences,$chat) {
78 global $CFG;
80 $status = true;
82 $chat_messages = get_records("chat_messages","chatid",$chat,"id");
83 //If there is messages
84 if ($chat_messages) {
85 //Write start tag
86 $status =fwrite ($bf,start_tag("MESSAGES",4,true));
87 //Iterate over each message
88 foreach ($chat_messages as $cha_mes) {
89 //Start message
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));
98 //End submission
99 $status =fwrite ($bf,end_tag("MESSAGE",5,true));
101 //Write end tag
102 $status =fwrite ($bf,end_tag("MESSAGES",4,true));
104 return $status;
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)) {
111 $info = array();
112 foreach ($instances as $id => $instance) {
113 $info += chat_check_backup_mods_instances($instance,$backup_unique_code);
115 return $info;
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);
121 } else {
122 $info[0][1] = 0;
125 //Now, if requested, the user_data
126 if ($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);
130 } else {
131 $info[1][1] = 0;
134 return $info;
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);
148 } else {
149 $info[$instance->id.'1'][1] = 0;
152 return $info;
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) {
159 global $CFG;
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);
171 return $result;
174 // INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE
176 //Returns an array of chats id
177 function chat_ids ($course) {
179 global $CFG;
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) {
189 global $CFG;
191 return get_records_sql ("SELECT m.id , m.chatid
192 FROM {$CFG->prefix}chat_messages m,
193 {$CFG->prefix}chat c
194 WHERE c.course = '$course' AND
195 m.chatid = c.id");
198 //Returns an array of chat id
199 function chat_message_ids_by_instance ($instanceid) {
201 global $CFG;
203 return get_records_sql ("SELECT m.id , m.chatid
204 FROM {$CFG->prefix}chat_messages m
205 WHERE m.chatid = $instanceid");