Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / mod / journal / backuplib.php
blobbf47f88fc1dea76edbbe7a0274c7ec94a52eae9a
1 <?php //$Id$
2 //This php script contains all the stuff to backup/restore
3 //journal mods
5 //This is the "graphical" structure of the journal mod:
6 //
7 // journal
8 // (CL,pk->id)
9 // |
10 // |
11 // |
12 // journal_entries
13 // (UL,pk->id, fk->journal)
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 function journal_backup_mods($bf,$preferences) {
26 global $CFG;
28 $status = true;
30 //Iterate over journal table
31 $journals = get_records ("journal","course",$preferences->backup_course,"id");
32 if ($journals) {
33 foreach ($journals as $journal) {
34 if (backup_mod_selected($preferences,'journal',$journal->id)) {
35 $status = journal_backup_one_mod($bf,$preferences,$journal);
39 return $status;
42 function journal_backup_one_mod($bf,$preferences,$journal) {
44 global $CFG;
46 if (is_numeric($journal)) {
47 $journal = get_record('journal','id',$journal);
50 $status = true;
52 //Start mod
53 fwrite ($bf,start_tag("MOD",3,true));
54 //Print journal data
55 fwrite ($bf,full_tag("ID",4,false,$journal->id));
56 fwrite ($bf,full_tag("MODTYPE",4,false,"journal"));
57 fwrite ($bf,full_tag("NAME",4,false,$journal->name));
58 fwrite ($bf,full_tag("INTRO",4,false,$journal->intro));
59 fwrite ($bf,full_tag("INTROFORMAT",4,false,$journal->introformat));
60 fwrite ($bf,full_tag("DAYS",4,false,$journal->days));
61 fwrite ($bf,full_tag("ASSESSED",4,false,$journal->assessed));
62 fwrite ($bf,full_tag("TIMEMODIFIED",4,false,$journal->timemodified));
64 //if we've selected to backup users info, then execute backup_journal_entries
65 if (backup_userdata_selected($preferences,'journal',$journal->id)) {
66 $status = backup_journal_entries($bf,$preferences,$journal->id);
68 //End mod
69 $status =fwrite ($bf,end_tag("MOD",3,true));
71 return $status;
74 //Backup journal_entries contents (executed from journal_backup_mods)
75 function backup_journal_entries ($bf,$preferences,$journal) {
77 global $CFG;
79 $status = true;
81 $journal_entries = get_records("journal_entries","journal",$journal,"id");
82 //If there is entries
83 if ($journal_entries) {
84 //Write start tag
85 $status =fwrite ($bf,start_tag("ENTRIES",4,true));
86 //Iterate over each entry
87 foreach ($journal_entries as $jou_ent) {
88 //Start entry
89 $status =fwrite ($bf,start_tag("ENTRY",5,true));
90 //Print journal_entries contents
91 fwrite ($bf,full_tag("ID",6,false,$jou_ent->id));
92 fwrite ($bf,full_tag("USERID",6,false,$jou_ent->userid));
93 fwrite ($bf,full_tag("MODIFIED",6,false,$jou_ent->modified));
94 fwrite ($bf,full_tag("TEXT",6,false,$jou_ent->text));
95 fwrite ($bf,full_tag("FORMAT",6,false,$jou_ent->format));
96 fwrite ($bf,full_tag("RATING",6,false,$jou_ent->rating));
97 fwrite ($bf,full_tag("ENTRYCOMMENT",6,false,$jou_ent->entrycomment));
98 fwrite ($bf,full_tag("TEACHER",6,false,$jou_ent->teacher));
99 fwrite ($bf,full_tag("TIMEMARKED",6,false,$jou_ent->timemarked));
100 fwrite ($bf,full_tag("MAILED",6,false,$jou_ent->mailed));
101 //End entry
102 $status =fwrite ($bf,end_tag("ENTRY",5,true));
104 //Write end tag
105 $status =fwrite ($bf,end_tag("ENTRIES",4,true));
107 return $status;
110 ////Return an array of info (name,value)
111 function journal_check_backup_mods($course,$user_data=false,$backup_unique_code, $instances=null) {
112 if (!empty($instances) && is_array($instances) && count($instances)) {
113 $info = array();
114 foreach ($instances as $id => $instance) {
115 $info += journal_check_backup_mods_instances($instance,$backup_unique_code);
117 return $info;
119 //First the course data
120 $info[0][0] = get_string("modulenameplural","journal");
121 if ($ids = journal_ids ($course)) {
122 $info[0][1] = count($ids);
123 } else {
124 $info[0][1] = 0;
127 //Now, if requested, the user_data
128 if ($user_data) {
129 $info[1][0] = get_string("entries","journal");
130 if ($ids = journal_entry_ids_by_course ($course)) {
131 $info[1][1] = count($ids);
132 } else {
133 $info[1][1] = 0;
136 return $info;
139 ////Return an array of info (name,value)
140 function journal_check_backup_mods_instances($instance,$backup_unique_code) {
141 //First the course data
142 $info[$instance->id.'0'][0] = '<b>'.$instance->name.'</b>';
143 $info[$instance->id.'0'][1] = '';
145 //Now, if requested, the user_data
146 if (!empty($instance->userdata)) {
147 $info[$instance->id.'1'][0] = get_string("entries","journal");
148 if ($ids = journal_entry_ids_by_instance ($instance->id)) {
149 $info[$instance->id.'1'][1] = count($ids);
150 } else {
151 $info[$instance->id.'1'][1] = 0;
154 return $info;
161 // INTERNAL FUNCTIONS. BASED IN THE MOD STRUCTURE
163 //Returns an array of journals id
164 function journal_ids ($course) {
166 global $CFG;
168 return get_records_sql ("SELECT a.id, a.course
169 FROM {$CFG->prefix}journal a
170 WHERE a.course = '$course'");
173 //Returns an array of journal entries id
174 function journal_entry_ids_by_course ($course) {
176 global $CFG;
178 return get_records_sql ("SELECT s.id , s.journal
179 FROM {$CFG->prefix}journal_entries s,
180 {$CFG->prefix}journal a
181 WHERE a.course = '$course' AND
182 s.journal = a.id");
185 //Returns an array of journal entries id
186 function journal_entry_ids_by_instance ($instanceid) {
188 global $CFG;
190 return get_records_sql ("SELECT s.id , s.journal
191 FROM {$CFG->prefix}journal_entries s
192 WHERE s.journal = $instanceid");