123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260 |
- <?php
- /* For licensing terms, see /license.txt */
- use Fhaculty\Graph\Graph;
- use Fhaculty\Graph\Vertex;
- /**
- * Class Career.
- */
- class Career extends Model
- {
- public $table;
- public $columns = [
- 'id',
- 'name',
- 'description',
- 'status',
- 'created_at',
- 'updated_at',
- ];
- /**
- * Constructor.
- */
- public function __construct()
- {
- $this->table = Database::get_main_table(TABLE_CAREER);
- }
- /**
- * Get the count of elements.
- *
- * @return int
- */
- public function get_count()
- {
- $row = Database::select(
- 'count(*) as count',
- $this->table,
- [],
- 'first'
- );
- return $row['count'];
- }
- /**
- * @param array $where_conditions
- *
- * @return array
- */
- public function get_all($where_conditions = [])
- {
- return Database::select(
- '*',
- $this->table,
- ['where' => $where_conditions, 'order' => 'name ASC']
- );
- }
- /**
- * Update all promotion status by career.
- *
- * @param int $career_id
- * @param int $status (1 or 0)
- */
- public function update_all_promotion_status_by_career_id($career_id, $status)
- {
- $promotion = new Promotion();
- $promotion_list = $promotion->get_all_promotions_by_career_id($career_id);
- if (!empty($promotion_list)) {
- foreach ($promotion_list as $item) {
- $params['id'] = $item['id'];
- $params['status'] = $status;
- $promotion->update($params);
- $promotion->update_all_sessions_status_by_promotion_id($params['id'], $status);
- }
- }
- }
- /**
- * Returns HTML the title + grid.
- *
- * @return string
- */
- public function display()
- {
- $html = '<div class="actions" style="margin-bottom:20px">';
- $html .= '<a href="career_dashboard.php">'.
- Display::return_icon('back.png', get_lang('Back'), '', ICON_SIZE_MEDIUM).'</a>';
- if (api_is_platform_admin()) {
- $html .= '<a href="'.api_get_self().'?action=add">'.
- Display::return_icon('new_career.png', get_lang('Add'), '', ICON_SIZE_MEDIUM).'</a>';
- }
- $html .= '</div>';
- $html .= Display::grid_html('careers');
- return $html;
- }
- /**
- * @return array
- */
- public function get_status_list()
- {
- return [
- CAREER_STATUS_ACTIVE => get_lang('Unarchived'),
- CAREER_STATUS_INACTIVE => get_lang('Archived'),
- ];
- }
- /**
- * Returns a Form validator Obj.
- *
- * @todo the form should be auto generated
- *
- * @param string $url
- * @param string $action add, edit
- *
- * @return FormValidator
- */
- public function return_form($url, $action)
- {
- $form = new FormValidator('career', 'post', $url);
- // Setting the form elements
- $header = get_lang('Add');
- if ($action == 'edit') {
- $header = get_lang('Modify');
- }
- $id = isset($_GET['id']) ? intval($_GET['id']) : '';
- $form->addHeader($header);
- $form->addHidden('id', $id);
- $form->addElement('text', 'name', get_lang('Name'), ['size' => '70']);
- $form->addHtmlEditor(
- 'description',
- get_lang('Description'),
- false,
- false,
- [
- 'ToolbarSet' => 'Careers',
- 'Width' => '100%',
- 'Height' => '250',
- ]
- );
- $status_list = $this->get_status_list();
- $form->addElement('select', 'status', get_lang('Status'), $status_list);
- if ($action == 'edit') {
- $form->addElement('text', 'created_at', get_lang('CreatedAt'));
- $form->freeze('created_at');
- }
- if ($action == 'edit') {
- $form->addButtonSave(get_lang('Modify'), 'submit');
- } else {
- $form->addButtonCreate(get_lang('Add'), 'submit');
- }
- // Setting the defaults
- $defaults = $this->get($id);
- if (!empty($defaults['created_at'])) {
- $defaults['created_at'] = api_convert_and_format_date($defaults['created_at']);
- }
- if (!empty($defaults['updated_at'])) {
- $defaults['updated_at'] = api_convert_and_format_date($defaults['updated_at']);
- }
- $form->setDefaults($defaults);
- // Setting the rules
- $form->addRule('name', get_lang('ThisFieldIsRequired'), 'required');
- return $form;
- }
- /**
- * Copies the career to a new one.
- *
- * @param int Career ID
- * @param bool Whether or not to copy the promotions inside
- *
- * @return int New career ID on success, false on failure
- */
- public function copy($id, $copy_promotions = false)
- {
- $career = $this->get($id);
- $new = [];
- foreach ($career as $key => $val) {
- switch ($key) {
- case 'id':
- case 'updated_at':
- break;
- case 'name':
- $val .= ' '.get_lang('CopyLabelSuffix');
- $new[$key] = $val;
- break;
- case 'created_at':
- $val = api_get_utc_datetime();
- $new[$key] = $val;
- break;
- default:
- $new[$key] = $val;
- break;
- }
- }
- $cid = $this->save($new);
- if ($copy_promotions) {
- //Now also copy each session of the promotion as a new session and register it inside the promotion
- $promotion = new Promotion();
- $promo_list = $promotion->get_all_promotions_by_career_id($id);
- if (!empty($promo_list)) {
- foreach ($promo_list as $item) {
- $promotion->copy($item['id'], $cid, true);
- }
- }
- }
- return $cid;
- }
- /**
- * @param int $career_id
- *
- * @return bool
- */
- public function get_status($career_id)
- {
- $table = Database::get_main_table(TABLE_CAREER);
- $career_id = intval($career_id);
- $sql = "SELECT status FROM $table WHERE id = '$career_id'";
- $result = Database::query($sql);
- if (Database::num_rows($result) > 0) {
- $data = Database::fetch_array($result);
- return $data['status'];
- } else {
- return false;
- }
- }
- /**
- * @param array $params
- * @param bool $show_query
- *
- * @return int
- */
- public function save($params, $show_query = false)
- {
- if (isset($params['description'])) {
- $params['description'] = Security::remove_XSS($params['description']);
- }
- $id = parent::save($params);
- if (!empty($id)) {
- Event::addEvent(
- LOG_CAREER_CREATE,
- LOG_CAREER_ID,
- $id,
- api_get_utc_datetime(),
- api_get_user_id()
- );
- }
- return $id;
- }
- /**
- * Delete a record from the career table and report in the default events log table.
- *
- * @param int $id The ID of the career to delete
- *
- * @return bool True if the career could be deleted, false otherwise
- */
- public function delete($id)
- {
- $res = parent::delete($id);
- if ($res) {
- $extraFieldValues = new ExtraFieldValue('career');
- $extraFieldValues->deleteValuesByItem($id);
- Event::addEvent(
- LOG_CAREER_DELETE,
- LOG_CAREER_ID,
- $id,
- api_get_utc_datetime(),
- api_get_user_id()
- );
- }
- return $res;
- }
- /**
- * {@inheritdoc}
- */
- public function update($params, $showQuery = false)
- {
- if (isset($params['description'])) {
- $params['description'] = Security::remove_XSS($params['description']);
- }
- return parent::update($params, $showQuery);
- }
- /**
- * @param array
- * @param Graph $graph
- *
- * @return string
- */
- public static function renderDiagram($careerInfo, $graph)
- {
- if (!($graph instanceof Graph)) {
- return '';
- }
- // Getting max column
- $maxColumn = 0;
- foreach ($graph->getVertices() as $vertex) {
- $groupId = (int) $vertex->getGroup();
- if ($groupId > $maxColumn) {
- $maxColumn = $groupId;
- }
- }
- $list = [];
- /** @var Vertex $vertex */
- foreach ($graph->getVertices() as $vertex) {
- $group = $vertex->getAttribute('Group');
- $groupData = explode(':', $group);
- $group = $groupData[0];
- $groupLabel = isset($groupData[1]) ? $groupData[1] : '';
- $subGroup = $vertex->getAttribute('SubGroup');
- $subGroupData = explode(':', $subGroup);
- $column = $vertex->getGroup();
- $row = $vertex->getAttribute('Row');
- $subGroupId = $subGroupData[0];
- $label = isset($subGroupData[1]) ? $subGroupData[1] : '';
- $list[$group][$subGroupId]['columns'][$column][$row] = $vertex;
- $list[$group][$subGroupId]['label'] = $label;
- $list[$group]['label'] = $groupLabel;
- }
- $maxGroups = count($list);
- $widthGroup = 30;
- if (!empty($maxGroups)) {
- $widthGroup = 85 / $maxGroups;
- }
- $connections = '';
- $groupDrawLine = [];
- $groupCourseList = [];
- // Read Connections column
- foreach ($list as $group => $subGroupList) {
- foreach ($subGroupList as $subGroupData) {
- $columns = isset($subGroupData['columns']) ? $subGroupData['columns'] : [];
- $showGroupLine = true;
- if (count($columns) == 1) {
- $showGroupLine = false;
- }
- $groupDrawLine[$group] = $showGroupLine;
- //if ($showGroupLine == false) {
- /** @var Vertex $vertex */
- foreach ($columns as $row => $items) {
- foreach ($items as $vertex) {
- if ($vertex instanceof Vertex) {
- $groupCourseList[$group][] = $vertex->getId();
- $connectionList = $vertex->getAttribute('Connections');
- $firstConnection = '';
- $secondConnection = '';
- if (!empty($connectionList)) {
- $explode = explode('-', $connectionList);
- $pos = strpos($explode[0], 'SG');
- if ($pos === false) {
- $pos = strpos($explode[0], 'G');
- if (is_numeric($pos)) {
- // group_123 id
- $groupValueId = (int) str_replace(
- 'G',
- '',
- $explode[0]
- );
- $firstConnection = 'group_'.$groupValueId;
- $groupDrawLine[$groupValueId] = true;
- } else {
- // Course block (row_123 id)
- if (!empty($explode[0])) {
- $firstConnection = 'row_'.(int) $explode[0];
- }
- }
- } else {
- // subgroup__123 id
- $firstConnection = 'subgroup_'.(int) str_replace('SG', '', $explode[0]);
- }
- $pos = strpos($explode[1], 'SG');
- if ($pos === false) {
- $pos = strpos($explode[1], 'G');
- if (is_numeric($pos)) {
- $groupValueId = (int) str_replace(
- 'G',
- '',
- $explode[1]
- );
- $secondConnection = 'group_'.$groupValueId;
- $groupDrawLine[$groupValueId] = true;
- } else {
- // Course block (row_123 id)
- if (!empty($explode[0])) {
- $secondConnection = 'row_'.(int) $explode[1];
- }
- }
- } else {
- $secondConnection = 'subgroup_'.(int) str_replace('SG', '', $explode[1]);
- }
- if (!empty($firstConnection) && !empty($firstConnection)) {
- $connections .= self::createConnection(
- $firstConnection,
- $secondConnection,
- ['Left', 'Right']
- );
- }
- }
- }
- }
- }
- //}
- }
- }
- $graphHtml = '<div class="container">';
- foreach ($list as $group => $subGroupList) {
- $showGroupLine = false;
- if (isset($groupDrawLine[$group]) && $groupDrawLine[$group]) {
- $showGroupLine = true;
- }
- $graphHtml .= self::parseSubGroups(
- $groupCourseList,
- $group,
- $list[$group]['label'],
- $showGroupLine,
- $subGroupList,
- $widthGroup
- );
- }
- $graphHtml .= '</div>';
- $graphHtml .= $connections;
- return $graphHtml;
- }
- /**
- * @param Graph $graph
- * @param Template $tpl
- *
- * @return string
- */
- public static function renderDiagramByColumn($graph, $tpl)
- {
- if (!($graph instanceof Graph)) {
- return '';
- }
- // Getting max column
- $maxColumn = 0;
- foreach ($graph->getVertices() as $vertex) {
- $groupId = (int) $vertex->getGroup();
- if ($groupId > $maxColumn) {
- $maxColumn = $groupId;
- }
- }
- $list = [];
- $subGroups = [];
- /** @var Vertex $vertex */
- foreach ($graph->getVertices() as $vertex) {
- $column = $vertex->getGroup();
- $group = $vertex->getAttribute('Group');
- $groupData = explode(':', $group);
- $group = $groupData[0];
- $groupLabel = isset($groupData[1]) ? $groupData[1] : '';
- $subGroup = $vertex->getAttribute('SubGroup');
- $subGroupData = explode(':', $subGroup);
- $row = $vertex->getAttribute('Row');
- $subGroupId = $subGroupData[0];
- $subGroupLabel = isset($subGroupData[1]) ? $subGroupData[1] : '';
- if (!empty($subGroupId) && !in_array($subGroupId, $subGroups)) {
- //$subGroups[$subGroupId][] = $vertex->getId();
- $subGroups[$subGroupId]['items'][] = $vertex->getId();
- $subGroups[$subGroupId]['label'] = $subGroupLabel;
- }
- $list[$column]['rows'][$row]['items'][] = $vertex;
- $list[$column]['rows'][$row]['label'] = $subGroupId;
- $list[$column]['rows'][$row]['group'] = $group;
- $list[$column]['rows'][$row]['group_label'] = $groupLabel;
- $list[$column]['rows'][$row]['subgroup'] = $subGroup;
- $list[$column]['rows'][$row]['subgroup_label'] = $subGroupLabel;
- $list[$column]['label'] = $groupLabel;
- $list[$column]['column'] = $column;
- }
- $connections = '';
- $groupDrawLine = [];
- $groupCourseList = [];
- $simpleConnectionList = [];
- // Read Connections column
- foreach ($list as $column => $groupList) {
- foreach ($groupList['rows'] as $subGroupList) {
- /** @var Vertex $vertex */
- foreach ($subGroupList['items'] as $vertex) {
- if ($vertex instanceof Vertex) {
- $rowId = $vertex->getId();
- $groupCourseList[$vertex->getAttribute('Column')][] = $vertex->getId();
- $connectionList = $vertex->getAttribute('Connections');
- if (empty($connectionList)) {
- continue;
- }
- $simpleFirstConnection = '';
- $simpleSecondConnection = '';
- $explode = explode('-', $connectionList);
- $pos = strpos($explode[0], 'SG');
- if ($pos === false) {
- $pos = strpos($explode[0], 'G');
- if (is_numeric($pos)) {
- // Is group
- $groupValueId = (int) str_replace(
- 'G',
- '',
- $explode[0]
- );
- $groupDrawLine[$groupValueId] = true;
- $simpleFirstConnection = 'g'.(int) $groupValueId;
- } else {
- // Course block (row_123 id)
- if (!empty($explode[0])) {
- $simpleFirstConnection = 'v'.(int) $explode[0];
- }
- }
- } else {
- // subgroup__123 id
- $simpleFirstConnection = 'sg'.(int) str_replace('SG', '', $explode[0]);
- }
- $pos = false;
- if (isset($explode[1])) {
- $pos = strpos($explode[1], 'SG');
- }
- if ($pos === false) {
- if (isset($explode[1])) {
- $pos = strpos($explode[1], 'G');
- $value = $explode[1];
- }
- if (is_numeric($pos)) {
- $groupValueId = (int) str_replace(
- 'G',
- '',
- $value
- );
- $simpleSecondConnection = 'g'.(int) $groupValueId;
- $groupDrawLine[$groupValueId] = true;
- } else {
- // Course block (row_123 id)
- if (!empty($explode[0]) && isset($explode[1])) {
- $simpleSecondConnection = 'v'.(int) $explode[1];
- }
- }
- } else {
- $simpleSecondConnection = 'sg'.(int) str_replace('SG', '', $explode[1]);
- }
- if (!empty($simpleFirstConnection) && !empty($simpleSecondConnection)) {
- $simpleConnectionList[] = [
- 'from' => $simpleFirstConnection,
- 'to' => $simpleSecondConnection,
- ];
- }
- }
- }
- }
- }
- $graphHtml = '';
- $groupsBetweenColumns = [];
- foreach ($list as $column => $columnList) {
- foreach ($columnList['rows'] as $subGroupList) {
- $newGroup = $subGroupList['group'];
- $label = $subGroupList['group_label'];
- $newOrder[$newGroup]['items'][] = $subGroupList;
- $newOrder[$newGroup]['label'] = $label;
- $groupsBetweenColumns[$newGroup][] = $subGroupList;
- }
- }
- // Creates graph
- $graph = new stdClass();
- $graph->blockWidth = 240;
- $graph->blockHeight = 120;
- $graph->xGap = 70;
- $graph->yGap = 40;
- $graph->xDiff = 70;
- $graph->yDiff = 40;
- $graph->groupXGap = 50;
- foreach ($groupsBetweenColumns as $group => $items) {
- self::parseColumnList($groupCourseList, $items, '', $graph, $simpleConnectionList);
- }
- $graphHtml .= '<style>
- .panel-title {
- font-size: 11px;
- }
- </style>';
- // Create groups
- if (!empty($graph->groupList)) {
- $groupList = [];
- $groupDiffX = 20;
- $groupDiffY = 10;
- $style = 'whiteSpace=wrap;rounded;html=1;strokeColor=red;fillColor=none;strokeWidth=2;align=left;verticalAlign=top;';
- foreach ($graph->groupList as $id => $data) {
- if (empty($id)) {
- continue;
- }
- $x = $data['min_x'] - $groupDiffX;
- $y = $data['min_y'] - $groupDiffY;
- $width = $data['max_width'] + ($groupDiffX * 2);
- $height = $data['max_height'] + $groupDiffY * 2;
- $label = '<h4>'.$data['label'].'</h4>';
- $vertexData = "var g$id = graph.insertVertex(parent, null, '$label', $x, $y, $width, $height, '$style');";
- $groupList[] = $vertexData;
- }
- $tpl->assign('group_list', $groupList);
- }
- // Create subgroups
- $subGroupList = [];
- $subGroupListData = [];
- foreach ($subGroups as $subGroupId => $vertexData) {
- $label = $vertexData['label'];
- $vertexIdList = $vertexData['items'];
- foreach ($vertexIdList as $rowId) {
- $data = $graph->allData[$rowId];
- $originalRow = $data['row'];
- $column = $data['column'];
- $x = $data['x'];
- $y = $data['y'];
- $width = $data['width'];
- $height = $data['height'];
- if (!isset($subGroupListData[$subGroupId])) {
- $subGroupListData[$subGroupId]['min_x'] = 1000;
- $subGroupListData[$subGroupId]['min_y'] = 1000;
- $subGroupListData[$subGroupId]['max_width'] = 0;
- $subGroupListData[$subGroupId]['max_height'] = 0;
- $subGroupListData[$subGroupId]['label'] = $label;
- }
- if ($x < $subGroupListData[$subGroupId]['min_x']) {
- $subGroupListData[$subGroupId]['min_x'] = $x;
- }
- if ($y < $subGroupListData[$subGroupId]['min_y']) {
- $subGroupListData[$subGroupId]['min_y'] = $y;
- }
- $subGroupListData[$subGroupId]['max_width'] = ($column + 1) * ($width + $graph->xGap) - $subGroupListData[$subGroupId]['min_x'];
- $subGroupListData[$subGroupId]['max_height'] = ($originalRow + 1) * ($height + $graph->yGap) - $subGroupListData[$subGroupId]['min_y'];
- }
- $style = 'whiteSpace=wrap;rounded;dashed=1;strokeColor=blue;fillColor=none;strokeWidth=2;align=left;verticalAlign=bottom;';
- $subGroupDiffX = 5;
- foreach ($subGroupListData as $subGroupId => $data) {
- $x = $data['min_x'] - $subGroupDiffX;
- $y = $data['min_y'] - $subGroupDiffX;
- $width = $data['max_width'] + $subGroupDiffX * 2;
- $height = $data['max_height'] + $subGroupDiffX * 2;
- $label = '<h4>'.$data['label'].'</h4>';
- $vertexData = "var sg$subGroupId = graph.insertVertex(parent, null, '$label', $x, $y, $width, $height, '$style');";
- $subGroupList[] = $vertexData;
- }
- }
- // Create connections (arrows)
- if (!empty($simpleConnectionList)) {
- $connectionList = [];
- //$style = 'endArrow=classic;html=1;strokeWidth=4;exitX=1;exitY=0.5;entryX=0;entryY=0.5;';
- $style = '';
- foreach ($simpleConnectionList as $connection) {
- $from = $connection['from'];
- $to = $connection['to'];
- $vertexData = "var e1 = graph.insertEdge(parent, null, '', $from, $to, '$style')";
- $connectionList[] = $vertexData;
- }
- $tpl->assign('connections', $connectionList);
- }
- $tpl->assign('subgroup_list', $subGroupList);
- $tpl->assign('vertex_list', $graph->elementList);
- $graphHtml .= '<div id="graphContainer"></div>';
- return $graphHtml;
- }
- public static function parseColumnList($groupCourseList, $columnList, $width, &$graph, &$connections)
- {
- $graphHtml = '';
- $oldGroup = null;
- $newOrder = [];
- foreach ($columnList as $key => $subGroupList) {
- $newGroup = $subGroupList['group'];
- $label = $subGroupList['group_label'];
- $newOrder[$newGroup]['items'][] = $subGroupList;
- $newOrder[$newGroup]['label'] = $label;
- }
- foreach ($newOrder as $newGroup => $data) {
- $label = $data['label'];
- $subGroupList = $data['items'];
- if (!isset($graph->groupList[$newGroup])) {
- $graph->groupList[$newGroup]['min_x'] = 1000;
- $graph->groupList[$newGroup]['min_y'] = 1000;
- $graph->groupList[$newGroup]['max_width'] = 0;
- $graph->groupList[$newGroup]['max_height'] = 0;
- $graph->groupList[$newGroup]['label'] = $label;
- }
- $maxColumn = 0;
- $maxRow = 0;
- $minColumn = 100;
- $minRow = 100;
- foreach ($subGroupList as $item) {
- /** @var Vertex $vertex */
- foreach ($item['items'] as $vertex) {
- $column = $vertex->getAttribute('Column');
- $realRow = $vertex->getAttribute('Row');
- if ($column > $maxColumn) {
- $maxColumn = $column;
- }
- if ($realRow > $maxRow) {
- $maxRow = $realRow;
- }
- if ($column < $minColumn) {
- $minColumn = $column;
- }
- if ($realRow < $minRow) {
- $minRow = $realRow;
- }
- }
- }
- if (!empty($newGroup)) {
- $graphHtml .= '<div
- id ="group_'.$newGroup.'"
- class="group'.$newGroup.' group_class"
- style="display:grid;
- align-self: start;
- grid-gap: 10px;
- justify-items: stretch;
- align-items: start;
- align-content: start;
- justify-content: stretch;
- grid-area:'.$minRow.'/'.$minColumn.'/'.$maxRow.'/'.$maxColumn.'">'; //style="display:grid"
- }
- $addRow = 0;
- if (!empty($label)) {
- $graphHtml .= "<div class='my_label' style='grid-area:$minRow/$minColumn/$maxRow/$maxColumn'>$label</div>";
- $addRow = 1;
- }
- foreach ($subGroupList as $item) {
- $graphHtml .= self::parseVertexList(
- $groupCourseList,
- $item['items'],
- $addRow,
- $graph,
- $newGroup,
- $connections
- );
- }
- if (!empty($newGroup)) {
- $graphHtml .= '</div >';
- }
- }
- return $graphHtml;
- }
- /**
- * @param array $groupCourseList
- * @param array $vertexList
- * @param int $addRow
- * @param stdClass $graph
- * @param int $group
- * @param array $connections
- *
- * @return string
- */
- public static function parseVertexList($groupCourseList, $vertexList, $addRow = 0, &$graph, $group, &$connections)
- {
- if (empty($vertexList)) {
- return '';
- }
- $graphHtml = '';
- /** @var Vertex $vertex */
- foreach ($vertexList as $vertex) {
- $column = $vertex->getAttribute('Column');
- $realRow = $originalRow = $vertex->getAttribute('Row');
- if ($addRow) {
- $realRow = $realRow + $addRow;
- }
- $id = $vertex->getId();
- $area = "$realRow/$column";
- $graphHtml .= '<div
- id = "row_wrapper_'.$id.'"
- data= "'.$originalRow.'-'.$column.'"
- style="
- align-self: start;
- justify-content: stretch;
- grid-area:'.$area.'"
- >';
- $color = '';
- if (!empty($vertex->getAttribute('DefinedColor'))) {
- $color = $vertex->getAttribute('DefinedColor');
- }
- $content = '<div class="pull-left">'.$vertex->getAttribute('Notes').'</div>';
- $content .= '<div class="pull-right">['.$id.']</div>';
- $title = $vertex->getAttribute('graphviz.label');
- if (!empty($vertex->getAttribute('LinkedElement'))) {
- $title = Display::url($title, $vertex->getAttribute('LinkedElement'));
- }
- $originalRow--;
- $column--;
- //$title = "$originalRow / $column";
- $graphHtml .= Display::panel(
- $content,
- $title,
- null,
- null,
- null,
- "row_$id",
- $color
- );
- $panel = Display::panel(
- $content,
- $title,
- null,
- null,
- null,
- "row_$id",
- $color
- );
- $x = $column * $graph->blockWidth + $graph->xDiff;
- $y = $originalRow * $graph->blockHeight + $graph->yDiff;
- $width = $graph->blockWidth - $graph->xGap;
- $height = $graph->blockHeight - $graph->yGap;
- $style = 'text;html=1;strokeColor=green;fillColor=#ffffff;overflow=fill;rounded=0;align=left;';
- $panel = str_replace(["\n", "\r"], '', $panel);
- $vertexData = "var v$id = graph.insertVertex(parent, null, '".addslashes($panel)."', $x, $y, $width, $height, '$style');";
- $graph->elementList[$id] = $vertexData;
- $graph->allData[$id] = [
- 'x' => $x,
- 'y' => $y,
- 'width' => $width,
- 'height' => $height,
- 'row' => $originalRow,
- 'column' => $column,
- 'label' => $title,
- ];
- if ($x < $graph->groupList[$group]['min_x']) {
- $graph->groupList[$group]['min_x'] = $x;
- }
- if ($y < $graph->groupList[$group]['min_y']) {
- $graph->groupList[$group]['min_y'] = $y;
- }
- $graph->groupList[$group]['max_width'] = ($column + 1) * ($width + $graph->xGap) - $graph->groupList[$group]['min_x'];
- $graph->groupList[$group]['max_height'] = ($originalRow + 1) * ($height + ($graph->yGap)) - $graph->groupList[$group]['min_y'];
- $graphHtml .= '</div>';
- $arrow = $vertex->getAttribute('DrawArrowFrom');
- $found = false;
- if (!empty($arrow)) {
- $pos = strpos($arrow, 'SG');
- if ($pos === false) {
- $pos = strpos($arrow, 'G');
- if (is_numeric($pos)) {
- $parts = explode('G', $arrow);
- if (empty($parts[0]) && count($parts) == 2) {
- $groupArrow = $parts[1];
- $graphHtml .= self::createConnection(
- "group_$groupArrow",
- "row_$id",
- ['Left', 'Right']
- );
- $found = true;
- $connections[] = [
- 'from' => "g$groupArrow",
- 'to' => "v$id",
- ];
- }
- }
- } else {
- // Case is only one subgroup value example: SG1
- $parts = explode('SG', $arrow);
- if (empty($parts[0]) && count($parts) == 2) {
- $subGroupArrow = $parts[1];
- $graphHtml .= self::createConnection(
- "subgroup_$subGroupArrow",
- "row_$id",
- ['Left', 'Right']
- );
- $found = true;
- $connections[] = [
- 'from' => "sg$subGroupArrow",
- 'to' => "v$id",
- ];
- }
- }
- if ($found == false) {
- // case is connected to 2 subgroups: Example SG1-SG2
- $parts = explode('-', $arrow);
- if (count($parts) == 2 && !empty($parts[0]) && !empty($parts[1])) {
- $defaultArrow = ['Top', 'Bottom'];
- $firstPrefix = '';
- $firstId = '';
- $secondId = '';
- $secondPrefix = '';
- if (is_numeric($pos = strpos($parts[0], 'SG'))) {
- $firstPrefix = 'sg';
- $firstId = str_replace('SG', '', $parts[0]);
- }
- if (is_numeric($pos = strpos($parts[1], 'SG'))) {
- $secondPrefix = 'sg';
- $secondId = str_replace('SG', '', $parts[1]);
- }
- if (!empty($secondId) && !empty($firstId)) {
- $connections[] = [
- 'from' => $firstPrefix.$firstId,
- 'to' => $secondPrefix.$secondId,
- $defaultArrow,
- ];
- $found = true;
- }
- }
- }
- if ($found == false) {
- // case DrawArrowFrom is an integer
- $defaultArrow = ['Left', 'Right'];
- if (isset($groupCourseList[$column]) &&
- in_array($arrow, $groupCourseList[$column])
- ) {
- $defaultArrow = ['Top', 'Bottom'];
- }
- $graphHtml .= self::createConnection(
- "row_$arrow",
- "row_$id",
- $defaultArrow
- );
- $connections[] = [
- 'from' => "v$arrow",
- 'to' => "v$id",
- ];
- }
- }
- }
- return $graphHtml;
- }
- /**
- * @param array $groupCourseList list of groups and their courses
- * @param int $group
- * @param string $groupLabel
- * @param bool $showGroupLine
- * @param array $subGroupList
- * @param $widthGroup
- *
- * @return string
- */
- public static function parseSubGroups(
- $groupCourseList,
- $group,
- $groupLabel,
- $showGroupLine,
- $subGroupList,
- $widthGroup
- ) {
- $topValue = 90;
- $defaultSpace = 40;
- $leftGroup = $defaultSpace.'px';
- if ($group == 1) {
- $leftGroup = 0;
- }
- $groupIdTag = "group_$group";
- $borderLine = $showGroupLine === true ? 'border-style:solid;' : '';
- $graphHtml = '<div
- id="'.$groupIdTag.'" class="career_group"
- style=" '.$borderLine.' padding:15px; float:left; margin-left:'.$leftGroup.'; width:'.$widthGroup.'%">';
- if (!empty($groupLabel)) {
- $graphHtml .= '<h3>'.$groupLabel.'</h3>';
- }
- foreach ($subGroupList as $subGroup => $subGroupData) {
- $subGroupLabel = isset($subGroupData['label']) ? $subGroupData['label'] : '';
- $columnList = isset($subGroupData['columns']) ? $subGroupData['columns'] : [];
- if (empty($columnList)) {
- continue;
- }
- $line = '';
- if (!empty($subGroup)) {
- $line = 'border-style:solid;';
- }
- // padding:15px;
- $graphHtml .= '<div
- id="subgroup_'.$subGroup.'" class="career_subgroup"
- style="'.$line.' margin-bottom:20px; padding:15px; float:left; margin-left:0px; width:100%">';
- if (!empty($subGroupLabel)) {
- $graphHtml .= '<h3>'.$subGroupLabel.'</h3>';
- }
- foreach ($columnList as $column => $rows) {
- $leftColumn = $defaultSpace.'px';
- if ($column == 1) {
- $leftColumn = 0;
- }
- if (count($columnList) == 1) {
- $leftColumn = 0;
- }
- $widthColumn = 85 / count($columnList);
- $graphHtml .= '<div
- id="col_'.$column.'" class="career_column"
- style="padding:15px;float:left; margin-left:'.$leftColumn.'; width:'.$widthColumn.'%">';
- $maxRow = 0;
- foreach ($rows as $row => $vertex) {
- if ($row > $maxRow) {
- $maxRow = $row;
- }
- }
- $newRowList = [];
- $defaultSubGroup = -1;
- $subGroupCountList = [];
- for ($i = 0; $i < $maxRow; $i++) {
- /** @var Vertex $vertex */
- $vertex = isset($rows[$i + 1]) ? $rows[$i + 1] : null;
- if (!is_null($vertex)) {
- $subGroup = $vertex->getAttribute('SubGroup');
- if ($subGroup == '' || empty($subGroup)) {
- $defaultSubGroup = 0;
- } else {
- $defaultSubGroup = (int) $subGroup;
- }
- }
- $newRowList[$i + 1][$defaultSubGroup][] = $vertex;
- if (!isset($subGroupCountList[$defaultSubGroup])) {
- $subGroupCountList[$defaultSubGroup] = 1;
- } else {
- $subGroupCountList[$defaultSubGroup]++;
- }
- }
- $subGroup = null;
- $subGroupAdded = [];
- /** @var Vertex $vertex */
- foreach ($newRowList as $row => $subGroupList) {
- foreach ($subGroupList as $subGroup => $vertexList) {
- if (!empty($subGroup) && $subGroup != -1) {
- if (!isset($subGroupAdded[$subGroup])) {
- $subGroupAdded[$subGroup] = 1;
- } else {
- $subGroupAdded[$subGroup]++;
- }
- }
- foreach ($vertexList as $vertex) {
- if (is_null($vertex)) {
- $graphHtml .= '<div class="career_empty" style="height: 130px">';
- $graphHtml .= '</div>';
- continue;
- }
- $id = $vertex->getId();
- $rowId = "row_$row";
- $graphHtml .= '<div id = "row_'.$id.'" class="'.$rowId.' career_row" >';
- $color = '';
- if (!empty($vertex->getAttribute('DefinedColor'))) {
- $color = $vertex->getAttribute('DefinedColor');
- }
- $content = $vertex->getAttribute('Notes');
- $content .= '<div class="pull-right">['.$id.']</div>';
- $title = $vertex->getAttribute('graphviz.label');
- if (!empty($vertex->getAttribute('LinkedElement'))) {
- $title = Display::url($title, $vertex->getAttribute('LinkedElement'));
- }
- $graphHtml .= Display::panel(
- $content,
- $title,
- null,
- null,
- null,
- null,
- $color
- );
- $graphHtml .= '</div>';
- $arrow = $vertex->getAttribute('DrawArrowFrom');
- $found = false;
- if (!empty($arrow)) {
- $pos = strpos($arrow, 'SG');
- if ($pos === false) {
- $pos = strpos($arrow, 'G');
- if (is_numeric($pos)) {
- $parts = explode('G', $arrow);
- if (empty($parts[0]) && count($parts) == 2) {
- $groupArrow = $parts[1];
- $graphHtml .= self::createConnection(
- "group_$groupArrow",
- "row_$id",
- ['Left', 'Right']
- );
- $found = true;
- }
- }
- } else {
- $parts = explode('SG', $arrow);
- if (empty($parts[0]) && count($parts) == 2) {
- $subGroupArrow = $parts[1];
- $graphHtml .= self::createConnection(
- "subgroup_$subGroupArrow",
- "row_$id",
- ['Left', 'Right']
- );
- $found = true;
- }
- }
- }
- if ($found == false) {
- $defaultArrow = ['Left', 'Right'];
- if (isset($groupCourseList[$group]) &&
- in_array($arrow, $groupCourseList[$group])
- ) {
- $defaultArrow = ['Top', 'Bottom'];
- }
- $graphHtml .= self::createConnection(
- "row_$arrow",
- "row_$id",
- $defaultArrow
- );
- }
- }
- }
- }
- $graphHtml .= '</div>';
- }
- $graphHtml .= '</div>';
- }
- $graphHtml .= '</div>';
- return $graphHtml;
- }
- /**
- * @param string $source
- * @param string $target
- * @param array $anchor
- *
- * @return string
- */
- public static function createConnection($source, $target, $anchor = [])
- {
- if (empty($anchor)) {
- // Default
- $anchor = ['Bottom', 'Right'];
- }
- $anchor = implode('","', $anchor);
- $html = '<script>
- var connectorPaintStyle = {
- strokeWidth: 2,
- stroke: "#a31ed3",
- joinstyle: "round",
- outlineStroke: "white",
- outlineWidth: 2
- },
- // .. and this is the hover style.
- connectorHoverStyle = {
- strokeWidth: 3,
- stroke: "#216477",
- outlineWidth: 5,
- outlineStroke: "white"
- },
- endpointHoverStyle = {
- fill: "#E80CAF",
- stroke: "#E80CAF"
- };
- jsPlumb.ready(function() { ';
- $html .= 'jsPlumb.connect({
- source:"'.$source.'",
- target:"'.$target.'",
- endpoint:[ "Rectangle", { width:1, height:1 }],
- connector: ["Flowchart"],
- paintStyle: connectorPaintStyle,
- hoverPaintStyle: endpointHoverStyle,
- anchor: ["'.$anchor.'"],
- overlays: [
- [
- "Arrow",
- {
- location:1,
- width:11,
- length:11
- }
- ],
- ],
- });';
- $html .= '});</script>'.PHP_EOL;
- return $html;
- }
- }
|