ILIAS  eassessment Revision 61809
 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  $set = $ilDB->query("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  );
241  $dirty = false;
242  if ($rec = $ilDB->fetchAssoc($set))
243  {
244  $dirty = true;
245  }
246 
247  // check if any records are missing
248  $missing = false;
249  if (!$dirty && is_array($a_users) && count($a_users) > 0)
250  {
251  $set = $ilDB->query("SELECT count(usr_id) cnt FROM ut_lp_marks WHERE ".
252  " obj_id = ".$ilDB->quote($a_obj_id, "integer")." AND ".
253  $ilDB->in("usr_id", $a_users, false, "integer"));
254  $r = $ilDB->fetchAssoc($set);
255  if ($r["cnt"] < count($a_users))
256  {
257  $missing = true;
258  }
259  }
260 
261  // refresh status, if records are dirty or missing
262  if ($dirty || $missing)
263  {
264  $class = ilLPStatusFactory::_getClassById($a_obj_id);
265  $trac_obj = new $class($a_obj_id);
266  $trac_obj->refreshStatus($a_obj_id);
267  }
268  }
269 
276  function refreshStatus($a_obj_id)
277  {
278  include_once("./Services/Tracking/classes/class.ilLPStatusWrapper.php");
279  $not_attempted = ilLPStatusWrapper::_getNotAttempted($a_obj_id);
280  foreach ($not_attempted as $user_id)
281  {
282  $percentage = $this->determinePercentage($a_obj_id, $user_id);
283  ilLPStatus::writeStatus($a_obj_id, $user_id, LP_STATUS_NOT_ATTEMPTED_NUM, $percentage, true);
284  }
285  $in_progress = ilLPStatusWrapper::_getInProgress($a_obj_id);
286  foreach ($in_progress as $user_id)
287  {
288  $percentage = $this->determinePercentage($a_obj_id, $user_id);
289  ilLPStatus::writeStatus($a_obj_id, $user_id, LP_STATUS_IN_PROGRESS_NUM, $percentage, true);
290  }
291  $completed = ilLPStatusWrapper::_getCompleted($a_obj_id);
292  foreach ($completed as $user_id)
293  {
294  $percentage = $this->determinePercentage($a_obj_id, $user_id);
295  ilLPStatus::writeStatus($a_obj_id, $user_id, LP_STATUS_COMPLETED_NUM, $percentage, true);
296  }
297  $failed = ilLPStatusWrapper::_getFailed($a_obj_id);
298  foreach ($failed as $user_id)
299  {
300  $percentage = $this->determinePercentage($a_obj_id, $user_id);
301  ilLPStatus::writeStatus($a_obj_id, $user_id, LP_STATUS_FAILED_NUM, $percentage, true);
302  }
303  }
304 
311  static function writeStatus($a_obj_id, $a_user_id, $a_status, $a_percentage = false, $a_force_per = false)
312  {
313  global $ilDB;
314 
315  $set = $ilDB->query("SELECT usr_id FROM ut_lp_marks WHERE ".
316  " obj_id = ".$ilDB->quote($a_obj_id, "integer")." AND ".
317  " usr_id = ".$ilDB->quote($a_user_id, "integer")
318  );
319 
320  $update_collections = false;
321  if ($rec = $ilDB->fetchAssoc($set))
322  {
323  $ret = $ilDB->manipulate("UPDATE ut_lp_marks SET ".
324  " status = ".$ilDB->quote($a_status, "integer").",".
325  " status_changed = ".$ilDB->now().",".
326  " status_dirty = ".$ilDB->quote(0, "integer").
327  " WHERE usr_id = ".$ilDB->quote($a_user_id, "integer").
328  " AND obj_id = ".$ilDB->quote($a_obj_id, "integer").
329  " AND status <> ".$ilDB->quote($a_status, "integer")
330  );
331  if ($ret != 0)
332  {
333  $update_collections = true;
334  }
335  }
336  else
337  {
338  $ilDB->manipulate("INSERT INTO ut_lp_marks ".
339  "(status, status_changed, usr_id, obj_id, status_dirty) VALUES (".
340  $ilDB->quote($a_status, "integer").",".
341  $ilDB->now().",".
342  $ilDB->quote($a_user_id, "integer").",".
343  $ilDB->quote($a_obj_id, "integer").",".
344  $ilDB->quote(0, "integer").
345  ")");
346  $update_collections = true;
347  }
348 
349  // always reset dirty flag
350  $ilDB->manipulate("UPDATE ut_lp_marks SET ".
351  " status_dirty = ".$ilDB->quote(0, "integer").
352  " WHERE usr_id = ".$ilDB->quote($a_user_id, "integer").
353  " AND obj_id = ".$ilDB->quote($a_obj_id, "integer")
354  );
355 
356  // update percentage
357  if ($a_percentage !== false || $a_force_per)
358  {
359  $a_percentage = max(0, (int) $a_percentage);
360  $a_percentage = min(100, $a_percentage);
361  $ret = $ilDB->manipulate("UPDATE ut_lp_marks SET ".
362  " percentage = ".$ilDB->quote($a_percentage, "integer").
363  " WHERE usr_id = ".$ilDB->quote($a_user_id, "integer").
364  " AND obj_id = ".$ilDB->quote($a_obj_id, "integer")
365  );
366  }
367 
368  // update collections
369  if ($update_collections)
370  {
371  $set = $ilDB->query("SELECT ut_lp_collections.obj_id obj_id FROM ".
372  "object_reference JOIN ut_lp_collections ON ".
373  "(object_reference.obj_id = ".$ilDB->quote($a_obj_id, "integer").
374  " AND object_reference.ref_id = ut_lp_collections.item_id)");
375  while ($rec = $ilDB->fetchAssoc($set))
376  {
377  if (in_array(ilObject::_lookupType($rec["obj_id"]), array("crs", "grp", "fold")))
378  {
379  include_once("./Services/Tracking/classes/class.ilLPStatusWrapper.php");
380  ilLPStatusWrapper::_updateStatus($rec["obj_id"], $a_user_id);
381  }
382  }
383  }
384  }
385 
391  static function setInProgressIfNotAttempted($a_obj_id, $a_user_id)
392  {
393  global $ilDB;
394 
395  $set = $ilDB->query("SELECT usr_id FROM ut_lp_marks WHERE ".
396  " obj_id = ".$ilDB->quote($a_obj_id, "integer")." AND ".
397  " usr_id = ".$ilDB->quote($a_user_id, "integer")
398  );
399 
400  $update_collections = false;
401  if ($rec = $ilDB->fetchAssoc($set))
402  {
403  $ret = $ilDB->manipulate("UPDATE ut_lp_marks SET ".
404  " status = ".$ilDB->quote(LP_STATUS_IN_PROGRESS_NUM, "integer").",".
405  " status_changed = ".$ilDB->now().",".
406  " status_dirty = ".$ilDB->quote(0, "integer").
407  " WHERE usr_id = ".$ilDB->quote($a_user_id, "integer").
408  " AND obj_id = ".$ilDB->quote($a_obj_id, "integer").
409  " AND status = ".$ilDB->quote(LP_STATUS_NOT_ATTEMPTED_NUM, "integer")
410  );
411  if ($ret != 0)
412  {
413  $update_collections = true;
414  }
415  }
416  else
417  {
418  $ilDB->manipulate("INSERT INTO ut_lp_marks ".
419  "(status, status_changed, usr_id, obj_id, status_dirty) VALUES (".
420  $ilDB->quote(LP_STATUS_IN_PROGRESS_NUM, "integer").",".
421  $ilDB->now().",".
422  $ilDB->quote($a_user_id, "integer").",".
423  $ilDB->quote($a_obj_id, "integer").",".
424  $ilDB->quote(0, "integer").
425  ")");
426  $update_collections = true;
427  }
428 
429  // update collections
430  if ($update_collections)
431  {
432  $set = $ilDB->query("SELECT ut_lp_collections.obj_id obj_id FROM ".
433  "object_reference JOIN ut_lp_collections ON ".
434  "(object_reference.obj_id = ".$ilDB->quote($a_obj_id, "integer").
435  " AND object_reference.ref_id = ut_lp_collections.item_id)");
436  while ($rec = $ilDB->fetchAssoc($set))
437  {
438  if (in_array(ilObject::_lookupType($rec["obj_id"]), array("crs", "grp", "fold")))
439  {
440  include_once("./Services/Tracking/classes/class.ilLPStatusWrapper.php");
441  ilLPStatusWrapper::_updateStatus($rec["obj_id"], $a_user_id);
442  }
443  }
444  }
445  }
446 
453  static function setAllDirty()
454  {
455  global $ilDB;
456 
457  $ilDB->manipulate("UPDATE ut_lp_marks SET ".
458  " status_dirty = ".$ilDB->quote(1, "integer")
459  );
460 
461  }
462 
469  static function setDirty($a_obj_id)
470  {
471  global $ilDB;
472 
473  $ilDB->manipulate("UPDATE ut_lp_marks SET ".
474  " status_dirty = ".$ilDB->quote(1, "integer").
475  " WHERE obj_id = ".$ilDB->quote($a_obj_id, "integer")
476  );
477 
478  }
479 
480 }
481 ?>