ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
class.ilLOUserResults.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
12 {
13  protected $course_obj_id; // [int]
14  protected $user_id; // [int]
15 
16  const TYPE_INITIAL = 1;
17  const TYPE_QUALIFIED = 2;
18 
19  const STATUS_COMPLETED = 1;
20  const STATUS_FAILED = 2;
21 
29  public function __construct($a_course_obj_id, $a_user_id)
30  {
31  $this->course_obj_id = (int)$a_course_obj_id;
32  $this->user_id = (int)$a_user_id;
33  }
34 
38  public static function lookupResult($a_course_obj_id, $a_user_id, $a_objective_id, $a_tst_type)
39  {
40  global $ilDB;
41 
42  $query = 'SELECT * FROM loc_user_results '.
43  'WHERE user_id = '.$ilDB->quote($a_user_id,'integer').' '.
44  'AND course_id = '.$ilDB->quote($a_course_obj_id,'integer').' '.
45  'AND objective_id = '.$ilDB->quote($a_objective_id,'integer').' '.
46  'AND type = '.$ilDB->quote($a_tst_type,'integer');
47  $res = $ilDB->query($query);
48  $ur = array(
49  'status' => self::STATUS_FAILED,
50  'result_perc' => 0,
51  'limit_perc' => 0,
52  'tries' => 0,
53  'is_final' => 0
54  );
55  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
56  {
57  $ur['status'] = $row->status;
58  $ur['result_perc'] = $row->result_perc;
59  $ur['limit_perc'] = $row->limit_perc;
60  $ur['tries'] = $row->tries;
61  $ur['is_final'] = $row->is_final;
62  }
63  return $ur;
64  }
65 
66  public static function resetFinalByObjective($a_objective_id)
67  {
68  $query = 'UPDATE loc_user_results '.
69  'SET is_final = '.$GLOBALS['ilDB']->quote(0,'integer').' '.
70  'WHERE objective_id = '.$GLOBALS['ilDB']->quote($a_objective_id,'integer');
71  $GLOBALS['ilDB']->manipulate($query);
72  }
73 
74 
81  protected static function isValidType($a_type)
82  {
83  return in_array((int)$a_type, array(self::TYPE_INITIAL, self::TYPE_QUALIFIED));
84  }
85 
92  protected static function isValidStatus($a_status)
93  {
94  return in_array((int)$a_status, array(self::STATUS_COMPLETED, self::STATUS_FAILED));
95  }
96 
103  public static function deleteResultsForUser($a_user_id)
104  {
105  global $ilDB;
106 
107  if(!(int)$a_user_id)
108  {
109  return false;
110  }
111 
112  $ilDB->manipulate("DELETE FROM loc_user_results".
113  " WHERE user_id = ".$ilDB->quote($a_user_id, "integer"));
114  return true;
115  }
116 
117 
124  public static function deleteResultsForCourse($a_course_id)
125  {
126  global $ilDB;
127 
128  if(!(int)$a_course_id)
129  {
130  return false;
131  }
132 
133  $ilDB->manipulate("DELETE FROM loc_user_results".
134  " WHERE course_id = ".$ilDB->quote($a_course_id, "integer"));
135  return true;
136  }
137 
142  public function delete()
143  {
144  global $ilDB;
145 
146  $query = 'DELETE FROM loc_user_results '.
147  'WHERE course_id = '.$ilDB->quote($this->course_obj_id).' '.
148  'AND user_id = '.$ilDB->quote($this->user_id);
149  $ilDB->manipulate($query);
150  }
151 
161  public static function deleteResultsFromLP($a_course_id, array $a_user_ids, $a_remove_initial, $a_remove_qualified)
162  {
163  global $ilDB;
164 
165  if(!(int)$a_course_id || !sizeof($a_user_ids))
166  {
167  return false;
168  }
169 
170  $sql = "DELETE FROM loc_user_results".
171  " WHERE course_id = ".$ilDB->quote($a_course_id, "integer").
172  " AND ".$ilDB->in("user_id", $a_user_ids, "", "integer");
173 
174  if(!(bool)$a_remove_initial || !(bool)$a_remove_qualified)
175  {
176  if((bool)$a_remove_initial)
177  {
178  $sql .= " AND type = ".$ilDB->quote(self::TYPE_INITIAL, "integer");
179  }
180  else
181  {
182  $sql .= " AND type = ".$ilDB->quote(self::TYPE_QUALIFIED, "integer");
183  }
184  }
185 
186  $ilDB->manipulate($sql);
187  return true;
188  }
189 
190 
203  public function saveObjectiveResult($a_objective_id, $a_type, $a_status, $a_result_percentage, $a_limit_percentage, $a_tries, $a_is_final)
204  {
205  global $ilDB;
206 
207  if(!self::isValidType($a_type) ||
208  !self::isValidStatus($a_status))
209  {
210  return false;
211  }
212  $ilDB->replace("loc_user_results",
213  array(
214  "course_id" => array("integer", $this->course_obj_id),
215  "user_id" => array("integer", $this->user_id),
216  "objective_id" => array("integer", $a_objective_id),
217  "type" => array("integer", $a_type)
218  ),
219  array(
220  "status" => array("integer", $a_status),
221  "result_perc" => array("integer", $a_result_percentage),
222  "limit_perc" => array("integer", $a_limit_percentage),
223  "tries" => array("integer", $a_tries),
224  "is_final" => array("integer", $a_is_final),
225  "tstamp" => array("integer", time()),
226  )
227  );
228  return true;
229  }
230 
239  protected function findObjectiveIds($a_type = null, $a_status = null, $a_is_final = null)
240  {
241  global $ilDB;
242 
243  $res = array();
244 
245  $sql = "SELECT objective_id".
246  " FROM loc_user_results".
247  " WHERE course_id = ".$ilDB->quote($this->course_obj_id, "integer").
248  " AND user_id = ".$ilDB->quote($this->user_id, "integer");
249 
250  if($this->isValidType($a_type))
251  {
252  $sql .= " AND type = ".$ilDB->quote($a_type, "integer");
253  }
254  if($this->isValidStatus($a_status))
255  {
256  $sql .= " AND status = ".$ilDB->quote($a_status, "integer");
257  }
258  if($a_is_final !== null)
259  {
260  $sql .= " AND is_final = ".$ilDB->quote($a_is_final, "integer");
261  }
262 
263  $set = $ilDB->query($sql);
264  while($row = $ilDB->fetchAssoc($set))
265  {
266  $res[] = $row["objective_id"];
267  }
268 
269  return $res;
270  }
271 
277  public function getCompletedObjectiveIdsByType($a_type)
278  {
279  return $this->findObjectiveIds($a_type, self::STATUS_COMPLETED);
280  }
281 
287  public function getSuggestedObjectiveIds()
288  {
289  return $this->findObjectiveIds(self::TYPE_INITIAL, self::STATUS_FAILED);
290  }
291 
297  public function getCompletedObjectiveIds()
298  {
299  include_once './Modules/Course/classes/Objectives/class.ilLOSettings.php';
300  $settings = ilLOSettings::getInstanceByObjId($this->course_obj_id);
301 
302  if(!$settings->isInitialTestQualifying() or !$settings->worksWithInitialTest())
303  {
304  return $this->findObjectiveIds(self::TYPE_QUALIFIED, self::STATUS_COMPLETED);
305  }
306 
307  // qualifying initial
308  return array_unique(
309  array_merge(
310  $this->findObjectiveIds(self::TYPE_INITIAL, self::STATUS_COMPLETED),
311  $this->findObjectiveIds(self::TYPE_QUALIFIED, self::STATUS_COMPLETED)
312  )
313  );
314  }
315 
322  public function getFailedObjectiveIds($a_is_final = true)
323  {
324  return $this->findObjectiveIds(self::TYPE_QUALIFIED, self::STATUS_FAILED, $a_is_final);
325  }
326 
333  {
334  global $ilDB;
335 
336  $res = array();
337 
338  $set = $ilDB->query("SELECT *".
339  " FROM loc_user_results".
340  " WHERE course_id = ".$ilDB->quote($this->course_obj_id, "integer").
341  " AND user_id = ".$ilDB->quote($this->user_id, "integer"));
342  while($row = $ilDB->fetchAssoc($set))
343  {
344  $objective_id = $row["objective_id"];
345  $type = $row["type"];
346  unset($row["objective_id"]);
347  unset($row["type"]);
348  $res[$objective_id][$type] = $row;
349  }
350 
351  return $res;
352  }
353 
354  public static function getObjectiveStatusForLP($a_user_id, $a_obj_id, array $a_objective_ids)
355  {
356  global $ilDB;
357 
358  // are initital test(s) qualifying?
359  include_once "Modules/Course/classes/Objectives/class.ilLOSettings.php";
360  $lo_set = ilLOSettings::getInstanceByObjId($a_obj_id);
361  $initial_qualifying = $lo_set->isInitialTestQualifying();
362 
363  // this method returns LP status codes!
364  include_once "Services/Tracking/classes/class.ilLPStatus.php";
365 
366  $res = array();
367 
368  $sql = "SELECT lor.objective_id, lor.user_id, lor.status, lor.is_final".
369  " FROM loc_user_results lor".
370  " JOIN crs_objectives cobj ON (cobj.objective_id = lor.objective_id)".
371  " WHERE ".$ilDB->in("lor.objective_id", $a_objective_ids, "", "integer");
372  if(!(bool)$initial_qualifying)
373  {
374  $sql .= " AND lor.type = ".$ilDB->quote(self::TYPE_QUALIFIED, "integer");
375  }
376  $sql .= " AND lor.user_id = ".$ilDB->quote($a_user_id, "integer").
377  " AND cobj.active = ".$ilDB->quote(1, "integer").
378  " ORDER BY lor.type"; // qualified must come last!
379  $set = $ilDB->query($sql);
380  while($row = $ilDB->fetchAssoc($set))
381  {
382  switch($row["status"])
383  {
384  case self::STATUS_FAILED:
385  if((bool)$row["is_final"])
386  {
388  }
389  else
390  {
391  // #15379
393  }
394  break;
395 
396  case self::STATUS_COMPLETED:
398  break;
399 
400  default:
401  /*
402  $status = ilLPStatus::LP_STATUS_NOT_ATTEMPTED_NUM;
403  break;
404  */
405  continue;
406  }
407 
408  // if both initial and qualified, qualified will overwrite initial
409  $res[$row["objective_id"]] = $status;
410  }
411 
412  return $res;
413  }
414 
415  public static function getSummarizedObjectiveStatusForLP($a_obj_id, array $a_objective_ids, $a_user_id = null)
416  {
417  global $ilDB;
418 
419  // change event is NOT parsed here!
420 
421  // are initital test(s) qualifying?
422  include_once "Modules/Course/classes/Objectives/class.ilLOSettings.php";
423  $lo_set = ilLOSettings::getInstanceByObjId($a_obj_id);
424  $initial_qualifying = $lo_set->isInitialTestQualifying();
425 
426  // this method returns LP status codes!
427  include_once "Services/Tracking/classes/class.ilLPStatus.php";
428 
429  $res = $tmp_completed = array();
430 
431  $sql = "SELECT lor.objective_id, lor.user_id, lor.status, lor.type".
432  " FROM loc_user_results lor".
433  " JOIN crs_objectives cobj ON (cobj.objective_id = lor.objective_id)".
434  " WHERE ".$ilDB->in("lor.objective_id", $a_objective_ids, "", "integer").
435  " AND cobj.active = ".$ilDB->quote(1, "integer");
436  if(!(bool)$initial_qualifying)
437  {
438  $sql .= " AND lor.type = ".$ilDB->quote(self::TYPE_QUALIFIED, "integer");
439  }
440  if($a_user_id)
441  {
442  $sql .= " AND lor.user_id = ".$ilDB->quote($a_user_id, "integer");
443  }
444  $sql .= " ORDER BY lor.type DESC"; // qualified must come first!
445  $set = $ilDB->query($sql);
446  while($row = $ilDB->fetchAssoc($set))
447  {
448  $user_id = (int)$row["user_id"];
449  $status = (int)$row["status"];
450 
451  // initial tests only count if no qualified test
452  if($row["type"] == self::TYPE_INITIAL &&
453  isset($res[$user_id]))
454  {
455  continue;
456  }
457 
458  // user did do something
460 
461  switch($status)
462  {
463  case self::STATUS_COMPLETED:
464  $tmp_completed[$user_id]++;
465  break;
466 
467  case self::STATUS_FAILED:
468  if((bool)$row["is_final"])
469  {
470  // object is failed when at least 1 objective is failed without any tries left
472  }
473  break;
474  }
475  }
476 
477  $all_nr = sizeof($a_objective_ids);
478  foreach($tmp_completed as $user_id => $counter)
479  {
480  // if used as precondition object should be completed ASAP, status can be lost on subsequent tries
481  if($counter == $all_nr)
482  {
484  }
485  }
486 
487  if($a_user_id)
488  {
489  // might return null!
490  return $res[$a_user_id];
491  }
492  else
493  {
494  return $res;
495  }
496  }
497 
498  public static function hasResults($a_container_id, $a_user_id)
499  {
500  global $ilDB;
501 
502  $query = 'SELECT objective_id FROM loc_user_results '.
503  'WHERE course_id = '.$ilDB->quote($a_container_id,'integer').' '.
504  'AND user_id = '.$ilDB->quote($a_user_id,'integer');
505 
506  $res = $ilDB->query($query);
507  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
508  {
509  return true;
510  }
511  return false;
512  }
513 }
514 
515 ?>
const LP_STATUS_COMPLETED_NUM
static getObjectiveStatusForLP($a_user_id, $a_obj_id, array $a_objective_ids)
static getInstanceByObjId($a_obj_id)
get singleton instance
static resetFinalByObjective($a_objective_id)
getCompletedObjectiveIdsByType($a_type)
All completed objectives by type.
static getSummarizedObjectiveStatusForLP($a_obj_id, array $a_objective_ids, $a_user_id=null)
static lookupResult($a_course_obj_id, $a_user_id, $a_objective_id, $a_tst_type)
Lookup user result.
const LP_STATUS_IN_PROGRESS_NUM
static deleteResultsForUser($a_user_id)
Delete all result entries for user.
getSuggestedObjectiveIds()
Get all objectives where the user failed the initial test.
const DB_FETCHMODE_OBJECT
Definition: class.ilDB.php:11
getCompletedObjectiveIds()
Get all objectives where the user completed the qualified test.
saveObjectiveResult($a_objective_id, $a_type, $a_status, $a_result_percentage, $a_limit_percentage, $a_tries, $a_is_final)
Save objective result.
static deleteResultsForCourse($a_course_id)
Delete all result entries for course.
static deleteResultsFromLP($a_course_id, array $a_user_ids, $a_remove_initial, $a_remove_qualified)
Delete all (qualified) result entries for course members.
getFailedObjectiveIds($a_is_final=true)
Get all objectives where the user failed the qualified test.
findObjectiveIds($a_type=null, $a_status=null, $a_is_final=null)
Find objective ids by type and/or status.
global $ilDB
static isValidStatus($a_status)
Is given status valid?
$GLOBALS['PHPCAS_CLIENT']
This global variable is used by the interface class phpCAS.
Definition: CAS.php:276
static isValidType($a_type)
Is given type valid?
getCourseResultsForUserPresentation()
Get all results for course and user.
static hasResults($a_container_id, $a_user_id)
const LP_STATUS_FAILED_NUM
__construct($a_course_obj_id, $a_user_id)
Constructor.