GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — develop ( 64c0c8...2d0c86 )
by Luis Ramón
05:14
created

activities.php ➔ parseEvents()   C

Complexity

Conditions 9
Paths 32

Size

Total Lines 50
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 9
eloc 29
c 2
b 1
f 0
nc 32
nop 5
dl 0
loc 50
rs 6
1
<?php
2
3
/*  ATICA - Web application for supporting Quality Management Systems
4
  Copyright (C) 2009-2015: Luis-Ramón López López
5
6
  This program is free software: you can redistribute it and/or modify
7
  it under the terms of the GNU Affero General Public License as published by
8
  the Free Software Foundation, either version 3 of the License, or
9
  (at your option) any later version.
10
11
  This program is distributed in the hope that it will be useful,
12
  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
  GNU Affero General Public License for more details.
15
16
  You should have received a copy of the GNU Affero General Public License
17
  along with this program.  If not, see [http://www.gnu.org/licenses/]. */
18
19
$app->get('/actividades(/:id)', function ($id = null) use ($app, $user, $config, $organization) {
20
    if (!$user) {
21
        $app->redirect($app->urlFor('login'));
22
    }
23
24
    // indica si el perfil pertence al usuario
25
    $isMine = true;
26
27
    // obtener perfiles
28
    $profiles = parseArray(getUserProfiles($user['id'], $organization['id'], false));
29
30
    // barra superior de perfiles
31
    $profile_bar = array();
32
33
    if (count($profiles, COUNT_NORMAL)>1) {
34
       $profile_bar[] = array('caption' => 'Ver todas', 'active' => ($id == null), 'target' => $app->urlFor('activities'));
35
    }
36
37
    $current = null;
38
    $detail = '';
39
    $profile_ids = array();
40
    $profile_group_ids = array();
41
    foreach ($profiles as $profile) {
42
        $gender = array ($profile['display_name_neutral'], $profile['display_name_male'], $profile['display_name_female']);
43
        $caption = $gender[$user['gender']] . ' ' . $profile['display_name'];
44
        if ($profile['id'] == $id) {
45
            $current = $profile;
46
            $detail = $caption;
47
            $active = true;
48
        }
49
        else {
50
            $active = false;
51
        }
52
        array_push($profile_ids, $profile['id']);
53
        if (!in_array($profile['profile_group_id'], $profile_group_ids)) {
54
            $profile_group_ids[] = $profile['profile_group_id'];
55
        }
56
        array_push($profile_bar, array('caption' => $caption,
57
            'active' => $active, 'target' => $app->urlFor('activities', array('id' => $profile['id']))));
58
    }
59
60
    // obtener otros perfiles
61
    $otherProfiles = getUserOtherProfiles($organization['id'], $profile_group_ids);
62
    if (count($otherProfiles, COUNT_NORMAL) > 0) {
63
        $other_profile_bar = array();
64
65
        foreach ($otherProfiles as $profile) {
66
            $captionOther = $profile['display_name_neutral'] . ' ' . $profile['display_name'];
67
            if ($profile['id'] == $id) {
68
                $current = $profile;
69
                $detail = $captionOther;
70
                $activeOther = true;
71
                $isMine = false;
72
            }
73
            else {
74
                $activeOther = false;
75
            }
76
            array_push($other_profile_bar, array('caption' => $captionOther,
77
                'active' => $activeOther, 'target' => $app->urlFor('activities', array('id' => $profile['id']))));
78
        }
79
        array_push($profile_bar, array(
80
            'caption' => 'Otros perfiles',
81
            'target' => '/',
82
            'subitems' => $other_profile_bar
83
        ));
84
    }
85
86
    $topbar = array();
87
88
    array_push($topbar, $profile_bar);
89
90
    // si hay un perfil como parámetro que no está asociado al usuario, redirigir
91
    if ((null != $id) && (null == $current)) {
92
        $app->redirect($app->urlFor('activities'));
93
    }
94
95
    // barra superior de navegación
96
    if (null != $id) {
97
        $breadcrumb = array(
98
            array('display_name' => 'Actividades', 'target' => $app->urlFor('activities')),
99
            array('display_name' => $detail)
100
        );
101
    }
102
    else {
103
        $breadcrumb = array(
104
            array('display_name' => 'Actividades')
105
        );
106
    }
107
108
    if ($id) {
109
        // si es un perfil del usuario, extraer el perfil contenedor
110
        // si no, dejarlo como estaba
111
        if (isset($profiles[$id])) {
112
            $profile_ids = array ( $id );
113
            $profile_group_ids = array ($profiles[$id]['profile_group_id'] );
114
        }
115
        else {
116
            $profile_ids = array();
117
            $profile_group_ids = array( $id );
118
            $isMine = false;
119
        }
120
    }
121
122
    // obtener actividades
123
    $events = getEventsForProfiles($profile_ids, $profile_group_ids, $user, $config['calendar.base_week']);
124
125
    // formatear los eventos en grupos de perfiles de arrays
126
    $parsedEvents = parseEvents($events,
127
            'profile_id', array('profile_display_name', 'profile_group_display_name', 'profile_id'),
128
            'activity_id', array('activity_display_name', 'activity_description', 'activity_id'));
129
130
    $now = getdate();
131
    $currentWeek = ($now['mon']-1)*4 + floor(($now['mday']-1)/7);
132
133
    // generar página
134
    $app->render('activities.html.twig', array(
135
        'navigation' => $breadcrumb, 'search' => true,
136
        'detail' => $detail,
137
        'base' => $config['calendar.base_week'],
138
        'current' => $currentWeek,
139
        'topbar' => $topbar,
140
        'isMine' => $isMine,
141
        'events' => $parsedEvents));
142
})->name('activities');
143
144
$app->map('/grupoactividad/:id', function ($id) use ($app, $user, $config, $organization) {
145
146
    if (!$user || !$user['is_admin']) {
147
        $app->redirect($app->urlFor('login'));
148
    }
149
150
    if (0 != $id) {
151
        $activity = getActivityObject($organization['id'], $id);
152
153
        if (!$activity) {
154
            $app->redirect($app->urlFor('frontpage'));
155
        }
156
    }
157
    else {
158
        $activity = array();
159
    }
160
161
    if ((0 != $id) && isset($_POST['delete'])) {
162
163
        ORM::get_db()->beginTransaction();
164
        $ok = $activity->delete();
165
166
        if ($ok) {
167
            $app->flash('save_ok', 'delete');
168
            ORM::get_db()->commit();
169
            $app->redirect($app->urlFor('manageallevents'));
170
        }
171
        else {
172
            $app->flash('save_error', 'delete');
173
            ORM::get_db()->rollback();
174
            $app->redirect($app->request()->getPathInfo());
175
        }
176
    }
177
178
    if (isset($_POST['saveactivity'])) {
179
        ORM::get_db()->beginTransaction();
180
181
        if ($id == 0) {
182
            $local = ORM::for_table('activity')->create();
183
        }
184
        else {
185
            $local = $activity;
186
        }
187
        $local->set('organization_id', $organization['id']);
188
        $local->set('display_name', $_POST['displayname']);
189
        $local->set('description', strlen($_POST['description'])>0 ? $_POST['description'] : null);
190
191
        if ($local->save()) {
192
            $app->flash('save_ok', 'ok');
193
                ORM::get_db()->commit();
194
        }
195
        else {
196
            $app->flash('save_error', 'error');
197
            ORM::get_db()->rollBack();
198
        }
199
        $app->redirect($app->request()->getPathInfo());
200
    }
201
202
    $breadcrumb = array(
203
        array('display_name' => 'Actividades', 'target' => $app->urlFor('activities')),
204
        array('display_name' => ($id == 0) ? 'Nueva agrupación de actividades' : $activity['display_name'])
205
    );
206
207
    // generar página
208
    $app->render('manage_activity.html.twig', array(
209
        'navigation' => $breadcrumb,
210
        'url' => $app->request()->getPathInfo(),
211
        'new' => ($id == 0),
212
        'activity' => ($id == 0) ? array() : $activity));
213
214
})->name('manageactivity')->via('GET', 'POST');
215
216
$app->map('/actividad/listar', function () use ($app, $user, $organization) {
217
    if (!$user && $user['is_admin']) {
218
        $app->redirect($app->urlFor('login'));
219
    }
220
221
    if (isset($_POST['delete'])) {
222
        ORM::get_db()->beginTransaction();
223
        $ok = true;
224
225
        foreach($_POST['item'] as $item) {
226
            $ok = $ok && deleteEvent($organization['id'], $item);
227
        }
228
229
        if ($ok) {
230
            ORM::get_db()->commit();
231
            $app->flash('save_ok', 'ok');
232
        }
233
        else {
234
            ORM::get_db()->rollBack();
235
            $app->flash('save_error', 'error');
236
        }
237
        $app->redirect($app->request()->getPathInfo());
238
    }
239
240
    $breadcrumb = array(
241
        array('display_name' => 'Actividades', 'target' => $app->urlFor('activities')),
242
        array('display_name' => 'Gestionar actividades')
243
    );
244
245
    $events = getAllEventsGroupedByActivity($organization['id']);
246
    $activities = getAllActivities($organization['id']);
247
    $folders = getAllFoldersFromActivities($organization['id']);
248
    $profilesEvent = getProfilesForAllEvents($organization['id']);
249
    $allProfiles = parseArray(getProfilesByOrganization($organization['id'], false, true));
250
251
    $app->render('manage_all_event.html.twig', array(
252
        'navigation' => $breadcrumb, 'search' => true,
253
        'select2' => true,
254
        'url' => $app->request()->getPathInfo(),
255
        'events' => $events,
256
        'activities' => $activities,
257
        'profiles_event' => $profilesEvent,
258
        'all_profiles' => $allProfiles,
259
        'back_url' => $app->urlFor('activities'),
260
        'folders' => $folders)
261
    );
262
})->name('manageallevents')->via('GET', 'POST');
263
264
function getUserProfiles($user_id, $org_id, $extended) {
265
    // $extended indica si queremos recibir también los perfiles generales
266
    $data = ORM::for_table('person_profile')->
267
            select('profile.*')->
268
            select('profile_group.display_name_neutral')->
269
            select('profile_group.display_name_male')->
270
            select('profile_group.display_name_female')->
271
            inner_join('profile', array('person_profile.profile_id','=','profile.id'))->
272
            inner_join('profile_group', array('profile_group.id','=','profile.profile_group_id'))->
273
            where('person_id', $user_id)->
274
            where('profile_group.organization_id', $org_id)->
275
            order_by_asc('profile_group.display_name_neutral')->
276
            order_by_asc('profile.order_nr')->find_array();
277
278
    if ($extended) {
279
        $data = array_merge($data,
280
            ORM::for_table('person_profile')->
281
                select('profile.profile_group_id')->
282
                select_expr('null', 'display_name')->
283
                select('profile.order_nr')->
284
                select('profile_group.id', 'id')->
285
                select('profile_group.display_name_neutral')->
286
                select('profile_group.display_name_male')->
287
                select('profile_group.display_name_female')->
288
                inner_join('profile', array('person_profile.profile_id','=','profile.id'))->
289
                inner_join('profile_group', array('profile_group.id','=','profile.profile_group_id'))->
290
                where('person_id', $user_id)->
291
                where_not_null('profile.display_name')->
292
                where('profile_group.organization_id', $org_id)->
293
                order_by_asc('profile_group.display_name_neutral')->
294
                order_by_asc('profile.order_nr')->find_array()
295
        );
296
    }
297
298
    return $data;
299
}
300
301
function getUserOtherProfiles($org_id, $current) {
302
    $data = ORM::for_table('profile_group')->
303
            select('profile_group.*')->
304
            where_not_in('profile_group.id', $current)->
305
            where('profile_group.organization_id', $org_id)->
306
            order_by_asc('profile_group.display_name_neutral')->find_many();
307
308
    return $data;
309
}
310
311
function getEventsForProfiles($profile_ids, $profile_group_ids, $user, $base = 33) {
312
    $genderChoice = array ('display_name_neutral', 'display_name_male', 'display_name_female');
313
    $data = ORM::for_table('event')->
314
            select('event.*')->
315
            select('event_profile.*')->
316
            select('activity.id', 'activity_id')->
317
            select('activity.display_name', 'activity_display_name')->
318
            select('activity.description', 'activity_description')->
319
            select('profile.display_name', 'profile_display_name')->
320
            select('profile_group.' . $genderChoice[$user['gender']], 'profile_group_display_name')->
321
            select('profile.is_container')->
322
            select('completed_event.completed_date')->
323
            select_expr('(event.from_week+48-' . $base . ') % 48', 'n_from_week')->
324
            select_expr('(event.to_week+48-' . $base . ') % 48', 'n_to_week')->
325
            inner_join('activity_event', array('activity_event.event_id', '=', 'event.id'))->
326
            inner_join('event_profile', array('event_profile.event_id', '=', 'event.id'))->
327
            inner_join('activity', array('activity.id', '=', 'activity_event.activity_id'))->
328
            inner_join('profile', array('profile.id', '=', 'event_profile.profile_id'))->
329
            inner_join('profile_group', array('profile_group.id', '=', 'profile.profile_group_id'))->
330
            left_outer_join('completed_event', 'completed_event.event_id = event.id AND completed_event.person_id = ' . $user['id'])->
331
            order_by_asc('profile_group.display_name_neutral')->
332
            order_by_asc('profile.display_name')->
333
            order_by_asc('activity.id')->
334
            order_by_asc('n_from_week')->
335
            order_by_asc('n_to_week');
336
337
    if (($profile_ids) || ($profile_group_ids)) {
338
        $data = $data->where_in('profile_id', array_merge($profile_ids, $profile_group_ids));
339
    }
340
341
    return $data->find_array();
342
}
343
344
function addDataInfo($data, $info = array(), $fields = array()) {
345
    $current = array();
346
347
    foreach ($info as $field) {
348
        $current[$field] = $fields[$field];
349
    }
350
    return array(
351
        'info' => $current,
352
        'data' => $data
353
    );
354
}
355
356
function parseEvents($events,
357
        $first_level = 'profile_id', $first_info = array(),
358
        $second_level = 'activity_id', $second_info = array()) {
359
360
    $return = array();
361
    $currentFirst = array();
362
    $currentSecond = array();
363
364
    $lastItem = null;
365
366
    $old = array(
367
        'first' => null,
368
        'second' => null
369
    );
370
371
    foreach ($events as $event) {
372
373
        if (($old['first'] != $event[$first_level]) ||
374
            ($old['second'] != $event[$second_level])) {
375
376
            if (!empty($currentSecond)) {
377
                $currentFirst[$old['second']] = addDataInfo($currentSecond, $second_info, $lastItem);
378
                $currentSecond = array();
379
            }
380
            $old['second'] = $event[$second_level];
381
382
            if ($old['first'] != $event[$first_level]) {
383
                if (!empty($currentFirst)) {
384
                    $return[$old['first']] = addDataInfo($currentFirst, $first_info, $lastItem);
385
386
                    $currentFirst = array();
387
                }
388
                $old['first'] = $event[$first_level];
389
            }
390
        }
391
392
        $currentSecond[] = $event;
393
        $lastItem = $event;
394
    }
395
396
    if (!empty($currentSecond)) {
397
        $currentFirst[$old['second']] = addDataInfo($currentSecond, $second_info, $lastItem);
398
    }
399
400
    if (!empty($currentFirst)) {
401
        $return[$old['first']] = addDataInfo($currentFirst, $first_info, $lastItem);
402
    }
403
404
    return $return;
405
}
406
407
function getActivityObject($orgId, $actId) {
408
    return ORM::for_table('activity')->
409
            where('organization_id', $orgId)->
410
            where('id', $actId)->
411
            find_one();
412
}
413
414 View Code Duplication
function getAllFoldersFromActivities($orgId) {
415
    $folders = ORM::for_table('event')->
416
            select('id')->
417
            distinct()->
418
            where('organization_id', $orgId)->
419
            find_array();
420
421
    $folders = array_column($folders, 'id');
422
423
    $data = parseArray(
424
        ORM::for_table('folder')->
425
            where_id_in($folders)->
426
            find_many()
427
    );
428
    return $data;
429
}
430
431
function getProfilesForAllEvents($orgId) {
432
    $folders = ORM::for_table('event_profile')->
433
            select('event_profile.*')->
434
            inner_join('event', array('event_id', '=', 'event.id'))->
435
            where('event.organization_id', $orgId)->
436
            find_array();
437
438
    $data = parseArrayMix($folders, 'event_id');
439
440
    return $data;
441
}
442