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