File: class.mylog.php

package info (click to toggle)
collabtive 2.0%2Bdfsg-5
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 17,708 kB
  • ctags: 27,425
  • sloc: php: 100,872; sh: 72; makefile: 50
file content (230 lines) | stat: -rw-r--r-- 6,380 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
/*
 * This class provides methods to realize the logging of different activities
 *
 * @author Philipp Kiszka <info@o-dyn.de>
 * @name mylog
 * @version 1.0
 * @package Collabtive
 * @link http://www.o-dyn.de
 * @license http://opensource.org/licenses/gpl-license.php GNU General Public License v3 or later
 */
class mylog {
    /*
    * Constructor
    */
    function __construct()
    {
        $this->userid = getArrayVal($_SESSION, "userid");
        $this->uname = getArrayVal($_SESSION, "username");
    }

    /*
     * Add an event log entry
     *
     * @param string $name Name of the affected object
     * @param string $type Type of the affected object
     * @param int $action Action (1 = added, 2 = edited, 3 = deleted, 4 = opened, 5 = finished, 6 = assigned, 7 = deleted assignment)
     * @param int $project Project ID
     * @return bool
     */
    function add($name, $type, $action, $project)
    {
        global $conn;
        $user = $this->userid;
        $uname = $this->uname;

        $action = (int) $action;
        $project = (int) $project;

        $now = time();

        $insStmt = $conn->prepare("INSERT INTO log (user,username,name,type,action,project,datum) VALUES (?, ?, ?, ?, ?, ?, ?)");
        $ins = $insStmt->execute(array($user, $uname, $name, $type, $action, $project, $now));
        if ($ins) {
            $insid = $conn->lastInsertId();
            return $insid;
        } else {
            return false;
        }
    }

    /*
     * Delete an event log entry
     *
     * @param int $id Log entry ID
     * @return bool
     */
    function del($id)
    {
        global $conn;
        $id = (int) $id;

        $del = $conn->query("DELETE FROM log WHERE ID = $id LIMIT 1");
        if ($del) {
            return true;
        } else {
            return false;
        }
    }

    /*
     * Return all log entries associated with a given project
     *
     * @param int $project Project ID
     * @param int $limit Number of entries to return
     * @return array $mylog Log entries
     */
    function getProjectLog($project, $lim = 25)
    {
        global $conn;
        $project = (int) $project;
        $lim = (int) $lim;

        $sel = $conn->query("SELECT COUNT(*) FROM log WHERE project = $project ");
        if ($sel) {
	    $num = $sel->fetch();
	}
        $num = $num[0];
        if ($num > 200) {
            $num = 200;
        }
        SmartyPaginate::connect();
        // set items per page
        SmartyPaginate::setLimit($lim);
        SmartyPaginate::setTotal($num);

        $start = SmartyPaginate::getCurrentIndex();
        $lim = SmartyPaginate::getLimit();
        $sql = "SELECT * FROM log WHERE project = $project ORDER BY ID DESC LIMIT $start,$lim";
        $sel2 = $conn->query($sql);

        $mylog = array();
        while ($sel2 and $log = $sel2->fetch()) {
            if (!empty($log)) {
                $sel3 = $conn->query("SELECT name FROM projekte WHERE ID = $log[project]");
		if ($sel3) {
		    $proname = $sel3->fetch();
		    $proname = $proname[0];
		    $log["proname"] = $proname;
		    $log["proname"] = stripslashes($log["proname"]);
		}
                $log["username"] = stripslashes($log["username"]);
                $log["name"] = stripslashes($log["name"]);
                array_push($mylog, $log);
            }
        }

        if (!empty($mylog)) {
            return $mylog;
        } else {
            return false;
        }
    }

    /*
     * Return the log of the latest activities of a given user
     *
     * @param int $user User ID
     * @param int $limit Number of entries to return
     * @return array $mylog Latest entries
     */
    function getUserLog($user, $limit = 25)
    {
        global $conn;
        $user = (int) $user;
        $limit = (int) $limit;

        $sel = $conn->query("SELECT * FROM log WHERE user = $user ORDER BY ID DESC LIMIT $limit");

        $mylog = array();
        while ($sel and $log = $sel->fetch()) {
            $log["username"] = stripslashes($log["username"]);
            $log["name"] = stripslashes($log["name"]);
            array_push($mylog, $log);
        }

        if (!empty($mylog)) {
            return $mylog;
        } else {
            return false;
        }
    }

    /*
     * Return the latest log entries
     *
     * @param int $limit Number of entries to return
     * @return array $mylog Latest entries
     */
    function getLog($limit = 5)
    {
        global $conn;
        $userid = $_SESSION["userid"];
        $limit = (int) $limit;

        $mylog = array();
        $sel3 = $conn->query("SELECT projekt FROM projekte_assigned WHERE user = $userid");
        $prstring = "";
        while ($sel3 and $upro = $sel3->fetch()) {
            $projekt = $upro[0];
            $prstring .= $projekt . ",";
        }

        $prstring = substr($prstring, 0, strlen($prstring)-1);

        if ($prstring) {
            $sel = $conn->query("SELECT * FROM log  WHERE project IN($prstring) OR project = 0 ORDER BY ID DESC LIMIT $limit");

            while ($sel and $log = $sel->fetch()) {
                $sel2 = $conn->query("SELECT name FROM projekte WHERE ID = $log[project]");
                if ($sel2) {
		    $proname = $sel2->fetch();
		    $proname = $proname[0];
		    $log["proname"] = $proname;
		    $log["proname"] = stripslashes($log["proname"]);
		}
                $log["username"] = stripslashes($log["username"]);
                $log["name"] = stripslashes($log["name"]);
                array_push($mylog, $log);
            }
        }

        if (!empty($mylog)) {
            return $mylog;
        } else {
            return false;
        }
    }

    /*
     * Format the date of an entry
     *
     * @param int $log Log entry ID
     * @param string $format Wanted format
     * @return array $log Entry with the formatted time
     */
    function formatdate($log, $format = "")
    {
        if (!$format) {
            $format = CL_DATEFORMAT . " (H:i:s)";
        }
        $cou = 0;

        if ($log) {
            foreach($log as $thelog) {
                $datetime = date($format, $thelog[7]);
                $log[$cou]["datum"] = $datetime;
                $cou = $cou + 1;
            }
        }

        if (!empty($log)) {
            return $log;
        } else {
            return false;
        }
    }
}

?>