move code up one directory
[atutor.git] / include / classes / Message / Message.class.php
1 <?php
2 /************************************************************************/
3 /* ATutor                                                                                                                               */
4 /************************************************************************/
5 /* Copyright (c) 2002-2010                                              */
6 /* Inclusive Design Institute                                           */
7 /* http://atutor.ca                                                     */
8 /* This program is free software. You can redistribute it and/or        */
9 /* modify it under the terms of the GNU General Public License          */
10 /* as published by the Free Software Foundation.                        */
11 /************************************************************************/
12
13 /**
14 * Message
15 * Class acting as MessageHandler for various message types
16 * @access       public
17 * @author       Jacek Materna
18 */
19
20 class Message {
21
22         /*
23         * Reference to savant obj.
24         * @access private
25         * @see /include/classes/Savant/Savant.php
26         * @var object   
27         */
28         var $savant;
29         
30         /*
31         * Stastic assoc. array of message types mapped to Savant template file names
32         * @access private
33         * @see /templates/
34         * @var array
35         */
36         var $tmpl = array(      'error' => 'errormessage.tmpl.php',
37                                                 'feedback' => 'feedbackmessage.tmpl.php',
38                                                 'warning' => 'warningmessage.tmpl.php',
39                                                 'info' => 'infomessage.tmpl.php',
40                                                 'help' => 'helpmessage.tmpl.php',
41                                                 'confirm' => 'confirmmessage.tmpl.php'
42                                 );
43         
44         /*
45         * Static assoc array of message types mapped to Language code prefixes
46         * @access private
47         * @see /include/lib/lang_constant.inc.php
48         * @var array    
49         */
50         var $prefix = array( 'error'  =>'AT_ERROR_',
51                                                 'feedback' => 'AT_FEEDBACK_',
52                                                 'warning' => 'AT_WARNING_',
53                                                 'info' => 'AT_INFOS_',
54                                                 'help' => 'AT_HELP_',
55                                                 'confirm' => 'AT_CONFIRM_'
56                                   );
57         
58         /**
59         * Constructor
60         * @access  public
61         * @param   obj $savant Reference to Savant object
62         * @author  Jacek Materna
63         */
64         function Message($savant) { 
65                 $this->savant = $savant;
66         } 
67                 
68         /**
69         * Print message(s) of type $type. Processes stored messages in session var for type $type
70         * and translates them into language spec. Then passes processed data to savant template for display
71         * @access  public
72         * @param   string $type                                 error|warning|info|feedback|help|help_pop
73         * @author  Jacek Materna
74         */
75         function printAbstract($type) {
76                 if (!isset($_SESSION['message'][$type])) return;
77
78                 $_result = array();
79                 
80                 foreach($_SESSION['message'][$type] as $e => $item) {
81                         $result = '';
82
83                         if ($type == 'confirm') {
84                                 // the confirm msg's have the hidden vars as the last element in the array
85                                 $hide_button_no = array_pop($item);
86                                 $button_no_text = array_pop($item);
87                                 $button_yes_text = array_pop($item);
88                                 $hidden_vars = array_pop($item);
89                                 
90                                 if (count($item) == 1) {
91                                         $item = $item[0];
92                                 }
93                         }
94
95                         // $item is either just a code or an array of argument with a particular code
96                         if (is_array($item)) {
97         
98                         
99                                 /* this is an array with terms to replace */
100                                 $first = array_shift($item);
101                                 $result = _AT($first); // lets translate the code
102                                 
103                                 if ($result == '') { // if the code is not in the db lets just print out the code for easier trackdown
104                                         $result = '[' . $first . ']';
105                                 }
106                                                                                 
107                                 $terms = $item;
108                         
109                                 /* replace the tokens with the terms */
110                                 $result = vsprintf($result, $terms);
111                                 
112                         } else {
113                                 $result = _AT($item);
114                                 if ($result == '') // if the code is not in the db lets just print out the code for easier trackdown
115                                         $result = '[' . $item . ']';
116                         }
117                         
118                         array_push($_result, $result); // append to array
119                 }
120                 
121                 if (count($_result) > 0) {
122                         $this->savant->assign('item', $_result);        // pass translated payload to savant var for processing
123
124                         if ($type == 'confirm') {
125                                 $this->savant->assign('hidden_vars', $hidden_vars);
126                                 $this->savant->assign('button_yes_text', $button_yes_text);
127                                 $this->savant->assign('button_no_text', $button_no_text);
128                                 $this->savant->assign('hide_button_no', $hide_button_no);
129
130                         } else if ($type == 'help') { // special case for help message, we need to check a few conditions
131                                 $a = (!isset($_GET['e']) && !$_SESSION['prefs']['PREF_HELP'] && !$_GET['h']);
132                                 $b = ($_SESSION['prefs']['PREF_CONTENT_ICONS'] == 2);
133                                 $c = isset($_GET['e']);
134                                 $d = $_SESSION['course_id'];
135                                 
136                                 $this->savant->assign('a', $a);
137                                 $this->savant->assign('b', $b);
138                                 $this->savant->assign('c', $c);
139                                 $this->savant->assign('d', $d);
140                         }
141                 
142                         $this->savant->display($this->tmpl[$type]);
143                 }
144
145                 unset($_SESSION['message'][$type]);
146         }
147
148         /**
149         * Add message to be tracked by session obj
150         * @access  public
151         * @param   string $sync                                 ref to type of message
152         * @param   string|array $code                   code of the message or array(code, args...)
153         * @author  Jacek Materna
154         */
155         function addAbstract($sync, $code) {
156                 $first = ''; // key value for storage
157                 // Convert to strings
158                 if (is_array($code)) {
159                         foreach($code as $e) {
160                                 settype($e, "string");
161                         }
162
163                         $code[0] = $this->prefix[$sync] . $code[0]; // add prefix               
164
165                         $first = $code[0];
166                 } else {
167                         if (!is_string($code))  
168                                 settype($code, "string");
169                         
170                         $code = $this->prefix[$sync] . $code;
171                         $first = $code;         
172                 }
173                 
174                 $payload = $code;
175                 
176                 if (!isset($_SESSION['message'][$sync]) || count($_SESSION['message'][$sync]) == 0) { // fresh
177                         
178                         // PHP 5 
179                         //try {
180                                 $_SESSION['message'][$sync] = array($first => $payload);
181                         //} catch (Exception $e) {
182                         //      return false;
183                         //}
184                 } else if (isset($_SESSION['message'][$sync][$first])) { // already data there for that code, append
185                         // existing data is either a collection or a single node
186                         if(is_array($_SESSION['message'][$sync][$first])) { // already an array there
187                                 if (is_array($payload)) {
188                                         // lets ignore the code, its already there as the first element
189                                         $elem = array_shift($payload);
190                                         foreach($payload as $elem) {
191                                                 array_push($_SESSION['message'][$sync][$first], $elem); // add ourselves to the chain
192                                         }
193                                 } else // no array here yet
194                                         $_SESSION['message'][$sync][$first][] = $payload; // add ourselves 
195                                 
196                         } else { // just a string
197                                 if (is_array($payload)) {
198                                         $temp = $_SESSION['message'][$sync][$first]; // grab it
199                                         unset($_SESSION['message'][$sync][$first]); // make sure its gone
200                                         
201                                         $arr = array($temp);
202                                         
203                                         // skip first elem, we're asserting here that $first === $payload[0]
204                                         $grb = array_shift($payload);
205                                         foreach($payload as $elem) { // lets finish building the array
206                                                 array_push($arr, $elem);
207                                         }
208                                         
209                                         $_SESSION['message'][$sync][$first] = $arr; // put it back 
210                                 }
211                         }
212                 } else {
213                 
214                         // Already an array there, could be empty or have something in it, append.
215                         // Store key = value for much faster unset as needed 
216                         
217                         // PHP 5
218                         //try {
219                                 $new = array($first => $payload);
220                                 $final = array_merge((array) $_SESSION['message'][$sync], (array) $new);
221
222                                 unset($_SESSION['message'][$sync]);
223                                 $_SESSION['message'][$sync] = $final;
224                         //} catch (exception $e) {
225                         //      return false;
226                         //}
227                 }
228         }
229         
230         /**
231         * Simply check is a type $type message isset in the session obj
232         * @access  public
233         * @param   string $type                                 what type of message to check for
234         * @author  Jacek Materna
235         */
236         function abstractContains($type) {
237                 return (isset($_SESSION['message'][$type]));
238         }
239         
240         /**
241         * Deletes the tracked message code $code from the Session obj as well as all 
242         * if its children
243         * @access  public
244         * @param   string $type                                 what type of message to delete
245         # @param   string $code                                 The code to delete
246         * @author  Jacek Materna
247         */
248         function abstractDelete($type, $code) {
249                 if (!is_string($code))
250                         settype($code, "string");
251
252                 // Lets append the right prefic to this code for searching
253                 $code = $this->prefix[$type] . $code;
254         
255                 if(isset($_SESSION['message'][$type][$code])) {
256                         unset($_SESSION['message'][$type][$code]); // delete it and its children
257                 }
258         }
259         
260         /**
261         * Add error message to be tracked by session obj
262         * @access  public
263         * @param   string|array $code                   code of the message or array(code, args...)
264         * @author  Jacek Materna
265         */
266         function addError($code) {
267                 $this->addAbstract('error', $code);
268         }
269         
270         /**
271         * Print error messages using Savant template
272         * @access  public
273         * @author  Jacek Materna
274         */
275         function printErrors($optional=null) {
276                 if ($optional != null)  // shortcut
277                         $this->addAbstract('error', $optional);
278
279                 $this->printAbstract('error');
280         }
281         
282
283         function addConfirm($code, $hidden_vars = '', $button_yes_text='', $button_no_text='', $hide_button_no=false) {
284                 $hidden_vars_string = '';
285                 if (is_array($hidden_vars)) {
286                         foreach ($hidden_vars as $key => $value) {
287                                 $hidden_vars_string .= '<input type="hidden" name="'.$key.'" value="'.$value.'" />';
288                         }
289                 }
290                 if (!is_array($code)) {
291                         $code = array($code);
292                 }
293                 $code[] = $hidden_vars_string;
294                 $code[] = ($button_yes_text == '') ? _AT("submit_yes") : $button_yes_text;
295                 $code[] = ($button_no_text == '') ? _AT("submit_no") : $button_no_text;
296                 $code[] = $hide_button_no;
297                 
298                 $this->addAbstract('confirm', $code);
299         }
300         
301         function printConfirm($optional=null) {
302                 if ($optional != null)  // shortcut
303                         $this->addAbstract('confirm', $optional);
304
305                 $this->printAbstract('confirm');
306         }
307
308         /**
309         * Add warning message to be tracked by session obj
310         * @access  public
311         * @param   string|array $code                   code of the message or array(code, args...)
312         * @author  Jacek Materna
313         */
314         function addWarning($code) { 
315                 $this->addAbstract('warning', $code);
316         }
317         
318         /**
319         * Print warning messages using Savant template
320         * @access  public
321         * @author  Jacek Materna
322         */
323         function printWarnings($optional=null) {
324                 if ($optional != null)  // shortcut
325                         $this->addAbstract('warning', $optional);
326                 
327                 $this->printAbstract('warning');
328         }
329         
330         /**
331         * Add info message to be tracked by session obj
332         * @access  public
333         * @param   string|array $code                   code of the message or array(code, args...)
334         * @author  Jacek Materna
335         */
336         function addInfo($code) { 
337                 $this->addAbstract('info', $code);
338         }
339         
340         /**
341         * Print info messages using Savant template
342         * @access  public
343         * @author  Jacek Materna
344         */
345         function printInfos($optional=null) { 
346                 if ($optional != null)  // shortcut
347                         $this->addAbstract('info', $optional);
348
349                 $this->printAbstract('info');
350         }
351         
352         /**
353         * Add feedback message to be tracked by session obj
354         * @access  public
355         * @param   string|array $code                   code of the message or array(code, args...)
356         * @author  Jacek Materna
357         */
358         function addFeedback($code) { 
359                 $this->addAbstract('feedback', $code); 
360         }
361         
362         /**
363         * Print feedback messages using Savant template
364         * @access  public
365         * @author  Jacek Materna
366         */
367         function printFeedbacks($optional=null) {
368                 if ($optional != null) // shortcut
369                         $this->addAbstract('feedback', $optional); 
370                         
371                 $this->printAbstract('feedback');
372         }
373         
374         /**
375         * Add help message to be tracked by session obj
376         * @access  public
377         * @param   string|array $code                   code of the message or array(code, args...)
378         * @author  Jacek Materna
379         */
380         function addHelp($code) { 
381                 $this->addAbstract('help', $code);
382         }
383         
384         /**
385         * Print help messages using Savant template
386         * @access  public
387         * @author  Jacek Materna
388         */
389         function printHelps($optional=null) {
390                 if ($optional != null)  // shortcut
391                         $this->addAbstract('help', $optional);
392                         
393                 $this->printAbstract('help');
394         }
395          
396         /**
397         * Dump all the messages in the session to the screen in the following order
398         * @access  public
399         * @author  Jacek Materna
400         */
401         function printAll() {
402                 $this->printAbstract('feedback');
403                 $this->printAbstract('error');
404                 $this->printAbstract('warning');
405                 $this->printAbstract('help');
406                 $this->printAbstract('info');
407         }
408         
409         /**
410         * Print feedback message using Savant template with no Session dialog and
411         * no database dialog, straight text inside feedback box
412         * @access  public
413         * @param String String message to display inside feedback box
414         * @author  Jacek Materna
415         */
416         function printNoLookupFeedback($str) {
417                 if (str != null) {
418                         $this->savant->assign('item', array($str));     // pass string to savant var for processing
419                         $this->savant->display($this->tmpl['feedback']);
420                 }
421         }
422         
423         /**
424          * Method which simply check if a particular message type exists in the session obj
425          */
426         function containsErrors() {
427                 return $this->abstractContains('error');
428         }
429         
430         function containsFeedbacks() {
431                 return $this->abstractContains('feedback');
432         }
433         
434         function containsWarnings() {
435                 return $this->abstractContains('warning');
436         }
437         
438         function containsInfos() {
439                 return $this->abstractContains('info');
440         }
441         
442         function containsHelps() {
443                 return $this->abstractContains('help');
444         }
445         
446         /**
447          * Method that allow deletion of individual Message codes form the Session obj
448          */
449         function deleteError($code) {
450                 $this->abstractDelete('error', $code);
451         }
452         
453         function deleteFeedback($code) {
454                 $this->abstractDelete('feedback', $code);
455         }
456         
457         function deleteWarning($code) {
458                 $this->abstractDelete('warning', $code);
459         }
460         
461         function deleteInfo($code) {
462                 $this->abstractDelete('info', $code);
463         }
464         
465         function deleteHelp($code) {
466                 $this->abstractDelete('help', $code);
467         }
468         
469 } // end of class
470
471
472
473 ?>