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