ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
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  return $this->findObjectiveIds(self::TYPE_QUALIFIED, self::STATUS_COMPLETED);
300  }
301 
308  public function getFailedObjectiveIds($a_is_final = true)
309  {
310  return $this->findObjectiveIds(self::TYPE_QUALIFIED, self::STATUS_FAILED, $a_is_final);
311  }
312 
319  {
320  global $ilDB;
321 
322  $res = array();
323 
324  $set = $ilDB->query("SELECT *".
325  " FROM loc_user_results".
326  " WHERE course_id = ".$ilDB->quote($this->course_obj_id, "integer").
327  " AND user_id = ".$ilDB->quote($this->user_id, "integer"));
328  while($row = $ilDB->fetchAssoc($set))
329  {
330  $objective_id = $row["objective_id"];
331  $type = $row["type"];
332  unset($row["objective_id"]);
333  unset($row["type"]);
334  $res[$objective_id][$type] = $row;
335  }
336 
337  return $res;
338  }
339 
340  public static function getObjectiveStatusForLP($a_user_id, array $a_objective_ids)
341  {
342  global $ilDB;
343 
344  // this method returns LP status codes!
345  include_once "Services/Tracking/classes/class.ilLPStatus.php";
346 
347  $res = array();
348 
349  $sql = "SELECT lor.objective_id, lor.user_id, lor.status, lor.is_final".
350  " FROM loc_user_results lor".
351  " JOIN crs_objectives cobj ON (cobj.objective_id = lor.objective_id)".
352  " WHERE ".$ilDB->in("lor.objective_id", $a_objective_ids, "", "integer").
353  " AND lor.type = ".$ilDB->quote(self::TYPE_QUALIFIED, "integer").
354  " AND lor.user_id = ".$ilDB->quote($a_user_id, "integer").
355  " AND cobj.active = ".$ilDB->quote(1, "integer");
356  $set = $ilDB->query($sql);
357  while($row = $ilDB->fetchAssoc($set))
358  {
359  switch($row["status"])
360  {
361  case self::STATUS_FAILED:
363  break;
364 
365  case self::STATUS_COMPLETED:
367  break;
368 
369  default:
370  /*
371  $status = ilLPStatus::LP_STATUS_NOT_ATTEMPTED_NUM;
372  break;
373  */
374  continue;
375  }
376 
377  $res[$row["objective_id"]] = $status;
378  }
379 
380  return $res;
381  }
382 
383  public static function getSummarizedObjectiveStatusForLP(array $a_objective_ids, $a_user_id = null)
384  {
385  global $ilDB;
386 
387  // this method returns LP status codes!
388  include_once "Services/Tracking/classes/class.ilLPStatus.php";
389 
390  $res = $tmp_completed = array();
391 
392  $sql = "SELECT lor.objective_id, lor.user_id, lor.status, lor.is_final".
393  " FROM loc_user_results lor".
394  " JOIN crs_objectives cobj ON (cobj.objective_id = lor.objective_id)".
395  " WHERE ".$ilDB->in("lor.objective_id", $a_objective_ids, "", "integer").
396  " AND lor.type = ".$ilDB->quote(self::TYPE_QUALIFIED, "integer").
397  " AND cobj.active = ".$ilDB->quote(1, "integer");
398  if($a_user_id)
399  {
400  $sql .= " AND lor.user_id = ".$ilDB->quote($a_user_id, "integer");
401  }
402  $set = $ilDB->query($sql);
403  while($row = $ilDB->fetchAssoc($set))
404  {
405  $user_id = (int)$row["user_id"];
406  $status = (int)$row["status"];
407 
408  // user did do something
410 
411  switch($status)
412  {
413  case self::STATUS_COMPLETED:
414  $tmp_completed[$user_id]++;
415  break;
416 
417  case self::STATUS_FAILED:
418  if((bool)$row["is_final"])
419  {
420  // object is failed when at least 1 objective is failed without any tries left
422  }
423  break;
424  }
425  }
426 
427  $all_nr = sizeof($a_objective_ids);
428  foreach($tmp_completed as $user_id => $counter)
429  {
430  // if used as precondition object should be completed ASAP, status can be lost on subsequent tries
431  if($counter == $all_nr)
432  {
434  }
435  }
436 
437  if($a_user_id)
438  {
439  // might return null!
440  return $res[$a_user_id];
441  }
442  else
443  {
444  return $res;
445  }
446  }
447 
448 
449  public static function hasResults($a_container_id, $a_user_id)
450  {
451  global $ilDB;
452 
453  $query = 'SELECT objective_id FROM loc_user_results '.
454  'WHERE course_id = '.$ilDB->quote($a_container_id,'integer').' '.
455  'AND user_id = '.$ilDB->quote($a_user_id,'integer');
456 
457  $res = $ilDB->query($query);
458  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
459  {
460  return true;
461  }
462  return false;
463  }
464 }
465 
466 ?>