ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilCourseObjective.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 
35 {
36  var $db = null;
37 
38  var $course_obj = null;
39  var $objective_id = null;
40 
41  function ilCourseObjective(&$course_obj,$a_objective_id = 0)
42  {
43  global $ilDB;
44 
45  $this->db =& $ilDB;
46  $this->course_obj =& $course_obj;
47 
48  $this->objective_id = $a_objective_id;
49  if($this->objective_id)
50  {
51  $this->__read();
52  }
53  }
54 
63  public static function _lookupContainerIdByObjectiveId($a_objective_id)
64  {
65  global $ilDB;
66 
67  $query = "SELECT crs_id FROM crs_objectives ".
68  "WHERE objective_id = ".$ilDB->quote($a_objective_id ,'integer');
69  $res = $ilDB->query($query);
70  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
71  {
72  return $row->crs_id;
73  }
74  return false;
75  }
76 
85  public static function _getCountObjectives($a_obj_id)
86  {
87  global $ilDB;
88 
89  $query = "SELECT * FROM crs_objectives ".
90  "WHERE crs_id = ".$ilDB->quote($a_obj_id ,'integer')." ";
91  $res = $ilDB->query($query);
92  return $res->numRows() ? true : false;
93  }
94 
103  public function ilClone($a_target_id,$a_copy_id)
104  {
105  global $ilLog;
106 
107  $ilLog->write(__METHOD__.': Start cloning learning objectives...');
108 
109  $query = "SELECT * FROM crs_objectives ".
110  "WHERE crs_id = ".$this->db->quote($this->course_obj->getId() ,'integer').' '.
111  "ORDER BY position ";
112  $res = $this->db->query($query);
113  if(!$res->numRows())
114  {
115  $ilLog->write(__METHOD__.': ... no objectives found.');
116  return true;
117  }
118 
119  if(!is_object($new_course = ilObjectFactory::getInstanceByRefId($a_target_id,false)))
120  {
121  $ilLog->write(__METHOD__.': Cannot init new course object.');
122  return true;
123  }
124  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
125  {
126  $new_objective = new ilCourseObjective($new_course);
127  $new_objective->setTitle($row->title);
128  $new_objective->setDescription($row->description);
129  $objective_id = $new_objective->add();
130  $ilLog->write(__METHOD__.': Added new objective nr: '.$objective_id);
131 
132  // Clone crs_objective_tst entries
133  include_once('Modules/Course/classes/class.ilCourseObjectiveQuestion.php');
134  $objective_qst = new ilCourseObjectiveQuestion($row->objective_id);
135  $objective_qst->cloneDependencies($objective_id,$a_copy_id);
136 
137  $ilLog->write(__METHOD__.': Finished objective question dependencies: '.$objective_id);
138 
139  // Clone crs_objective_lm entries (assigned course materials)
140  include_once('Modules/Course/classes/class.ilCourseObjectiveMaterials.php');
141  $objective_material = new ilCourseObjectiveMaterials($row->objective_id);
142  $objective_material->cloneDependencies($objective_id,$a_copy_id);
143  }
144  $ilLog->write(__METHOD__.': Finished cloning objectives.');
145  }
146 
147  function setTitle($a_title)
148  {
149  $this->title = $a_title;
150  }
151  function getTitle()
152  {
153  return $this->title;
154  }
155  function setDescription($a_description)
156  {
157  $this->description = $a_description;
158  }
159  function getDescription()
160  {
161  return $this->description;
162  }
163  function setObjectiveId($a_objective_id)
164  {
165  $this->objective_id = $a_objective_id;
166  }
167  function getObjectiveId()
168  {
169  return $this->objective_id;
170  }
171 
172  function add()
173  {
174  global $ilDB;
175 
176  $next_id = $ilDB->nextId('crs_objectives');
177  $query = "INSERT INTO crs_objectives (crs_id,objective_id,title,description,position,created) ".
178  "VALUES( ".
179  $ilDB->quote($this->course_obj->getId() ,'integer').", ".
180  $ilDB->quote($next_id,'integer').", ".
181  $ilDB->quote($this->getTitle() ,'text').", ".
182  $ilDB->quote($this->getDescription() ,'text').", ".
183  $ilDB->quote($this->__getLastPosition() + 1 ,'integer').", ".
184  $ilDB->quote(time() ,'integer')." ".
185  ")";
186  $res = $ilDB->manipulate($query);
187 
188  return $this->objective_id = $next_id;
189  }
190 
191  function update()
192  {
193  global $ilDB;
194 
195  $query = "UPDATE crs_objectives ".
196  "SET title = ".$ilDB->quote($this->getTitle() ,'text').", ".
197  "description = ".$ilDB->quote($this->getDescription() ,'text')." ".
198  "WHERE objective_id = ".$ilDB->quote($this->getObjectiveId() ,'integer')." ".
199  "AND crs_id = ".$ilDB->quote($this->course_obj->getId() ,'integer')."";
200  $res = $ilDB->manipulate($query);
201 
202  return true;
203  }
204 
212  public function writePosition($a_position)
213  {
214  global $ilDB;
215 
216  $query = "UPDATE crs_objectives ".
217  "SET position = ".$this->db->quote((string) $a_position ,'integer')." ".
218  "WHERE objective_id = ".$this->db->quote($this->getObjectiveId() ,'integer')." ";
219  $res = $ilDB->manipulate($query);
220  }
221 
229  public function validate()
230  {
231  return (bool) strlen($this->getTitle());
232  }
233 
234  function delete()
235  {
236  global $ilDB;
237 
238  include_once './Modules/Course/classes/class.ilCourseObjectiveQuestion.php';
239 
240  $tmp_obj_qst =& new ilCourseObjectiveQuestion($this->getObjectiveId());
241  $tmp_obj_qst->deleteAll();
242 
243  include_once './Modules/Course/classes/class.ilCourseObjectiveMaterials.php';
244 
245  $tmp_obj_lm =& new ilCourseObjectiveMaterials($this->getObjectiveId());
246  $tmp_obj_lm->deleteAll();
247 
248 
249  $query = "DELETE FROM crs_objectives ".
250  "WHERE crs_id = ".$ilDB->quote($this->course_obj->getId() ,'integer')." ".
251  "AND objective_id = ".$ilDB->quote($this->getObjectiveId() ,'integer')." ";
252  $res = $ilDB->manipulate($query);
253 
254  return true;
255  }
256 
257  function moveUp()
258  {
259  global $ilDB;
260 
261  if(!$this->getObjectiveId())
262  {
263  return false;
264  }
265  // Stop if position is first
266  if($this->__getPosition() == 1)
267  {
268  return false;
269  }
270 
271  $query = "UPDATE crs_objectives ".
272  "SET position = position + 1 ".
273  "WHERE position = ".$ilDB->quote($this->__getPosition() - 1 ,'integer')." ".
274  "AND crs_id = ".$ilDB->quote($this->course_obj->getId() ,'integer')." ";
275  $res = $ilDB->manipulate($query);
276 
277  $query = "UPDATE crs_objectives ".
278  "SET position = position - 1 ".
279  "WHERE objective_id = ".$ilDB->quote($this->getObjectiveId() ,'integer')." ".
280  "AND crs_id = ".$ilDB->quote($this->course_obj->getId() ,'integer')." ";
281  $res = $ilDB->manipulate($query);
282 
283  $this->__read();
284 
285  return true;
286  }
287 
288  function moveDown()
289  {
290  global $ilDB;
291 
292  if(!$this->getObjectiveId())
293  {
294  return false;
295  }
296  // Stop if position is last
297  if($this->__getPosition() == $this->__getLastPosition())
298  {
299  return false;
300  }
301 
302  $query = "UPDATE crs_objectives ".
303  "SET position = position - 1 ".
304  "WHERE position = ".$ilDB->quote($this->__getPosition() + 1 ,'integer')." ".
305  "AND crs_id = ".$ilDB->quote($this->course_obj->getId() ,'integer')." ";
306  $res = $ilDB->manipulate($query);
307 
308  $query = "UPDATE crs_objectives ".
309  "SET position = position + 1 ".
310  "WHERE objective_id = ".$ilDB->quote($this->getObjectiveId() ,'integer')." ".
311  "AND crs_id = ".$ilDB->quote($this->course_obj->getId() ,'integer')." ";
312  $res = $ilDB->manipulate($query);
313 
314  $this->__read();
315 
316  return true;
317  }
318 
319  // PRIVATE
320  function __setPosition($a_position)
321  {
322  $this->position = $a_position;
323  }
324  function __getPosition()
325  {
326  return $this->position;
327  }
328  function __setCreated($a_created)
329  {
330  $this->created = $a_created;
331  }
332  function __getCreated()
333  {
334  return $this->created;
335  }
336 
337 
338  function __read()
339  {
340  global $ilDB;
341 
342  if($this->getObjectiveId())
343  {
344  $query = "SELECT * FROM crs_objectives ".
345  "WHERE crs_id = ".$ilDB->quote($this->course_obj->getId() ,'integer')." ".
346  "AND objective_id = ".$ilDB->quote($this->getObjectiveId() ,'integer')." ";
347 
348 
349  $res = $this->db->query($query);
350  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
351  {
352  $this->setObjectiveId($row->objective_id);
353  $this->setTitle($row->title);
354  $this->setDescription($row->description);
355  $this->__setPosition($row->position);
356  $this->__setCreated($row->created);
357  }
358  return true;
359  }
360  return false;
361  }
362 
363  function __getOrderColumn()
364  {
365  switch($this->course_obj->getOrderType())
366  {
368  return 'ORDER BY position';
369 
371  return 'ORDER BY title';
372 
374  return 'ORDER BY create';
375  }
376  return false;
377  }
378 
379  function __updateTop()
380  {
381  global $ilDB;
382 
383  $query = "UPDATE crs_objectives ".
384  "SET position = position - 1 ".
385  "WHERE position > ".$ilDB->quote($this->__getPosition() ,'integer')." ".
386  "AND crs_id = ".$ilDB->quote($this->course_obj->getId() ,'integer')." ";
387  $res = $ilDB->manipulate($query);
388 
389  return true;
390  }
391 
392  function __getLastPosition()
393  {
394  global $ilDB;
395 
396  $query = "SELECT MAX(position) pos FROM crs_objectives ".
397  "WHERE crs_id = ".$ilDB->quote($this->course_obj->getId() ,'integer')." ";
398 
399  $res = $this->db->query($query);
400  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
401  {
402  return $row->pos;
403  }
404  return 0;
405  }
406 
407  // STATIC
408  function _getObjectiveIds($course_id)
409  {
410  global $ilDB;
411 
412  $query = "SELECT objective_id FROM crs_objectives ".
413  "WHERE crs_id = ".$ilDB->quote($course_id ,'integer')." ".
414  "ORDER BY position";
415 
416  $res = $ilDB->query($query);
417  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
418  {
419  $ids[] = $row->objective_id;
420  }
421 
422  return $ids ? $ids : array();
423  }
424 
425  function _deleteAll($course_id)
426  {
427  global $ilDB;
428 
429  $ids = ilCourseObjective::_getObjectiveIds($course_id);
430 
431  if(!count($ids))
432  {
433  return true;
434  }
435 
436  $in = $ilDB->in('objective_id',$ids,false,'integer');
437 
438 
439  $query = "DELETE FROM crs_objective_lm WHERE ".$in;
440  $res = $ilDB->manipulate($query);
441 
442  $query = "DELETE FROM crs_objective_tst WHERE ".$in;
443  $res = $ilDB->manipulate($query);
444 
445  $query = "DELETE FROM crs_objective_qst WHERE ".$in;
446  $res = $ilDB->manipulate($query);
447 
448  $query = "DELETE FROM crs_objectives WHERE crs_id = ".$ilDB->quote($course_id ,'integer');
449  $res = $ilDB->manipulate($query);
450 
451  return true;
452  }
453 }
454 ?>