ILIAS  Release_4_1_x_branch Revision 61804
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilLPStatus.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 define('LP_STATUS_NOT_ATTEMPTED','trac_no_attempted');
5 define('LP_STATUS_IN_PROGRESS','trac_in_progress');
6 define('LP_STATUS_COMPLETED','trac_completed');
7 define('LP_STATUS_FAILED','trac_failed');
8 
9 define('LP_STATUS_NOT_ATTEMPTED_NUM', 0);
10 define('LP_STATUS_IN_PROGRESS_NUM', 1);
11 define('LP_STATUS_COMPLETED_NUM', 2);
12 define('LP_STATUS_FAILED_NUM', 3);
13 
14 // Stati for events
15 define('LP_STATUS_REGISTERED','trac_registered');
16 define('LP_STATUS_NOT_REGISTERED','trac_not_registered');
17 define('LP_STATUS_PARTICIPATED','trac_participated');
18 define('LP_STATUS_NOT_PARTICIPATED','trac_not_participated');
19 
32 {
33  var $obj_id = null;
34 
35  var $db = null;
36 
37  function ilLPStatus($a_obj_id)
38  {
39  global $ilDB;
40 
41  $this->obj_id = $a_obj_id;
42  $this->db =& $ilDB;
43  }
44 
45  function _getCountNotAttempted($a_obj_id)
46  {
47  return 0;
48  }
49 
50  function _getNotAttempted($a_obj_id)
51  {
52  return array();
53  }
54 
55  function _getCountInProgress($a_obj_id)
56  {
57  return 0;
58  }
59  function _getInProgress($a_obj_id)
60  {
61  return array();
62  }
63 
64  function _getCountCompleted($a_obj_id)
65  {
66  return 0;
67  }
68  function _getCompleted($a_obj_id)
69  {
70  return array();
71  }
72  function _getFailed($a_obj_id)
73  {
74  return array();
75  }
76  function _getCountFailed()
77  {
78  return 0;
79  }
80  function _getStatusInfo($a_obj_id)
81  {
82  return array();
83  }
84  function _getTypicalLearningTime($a_obj_id)
85  {
86  include_once 'Services/MetaData/classes/class.ilMDEducational.php';
88  }
89 
90 
191  function _updateStatus($a_obj_id, $a_usr_id, $a_obj = null)
192  {
193  $status = $this->determineStatus($a_obj_id, $a_usr_id, $a_obj);
194  $percentage = $this->determinePercentage($a_obj_id, $a_usr_id, $a_obj);
195  ilLPStatus::writeStatus($a_obj_id, $a_usr_id, $status, $percentage);
196  }
197 
204  function determinePercentage($a_obj_id, $a_usr_id, $a_obj = null)
205  {
206  return false;
207  }
208 
215  function determineStatus($a_obj_id, $a_usr_id, $a_obj = null)
216  {
217  return false;
218  }
219 
220 
228  static function checkStatusForObject($a_obj_id, $a_users = false)
229  {
230  global $ilDB;
231 
232 //@todo: there maybe the need to add extra handling for sessions here, since the
233 // "in progress" status is time dependent here. On the other hand, if they registered
234 // to the session, they already accessed the course and should have a "in progress"
235 // anyway. But the status on the session itself may not be correct.
236 
237  $sql = "SELECT usr_id FROM ut_lp_marks WHERE ".
238  " obj_id = ".$ilDB->quote($a_obj_id, "integer")." AND ".
239  " status_dirty = ".$ilDB->quote(1, "integer");
240  if(is_array($a_users) && count($a_users) > 0)
241  {
242  $sql .= " AND ".$ilDB->in("usr_id", $a_users, false, "integer");
243  }
244  $set = $ilDB->query($sql);
245  $dirty = false;
246  if ($rec = $ilDB->fetchAssoc($set))
247  {
248  $dirty = true;
249  }
250 
251  // check if any records are missing
252  $missing = false;
253  if (!$dirty && is_array($a_users) && count($a_users) > 0)
254  {
255  $set = $ilDB->query("SELECT count(usr_id) cnt FROM ut_lp_marks WHERE ".
256  " obj_id = ".$ilDB->quote($a_obj_id, "integer")." AND ".
257  $ilDB->in("usr_id", $a_users, false, "integer"));
258  $r = $ilDB->fetchAssoc($set);
259  if ($r["cnt"] < count($a_users))
260  {
261  $missing = true;
262  }
263  }
264 
265  // refresh status, if records are dirty or missing
266  if ($dirty || $missing)
267  {
268  $class = ilLPStatusFactory::_getClassById($a_obj_id);
269  $trac_obj = new $class($a_obj_id);
270  $trac_obj->refreshStatus($a_obj_id, $a_users);
271  }
272  }
273 
280  function refreshStatus($a_obj_id, $a_users = null)
281  {
282  include_once("./Services/Tracking/classes/class.ilLPStatusWrapper.php");
283  $not_attempted = ilLPStatusWrapper::_getNotAttempted($a_obj_id);
284  foreach ($not_attempted as $user_id)
285  {
286  $percentage = $this->determinePercentage($a_obj_id, $user_id);
287  ilLPStatus::writeStatus($a_obj_id, $user_id, LP_STATUS_NOT_ATTEMPTED_NUM, $percentage, true);
288  }
289  $in_progress = ilLPStatusWrapper::_getInProgress($a_obj_id);
290  foreach ($in_progress as $user_id)
291  {
292  $percentage = $this->determinePercentage($a_obj_id, $user_id);
293  ilLPStatus::writeStatus($a_obj_id, $user_id, LP_STATUS_IN_PROGRESS_NUM, $percentage, true);
294  }
295  $completed = ilLPStatusWrapper::_getCompleted($a_obj_id);
296  foreach ($completed as $user_id)
297  {
298  $percentage = $this->determinePercentage($a_obj_id, $user_id);
299  ilLPStatus::writeStatus($a_obj_id, $user_id, LP_STATUS_COMPLETED_NUM, $percentage, true);
300  }
302  foreach ($failed as $user_id)
303  {
304  $percentage = $this->determinePercentage($a_obj_id, $user_id);
305  ilLPStatus::writeStatus($a_obj_id, $user_id, LP_STATUS_FAILED_NUM, $percentage, true);
306  }
307  if($a_users)
308  {
309  $missing_users = array_diff($a_users, $not_attempted+$in_progress+$completed+$failed);
310  if($missing_users)
311  {
312  foreach ($missing_users as $user_id)
313  {
314  ilLPStatusWrapper::_updateStatus($a_obj_id, $user_id);
315  }
316  }
317  }
318  }
319 
326  static function writeStatus($a_obj_id, $a_user_id, $a_status, $a_percentage = false, $a_force_per = false)
327  {
328  global $ilDB;
329 
330  $set = $ilDB->query("SELECT usr_id FROM ut_lp_marks WHERE ".
331  " obj_id = ".$ilDB->quote($a_obj_id, "integer")." AND ".
332  " usr_id = ".$ilDB->quote($a_user_id, "integer")
333  );
334 
335  $update_collections = false;
336  if ($rec = $ilDB->fetchAssoc($set))
337  {
338  $ret = $ilDB->manipulate("UPDATE ut_lp_marks SET ".
339  " status = ".$ilDB->quote($a_status, "integer").",".
340  " status_changed = ".$ilDB->now().",".
341  " status_dirty = ".$ilDB->quote(0, "integer").
342  " WHERE usr_id = ".$ilDB->quote($a_user_id, "integer").
343  " AND obj_id = ".$ilDB->quote($a_obj_id, "integer").
344  " AND status <> ".$ilDB->quote($a_status, "integer")
345  );
346  if ($ret != 0)
347  {
348  $update_collections = true;
349  }
350  }
351  else
352  {
353  $ilDB->manipulate("INSERT INTO ut_lp_marks ".
354  "(status, status_changed, usr_id, obj_id, status_dirty) VALUES (".
355  $ilDB->quote($a_status, "integer").",".
356  $ilDB->now().",".
357  $ilDB->quote($a_user_id, "integer").",".
358  $ilDB->quote($a_obj_id, "integer").",".
359  $ilDB->quote(0, "integer").
360  ")");
361  $update_collections = true;
362  }
363 
364  // always reset dirty flag
365  $ilDB->manipulate("UPDATE ut_lp_marks SET ".
366  " status_dirty = ".$ilDB->quote(0, "integer").
367  " WHERE usr_id = ".$ilDB->quote($a_user_id, "integer").
368  " AND obj_id = ".$ilDB->quote($a_obj_id, "integer")
369  );
370 
371  // update percentage
372  if ($a_percentage !== false || $a_force_per)
373  {
374  $a_percentage = max(0, (int) $a_percentage);
375  $a_percentage = min(100, $a_percentage);
376  $ret = $ilDB->manipulate("UPDATE ut_lp_marks SET ".
377  " percentage = ".$ilDB->quote($a_percentage, "integer").
378  " WHERE usr_id = ".$ilDB->quote($a_user_id, "integer").
379  " AND obj_id = ".$ilDB->quote($a_obj_id, "integer")
380  );
381  }
382 
383  // update collections
384  if ($update_collections)
385  {
386  // a change occured - remove existing cache entry
387  include_once("./Services/Tracking/classes/class.ilLPStatusWrapper.php");
388  ilLPStatusWrapper::_removeStatusCache($a_obj_id, $a_user_id);
389 
390  $set = $ilDB->query("SELECT ut_lp_collections.obj_id obj_id FROM ".
391  "object_reference JOIN ut_lp_collections ON ".
392  "(object_reference.obj_id = ".$ilDB->quote($a_obj_id, "integer").
393  " AND object_reference.ref_id = ut_lp_collections.item_id)");
394  while ($rec = $ilDB->fetchAssoc($set))
395  {
396  if (in_array(ilObject::_lookupType($rec["obj_id"]), array("crs", "grp", "fold")))
397  {
398  // just to make sure - remove existing cache entry
399  ilLPStatusWrapper::_removeStatusCache($rec["obj_id"], $a_user_id);
400 
401  ilLPStatusWrapper::_updateStatus($rec["obj_id"], $a_user_id);
402  }
403  }
404  }
405  }
406 
412  static function setInProgressIfNotAttempted($a_obj_id, $a_user_id)
413  {
414  global $ilDB;
415 
416  $set = $ilDB->query("SELECT usr_id FROM ut_lp_marks WHERE ".
417  " obj_id = ".$ilDB->quote($a_obj_id, "integer")." AND ".
418  " usr_id = ".$ilDB->quote($a_user_id, "integer")
419  );
420 
421  $update_collections = false;
422  if ($rec = $ilDB->fetchAssoc($set))
423  {
424  $ret = $ilDB->manipulate("UPDATE ut_lp_marks SET ".
425  " status = ".$ilDB->quote(LP_STATUS_IN_PROGRESS_NUM, "integer").",".
426  " status_changed = ".$ilDB->now().",".
427  " status_dirty = ".$ilDB->quote(0, "integer").
428  " WHERE usr_id = ".$ilDB->quote($a_user_id, "integer").
429  " AND obj_id = ".$ilDB->quote($a_obj_id, "integer").
430  " AND status = ".$ilDB->quote(LP_STATUS_NOT_ATTEMPTED_NUM, "integer")
431  );
432  if ($ret != 0)
433  {
434  $update_collections = true;
435  }
436  }
437  else
438  {
439  $ilDB->manipulate("INSERT INTO ut_lp_marks ".
440  "(status, status_changed, usr_id, obj_id, status_dirty) VALUES (".
441  $ilDB->quote(LP_STATUS_IN_PROGRESS_NUM, "integer").",".
442  $ilDB->now().",".
443  $ilDB->quote($a_user_id, "integer").",".
444  $ilDB->quote($a_obj_id, "integer").",".
445  $ilDB->quote(0, "integer").
446  ")");
447  $update_collections = true;
448  }
449 
450  // update collections
451  if ($update_collections)
452  {
453  $set = $ilDB->query("SELECT ut_lp_collections.obj_id obj_id FROM ".
454  "object_reference JOIN ut_lp_collections ON ".
455  "(object_reference.obj_id = ".$ilDB->quote($a_obj_id, "integer").
456  " AND object_reference.ref_id = ut_lp_collections.item_id)");
457  while ($rec = $ilDB->fetchAssoc($set))
458  {
459  if (in_array(ilObject::_lookupType($rec["obj_id"]), array("crs", "grp", "fold")))
460  {
461  include_once("./Services/Tracking/classes/class.ilLPStatusWrapper.php");
462  ilLPStatusWrapper::_updateStatus($rec["obj_id"], $a_user_id);
463  }
464  }
465  }
466  }
467 
474  static function setAllDirty()
475  {
476  global $ilDB;
477 
478  $ilDB->manipulate("UPDATE ut_lp_marks SET ".
479  " status_dirty = ".$ilDB->quote(1, "integer")
480  );
481 
482  }
483 
490  static function setDirty($a_obj_id)
491  {
492  global $ilDB;
493 
494  $ilDB->manipulate("UPDATE ut_lp_marks SET ".
495  " status_dirty = ".$ilDB->quote(1, "integer").
496  " WHERE obj_id = ".$ilDB->quote($a_obj_id, "integer")
497  );
498 
499  }
500 
501 }
502 ?>