Passed
Push — 1.11.x ( 9f10d1...f1597a )
by Angel Fernando Quiroz
09:54 queued 12s
created

Career::delete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 2
nop 1
dl 0
loc 16
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
use Fhaculty\Graph\Graph;
6
use Fhaculty\Graph\Vertex;
7
8
/**
9
 * Class Career.
10
 */
11
class Career extends Model
12
{
13
    public $table;
14
    public $columns = [
15
        'id',
16
        'name',
17
        'description',
18
        'status',
19
        'created_at',
20
        'updated_at',
21
    ];
22
23
    public function __construct()
24
    {
25
        $this->table = Database::get_main_table(TABLE_CAREER);
26
    }
27
28
    public function getCareerFromId($id)
29
    {
30
        if (api_get_configuration_value('use_career_external_id_as_identifier_in_diagrams')) {
31
            // Try with the external career id.
32
            $careerInfo = $this->getCareerFromExternalToInternal($id);
33
        } else {
34
            $careerInfo = $this->get($id);
35
        }
36
37
        return $careerInfo;
38
    }
39
40
    public function getCareerFromExternalToInternal($externalCareerId, $extraFieldVariable = 'external_career_id')
41
    {
42
        $careerExtraFieldValue = new ExtraFieldValue('career');
43
        $careerValue = $careerExtraFieldValue->get_item_id_from_field_variable_and_field_value(
44
            $extraFieldVariable,
45
            $externalCareerId
46
        );
47
48
        $careerInfo = [];
49
        if (isset($careerValue['item_id'])) {
50
            $careerInfo = $this->get($careerValue['item_id']);
51
        }
52
53
        return $careerInfo;
54
    }
55
56
    public function getCareerIdFromInternalToExternal($internalCareerId)
57
    {
58
        $careerExtraFieldValue = new ExtraFieldValue('career');
59
        $externalCareerValue = $careerExtraFieldValue->get_values_by_handler_and_field_variable(
60
            $internalCareerId,
61
            'external_career_id'
62
        );
63
64
        if (!empty($externalCareerValue) && isset($externalCareerValue['value'])) {
65
            return $externalCareerValue['value'];
66
        }
67
68
        return null;
69
    }
70
71
    /**
72
     * Get the count of elements.
73
     *
74
     * @return int
75
     */
76
    public function get_count()
77
    {
78
        $row = Database::select(
79
            'count(*) as count',
80
            $this->table,
81
            [],
82
            'first'
83
        );
84
85
        return $row['count'];
86
    }
87
88
    /**
89
     * @param array $where_conditions
90
     *
91
     * @return array
92
     */
93
    public function get_all($where_conditions = [])
94
    {
95
        return Database::select(
96
            '*',
97
            $this->table,
98
            ['where' => $where_conditions, 'order' => 'name ASC']
99
        );
100
    }
101
102
    /**
103
     * Update all promotion status by career.
104
     *
105
     * @param int $career_id
106
     * @param int $status    (1 or 0)
107
     */
108
    public function update_all_promotion_status_by_career_id($career_id, $status)
109
    {
110
        $promotion = new Promotion();
111
        $promotion_list = $promotion->get_all_promotions_by_career_id($career_id);
112
        if (!empty($promotion_list)) {
113
            foreach ($promotion_list as $item) {
114
                $params['id'] = $item['id'];
115
                $params['status'] = $status;
116
                $promotion->update($params);
117
                $promotion->update_all_sessions_status_by_promotion_id($params['id'], $status);
118
            }
119
        }
120
    }
121
122
    /**
123
     * Returns HTML the title + grid.
124
     *
125
     * @return string
126
     */
127
    public function display()
128
    {
129
        $html = '<div class="actions" style="margin-bottom:20px">';
130
        $html .= '<a href="career_dashboard.php">'.
131
            Display::return_icon('back.png', get_lang('Back'), '', ICON_SIZE_MEDIUM).'</a>';
132
        if (api_is_platform_admin()) {
133
            $html .= '<a href="'.api_get_self().'?action=add">'.
134
                    Display::return_icon('new_career.png', get_lang('Add'), '', ICON_SIZE_MEDIUM).'</a>';
135
        }
136
        $html .= '</div>';
137
        $html .= Display::grid_html('careers');
138
139
        return $html;
140
    }
141
142
    /**
143
     * @return array
144
     */
145
    public function get_status_list()
146
    {
147
        return [
148
            CAREER_STATUS_ACTIVE => get_lang('Unarchived'),
149
            CAREER_STATUS_INACTIVE => get_lang('Archived'),
150
        ];
151
    }
152
153
    /**
154
     * Returns a Form validator Obj.
155
     *
156
     * @todo the form should be auto generated
157
     *
158
     * @param string $url
159
     * @param string $action add, edit
160
     *
161
     * @return FormValidator
162
     */
163
    public function return_form($url, $action)
164
    {
165
        $form = new FormValidator('career', 'post', $url);
166
        // Setting the form elements
167
        $header = get_lang('Add');
168
        if ($action == 'edit') {
169
            $header = get_lang('Modify');
170
        }
171
172
        $id = isset($_GET['id']) ? (int) $_GET['id'] : '';
173
        $form->addHeader($header);
174
        $form->addHidden('id', $id);
175
        $form->addElement('text', 'name', get_lang('Name'), ['size' => '70']);
176
        $form->addHtmlEditor(
177
            'description',
178
            get_lang('Description'),
179
            false,
180
            false,
181
            [
182
                'ToolbarSet' => 'Careers',
183
                'Width' => '100%',
184
                'Height' => '250',
185
            ]
186
        );
187
        $status_list = $this->get_status_list();
188
        $form->addElement('select', 'status', get_lang('Status'), $status_list);
189
190
        if ($action == 'edit') {
191
            $extraField = new ExtraField('career');
192
            $extraField->addElements($form, $id);
193
194
            $form->addElement('text', 'created_at', get_lang('CreatedAt'));
195
            $form->freeze('created_at');
196
            $form->addButtonSave(get_lang('Modify'));
197
        } else {
198
            $form->addButtonCreate(get_lang('Add'));
199
        }
200
201
        // Setting the defaults
202
        $defaults = $this->get($id);
203
204
        if (!empty($defaults['created_at'])) {
205
            $defaults['created_at'] = api_convert_and_format_date($defaults['created_at']);
206
        }
207
        if (!empty($defaults['updated_at'])) {
208
            $defaults['updated_at'] = api_convert_and_format_date($defaults['updated_at']);
209
        }
210
211
        $form->setDefaults($defaults);
212
213
        // Setting the rules
214
        $form->addRule('name', get_lang('ThisFieldIsRequired'), 'required');
215
216
        return $form;
217
    }
218
219
    /**
220
     * Copies the career to a new one.
221
     *
222
     * @param   int     Career ID
223
     * @param   bool     Whether or not to copy the promotions inside
224
     *
225
     * @return int New career ID on success, false on failure
226
     */
227
    public function copy($id, $copy_promotions = false)
228
    {
229
        $career = $this->get($id);
230
        $new = [];
231
        foreach ($career as $key => $val) {
232
            switch ($key) {
233
                case 'id':
234
                case 'updated_at':
235
                    break;
236
                case 'name':
237
                    $val .= ' '.get_lang('CopyLabelSuffix');
238
                    $new[$key] = $val;
239
                    break;
240
                case 'created_at':
241
                    $val = api_get_utc_datetime();
242
                    $new[$key] = $val;
243
                    break;
244
                default:
245
                    $new[$key] = $val;
246
                    break;
247
            }
248
        }
249
        $cid = $this->save($new);
250
        if ($copy_promotions) {
251
            //Now also copy each session of the promotion as a new session and register it inside the promotion
252
            $promotion = new Promotion();
253
            $promo_list = $promotion->get_all_promotions_by_career_id($id);
254
            if (!empty($promo_list)) {
255
                foreach ($promo_list as $item) {
256
                    $promotion->copy($item['id'], $cid, true);
257
                }
258
            }
259
        }
260
261
        return $cid;
262
    }
263
264
    /**
265
     * @param int $career_id
266
     *
267
     * @return bool
268
     */
269
    public function get_status($career_id)
270
    {
271
        $table = Database::get_main_table(TABLE_CAREER);
272
        $career_id = intval($career_id);
273
        $sql = "SELECT status FROM $table WHERE id = '$career_id'";
274
        $result = Database::query($sql);
275
        if (Database::num_rows($result) > 0) {
276
            $data = Database::fetch_array($result);
277
278
            return $data['status'];
279
        } else {
280
            return false;
281
        }
282
    }
283
284
    /**
285
     * @param array $params
286
     * @param bool  $show_query
287
     *
288
     * @return int
289
     */
290
    public function save($params, $show_query = false)
291
    {
292
        if (isset($params['description'])) {
293
            $params['description'] = Security::remove_XSS($params['description']);
294
        }
295
296
        $id = parent::save($params);
297
        if (!empty($id)) {
298
            Event::addEvent(
299
                LOG_CAREER_CREATE,
300
                LOG_CAREER_ID,
301
                $id,
302
                api_get_utc_datetime(),
303
                api_get_user_id()
304
            );
305
        }
306
307
        return $id;
308
    }
309
310
    /**
311
     * Delete a record from the career table and report in the default events log table.
312
     *
313
     * @param int $id The ID of the career to delete
314
     *
315
     * @return bool True if the career could be deleted, false otherwise
316
     */
317
    public function delete($id)
318
    {
319
        $res = parent::delete($id);
320
        if ($res) {
321
            $extraFieldValues = new ExtraFieldValue('career');
322
            $extraFieldValues->deleteValuesByItem($id);
323
            Event::addEvent(
324
                LOG_CAREER_DELETE,
325
                LOG_CAREER_ID,
326
                $id,
327
                api_get_utc_datetime(),
328
                api_get_user_id()
329
            );
330
        }
331
332
        return $res;
333
    }
334
335
    /**
336
     * {@inheritdoc}
337
     */
338
    public function update($params, $showQuery = false)
339
    {
340
        if (isset($params['description'])) {
341
            $params['description'] = Security::remove_XSS($params['description']);
342
        }
343
344
        return parent::update($params, $showQuery);
345
    }
346
347
    /**
348
     * @param array
349
     * @param Graph $graph
350
     *
351
     * @return string
352
     */
353
    public static function renderDiagram($careerInfo, $graph)
354
    {
355
        if (!($graph instanceof Graph)) {
356
            return '';
357
        }
358
359
        // Getting max column
360
        $maxColumn = 0;
361
        foreach ($graph->getVertices() as $vertex) {
362
            $groupId = (int) $vertex->getGroup();
363
            if ($groupId > $maxColumn) {
364
                $maxColumn = $groupId;
365
            }
366
        }
367
368
        $list = [];
369
        /** @var Vertex $vertex */
370
        foreach ($graph->getVertices() as $vertex) {
371
            $group = $vertex->getAttribute('Group');
372
            $groupData = explode(':', $group);
373
            $group = $groupData[0];
374
            $groupLabel = isset($groupData[1]) ? $groupData[1] : '';
375
            $subGroup = $vertex->getAttribute('SubGroup');
376
            $subGroupData = explode(':', $subGroup);
377
            $column = $vertex->getGroup();
378
            $row = $vertex->getAttribute('Row');
379
            $subGroupId = $subGroupData[0];
380
            $label = isset($subGroupData[1]) ? $subGroupData[1] : '';
381
            $list[$group][$subGroupId]['columns'][$column][$row] = $vertex;
382
            $list[$group][$subGroupId]['label'] = $label;
383
            $list[$group]['label'] = $groupLabel;
384
        }
385
386
        $maxGroups = count($list);
387
        $widthGroup = 30;
388
        if (!empty($maxGroups)) {
389
            $widthGroup = 85 / $maxGroups;
390
        }
391
392
        $connections = '';
393
        $groupDrawLine = [];
394
        $groupCourseList = [];
395
        // Read Connections column
396
        foreach ($list as $group => $subGroupList) {
397
            foreach ($subGroupList as $subGroupData) {
398
                $columns = isset($subGroupData['columns']) ? $subGroupData['columns'] : [];
399
                $showGroupLine = true;
400
                if (count($columns) == 1) {
401
                    $showGroupLine = false;
402
                }
403
                $groupDrawLine[$group] = $showGroupLine;
404
405
                //if ($showGroupLine == false) {
406
                /** @var Vertex $vertex */
407
                foreach ($columns as $row => $items) {
408
                    foreach ($items as $vertex) {
409
                        if ($vertex instanceof Vertex) {
410
                            $groupCourseList[$group][] = $vertex->getId();
411
                            $connectionList = $vertex->getAttribute('Connections');
412
                            $firstConnection = '';
413
                            $secondConnection = '';
414
                            if (!empty($connectionList)) {
415
                                $explode = explode('-', $connectionList);
416
                                $pos = strpos($explode[0], 'SG');
417
                                if ($pos === false) {
418
                                    $pos = strpos($explode[0], 'G');
419
                                    if (is_numeric($pos)) {
420
                                        // group_123 id
421
                                        $groupValueId = (int) str_replace(
422
                                            'G',
423
                                            '',
424
                                            $explode[0]
425
                                        );
426
                                        $firstConnection = 'group_'.$groupValueId;
427
                                        $groupDrawLine[$groupValueId] = true;
428
                                    } else {
429
                                        // Course block (row_123 id)
430
                                        if (!empty($explode[0])) {
431
                                            $firstConnection = 'row_'.(int) $explode[0];
432
                                        }
433
                                    }
434
                                } else {
435
                                    // subgroup__123 id
436
                                    $firstConnection = 'subgroup_'.(int) str_replace('SG', '', $explode[0]);
437
                                }
438
439
                                $pos = strpos($explode[1], 'SG');
440
                                if ($pos === false) {
441
                                    $pos = strpos($explode[1], 'G');
442
                                    if (is_numeric($pos)) {
443
                                        $groupValueId = (int) str_replace(
444
                                            'G',
445
                                            '',
446
                                            $explode[1]
447
                                        );
448
                                        $secondConnection = 'group_'.$groupValueId;
449
                                        $groupDrawLine[$groupValueId] = true;
450
                                    } else {
451
                                        // Course block (row_123 id)
452
                                        if (!empty($explode[0])) {
453
                                            $secondConnection = 'row_'.(int) $explode[1];
454
                                        }
455
                                    }
456
                                } else {
457
                                    $secondConnection = 'subgroup_'.(int) str_replace('SG', '', $explode[1]);
458
                                }
459
460
                                if (!empty($firstConnection) && !empty($firstConnection)) {
461
                                    $connections .= self::createConnection(
462
                                        $firstConnection,
463
                                        $secondConnection,
464
                                        ['Left', 'Right']
465
                                    );
466
                                }
467
                            }
468
                        }
469
                    }
470
                }
471
                //}
472
            }
473
        }
474
475
        $graphHtml = '<div class="container">';
476
        foreach ($list as $group => $subGroupList) {
477
            $showGroupLine = false;
478
            if (isset($groupDrawLine[$group]) && $groupDrawLine[$group]) {
479
                $showGroupLine = true;
480
            }
481
            $graphHtml .= self::parseSubGroups(
482
                $groupCourseList,
483
                $group,
484
                $list[$group]['label'],
485
                $showGroupLine,
486
                $subGroupList,
487
                $widthGroup
488
            );
489
        }
490
        $graphHtml .= '</div>';
491
        $graphHtml .= $connections;
492
493
        return $graphHtml;
494
    }
495
496
    /**
497
     * @param array    $careerInfo
498
     * @param Template $tpl
499
     * @param int      $loadUserIdData
500
     *
501
     * @return string
502
     */
503
    public static function renderDiagramByColumn($careerInfo, $tpl, $loadUserIdData = 0, $showFooter = true)
504
    {
505
        $careerId = $careerInfo['id'] ?? 0;
506
        if (empty($careerId)) {
507
            return '';
508
        }
509
510
        $extraFieldValue = new ExtraFieldValue('career');
511
        $item = $extraFieldValue->get_values_by_handler_and_field_variable(
512
            $careerId,
513
            'career_diagram',
514
            false,
515
            false,
516
            false
517
        );
518
519
        $graph = null;
520
        if (!empty($item) && isset($item['value']) && !empty($item['value'])) {
521
            /** @var Graph $graph */
522
            $graph = UnserializeApi::unserialize('career', $item['value']);
523
        }
524
525
        if (!($graph instanceof Graph)) {
526
            return '';
527
        }
528
529
        // Getting max column
530
        $maxColumn = 0;
531
        foreach ($graph->getVertices() as $vertex) {
532
            $groupId = (int) $vertex->getGroup();
533
            if ($groupId > $maxColumn) {
534
                $maxColumn = $groupId;
535
            }
536
        }
537
538
        $userResult = [];
539
        if (!empty($loadUserIdData)) {
540
            $careerData = UserManager::getUserCareer($loadUserIdData, $careerId);
541
            if (isset($careerData['extra_data']) && !empty($careerData['extra_data'])) {
542
                $userResult = unserialize($careerData['extra_data']);
543
            }
544
        }
545
546
        $list = [];
547
        $subGroups = [];
548
        /** @var Vertex $vertex */
549
        foreach ($graph->getVertices() as $vertex) {
550
            $column = $vertex->getGroup();
551
            $group = $vertex->getAttribute('Group');
552
553
            $groupData = explode(':', $group);
554
            $group = $groupData[0];
555
            $groupLabel = isset($groupData[1]) ? $groupData[1] : '';
556
557
            $subGroup = $vertex->getAttribute('SubGroup');
558
            $subGroupData = explode(':', $subGroup);
559
560
            $row = $vertex->getAttribute('Row');
561
            $subGroupId = $subGroupData[0];
562
            $subGroupLabel = isset($subGroupData[1]) ? $subGroupData[1] : '';
563
564
            if (!empty($subGroupId) && !in_array($subGroupId, $subGroups)) {
565
                $subGroups[$subGroupId]['items'][] = $vertex->getId();
566
                $subGroups[$subGroupId]['label'] = $subGroupLabel;
567
            }
568
569
            $list[$column]['rows'][$row]['items'][] = $vertex;
570
            $list[$column]['rows'][$row]['label'] = $subGroupId;
571
            $list[$column]['rows'][$row]['group'] = $group;
572
            $list[$column]['rows'][$row]['group_label'] = $groupLabel;
573
            $list[$column]['rows'][$row]['subgroup'] = $subGroup;
574
            $list[$column]['rows'][$row]['subgroup_label'] = $subGroupLabel;
575
            $list[$column]['label'] = $groupLabel;
576
            $list[$column]['column'] = $column;
577
        }
578
579
        $groupCourseList = [];
580
        $simpleConnectionList = [];
581
582
        // Read Connections column
583
        foreach ($list as $column => $groupList) {
584
            foreach ($groupList['rows'] as $subGroupList) {
585
                /** @var Vertex $vertex */
586
                foreach ($subGroupList['items'] as $vertex) {
587
                    if ($vertex instanceof Vertex) {
588
                        $groupCourseList[$vertex->getAttribute('Column')][] = $vertex->getId();
589
                        $connectionList = $vertex->getAttribute('Connections');
590
                        if (empty($connectionList)) {
591
                            continue;
592
                        }
593
                        $simpleFirstConnection = '';
594
                        $simpleSecondConnection = '';
595
596
                        $explode = explode('-', $connectionList);
597
                        $pos = strpos($explode[0], 'SG');
598
                        if ($pos === false) {
599
                            $pos = strpos($explode[0], 'G');
600
                            if (is_numeric($pos)) {
601
                                // Is group
602
                                $groupValueId = (int) str_replace(
603
                                    'G',
604
                                    '',
605
                                    $explode[0]
606
                                );
607
                                $simpleFirstConnection = 'g'.(int) $groupValueId;
608
                            } else {
609
                                // Course block (row_123 id)
610
                                if (!empty($explode[0])) {
611
                                    $simpleFirstConnection = 'v'.$explode[0];
612
                                }
613
                            }
614
                        } else {
615
                            // subgroup__123 id
616
                            $simpleFirstConnection = 'sg'.(int) str_replace('SG', '', $explode[0]);
617
                        }
618
619
                        $pos = false;
620
                        if (isset($explode[1])) {
621
                            $pos = strpos($explode[1], 'SG');
622
                        }
623
                        if ($pos === false) {
624
                            if (isset($explode[1])) {
625
                                $pos = strpos($explode[1], 'G');
626
                                $value = $explode[1];
627
                            }
628
                            if (is_numeric($pos)) {
629
                                $groupValueId = (int) str_replace(
630
                                    'G',
631
                                    '',
632
                                    $value
633
                                );
634
                                $simpleSecondConnection = 'g'.(int) $groupValueId;
635
                            } else {
636
                                // Course block (row_123 id)
637
                                if (!empty($explode[0]) && isset($explode[1])) {
638
                                    $simpleSecondConnection = 'v'.(int) $explode[1];
639
                                }
640
                            }
641
                        } else {
642
                            $simpleSecondConnection = 'sg'.(int) str_replace('SG', '', $explode[1]);
643
                        }
644
645
                        if (!empty($simpleFirstConnection) && !empty($simpleSecondConnection)) {
646
                            $simpleConnectionList[] = [
647
                                'from' => $simpleFirstConnection,
648
                                'to' => $simpleSecondConnection,
649
                            ];
650
                        }
651
                    }
652
                }
653
            }
654
        }
655
656
        $graphHtml = '';
657
        $groupsBetweenColumns = [];
658
        foreach ($list as $column => $columnList) {
659
            foreach ($columnList['rows'] as $subGroupList) {
660
                $newGroup = $subGroupList['group'];
661
                $label = $subGroupList['group_label'];
662
                $newOrder[$newGroup]['items'][] = $subGroupList;
663
                $newOrder[$newGroup]['label'] = $label;
664
                $groupsBetweenColumns[$newGroup][] = $subGroupList;
665
            }
666
        }
667
668
        // Creates graph
669
        $graph = new stdClass();
670
        $graph->blockWidth = 280;
671
        $graph->blockHeight = 150;
672
673
        $graph->xGap = 70;
674
        $graph->yGap = 55;
675
676
        $graph->xDiff = 70;
677
        $graph->yDiff = 55;
678
679
        if (!empty($userResult)) {
680
            $graph->blockHeight = 180;
681
            $graph->yGap = 60;
682
            $graph->yDiff = 60;
683
        }
684
685
        foreach ($groupsBetweenColumns as $group => $items) {
686
            self::parseColumnList($groupCourseList, $items, $graph, $simpleConnectionList, $userResult);
687
        }
688
689
        $graphHtml .= '<style>
690
             .panel-title {
691
                font-size: 11px;
692
                height: 40px;
693
             }
694
695
             .panel-body{
696
                min-height: 55px;
697
             }
698
             </style>';
699
700
        // Create groups
701
        if (!empty($graph->groupList)) {
702
            $groupList = [];
703
            $groupDiffX = 20;
704
            $groupDiffY = 50;
705
            $style = 'whiteSpace=wrap;rounded;html=1;strokeColor=red;fillColor=none;strokeWidth=2;align=left;verticalAlign=top;';
706
            foreach ($graph->groupList as $id => $data) {
707
                if (empty($id)) {
708
                    continue;
709
                }
710
711
                $x = $data['min_x'] - $groupDiffX;
712
                $y = $data['min_y'] - $groupDiffY;
713
                $width = $data['max_width'] + ($groupDiffX * 2);
714
                $height = $data['max_height'] + $groupDiffY * 2;
715
                $label = '<h4>'.$data['label'].'</h4>';
716
                $vertexData = "var g$id = graph.insertVertex(parent, null, '$label', $x, $y, $width, $height, '$style');";
717
                $groupList[] = $vertexData;
718
            }
719
            $tpl->assign('group_list', $groupList);
720
        }
721
722
        // Create subgroups
723
        $subGroupList = [];
724
        $subGroupListData = [];
725
        foreach ($subGroups as $subGroupId => $vertexData) {
726
            $label = $vertexData['label'];
727
            $vertexIdList = $vertexData['items'];
728
            foreach ($vertexIdList as $rowId) {
729
                $data = $graph->allData[$rowId];
730
                $originalRow = $data['row'];
731
                $column = $data['column'];
732
                $x = $data['x'];
733
                $y = $data['y'];
734
                $width = $data['width'];
735
                $height = $data['height'];
736
737
                if (!isset($subGroupListData[$subGroupId])) {
738
                    $subGroupListData[$subGroupId]['min_x'] = 1000;
739
                    $subGroupListData[$subGroupId]['min_y'] = 1000;
740
                    $subGroupListData[$subGroupId]['max_width'] = 0;
741
                    $subGroupListData[$subGroupId]['max_height'] = 0;
742
                    $subGroupListData[$subGroupId]['label'] = $label;
743
                }
744
745
                if ($x < $subGroupListData[$subGroupId]['min_x']) {
746
                    $subGroupListData[$subGroupId]['min_x'] = $x;
747
                }
748
749
                if ($y < $subGroupListData[$subGroupId]['min_y']) {
750
                    $subGroupListData[$subGroupId]['min_y'] = $y;
751
                }
752
753
                $subGroupListData[$subGroupId]['max_width'] = ($column + 1) * ($width + $graph->xGap) - $subGroupListData[$subGroupId]['min_x'];
754
                $subGroupListData[$subGroupId]['max_height'] = ($originalRow + 1) * ($height + $graph->yGap) - $subGroupListData[$subGroupId]['min_y'];
755
            }
756
757
            $style = 'whiteSpace=wrap;rounded;dashed=1;strokeColor=blue;fillColor=none;strokeWidth=2;align=left;verticalAlign=bottom;';
758
            $subGroupDiffX = 5;
759
            foreach ($subGroupListData as $subGroupId => $data) {
760
                $x = $data['min_x'] - $subGroupDiffX;
761
                $y = $data['min_y'] - $subGroupDiffX;
762
763
                $spaceForSubGroupTitle = 0;
764
                if (!empty($data['label'])) {
765
                    $spaceForSubGroupTitle = 40;
766
                }
767
768
                $width = $data['max_width'] + $subGroupDiffX * 2;
769
                $height = $data['max_height'] + $subGroupDiffX * 2 + $spaceForSubGroupTitle;
770
771
                $label = '<h4 style="background: white">'.$data['label'].'</h4>';
772
                $vertexData = "var sg$subGroupId = graph.insertVertex(parent, null, '$label', $x, $y, $width, $height, '$style');";
773
                $subGroupList[] = $vertexData;
774
            }
775
        }
776
777
        // Create connections (arrows)
778
        if (!empty($simpleConnectionList)) {
779
            $connectionList = [];
780
            //$style = 'endArrow=classic;html=1;strokeWidth=4;exitX=1;exitY=0.5;entryX=0;entryY=0.5;';
781
            $style = '';
782
            foreach ($simpleConnectionList as $connection) {
783
                $from = $connection['from'];
784
                $to = $connection['to'];
785
                $vertexData = "var e1 = graph.insertEdge(parent, null, '', $from, $to, '$style')";
786
                $connectionList[] = $vertexData;
787
            }
788
            $tpl->assign('connections', $connectionList);
789
        }
790
791
        $tpl->assign('subgroup_list', $subGroupList);
792
        $tpl->assign('vertex_list', $graph->elementList);
793
794
        $graphHtml .= '<div id="graphContainer"></div>';
795
        if ($showFooter) {
796
            $graphHtml .= self::renderDiagramFooter();
797
        }
798
799
        return $graphHtml;
800
    }
801
802
    /**
803
     * @param $groupCourseList
804
     * @param $columnList
805
     * @param $graph
806
     * @param $connections
807
     * @param $userResult
808
     *
809
     * @return string
810
     */
811
    public static function parseColumnList($groupCourseList, $columnList, &$graph, &$connections, $userResult)
812
    {
813
        $graphHtml = '';
814
        $newOrder = [];
815
        foreach ($columnList as $subGroupList) {
816
            $newGroup = $subGroupList['group'];
817
            $label = $subGroupList['group_label'];
818
            $newOrder[$newGroup]['items'][] = $subGroupList;
819
            $newOrder[$newGroup]['label'] = $label;
820
        }
821
822
        foreach ($newOrder as $newGroup => $data) {
823
            $label = $data['label'];
824
            $subGroupList = $data['items'];
825
826
            if (!isset($graph->groupList[$newGroup])) {
827
                $graph->groupList[$newGroup]['min_x'] = 1000;
828
                $graph->groupList[$newGroup]['min_y'] = 1000;
829
                $graph->groupList[$newGroup]['max_width'] = 0;
830
                $graph->groupList[$newGroup]['max_height'] = 0;
831
                $graph->groupList[$newGroup]['label'] = $label;
832
            }
833
834
            $maxColumn = 0;
835
            $maxRow = 0;
836
            $minColumn = 100;
837
            $minRow = 100;
838
            foreach ($subGroupList as $item) {
839
                /** @var Vertex $vertex */
840
                foreach ($item['items'] as $vertex) {
841
                    $column = $vertex->getAttribute('Column');
842
                    $realRow = $vertex->getAttribute('Row');
843
844
                    if ($column > $maxColumn) {
845
                        $maxColumn = $column;
846
                    }
847
                    if ($realRow > $maxRow) {
848
                        $maxRow = $realRow;
849
                    }
850
851
                    if ($column < $minColumn) {
852
                        $minColumn = $column;
853
                    }
854
                    if ($realRow < $minRow) {
855
                        $minRow = $realRow;
856
                    }
857
                }
858
            }
859
860
            if (!empty($newGroup)) {
861
                $graphHtml .= '<div
862
                    id ="group_'.$newGroup.'"
863
                    class="group'.$newGroup.' group_class"
864
                    style="display:grid;
865
                        align-self: start;
866
                        grid-gap: 10px;
867
                        justify-items: stretch;
868
                        align-items: start;
869
                        align-content: start;
870
                        justify-content: stretch;
871
                        grid-area:'.$minRow.'/'.$minColumn.'/'.$maxRow.'/'.$maxColumn.'">'; //style="display:grid"
872
            }
873
874
            $addRow = 0;
875
            if (!empty($label)) {
876
                $graphHtml .= "<div class='my_label' style='grid-area:$minRow/$minColumn/$maxRow/$maxColumn'>$label</div>";
877
                $addRow = 1;
878
            }
879
880
            foreach ($subGroupList as $item) {
881
                $graphHtml .= self::parseVertexList(
882
                    $groupCourseList,
883
                    $item['items'],
884
                    $addRow,
885
                    $graph,
886
                    $newGroup,
887
                    $connections,
888
                    $userResult
889
                );
890
            }
891
892
            if (!empty($newGroup)) {
893
                $graphHtml .= '</div >';
894
            }
895
        }
896
897
        return $graphHtml;
898
    }
899
900
    /**
901
     * @param array    $groupCourseList
902
     * @param array    $vertexList
903
     * @param int      $addRow
904
     * @param stdClass $graph
905
     * @param int      $group
906
     * @param array    $connections
907
     * @param array    $userResult
908
     *
909
     * @return string
910
     */
911
    public static function parseVertexList($groupCourseList, $vertexList, $addRow, &$graph, $group, &$connections, $userResult)
912
    {
913
        if (empty($vertexList)) {
914
            return '';
915
        }
916
917
        $graphHtml = '';
918
        /** @var Vertex $vertex */
919
        foreach ($vertexList as $vertex) {
920
            $borderColor = 'green';
921
            $column = $vertex->getAttribute('Column');
922
            $realRow = $originalRow = $vertex->getAttribute('Row');
923
            if ($addRow) {
924
                $realRow = $realRow + $addRow;
925
            }
926
            $id = $vertex->getId();
927
            $area = "$realRow/$column";
928
            $graphHtml .= '<div
929
                id = "row_wrapper_'.$id.'"
930
                data= "'.$originalRow.'-'.$column.'"
931
                style="
932
                    align-self: start;
933
                    justify-content: stretch;
934
                    grid-area:'.$area.'"
935
            >';
936
            $color = '';
937
            if (!empty($vertex->getAttribute('DefinedColor'))) {
938
                $color = $vertex->getAttribute('DefinedColor');
939
            }
940
            $content = '<div class="pull-left">'.$vertex->getAttribute('Notes').'</div>';
941
            $content .= '<div class="pull-right">['.$id.']</div>';
942
943
            if (!empty($userResult) && isset($userResult[$id]) && !empty($userResult[$id])) {
944
                // Order by SortDate
945
                $sortedByDate = $userResult[$id];
946
                foreach ($sortedByDate as $resultId => &$result) {
947
                    $result['resultId'] = $resultId;
948
                }
949
950
                usort($sortedByDate, function ($item1, $item2) {
951
                    if (!isset($item1['SortDate']) || !isset($item2['SortDate'])) {
952
                        return false;
953
                    }
954
955
                    return $item1['SortDate'] > $item2['SortDate'];
956
                });
957
958
                $lastItem = end($sortedByDate);
959
                if ($lastItem && isset($lastItem['BgColor']) && !empty($lastItem['BgColor'])) {
960
                    $color = $lastItem['BgColor'].'; color: '.$lastItem['Color'];
961
                    $borderColor = $lastItem['BorderColor'];
962
                }
963
                $results = '';
964
                $size = 2;
965
                foreach ($sortedByDate as $iconData) {
966
                    $resultId = $iconData['resultId'];
967
                    $icon = '';
968
                    switch ($iconData['Icon']) {
969
                        case 0:
970
                            $icon = Display::returnFontAwesomeIcon('times-circle', $size);
971
                            break;
972
                        case 1:
973
                            $icon = Display::returnFontAwesomeIcon('check-circle', $size);
974
                            break;
975
                        case 2:
976
                            $icon = Display::returnFontAwesomeIcon('info-circle', $size);
977
                            break;
978
                    }
979
980
                    if (substr($resultId, 0, 1) == 2) {
981
                        $iconData['Description'] = 'Result Id = '.$resultId;
982
                    }
983
984
                    if ('Joe Anonymous' === $iconData['TeacherUsername']) {
985
                        $iconData['TeacherUsername'] = '';
986
                    }
987
988
                    if (!empty($icon)) {
989
                        $params = [
990
                            'id' => 'course_'.$id.'_'.$resultId,
991
                            'data-toggle' => 'popover',
992
                            'title' => 'Popover title',
993
                            'class' => 'popup',
994
                            'data-description' => $iconData['Description'],
995
                            'data-period' => $iconData['Period'],
996
                            'data-teacher-text' => $iconData['TeacherText'],
997
                            'data-teacher' => $iconData['TeacherUsername'],
998
                            'data-score' => $iconData['ScoreText'],
999
                            'data-score-value' => $iconData['ScoreValue'],
1000
                            'data-info' => $iconData['Info'],
1001
                            'data-background-color' => $iconData['BgColor'],
1002
                            'data-color' => $iconData['Color'],
1003
                            'data-border-color' => $iconData['BorderColor'],
1004
                            'style' => 'color:'.$iconData['IconColor'],
1005
                        ];
1006
                        $results .= Display::url($icon, 'javascript:void(0);', $params);
1007
                    }
1008
                }
1009
1010
                if (!empty($results)) {
1011
                    $content .= '<div class="row"></div><div class="pull-left">'.$results.'</div>';
1012
                }
1013
            }
1014
1015
            $title = $vertex->getAttribute('graphviz.label');
1016
            if (!empty($vertex->getAttribute('LinkedElement'))) {
1017
                $title = Display::url($title, $vertex->getAttribute('LinkedElement').'&iframe=1');
1018
            }
1019
1020
            $originalRow--;
1021
            $column--;
1022
1023
            $graphHtml .= Display::panel(
1024
                $content,
1025
                $title,
1026
                null,
1027
                null,
1028
                null,
1029
                "row_$id",
1030
                $color
1031
            );
1032
1033
            $panel = Display::panel(
1034
                $content,
1035
                $title,
1036
                null,
1037
                null,
1038
                null,
1039
                "row_$id",
1040
                $color
1041
            );
1042
1043
            $x = $column * $graph->blockWidth + $graph->xDiff;
1044
            $y = $originalRow * $graph->blockHeight + $graph->yDiff;
1045
1046
            $width = $graph->blockWidth - $graph->xGap;
1047
            $height = $graph->blockHeight - $graph->yGap;
1048
1049
            $style = 'text;html=1;strokeColor='.$borderColor.';fillColor=#ffffff;overflow=fill;rounded=0;align=left;';
1050
1051
            $panel = str_replace(["\n", "\r"], '', $panel);
1052
            $vertexData = "var v$id = graph.insertVertex(parent, null, '".addslashes($panel)."', $x, $y, $width, $height, '$style');";
1053
1054
            $graph->elementList[$id] = $vertexData;
1055
            $graph->allData[$id] = [
1056
                'x' => $x,
1057
                'y' => $y,
1058
                'width' => $width,
1059
                'height' => $height,
1060
                'row' => $originalRow,
1061
                'column' => $column,
1062
                'label' => $title,
1063
            ];
1064
1065
            if ($x < $graph->groupList[$group]['min_x']) {
1066
                $graph->groupList[$group]['min_x'] = $x;
1067
            }
1068
1069
            if ($y < $graph->groupList[$group]['min_y']) {
1070
                $graph->groupList[$group]['min_y'] = $y;
1071
            }
1072
1073
            $graph->groupList[$group]['max_width'] = ($column + 1) * ($width + $graph->xGap) - $graph->groupList[$group]['min_x'];
1074
            $graph->groupList[$group]['max_height'] = ($originalRow + 1) * ($height + ($graph->yGap)) - $graph->groupList[$group]['min_y'];
1075
1076
            $graphHtml .= '</div>';
1077
            $arrow = $vertex->getAttribute('DrawArrowFrom');
1078
            $found = false;
1079
            if (!empty($arrow)) {
1080
                $pos = strpos($arrow, 'SG');
1081
                if ($pos === false) {
1082
                    $pos = strpos($arrow, 'G');
1083
                    if (is_numeric($pos)) {
1084
                        $parts = explode('G', $arrow);
1085
                        if (empty($parts[0]) && count($parts) == 2) {
1086
                            $groupArrow = $parts[1];
1087
                            $graphHtml .= self::createConnection(
1088
                                "group_$groupArrow",
1089
                                "row_$id",
1090
                                ['Left', 'Right']
1091
                            );
1092
                            $found = true;
1093
                            $connections[] = [
1094
                              'from' => "g$groupArrow",
1095
                              'to' => "v$id",
1096
                            ];
1097
                        }
1098
                    }
1099
                } else {
1100
                    // Case is only one subgroup value example: SG1
1101
                    $parts = explode('SG', $arrow);
1102
                    if (empty($parts[0]) && count($parts) == 2) {
1103
                        $subGroupArrow = $parts[1];
1104
                        $graphHtml .= self::createConnection(
1105
                            "subgroup_$subGroupArrow",
1106
                            "row_$id",
1107
                            ['Left', 'Right']
1108
                        );
1109
                        $found = true;
1110
                        $connections[] = [
1111
                            'from' => "sg$subGroupArrow",
1112
                            'to' => "v$id",
1113
                        ];
1114
                    }
1115
                }
1116
1117
                if ($found == false) {
1118
                    // case is connected to 2 subgroups: Example SG1-SG2
1119
                    $parts = explode('-', $arrow);
1120
                    if (count($parts) == 2 && !empty($parts[0]) && !empty($parts[1])) {
1121
                        $defaultArrow = ['Top', 'Bottom'];
1122
                        $firstPrefix = '';
1123
                        $firstId = '';
1124
                        $secondId = '';
1125
                        $secondPrefix = '';
1126
                        if (is_numeric($pos = strpos($parts[0], 'SG'))) {
1127
                            $firstPrefix = 'sg';
1128
                            $firstId = str_replace('SG', '', $parts[0]);
1129
                        }
1130
1131
                        if (is_numeric($pos = strpos($parts[1], 'SG'))) {
1132
                            $secondPrefix = 'sg';
1133
                            $secondId = str_replace('SG', '', $parts[1]);
1134
                        }
1135
                        if (!empty($secondId) && !empty($firstId)) {
1136
                            $connections[] = [
1137
                                'from' => $firstPrefix.$firstId,
1138
                                'to' => $secondPrefix.$secondId,
1139
                                $defaultArrow,
1140
                            ];
1141
                            $found = true;
1142
                        }
1143
                    }
1144
                }
1145
1146
                if ($found == false) {
1147
                    // case DrawArrowFrom is an integer
1148
                    $defaultArrow = ['Left', 'Right'];
1149
                    if (isset($groupCourseList[$column]) &&
1150
                        in_array($arrow, $groupCourseList[$column])
1151
                    ) {
1152
                        $defaultArrow = ['Top', 'Bottom'];
1153
                    }
1154
                    $graphHtml .= self::createConnection(
1155
                        "row_$arrow",
1156
                        "row_$id",
1157
                        $defaultArrow
1158
                    );
1159
1160
                    $connections[] = [
1161
                        'from' => "v$arrow",
1162
                        'to' => "v$id",
1163
                    ];
1164
                }
1165
            }
1166
        }
1167
1168
        return $graphHtml;
1169
    }
1170
1171
    /**
1172
     * @param array  $groupCourseList list of groups and their courses
1173
     * @param int    $group
1174
     * @param string $groupLabel
1175
     * @param bool   $showGroupLine
1176
     * @param array  $subGroupList
1177
     * @param $widthGroup
1178
     *
1179
     * @return string
1180
     */
1181
    public static function parseSubGroups(
1182
        $groupCourseList,
1183
        $group,
1184
        $groupLabel,
1185
        $showGroupLine,
1186
        $subGroupList,
1187
        $widthGroup
1188
    ) {
1189
        $topValue = 90;
1190
        $defaultSpace = 40;
1191
        $leftGroup = $defaultSpace.'px';
1192
        if ($group == 1) {
1193
            $leftGroup = 0;
1194
        }
1195
1196
        $groupIdTag = "group_$group";
1197
        $borderLine = $showGroupLine === true ? 'border-style:solid;' : '';
1198
1199
        $graphHtml = '<div
1200
            id="'.$groupIdTag.'" class="career_group"
1201
            style=" '.$borderLine.' padding:15px; float:left; margin-left:'.$leftGroup.'; width:'.$widthGroup.'%">';
1202
1203
        if (!empty($groupLabel)) {
1204
            $graphHtml .= '<h3>'.$groupLabel.'</h3>';
1205
        }
1206
1207
        foreach ($subGroupList as $subGroup => $subGroupData) {
1208
            $subGroupLabel = isset($subGroupData['label']) ? $subGroupData['label'] : '';
1209
            $columnList = isset($subGroupData['columns']) ? $subGroupData['columns'] : [];
1210
1211
            if (empty($columnList)) {
1212
                continue;
1213
            }
1214
1215
            $line = '';
1216
            if (!empty($subGroup)) {
1217
                $line = 'border-style:solid;';
1218
            }
1219
1220
            // padding:15px;
1221
            $graphHtml .= '<div
1222
                id="subgroup_'.$subGroup.'" class="career_subgroup"
1223
                style="'.$line.' margin-bottom:20px; padding:15px; float:left; margin-left:0px; width:100%">';
1224
            if (!empty($subGroupLabel)) {
1225
                $graphHtml .= '<h3>'.$subGroupLabel.'</h3>';
1226
            }
1227
            foreach ($columnList as $column => $rows) {
1228
                $leftColumn = $defaultSpace.'px';
1229
                if ($column == 1) {
1230
                    $leftColumn = 0;
1231
                }
1232
                if (count($columnList) == 1) {
1233
                    $leftColumn = 0;
1234
                }
1235
1236
                $widthColumn = 85 / count($columnList);
1237
                $graphHtml .= '<div
1238
                    id="col_'.$column.'" class="career_column"
1239
                    style="padding:15px;float:left; margin-left:'.$leftColumn.'; width:'.$widthColumn.'%">';
1240
                $maxRow = 0;
1241
                foreach ($rows as $row => $vertex) {
1242
                    if ($row > $maxRow) {
1243
                        $maxRow = $row;
1244
                    }
1245
                }
1246
1247
                $newRowList = [];
1248
                $defaultSubGroup = -1;
1249
                $subGroupCountList = [];
1250
                for ($i = 0; $i < $maxRow; $i++) {
1251
                    /** @var Vertex $vertex */
1252
                    $vertex = isset($rows[$i + 1]) ? $rows[$i + 1] : null;
1253
                    if (!is_null($vertex)) {
1254
                        $subGroup = $vertex->getAttribute('SubGroup');
1255
                        if ($subGroup == '' || empty($subGroup)) {
1256
                            $defaultSubGroup = 0;
1257
                        } else {
1258
                            $defaultSubGroup = (int) $subGroup;
1259
                        }
1260
                    }
1261
                    $newRowList[$i + 1][$defaultSubGroup][] = $vertex;
1262
                    if (!isset($subGroupCountList[$defaultSubGroup])) {
1263
                        $subGroupCountList[$defaultSubGroup] = 1;
1264
                    } else {
1265
                        $subGroupCountList[$defaultSubGroup]++;
1266
                    }
1267
                }
1268
1269
                $subGroup = null;
1270
                $subGroupAdded = [];
1271
                /** @var Vertex $vertex */
1272
                foreach ($newRowList as $row => $subGroupList) {
1273
                    foreach ($subGroupList as $subGroup => $vertexList) {
1274
                        if (!empty($subGroup) && $subGroup != -1) {
1275
                            if (!isset($subGroupAdded[$subGroup])) {
1276
                                $subGroupAdded[$subGroup] = 1;
1277
                            } else {
1278
                                $subGroupAdded[$subGroup]++;
1279
                            }
1280
                        }
1281
1282
                        foreach ($vertexList as $vertex) {
1283
                            if (is_null($vertex)) {
1284
                                $graphHtml .= '<div class="career_empty" style="height: 130px">';
1285
                                $graphHtml .= '</div>';
1286
                                continue;
1287
                            }
1288
1289
                            $id = $vertex->getId();
1290
                            $rowId = "row_$row";
1291
                            $graphHtml .= '<div id = "row_'.$id.'" class="'.$rowId.' career_row" >';
1292
                            $color = '';
1293
                            if (!empty($vertex->getAttribute('DefinedColor'))) {
1294
                                $color = $vertex->getAttribute('DefinedColor');
1295
                            }
1296
                            $content = $vertex->getAttribute('Notes');
1297
                            $content .= '<div class="pull-right">['.$id.']</div>';
1298
1299
                            $title = $vertex->getAttribute('graphviz.label');
1300
                            if (!empty($vertex->getAttribute('LinkedElement'))) {
1301
                                $title = Display::url($title, $vertex->getAttribute('LinkedElement'));
1302
                            }
1303
1304
                            $graphHtml .= Display::panel(
1305
                                $content,
1306
                                $title,
1307
                                null,
1308
                                null,
1309
                                null,
1310
                                null,
1311
                                $color
1312
                            );
1313
                            $graphHtml .= '</div>';
1314
1315
                            $arrow = $vertex->getAttribute('DrawArrowFrom');
1316
                            $found = false;
1317
                            if (!empty($arrow)) {
1318
                                $pos = strpos($arrow, 'SG');
1319
                                if ($pos === false) {
1320
                                    $pos = strpos($arrow, 'G');
1321
                                    if (is_numeric($pos)) {
1322
                                        $parts = explode('G', $arrow);
1323
                                        if (empty($parts[0]) && count($parts) == 2) {
1324
                                            $groupArrow = $parts[1];
1325
                                            $graphHtml .= self::createConnection(
1326
                                                "group_$groupArrow",
1327
                                                "row_$id",
1328
                                                ['Left', 'Right']
1329
                                            );
1330
                                            $found = true;
1331
                                        }
1332
                                    }
1333
                                } else {
1334
                                    $parts = explode('SG', $arrow);
1335
                                    if (empty($parts[0]) && count($parts) == 2) {
1336
                                        $subGroupArrow = $parts[1];
1337
                                        $graphHtml .= self::createConnection(
1338
                                            "subgroup_$subGroupArrow",
1339
                                            "row_$id",
1340
                                            ['Left', 'Right']
1341
                                        );
1342
                                        $found = true;
1343
                                    }
1344
                                }
1345
                            }
1346
1347
                            if ($found == false) {
1348
                                $defaultArrow = ['Left', 'Right'];
1349
                                if (isset($groupCourseList[$group]) &&
1350
                                    in_array($arrow, $groupCourseList[$group])
1351
                                ) {
1352
                                    $defaultArrow = ['Top', 'Bottom'];
1353
                                }
1354
                                $graphHtml .= self::createConnection(
1355
                                    "row_$arrow",
1356
                                    "row_$id",
1357
                                    $defaultArrow
1358
                                );
1359
                            }
1360
                        }
1361
                    }
1362
                }
1363
                $graphHtml .= '</div>';
1364
            }
1365
            $graphHtml .= '</div>';
1366
        }
1367
        $graphHtml .= '</div>';
1368
1369
        return $graphHtml;
1370
    }
1371
1372
    /**
1373
     * @param string $source
1374
     * @param string $target
1375
     * @param array  $anchor
1376
     *
1377
     * @return string
1378
     */
1379
    public static function createConnection($source, $target, $anchor = [])
1380
    {
1381
        if (empty($anchor)) {
1382
            // Default
1383
            $anchor = ['Bottom', 'Right'];
1384
        }
1385
1386
        $anchor = implode('","', $anchor);
1387
        $html = '<script>
1388
1389
        var connectorPaintStyle = {
1390
            strokeWidth: 2,
1391
            stroke: "#a31ed3",
1392
            joinstyle: "round",
1393
            outlineStroke: "white",
1394
            outlineWidth: 2
1395
        },
1396
        // .. and this is the hover style.
1397
        connectorHoverStyle = {
1398
            strokeWidth: 3,
1399
            stroke: "#216477",
1400
            outlineWidth: 5,
1401
            outlineStroke: "white"
1402
        },
1403
        endpointHoverStyle = {
1404
            fill: "#E80CAF",
1405
            stroke: "#E80CAF"
1406
        };
1407
        jsPlumb.ready(function() { ';
1408
        $html .= 'jsPlumb.connect({
1409
            source:"'.$source.'",
1410
            target:"'.$target.'",
1411
            endpoint:[ "Rectangle", { width:1, height:1 }],
1412
            connector: ["Flowchart"],
1413
            paintStyle: connectorPaintStyle,
1414
            hoverPaintStyle: endpointHoverStyle,
1415
            anchor: ["'.$anchor.'"],
1416
            overlays: [
1417
                [
1418
                    "Arrow",
1419
                    {
1420
                        location:1,
1421
                        width:11,
1422
                        length:11
1423
                    }
1424
                ],
1425
            ],
1426
        });';
1427
        $html .= '});</script>'.PHP_EOL;
1428
1429
        return $html;
1430
    }
1431
1432
    public static function renderDiagramFooter(): string
1433
    {
1434
        $footer = '';
1435
        if (api_get_configuration_value('career_diagram_legend')) {
1436
            $footer .= get_lang('CareerDiagramLegend');
1437
        }
1438
        if (api_get_configuration_value('career_diagram_disclaimer')) {
1439
            $footer .= get_lang('CareerDiagramDisclaimer');
1440
        }
1441
1442
        return $footer;
1443
    }
1444
1445
    public static function addCareerFieldsToForm(FormValidator $form, array $values = [])
1446
    {
1447
        $career = new self();
1448
        $careerList = $career->get_all();
1449
        $list = array_column($careerList, 'name', 'id');
1450
1451
        $url = api_get_path(WEB_AJAX_PATH).'career.ajax.php';
1452
1453
        $form->addHtml('<script>
1454
                $(function () {
1455
                    var url = "'.$url.'";
1456
                    var $txtPromotion = $("#promotion_id");
1457
1458
                    $("#career_id").on("change", function () {
1459
                        $("#promotion").show();
1460
1461
                        var id = this.value;
1462
1463
                        $txtPromotion.empty().append($("<option>", {
1464
                            value: 0,
1465
                            text: "'.get_lang('All').'"
1466
                        }));
1467
1468
                        $.getJSON(url, {
1469
                            "career_id": id,
1470
                            "a": "get_promotions"
1471
                        }).done(function (data) {
1472
                            $.each(data, function (index, value) {
1473
                                $txtPromotion.append($("<option>", {
1474
                                    value: value.id,
1475
                                    text: value.name
1476
                                }));
1477
                            });
1478
1479
                            $txtPromotion.selectpicker("refresh");
1480
                        });
1481
                    });
1482
                });
1483
            </script>');
1484
        $form->addSelect(
1485
            'career_id',
1486
            get_lang('Career'),
1487
            $list,
1488
            [
1489
                'placeholder' => get_lang('SelectAnOption'),
1490
                'id' => 'career_id',
1491
            ]
1492
        );
1493
1494
        $display = 'none;';
1495
        $options = [];
1496
        if (isset($values['promotion_id'])) {
1497
            $promotion = new Promotion();
1498
            $promotion = $promotion->get($values['promotion_id']);
1499
            if ($promotion) {
1500
                $options = [$promotion['id'] => $promotion['name']];
1501
                $display = 'block';
1502
            }
1503
        }
1504
1505
        $form->addHtml('<div id="promotion" style="display:'.$display.';">');
1506
        $form->addSelect(
1507
            'promotion_id',
1508
            get_lang('Promotion'),
1509
            $options,
1510
            ['id' => 'promotion_id']
1511
        );
1512
        $form->addHtml('</div>');
1513
    }
1514
}
1515