|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (c) 2014 - Arno van Rossum <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
6
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
7
|
|
|
* in the Software without restriction, including without limitation the rights |
|
8
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
9
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
10
|
|
|
* furnished to do so, subject to the following conditions: |
|
11
|
|
|
* |
|
12
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
13
|
|
|
* all copies or substantial portions of the Software. |
|
14
|
|
|
* |
|
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
16
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
18
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
19
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
20
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
21
|
|
|
* THE SOFTWARE. |
|
22
|
|
|
*/ |
|
23
|
|
|
namespace OCA\ocUsageCharts\Entity\Activity; |
|
24
|
|
|
|
|
25
|
|
|
use OC_DB_StatementWrapper; |
|
26
|
|
|
use OCP\AppFramework\Db\Mapper; |
|
27
|
|
|
use OCP\IDb; |
|
28
|
|
|
|
|
29
|
|
|
class ActivityUsageRepository extends Mapper |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* @param IDb $db |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct(IDb $db) { |
|
35
|
|
|
parent::__construct($db, 'activity', '\OCA\ocUsageCharts\Entity\Activity\ActivityUsage'); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Find all activity usages after the created date |
|
40
|
|
|
* When username is given, it will return only activities for that user |
|
41
|
|
|
* |
|
42
|
|
|
* @param \DateTime $created |
|
43
|
|
|
* @param string $username |
|
44
|
|
|
* @return ActivityUsage[] |
|
45
|
|
|
*/ |
|
46
|
|
|
public function findAfterCreatedByUsername(\DateTime $created, $username) { |
|
47
|
|
|
$params = array( |
|
48
|
|
|
$username, |
|
49
|
|
|
$created->getTimestamp() |
|
50
|
|
|
); |
|
51
|
|
|
$sql = 'SELECT * FROM `*PREFIX*activity` WHERE `user` = ? AND `timestamp` > ? ORDER BY timestamp DESC'; |
|
52
|
|
|
return $this->findEntities($sql, $params); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Calculate the start and end timestamp based on parameters given |
|
57
|
|
|
* |
|
58
|
|
|
* @param integer $month |
|
59
|
|
|
* @param \DateTime $created |
|
60
|
|
|
* @return array |
|
61
|
|
|
*/ |
|
62
|
|
|
private function calculateStartAndEndTimestamp($month, \DateTime $created) |
|
63
|
|
|
{ |
|
64
|
|
|
$endTimestamp = $created->getTimestamp(); |
|
65
|
|
|
|
|
66
|
|
|
// First month is first day for this month |
|
67
|
|
|
if ( $month == 1 ) |
|
68
|
|
|
{ |
|
69
|
|
|
$created->setDate($created->format('Y'), $created->format('m'), 1); |
|
70
|
|
|
} |
|
71
|
|
|
else |
|
72
|
|
|
{ |
|
73
|
|
|
$created->sub(new \DateInterval('P1M')); |
|
74
|
|
|
} |
|
75
|
|
|
$startTimestamp = $created->getTimestamp(); |
|
76
|
|
|
return array($startTimestamp, $endTimestamp); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Find all activity usages grouped by username and month |
|
81
|
|
|
* When username supplied, only for that user |
|
82
|
|
|
* With a maximum of going back 1 year |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $username |
|
85
|
|
|
* @return array |
|
86
|
|
|
*/ |
|
87
|
|
|
public function findAllPerMonth($username = '') |
|
88
|
|
|
{ |
|
89
|
|
|
$return = array(); |
|
90
|
|
|
$created = new \DateTime(); |
|
91
|
|
|
// Months, maximum 1 year |
|
92
|
|
|
$runs = 12; |
|
93
|
|
|
for($i = 1; $i <= $runs; $i++) |
|
94
|
|
|
{ |
|
95
|
|
|
list($startTimestamp, $endTimestamp) = $this->calculateStartAndEndTimestamp($i, $created); |
|
96
|
|
|
if ( $username !== '' ) |
|
97
|
|
|
{ |
|
98
|
|
|
$return[$created->format('Y-m-d')] = $this->findActivitiesBetweenTimestampAndUser($startTimestamp, $endTimestamp, $username); |
|
99
|
|
|
} |
|
100
|
|
|
else |
|
101
|
|
|
{ |
|
102
|
|
|
$return[$created->format('Y-m-d')] = $this->findActivitiesBetweenTimestamp($startTimestamp, $endTimestamp); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
return $return; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Return all activities for all users between a certain timestamp |
|
110
|
|
|
* |
|
111
|
|
|
* @param integer $startTimestamp |
|
112
|
|
|
* @param integer $endTimestamp |
|
113
|
|
|
* @return array |
|
114
|
|
|
*/ |
|
115
|
|
|
private function findActivitiesBetweenTimestamp($startTimestamp, $endTimestamp) |
|
116
|
|
|
{ |
|
117
|
|
|
$sql = 'SELECT user, count(1) as activities, user FROM *PREFIX*activity WHERE timestamp >= ? AND timestamp < ? GROUP BY user'; |
|
118
|
|
|
$params = array( |
|
119
|
|
|
$startTimestamp, $endTimestamp |
|
120
|
|
|
); |
|
121
|
|
|
return $this->findActivitiesBySQLAndParams($sql, $params); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* * Return all activities between a certain timestamp, for a specific username |
|
126
|
|
|
* |
|
127
|
|
|
* @param integer $startTimestamp |
|
128
|
|
|
* @param integer $endTimestamp |
|
129
|
|
|
* @param string $username |
|
130
|
|
|
* @return array |
|
131
|
|
|
*/ |
|
132
|
|
|
private function findActivitiesBetweenTimestampAndUser($startTimestamp, $endTimestamp, $username) |
|
133
|
|
|
{ |
|
134
|
|
|
$sql = 'SELECT user, count(1) as activities, user FROM *PREFIX*activity WHERE timestamp >= ? AND timestamp < ? AND user = ? GROUP BY user'; |
|
135
|
|
|
$params = array( |
|
136
|
|
|
$startTimestamp, $endTimestamp, $username |
|
137
|
|
|
); |
|
138
|
|
|
return $this->findActivitiesBySQLAndParams($sql, $params); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @param string $sql |
|
143
|
|
|
* @param array $params |
|
144
|
|
|
* @return array |
|
145
|
|
|
*/ |
|
146
|
|
|
private function findActivitiesBySQLAndParams($sql, $params) |
|
147
|
|
|
{ |
|
148
|
|
|
$query = $this->db->prepareQuery($sql); |
|
149
|
|
|
$result = $query->execute($params); |
|
150
|
|
|
return $this->parsePerMonthEntities($result); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Just some method to parse database results for |
|
155
|
|
|
* |
|
156
|
|
|
* @param OC_DB_StatementWrapper $result |
|
157
|
|
|
* @return array |
|
158
|
|
|
*/ |
|
159
|
|
|
private function parsePerMonthEntities($result) |
|
160
|
|
|
{ |
|
161
|
|
|
$entities = array(); |
|
162
|
|
|
while($row = $result->fetch()){ |
|
163
|
|
|
if ( !isset($entities[$row['user']])) |
|
164
|
|
|
{ |
|
165
|
|
|
$entities[$row['user']] = ""; |
|
166
|
|
|
} |
|
167
|
|
|
$entities[$row['user']] = $row['activities']; |
|
168
|
|
|
} |
|
169
|
|
|
return $entities; |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|