ILIAS  Release_4_1_x_branch Revision 61804
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilCourseObjective.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
14 {
15  var $db = null;
16 
17  var $course_obj = null;
18  var $objective_id = null;
19 
20  function ilCourseObjective(&$course_obj,$a_objective_id = 0)
21  {
22  global $ilDB;
23 
24  $this->db =& $ilDB;
25  $this->course_obj =& $course_obj;
26 
27  $this->objective_id = $a_objective_id;
28  if($this->objective_id)
29  {
30  $this->__read();
31  }
32  }
33 
42  public static function _lookupContainerIdByObjectiveId($a_objective_id)
43  {
44  global $ilDB;
45 
46  $query = "SELECT crs_id FROM crs_objectives ".
47  "WHERE objective_id = ".$ilDB->quote($a_objective_id ,'integer');
48  $res = $ilDB->query($query);
49  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
50  {
51  return $row->crs_id;
52  }
53  return false;
54  }
55 
64  public static function _getCountObjectives($a_obj_id)
65  {
66  global $ilDB;
67 
68  $query = "SELECT * FROM crs_objectives ".
69  "WHERE crs_id = ".$ilDB->quote($a_obj_id ,'integer')." ";
70  $res = $ilDB->query($query);
71  return $res->numRows() ? true : false;
72  }
73 
82  public function ilClone($a_target_id,$a_copy_id)
83  {
84  global $ilLog;
85 
86  $ilLog->write(__METHOD__.': Start cloning learning objectives...');
87 
88  $query = "SELECT * FROM crs_objectives ".
89  "WHERE crs_id = ".$this->db->quote($this->course_obj->getId() ,'integer').' '.
90  "ORDER BY position ";
91  $res = $this->db->query($query);
92  if(!$res->numRows())
93  {
94  $ilLog->write(__METHOD__.': ... no objectives found.');
95  return true;
96  }
97 
98  if(!is_object($new_course = ilObjectFactory::getInstanceByRefId($a_target_id,false)))
99  {
100  $ilLog->write(__METHOD__.': Cannot init new course object.');
101  return true;
102  }
103  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
104  {
105  $new_objective = new ilCourseObjective($new_course);
106  $new_objective->setTitle($row->title);
107  $new_objective->setDescription($row->description);
108  $objective_id = $new_objective->add();
109  $ilLog->write(__METHOD__.': Added new objective nr: '.$objective_id);
110 
111  // Clone crs_objective_tst entries
112  include_once('Modules/Course/classes/class.ilCourseObjectiveQuestion.php');
113  $objective_qst = new ilCourseObjectiveQuestion($row->objective_id);
114  $objective_qst->cloneDependencies($objective_id,$a_copy_id);
115 
116  $ilLog->write(__METHOD__.': Finished objective question dependencies: '.$objective_id);
117 
118  // Clone crs_objective_lm entries (assigned course materials)
119  include_once('Modules/Course/classes/class.ilCourseObjectiveMaterials.php');
120  $objective_material = new ilCourseObjectiveMaterials($row->objective_id);
121  $objective_material->cloneDependencies($objective_id,$a_copy_id);
122  }
123  $ilLog->write(__METHOD__.': Finished cloning objectives.');
124  }
125 
126  function setTitle($a_title)
127  {
128  $this->title = $a_title;
129  }
130  function getTitle()
131  {
132  return $this->title;
133  }
134  function setDescription($a_description)
135  {
136  $this->description = $a_description;
137  }
138  function getDescription()
139  {
140  return $this->description;
141  }
142  function setObjectiveId($a_objective_id)
143  {
144  $this->objective_id = $a_objective_id;
145  }
146  function getObjectiveId()
147  {
148  return $this->objective_id;
149  }
150 
151  function add()
152  {
153  global $ilDB;
154 
155  $next_id = $ilDB->nextId('crs_objectives');
156  $query = "INSERT INTO crs_objectives (crs_id,objective_id,title,description,position,created) ".
157  "VALUES( ".
158  $ilDB->quote($this->course_obj->getId() ,'integer').", ".
159  $ilDB->quote($next_id,'integer').", ".
160  $ilDB->quote($this->getTitle() ,'text').", ".
161  $ilDB->quote($this->getDescription() ,'text').", ".
162  $ilDB->quote($this->__getLastPosition() + 1 ,'integer').", ".
163  $ilDB->quote(time() ,'integer')." ".
164  ")";
165  $res = $ilDB->manipulate($query);
166 
167  // refresh learning progress status after adding new objective
168  include_once("./Services/Tracking/classes/class.ilLPStatusWrapper.php");
169  ilLPStatusWrapper::_refreshStatus($this->course_obj->getId());
170 
171  return $this->objective_id = $next_id;
172  }
173 
174  function update()
175  {
176  global $ilDB;
177 
178  $query = "UPDATE crs_objectives ".
179  "SET title = ".$ilDB->quote($this->getTitle() ,'text').", ".
180  "description = ".$ilDB->quote($this->getDescription() ,'text')." ".
181  "WHERE objective_id = ".$ilDB->quote($this->getObjectiveId() ,'integer')." ".
182  "AND crs_id = ".$ilDB->quote($this->course_obj->getId() ,'integer')."";
183  $res = $ilDB->manipulate($query);
184 
185  return true;
186  }
187 
195  public function writePosition($a_position)
196  {
197  global $ilDB;
198 
199  $query = "UPDATE crs_objectives ".
200  "SET position = ".$this->db->quote((string) $a_position ,'integer')." ".
201  "WHERE objective_id = ".$this->db->quote($this->getObjectiveId() ,'integer')." ";
202  $res = $ilDB->manipulate($query);
203  }
204 
212  public function validate()
213  {
214  return (bool) strlen($this->getTitle());
215  }
216 
217  function delete()
218  {
219  global $ilDB;
220 
221  include_once './Modules/Course/classes/class.ilCourseObjectiveQuestion.php';
222 
223  $tmp_obj_qst =& new ilCourseObjectiveQuestion($this->getObjectiveId());
224  $tmp_obj_qst->deleteAll();
225 
226  include_once './Modules/Course/classes/class.ilCourseObjectiveMaterials.php';
227 
228  $tmp_obj_lm =& new ilCourseObjectiveMaterials($this->getObjectiveId());
229  $tmp_obj_lm->deleteAll();
230 
231 
232  $query = "DELETE FROM crs_objectives ".
233  "WHERE crs_id = ".$ilDB->quote($this->course_obj->getId() ,'integer')." ".
234  "AND objective_id = ".$ilDB->quote($this->getObjectiveId() ,'integer')." ";
235  $res = $ilDB->manipulate($query);
236 
237  // refresh learning progress status after deleting objective
238  include_once("./Services/Tracking/classes/class.ilLPStatusWrapper.php");
239  ilLPStatusWrapper::_refreshStatus($this->course_obj->getId());
240 
241  return true;
242  }
243 
244  function moveUp()
245  {
246  global $ilDB;
247 
248  if(!$this->getObjectiveId())
249  {
250  return false;
251  }
252  // Stop if position is first
253  if($this->__getPosition() == 1)
254  {
255  return false;
256  }
257 
258  $query = "UPDATE crs_objectives ".
259  "SET position = position + 1 ".
260  "WHERE position = ".$ilDB->quote($this->__getPosition() - 1 ,'integer')." ".
261  "AND crs_id = ".$ilDB->quote($this->course_obj->getId() ,'integer')." ";
262  $res = $ilDB->manipulate($query);
263 
264  $query = "UPDATE crs_objectives ".
265  "SET position = position - 1 ".
266  "WHERE objective_id = ".$ilDB->quote($this->getObjectiveId() ,'integer')." ".
267  "AND crs_id = ".$ilDB->quote($this->course_obj->getId() ,'integer')." ";
268  $res = $ilDB->manipulate($query);
269 
270  $this->__read();
271 
272  return true;
273  }
274 
275  function moveDown()
276  {
277  global $ilDB;
278 
279  if(!$this->getObjectiveId())
280  {
281  return false;
282  }
283  // Stop if position is last
284  if($this->__getPosition() == $this->__getLastPosition())
285  {
286  return false;
287  }
288 
289  $query = "UPDATE crs_objectives ".
290  "SET position = position - 1 ".
291  "WHERE position = ".$ilDB->quote($this->__getPosition() + 1 ,'integer')." ".
292  "AND crs_id = ".$ilDB->quote($this->course_obj->getId() ,'integer')." ";
293  $res = $ilDB->manipulate($query);
294 
295  $query = "UPDATE crs_objectives ".
296  "SET position = position + 1 ".
297  "WHERE objective_id = ".$ilDB->quote($this->getObjectiveId() ,'integer')." ".
298  "AND crs_id = ".$ilDB->quote($this->course_obj->getId() ,'integer')." ";
299  $res = $ilDB->manipulate($query);
300 
301  $this->__read();
302 
303  return true;
304  }
305 
306  // PRIVATE
307  function __setPosition($a_position)
308  {
309  $this->position = $a_position;
310  }
311  function __getPosition()
312  {
313  return $this->position;
314  }
315  function __setCreated($a_created)
316  {
317  $this->created = $a_created;
318  }
319  function __getCreated()
320  {
321  return $this->created;
322  }
323 
324 
325  function __read()
326  {
327  global $ilDB;
328 
329  if($this->getObjectiveId())
330  {
331  $query = "SELECT * FROM crs_objectives ".
332  "WHERE crs_id = ".$ilDB->quote($this->course_obj->getId() ,'integer')." ".
333  "AND objective_id = ".$ilDB->quote($this->getObjectiveId() ,'integer')." ";
334 
335 
336  $res = $this->db->query($query);
337  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
338  {
339  $this->setObjectiveId($row->objective_id);
340  $this->setTitle($row->title);
341  $this->setDescription($row->description);
342  $this->__setPosition($row->position);
343  $this->__setCreated($row->created);
344  }
345  return true;
346  }
347  return false;
348  }
349 
350  function __getOrderColumn()
351  {
352  switch($this->course_obj->getOrderType())
353  {
355  return 'ORDER BY position';
356 
358  return 'ORDER BY title';
359 
361  return 'ORDER BY create';
362  }
363  return false;
364  }
365 
366  function __updateTop()
367  {
368  global $ilDB;
369 
370  $query = "UPDATE crs_objectives ".
371  "SET position = position - 1 ".
372  "WHERE position > ".$ilDB->quote($this->__getPosition() ,'integer')." ".
373  "AND crs_id = ".$ilDB->quote($this->course_obj->getId() ,'integer')." ";
374  $res = $ilDB->manipulate($query);
375 
376  return true;
377  }
378 
379  function __getLastPosition()
380  {
381  global $ilDB;
382 
383  $query = "SELECT MAX(position) pos FROM crs_objectives ".
384  "WHERE crs_id = ".$ilDB->quote($this->course_obj->getId() ,'integer')." ";
385 
386  $res = $this->db->query($query);
387  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
388  {
389  return $row->pos;
390  }
391  return 0;
392  }
393 
394  // STATIC
395  function _getObjectiveIds($course_id)
396  {
397  global $ilDB;
398 
399  $query = "SELECT objective_id FROM crs_objectives ".
400  "WHERE crs_id = ".$ilDB->quote($course_id ,'integer')." ".
401  "ORDER BY position";
402 
403  $res = $ilDB->query($query);
404  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
405  {
406  $ids[] = $row->objective_id;
407  }
408 
409  return $ids ? $ids : array();
410  }
411 
412  function _deleteAll($course_id)
413  {
414  global $ilDB;
415 
416  $ids = ilCourseObjective::_getObjectiveIds($course_id);
417 
418  if(!count($ids))
419  {
420  return true;
421  }
422 
423  $in = $ilDB->in('objective_id',$ids,false,'integer');
424 
425 
426  $query = "DELETE FROM crs_objective_lm WHERE ".$in;
427  $res = $ilDB->manipulate($query);
428 
429  $query = "DELETE FROM crs_objective_tst WHERE ".$in;
430  $res = $ilDB->manipulate($query);
431 
432  $query = "DELETE FROM crs_objective_qst WHERE ".$in;
433  $res = $ilDB->manipulate($query);
434 
435  $query = "DELETE FROM crs_objectives WHERE crs_id = ".$ilDB->quote($course_id ,'integer');
436  $res = $ilDB->manipulate($query);
437 
438  // refresh learning progress status after deleting objectives
439  include_once("./Services/Tracking/classes/class.ilLPStatusWrapper.php");
441 
442  return true;
443  }
444 }
445 ?>