ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilCourseObjectiveMaterials.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=0);
20
26{
27 private int $objective_id = 0;
28 private array $lms = [];
29 private int $lm_ref_id = 0;
30 private int $lm_obj_id = 0;
31 private string $type = '';
32
33 protected ilLogger $logger;
34 protected ilDBInterface $db;
36 protected ilTree $tree;
37
38 public function __construct(int $a_objective_id = 0)
39 {
40 global $DIC;
41
42 $this->objectDataCache = $DIC['ilObjDataCache'];
43 $this->tree = $DIC->repositoryTree();
44 $this->db = $DIC->database();
45 $this->objective_id = $a_objective_id;
46 $this->logger = $DIC->logger()->crs();
47 $this->__read();
48 }
49
50 public function cloneDependencies(int $a_new_objective, int $a_copy_id): void
51 {
52 $cwo = ilCopyWizardOptions::_getInstance($a_copy_id);
53 $mappings = $cwo->getMappings();
54 foreach ($this->getMaterials() as $material) {
55 // Copy action omit ?
56 if (!isset($mappings[$material['ref_id']]) || !$mappings[$material['ref_id']]) {
57 continue;
58 }
59 $material_ref_id = (int) $material['ref_id'];
60 $material_rbac_obj_id = $this->objectDataCache->lookupObjId($material_ref_id);
61 $material_obj_id = $material['obj_id'];
62 $new_ref_id = (int) $mappings[$material_ref_id];
63 $new_rbac_obj_id = $this->objectDataCache->lookupObjId($new_ref_id);
64
65 if ($new_rbac_obj_id === $material_rbac_obj_id) {
66 $this->logger->debug('Material has been linked. Keeping object id.');
67 $new_obj_id = $material_obj_id;
68 } elseif ($material['type'] == 'st' || $material['type'] == 'pg') {
69 // Chapter assignment
70 $new_material_info = $mappings[$material_ref_id . '_' . $material_obj_id] ?? '';
71 $new_material_arr = explode('_', $new_material_info);
72 if (!isset($new_material_arr[1]) || !$new_material_arr[1]) {
73 $this->logger->debug(': No mapping found for chapter: ' . $material_obj_id);
74 continue;
75 }
76 $new_obj_id = $new_material_arr[1];
77 $this->logger->debug('New material id is: ' . $new_obj_id);
78 } else {
79 $new_obj_id = $this->objectDataCache->lookupObjId((int) $mappings[$material_ref_id]);
80 }
81
82 $new_material = new ilCourseObjectiveMaterials($a_new_objective);
83 $new_material->setLMRefId($new_ref_id);
84 $new_material->setLMObjId($new_obj_id);
85 $new_material->setType($material['type']);
86 $new_material->add();
87 }
88 }
89
93 public static function _getAssignedMaterials(int $a_objective_id): array
94 {
95 global $DIC;
96
97 $ilDB = $DIC->database();
98 $query = "SELECT DISTINCT(ref_id) ref_id FROM crs_objective_lm " .
99 "WHERE objective_id = " . $ilDB->quote($a_objective_id, 'integer');
100 $res = $ilDB->query($query);
101 $ref_ids = [];
102 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
103 $ref_ids[] = (int) $row->ref_id;
104 }
105 return $ref_ids;
106 }
107
112 public static function _getAssignableMaterials(int $a_container_id): array
113 {
114 global $DIC;
115
116 $tree = $DIC->repositoryTree();
117 $container_obj_id = ilObject::_lookupObjId($a_container_id);
118
119 $all_materials = $tree->getSubTree($tree->getNodeData($a_container_id), true);
120 $all_materials = ilArrayUtil::sortArray($all_materials, 'title', 'asc');
121
122 // Filter
123 $assignable = [];
124 foreach ($all_materials as $material) {
125 switch ($material['type']) {
126 case 'tst':
127 $type = ilLOTestAssignments::getInstance($container_obj_id)->getTypeByTest($material['child']);
129 break;
130 }
131
132 $assignable[] = $material;
133 break;
134
135 case 'crs':
136 case 'rolf':
137 case 'itgr':
138 break;
139
140 default:
141 $assignable[] = $material;
142 break;
143 }
144 }
145 return $assignable;
146 }
147
151 public static function _getAllAssignedMaterials(int $a_container_id): array
152 {
153 global $DIC;
154
155 $ilDB = $DIC->database();
156 $query = "SELECT DISTINCT(com.ref_id) ref_id FROM crs_objectives co " .
157 "JOIN crs_objective_lm com ON co.objective_id = com.objective_id " .
158 "JOIN object_reference obr ON com.ref_id = obr.ref_id " .
159 "JOIN object_data obd ON obr.obj_id = obd.obj_id " .
160 "WHERE co.crs_id = " . $ilDB->quote($a_container_id, 'integer') . " " .
161 "ORDER BY obd.title ";
162
163 $res = $ilDB->query($query);
164 $ref_ids = [];
165 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
166 $ref_ids[] = (int) $row->ref_id;
167 }
168 return $ref_ids;
169 }
170
171 public function getMaterials(): array
172 {
173 return $this->lms;
174 }
175
176 public function getChapters(): array
177 {
178 $chapters = [];
179 foreach ($this->lms as $lm_data) {
180 if ($lm_data['type'] == 'st') {
181 $chapters[] = $lm_data;
182 }
183 if ($lm_data['type'] == 'pg') {
184 $chapters[] = $lm_data;
185 }
186 }
187 return $chapters;
188 }
189
190 public function getLM(int $lm_id): array
191 {
192 if ($this->lms[$lm_id]) {
193 return $this->lms[$lm_id];
194 } else {
195 return [];
196 }
197 }
198
199 public function getObjectiveId(): int
200 {
201 return $this->objective_id;
202 }
203
204 public function setLMRefId(int $a_ref_id): void
205 {
206 $this->lm_ref_id = $a_ref_id;
207 }
208
209 public function getLMRefId(): int
210 {
211 return $this->lm_ref_id;
212 }
213
214 public function setLMObjId(int $a_obj_id): void
215 {
216 $this->lm_obj_id = $a_obj_id;
217 }
218
219 public function getLMObjId(): int
220 {
221 return $this->lm_obj_id;
222 }
223
224 public function setType(string $a_type): void
225 {
226 $this->type = $a_type;
227 }
228
229 public function getType(): string
230 {
231 return $this->type;
232 }
233
237 public function isAssigned(int $a_ref_id, bool $a_get_id = false)
238 {
239 $query = "SELECT * FROM crs_objective_lm " .
240 "WHERE ref_id = " . $this->db->quote($a_ref_id, 'integer') . " " .
241 "AND objective_id = " . $this->db->quote($this->getObjectiveId(), 'integer') . " " .
242 "AND type != 'st' AND type != 'pg' ";
243 $res = $this->db->query($query);
244 if (!$a_get_id) {
245 return (bool) $res->numRows();
246 } else {
247 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
248 return (int) $row->lm_ass_id;
249 }
250 return 0;
251 }
252 }
253
254 public function isChapterAssigned(int $a_ref_id, int $a_obj_id): bool
255 {
256 $query = "SELECT * FROM crs_objective_lm " .
257 "WHERE ref_id = " . $this->db->quote($a_ref_id, 'integer') . " " .
258 "AND obj_id = " . $this->db->quote($a_obj_id, 'integer') . " " .
259 "AND objective_id = " . $this->db->quote($this->getObjectiveId(), 'integer') . " " .
260 "AND (type = 'st' OR type = 'pg')";
261 $res = $this->db->query($query);
262 return (bool) $res->numRows();
263 }
264
265 public function checkExists(): bool
266 {
267 if ($this->getLMObjId()) {
268 $query = "SELECT * FROM crs_objective_lm " .
269 "WHERE objective_id = " . $this->db->quote($this->getObjectiveId(), 'integer') . " " .
270 "AND ref_id = " . $this->db->quote($this->getLMRefId(), 'integer') . " " .
271 "AND obj_id = " . $this->db->quote($this->getLMObjId(), 'integer') . " ";
272 } else {
273 $query = "SELECT * FROM crs_objective_lm " .
274 "WHERE objective_id = " . $this->db->quote($this->getObjectiveId(), 'integer') . " " .
275 "AND ref_id = " . $this->db->quote($this->getLMRefId(), 'integer') . " ";
276 }
277
278 $res = $this->db->query($query);
279 return (bool) $res->numRows();
280 }
281
282 public function add(): int
283 {
284 $next_id = $this->db->nextId('crs_objective_lm');
285 $query = "INSERT INTO crs_objective_lm (lm_ass_id,objective_id,ref_id,obj_id,type) " .
286 "VALUES( " .
287 $this->db->quote($next_id, 'integer') . ", " .
288 $this->db->quote($this->getObjectiveId(), 'integer') . ", " .
289 $this->db->quote($this->getLMRefId(), 'integer') . ", " .
290 $this->db->quote($this->getLMObjId(), 'integer') . ", " .
291 $this->db->quote($this->getType(), 'text') .
292 ")";
293 $res = $this->db->manipulate($query);
294 return $next_id;
295 }
296
297 public function deleteMaterial(int $ref_id, int $obj_id): bool
298 {
299 $query = "DELETE FROM crs_objective_lm " .
300 "WHERE objective_id = " . $this->db->quote($this->getObjectiveId(), 'integer') . " " .
301 "AND ref_id = " . $this->db->quote($ref_id, 'integer') . " " .
302 "AND obj_id = " . $this->db->quote($obj_id, 'integer');
303 $this->db->manipulate($query);
304 return true;
305 }
306
307 public function isMaterialAssigned(int $ref_id, int $obj_id): bool
308 {
309 $query = "SELECT * FROM crs_objective_lm " .
310 "WHERE ref_id = " . $this->db->quote($ref_id, 'integer') . " " .
311 "AND obj_id = " . $this->db->quote($obj_id, 'integer') . " " .
312 "AND objective_id = " . $this->db->quote($this->getObjectiveId(), 'integer') . " ";
313 $res = $this->db->query($query);
314 return (bool) $res->numRows();
315 }
316
317 public function delete(int $lm_id): bool
318 {
319 if (!$lm_id) {
320 return false;
321 }
322
323 $query = "DELETE FROM crs_objective_lm " .
324 "WHERE lm_ass_id = " . $this->db->quote($lm_id, 'integer') . " ";
325 $res = $this->db->manipulate($query);
326 return true;
327 }
328
329 public function deleteAll(): bool
330 {
331 $query = "DELETE FROM crs_objective_lm " .
332 "WHERE objective_id = " . $this->db->quote($this->getObjectiveId(), 'integer') . " ";
333 $res = $this->db->manipulate($query);
334 return true;
335 }
336
337 public function writePosition(int $a_ass_id, int $a_position): void
338 {
339 $query = "UPDATE crs_objective_lm " .
340 "SET position = " . $this->db->quote((string) $a_position, 'integer') . " " .
341 "WHERE objective_id = " . $this->db->quote($this->getObjectiveId(), 'integer') . " " .
342 "AND lm_ass_id = " . $this->db->quote($a_ass_id, "integer");
343 $this->db->manipulate($query);
344 }
345
346 public function __read(): bool
347 {
349 $container_ref_id = current($container_ref_ids);
350
351 $this->lms = array();
352 $query = "SELECT position,lm_ass_id,lm.ref_id,lm.obj_id,lm.type FROM crs_objective_lm lm " .
353 "JOIN object_reference obr ON lm.ref_id = obr.ref_id " .
354 "JOIN object_data obd ON obr.obj_id = obd.obj_id " .
355 "LEFT JOIN lm_data lmd ON lmd.obj_id = lm.obj_id " .
356 "WHERE objective_id = " . $this->db->quote($this->getObjectiveId(), 'integer') . " " .
357 "ORDER BY position,obd.title,lmd.title";
358
359 $res = $this->db->query($query);
360 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
361 if (
362 !$this->tree->isInTree((int) $row->ref_id) ||
363 !$this->tree->isGrandChild($container_ref_id, (int) $row->ref_id)
364 ) {
365 $this->delete((int) $row->lm_ass_id);
366 continue;
367 }
368 if (
369 $row->obj_id > 0 &&
370 ($row->type == 'pg' || $row->type == 'st') &&
371 !ilLMObject::_exists((int) $row->obj_id)
372 ) {
373 continue;
374 }
375 $lm['ref_id'] = (int) $row->ref_id;
376 $lm['obj_id'] = (int) $row->obj_id;
377 $lm['type'] = (string) $row->type;
378 $lm['lm_ass_id'] = (int) $row->lm_ass_id;
379 $lm['position'] = (int) $row->position;
380 $this->lms[(int) $row->lm_ass_id] = $lm;
381 }
382 return true;
383 }
384
385 public function toXml(ilXmlWriter $writer): bool
386 {
387 foreach ($this->getMaterials() as $material) {
388 $writer->xmlElement(
389 'Material',
390 array(
391 'refId' => $material['ref_id'],
392 'objId' => $material['obj_id'],
393 'type' => $material['type'],
394 'position' => $material['position']
395 )
396 );
397 }
398 return true;
399 }
400}
static sortArray(array $array, string $a_array_sortby_key, string $a_array_sortorder="asc", bool $a_numeric=false, bool $a_keep_keys=false)
static _getInstance(int $a_copy_id)
class ilCourseObjectiveMaterials
writePosition(int $a_ass_id, int $a_position)
static _getAssignableMaterials(int $a_container_id)
Get an array of course material ids that can be assigned to learning objectives No tst,...
cloneDependencies(int $a_new_objective, int $a_copy_id)
isAssigned(int $a_ref_id, bool $a_get_id=false)
static _getAssignedMaterials(int $a_objective_id)
static _getAllAssignedMaterials(int $a_container_id)
isChapterAssigned(int $a_ref_id, int $a_obj_id)
static _lookupContainerIdByObjectiveId(int $a_objective_id)
static _exists(int $a_id)
checks wether a lm content object with specified id exists or not
static getInstance(int $a_container_id)
Component logger with individual log levels by component id.
class ilObjectDataCache
static _getAllReferences(int $id)
get all reference ids for object ID
static _lookupObjId(int $ref_id)
Tree class data representation in hierachical trees using the Nested Set Model with Gaps by Joe Celco...
getNodeData(int $a_node_id, ?int $a_tree_pk=null)
get all information of a node.
getSubTree(array $a_node, bool $a_with_data=true, array $a_type=[])
get all nodes in the subtree under specified node
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
xmlElement(string $tag, $attrs=null, $data=null, $encode=true, $escape=true)
Writes a basic element (no children, just textual content)
Interface ilDBInterface.
$ref_id
Definition: ltiauth.php:66
$res
Definition: ltiservices.php:69
global $DIC
Definition: shib_login.php:26