ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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  // begin-patch lok
21  protected $active = true;
22  protected $passes = 0;
23  // end-patch lok
24 
25  function ilCourseObjective(&$course_obj,$a_objective_id = 0)
26  {
27  global $ilDB;
28 
29  $this->db =& $ilDB;
30  $this->course_obj =& $course_obj;
31 
32  $this->objective_id = $a_objective_id;
33  if($this->objective_id)
34  {
35  $this->__read();
36  }
37  }
38 
42  public function getCourse()
43  {
44  return $this->course_obj;
45  }
46 
55  public static function _lookupContainerIdByObjectiveId($a_objective_id)
56  {
57  global $ilDB;
58 
59  $query = "SELECT crs_id FROM crs_objectives ".
60  "WHERE objective_id = ".$ilDB->quote($a_objective_id ,'integer');
61  $res = $ilDB->query($query);
62  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
63  {
64  return $row->crs_id;
65  }
66  return false;
67  }
68 
77  // begin-patch lok
78  public static function _getCountObjectives($a_obj_id,$a_activated_only = false)
79  {
80  return count(ilCourseObjective::_getObjectiveIds($a_obj_id,$a_activated_only));
81  }
82 
83  public static function lookupMaxPasses($a_objective_id)
84  {
85  global $ilDB;
86 
87  $query = 'SELECT passes from crs_objectives '.
88  'WHERE objective_id = '.$ilDB->quote($a_objective_id,'integer');
89  $res = $ilDB->query($query);
90  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
91  {
92  return (int) $row->passes;
93  }
94  return 0;
95  }
96 
97  public static function lookupObjectiveTitle($a_objective_id, $a_add_description = false)
98  {
99  global $ilDB;
100 
101  $query = 'SELECT title,description from crs_objectives '.
102  'WHERE objective_id = '.$ilDB->quote($a_objective_id,'integer');
103  $res = $ilDB->query($query);
104  while($row = $ilDB->fetchAssoc($res))
105  {
106  if(!$a_add_description)
107  {
108  return $row['title'];
109  }
110  else
111  {
112  return $row;
113  }
114  }
115  return "";
116  }
117  // end-patch lok
118 
127  public function ilClone($a_target_id,$a_copy_id)
128  {
129  global $ilLog;
130 
131  ilLoggerFactory::getLogger('crs')->debug('Start cloning learning objectives');
132 
133  $query = "SELECT * FROM crs_objectives ".
134  "WHERE crs_id = ".$this->db->quote($this->course_obj->getId() ,'integer').' '.
135  "ORDER BY position ";
136  $res = $this->db->query($query);
137  if(!$res->numRows())
138  {
139  ilLoggerFactory::getLogger('crs')->debug('.. no objectives found');
140  return true;
141  }
142 
143  if(!is_object($new_course = ilObjectFactory::getInstanceByRefId($a_target_id,false)))
144  {
145  ilLoggerFactory::getLogger('crs')->warning('Cannot create course instance');
146  return true;
147  }
148  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
149  {
150  $new_objective = new ilCourseObjective($new_course);
151  $new_objective->setTitle($row->title);
152  $new_objective->setDescription($row->description);
153  $new_objective->setActive($row->active);
154  $objective_id = $new_objective->add();
155  ilLoggerFactory::getLogger('crs')->debug('Added new objective nr: ' . $objective_id);
156 
157  // Clone crs_objective_tst entries
158  include_once('Modules/Course/classes/class.ilCourseObjectiveQuestion.php');
159  $objective_qst = new ilCourseObjectiveQuestion($row->objective_id);
160  $objective_qst->cloneDependencies($objective_id,$a_copy_id);
161 
162  include_once './Modules/Course/classes/Objectives/class.ilLORandomTestQuestionPools.php';
163  include_once './Modules/Course/classes/Objectives/class.ilLOSettings.php';
164  $random_i = new ilLORandomTestQuestionPools(
165  $this->getCourse()->getId(),
166  $row->objective_id,
168  );
169  $random_i->copy($a_copy_id, $new_course->getId(), $objective_id);
170 
171  $random_q = new ilLORandomTestQuestionPools(
172  $this->getCourse()->getId(),
173  $row->objective_id,
175  );
176  $random_q->copy($a_copy_id, $new_course->getId(), $objective_id);
177 
178  include_once './Modules/Course/classes/Objectives/class.ilLOTestAssignments.php';
179  $assignments = ilLOTestAssignments::getInstance($this->course_obj->getId());
180  $assignment_it = $assignments->getAssignmentByObjective($row->objective_id, ilLOSettings::TYPE_TEST_INITIAL);
181  if($assignment_it)
182  {
183  $assignment_it->cloneSettings($a_copy_id, $new_course->getId(), $objective_id);
184  }
185 
186  $assignment_qt = $assignments->getAssignmentByObjective($row->objective_id, ilLOSettings::TYPE_TEST_QUALIFIED);
187  if($assignment_qt)
188  {
189  $assignment_qt->cloneSettings($a_copy_id, $new_course->getId(), $objective_id);
190  }
191 
192  ilLoggerFactory::getLogger('crs')->debug('Finished copying question dependencies');
193 
194  // Clone crs_objective_lm entries (assigned course materials)
195  include_once('Modules/Course/classes/class.ilCourseObjectiveMaterials.php');
196  $objective_material = new ilCourseObjectiveMaterials($row->objective_id);
197  $objective_material->cloneDependencies($objective_id,$a_copy_id);
198  }
199  ilLoggerFactory::getLogger('crs')->debug('Finished copying objectives');
200  }
201 
202  // begin-patch lok
203  public function setActive($a_stat)
204  {
205  $this->active = $a_stat;
206  }
207 
208  public function isActive()
209  {
210  return $this->active;
211  }
212 
213  public function setPasses($a_passes)
214  {
215  $this->passes = $a_passes;
216  }
217 
218  public function getPasses()
219  {
220  return $this->passes;
221  }
222 
223  public function arePassesLimited()
224  {
225  return $this->passes > 0;
226  }
227  // end-patch lok
228 
229  function setTitle($a_title)
230  {
231  $this->title = $a_title;
232  }
233  function getTitle()
234  {
235  return $this->title;
236  }
237  function setDescription($a_description)
238  {
239  $this->description = $a_description;
240  }
241  function getDescription()
242  {
243  return $this->description;
244  }
245  function setObjectiveId($a_objective_id)
246  {
247  $this->objective_id = $a_objective_id;
248  }
249  function getObjectiveId()
250  {
251  return $this->objective_id;
252  }
253 
254  // begin-patch optes_lok_export
255  public function setPosition($a_pos)
256  {
257  $this->position = $a_pos;
258  }
259  // end-patch optes_lok_export
260 
261  function add()
262  {
263  global $ilDB;
264 
265  // begin-patch lok
266  $next_id = $ilDB->nextId('crs_objectives');
267  $query = "INSERT INTO crs_objectives (crs_id,objective_id,active,title,description,position,created,passes) ".
268  "VALUES( ".
269  $ilDB->quote($this->course_obj->getId() ,'integer').", ".
270  $ilDB->quote($next_id,'integer').", ".
271  $ilDB->quote($this->isActive(),'integer').', '.
272  $ilDB->quote($this->getTitle() ,'text').", ".
273  $ilDB->quote($this->getDescription() ,'text').", ".
274  $ilDB->quote($this->__getLastPosition() + 1 ,'integer').", ".
275  $ilDB->quote(time() ,'integer').", ".
276  $ilDB->quote($this->getPasses(),'integer').' '.
277  ")";
278  $res = $ilDB->manipulate($query);
279  // end-patch lok
280 
281  // refresh learning progress status after adding new objective
282  include_once("./Services/Tracking/classes/class.ilLPStatusWrapper.php");
283  ilLPStatusWrapper::_refreshStatus($this->course_obj->getId());
284 
285  return $this->objective_id = $next_id;
286  }
287 
288  function update()
289  {
290  global $ilDB;
291 
292  // begin-patch lok
293  $query = "UPDATE crs_objectives ".
294  "SET title = ".$ilDB->quote($this->getTitle() ,'text').", ".
295  'active = '.$ilDB->quote($this->isActive(),'integer').', '.
296  "description = ".$ilDB->quote($this->getDescription() ,'text').", ".
297  'passes = '.$ilDB->quote($this->getPasses(),'integer').' '.
298  "WHERE objective_id = ".$ilDB->quote($this->getObjectiveId() ,'integer')." ".
299  "AND crs_id = ".$ilDB->quote($this->course_obj->getId() ,'integer')." ";
300  $res = $ilDB->manipulate($query);
301  // end-patch lok
302 
303  return true;
304  }
305 
313  public function writePosition($a_position)
314  {
315  global $ilDB;
316 
317  $query = "UPDATE crs_objectives ".
318  "SET position = ".$this->db->quote((string) $a_position ,'integer')." ".
319  "WHERE objective_id = ".$this->db->quote($this->getObjectiveId() ,'integer')." ";
320  $res = $ilDB->manipulate($query);
321  }
322 
330  public function validate()
331  {
332  return (bool) strlen($this->getTitle());
333  }
334 
335  function delete()
336  {
337  global $ilDB;
338 
339  include_once './Modules/Course/classes/class.ilCourseObjectiveQuestion.php';
340 
341  $tmp_obj_qst =& new ilCourseObjectiveQuestion($this->getObjectiveId());
342  $tmp_obj_qst->deleteAll();
343 
344  include_once './Modules/Course/classes/class.ilCourseObjectiveMaterials.php';
345 
346  $tmp_obj_lm =& new ilCourseObjectiveMaterials($this->getObjectiveId());
347  $tmp_obj_lm->deleteAll();
348 
349 
350  $query = "DELETE FROM crs_objectives ".
351  "WHERE crs_id = ".$ilDB->quote($this->course_obj->getId() ,'integer')." ".
352  "AND objective_id = ".$ilDB->quote($this->getObjectiveId() ,'integer')." ";
353  $res = $ilDB->manipulate($query);
354 
355  // refresh learning progress status after deleting objective
356  include_once("./Services/Tracking/classes/class.ilLPStatusWrapper.php");
357  ilLPStatusWrapper::_refreshStatus($this->course_obj->getId());
358 
359  return true;
360  }
361 
362  function moveUp()
363  {
364  global $ilDB;
365 
366  if(!$this->getObjectiveId())
367  {
368  return false;
369  }
370  // Stop if position is first
371  if($this->__getPosition() == 1)
372  {
373  return false;
374  }
375 
376  $query = "UPDATE crs_objectives ".
377  "SET position = position + 1 ".
378  "WHERE position = ".$ilDB->quote($this->__getPosition() - 1 ,'integer')." ".
379  "AND crs_id = ".$ilDB->quote($this->course_obj->getId() ,'integer')." ";
380  $res = $ilDB->manipulate($query);
381 
382  $query = "UPDATE crs_objectives ".
383  "SET position = position - 1 ".
384  "WHERE objective_id = ".$ilDB->quote($this->getObjectiveId() ,'integer')." ".
385  "AND crs_id = ".$ilDB->quote($this->course_obj->getId() ,'integer')." ";
386  $res = $ilDB->manipulate($query);
387 
388  $this->__read();
389 
390  return true;
391  }
392 
393  function moveDown()
394  {
395  global $ilDB;
396 
397  if(!$this->getObjectiveId())
398  {
399  return false;
400  }
401  // Stop if position is last
402  if($this->__getPosition() == $this->__getLastPosition())
403  {
404  return false;
405  }
406 
407  $query = "UPDATE crs_objectives ".
408  "SET position = position - 1 ".
409  "WHERE position = ".$ilDB->quote($this->__getPosition() + 1 ,'integer')." ".
410  "AND crs_id = ".$ilDB->quote($this->course_obj->getId() ,'integer')." ";
411  $res = $ilDB->manipulate($query);
412 
413  $query = "UPDATE crs_objectives ".
414  "SET position = position + 1 ".
415  "WHERE objective_id = ".$ilDB->quote($this->getObjectiveId() ,'integer')." ".
416  "AND crs_id = ".$ilDB->quote($this->course_obj->getId() ,'integer')." ";
417  $res = $ilDB->manipulate($query);
418 
419  $this->__read();
420 
421  return true;
422  }
423 
424  // PRIVATE
425  function __setPosition($a_position)
426  {
427  $this->position = $a_position;
428  }
429  function __getPosition()
430  {
431  return $this->position;
432  }
433  function __setCreated($a_created)
434  {
435  $this->created = $a_created;
436  }
437  function __getCreated()
438  {
439  return $this->created;
440  }
441 
442 
443  function __read()
444  {
445  global $ilDB;
446 
447  if($this->getObjectiveId())
448  {
449  $query = "SELECT * FROM crs_objectives ".
450  "WHERE crs_id = ".$ilDB->quote($this->course_obj->getId() ,'integer')." ".
451  "AND objective_id = ".$ilDB->quote($this->getObjectiveId() ,'integer')." ";
452 
453 
454  $res = $this->db->query($query);
455  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
456  {
457  // begin-patch lok
458  $this->setActive($row->active);
459  $this->setPasses($row->passes);
460  // end-patch lok
461  $this->setObjectiveId($row->objective_id);
462  $this->setTitle($row->title);
463  $this->setDescription($row->description);
464  $this->__setPosition($row->position);
465  $this->__setCreated($row->created);
466  }
467  return true;
468  }
469  return false;
470  }
471 
472  function __getOrderColumn()
473  {
474  switch($this->course_obj->getOrderType())
475  {
477  return 'ORDER BY position';
478 
480  return 'ORDER BY title';
481 
483  return 'ORDER BY create';
484  }
485  return false;
486  }
487 
488  function __updateTop()
489  {
490  global $ilDB;
491 
492  $query = "UPDATE crs_objectives ".
493  "SET position = position - 1 ".
494  "WHERE position > ".$ilDB->quote($this->__getPosition() ,'integer')." ".
495  "AND crs_id = ".$ilDB->quote($this->course_obj->getId() ,'integer')." ";
496  $res = $ilDB->manipulate($query);
497 
498  return true;
499  }
500 
501  function __getLastPosition()
502  {
503  global $ilDB;
504 
505  $query = "SELECT MAX(position) pos FROM crs_objectives ".
506  "WHERE crs_id = ".$ilDB->quote($this->course_obj->getId() ,'integer')." ";
507 
508  $res = $this->db->query($query);
509  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
510  {
511  return $row->pos;
512  }
513  return 0;
514  }
515 
516  // STATIC
517  // begin-patch lok
518  static function _getObjectiveIds($course_id, $a_activated_only = false)
519  {
520  global $ilDB;
521 
522  if($a_activated_only)
523  {
524  $query = "SELECT objective_id FROM crs_objectives ".
525  "WHERE crs_id = ".$ilDB->quote($course_id ,'integer')." ".
526  'AND active = '.$ilDB->quote(1,'integer').' '.
527  "ORDER BY position";
528  }
529  else
530  {
531  $query = "SELECT objective_id FROM crs_objectives ".
532  "WHERE crs_id = ".$ilDB->quote($course_id ,'integer')." ".
533  "ORDER BY position";
534  }
535 
536  $res = $ilDB->query($query);
537  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
538  {
539  $ids[] = $row->objective_id;
540  }
541 
542  return $ids ? $ids : array();
543  }
544  // end-patch lok
545 
546  function _deleteAll($course_id)
547  {
548  global $ilDB;
549 
550  // begin-patch lok
551  $ids = ilCourseObjective::_getObjectiveIds($course_id,false);
552  // end-patch lok
553  if(!count($ids))
554  {
555  return true;
556  }
557 
558  $in = $ilDB->in('objective_id',$ids,false,'integer');
559 
560 
561  $query = "DELETE FROM crs_objective_lm WHERE ".$in;
562  $res = $ilDB->manipulate($query);
563 
564  $query = "DELETE FROM crs_objective_tst WHERE ".$in;
565  $res = $ilDB->manipulate($query);
566 
567  $query = "DELETE FROM crs_objective_qst WHERE ".$in;
568  $res = $ilDB->manipulate($query);
569 
570  $query = "DELETE FROM crs_objectives WHERE crs_id = ".$ilDB->quote($course_id ,'integer');
571  $res = $ilDB->manipulate($query);
572 
573  // refresh learning progress status after deleting objectives
574  include_once("./Services/Tracking/classes/class.ilLPStatusWrapper.php");
576 
577  return true;
578  }
579 
580  // begin-patch optes_lok_export
585  public function toXml(ilXmlWriter $writer)
586  {
587  $writer->xmlStartTag(
588  'Objective',
589  array(
590  'online' => (int) $this->isActive(),
591  'position' => (int) $this->position,
592  'id' => (int) $this->getObjectiveId()
593  )
594  );
595  $writer->xmlElement('Title',array(), $this->getTitle());
596  $writer->xmlElement('Description',array(), $this->getDescription());
597 
598  // materials
599  include_once './Modules/Course/classes/class.ilCourseObjectiveMaterials.php';
600  $materials = new ilCourseObjectiveMaterials($this->getObjectiveId());
601  $materials->toXml($writer);
602 
603  // test/questions
604  include_once './Modules/Course/classes/class.ilCourseObjectiveQuestion.php';
606  $test->toXml($writer);
607 
608  include_once './Modules/Course/classes/Objectives/class.ilLOTestAssignments.php';
609  $assignments = ilLOTestAssignments::getInstance($this->course_obj->getId());
610  $assignments->toXml($writer, $this->getObjectiveId());
611 
612  include_once './Modules/Course/classes/Objectives/class.ilLORandomTestQuestionPools.php';
614 
615  $writer->xmlEndTag('Objective');
616  }
617  // end-patch optes_lok_export
618 }
619 ?>
static getInstance($a_container_id)
Get instance by container id.
static lookupObjectiveTitle($a_objective_id, $a_add_description=false)
static toXml(ilXmlWriter $writer, $a_objective_id)
getInstanceByRefId($a_ref_id, $stop_on_error=true)
get an instance of an Ilias object by reference id
static _lookupContainerIdByObjectiveId($a_objective_id)
Get container of object.
static lookupMaxPasses($a_objective_id)
toXml(ilXmlWriter $writer)
write objective xml
xmlStartTag($tag, $attrs=NULL, $empty=FALSE, $encode=TRUE, $escape=TRUE)
Writes a starttag.
static _getObjectiveIds($course_id, $a_activated_only=false)
XML writer class.
xmlElement($tag, $attrs=NULL, $data=Null, $encode=TRUE, $escape=TRUE)
Writes a basic element (no children, just textual content)
setObjectiveId($a_objective_id)
xmlEndTag($tag)
Writes an endtag.
const DB_FETCHMODE_OBJECT
Definition: class.ilDB.php:11
class ilCourseObjectiveMaterials
class ilcourseobjective
writePosition($a_position)
write position
ilCourseObjective(&$course_obj, $a_objective_id=0)
_refreshStatus($a_obj_id, $a_users=null)
Set dirty.
static _getCountObjectives($a_obj_id, $a_activated_only=false)
get count objectives
ilClone($a_target_id, $a_copy_id)
clone objectives
global $ilDB
static getLogger($a_component_id)
Get component logger.
class ilcourseobjectiveQuestion
setDescription($a_description)
$test
Definition: Utf8Test.php:85