ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilCourseObjectiveQuestion.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 {
37  const TYPE_FINAL_TEST = 1;
38 
39  public $db = null;
40 
41  public $objective_id = null;
42  public $questions;
43  protected $tests = array();
44 
45  function ilCourseObjectiveQuestion($a_objective_id)
46  {
47  global $ilDB;
48 
49  $this->db =& $ilDB;
50 
51  $this->objective_id = $a_objective_id;
52 
53  $this->__read();
54  }
55 
56 
67  public static function _isTestAssignedToObjective($a_test_id,$a_objective_id)
68  {
69  global $ilDB;
70 
71  $query = "SELECT qst_ass_id FROM crs_objective_qst ".
72  "WHERE ref_id = ".$ilDB->quote($a_test_id)." ".
73  "AND objective_id = ".$ilDB->quote($a_objective_id);
74  $res = $ilDB->query($query);
75  return $res->numRows() ? true : false;
76  }
77 
87  public function cloneDependencies($a_new_objective,$a_copy_id)
88  {
89  global $ilObjDataCache,$ilLog;
90 
91  include_once('Services/CopyWizard/classes/class.ilCopyWizardOptions.php');
92  $cwo = ilCopyWizardOptions::_getInstance($a_copy_id);
93  $mappings = $cwo->getMappings();
94  foreach($this->getQuestions() as $question)
95  {
96  if(!isset($mappings["$question[ref_id]"]) or !$mappings["$question[ref_id]"])
97  {
98  continue;
99  }
100  $question_ref_id = $question['ref_id'];
101  $question_obj_id = $question['obj_id'];
102  $question_qst_id = $question['question_id'];
103  $new_ref_id = $mappings[$question_ref_id];
104  $new_obj_id = $ilObjDataCache->lookupObjId($new_ref_id);
105 
106  if($new_obj_id == $question_obj_id)
107  {
108  $ilLog->write(__METHOD__.': Test has been linked. Keeping question id.');
109  // Object has been linked
110  $new_question_id = $question_qst_id;
111  }
112  else
113  {
114  $new_question_info = $mappings[$question_ref_id.'_'.$question_qst_id];
115  $new_question_arr = explode('_',$new_question_info);
116  if(!isset($new_question_arr[1]) or !$new_question_arr[1])
117  {
118  continue;
119  }
120  $new_question_id = $new_question_arr[1];
121  $ilLog->write(__METHOD__.': New question id is: '.$new_question_id);
122  }
123 
124  $new_question = new ilCourseObjectiveQuestion($a_new_objective);
125  $new_question->setTestRefId($new_ref_id);
126  $new_question->setTestObjId($new_obj_id);
127  $new_question->setQuestionId($new_question_id);
128  $new_question->add();
129  }
130 
131  // Copy tests
132  foreach($this->getTests() as $test)
133  {
134  $new_test_id = $mappings["$test[ref_id]"];
135 
136  $query = "UPDATE crs_objective_tst ".
137  "SET tst_status = ".$this->db->quote($test['tst_status']).", ".
138  "tst_limit = ".$this->db->quote($test['tst_limit'])." ".
139  "WHERE objective_id = ".$this->db->quote($a_new_objective)." ".
140  "AND ref_id = ".$this->db->quote($new_test_id);
141  $this->db->query($query);
142  }
143  }
144 
153  public static function _getAssignableTests($a_container_ref_id)
154  {
155  global $tree;
156 
157  return $tree->getSubTree($tree->getNodeData($a_container_ref_id),true,'tst');
158  }
159 
160  // ######################################################## Methods for test table
161  function setTestStatus($a_status)
162  {
163  $this->tst_status = $a_status;
164  }
165  function getTestStatus()
166  {
167  return (int) $this->tst_status;
168  }
169  function setTestSuggestedLimit($a_limit)
170  {
171  $this->tst_limit = $a_limit;
172  }
174  {
175  return (int) $this->tst_limit;
176  }
177  function __addTest()
178  {
179  global $ilDB;
180 
181  $query = "UPDATE crs_objective_tst ".
182  "SET tst_status = ".$this->db->quote($this->getTestStatus())." ".
183  "WHERE objective_id = ".$this->db->quote($this->getObjectiveId())." ".
184  "AND ref_id = ".$this->db->quote($this->getTestRefId())." ";
185  $this->db->query($query);
186 
187 
188  // CHECK if entry already exists
189  $query = "SELECT * FROM crs_objective_tst ".
190  "WHERE objective_id = ".$ilDB->quote($this->getObjectiveId())." ".
191  "AND ref_id = ".$ilDB->quote($this->getTestRefId())."";
192 
193  $res = $this->db->query($query);
194  if($res->numRows())
195  {
196  return false;
197  }
198 
199  // Check for existing limit
200  $query = "SELECT tst_limit FROM crs_objective_tst ".
201  "WHERE objective_id = ".$this->db->quote($this->getObjectiveId())." ".
202  "AND tst_status = ".$this->db->quote($this->getTestStatus())." ";
203 
204  $res = $this->db->query($query);
205 
206  $limit = -1;
207  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
208  {
209  $limit = $row->tst_limit;
210  }
211 
212  $query = "INSERT INTO crs_objective_tst ".
213  "SET objective_id = ".$ilDB->quote($this->getObjectiveId()).", ".
214  "ref_id = ".$ilDB->quote($this->getTestRefId()).", ".
215  "obj_id = ".$ilDB->quote($this->getTestObjId()).", ".
216  "tst_status = ".$ilDB->quote($this->getTestStatus()).", ".
217  "tst_limit = ".$this->db->quote($limit)." ";
218 
219  $this->db->query($query);
220 
221  return true;
222  }
223 
224  function __deleteTest($a_test_ref_id)
225  {
226  global $ilDB;
227 
228  // Delete questions
229  $query = "DELETE FROM crs_objective_qst ".
230  "WHERE objective_id = ".$ilDB->quote($this->getObjectiveId())." ".
231  "AND ref_id = ".$ilDB->quote($a_test_ref_id)." ";
232 
233  $this->db->query($query);
234 
235  // delete tst entries
236  $query = "DELETE FROM crs_objective_tst ".
237  "WHERE objective_id = ".$ilDB->quote($this->getObjectiveId())." ".
238  "AND ref_id = ".$ilDB->quote($a_test_ref_id)." ";
239 
240  $this->db->query($query);
241 
242  unset($this->tests[$a_test_ref_id]);
243 
244  return true;
245  }
246 
257  public static function _updateTestLimits($a_objective_id,$a_status,$a_limit)
258  {
259  global $ilDB;
260 
261  $query = "UPDATE crs_objective_tst ".
262  "SET tst_limit = ".$ilDB->quote($a_limit)." ".
263  "WHERE tst_status = ".$ilDB->quote($a_status)." ".
264  "AND objective_id = ".$ilDB->quote($a_objective_id);
265 
266  $ilDB->query($query);
267  return true;
268  }
269 
270  function updateTest($a_objective_id)
271  {
272  global $ilDB;
273 
274  $query = "UPDATE crs_objective_tst ".
275  "SET tst_status = ".$ilDB->quote($this->getTestStatus()).", ".
276  "tst_limit = ".$ilDB->quote($this->getTestSuggestedLimit())." ".
277  "WHERE test_objective_id = ".$ilDB->quote($a_objective_id)."";
278 
279  $this->db->query($query);
280 
281  return true;
282  }
283 
284  function getTests()
285  {
286  global $ilDB;
287 
288  $query = "SELECT * FROM crs_objective_tst as cot ".
289  "JOIN object_data as obd ON cot.obj_id = obd.obj_id ".
290  "WHERE objective_id = ".$ilDB->quote($this->getObjectiveId())." ".
291  "ORDER BY title ";
292 
293  $res = $this->db->query($query);
294  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
295  {
296  $test['test_objective_id'] = $row->test_objective_id;
297  $test['objective_id'] = $row->objective_id;
298  $test['ref_id'] = $row->ref_id;
299  $test['obj_id'] = $row->obj_id;
300  $test['tst_status'] = $row->tst_status;
301  $test['tst_limit'] = $row->tst_limit;
302  $test['title'] = $row->title;
303 
304  $tests[] = $test;
305  }
306 
307  return $tests ? $tests : array();
308  }
309 
317  public function getSelfAssessmentTests()
318  {
319  foreach($this->tests as $test)
320  {
321  if($test['status'] == self::TYPE_SELF_ASSESSMENT)
322  {
323  $self[] = $test;
324  }
325  }
326  return $self ? $self : array();
327  }
328 
335  public function getFinalTests()
336  {
337  foreach($this->tests as $test)
338  {
339  if($test['status'] == self::TYPE_FINAL_TEST)
340  {
341  $final[] = $test;
342  }
343  }
344  return $final ? $final : array();
345  }
346 
347  function _getTest($a_test_objective_id)
348  {
349  global $ilDB;
350 
351  $query = "SELECT * FROM crs_objective_tst ".
352  "WHERE test_objective_id = ".$ilDB->quote($a_test_objective_id)." ";
353 
354  $res = $ilDB->query($query);
355  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
356  {
357  $test['test_objective_id'] = $row->test_objective_id;
358  $test['objective_id'] = $row->objective_id;
359  $test['ref_id'] = $row->ref_id;
360  $test['obj_id'] = $row->obj_id;
361  $test['tst_status'] = $row->tst_status;
362  $test['tst_limit'] = $row->tst_limit;
363  }
364 
365  return $test ? $test : array();
366  }
367 
368  // ############################################################# METHODS for question table
369  function getQuestions()
370  {
371  return $this->questions ? $this->questions : array();
372  }
373 
380  public function getSelfAssessmentQuestions()
381  {
382  foreach($this->questions as $question)
383  {
384  if($question['test_type'] == self::TYPE_SELF_ASSESSMENT)
385  {
386  $self[] = $question;
387  }
388  }
389  return $self ? $self : array();
390  }
391 
398  public function getSelfAssessmentPoints()
399  {
400  foreach($this->getSelfAssessmentQuestions() as $question)
401  {
402  $points += $question['points'];
403  }
404  return $points ? $points : 0;
405  }
406 
413  public function getFinalTestPoints()
414  {
415  foreach($this->getFinalTestQuestions() as $question)
416  {
417  $points += $question['points'];
418  }
419  return $points ? $points : 0;
420  }
421 
428  public function isSelfAssessmentQuestion($a_question_id)
429  {
430  foreach($this->questions as $question)
431  {
432  if($question['question_id'] == $a_question_id)
433  {
434  return $question['test_type'] == self::TYPE_SELF_ASSESSMENT;
435  }
436  }
437  return false;
438  }
439 
447  public function isFinalTestQuestion($a_question_id)
448  {
449  foreach($this->questions as $question)
450  {
451  if($question['question_id'] == $a_question_id)
452  {
453  return $question['test_type'] == self::TYPE_FINAL_TEST;
454  }
455  }
456  return false;
457 
458  }
459 
466  public function getFinalTestQuestions()
467  {
468  foreach($this->questions as $question)
469  {
470  if($question['test_type'] == self::TYPE_FINAL_TEST)
471  {
472  $final[] = $question;
473  }
474  }
475  return $final ? $final : array();
476  }
477 
478 
479 
487  public function getQuestionsOfTest($a_test_id)
488  {
489  foreach($this->getQuestions() as $qst)
490  {
491  if($a_test_id == $qst['obj_id'])
492  {
493  $questions[] = $qst;
494  }
495  }
496  return $questions ? $questions : array();
497  }
498 
499  function getQuestion($question_id)
500  {
501  return $this->questions[$question_id] ? $this->questions[$question_id] : array();
502  }
503 
504  function getObjectiveId()
505  {
506  return $this->objective_id;
507  }
508 
509  function setTestRefId($a_ref_id)
510  {
511  $this->tst_ref_id = $a_ref_id;
512  }
513  function getTestRefId()
514  {
515  return $this->tst_ref_id ? $this->tst_ref_id : 0;
516  }
517  function setTestObjId($a_obj_id)
518  {
519  $this->tst_obj_id = $a_obj_id;
520  }
521  function getTestObjId()
522  {
523  return $this->tst_obj_id ? $this->tst_obj_id : 0;
524  }
525  function setQuestionId($a_question_id)
526  {
527  $this->question_id = $a_question_id;
528  }
529  function getQuestionId()
530  {
531  return $this->question_id;
532  }
533 
534 
536  {
537  include_once './Modules/Test/classes/class.ilObjTest.php';
538 
539  $points = 0;
540  foreach($this->getQuestions() as $question)
541  {
542  $tmp_test =& ilObjectFactory::getInstanceByRefId($question['ref_id']);
543 
544  $tmp_question =& ilObjTest::_instanciateQuestion($question['question_id']);
545 
546  $points += $tmp_question->getMaximumPoints();
547 
548  unset($tmp_question);
549  unset($tmp_test);
550  }
551  return $points;
552  }
553 
554  function getMaxPointsByTest($a_test_ref_id)
555  {
556  $points = 0;
557 
558  $tmp_test =& ilObjectFactory::getInstanceByRefId($a_test_ref_id);
559 
560  foreach($this->getQuestions() as $question)
561  {
562  if($question['ref_id'] == $a_test_ref_id)
563  {
564  $tmp_question =& ilObjTest::_instanciateQuestion($question['question_id']);
565 
566  $points += $tmp_question->getMaximumPoints();
567 
568  unset($tmp_question);
569  }
570  }
571  unset($tmp_test);
572 
573  return $points;
574  }
575 
584  public static function _lookupMaximumPointsOfQuestion($a_question_id)
585  {
586  include_once('Modules/TestQuestionPool/classes/class.assQuestion.php');
587  return assQuestion::_getMaximumPoints($a_question_id);
588  }
589 
590 
591  function getNumberOfQuestionsByTest($a_test_ref_id)
592  {
593  $counter = 0;
594 
595  foreach($this->getQuestions() as $question)
596  {
597  if($question['ref_id'] == $a_test_ref_id)
598  {
599  ++$counter;
600  }
601  }
602  return $counter;
603  }
604 
605  function getQuestionsByTest($a_test_ref_id)
606  {
607  foreach($this->getQuestions() as $question)
608  {
609  if($question['ref_id'] == $a_test_ref_id)
610  {
611  $qst[] = $question['question_id'];
612  }
613  }
614  return $qst ? $qst : array();
615  }
616 
624  public function updateLimits()
625  {
626  foreach($this->tests as $ref_id => $test_data)
627  {
628  switch($test_data['status'])
629  {
631  $points = $this->getSelfAssessmentPoints();
632  break;
633 
634  case self::TYPE_FINAL_TEST:
635  $points = $this->getFinalTestPoints();
636  break;
637  }
638  if($test_data['limit'] == -1 or $test_data['limit'] > $points)
639  {
640  switch($test_data['status'])
641  {
643  $points = $this->getSelfAssessmentPoints();
644  break;
645 
646  case self::TYPE_FINAL_TEST:
647  $points = $this->getFinalTestPoints();
648  break;
649  }
650  $query = "UPDATE crs_objective_tst ".
651  "SET tst_limit = ".$this->db->quote($points)." ".
652  "WHERE test_objective_id = ".$this->db->quote($test_data['test_objective_id'])." ";
653  $this->db->query($query);
654  }
655  }
656  }
657 
658 
659  function add()
660  {
661  global $ilDB;
662 
663  $query = "DELETE FROM crs_objective_qst ".
664  "WHERE objective_id = ".$this->db->quote($this->getObjectiveId())." ".
665  "AND question_id = ".$this->db->quote($this->getQuestionId())." ";
666  $this->db->query($query);
667 
668 
669  $query = "INSERT INTO crs_objective_qst ".
670  "SET objective_id = ".$ilDB->quote($this->getObjectiveId()).", ".
671  "ref_id = ".$ilDB->quote($this->getTestRefId()).", ".
672  "obj_id = ".$ilDB->quote($this->getTestObjId()).", ".
673  "question_id = ".$ilDB->quote($this->getQuestionId())."";
674 
675  $this->db->query($query);
676 
677  $this->__addTest();
678 
679  $this->__read();
680 
681  return true;
682  }
683  function delete($qst_id)
684  {
685  global $ilDB;
686 
687  if(!$qst_id)
688  {
689  return false;
690  }
691 
692  $query = "SELECT * FROM crs_objective_qst ".
693  "WHERE qst_ass_id = ".$ilDB->quote($qst_id)." ";
694 
695  $res = $this->db->query($query);
696  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
697  {
698  $test_rid = $row->ref_id;
699  $test_oid = $row->obj_id;
700  }
701 
702  $query = "DELETE FROM crs_objective_qst ".
703  "WHERE qst_ass_id = ".$ilDB->quote($qst_id)." ";
704 
705  $this->db->query($query);
706 
707  // delete test if it was the last question
708  $query = "SELECT * FROM crs_objective_qst ".
709  "WHERE ref_id = ".$ilDB->quote($test_rid)." ".
710  "AND obj_id = ".$ilDB->quote($test_oid)." ".
711  "AND objective_id = ".$ilDB->quote($this->getObjectiveId())." ";
712 
713 
714  $res = $this->db->query($query);
715  if(!$res->numRows())
716  {
717  $this->__deleteTest($test_rid);
718  }
719 
720  return true;
721  }
722 
723  function deleteAll()
724  {
725  global $ilDB;
726 
727  $query = "DELETE FROM crs_objective_qst ".
728  "WHERE objective_id = ".$ilDB->quote($this->getObjectiveId())." ";
729 
730  $this->db->query($query);
731 
732  $query = "DELETE FROM crs_objective_tst ".
733  "WHERE objective_id = ".$ilDB->quote($this->getObjectiveId())." ";
734 
735  $this->db->query($query);
736 
737  return true;
738  }
739 
740 
741  // PRIVATE
742  function __read()
743  {
744  global $ilDB,$tree;
745 
746  include_once './Modules/Test/classes/class.ilObjTest.php';
747  include_once('Modules/Course/classes/class.ilCourseObjective.php');
748 
749  $container_ref_ids = ilObject::_getAllReferences(ilCourseObjective::_lookupContainerIdByObjectiveId($this->objective_id));
750  $container_ref_id = current($container_ref_ids);
751 
752  // Read test data
753  $query = "SELECT * FROM crs_objective_tst ".
754  "WHERE objective_id = ".$ilDB->quote($this->getObjectiveId())." ";
755  $res = $this->db->query($query);
756  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
757  {
758  $this->tests[$row->ref_id]['test_objective_id'] = $row->test_objective_id;
759  $this->tests[$row->ref_id]['ref_id'] = $row->ref_id;
760  $this->tests[$row->ref_id]['obj_id'] = $row->obj_id;
761  $this->tests[$row->ref_id]['status'] = $row->tst_status;
762  $this->tests[$row->ref_id]['limit'] = $row->tst_limit;
763  }
764 
765  $this->questions = array();
766  $query = "SELECT * FROM crs_objective_qst as coq ".
767  "JOIN qpl_questions as qq ON coq.question_id = qq.question_id ".
768  "WHERE objective_id = ".$ilDB->quote($this->getObjectiveId())." ".
769  "ORDER BY title";
770 
771  $res = $this->db->query($query);
772  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
773  {
774  if(!$tree->isInTree($row->ref_id) or !$tree->isGrandChild($container_ref_id,$row->ref_id))
775  {
776  $this->__deleteTest($row->ref_id);
777  continue;
778  }
779  if(!$question = ilObjTest::_instanciateQuestion($row->question_id))
780  {
781  $this->delete($row->question_id);
782  continue;
783  }
784 
785  $qst['ref_id'] = $row->ref_id;
786  $qst['obj_id'] = $row->obj_id;
787  $qst['question_id'] = $row->question_id;
788  $qst['qst_ass_id'] = $row->qst_ass_id;
789  $qst['title'] = $question->getTitle();
790  $qst['description'] = $question->getComment();
791  $qst['test_type'] = $this->tests[$row->ref_id]['status'];
792  $qst['points'] = $question->getPoints();
793 
794  $this->questions[$row->qst_ass_id] = $qst;
795  }
796 
797  return true;
798  }
799 
800  // STATIC
808  public function _hasTests($a_course_id)
809  {
810  global $ilDB;
811 
812  $query = "SELECT co.objective_id FROM crs_objectives AS co JOIN ".
813  "crs_objective_tst AS cot ON co.objective_id = cot.objective_id ".
814  "WHERE crs_id = ".$ilDB->quote($a_course_id)." ";
815  $res = $ilDB->query($query);
816  return $res->numRows() ? true : false;
817  }
818 
819 
820  function _isAssigned($a_objective_id,$a_tst_ref_id,$a_question_id)
821  {
822  global $ilDB;
823 
824  $query = "SELECT crs_qst.objective_id as objective_id FROM crs_objective_qst as crs_qst, crs_objectives as crs_obj ".
825  "WHERE crs_qst.objective_id = crs_obj.objective_id ".
826  "AND crs_qst.objective_id = ".$ilDB->quote($a_objective_id) ." ".
827  "AND ref_id = ".$ilDB->quote($a_tst_ref_id)." ".
828  "AND question_id = ".$ilDB->quote($a_question_id)." ";
829 
830  $res = $ilDB->query($query);
831  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
832  {
833  $objective_id = $row->objective_id;
834  }
835 
836  return $objective_id ? $objective_id : 0;
837  }
838 
839 }
840 ?>