ILIAS  release_8 Revision v8.23
class.ilLPStatusCollectionManual.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=0);
4 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
5 
11 {
12  public static function _getInProgress(int $a_obj_id): array
13  {
14  $status_info = ilLPStatusWrapper::_getStatusInfo($a_obj_id);
15 
16  // find any completed item
17  $users = array();
18  if (isset($status_info['completed'])) {
19  foreach ($status_info['completed'] as $in_progress) {
20  $users = array_merge($users, $in_progress);
21  }
22  $users = array_unique($users);
23  }
24  // remove all users which have completed ALL items
25  return array_diff($users, ilLPStatusWrapper::_getCompleted($a_obj_id));
26  }
27 
28  public static function _getCompleted(int $a_obj_id): array
29  {
30  $status_info = ilLPStatusWrapper::_getStatusInfo($a_obj_id);
31 
32  $counter = 0;
33  $users = array();
34  foreach ($status_info['items'] as $item_id) {
35  $tmp_users = $status_info['completed'][$item_id];
36 
37  if (!$counter++) {
38  $users = $tmp_users;
39  } else {
40  $users = array_intersect($users, $tmp_users);
41  }
42  }
43  return array_unique($users);
44  }
45 
46  public static function _getStatusInfo(int $a_obj_id): array
47  {
48  $status_info = array();
49 
50  $olp = ilObjectLP::getInstance($a_obj_id);
51  $collection = $olp->getCollectionInstance();
52  if ($collection) {
53  // @todo check if obj_id can be removed
54  $status_info["items"] = $collection->getItems($a_obj_id);
55 
56  foreach ($status_info["items"] as $item_id) {
57  $status_info["completed"][$item_id] = array();
58  }
59 
60  $ref_ids = ilObject::_getAllReferences($a_obj_id);
61  $ref_id = end($ref_ids);
62  $possible_items = $collection->getPossibleItems($ref_id);
63  $chapter_ids = array_intersect(
64  array_keys($possible_items),
65  $status_info["items"]
66  );
67 
68  // fix order (adapt from possible items)
69  $status_info["items"] = $chapter_ids;
70 
71  if ($chapter_ids) {
72  $status = self::_getObjectStatus($a_obj_id);
73 
74  foreach ($chapter_ids as $item_id) {
75  $status_info["item_titles"][$item_id] = $possible_items[$item_id]["title"];
76 
77  if (isset($status[$item_id])) {
78  foreach ($status[$item_id] as $user_id => $user_status) {
79  if ($user_status) {
80  $status_info["completed"][$item_id][] = $user_id;
81  }
82  }
83  }
84  }
85  }
86  }
87  return $status_info;
88  }
89 
90  public function determineStatus(
91  int $a_obj_id,
92  int $a_usr_id,
93  object $a_obj = null
94  ): int {
95  $info = self::_getStatusInfo($a_obj_id);
96 
97  if (isset($info["completed"])) {
98  $completed = true;
99  $in_progress = false;
100  foreach ($info["completed"] as $user_ids) {
101  // has completed at least 1 item
102  if (in_array($a_usr_id, $user_ids)) {
103  $in_progress = true;
104  } // must have completed all items to complete collection
105  else {
106  $completed = false;
107  }
108  }
109  if ($completed) {
110  return self::LP_STATUS_COMPLETED_NUM;
111  }
112  if ($in_progress) {
113  return self::LP_STATUS_IN_PROGRESS_NUM;
114  }
115  }
116 
117  return self::LP_STATUS_NOT_ATTEMPTED_NUM;
118  }
119 
120  public static function _getObjectStatus(
121  $a_obj_id,
122  $a_user_id = null
123  ): array {
124  global $DIC;
125 
126  $ilDB = $DIC['ilDB'];
127 
128  $res = array();
129 
130  $sql = "SELECT subitem_id, completed, usr_id, last_change" .
131  " FROM ut_lp_coll_manual" .
132  " WHERE obj_id = " . $ilDB->quote($a_obj_id, "integer");
133  if ($a_user_id) {
134  $sql .= " AND usr_id = " . $ilDB->quote($a_user_id, "integer");
135  }
136  $set = $ilDB->query($sql);
137  while ($row = $ilDB->fetchAssoc($set)) {
138  if (!$a_user_id) {
139  $res[(int) $row["subitem_id"]][(int) $row["usr_id"]] = (int) $row["completed"];
140  } else {
141  $res[(int) $row["subitem_id"]] = array((int) $row["completed"],
142  $row["last_change"]
143  );
144  }
145  }
146  return $res;
147  }
148 
149  public static function _setObjectStatus(
150  int $a_obj_id,
151  int $a_user_id,
152  array $a_completed = null
153  ): void {
154  global $DIC;
155 
156  $ilDB = $DIC['ilDB'];
157 
158  $now = time();
159 
160  if (!$a_completed) {
161  $a_completed = array();
162  }
163 
164  $olp = ilObjectLP::getInstance($a_obj_id);
165  $collection = $olp->getCollectionInstance();
166  if ($collection) {
167  $existing = self::_getObjectStatus($a_obj_id, $a_user_id);
168 
169  foreach ($collection->getItems() as $item_id) {
170  if (isset($existing[$item_id])) {
171  // value changed
172  if ((!$existing[$item_id][0] && in_array(
173  $item_id,
174  $a_completed
175  )) ||
176  ($existing[$item_id][0] && !in_array(
177  $item_id,
178  $a_completed
179  ))) {
180  $ilDB->manipulate(
181  "UPDATE ut_lp_coll_manual SET " .
182  " completed = " . $ilDB->quote(
183  in_array($item_id, $a_completed),
184  "integer"
185  ) .
186  " , last_change = " . $ilDB->quote(
187  $now,
188  "integer"
189  ) .
190  " WHERE obj_id = " . $ilDB->quote(
191  $a_obj_id,
192  "integer"
193  ) .
194  " AND usr_id = " . $ilDB->quote(
195  $a_user_id,
196  "integer"
197  ) .
198  " AND subitem_id = " . $ilDB->quote(
199  $item_id,
200  "integer"
201  )
202  );
203  }
204  } elseif (in_array($item_id, $a_completed)) {
205  $ilDB->manipulate(
206  "INSERT INTO ut_lp_coll_manual" .
207  "(obj_id,usr_id,subitem_id,completed,last_change)" .
208  " VALUES (" . $ilDB->quote($a_obj_id, "integer") .
209  " , " . $ilDB->quote($a_user_id, "integer") .
210  " , " . $ilDB->quote($item_id, "integer") .
211  " , " . $ilDB->quote(1, "integer") .
212  " , " . $ilDB->quote($now, "integer") . ")"
213  );
214  }
215  }
216  }
217 
218  ilLPStatusWrapper::_updateStatus($a_obj_id, $a_user_id);
219  }
220 }
$res
Definition: ltiservices.php:69
determineStatus(int $a_obj_id, int $a_usr_id, object $a_obj=null)
static _getObjectStatus( $a_obj_id, $a_user_id=null)
static _getCompleted(int $a_obj_id)
Static function to read the users who have the status &#39;completed&#39;.
static _getStatusInfo(int $a_obj_id)
Reads informations about the object e.g test results, tlt, number of visits.
static _getAllReferences(int $id)
get all reference ids for object ID
global $DIC
Definition: feed.php:28
static _setObjectStatus(int $a_obj_id, int $a_user_id, array $a_completed=null)
$ref_id
Definition: ltiauth.php:67
static getInstance(int $obj_id)
static _updateStatus(int $a_obj_id, int $a_usr_id, ?object $a_obj=null, bool $a_percentage=false, bool $a_force_raise=false)