Total Complexity | 221 |
Total Lines | 1502 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 | { |
||
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 = []) |
||
1430 | } |
||
1431 | |||
1432 | public static function renderDiagramFooter(): string |
||
1443 | } |
||
1444 | |||
1445 | public static function addCareerFieldsToForm(FormValidator $form, array $values = []) |
||
1446 | { |
||
1515 |