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