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