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