ILIAS  Release_3_10_x_branch Revision 61812
 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);
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)." ";
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()).
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  $query = "INSERT INTO crs_objectives ".
177  "SET crs_id = ".$ilDB->quote($this->course_obj->getId()).", ".
178  "title = ".$ilDB->quote($this->getTitle()).", ".
179  "description = ".$ilDB->quote($this->getDescription()).", ".
180  "position = ".$ilDB->quote($this->__getLastPosition() + 1).", ".
181  "created = ".$ilDB->quote(time());
182 
183  $this->db->query($query);
184 
185  return $this->objective_id = $this->db->getLastInsertId();
186  }
187 
188  function update()
189  {
190  global $ilDB;
191 
192  $query = "UPDATE crs_objectives ".
193  "SET title = ".$ilDB->quote($this->getTitle()).", ".
194  "description = ".$ilDB->quote($this->getDescription())." ".
195  "WHERE objective_id = ".$ilDB->quote($this->getObjectiveId())." ".
196  "AND crs_id = ".$ilDB->quote($this->course_obj->getId())."";
197 
198  $this->db->query($query);
199 
200  return true;
201  }
202 
210  public function writePosition($a_position)
211  {
212  $query = "UPDATE crs_objectives ".
213  "SET position = ".$this->db->quote((string) $a_position)." ".
214  "WHERE objective_id = ".$this->db->quote($this->getObjectiveId())." ";
215  $this->db->query($query);
216  }
217 
225  public function validate()
226  {
227  return (bool) strlen($this->getTitle());
228  }
229 
230  function delete()
231  {
232  global $ilDB;
233 
234  include_once './Modules/Course/classes/class.ilCourseObjectiveQuestion.php';
235 
236  $tmp_obj_qst =& new ilCourseObjectiveQuestion($this->getObjectiveId());
237  $tmp_obj_qst->deleteAll();
238 
239  include_once './Modules/Course/classes/class.ilCourseObjectiveMaterials.php';
240 
241  $tmp_obj_lm =& new ilCourseObjectiveMaterials($this->getObjectiveId());
242  $tmp_obj_lm->deleteAll();
243 
244 
245  $query = "DELETE FROM crs_objectives ".
246  "WHERE crs_id = ".$ilDB->quote($this->course_obj->getId())." ".
247  "AND objective_id = ".$ilDB->quote($this->getObjectiveId())." ";
248 
249  $this->db->query($query);
250 
251  return true;
252  }
253 
254  function moveUp()
255  {
256  global $ilDB;
257 
258  if(!$this->getObjectiveId())
259  {
260  return false;
261  }
262  // Stop if position is first
263  if($this->__getPosition() == 1)
264  {
265  return false;
266  }
267 
268  $query = "UPDATE crs_objectives ".
269  "SET position = position + 1 ".
270  "WHERE position = ".$ilDB->quote($this->__getPosition() - 1)." ".
271  "AND crs_id = ".$ilDB->quote($this->course_obj->getId())." ";
272 
273  $this->db->query($query);
274 
275  $query = "UPDATE crs_objectives ".
276  "SET position = position - 1 ".
277  "WHERE objective_id = ".$ilDB->quote($this->getObjectiveId())." ".
278  "AND crs_id = ".$ilDB->quote($this->course_obj->getId())." ";
279 
280  $this->db->query($query);
281 
282  $this->__read();
283 
284  return true;
285  }
286 
287  function moveDown()
288  {
289  global $ilDB;
290 
291  if(!$this->getObjectiveId())
292  {
293  return false;
294  }
295  // Stop if position is last
296  if($this->__getPosition() == $this->__getLastPosition())
297  {
298  return false;
299  }
300 
301  $query = "UPDATE crs_objectives ".
302  "SET position = position - 1 ".
303  "WHERE position = ".$ilDB->quote($this->__getPosition() + 1)." ".
304  "AND crs_id = ".$ilDB->quote($this->course_obj->getId())." ";
305 
306  $this->db->query($query);
307 
308  $query = "UPDATE crs_objectives ".
309  "SET position = position + 1 ".
310  "WHERE objective_id = ".$ilDB->quote($this->getObjectiveId())." ".
311  "AND crs_id = ".$ilDB->quote($this->course_obj->getId())." ";
312 
313  $this->db->query($query);
314 
315  $this->__read();
316 
317  return true;
318  }
319 
320  // PRIVATE
321  function __setPosition($a_position)
322  {
323  $this->position = $a_position;
324  }
325  function __getPosition()
326  {
327  return $this->position;
328  }
329  function __setCreated($a_created)
330  {
331  $this->created = $a_created;
332  }
333  function __getCreated()
334  {
335  return $this->created;
336  }
337 
338 
339  function __read()
340  {
341  global $ilDB;
342 
343  if($this->getObjectiveId())
344  {
345  $query = "SELECT * FROM crs_objectives ".
346  "WHERE crs_id = ".$ilDB->quote($this->course_obj->getId())." ".
347  "AND objective_id = ".$ilDB->quote($this->getObjectiveId())." ";
348 
349 
350  $res = $this->db->query($query);
351  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
352  {
353  $this->setObjectiveId($row->objective_id);
354  $this->setTitle($row->title);
355  $this->setDescription($row->description);
356  $this->__setPosition($row->position);
357  $this->__setCreated($row->created);
358  }
359  return true;
360  }
361  return false;
362  }
363 
364  function __getOrderColumn()
365  {
366  switch($this->course_obj->getOrderType())
367  {
369  return 'ORDER BY position';
370 
372  return 'ORDER BY title';
373 
375  return 'ORDER BY create';
376  }
377  return false;
378  }
379 
380  function __updateTop()
381  {
382  global $ilDB;
383 
384  $query = "UPDATE crs_objectives ".
385  "SET position = position - 1 ".
386  "WHERE position > ".$ilDB->quote($this->__getPosition())." ".
387  "AND crs_id = ".$ilDB->quote($this->course_obj->getId())." ";
388 
389  $this->db->query($query);
390 
391  return true;
392  }
393 
394  function __getLastPosition()
395  {
396  global $ilDB;
397 
398  $query = "SELECT MAX(position) AS pos FROM crs_objectives ".
399  "WHERE crs_id = ".$ilDB->quote($this->course_obj->getId())." ";
400 
401  $res = $this->db->query($query);
402  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
403  {
404  return $row->pos;
405  }
406  return 0;
407  }
408 
409  // STATIC
410  function _getObjectiveIds($course_id)
411  {
412  global $ilDB;
413 
414  $query = "SELECT objective_id FROM crs_objectives ".
415  "WHERE crs_id = ".$ilDB->quote($course_id)." ".
416  "ORDER BY position";
417 
418  $res = $ilDB->query($query);
419  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
420  {
421  $ids[] = $row->objective_id;
422  }
423 
424  return $ids ? $ids : array();
425  }
426 
427  function _deleteAll($course_id)
428  {
429  global $ilDB;
430 
431  $ids = ilCourseObjective::_getObjectiveIds($course_id);
432 
433  if(!count($ids))
434  {
435  return true;
436  }
437  $in = "IN (";
438  $in .= implode(",",ilUtil::quoteArray($ids));
439  $in .= ")";
440 
441  $query = "DELETE FROM crs_objective_lm WHERE objective_id ".$in;
442  $ilDB->query($query);
443 
444  $query = "DELETE FROM crs_objective_tst WHERE objective_id ".$in;
445  $ilDB->query($query);
446 
447  $query = "DELETE FROM crs_objective_qst WHERE objective_id ".$in;
448  $ilDB->query($query);
449 
450  $query = "DELETE FROM crs_objectives WHERE crs_id = ".$ilDB->quote($course_id);
451  $ilDB->query($query);
452 
453  return true;
454  }
455 }
456 ?>