Added an option to perform one action on multiple notifications at once.
[gnucomo.git] / src / web / notification.php
1 <?php 
2
3 /**************************************************************************
4 **  (c) Copyright 2003, Andromeda Technology & Automation
5 ** This is free software; you can redistribute it and/or modify it under the
6 ** terms of the GNU General Public License, see the file COPYING.
7 ***************************************************************************
8 ** MODULE INFORMATION *
9 ***********************
10 **      FILE NAME      : notification.php
11 **      SYSTEM NAME    : Gnucomo - Gnu Computer Monitoring
12 **      VERSION NUMBER : $Revision: 1.6 $
13 **
14 **  DESCRIPTION      : Display and handle notifications.
15 **                     There are two major views to this page: either a list of
16 **                     all notifications for an object or the display of a single
17 **                     notification. In the 'single notification' view, actions
18 **                     can be added to the notification and the status can be
19 **                     changed as a result of those actions.
20 **
21 **                     Input: GET['oid']   - If set, list all notifications for object.
22 **                            GET['notid'] - If set, display a single notification.
23 **                            POST['notid']- If set, update the notification.
24 **
25 **  EXPORTED OBJECTS : 
26 **  LOCAL    OBJECTS : 
27 **  MODULES  USED    :
28 ***************************************************************************
29 **  ADMINISTRATIVE INFORMATION *
30 ********************************
31 **      ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl
32 **      CREATION DATE   : Dec 12, 2002
33 **      LAST UPDATE     : Feb 16, 2003
34 **      MODIFICATIONS   : 
35 **************************************************************************/
36
37 /*****************************
38    $Log: notification.php,v $
39    Revision 1.6  2007-12-10 16:27:33  arjen
40    Added an option to perform one action on multiple notifications at once.
41
42    Revision 1.5  2007/11/21 14:38:06  arjen
43    The buttonbar at the top of each page is now a fixed 'div' element
44    instead of a framed page.
45    Contributed by Edwin Nadorp.
46
47    Revision 1.4  2007/01/11 13:44:29  arjen
48    Manually edit parameters.
49    View logs from abusing IP addresses.
50
51    Revision 1.3  2003/02/21 08:49:16  arjen
52    Keep a record of each time a notification
53    is displayed. Either on a listing or in detail.
54
55    Revision 1.2  2003/02/13 09:01:29  arjen
56    All web interface pages use the page class.
57
58    Revision 1.1  2003/02/05 09:48:14  arjen
59    Added display and handling of notifications
60
61 ******************************/
62
63 // RCSID = "$Id: notification.php,v 1.6 2007-12-10 16:27:33 arjen Exp $";
64
65 ini_set('include_path', '.:./classes:../phpclasses');
66
67 require_once('page.class.php');
68
69 function clientscripts()
70 {
71 ?>
72
73 <script type='text/ecmascript'>
74
75 function CheckRemark(f)
76 {
77    if (f.remark.value == "")
78    {
79       alert("Your remark is still empty");
80       return false;
81    }
82    return true;
83 }
84
85 </script>
86
87 <?php
88 }
89
90 /*
91  *   Add an action to the notification $note.
92  *   The status of the notification may alter as a result.
93  */
94
95 function add_action($db, $note, $actionid, $remark)
96 {
97    $r = pg_exec($db, "SELECT statuscode FROM action WHERE actionid=CAST('" . $actionid."' AS BIGINT)");
98    $action = pg_fetch_object($r, 0);
99
100    if ($action->statuscode != "" && $note->statuscode != $action->statuscode)
101    {
102       //  Update the status of the notification.
103
104       $note->statuscode = $action->statuscode;
105
106       pg_exec($db, "UPDATE notification SET statuscode='" . $note->statuscode
107                 . "' WHERE notificationid = " . $note->notificationid);
108    }
109
110    //  Add the action to the action_user table.
111
112    pg_exec($db, "INSERT INTO action_user (actionid, username, notificationid,
113                            timestamp, statuscode, remarks) values ('$actionid', '"
114                          . $_SESSION['username'] . "', '" . $note->notificationid
115                          . "', '" . date('Y-m-d H:i:s') . "', '" . $note->statuscode
116                          . "', '" . $remark . "')");
117
118    return $note->statuscode;
119 }
120
121 class notification_page extends page
122 {
123
124    function notification_list($objectid)
125    {
126
127       //  Display a list of all notifications for this object['oid']
128
129       $res = pg_exec($this->database, "SELECT objectname FROM object WHERE objectid=" . $objectid);
130       $obj = pg_fetch_object($res, 0);
131       echo "<h1>Notifications for " . $obj->objectname . "</h1><hr>";
132
133       $res = pg_exec($this->database, "SELECT notificationid, timestamp, type_of_issueid,
134                                            statuscode, priority
135                                        FROM notification WHERE objectid= CAST ('" 
136                                 . $objectid ."' AS BIGINT) AND statuscode != 'cls'"
137                                        . " ORDER BY notificationid");
138
139       echo "<table>";
140       echo '<tr><th>Time</th><th>Number</th><th>Issue</th><th>Status</th><th>Priority</th></tr>';
141       $row = 0;
142       while ($row < pg_num_rows($res))
143       {
144          $note = pg_fetch_object($res, $row);
145          $r = pg_exec($this->database, "SELECT name from type_of_issue WHERE type_of_issueid = CAST('"
146                        . $note->type_of_issueid . "' AS BIGINT)");
147          $issue = pg_fetch_object($r, 0);
148          echo "<tr><td align='center'>\n";
149          echo $note->timestamp;
150          echo "</td><td>";
151          echo $note->notificationid;
152          echo "</td><td>";
153          echo "<a href='notification.php?notid=";
154          echo $note->notificationid;
155          echo "'>";
156          echo $issue->name;
157          echo "</a>";
158          echo "</td><td>";
159          echo $note->statuscode;
160          echo "</td><td>";
161          echo $note->priority;
162          echo "</td></tr>\n";
163          $row++;
164
165          //Save evidence that this notification was presented to the user.
166          //$note->statuscode = add_action($this->database, $note, 21,
167          //            "This notification was displayed with others from the same object");
168       }
169       echo "</table>";
170    }
171
172    function display_notification($note)
173    {
174       $res = pg_exec($this->database, "SELECT objectname FROM object WHERE objectid= CAST ('" . $note->objectid . "' AS BIGINT)");
175       $obj = pg_fetch_object($res, 0);
176       echo "<h1>Notification " . $note->notificationid . " for " . $obj->objectname . "</h1><hr>";
177       echo "<script type='text/ecmascript'>document.getElementById('menu_title').innerHTML = '<h1>Notification " . $note->notificationid . " for " . $obj->objectname . "<\/h1>'</script><br>";
178
179       $r = pg_exec($this->database, "SELECT description FROM type_of_issue WHERE type_of_issueid = CAST ('"
180                        . $note->type_of_issueid . "' AS BIGINT)");
181       $issue = pg_fetch_object($r, 0);
182
183       $r = pg_exec($this->database, "SELECT statusname FROM status
184                                      WHERE statuscode = '" . $note->statuscode . "'");
185
186       $status = pg_fetch_object($r, 0);
187
188       echo "Issue : " . $issue->description . ", Priority " . $note->priority . "<br>";
189       echo "Creation time : " . $note->timestamp . "<br>";
190       echo "Current status : " . $status->statusname . "<br>";
191
192       //  List all actions that occurred on this notification
193
194       echo "<h2>Action history for notification " . $note->notificationid . "</h2>";
195       echo "<table>";
196       echo "<tr><th>Time</th><th>User</th><th>Remarks</th></tr>";
197
198       $r = pg_exec($this->database, "SELECT actionid, username, timestamp, remarks
199                                      FROM action_user WHERE notificationid= CAST ('" 
200                                      . $note->notificationid . "' AS BIGINT )"
201                                      ." ORDER BY timestamp");
202       for ($row = 0; $row < pg_numrows($r); $row++)
203       {
204          $action_user = pg_fetch_object($r, $row);
205
206          echo "<tr><td class='time'>";
207          echo $action_user->timestamp;
208          echo "</td><td>";
209          echo $action_user->username;
210          echo "</td><td>";
211          echo htmlspecialchars(stripslashes($action_user->remarks));
212          echo "</td></tr>";
213       }
214       echo "</table>";
215
216       //  See if we can list any parameters for this notification
217
218       $r = pg_exec($this->database, "SELECT * FROM parameter WHERE paramid IN
219                                      ( SELECT paramid FROM parameter_notification
220                                      WHERE notificationid = CAST ('" . $note->notificationid 
221                                      . "' AS BIGINT) )");
222       if (pg_numrows($r) > 0)
223       {
224          echo "<h2>Parameters involved in this notification</h2>";
225          echo "<table>";
226          echo "<tr><th>Class</th><th>Name</th><th>Description</th><th>History</th></tr>";
227
228          for ($row = 0; $row < pg_numrows($r); $row++)
229          {
230             $p = pg_fetch_object($r, $row);
231             $res = pg_exec($this->database, "SELECT * from history WHERE paramid= CAST('" . $p->paramid
232                                             . "' AS BIGINT) ORDER BY modified");
233
234             echo "<tr><td>";
235             echo $p->class;
236             echo "</td><td>";
237             echo $p->name;
238             echo "</td><td>";
239             echo $p->description;
240             echo "</td><td>";
241             for ($hrow = 0; $hrow < pg_numrows($res); $hrow++)
242             {
243                $hist = pg_fetch_object($res, $hrow);
244                echo $hist->modified . " ";
245                echo $hist->change_nature . " ";
246                echo $hist->changed_property . " ";
247                echo $hist->new_value;
248                echo "<br>";
249             }
250             echo "</td></tr>";
251
252          }
253          echo "</table>";
254       }
255
256       //  See if we can list any log entries for this notification
257
258       $r = pg_exec($this->database, "SELECT * FROM log WHERE logid IN
259                                      ( SELECT logid FROM log_notification
260                                      WHERE notificationid = CAST ('" 
261                                       . $note->notificationid . "' AS BIGINT)) ORDER BY object_timestamp");
262       if (pg_numrows($r) > 0)
263       {
264          echo "<h2>Log entries involved in this notification</h2>";
265          echo "<table>";
266          echo "<tr><th>Time</th><th>Service</th><th>Log entry</th></tr>";
267
268          for ($row = 0; $row < pg_numrows($r); $row++)
269          {
270             $p = pg_fetch_object($r, $row);
271
272             echo "<tr><td>";
273             echo $p->object_timestamp;
274             echo "</td><td>";
275             echo $p->servicecode;
276             echo "</td><td>";
277             echo htmlentities($p->rawdata);
278             echo "</td></tr>";
279
280          }
281          echo "</table>";
282       }
283
284    }
285
286    function notification_form($note)
287    {
288       /*
289        *  Depending on the state of the notification, specific actions
290        *  are possible. One such actions determines a transition to
291        *  the next state. The array $possible_action lists the state transitions
292        *  for each state.
293        */
294
295       $possible_action = array
296       (
297          "opn" => array (3, 6, 7, 13),
298          "pen" => array (3, 6, 7, 8, 11, 18),
299          "inv" => array (3, 9),
300          "vrf" => array (12, 13),
301          "cls" => array (19)
302       );
303
304       echo "<h2>Enter a remark for the next action</h2>";
305       echo "<form method='post' onSubmit='return CheckRemark(this)' action=''>";
306       echo "<textarea name='remark' cols='30' rows='5'></textarea><br>";
307
308       /*  Display a list of possible actions */
309
310       echo "<h2>Select one of the actions to take below</h2>";
311
312       $first = true;
313
314       foreach ($possible_action[$note->statuscode] as $act)
315       {
316          $action = pg_fetch_object(pg_exec("SELECT description FROM action WHERE actionid= CAST('"
317           . $act . "' AS BIGINT)"),0);
318          echo "<input type='radio' name='actionid'";
319          if ($first)
320          {
321             $first = false;
322             echo " CHECKED";
323          }
324          echo " value='$act'>" . $action->description . "<br>";
325       }
326
327       echo "<input type='checkbox' name='act_on_all'>Perform this action on all notifications with the same issue.<br>";
328       echo "<input type='submit' value='Submit action'>";
329       echo "</form>";
330    }
331
332    function Body()
333    {
334
335       if ($_SERVER['REQUEST_METHOD'] == 'GET' && !empty($_GET['oid']))
336       {
337
338          //  Display a list of all notifications for this object['oid']
339
340          $res = pg_exec($this->database, "SELECT objectname FROM object WHERE objectid=" . $_GET['oid']);
341          $obj = pg_fetch_object($res, 0);
342          echo "<script type='text/ecmascript'>document.getElementById('menu_title').innerHTML = '<h1>Notifications for " . $obj->objectname . "<\/h1>'</script><br>";
343
344          $res = pg_exec($this->database, "SELECT notificationid, timestamp, type_of_issueid,
345                                               statuscode, priority
346                                           FROM notification WHERE objectid= CAST ('" 
347                                           . $_GET['oid'] ."' AS BIGINT) AND statuscode != 'cls'"
348                                           . " ORDER BY notificationid");
349
350          echo "<table>";
351          echo '<tr><th>Time</th><th>Number</th><th>Issue</th><th>Status</th><th>Priority</th></tr>';
352          $row = 0;
353          while ($row < pg_numrows($res))
354          {
355             $note = pg_fetch_object($res, $row);
356             $r = pg_exec($this->database, "SELECT name from type_of_issue WHERE type_of_issueid = CAST('"
357                           . $note->type_of_issueid . "' AS BIGINT)");
358             $issue = pg_fetch_object($r, 0);
359             echo "<tr><td align='center'>\n";
360             echo $note->timestamp;
361             echo "</td><td>";
362             echo $note->notificationid;
363             echo "</td><td>";
364             echo "<a href='notification.php?notid=";
365             echo $note->notificationid;
366             echo "'>";
367             echo $issue->name;
368             echo "</a>";
369             echo "</td><td>";
370             echo $note->statuscode;
371             echo "</td><td>";
372             echo $note->priority;
373             echo "</td></tr>\n";
374             $row++;
375
376             //Save evidence that this notification was presented to the user.
377             //$note->statuscode = add_action($this->database, $note, 21,
378             //            "This notification was displayed with others from the same object");
379          }
380          echo "</table>";
381       }
382       else if ($_SERVER['REQUEST_METHOD'] == 'GET' && !empty($_GET['notid']))
383       {
384          clientscripts();
385
386          //  Display all information about a single notification['notid']
387
388          $local_sql = "SELECT * from notification
389                        WHERE notificationid = CAST('" . $_GET['notid'] . "' AS BIGINT)";
390          $res = pg_exec($this->database, $local_sql);
391          $note = pg_fetch_object($res, 0);
392
393          //Gather proof that the notification has been presented
394          if ($note->statuscode == 'new')
395          {
396             $note->statuscode = add_action($this->database, $note, 2,
397                                   "Notification displayed in detail through web interface");
398          }
399          else
400          {
401             $note->statuscode = add_action($this->database, $note, 20,
402                                   "Notification redisplayed in detail through web interface");
403          }
404          $this->display_notification($note);
405          echo "<hr>";
406          $this->notification_form($note);
407          $this->notification_list($note->objectid);
408       }
409       else if ($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_GET['notid']))
410       {
411          clientscripts();
412
413          //  Display all information about a single notification['notid']
414
415          $res = pg_exec($this->database, "SELECT * from notification
416                                           WHERE notificationid=" . $_GET['notid']);
417          $note = pg_fetch_object($res, 0);
418
419          if (!empty($_POST['act_on_all']))
420          {
421             //   Perform the action on all notifications with the same object and issue.
422             $res = pg_exec($this->database, "SELECT notificationid, timestamp, type_of_issueid, statuscode
423                                        FROM notification WHERE objectid= CAST ('" 
424                                 . $note->objectid ."' AS BIGINT) AND statuscode != 'cls'
425                                   AND type_of_issueid = '" . $note->type_of_issueid
426                                        . "' ORDER BY notificationid");
427             for ($row = 0; $row < pg_num_rows($res); $row++)
428             {
429                $similar_note = pg_fetch_object($res, $row);
430                $new_statuscode = add_action($this->database, $similar_note,
431                                                  $_POST['actionid'], $_POST['remark']);
432                if ($similar_note->notificationid == $note->notificationid)
433                {
434                   $note->statuscode = $new_statuscode;
435                }
436             }
437          }
438          else
439          {
440             $note->statuscode = add_action($this->database, $note, $_POST['actionid'], $_POST['remark']);
441          }
442          $this->display_notification($note);
443          echo "<hr>";
444          $this->notification_form($note);
445          $this->notification_list($note->objectid);
446       }
447       else
448       {
449          echo "<h1>Insufficient input to create page</h1>";
450          phpinfo();
451       }
452    }
453 }
454
455 $notif_page = new notification_page("Gnucomo Notifications");
456
457 $notif_page->Showpage();
458
459 ?>