1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Scriptotek\Alma\Users; |
4
|
|
|
|
5
|
|
|
use Scriptotek\Alma\Client; |
6
|
|
|
use Scriptotek\Alma\Model\Model; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Iterable collection of statistics resources. |
10
|
|
|
*/ |
11
|
|
|
class UserStatistics extends Model |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Get the user's statistics. |
15
|
|
|
* |
16
|
|
|
* @return Array of Statistics. |
17
|
|
|
*/ |
18
|
|
|
public function getStatistics() |
19
|
|
|
{ |
20
|
|
|
$stats = array(); |
21
|
|
|
foreach ($this->data as $statistic) { |
|
|
|
|
22
|
|
|
$stats[] = $statistic; |
23
|
|
|
} |
24
|
|
|
return $stats; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Get the user's statistics. |
29
|
|
|
* |
30
|
|
|
* @return array of statistics. |
31
|
|
|
*/ |
32
|
|
|
public function get() |
33
|
|
|
{ |
34
|
|
|
$stats = array(); |
35
|
|
|
foreach ($this->data as $statistic) { |
|
|
|
|
36
|
|
|
$stats[] = $statistic; |
37
|
|
|
} |
38
|
|
|
return $stats; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Get a statistic. |
43
|
|
|
* |
44
|
|
|
* @param $typeCode code of the category type. |
45
|
|
|
* @param $categoryCode code of the statistical category. |
46
|
|
|
* |
47
|
|
|
* @return Statistic. |
|
|
|
|
48
|
|
|
*/ |
49
|
|
|
public function getStatistic($typeCode,$categoryCode) |
50
|
|
|
{ |
51
|
|
|
$stats = array(); |
52
|
|
|
foreach ($this->data as $statistic) { |
|
|
|
|
53
|
|
|
if (($statistic->category_type->value == $typeCode) && |
54
|
|
|
($statistic->statistic_category->value == $categoryCode)) { |
55
|
|
|
$stats[] = $statistic; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
return $stats; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Add a user statistic with no regard to existing Types and Categories. |
63
|
|
|
* |
64
|
|
|
* @param string $typeCode code of the category type. |
65
|
|
|
* @param string $typeDesc description of the category type. |
66
|
|
|
* @param string $categoryCode code of the statistical category. |
67
|
|
|
* @param string $categoryDesc description of the statistical category. |
68
|
|
|
* @param string $note free text description of the statistic. |
69
|
|
|
* @param string $segment_type (Internal|External) |
70
|
|
|
* |
71
|
|
|
* @return Statistic |
72
|
|
|
*/ |
73
|
|
|
public function addStatisticRaw($typeCode,$typeDesc,$categoryCode,$categoryDesc,$segment_type,$note) |
74
|
|
|
{ |
75
|
|
|
# Create the new statistic object |
76
|
|
|
$stat_obj = (object) [ |
77
|
|
|
'statistic_category' => (object) [ |
78
|
|
|
'value' => $categoryCode, |
79
|
|
|
'desc' => $categoryDesc, |
80
|
|
|
], |
81
|
|
|
'category_type' => (object) [ |
82
|
|
|
'value' => $typeCode, |
83
|
|
|
'desc' => $typeDesc, |
84
|
|
|
], |
85
|
|
|
'statistic_note' => $note, |
86
|
|
|
'segment_type' => $segment_type, |
87
|
|
|
]; |
88
|
|
|
|
89
|
|
|
# Add the object to the user |
90
|
|
|
$this->data[] = $stat_obj; |
91
|
|
|
|
92
|
|
|
/* Not Sure this is quite right or needed. */ |
93
|
|
|
#return Statistic::make($this->client,$stat_obj); |
94
|
|
|
#return($this->data); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Add a user statistic based upon existing Types and Categories. |
99
|
|
|
* |
100
|
|
|
* @param string $typeCode code of the category type. |
101
|
|
|
* @param string $categoryCode code of the statistical category. |
102
|
|
|
* |
103
|
|
|
* @return Statistic |
104
|
|
|
*/ |
105
|
|
|
public function addStatistic($typeCode,$categoryCode,$segmentType,$note) |
106
|
|
|
{ |
107
|
|
|
//* TODO HERE *// |
108
|
|
|
|
109
|
|
|
/* Logic: */ |
110
|
|
|
/* Lookup both $typeCode and $categoryCode in codetable */ |
111
|
|
|
/* Obtain information (code/description) from both code tables. */ |
112
|
|
|
/* If found and ok, add the statistic to the user */ |
113
|
|
|
|
114
|
|
|
/* Code Tables used: |
115
|
|
|
Code Table; UserStatisticalTypes |
116
|
|
|
Code Table: UserStatCategories |
117
|
|
|
*/ |
118
|
|
|
|
119
|
|
|
# Note need some error checking etc. here. |
120
|
|
|
|
121
|
|
|
# Get the Type and Category Descriptions for the codes from the code tables. |
122
|
|
|
$stat = $this->client->conf->codetables->get('UserStatisticalTypes')->getRowByCode($typeCode); |
123
|
|
|
$typeDesc = $stat[0]->description; |
124
|
|
|
|
125
|
|
|
$stat = $this->client->conf->codetables->get('UserStatCategories')->getRowByCode($categoryCode); |
126
|
|
|
$categoryDesc = $stat[0]->description; |
127
|
|
|
# |
128
|
|
|
# These are temporary for testing. |
129
|
|
|
#$typeDesc = 'Test'; |
130
|
|
|
#$categoryDesc = 'Test'; |
131
|
|
|
|
132
|
|
|
# Add the statistic: |
133
|
|
|
$this->addStatisticRaw($typeCode,$typeDesc,$categoryCode,$categoryDesc,$segmentType,$note); |
134
|
|
|
|
135
|
|
|
# For debugging: |
136
|
|
|
#print "TypeCode: $typeCode\nTypeDesc: $typeDesc\nCategoryCode: $categoryCode\nCategoryDesc: $categoryDesc\n"; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Delete a user statistic. |
141
|
|
|
* |
142
|
|
|
* @param string $typeCode code of the category type. |
143
|
|
|
* @param string $categoryCode code of the statistical category. |
144
|
|
|
*/ |
145
|
|
|
public function removeStatistic($typeCode,$categoryCode) |
146
|
|
|
{ |
147
|
|
|
# Old way: |
148
|
|
|
#$max = sizeof($this->data); |
149
|
|
|
#for($i = 0; $i < $max; $i++) { |
150
|
|
|
# if (($this->data[$i]->category_type->value == $typeCode) && ($this->data[$i]->statistic_category->value == $categoryCode)) { |
151
|
|
|
# unset($this->data[$i]); |
152
|
|
|
# $ret = true; |
153
|
|
|
# } |
154
|
|
|
#} |
155
|
|
|
|
156
|
|
|
# New way: (Thanks Rick!) |
157
|
|
|
foreach($this->data as $key => $row) { |
158
|
|
|
if (($row->category_type->value == $typeCode) && ($row->statistic_category->value == $categoryCode)) { |
159
|
|
|
array_splice($this->data, $key, 1); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
return; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Update a user statistic. |
167
|
|
|
* |
168
|
|
|
*/ |
169
|
|
|
public function updateStatistic($fromTypeCode,$fromCategoryCode,$toTypeCode,$toCategoryCode,$segmentType,$note) |
170
|
|
|
{ |
171
|
|
|
/* Remove "from" statistic, then add "to" statistic */ |
172
|
|
|
$this->removeStatistic($fromTypeCode,$fromCategoryCode); |
173
|
|
|
$this->addStatistic($toTypeCode,$toCategoryCode,$segmentType,$note); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Get Stats By Type Code. |
178
|
|
|
* |
179
|
|
|
* @return Array of Statistics. |
180
|
|
|
*/ |
181
|
|
View Code Duplication |
public function getStatsByTypeCode($typeCode) |
|
|
|
|
182
|
|
|
{ |
183
|
|
|
$stats = array(); |
184
|
|
|
foreach ($this->data as $statistic) { |
|
|
|
|
185
|
|
|
if ($statistic->category_type->value == $typeCode) { |
186
|
|
|
array_push($stats,$statistic); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
} |
190
|
|
|
return $stats; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Get Stats by Type Desc. |
195
|
|
|
* |
196
|
|
|
* @return Array of statistics. |
197
|
|
|
*/ |
198
|
|
View Code Duplication |
public function getStatsByTypeDesc($typeDesc) |
|
|
|
|
199
|
|
|
{ |
200
|
|
|
$stats = array(); |
201
|
|
|
foreach ($this->data as $statistic) { |
|
|
|
|
202
|
|
|
if ($statistic->category_type->desc == $typeDesc) { |
203
|
|
|
array_push($stats,$statistic); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
return $stats; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Get Stats by Category Code. |
211
|
|
|
* |
212
|
|
|
* @return Array of Statistics. |
213
|
|
|
*/ |
214
|
|
View Code Duplication |
public function getStatsByCategoryCode($categoryCode) |
|
|
|
|
215
|
|
|
{ |
216
|
|
|
$stats = array(); |
217
|
|
|
foreach ($this->data as $statistic) { |
|
|
|
|
218
|
|
|
if ($statistic->statistic_category->value == $categoryCode) { |
219
|
|
|
array_push($stats,$statistic); |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
return $stats; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Get Stats by Category Desc. |
227
|
|
|
* |
228
|
|
|
* @return Array of Statistics. |
229
|
|
|
*/ |
230
|
|
View Code Duplication |
public function getStatsByCategoryDesc($categoryDesc) |
|
|
|
|
231
|
|
|
{ |
232
|
|
|
$stats = array(); |
233
|
|
|
foreach ($this->data as $statistic) { |
|
|
|
|
234
|
|
|
if ($statistic->statistic_category->desc == $categoryDesc) { |
235
|
|
|
array_push($stats,$statistic); |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
return $stats; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Get Stats by Segment Type. |
243
|
|
|
* |
244
|
|
|
* @return Array of Statistics. |
245
|
|
|
*/ |
246
|
|
|
public function getStatsBySegmentType($segmentType) |
247
|
|
|
{ |
248
|
|
|
$stats = array(); |
249
|
|
|
foreach ($this->data as $statistic) { |
|
|
|
|
250
|
|
|
if ($statistic->segment_type == $segmentType) { |
251
|
|
|
array_push($stats,$statistic); |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
return $stats; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Search Stats by Note. |
259
|
|
|
* |
260
|
|
|
* @return Array of Statistics. |
261
|
|
|
*/ |
262
|
|
|
public function searchStatsByNote($note) |
263
|
|
|
{ |
264
|
|
|
$stats = array(); |
265
|
|
|
foreach ($this->data as $statistic) { |
|
|
|
|
266
|
|
|
if (preg_match("/$note/i", $statistic->statistic_note)) { |
267
|
|
|
array_push($stats,$statistic); |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
return $stats; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Search Stats by Type Code. |
275
|
|
|
* |
276
|
|
|
* @return Array of Statistics. |
277
|
|
|
*/ |
278
|
|
View Code Duplication |
public function searchStatsByTypeCode($typeCode) |
|
|
|
|
279
|
|
|
{ |
280
|
|
|
$stats = array(); |
281
|
|
|
foreach ($this->data as $statistic) { |
|
|
|
|
282
|
|
|
if (preg_match("/$typeCode/i", $statistic->category_type->value)) { |
283
|
|
|
array_push($stats,$statistic); |
284
|
|
|
} |
285
|
|
|
} |
286
|
|
|
return $stats; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Search Stats by Type Code. |
291
|
|
|
* |
292
|
|
|
* @return Array of Statistics. |
293
|
|
|
*/ |
294
|
|
View Code Duplication |
public function searchStatsByTypeDesc($typeDesc) |
|
|
|
|
295
|
|
|
{ |
296
|
|
|
$stats = array(); |
297
|
|
|
foreach ($this->data as $statistic) { |
|
|
|
|
298
|
|
|
if (preg_match("/$typeDesc/i", $statistic->category_type->desc)) { |
299
|
|
|
array_push($stats,$statistic); |
300
|
|
|
} |
301
|
|
|
} |
302
|
|
|
return $stats; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Search Stats by Type Code. |
307
|
|
|
* |
308
|
|
|
* @return Array of Statistics. |
309
|
|
|
*/ |
310
|
|
View Code Duplication |
public function searchStatsByCategoryCode($categoryCode) |
|
|
|
|
311
|
|
|
{ |
312
|
|
|
$stats = array(); |
313
|
|
|
foreach ($this->data as $statistic) { |
|
|
|
|
314
|
|
|
if (preg_match("/$categoryCode/i", $statistic->statistic_category->value)) { |
315
|
|
|
array_push($stats,$statistic); |
316
|
|
|
} |
317
|
|
|
} |
318
|
|
|
return $stats; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Search Stats by Type Code. |
323
|
|
|
* |
324
|
|
|
* @return Array of Statistics. |
325
|
|
|
*/ |
326
|
|
View Code Duplication |
public function searchStatsByCategoryDesc($categoryDesc) |
|
|
|
|
327
|
|
|
{ |
328
|
|
|
$stats = array(); |
329
|
|
|
foreach ($this->data as $statistic) { |
|
|
|
|
330
|
|
|
if (preg_match("/$categoryDesc/i", $statistic->statistic_category->desc)) { |
331
|
|
|
array_push($stats,$statistic); |
332
|
|
|
} |
333
|
|
|
} |
334
|
|
|
return $stats; |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
} |
338
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.