MDL-20700 coding style cleanup - cvs keywords removed, closign php tag removed, trail...
[moodle.git] / mod / chat / backuplib.php
1 <?php
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)
14     //
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)
21     //
22     //-----------------------------------------------------------
24     //This function executes all the backup procedure about this mod
25     function chat_backup_mods($bf,$preferences) {
26         global $CFG, $DB;
28         $status = true;
30         //Iterate over chat table
31         $chats = $DB->get_records ("chat", array("course"=>$preferences->backup_course), "id");
32         if ($chats) {
33             foreach ($chats as $chat) {
34                 if (backup_mod_selected($preferences,'chat',$chat->id)) {
35                     $status = chat_backup_one_mod($bf,$preferences,$chat);
36                 }
37             }
38         }
39         return $status;
40     }
42     function chat_backup_one_mod($bf,$preferences,$chat) {
43         global $CFG, $DB;
45         if (is_numeric($chat)) {
46             $chat = $DB->get_record('chat', array('id'=>$chat));
47         }
49         $status = true;
51         //Start mod
52         fwrite ($bf,start_tag("MOD",3,true));
53         //Print chat data
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);
66         }
67         //End mod
68         $status =fwrite ($bf,end_tag("MOD",3,true));
70         return $status;
71     }
73     //Backup chat_messages contents (executed from chat_backup_mods)
74     function backup_chat_messages ($bf,$preferences,$chat) {
75         global $CFG, $DB;
77         $status = true;
79         $chat_messages = $DB->get_records("chat_messages", array("chatid"=>$chat), "id");
80         //If there is messages
81         if ($chat_messages) {
82             //Write start tag
83             $status =fwrite ($bf,start_tag("MESSAGES",4,true));
84             //Iterate over each message
85             foreach ($chat_messages as $cha_mes) {
86                 //Start message
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));
95                 //End submission
96                 $status =fwrite ($bf,end_tag("MESSAGE",5,true));
97             }
98             //Write end tag
99             $status =fwrite ($bf,end_tag("MESSAGES",4,true));
100         }
101         return $status;
102     }
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)) {
108             $info = array();
109             foreach ($instances as $id => $instance) {
110                 $info += chat_check_backup_mods_instances($instance,$backup_unique_code);
111             }
112             return $info;
113         }
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);
118         } else {
119             $info[0][1] = 0;
120         }
122         //Now, if requested, the user_data
123         if ($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);
127             } else {
128                 $info[1][1] = 0;
129             }
130         }
131         return $info;
132     }
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);
145             } else {
146                 $info[$instance->id.'1'][1] = 0;
147             }
148         }
149         return $info;
150     }
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) {
156         global $CFG;
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);
168         return $result;
169     }
171     // INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE
173     //Returns an array of chats id
174     function chat_ids ($course) {
175         global $DB;
177         return $DB->get_records_sql("SELECT c.id, c.course
178                                        FROM {chat} c
179                                       WHERE c.course = ?", array($course));
180     }
182     //Returns an array of assignment_submissions id
183     function chat_message_ids_by_course ($course) {
184         global $DB;
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));
190     }
192     //Returns an array of chat id
193     function chat_message_ids_by_instance ($instanceid) {
194         global $DB;
196         return $DB->get_records_sql("SELECT m.id , m.chatid
197                                        FROM {chat_messages} m
198                                       WHERE m.chatid = ?", array($instanceid));
199     }