File: backuplib.php

package info (click to toggle)
moodle 1.6.3-2%2Betch3
  • links: PTS
  • area: main
  • in suites: etch
  • size: 37,172 kB
  • ctags: 51,688
  • sloc: php: 231,916; sql: 5,631; xml: 2,688; sh: 1,185; perl: 638; makefile: 48; pascal: 36
file content (207 lines) | stat: -rw-r--r-- 7,460 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php //$Id: backuplib.php,v 1.5 2006/01/13 03:45:29 mjollnir_ Exp $
    //This php script contains all the stuff to backup/restore
    //chat mods

    //This is the "graphical" structure of the chat mod:
    //
    //                       chat
    //                    (CL,pk->id)             
    //                        |
    //                        |
    //                        |
    //                   chat_messages 
    //               (UL,pk->id, fk->chatid)
    //
    // Meaning: pk->primary key field of the table
    //          fk->foreign key to link with parent
    //          nt->nested field (recursive data)
    //          CL->course level info
    //          UL->user level info
    //          files->table may have files)
    //
    //-----------------------------------------------------------

    //This function executes all the backup procedure about this mod
    function chat_backup_mods($bf,$preferences) {

        global $CFG;

        $status = true;

        //Iterate over chat table
        $chats = get_records ("chat","course",$preferences->backup_course,"id");
        if ($chats) {
            foreach ($chats as $chat) {
                if (backup_mod_selected($preferences,'chat',$chat->id)) {
                    $status = chat_backup_one_mod($bf,$preferences,$chat);
                }
            }
        }
        return $status;  
    }

    function chat_backup_one_mod($bf,$preferences,$chat) {

        global $CFG;
    
        if (is_numeric($chat)) {
            $chat = get_record('chat','id',$chat);
        }
    
        $status = true;

        //Start mod
        fwrite ($bf,start_tag("MOD",3,true));
        //Print chat data
        fwrite ($bf,full_tag("ID",4,false,$chat->id));
        fwrite ($bf,full_tag("MODTYPE",4,false,"chat"));
        fwrite ($bf,full_tag("NAME",4,false,$chat->name));
        fwrite ($bf,full_tag("INTRO",4,false,$chat->intro));
        fwrite ($bf,full_tag("KEEPDAYS",4,false,$chat->keepdays));
        fwrite ($bf,full_tag("STUDENTLOGS",4,false,$chat->studentlogs));
        fwrite ($bf,full_tag("SCHEDULE",4,false,$chat->schedule));
        fwrite ($bf,full_tag("CHATTIME",4,false,$chat->chattime));
        fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$chat->timemodified));
        //if we've selected to backup users info, then execute backup_chat_messages
        if (backup_userdata_selected($preferences,'chat',$chat->id)) {
            $status = backup_chat_messages($bf,$preferences,$chat->id);
        }
        //End mod
        $status =fwrite ($bf,end_tag("MOD",3,true));

        return $status;
    }

    //Backup chat_messages contents (executed from chat_backup_mods)
    function backup_chat_messages ($bf,$preferences,$chat) {

        global $CFG;

        $status = true;

        $chat_messages = get_records("chat_messages","chatid",$chat,"id");
        //If there is messages
        if ($chat_messages) {
            //Write start tag
            $status =fwrite ($bf,start_tag("MESSAGES",4,true));
            //Iterate over each message
            foreach ($chat_messages as $cha_mes) {
                //Start message
                $status =fwrite ($bf,start_tag("MESSAGE",5,true));
                //Print message contents
                fwrite ($bf,full_tag("ID",6,false,$cha_mes->id));       
                fwrite ($bf,full_tag("USERID",6,false,$cha_mes->userid));       
                fwrite ($bf,full_tag("GROUPID",6,false,$cha_mes->groupid)); 
                fwrite ($bf,full_tag("SYSTEM",6,false,$cha_mes->system));       
                fwrite ($bf,full_tag("MESSAGE_TEXT",6,false,$cha_mes->message));       
                fwrite ($bf,full_tag("TIMESTAMP",6,false,$cha_mes->timestamp));       
                //End submission
                $status =fwrite ($bf,end_tag("MESSAGE",5,true));
            }
            //Write end tag
            $status =fwrite ($bf,end_tag("MESSAGES",4,true));
        }
        return $status;
    }

    //Return an array of info (name,value)
    function chat_check_backup_mods($course,$user_data=false,$backup_unique_code,$instances=null) {

        if (!empty($instances) && is_array($instances) && count($instances)) {
            $info = array();
            foreach ($instances as $id => $instance) {
                $info += chat_check_backup_mods_instances($instance,$backup_unique_code);
            }
            return $info;
        }
        //First the course data
        $info[0][0] = get_string("modulenameplural","chat");
        if ($ids = chat_ids ($course)) {
            $info[0][1] = count($ids);
        } else {
            $info[0][1] = 0;
        }

        //Now, if requested, the user_data
        if ($user_data) {
            $info[1][0] = get_string("messages","chat");
            if ($ids = chat_message_ids_by_course ($course)) { 
                $info[1][1] = count($ids);
            } else {
                $info[1][1] = 0;
            }
        }
        return $info;
    }

    //Return an array of info (name,value)
    function chat_check_backup_mods_instances($instance,$backup_unique_code) {
        //First the course data
        $info[$instance->id.'0'][0] = '<b>'.$instance->name.'</b>';
        $info[$instance->id.'0'][1] = '';

        //Now, if requested, the user_data
        if (!empty($instance->userdata)) {
            $info[$instance->id.'1'][0] = get_string("messages","chat");
            if ($ids = chat_message_ids_by_instance ($instance->id)) { 
                $info[$instance->id.'1'][1] = count($ids);
            } else {
                $info[$instance->id.'1'][1] = 0;
            }
        }
        return $info;
    }

    //Return a content encoded to support interactivities linking. Every module
    //should have its own. They are called automatically from the backup procedure.
    function chat_encode_content_links ($content,$preferences) {

        global $CFG;

        $base = preg_quote($CFG->wwwroot,"/");

        //Link to the list of chats
        $buscar="/(".$base."\/mod\/chat\/index.php\?id\=)([0-9]+)/";
        $result= preg_replace($buscar,'$@CHATINDEX*$2@$',$content);

        //Link to chat view by moduleid
        $buscar="/(".$base."\/mod\/chat\/view.php\?id\=)([0-9]+)/";
        $result= preg_replace($buscar,'$@CHATVIEWBYID*$2@$',$result);

        return $result;
    }

    // INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE

    //Returns an array of chats id 
    function chat_ids ($course) {

        global $CFG;

        return get_records_sql ("SELECT c.id, c.course
                                 FROM {$CFG->prefix}chat c
                                 WHERE c.course = '$course'");
    }
    
    //Returns an array of assignment_submissions id
    function chat_message_ids_by_course ($course) {

        global $CFG;

        return get_records_sql ("SELECT m.id , m.chatid
                                 FROM {$CFG->prefix}chat_messages m,
                                      {$CFG->prefix}chat c
                                 WHERE c.course = '$course' AND
                                       m.chatid = c.id");
    }

    //Returns an array of chat id
    function chat_message_ids_by_instance ($instanceid) {

        global $CFG;

        return get_records_sql ("SELECT m.id , m.chatid
                                 FROM {$CFG->prefix}chat_messages m
                                 WHERE m.chatid = $instanceid");
    }
?>