ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.AssignMaterialsTable.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\Skill\Table;
22
23use ILIAS\Data;
24use ILIAS\UI;
25use Psr\Http\Message\ServerRequestInterface;
28
33{
34 protected \ilCtrl $ctrl;
35 protected \ilLanguage $lng;
36 protected \ilObjUser $user;
37 protected \ilWorkspaceTree $ws_tree;
38 protected \ilWorkspaceAccessHandler $ws_access;
39 protected UI\Factory $ui_fac;
40 protected ServerRequestInterface $request;
41 protected Data\Factory $df;
42 protected \ilSkillTreeRepository $tree_repo;
45 protected int $top_skill_id = 0;
46 protected int $tref_id = 0;
47 protected int $basic_skill_id = 0;
48
49 public function __construct(int $top_skill_id, int $tref_id, int $basic_skill_id)
50 {
51 global $DIC;
52
53 $this->ctrl = $DIC->ctrl();
54 $this->lng = $DIC->language();
55 $this->user = $DIC->user();
56 $this->ui_fac = $DIC->ui()->factory();
57 $this->request = $DIC->http()->request();
58 $this->df = new Data\Factory();
59 $this->ws_tree = new \ilWorkspaceTree($this->user->getId());
60 if (!$this->ws_tree->readRootId()) {
61 $this->ws_tree->createTreeForUser($this->user->getId());
62 }
63 $this->ws_access = new \ilWorkspaceAccessHandler();
64
65 $this->top_skill_id = $top_skill_id;
66 $this->tref_id = $tref_id;
67 $this->basic_skill_id = $basic_skill_id;
68
69 $this->tree_repo = $DIC->skills()->internal()->repo()->getTreeRepo();
70 $tree_id = $this->tree_repo->getTreeIdForNodeId($this->basic_skill_id);
71 $this->node_manager = $DIC->skills()->internal()->manager()->getTreeNodeManager($tree_id);
72 $this->assigned_material_manager = $DIC->skills()->internal()->manager()->getAssignedMaterialManager();
73 }
74
75 public function getComponent(): UI\Component\Table\Data
76 {
77 $columns = $this->getColumns();
78 $actions = $this->getActions();
79 $data_retrieval = $this->getDataRetrieval();
80
81 $title = $this->node_manager->getWrittenPath($this->basic_skill_id);
82 $table = $this->ui_fac->table()
83 ->data($data_retrieval, $title, $columns)
84 ->withId(
85 self::class . "_" .
86 $this->top_skill_id . "_" .
87 $this->tref_id . "_" .
88 $this->basic_skill_id
89 )
90 ->withActions($actions)
91 ->withRequest($this->request);
92
93 return $table;
94 }
95
96 protected function getColumns(): array
97 {
98 $columns = [
99 "title" => $this->ui_fac->table()->column()->text($this->lng->txt("skmg_skill_level"))
100 ->withIsSortable(false),
101 "description" => $this->ui_fac->table()->column()->text($this->lng->txt("description"))
102 ->withIsSortable(false),
103 "resources" => $this->ui_fac->table()->column()->linkListing($this->lng->txt("skmg_materials"))
104 ->withIsSortable(false)
105 ];
106
107 return $columns;
108 }
109
110 protected function getActions(): array
111 {
112 $query_params_namespace = ["skl_assign_materials_table"];
113
114 $uri_assign = $this->df->uri(
115 ILIAS_HTTP_PATH . "/" . $this->ctrl->getLinkTargetByClass(
116 "ilpersonalskillsgui",
117 "assignMaterial"
118 )
119 );
120 $url_builder_assign = new UI\URLBuilder($uri_assign);
121 list($url_builder_assign, $action_parameter_token_assign, $row_id_token_assign) =
122 $url_builder_assign->acquireParameters(
123 $query_params_namespace,
124 "action",
125 "level_ids"
126 );
127
128 $actions = [
129 "assign" => $this->ui_fac->table()->action()->single(
130 $this->lng->txt("skmg_assign_materials"),
131 $url_builder_assign->withParameter($action_parameter_token_assign, "assignMaterials"),
132 $row_id_token_assign
133 )
134 ];
135
136 foreach ($this->assigned_material_manager->getAllAssignedMaterialsForSkill(
137 $this->user->getId(),
138 $this->basic_skill_id,
139 $this->tref_id,
140 ) as $material) {
141 $obj_id = $this->ws_tree->lookupObjectId($material->getWorkspaceId());
142
143 $uri_remove = $this->df->uri(
144 ILIAS_HTTP_PATH . "/" . $this->ctrl->getLinkTargetByClass(
145 "ilpersonalskillsgui",
146 "removeMaterial"
147 )
148 );
149 $url_builder_remove = new UI\URLBuilder($uri_remove);
150 list($url_builder_remove, $action_parameter_token_remove, $row_id_token_remove, $wsp_token_remove) =
151 $url_builder_remove->acquireParameters(
152 $query_params_namespace,
153 "action",
154 "level_ids",
155 "wsp_id"
156 );
157 $url_builder_remove = $url_builder_remove->withParameter($wsp_token_remove, (string) $material->getWorkspaceId());
158
159 $actions["remove_" . $material->getLevelId() . "_" . $material->getWorkspaceId()] =
160 $this->ui_fac->table()->action()->single(
161 $this->lng->txt("skmg_remove") . " '" . \ilObject::_lookupTitle($obj_id) . "'",
162 $url_builder_remove->withParameter($action_parameter_token_remove, "removeMaterial"),
163 $row_id_token_remove
164 );
165 }
166
167 return $actions;
168 }
169
170 protected function getDataRetrieval(): UI\Component\Table\DataRetrieval
171 {
172 $data_retrieval = new class (
180 ) implements UI\Component\Table\DataRetrieval {
181 use TableRecords;
182
183 public function __construct(
184 protected int $basic_skill_id,
185 protected int $tref_id,
186 protected \ilObjUser $user,
187 protected \ilWorkspaceTree $ws_tree,
189 protected UI\Factory $ui_fac,
191 ) {
192 }
193
194 public function getRows(
195 UI\Component\Table\DataRowBuilder $row_builder,
196 array $visible_column_ids,
198 Data\Order $order,
199 ?array $filter_data,
200 ?array $additional_parameters
201 ): \Generator {
202 $records = $this->getRecords($range);
203 foreach ($records as $idx => $record) {
204 $row_id = $record["id"];
205 $res_ids = $record["res_ids"];
206
207 $data_row = $row_builder->buildDataRow((string) $row_id, $record);
208 foreach ($this->assigned_material_manager->getAllAssignedMaterialsForSkill(
209 $this->user->getId(),
210 $this->basic_skill_id,
211 $this->tref_id,
212 ) as $material) {
213 if (!in_array($material->getWorkspaceId(), $res_ids) || $row_id != $material->getLevelId()) {
214 $data_row = $data_row->withDisabledAction(
215 "remove_" . $material->getLevelId() . "_" . $material->getWorkspaceId()
216 );
217 }
218 }
219
220 yield $data_row;
221 }
222 }
223
224 public function getTotalRowCount(
225 ?array $filter_data,
226 ?array $additional_parameters
227 ): ?int {
228 return count($this->getRecords());
229 }
230
231 protected function getRecords(?Data\Range $range = null): array
232 {
233 $skill = \ilSkillTreeNodeFactory::getInstance($this->basic_skill_id);
234 $records = [];
235 $i = 0;
236 foreach ($skill->getLevelData() as $level) {
237 $records[$i]["id"] = $level["id"];
238 $records[$i]["title"] = $level["title"];
239 $records[$i]["description"] = $level["description"];
240
241 $materials = $this->assigned_material_manager->getAssignedMaterials(
242 $this->user->getId(),
243 $this->tref_id,
244 (int) $level["id"]
245 );
246 $wsp_ids = [];
247 $obj_links = [];
248 foreach ($materials as $m) {
249 $wsp_ids[] = $m->getWorkspaceId();
250 $obj_id = $this->ws_tree->lookupObjectId($m->getWorkspaceId());
251 $obj_links[] = $this->ui_fac->link()->standard(
252 \ilObject::_lookupTitle($obj_id),
253 $this->ws_access->getGotoLink($m->getWorkspaceId(), $obj_id)
254 );
255 }
256 $records[$i]["res_ids"] = $wsp_ids;
257 $records[$i]["resources"] = $this->ui_fac->listing()->unordered($obj_links);
258
259 $i++;
260 }
261
262 if ($range) {
263 $records = $this->limitRecords($records, $range);
264 }
265
266 return $records;
267 }
268 };
269
270 return $data_retrieval;
271 }
272}
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
Both the subject and the direction need to be specified when expressing an order.
Definition: Order.php:29
A simple class to express a naive range of whole positive numbers.
Definition: Range.php:29
__construct(int $top_skill_id, int $tref_id, int $basic_skill_id)
Personal AssignedMaterialManager $assigned_material_manager
Definition: UI.php:24
User class.
static _lookupTitle(int $obj_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: shib_login.php:26