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