ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilContainerStartObjects.php
Go to the documentation of this file.
1 <?php
2 
25 {
26  protected ilTree $tree;
27  protected ilDBInterface $db;
29  protected ilLogger $log;
30  protected int $ref_id;
31  protected int $obj_id;
32  protected array $start_objs = [];
33 
34  public function __construct(
35  int $a_object_ref_id,
36  int $a_object_id
37  ) {
38  global $DIC;
39 
40  $this->tree = $DIC->repositoryTree();
41  $this->db = $DIC->database();
42  $this->obj_data_cache = $DIC["ilObjDataCache"];
43  $this->log = $DIC["ilLog"];
44  $this->setRefId($a_object_ref_id);
45  $this->setObjId($a_object_id);
46 
47  $this->read();
48  }
49 
50  protected function setObjId(int $a_id): void
51  {
52  $this->obj_id = $a_id;
53  }
54 
55  public function getObjId(): int
56  {
57  return $this->obj_id;
58  }
59 
60  protected function setRefId(int $a_ref_id): void
61  {
62  $this->ref_id = $a_ref_id;
63  }
64 
65  public function getRefId(): int
66  {
67  return $this->ref_id;
68  }
69 
70  public function getStartObjects(): array
71  {
72  return $this->start_objs ?: [];
73  }
74 
75  protected function read(): void
76  {
77  $tree = $this->tree;
78  $ilDB = $this->db;
79 
80  $this->start_objs = [];
81 
82  $query = "SELECT * FROM crs_start" .
83  " WHERE crs_id = " . $ilDB->quote($this->getObjId(), 'integer') .
84  " ORDER BY pos, crs_start_id";
85  $res = $ilDB->query($query);
86  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
87  if ($tree->isInTree($row->item_ref_id)) {
88  $this->start_objs[$row->crs_start_id]['item_ref_id'] = (int) $row->item_ref_id;
89  } else {
90  $this->delete((int) $row->item_ref_id);
91  }
92  }
93  }
94 
95  public function delete(int $a_crs_start_id): void
96  {
97  $ilDB = $this->db;
98 
99  $query = "DELETE FROM crs_start" .
100  " WHERE crs_start_id = " . $ilDB->quote($a_crs_start_id, 'integer') .
101  " AND crs_id = " . $ilDB->quote($this->getObjId(), 'integer');
102  $ilDB->manipulate($query);
103  }
104 
105  public function deleteItem(int $a_item_ref_id): void
106  {
107  $ilDB = $this->db;
108 
109  $query = "DELETE FROM crs_start" .
110  " WHERE crs_id = " . $ilDB->quote($this->getObjId(), 'integer') .
111  " AND item_ref_id = " . $ilDB->quote($a_item_ref_id, 'integer');
112  $ilDB->manipulate($query);
113  }
114 
115  public function exists(int $a_item_ref_id): bool
116  {
117  $ilDB = $this->db;
118 
119  $query = "SELECT * FROM crs_start" .
120  " WHERE crs_id = " . $ilDB->quote($this->getObjId(), 'integer') .
121  " AND item_ref_id = " . $ilDB->quote($a_item_ref_id, 'integer');
122  $res = $ilDB->query($query);
123 
124  return (bool) $res->numRows();
125  }
126 
127  public function add(int $a_item_ref_id): bool
128  {
129  $ilDB = $this->db;
130 
131  if ($a_item_ref_id) {
132  $max_pos = $ilDB->query("SELECT max(pos) pos FROM crs_start" .
133  " WHERE crs_id = " . $ilDB->quote($this->getObjId(), "integer"));
134  $max_pos = $ilDB->fetchAssoc($max_pos);
135  $max_pos = ((int) $max_pos["pos"]) + 10;
136 
137  $next_id = $ilDB->nextId('crs_start');
138  $query = "INSERT INTO crs_start" .
139  " (crs_start_id,crs_id,item_ref_id,pos)" .
140  " VALUES" .
141  " (" . $ilDB->quote($next_id, 'integer') .
142  ", " . $ilDB->quote($this->getObjId(), 'integer') .
143  ", " . $ilDB->quote($a_item_ref_id, 'integer') .
144  ", " . $ilDB->quote($max_pos, 'integer') .
145  ")";
146  $ilDB->manipulate($query);
147  return true;
148  }
149  return false;
150  }
151 
152  public function setObjectPos(
153  int $a_start_id,
154  int $a_pos
155  ): void {
156  $ilDB = $this->db;
157 
158  if (!$a_start_id || !$a_pos) {
159  return;
160  }
161 
162  $ilDB->manipulate("UPDATE crs_start" .
163  " SET pos = " . $ilDB->quote($a_pos, "integer") .
164  " WHERE crs_id = " . $ilDB->quote($this->getObjId(), 'integer') .
165  " AND crs_start_id = " . $ilDB->quote($a_start_id, 'integer'));
166  }
167 
168  public function getPossibleStarters(): array
169  {
170  $poss_items = [];
171  foreach (ilObjectActivation::getItems($this->getRefId(), false) as $node) {
172  switch ($node['type']) {
173  case 'lm':
174  case 'sahs':
175  case 'svy':
176  case 'copa':
177  case 'tst':
178  $poss_items[] = $node['ref_id'];
179  break;
180  }
181  }
182  return $poss_items;
183  }
184 
185  public function allFullfilled(int $a_user_id): bool
186  {
187  foreach ($this->getStartObjects() as $item) {
188  if (!$this->isFullfilled($a_user_id, $item['item_ref_id'])) {
189  return false;
190  }
191  }
192  return true;
193  }
194 
195  public function isFullfilled(int $a_user_id, int $a_item_id): bool
196  {
197  $ilObjDataCache = $this->obj_data_cache;
198 
199  $obj_id = $ilObjDataCache->lookupObjId($a_item_id);
200  $type = $ilObjDataCache->lookupType($obj_id);
201 
202  switch ($type) {
203  case 'tst':
204  if (!ilObjTestAccess::checkCondition($obj_id, 'finished', '', $a_user_id)) { // #14000
205  return false;
206  }
207  break;
208 
209  case 'svy':
210 
211  if (!ilObjSurveyAccess::_lookupFinished($obj_id, $a_user_id)) {
212  return false;
213  }
214  break;
215 
216  case 'copa':
217  case 'sahs':
218  if (!ilLPStatus::_hasUserCompleted($obj_id, $a_user_id)) {
219  return false;
220  }
221  break;
222 
223  default:
224  $lm_continue = new ilCourseLMHistory($this->getRefId(), $a_user_id);
225  $continue_data = $lm_continue->getLMHistory();
226  if (!isset($continue_data[$a_item_id])) {
227  return false;
228  }
229  break;
230  }
231 
232  return true;
233  }
234 
235  public function cloneDependencies(
236  int $a_target_id,
237  int $a_copy_id
238  ): void {
239  $ilObjDataCache = $this->obj_data_cache;
240  $ilLog = $this->log;
241 
242  $ilLog->write(__METHOD__ . ': Begin course start objects...');
243 
244  $new_obj_id = $ilObjDataCache->lookupObjId($a_target_id);
245  $start = new self($a_target_id, $new_obj_id);
246 
247  $cwo = ilCopyWizardOptions::_getInstance($a_copy_id);
248  $mappings = $cwo->getMappings();
249  foreach ($this->getStartObjects() as $data) {
250  $item_ref_id = $data['item_ref_id'];
251  if (isset($mappings[$item_ref_id]) && $mappings[$item_ref_id]) {
252  $ilLog->write(__METHOD__ . ': Clone start object nr. ' . $item_ref_id);
253  $start->add($mappings[$item_ref_id]);
254  } else {
255  $ilLog->write(__METHOD__ . ': No mapping found for start object nr. ' . $item_ref_id);
256  }
257  }
258  $ilLog->write(__METHOD__ . ': ... end course start objects');
259  }
260 
261  public static function isStartObject(
262  int $a_container_id,
263  int $a_item_ref_id
264  ): bool {
265  global $DIC;
266 
267  $ilDB = $DIC->database();
268 
269  $query = 'SELECT crs_start_id FROM crs_start ' .
270  'WHERE crs_id = ' . $ilDB->quote($a_container_id, 'integer') . ' ' .
271  'AND item_ref_id = ' . $ilDB->quote($a_item_ref_id, 'integer');
272  $res = $ilDB->query($query);
273 
274  return $res->numRows() >= 1;
275  }
276 }
static _hasUserCompleted(int $a_obj_id, int $a_user_id)
Lookup user object completion.
$res
Definition: ltiservices.php:69
$type
isInTree(?int $a_node_id)
get all information of a node.
write(string $a_message, $a_level=ilLogLevel::INFO)
write log message
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _lookupFinished(int $a_obj_id, int $a_user_id=0)
get finished status
isFullfilled(int $a_user_id, int $a_item_id)
static getItems(int $parent_id, bool $with_list_data=true)
Get sub item data.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: feed.php:28
setObjectPos(int $a_start_id, int $a_pos)
$query
static isStartObject(int $a_container_id, int $a_item_ref_id)
static checkCondition(int $a_trigger_obj_id, string $a_operator, string $a_value, int $a_usr_id)
check condition
cloneDependencies(int $a_target_id, int $a_copy_id)
static _getInstance(int $a_copy_id)
__construct(int $a_object_ref_id, int $a_object_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...