Total Complexity | 215 |
Total Lines | 1418 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Career often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Career, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
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() |
||
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() |
||
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) |
||
345 | } |
||
346 | |||
347 | /** |
||
348 | * @param array |
||
349 | * @param Graph $graph |
||
350 | * |
||
351 | * @return string |
||
352 | */ |
||
353 | public static function renderDiagram($careerInfo, $graph) |
||
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 = isset($careerInfo['id']) ? $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 | $oldGroup = null; |
||
815 | $newOrder = []; |
||
816 | foreach ($columnList as $key => $subGroupList) { |
||
817 | $newGroup = $subGroupList['group']; |
||
818 | $label = $subGroupList['group_label']; |
||
819 | $newOrder[$newGroup]['items'][] = $subGroupList; |
||
820 | $newOrder[$newGroup]['label'] = $label; |
||
821 | } |
||
822 | |||
823 | foreach ($newOrder as $newGroup => $data) { |
||
824 | $label = $data['label']; |
||
825 | $subGroupList = $data['items']; |
||
826 | |||
827 | if (!isset($graph->groupList[$newGroup])) { |
||
828 | $graph->groupList[$newGroup]['min_x'] = 1000; |
||
829 | $graph->groupList[$newGroup]['min_y'] = 1000; |
||
830 | $graph->groupList[$newGroup]['max_width'] = 0; |
||
831 | $graph->groupList[$newGroup]['max_height'] = 0; |
||
832 | $graph->groupList[$newGroup]['label'] = $label; |
||
833 | } |
||
834 | |||
835 | $maxColumn = 0; |
||
836 | $maxRow = 0; |
||
837 | $minColumn = 100; |
||
838 | $minRow = 100; |
||
839 | foreach ($subGroupList as $item) { |
||
840 | /** @var Vertex $vertex */ |
||
841 | foreach ($item['items'] as $vertex) { |
||
842 | $column = $vertex->getAttribute('Column'); |
||
843 | $realRow = $vertex->getAttribute('Row'); |
||
844 | |||
845 | if ($column > $maxColumn) { |
||
846 | $maxColumn = $column; |
||
847 | } |
||
848 | if ($realRow > $maxRow) { |
||
849 | $maxRow = $realRow; |
||
850 | } |
||
851 | |||
852 | if ($column < $minColumn) { |
||
853 | $minColumn = $column; |
||
854 | } |
||
855 | if ($realRow < $minRow) { |
||
856 | $minRow = $realRow; |
||
857 | } |
||
858 | } |
||
859 | } |
||
860 | |||
861 | if (!empty($newGroup)) { |
||
862 | $graphHtml .= '<div |
||
863 | id ="group_'.$newGroup.'" |
||
864 | class="group'.$newGroup.' group_class" |
||
865 | style="display:grid; |
||
866 | align-self: start; |
||
867 | grid-gap: 10px; |
||
868 | justify-items: stretch; |
||
869 | align-items: start; |
||
870 | align-content: start; |
||
871 | justify-content: stretch; |
||
872 | grid-area:'.$minRow.'/'.$minColumn.'/'.$maxRow.'/'.$maxColumn.'">'; //style="display:grid" |
||
873 | } |
||
874 | |||
875 | $addRow = 0; |
||
876 | if (!empty($label)) { |
||
877 | $graphHtml .= "<div class='my_label' style='grid-area:$minRow/$minColumn/$maxRow/$maxColumn'>$label</div>"; |
||
878 | $addRow = 1; |
||
879 | } |
||
880 | |||
881 | foreach ($subGroupList as $item) { |
||
882 | $graphHtml .= self::parseVertexList( |
||
883 | $groupCourseList, |
||
884 | $item['items'], |
||
885 | $addRow, |
||
886 | $graph, |
||
887 | $newGroup, |
||
888 | $connections, |
||
889 | $userResult |
||
890 | ); |
||
891 | } |
||
892 | |||
893 | if (!empty($newGroup)) { |
||
894 | $graphHtml .= '</div >'; |
||
895 | } |
||
896 | } |
||
897 | |||
898 | return $graphHtml; |
||
899 | } |
||
900 | |||
901 | /** |
||
902 | * @param array $groupCourseList |
||
903 | * @param array $vertexList |
||
904 | * @param int $addRow |
||
905 | * @param stdClass $graph |
||
906 | * @param int $group |
||
907 | * @param array $connections |
||
908 | * @param array $userResult |
||
909 | * |
||
910 | * @return string |
||
911 | */ |
||
912 | public static function parseVertexList($groupCourseList, $vertexList, $addRow, &$graph, $group, &$connections, $userResult) |
||
1155 | } |
||
1156 | |||
1157 | /** |
||
1158 | * @param array $groupCourseList list of groups and their courses |
||
1159 | * @param int $group |
||
1160 | * @param string $groupLabel |
||
1161 | * @param bool $showGroupLine |
||
1162 | * @param array $subGroupList |
||
1163 | * @param $widthGroup |
||
1164 | * |
||
1165 | * @return string |
||
1166 | */ |
||
1167 | public static function parseSubGroups( |
||
1168 | $groupCourseList, |
||
1169 | $group, |
||
1170 | $groupLabel, |
||
1171 | $showGroupLine, |
||
1172 | $subGroupList, |
||
1173 | $widthGroup |
||
1174 | ) { |
||
1175 | $topValue = 90; |
||
1176 | $defaultSpace = 40; |
||
1177 | $leftGroup = $defaultSpace.'px'; |
||
1178 | if ($group == 1) { |
||
1179 | $leftGroup = 0; |
||
1180 | } |
||
1181 | |||
1182 | $groupIdTag = "group_$group"; |
||
1183 | $borderLine = $showGroupLine === true ? 'border-style:solid;' : ''; |
||
1184 | |||
1185 | $graphHtml = '<div |
||
1186 | id="'.$groupIdTag.'" class="career_group" |
||
1187 | style=" '.$borderLine.' padding:15px; float:left; margin-left:'.$leftGroup.'; width:'.$widthGroup.'%">'; |
||
1188 | |||
1189 | if (!empty($groupLabel)) { |
||
1190 | $graphHtml .= '<h3>'.$groupLabel.'</h3>'; |
||
1191 | } |
||
1192 | |||
1193 | foreach ($subGroupList as $subGroup => $subGroupData) { |
||
1194 | $subGroupLabel = isset($subGroupData['label']) ? $subGroupData['label'] : ''; |
||
1195 | $columnList = isset($subGroupData['columns']) ? $subGroupData['columns'] : []; |
||
1196 | |||
1197 | if (empty($columnList)) { |
||
1198 | continue; |
||
1199 | } |
||
1200 | |||
1201 | $line = ''; |
||
1202 | if (!empty($subGroup)) { |
||
1203 | $line = 'border-style:solid;'; |
||
1204 | } |
||
1205 | |||
1206 | // padding:15px; |
||
1207 | $graphHtml .= '<div |
||
1208 | id="subgroup_'.$subGroup.'" class="career_subgroup" |
||
1209 | style="'.$line.' margin-bottom:20px; padding:15px; float:left; margin-left:0px; width:100%">'; |
||
1210 | if (!empty($subGroupLabel)) { |
||
1211 | $graphHtml .= '<h3>'.$subGroupLabel.'</h3>'; |
||
1212 | } |
||
1213 | foreach ($columnList as $column => $rows) { |
||
1214 | $leftColumn = $defaultSpace.'px'; |
||
1215 | if ($column == 1) { |
||
1216 | $leftColumn = 0; |
||
1217 | } |
||
1218 | if (count($columnList) == 1) { |
||
1219 | $leftColumn = 0; |
||
1220 | } |
||
1221 | |||
1222 | $widthColumn = 85 / count($columnList); |
||
1223 | $graphHtml .= '<div |
||
1224 | id="col_'.$column.'" class="career_column" |
||
1225 | style="padding:15px;float:left; margin-left:'.$leftColumn.'; width:'.$widthColumn.'%">'; |
||
1226 | $maxRow = 0; |
||
1227 | foreach ($rows as $row => $vertex) { |
||
1228 | if ($row > $maxRow) { |
||
1229 | $maxRow = $row; |
||
1230 | } |
||
1231 | } |
||
1232 | |||
1233 | $newRowList = []; |
||
1234 | $defaultSubGroup = -1; |
||
1235 | $subGroupCountList = []; |
||
1236 | for ($i = 0; $i < $maxRow; $i++) { |
||
1237 | /** @var Vertex $vertex */ |
||
1238 | $vertex = isset($rows[$i + 1]) ? $rows[$i + 1] : null; |
||
1239 | if (!is_null($vertex)) { |
||
1240 | $subGroup = $vertex->getAttribute('SubGroup'); |
||
1241 | if ($subGroup == '' || empty($subGroup)) { |
||
1242 | $defaultSubGroup = 0; |
||
1243 | } else { |
||
1244 | $defaultSubGroup = (int) $subGroup; |
||
1245 | } |
||
1246 | } |
||
1247 | $newRowList[$i + 1][$defaultSubGroup][] = $vertex; |
||
1248 | if (!isset($subGroupCountList[$defaultSubGroup])) { |
||
1249 | $subGroupCountList[$defaultSubGroup] = 1; |
||
1250 | } else { |
||
1251 | $subGroupCountList[$defaultSubGroup]++; |
||
1252 | } |
||
1253 | } |
||
1254 | |||
1255 | $subGroup = null; |
||
1256 | $subGroupAdded = []; |
||
1257 | /** @var Vertex $vertex */ |
||
1258 | foreach ($newRowList as $row => $subGroupList) { |
||
1259 | foreach ($subGroupList as $subGroup => $vertexList) { |
||
1260 | if (!empty($subGroup) && $subGroup != -1) { |
||
1261 | if (!isset($subGroupAdded[$subGroup])) { |
||
1262 | $subGroupAdded[$subGroup] = 1; |
||
1263 | } else { |
||
1264 | $subGroupAdded[$subGroup]++; |
||
1265 | } |
||
1266 | } |
||
1267 | |||
1268 | foreach ($vertexList as $vertex) { |
||
1269 | if (is_null($vertex)) { |
||
1270 | $graphHtml .= '<div class="career_empty" style="height: 130px">'; |
||
1271 | $graphHtml .= '</div>'; |
||
1272 | continue; |
||
1273 | } |
||
1274 | |||
1275 | $id = $vertex->getId(); |
||
1276 | $rowId = "row_$row"; |
||
1277 | $graphHtml .= '<div id = "row_'.$id.'" class="'.$rowId.' career_row" >'; |
||
1278 | $color = ''; |
||
1279 | if (!empty($vertex->getAttribute('DefinedColor'))) { |
||
1280 | $color = $vertex->getAttribute('DefinedColor'); |
||
1281 | } |
||
1282 | $content = $vertex->getAttribute('Notes'); |
||
1283 | $content .= '<div class="pull-right">['.$id.']</div>'; |
||
1284 | |||
1285 | $title = $vertex->getAttribute('graphviz.label'); |
||
1286 | if (!empty($vertex->getAttribute('LinkedElement'))) { |
||
1287 | $title = Display::url($title, $vertex->getAttribute('LinkedElement')); |
||
1288 | } |
||
1289 | |||
1290 | $graphHtml .= Display::panel( |
||
1291 | $content, |
||
1292 | $title, |
||
1293 | null, |
||
1294 | null, |
||
1295 | null, |
||
1296 | null, |
||
1297 | $color |
||
1298 | ); |
||
1299 | $graphHtml .= '</div>'; |
||
1300 | |||
1301 | $arrow = $vertex->getAttribute('DrawArrowFrom'); |
||
1302 | $found = false; |
||
1303 | if (!empty($arrow)) { |
||
1304 | $pos = strpos($arrow, 'SG'); |
||
1305 | if ($pos === false) { |
||
1306 | $pos = strpos($arrow, 'G'); |
||
1307 | if (is_numeric($pos)) { |
||
1308 | $parts = explode('G', $arrow); |
||
1309 | if (empty($parts[0]) && count($parts) == 2) { |
||
1310 | $groupArrow = $parts[1]; |
||
1311 | $graphHtml .= self::createConnection( |
||
1312 | "group_$groupArrow", |
||
1313 | "row_$id", |
||
1314 | ['Left', 'Right'] |
||
1315 | ); |
||
1316 | $found = true; |
||
1317 | } |
||
1318 | } |
||
1319 | } else { |
||
1320 | $parts = explode('SG', $arrow); |
||
1321 | if (empty($parts[0]) && count($parts) == 2) { |
||
1322 | $subGroupArrow = $parts[1]; |
||
1323 | $graphHtml .= self::createConnection( |
||
1324 | "subgroup_$subGroupArrow", |
||
1325 | "row_$id", |
||
1326 | ['Left', 'Right'] |
||
1327 | ); |
||
1328 | $found = true; |
||
1329 | } |
||
1330 | } |
||
1331 | } |
||
1332 | |||
1333 | if ($found == false) { |
||
1334 | $defaultArrow = ['Left', 'Right']; |
||
1335 | if (isset($groupCourseList[$group]) && |
||
1336 | in_array($arrow, $groupCourseList[$group]) |
||
1337 | ) { |
||
1338 | $defaultArrow = ['Top', 'Bottom']; |
||
1339 | } |
||
1340 | $graphHtml .= self::createConnection( |
||
1341 | "row_$arrow", |
||
1342 | "row_$id", |
||
1343 | $defaultArrow |
||
1344 | ); |
||
1345 | } |
||
1346 | } |
||
1347 | } |
||
1348 | } |
||
1349 | $graphHtml .= '</div>'; |
||
1350 | } |
||
1351 | $graphHtml .= '</div>'; |
||
1352 | } |
||
1353 | $graphHtml .= '</div>'; |
||
1354 | |||
1355 | return $graphHtml; |
||
1356 | } |
||
1357 | |||
1358 | /** |
||
1359 | * @param string $source |
||
1360 | * @param string $target |
||
1361 | * @param array $anchor |
||
1362 | * |
||
1363 | * @return string |
||
1364 | */ |
||
1365 | public static function createConnection($source, $target, $anchor = []) |
||
1416 | } |
||
1417 | |||
1418 | public static function renderDiagramFooter(): string |
||
1429 | } |
||
1430 | } |
||
1431 |