Keep a record of each time a notification
[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.3 $
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.3  2003-02-21 08:49:16  arjen
40    Keep a record of each time a notification
41    is displayed. Either on a listing or in detail.
42
43    Revision 1.2  2003/02/13 09:01:29  arjen
44    All web interface pages use the page class.
45
46    Revision 1.1  2003/02/05 09:48:14  arjen
47    Added display and handling of notifications
48
49 ******************************/
50
51 // RCSID = "$Id: notification.php,v 1.3 2003-02-21 08:49:16 arjen Exp $";
52
53 ini_set('include_path', '.:./classes:../phpclasses');
54
55 require_once('page.class.php');
56
57 function clientscripts()
58 {
59 ?>
60
61 <script language='JavaScript'>
62
63 function CheckRemark(f)
64 {
65    if (f.remark.value == "")
66    {
67       alert("Your remark is still empty");
68       return false;
69    }
70    return true;
71 }
72
73 </script>
74
75 <?php
76 }
77
78 /*
79  *   Add an action to the notification $note.
80  *   The status of the notification may alter as a result.
81  */
82
83 function add_action($db, $note, $actionid, $remark)
84 {
85    $r = pg_exec($db, "SELECT statuscode FROM action WHERE actionid=CAST('" . $actionid."' AS BIGINT)");
86    $action = pg_fetch_object($r, 0);
87
88    if ($action->statuscode != "" && $note->statuscode != $action->statuscode)
89    {
90       //  Update the status of the notification.
91
92       $note->statuscode = $action->statuscode;
93
94       pg_exec($db, "UPDATE notification SET statuscode='" . $note->statuscode
95                 . "' WHERE notificationid = " . $note->notificationid);
96    }
97
98    //  Add the action to the action_user table.
99
100    pg_exec($db, "INSERT INTO action_user (actionid, username, notificationid,
101                            timestamp, statuscode, remarks) values ('$actionid', '"
102                          . $_SESSION['username'] . "', '" . $note->notificationid
103                          . "', '" . date('Y-m-d H:i:s') . "', '" . $note->statuscode
104                          . "', '" . $remark . "')");
105
106    return $note->statuscode;
107 }
108
109 class notification_page extends page
110 {
111
112    function display_notification($note)
113    {
114       $res = pg_exec($this->database, "SELECT objectname FROM object WHERE objectid= CAST ('" . $note->objectid . "' AS BIGINT)");
115       $obj = pg_fetch_object($res, 0);
116       echo "<h1>Notification " . $note->notificationid . " for " . $obj->objectname . "</h1><hr>";
117
118       $r = pg_exec($this->database, "SELECT description FROM type_of_issue WHERE type_of_issueid = CAST ('"
119                        . $note->type_of_issueid . "' AS BIGINT)");
120       $issue = pg_fetch_object($r, 0);
121
122       $r = pg_exec($this->database, "SELECT statusname FROM status
123                                      WHERE statuscode = '" . $note->statuscode . "'");
124
125       $status = pg_fetch_object($r, 0);
126
127       echo "Issue : " . $issue->description . ", Priority " . $note->priority . "<br>";
128       echo "Creation time : " . $note->timestamp . "<br>";
129       echo "Current status : " . $status->statusname . "<br>";
130
131       //  List all actions that occurred on this notification
132
133       echo "<h2>Action history for notification " . $note->notificationid . "</h2>";
134       echo "<table>";
135       echo "<tr><th>Time</th><th>User</th><th>Remarks</th></tr>";
136
137       $r = pg_exec($this->database, "SELECT actionid, username, timestamp, remarks
138                                      FROM action_user WHERE notificationid= CAST ('" 
139                                      . $note->notificationid . "' AS BIGINT )"
140                                      ." ORDER BY timestamp");
141       for ($row = 0; $row < pg_numrows($r); $row++)
142       {
143          $action_user = pg_fetch_object($r, $row);
144
145          echo "<tr><td class='time'>";
146          echo $action_user->timestamp;
147          echo "</td><td>";
148          echo $action_user->username;
149          echo "</td><td>";
150          echo htmlspecialchars(stripslashes($action_user->remarks));
151          echo "</td></tr>";
152       }
153       echo "</table>";
154
155       //  See if we can list any parameters for this notification
156
157       $r = pg_exec($this->database, "SELECT * FROM parameter WHERE paramid IN
158                                      ( SELECT paramid FROM parameter_notification
159                                      WHERE notificationid = CAST ('" . $note->notificationid 
160                                      . "' AS BIGINT) )");
161       if (pg_numrows($r) > 0)
162       {
163          echo "<h2>Parameters involved in this notification</h2>";
164          echo "<table>";
165          echo "<tr><th>Class</th><th>Name</th><th>Description</th><th>History</th></tr>";
166
167          for ($row = 0; $row < pg_numrows($r); $row++)
168          {
169             $p = pg_fetch_object($r, $row);
170             $res = pg_exec($this->database, "SELECT * from history WHERE paramid= CAST('" . $p->paramid
171                                             . "' AS BIGINT) ORDER BY modified");
172
173             echo "<tr><td>";
174             echo $p->class;
175             echo "</td><td>";
176             echo $p->name;
177             echo "</td><td>";
178             echo $p->description;
179             echo "</td><td>";
180             for ($hrow = 0; $hrow < pg_numrows($res); $hrow++)
181             {
182                $hist = pg_fetch_object($res, $hrow);
183                echo $hist->modified . " ";
184                echo $hist->change_nature . " ";
185                echo $hist->changed_property . " ";
186                echo $hist->new_value;
187                echo "<br>";
188             }
189             echo "</td></tr>";
190
191          }
192          echo "</table>";
193       }
194
195       //  See if we can list any log entries for this notification
196
197       $r = pg_exec($this->database, "SELECT * FROM log WHERE logid IN
198                                      ( SELECT logid FROM log_notification
199                                      WHERE notificationid = CAST ('" 
200                                       . $note->notificationid . "' AS BIGINT))");
201       if (pg_numrows($r) > 0)
202       {
203          echo "<h2>Log entries involved in this notification</h2>";
204          echo "<table>";
205          echo "<tr><th>Time</th><th>Service</th><th>Log entry</th></tr>";
206
207          for ($row = 0; $row < pg_numrows($r); $row++)
208          {
209             $p = pg_fetch_object($r, $row);
210
211             echo "<tr><td>";
212             echo $p->object_timestamp;
213             echo "</td><td>";
214             echo $p->servicecode;
215             echo "</td><td>";
216             echo $p->rawdata;
217             echo "</td></tr>";
218
219          }
220          echo "</table>";
221       }
222
223    }
224
225    function notification_form($note)
226    {
227       /*
228        *  Depending on the state of the notification, specific actions
229        *  are possible. One such actions determines a transition to
230        *  the next state. The array $possible_action lists the state transitions
231        *  for each state.
232        */
233
234       $possible_action = array
235       (
236          "opn" => array (3, 6, 7),
237          "pen" => array (3, 6, 7, 8, 11, 18),
238          "inv" => array (3, 9),
239          "vrf" => array (12, 13),
240          "cls" => array (19)
241       );
242
243       echo "<h2>Enter a remark for the next action</h2>";
244       echo "<form method='post' onSubmit='return CheckRemark(this)'>";
245       echo "<textarea name='remark'></textarea><br>";
246
247       /*  Display a list of possible actions */
248
249       echo "<h2>Select one of the actions to take below</h2>";
250
251       $first = true;
252
253       foreach ($possible_action[$note->statuscode] as $act)
254       {
255          $action = pg_fetch_object(pg_exec("SELECT description FROM action WHERE actionid= CAST('"
256           . $act . "' AS BIGINT)"),0);
257          echo "<input type='radio' name='actionid'";
258          if ($first)
259          {
260             $first = false;
261             echo " checked='on'";
262          }
263          echo " value='$act'>" . $action->description . "<br>";
264       }
265
266       echo "<input type='submit' value='Submit action'>";
267       echo "</form>";
268    }
269
270    function Body()
271    {
272
273       if ($_SERVER['REQUEST_METHOD'] == 'GET' && !empty($_GET['oid']))
274       {
275
276          //  Display a list of all notifications for this object['oid']
277
278          $res = pg_exec($this->database, "SELECT objectname FROM object WHERE objectid=" . $_GET['oid']);
279          $obj = pg_fetch_object($res, 0);
280          echo "<h1>Notifications for " . $obj->objectname . "</h1><hr>";
281
282          $res = pg_exec($this->database, "SELECT notificationid, timestamp, type_of_issueid,
283                                               statuscode, priority
284                                           FROM notification WHERE objectid= CAST ('" 
285                                           . $_GET['oid'] ."' AS BIGINT) AND statuscode != 'cls'"
286                                           . " ORDER BY notificationid");
287
288          echo "<table>";
289          $row = 0;
290          while ($row < pg_numrows($res))
291          {
292             $note = pg_fetch_object($res, $row);
293             $r = pg_exec($this->database, "SELECT name from type_of_issue WHERE type_of_issueid = CAST('"
294                           . $note->type_of_issueid . "' AS BIGINT)");
295             $issue = pg_fetch_object($r, 0);
296             echo "<tr><td align='center'>\n";
297             echo $note->timestamp;
298             echo "</td><td>";
299             echo $note->notificationid;
300             echo "</td><td>";
301             echo "<a href='notification.php?notid=";
302             echo $note->notificationid;
303             echo "'>";
304             echo $issue->name;
305             echo "</a>";
306             echo "</td><td>";
307             echo $note->statuscode;
308             echo "</td><td>";
309             echo $note->priority;
310             echo "</td></tr>\n";
311             $row++;
312
313             //Save evidence that this notification was presented to the user.
314             $note->statuscode = add_action($this->database, $note, 21,
315                         "This notification was displayed with others from the same object");
316          }
317          echo "</table>";
318       }
319       else if ($_SERVER['REQUEST_METHOD'] == 'GET' && !empty($_GET['notid']))
320       {
321          clientscripts();
322
323          //  Display all information about a single notification['notid']
324
325          $local_sql = "SELECT * from notification
326                        WHERE notificationid = CAST('" . $_GET['notid'] . "' AS BIGINT)";
327          $res = pg_exec($this->database, $local_sql);
328          $note = pg_fetch_object($res, 0);
329
330          //Gather proof that the notification has been presented
331          if ($note->statuscode == 'new')
332          {
333             $note->statuscode = add_action($this->database, $note, 2,
334                                   "Notification displayed in detail through web interface");
335          }
336          else
337          {
338             $note->statuscode = add_action($this->database, $note, 20,
339                                   "Notification redisplayed in detail through web interface");
340          }
341          $this->display_notification($note);
342          echo "<hr>";
343          $this->notification_form($note);
344       }
345       else if ($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_GET['notid']))
346       {
347          clientscripts();
348
349          //  Display all information about a single notification['notid']
350
351          $res = pg_exec($this->database, "SELECT * from notification
352                                           WHERE notificationid=" . $_GET['notid']);
353          $note = pg_fetch_object($res, 0);
354
355          $note->statuscode = add_action($this->database, $note, $_POST['actionid'], $_POST['remark']);
356          $this->display_notification($note);
357          echo "<hr>";
358          $this->notification_form($note);
359       }
360       else
361       {
362          echo "<h1>Insufficient input to create page</h1>";
363          phpinfo();
364       }
365    }
366 }
367
368 $notif_page = new notification_page("Gnucomo Notifications");
369
370 $notif_page->Showpage();
371
372 ?>