Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
52.94% covered (warning)
52.94%
18 / 34
0.00% covered (danger)
0.00%
0 / 1
CRAP
n/a
0 / 0
register_ticket_activity
50.00% covered (danger)
50.00%
16 / 32
0.00% covered (danger)
0.00%
0 / 1
38.50
1<?php
2    include_once __DIR__."/registry.php";
3    require_once __DIR__."/../legacy/ddbb/conx.php";
4
5    /**
6     * Register ticket activity. Uses tenant DB.
7     *
8     * @param \ConxHelper $conx Connection helper (tenant must be set).
9     * @param string $session Session id.
10     * @param array $entrynfo Ticket entry info with 'place' and optionally 'objid'.
11     * @return int ApplicationError code.
12     */
13    function register_ticket_activity(\ConxHelper $conx, $session, $entrynfo){
14        $retval = ApplicationError::Success;
15
16        if (!array_key_exists('place', $entrynfo) || ($entrynfo['place'] == null)){
17            addlog(__FILE__, LogLevel::Error, "Ticket without place: ".PHP_EOL.print_r($entrynfo, true).PHP_EOL);
18            $retval = ApplicationError::Invalid;
19        }
20        if ($conx->tenant === null) {
21            addlog(__FILE__, LogLevel::Error, "Tenant connection not available for register_ticket_activity.");
22            $retval = ApplicationError::Database;
23        }
24        if (success($retval)){
25            $dstpath = AppConstants::RegLogPath();
26            if (!file_exists($dstpath) && !mkdir($dstpath, 0775)){
27                addlog(__FILE__, LogLevel::Error, "Could not create local registry log folder in '".$dstpath."'");
28                $error = error_get_last();
29                if ($error) addlog(__FILE__, LogLevel::Debug, $error['message']);
30            }
31
32            if (is_dir($dstpath)){
33                $dstfile = sprintf('%s/%05d_ticket_activity.csv',  $dstpath, $entrynfo['place']);
34
35                $lineinfo = $entrynfo;
36
37                // replace non existing entry fields with the ones in the database entry
38                if (array_key_exists('objid', $entrynfo)){
39                    $results = [];
40                    $res = $conx->tenant->query('TICKET', [ 'objid' => $entrynfo['objid'] ], $results);
41                    if ($res){
42                        if (count($results) > 0){
43                            $ticket = $results[0];
44                            foreach($ticket as $field => &$value){
45                                if (!array_key_exists($field, $lineinfo)){
46                                    $lineinfo[$field] = $value;
47                                }
48                            } unset($value);
49
50                            $lineinfo['session'] = $session;    // replace the ticket session with the current session
51                        }
52                        else {
53                            addlog(__FILE__, LogLevel::Error, "Ticket not found for objid [".$entrynfo['objid']."]");
54                            $retval = ApplicationError::NotFound;
55                        }
56                    }
57                    else {
58                        $retval = ApplicationError::Database;
59                    }
60                }
61
62                append_logline($dstfile, $conx->tenant->now(), $entrynfo['place'], LogEvent::TicketUpdate, $lineinfo);
63            }
64        }
65
66        return $retval;
67    }
68?>