ILIAS  Release_4_2_x_branch Revision 61807
 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 
195  function _updateStatus($a_obj_id, $a_usr_id, $a_obj = null)
196  {
197 //global $ilLog;
198 //$ilLog->write("ilLPStatus-_updateStatus-");
199 
200  $status = $this->determineStatus($a_obj_id, $a_usr_id, $a_obj);
201  $percentage = $this->determinePercentage($a_obj_id, $a_usr_id, $a_obj);
202  ilLPStatus::writeStatus($a_obj_id, $a_usr_id, $status, $percentage);
203  }
204 
211  function determinePercentage($a_obj_id, $a_usr_id, $a_obj = null)
212  {
213  return false;
214  }
215 
222  function determineStatus($a_obj_id, $a_usr_id, $a_obj = null)
223  {
224  return false;
225  }
226 
227 
235  static function checkStatusForObject($a_obj_id, $a_users = false)
236  {
237  global $ilDB;
238 
239 //@todo: there maybe the need to add extra handling for sessions here, since the
240 // "in progress" status is time dependent here. On the other hand, if they registered
241 // to the session, they already accessed the course and should have a "in progress"
242 // anyway. But the status on the session itself may not be correct.
243 
244  $sql = "SELECT usr_id FROM ut_lp_marks WHERE ".
245  " obj_id = ".$ilDB->quote($a_obj_id, "integer")." AND ".
246  " status_dirty = ".$ilDB->quote(1, "integer");
247  if(is_array($a_users) && count($a_users) > 0)
248  {
249  $sql .= " AND ".$ilDB->in("usr_id", $a_users, false, "integer");
250  }
251  $set = $ilDB->query($sql);
252  $dirty = false;
253  if ($rec = $ilDB->fetchAssoc($set))
254  {
255  $dirty = true;
256  }
257 
258  // check if any records are missing
259  $missing = false;
260  if (!$dirty && is_array($a_users) && count($a_users) > 0)
261  {
262  $set = $ilDB->query("SELECT count(usr_id) cnt FROM ut_lp_marks WHERE ".
263  " obj_id = ".$ilDB->quote($a_obj_id, "integer")." AND ".
264  $ilDB->in("usr_id", $a_users, false, "integer"));
265  $r = $ilDB->fetchAssoc($set);
266  if ($r["cnt"] < count($a_users))
267  {
268  $missing = true;
269  }
270  }
271 
272  // refresh status, if records are dirty or missing
273  if ($dirty || $missing)
274  {
275  $class = ilLPStatusFactory::_getClassById($a_obj_id);
276  $trac_obj = new $class($a_obj_id);
277  $trac_obj->refreshStatus($a_obj_id, $a_users);
278  }
279  }
280 
287  function refreshStatus($a_obj_id, $a_users = null)
288  {
289  include_once("./Services/Tracking/classes/class.ilLPStatusWrapper.php");
290  $not_attempted = ilLPStatusWrapper::_getNotAttempted($a_obj_id);
291  foreach ($not_attempted as $user_id)
292  {
293  $percentage = $this->determinePercentage($a_obj_id, $user_id);
294  ilLPStatus::writeStatus($a_obj_id, $user_id, LP_STATUS_NOT_ATTEMPTED_NUM, $percentage, true);
295  }
296  $in_progress = ilLPStatusWrapper::_getInProgress($a_obj_id);
297  foreach ($in_progress as $user_id)
298  {
299  $percentage = $this->determinePercentage($a_obj_id, $user_id);
300  ilLPStatus::writeStatus($a_obj_id, $user_id, LP_STATUS_IN_PROGRESS_NUM, $percentage, true);
301  }
302  $completed = ilLPStatusWrapper::_getCompleted($a_obj_id);
303  foreach ($completed as $user_id)
304  {
305  $percentage = $this->determinePercentage($a_obj_id, $user_id);
306  ilLPStatus::writeStatus($a_obj_id, $user_id, LP_STATUS_COMPLETED_NUM, $percentage, true);
307  }
309  foreach ($failed as $user_id)
310  {
311  $percentage = $this->determinePercentage($a_obj_id, $user_id);
312  ilLPStatus::writeStatus($a_obj_id, $user_id, LP_STATUS_FAILED_NUM, $percentage, true);
313  }
314  if($a_users)
315  {
316  $missing_users = array_diff($a_users, $not_attempted+$in_progress+$completed+$failed);
317  if($missing_users)
318  {
319  foreach ($missing_users as $user_id)
320  {
321  ilLPStatusWrapper::_updateStatus($a_obj_id, $user_id);
322  }
323  }
324  }
325  }
326 
333  static function writeStatus($a_obj_id, $a_user_id, $a_status, $a_percentage = false, $a_force_per = false)
334  {
335  global $ilDB;
336 
337  $set = $ilDB->query("SELECT usr_id FROM ut_lp_marks WHERE ".
338  " obj_id = ".$ilDB->quote($a_obj_id, "integer")." AND ".
339  " usr_id = ".$ilDB->quote($a_user_id, "integer")
340  );
341 
342  $update_collections = false;
343  if ($rec = $ilDB->fetchAssoc($set))
344  {
345  $ret = $ilDB->manipulate("UPDATE ut_lp_marks SET ".
346  " status = ".$ilDB->quote($a_status, "integer").",".
347  " status_changed = ".$ilDB->now().",".
348  " status_dirty = ".$ilDB->quote(0, "integer").
349  " WHERE usr_id = ".$ilDB->quote($a_user_id, "integer").
350  " AND obj_id = ".$ilDB->quote($a_obj_id, "integer").
351  " AND status <> ".$ilDB->quote($a_status, "integer")
352  );
353  if ($ret != 0)
354  {
355  $update_collections = true;
356  }
357  }
358  else
359  {
360  $ilDB->manipulate("INSERT INTO ut_lp_marks ".
361  "(status, status_changed, usr_id, obj_id, status_dirty) VALUES (".
362  $ilDB->quote($a_status, "integer").",".
363  $ilDB->now().",".
364  $ilDB->quote($a_user_id, "integer").",".
365  $ilDB->quote($a_obj_id, "integer").",".
366  $ilDB->quote(0, "integer").
367  ")");
368  $update_collections = true;
369  }
370 
371  // always reset dirty flag
372  $ilDB->manipulate("UPDATE ut_lp_marks SET ".
373  " status_dirty = ".$ilDB->quote(0, "integer").
374  " WHERE usr_id = ".$ilDB->quote($a_user_id, "integer").
375  " AND obj_id = ".$ilDB->quote($a_obj_id, "integer")
376  );
377 
378  // update percentage
379  if ($a_percentage !== false || $a_force_per)
380  {
381  $a_percentage = max(0, (int) $a_percentage);
382  $a_percentage = min(100, $a_percentage);
383  $ret = $ilDB->manipulate("UPDATE ut_lp_marks SET ".
384  " percentage = ".$ilDB->quote($a_percentage, "integer").
385  " WHERE usr_id = ".$ilDB->quote($a_user_id, "integer").
386  " AND obj_id = ".$ilDB->quote($a_obj_id, "integer")
387  );
388  }
389 
390  // update collections
391  if ($update_collections)
392  {
393  // a change occured - remove existing cache entry
394  include_once("./Services/Tracking/classes/class.ilLPStatusWrapper.php");
395  ilLPStatusWrapper::_removeStatusCache($a_obj_id, $a_user_id);
396 
397  $set = $ilDB->query("SELECT ut_lp_collections.obj_id obj_id FROM ".
398  "object_reference JOIN ut_lp_collections ON ".
399  "(object_reference.obj_id = ".$ilDB->quote($a_obj_id, "integer").
400  " AND object_reference.ref_id = ut_lp_collections.item_id)");
401  while ($rec = $ilDB->fetchAssoc($set))
402  {
403  if (in_array(ilObject::_lookupType($rec["obj_id"]), array("crs", "grp", "fold")))
404  {
405  // just to make sure - remove existing cache entry
406  ilLPStatusWrapper::_removeStatusCache($rec["obj_id"], $a_user_id);
407 
408  ilLPStatusWrapper::_updateStatus($rec["obj_id"], $a_user_id);
409  }
410  }
411  }
412  }
413 
419  static function setInProgressIfNotAttempted($a_obj_id, $a_user_id)
420  {
421  global $ilDB;
422 
423  $set = $ilDB->query("SELECT usr_id FROM ut_lp_marks WHERE ".
424  " obj_id = ".$ilDB->quote($a_obj_id, "integer")." AND ".
425  " usr_id = ".$ilDB->quote($a_user_id, "integer")
426  );
427 
428  $update_collections = false;
429  if ($rec = $ilDB->fetchAssoc($set))
430  {
431  $ret = $ilDB->manipulate("UPDATE ut_lp_marks SET ".
432  " status = ".$ilDB->quote(LP_STATUS_IN_PROGRESS_NUM, "integer").",".
433  " status_changed = ".$ilDB->now().",".
434  " status_dirty = ".$ilDB->quote(0, "integer").
435  " WHERE usr_id = ".$ilDB->quote($a_user_id, "integer").
436  " AND obj_id = ".$ilDB->quote($a_obj_id, "integer").
437  " AND status = ".$ilDB->quote(LP_STATUS_NOT_ATTEMPTED_NUM, "integer")
438  );
439  if ($ret != 0)
440  {
441  $update_collections = true;
442  }
443  }
444  else
445  {
446  $ilDB->manipulate("INSERT INTO ut_lp_marks ".
447  "(status, status_changed, usr_id, obj_id, status_dirty) VALUES (".
448  $ilDB->quote(LP_STATUS_IN_PROGRESS_NUM, "integer").",".
449  $ilDB->now().",".
450  $ilDB->quote($a_user_id, "integer").",".
451  $ilDB->quote($a_obj_id, "integer").",".
452  $ilDB->quote(0, "integer").
453  ")");
454  $update_collections = true;
455  }
456 
457  // update collections
458  if ($update_collections)
459  {
460  $set = $ilDB->query("SELECT ut_lp_collections.obj_id obj_id FROM ".
461  "object_reference JOIN ut_lp_collections ON ".
462  "(object_reference.obj_id = ".$ilDB->quote($a_obj_id, "integer").
463  " AND object_reference.ref_id = ut_lp_collections.item_id)");
464  while ($rec = $ilDB->fetchAssoc($set))
465  {
466  if (in_array(ilObject::_lookupType($rec["obj_id"]), array("crs", "grp", "fold")))
467  {
468  include_once("./Services/Tracking/classes/class.ilLPStatusWrapper.php");
469  ilLPStatusWrapper::_updateStatus($rec["obj_id"], $a_user_id);
470  }
471  }
472  }
473  }
474 
481  static function setAllDirty()
482  {
483  global $ilDB;
484 
485  $ilDB->manipulate("UPDATE ut_lp_marks SET ".
486  " status_dirty = ".$ilDB->quote(1, "integer")
487  );
488 
489  }
490 
497  static function setDirty($a_obj_id)
498  {
499  global $ilDB;
500 
501  $ilDB->manipulate("UPDATE ut_lp_marks SET ".
502  " status_dirty = ".$ilDB->quote(1, "integer").
503  " WHERE obj_id = ".$ilDB->quote($a_obj_id, "integer")
504  );
505  }
506 
513  function _lookupStatus($a_obj_id, $a_user_id)
514  {
515  global $ilDB;
516 
517  $set = $ilDB->query("SELECT status FROM ut_lp_marks WHERE ".
518  " status_dirty = ".$ilDB->quote(0, "integer").
519  " AND usr_id = ".$ilDB->quote($a_user_id, "integer").
520  " AND obj_id = ".$ilDB->quote($a_obj_id, "integer")
521  );
522  if ($rec = $ilDB->fetchAssoc($set))
523  {
524  return $rec["status"];
525  }
526  else
527  {
528  include_once("./Services/Tracking/classes/class.ilLPStatusWrapper.php");
529  ilLPStatusWrapper::_updateStatus($a_obj_id, $a_user_id);
530  $set = $ilDB->query("SELECT status FROM ut_lp_marks WHERE ".
531  " status_dirty = ".$ilDB->quote(0, "integer").
532  " AND usr_id = ".$ilDB->quote($a_user_id, "integer").
533  " AND obj_id = ".$ilDB->quote($a_obj_id, "integer")
534  );
535  if ($rec = $ilDB->fetchAssoc($set))
536  {
537  return $rec["status"];
538  }
539  }
540  }
541 
548  function _lookupStatusChanged($a_obj_id, $a_user_id)
549  {
550  global $ilDB;
551 
552  $set = $ilDB->query("SELECT status_changed FROM ut_lp_marks WHERE ".
553  " status_dirty = ".$ilDB->quote(0, "integer").
554  " AND usr_id = ".$ilDB->quote($a_user_id, "integer").
555  " AND obj_id = ".$ilDB->quote($a_obj_id, "integer")
556  );
557  if ($rec = $ilDB->fetchAssoc($set))
558  {
559  return $rec["status_changed"];
560  }
561  else
562  {
563  include_once("./Services/Tracking/classes/class.ilLPStatusWrapper.php");
564  ilLPStatusWrapper::_updateStatus($a_obj_id, $a_user_id);
565  $set = $ilDB->query("SELECT status_changed FROM ut_lp_marks WHERE ".
566  " status_dirty = ".$ilDB->quote(0, "integer").
567  " AND usr_id = ".$ilDB->quote($a_user_id, "integer").
568  " AND obj_id = ".$ilDB->quote($a_obj_id, "integer")
569  );
570  if ($rec = $ilDB->fetchAssoc($set))
571  {
572  return $rec["status_changed"];
573  }
574  }
575  }
576 
577 }
578 ?>