-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprogutils.php
381 lines (319 loc) · 12.2 KB
/
progutils.php
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<?php
/**
* @package com_meego_devprogram
* @author Ferenc Szekely, http://www.nemein.com
* @copyright The Midgard Project, http://www.midgard-project.org
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
*/
class com_meego_devprogram_progutils extends com_meego_devprogram_utils
{
/**
* Adds some handy properties to program object
*
* @param object com_meego_devprogram_program object
* @return object extended com_meego_devprogram_program object
*/
private static function extend_program($object = null)
{
// q->toggle_readonly(false) does not work so we need a new object
$program = new com_meego_devprogram_program($object->guid);
// set some urls, they can come handy
$program->open_programs_url = com_meego_devprogram_utils::get_url('open_programs', array ());
$program->read_url = com_meego_devprogram_utils::get_url('program_read', array ('program_name' => $program->name));
$program->update_url = com_meego_devprogram_utils::get_url('program_update', array ('program_name' => $program->name));
$program->delete_url = com_meego_devprogram_utils::get_url('program_delete', array ('program_name' => $program->name));
$program->apply_url = com_meego_devprogram_utils::get_url('my_application_create', array ('program_name' => $program->name));
// reformat the duedate value so that templates and controllers don't have to bother
$program->deadline = date('Y-m-d', strtotime($program->duedate));
// create a placeholder for applications to this program by the curent user
$program->myapps = array();
// if current user is owner then we can add more goodies
$user = com_meego_devprogram_utils::get_current_user();
if (is_object($user))
{
$program->read_my_url = com_meego_devprogram_utils::get_url('my_program_read', array ('program_name' => $program->name));
$program->list_apps_url = com_meego_devprogram_utils::get_url('program_applications', array ('program_name' => $program->name));
// set the number of apps (all but the cancelled ones) under this program
$program->number_of_applications = com_meego_devprogram_apputils::get_count_applications_by_program($program->id);
// gather all apps of this user to this program
//$program->myapps = com_meego_devprogram_apputils::get_applications_of_current_user($object->id);
}
// get the provider of the device and set a new property to the program
$devices = com_meego_devprogram_devutils::get_devices(array('id' => $program->device));
$providers = com_meego_devprogram_provutils::get_providers(array('guid' => $devices[0]->providerobject->guid));
$program->provider = $providers[0];
// by default everybody can apply
$program->can_apply = true;
// current date and time
$now = new DateTime();
if ($object->duedate < $now)
{
// set the flag so we can show a user friendly status box
$program->closed = true;
}
if ( com_meego_devprogram_utils::is_current_user_creator($object)
|| ( count($program->myapps)
&& ! $object->multiple)
|| ($object->duedate < $now))
{
// owners of a program or admins should not apply for that program
// or
// if user applied then we disable further applications
// unless the program accepts multiple entries from the same person
$program->can_apply = false;
}
return $program;
}
/**
* Load a program by its guid or id
*
* @param string guid or id of the program
* @return object an extended com_meego_devprogram_program object
* added some useful urls as new properties
*/
public function get_program_by_id($id = 0)
{
$program = null;
if ($id)
{
$program = self::get_programs(array('id' => $id));
}
return $program;
}
/**
* Retrieves programs using various filters
*
* @param array filters, possible members: name, creator, status
* if multiple members used then we do a logical AND with them
* @return object com_meego_devprogram_program object
*/
public static function get_programs(array $filters)
{
$programs = array();
$storage = new midgard_query_storage('com_meego_devprogram_program');
$q = new midgard_query_select($storage);
if (count($filters) > 1)
{
$qc = new midgard_query_constraint_group('AND');
}
foreach ($filters as $filter => $value)
{
switch ($filter)
{
case 'deleted':
if ($value)
{
$q->include_deleted(true);
}
break;
case 'creator':
// check if the value is a real guid
if (mgd_is_guid($value))
{
$constraint = new midgard_query_constraint(
new midgard_query_property('metadata.creator'),
'=',
new midgard_query_value($value)
);
}
break;
case 'id':
case 'name':
case 'device':
$constraint = new midgard_query_constraint(
new midgard_query_property($filter),
'=',
new midgard_query_value($value)
);
break;
case 'status':
// current date and time
$now = date("Y-m-d H:i:s");
switch ($value) {
case CMD_PROGRAM_CLOSED:
$type = '<';
break;
case CMD_PROGRAM_OPEN:
default:
$type = '>';
}
$constraint = new midgard_query_constraint(
new midgard_query_property('duedate'),
$type,
new midgard_query_value($now)
);
break;
case 'daysleft':
// duedate < now + value
$now = new DateTime();
$limit = $now->add(new DateInterval('P' . $value . 'D'));
$constraint = new midgard_query_constraint(
new midgard_query_property('duedate'),
'<',
new midgard_query_value($limit->format('Y-m-d'))
);
break;
}
// set the constraint
(count($filters) > 1) ? $qc->add_constraint($constraint) : $qc = $constraint;
}
$q->set_constraint($qc);
$q->add_order(new midgard_query_property('metadata.created'), SORT_DESC);
$q->execute();
// does not seem to work
// @bug: $q->toggle_read_only(false);
$objects = $q->list_objects();
if (count($objects))
{
foreach ($objects as $object)
{
$programs[] = self::extend_program($object);
}
}
return $programs;
}
/**
* Checks if the user is the owner of the program (or an admin of the site)
* If user is not logged in then it will redirect to login page
*
* @param string name of the program
* @return boolean true if user is owner, false otherwise
*/
public static function is_owner_of_program($name = '')
{
$retval = false;
$user = self::require_login();
if (is_object($user))
{
$programs = self::get_programs(array('name' => $name));
if ( is_array($programs)
&& isset($programs[0]))
{
// do the creator check
if ( $user->is_admin()
|| $program->metadata_creator == $user->person_guid)
{
// for owners and admins return true
$retval = true;
}
}
}
return $retval;
}
/**
* Retrieves all programs of a given user
*
* @param string login name (ie. user name) of the user
* @return array an array of com_meego_devprogram_program objects
*/
public static function get_open_programs_of_user($login = '')
{
$programs = array();
if (! strlen($login))
{
return $programs;
}
// retrieve the user's guid based on the login name
$guid = self::get_guid_of_user($login);
$filters = array('status' => CMD_PROGRAM_OPEN, 'creator' => $guid);
return self::get_programs($filters);
}
/**
* Retrieves programs of the currently loged in user
*
* @return array an array of com_meego_devprogram_programs objects
* null if user is not logged in
*/
public static function get_open_programs_of_current_user()
{
$programs = array();
// retrieve the user's guid based on the login name
$user = self::require_login();
if (! is_object($user))
{
return null;
}
// all devices from this user and by fellow members of the same provider
$memberships = com_meego_devprogram_membutils::get_memberships_of_current_user();
foreach($memberships as $membership)
{
// check status
if ($membership->status == CMD_MEMBERSHIP_APPROVED)
{
// get device objects
$devices = com_meego_devprogram_devutils::get_devices(array('provider' => $membership->provider));
// find all programs belonging all devices found before
foreach ($devices as $device)
{
$filters = array('status' => CMD_PROGRAM_OPEN, 'device' => $device->id);
$programs = array_merge($programs, self::get_programs($filters));
}
}
}
return $programs;
}
/**
* Finds open programs that use a given device
*
* @param integer id of the device
* @return boolean true: if device is used; false otherwise
*/
public function any_open_program_uses_device($id = 0)
{
$retval = false;
if ($id)
{
$filters = array('status' => CMD_PROGRAM_OPEN, 'device' => $id);
$programs = self::get_programs($filters);
if (count($programs))
{
$retval = true;
}
unset($filters, $programs);
}
return $retval;
}
/**
* Deletes all expired programs
* A program is expired if its due date has passed
*/
public function delete_expired_programs()
{
$programs = self::get_programs(array('status' => CMD_PROGRAM_CLOSED));
foreach($programs as $program)
{
$program->delete();
}
}
/**
* Retrieves the latest program
*
* @return object extended com_meego_devprogram_program object
*/
public function get_latest_program()
{
$program = null;
$programs = self::get_programs(array('status' => CMD_PROGRAM_OPEN));
if (count($programs))
{
$program = array_shift($programs);
}
return $program;
}
/**
* Retrieves the programs which are soon to be closed
* The days left for closing can be configured
*
* @return object extended com_meego_devprogram_program object
*/
public function get_closing_programs($daysleft = 0)
{
$mvc = midgardmvc_core::get_instance();
if (! $daysleft)
{
$daysleft = $mvc->configuration->daysleft;
}
$programs = self::get_programs(array('status' => CMD_PROGRAM_OPEN, 'daysleft' => $daysleft));
return $programs;
}
}