• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

assessment/classes/class.assQuestion.php

Go to the documentation of this file.
00001 <?php
00002  /*
00003    +----------------------------------------------------------------------------+
00004    | ILIAS open source                                                          |
00005    +----------------------------------------------------------------------------+
00006    | Copyright (c) 1998-2001 ILIAS open source, University of Cologne           |
00007    |                                                                            |
00008    | This program is free software; you can redistribute it and/or              |
00009    | modify it under the terms of the GNU General Public License                |
00010    | as published by the Free Software Foundation; either version 2             |
00011    | of the License, or (at your option) any later version.                     |
00012    |                                                                            |
00013    | This program is distributed in the hope that it will be useful,            |
00014    | but WITHOUT ANY WARRANTY; without even the implied warranty of             |
00015    | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              |
00016    | GNU General Public License for more details.                               |
00017    |                                                                            |
00018    | You should have received a copy of the GNU General Public License          |
00019    | along with this program; if not, write to the Free Software                |
00020    | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
00021    +----------------------------------------------------------------------------+
00022 */
00023 
00024 require_once "./assessment/classes/class.assClozeTestGUI.php";
00025 require_once "./assessment/classes/class.assImagemapQuestionGUI.php";
00026 require_once "./assessment/classes/class.assJavaAppletGUI.php";
00027 require_once "./assessment/classes/class.assMatchingQuestionGUI.php";
00028 require_once "./assessment/classes/class.assMultipleChoiceGUI.php";
00029 require_once "./assessment/classes/class.assOrderingQuestionGUI.php";
00030 require_once "./assessment/classes/class.assTextQuestionGUI.php";
00031 require_once "./content/classes/Pages/class.ilPageObject.php";
00032 
00033 define("LIMIT_NO_LIMIT", 0);
00034 define("LIMIT_TIME_ONLY", 1);
00035 
00036 define("OUTPUT_HTML", 0);
00037 define("OUTPUT_JAVASCRIPT", 1);
00038 
00050 class ASS_Question
00051 {
00059         var $id;
00060 
00068         var $title;
00069 
00077         var $comment;
00078 
00087         var $owner;
00088 
00097         var $author;
00098 
00108         var $points;
00109 
00117         var $est_working_time;
00118 
00126         var $shuffle;
00127 
00135         var $test_id;
00136 
00144         var $obj_id;
00145 
00153         var $ilias;
00154 
00162         var $tpl;
00163 
00171         var $lng;
00172 
00180         var $domxml;
00181 
00189         var $outputType;
00190 
00191         var $suggested_solutions;
00203         function ASS_Question(
00204                 $title = "",
00205                 $comment = "",
00206                 $author = "",
00207                 $owner = -1
00208         )
00209         {
00210                 global $ilias;
00211                 global $lng;
00212                 global $tpl;
00213 
00214                 $this->ilias =& $ilias;
00215                 $this->lng =& $lng;
00216                 $this->tpl =& $tpl;
00217 
00218                 $this->title = $title;
00219                 $this->comment = $comment;
00220                 $this->author = $author;
00221                 if (!$this->author)
00222                 {
00223                         $this->author = $this->ilias->account->fullname;
00224                 }
00225                 $this->owner = $owner;
00226                 if ($this->owner == -1)
00227                 {
00228                         $this->owner = $this->ilias->account->id;
00229                 }
00230                 $this->id = -1;
00231                 $this->test_id = -1;
00232                 $this->suggested_solutions = array();
00233                 $this->shuffle = 1;
00234                 $this->setEstimatedWorkingTime(0,1,0);
00235                 $this->outputType = OUTPUT_HTML;
00236                 register_shutdown_function(array(&$this, '_ASS_Question'));
00237         }
00238 
00239         function _ASS_Question()
00240         {
00241                 if (!empty($this->domxml))
00242                 {
00243                         $this->domxml->free();
00244                 }
00245         }
00246 
00256         function to_xml()
00257         {
00258                 // to be implemented in the successor classes of ASS_Question
00259         }
00260 
00269         function isComplete()
00270         {
00271                 return false;
00272         }
00273 
00283         function questionTitleExists($title)
00284         {
00285                 $query = sprintf("SELECT * FROM qpl_questions WHERE title = %s",
00286                         $this->ilias->db->quote($title)
00287                         );
00288                 $result = $this->ilias->db->query($query);
00289                 if (strcmp(strtolower(get_class($result)), db_result) == 0)
00290                 {
00291                         if ($result->numRows() == 1)
00292                         {
00293                                 return TRUE;
00294                         }
00295                 }
00296                 return FALSE;
00297         }
00298 
00308         function setTitle($title = "")
00309         {
00310                 $this->title = $title;
00311         }
00312 
00322         function setId($id = -1)
00323         {
00324                 $this->id = $id;
00325         }
00326 
00336         function setTestId($id = -1)
00337         {
00338                 $this->test_id = $id;
00339         }
00340 
00350         function setComment($comment = "")
00351         {
00352                 $this->comment = $comment;
00353         }
00354 
00364         function setOutputType($outputType = OUTPUT_HTML)
00365         {
00366                 $this->outputType = $outputType;
00367         }
00368 
00369 
00379         function setShuffle($shuffle = true)
00380         {
00381                 if ($shuffle)
00382                 {
00383                         $this->shuffle = 1;
00384                 }
00385                         else
00386                 {
00387                         $this->shuffle = 0;
00388                 }
00389         }
00390 
00402         function setEstimatedWorkingTime($hour=0, $min=0, $sec=0)
00403         {
00404                 $this->est_working_time = array("h" => (int)$hour, "m" => (int)$min, "s" => (int)$sec);
00405         }
00406 
00416         function keyInArray($searchkey, $array)
00417         {
00418                 if ($searchKey)
00419                 {
00420                         foreach ($array as $key => $value)
00421                         {
00422                                 if (strcmp($key, $searchkey)==0)
00423                                 {
00424                                         return true;
00425                                 }
00426                         }
00427                 }
00428                 return false;
00429         }
00430 
00440         function setAuthor($author = "")
00441         {
00442                 if (!$author)
00443                 {
00444                         $author = $this->ilias->account->fullname;
00445                 }
00446                 $this->author = $author;
00447         }
00448 
00458         function setOwner($owner = "")
00459         {
00460                 $this->owner = $owner;
00461         }
00462 
00472         function getTitle()
00473         {
00474                 return $this->title;
00475         }
00476 
00486         function getId()
00487         {
00488                 return $this->id;
00489         }
00490 
00500         function getShuffle()
00501         {
00502                 return $this->shuffle;
00503         }
00504 
00514         function getTestId()
00515         {
00516                 return $this->test_id;
00517         }
00518 
00528         function getComment()
00529         {
00530                 return $this->comment;
00531         }
00532 
00542         function getOutputType()
00543         {
00544                 return $this->outputType;
00545         }
00546 
00556         function getEstimatedWorkingTime()
00557         {
00558                 if (!$this->est_working_time)
00559                 {
00560                         $this->est_working_time = array("h" => 0, "m" => 0, "s" => 0);
00561                 }
00562                 return $this->est_working_time;
00563         }
00564 
00574         function getAuthor()
00575         {
00576                 return $this->author;
00577         }
00578 
00588         function getOwner()
00589         {
00590                 return $this->owner;
00591         }
00592 
00602         function getObjId()
00603         {
00604                 return $this->obj_id;
00605         }
00606 
00616         function setObjId($obj_id = 0)
00617         {
00618                 $this->obj_id = $obj_id;
00619         }
00620 
00624         function createPageObject()
00625         {
00626 //              $qpl_id = ilObject::_lookupObjectId($this->getRefId());
00627                 $qpl_id = $this->getObjId();
00628 
00629                 $this->page = new ilPageObject("qpl", 0);
00630                 $this->page->setId($this->getId());
00631                 $this->page->setParentId($qpl_id);
00632                 $this->page->setXMLContent("<PageObject><PageContent>".
00633                         "<Question QRef=\"il__qst_".$this->getId()."\"/>".
00634                         "</PageContent></PageObject>");
00635                 $this->page->create();
00636         }
00637 
00646         function insertIntoTest($test_id)
00647         {
00648                 // get maximum sequence index in test
00649                 $query = sprintf("SELECT MAX(sequence) AS seq FROM dum_test_question WHERE test_fi=%s",
00650                         $this->ilias->db->quote($test_id)
00651                         );
00652                 $result = $this->ilias->db->query($query);
00653                 $sequence = 1;
00654                 if ($result->numRows() == 1)
00655                 {
00656                         $data = $result->fetchRow(DB_FETCHMODE_OBJECT);
00657                         $sequence = $data->seq + 1;
00658                 }
00659                 $query = sprintf("INSERT INTO dum_test_question (test_question_id, test_fi, question_fi, sequence, TIMESTAMP) VALUES (NULL, %s, %s, %s, NULL)",
00660                         $this->ilias->db->quote($test_id),
00661                         $this->ilias->db->quote($this->getId()),
00662                         $this->ilias->db->quote($sequence)
00663                         );
00664                 $result = $this->ilias->db->query($query);
00665                 if ($result != DB_OK)
00666                 {
00667                 // Fehlermeldung
00668                 }
00669         }
00670 
00680   function _getMaximumPoints($question_id) 
00681         {
00682                 global $ilDB;
00683 
00684                 $points = 0;
00685                 $query = sprintf("SELECT points FROM qpl_questions WHERE question_id = %s",
00686                         $ilDB->quote($question_id . "")
00687                 );
00688                 $result = $ilDB->query($query);
00689                 if ($result->numRows() == 1)
00690                 {
00691                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
00692                         $points = $row["points"];
00693                 }
00694                 return $points;
00695   }
00696 
00706         function &_getQuestionInfo($question_id)
00707         {
00708                 global $ilDB;
00709 
00710                 $query = sprintf("SELECT qpl_questions.*, qpl_question_type.type_tag FROM qpl_question_type, qpl_questions WHERE qpl_questions.question_id = %s AND qpl_questions.question_type_fi = qpl_question_type.question_type_id",
00711                         $ilDB->quote($question_id . "")
00712                 );
00713                 $result = $ilDB->query($query);
00714                 if ($result->numRows())
00715                 {
00716                         return $result->fetchRow(DB_FETCHMODE_ASSOC);
00717                 }
00718                 else return array();
00719         }
00720         
00730         function _getSuggestedSolutionCount($question_id)
00731         {
00732                 global $ilDB;
00733 
00734                 $query = sprintf("SELECT suggested_solution_id FROM qpl_suggested_solutions WHERE question_fi = %s",
00735                         $ilDB->quote($question_id . "")
00736                 );
00737                 $result = $ilDB->query($query);
00738                 return $result->numRows();
00739         }
00740 
00751         function &_getSuggestedSolution($question_id, $subquestion_index = 0)
00752         {
00753                 global $ilDB;
00754 
00755                 $query = sprintf("SELECT * FROM qpl_suggested_solutions WHERE question_fi = %s AND subquestion_index = %s",
00756                         $ilDB->quote($question_id . ""),
00757                         $ilDB->quote($subquestion_index . "")
00758                 );
00759                 $result = $ilDB->query($query);
00760                 if ($result->numRows() == 1)
00761                 {
00762                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
00763                         return array(
00764                                 "internal_link" => $row["internal_link"],
00765                                 "import_id" => $row["import_id"]
00766                         );
00767                 }
00768                 else
00769                 {
00770                         return array();
00771                 }
00772         }
00773         
00784         function _getReachedPoints($user_id, $test_id, $question_id)
00785         {
00786                 global $ilDB;
00787 
00788                 $points = 0;
00789                 $query = sprintf("SELECT * FROM tst_test_result WHERE user_fi = %s AND test_fi = %s AND question_fi = %s",
00790                         $ilDB->quote($user_id . ""),
00791                         $ilDB->quote($test_id . ""),
00792                         $ilDB->quote($question_id . "")
00793                 );
00794                 $result = $ilDB->query($query);
00795                 if ($result->numRows() == 1)
00796                 {
00797                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
00798                         $points = $row["points"];
00799                 }
00800                 return $points;
00801         }
00802 
00813         function getReachedPoints($user_id, $test_id)
00814         {
00815                 global $ilDB;
00816 
00817                 $points = 0;
00818                 $query = sprintf("SELECT * FROM tst_test_result WHERE user_fi = %s AND test_fi = %s AND question_fi = %s",
00819                         $ilDB->quote($user_id),
00820                         $ilDB->quote($test_id),
00821                         $ilDB->quote($this->getId())
00822                 );
00823                 $result = $ilDB->query($query);
00824                 if ($result->numRows() == 1)
00825                 {
00826                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
00827                         $points = $row["points"];
00828                 }
00829                 return $points;
00830         }
00831         
00840         function getMaximumPoints()
00841         {
00842                 return 0;
00843         }
00844 
00854         function saveWorkingData($test_id, $limit_to = LIMIT_NO_LIMIT)
00855         {
00856     global $ilDB;
00857                 global $ilUser;
00858     $db =& $ilDB->db;
00859                 $reached_points = $this->calculateReachedPoints($ilUser->id, $test_id);
00860                 $query = sprintf("REPLACE INTO tst_test_result (user_fi, test_fi, question_fi, points) VALUES (%s, %s, %s, %s)",
00861                         $db->quote($ilUser->id . ""),
00862                         $db->quote($test_id . ""),
00863                         $db->quote($this->getId() . ""),
00864                         $db->quote($reached_points . "")
00865                 );
00866     $result = $db->query($query);
00867         }
00868 
00877         function getJavaPath() {
00878                 return CLIENT_WEB_DIR . "/assessment/$this->obj_id/$this->id/java/";
00879         }
00880 
00889         function getImagePath()
00890         {
00891                 return CLIENT_WEB_DIR . "/assessment/$this->obj_id/$this->id/images/";
00892         }
00893 
00902         function getJavaPathWeb()
00903         {
00904                 $webdir = ilUtil::removeTrailingPathSeparators(CLIENT_WEB_DIR) . "/assessment/$this->obj_id/$this->id/java/";
00905                 return str_replace(ilUtil::removeTrailingPathSeparators(ILIAS_ABSOLUTE_PATH), ilUtil::removeTrailingPathSeparators(ILIAS_HTTP_PATH), $webdir);
00906         }
00907 
00916         function getImagePathWeb()
00917         {
00918                 $webdir = ilUtil::removeTrailingPathSeparators(CLIENT_WEB_DIR) . "/assessment/$this->obj_id/$this->id/images/";
00919                 return str_replace(ilUtil::removeTrailingPathSeparators(ILIAS_ABSOLUTE_PATH), ilUtil::removeTrailingPathSeparators(ILIAS_HTTP_PATH), $webdir);
00920         }
00921 
00931         function &getSolutionValues($test_id, $ilUser)
00932         {
00933                 global $ilDB;
00934 
00935                 $db =& $ilDB->db;
00936 
00937                 $query = sprintf("SELECT * FROM tst_solutions WHERE user_fi = %s AND test_fi = %s AND question_fi = %s",
00938                         $db->quote($ilUser->id),
00939                         $db->quote($test_id),
00940                         $db->quote($this->getId())
00941                         );
00942                 $result = $db->query($query);
00943                 $values = array();
00944                 while   ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
00945                 {
00946                         array_push($values, $row);
00947                 }
00948 
00949                 return $values;
00950         }
00951 
00960         function isInUse($question_id = "")
00961         {
00962                 if ($question_id < 1) $question_id = $this->id;
00963                 $query = sprintf("SELECT COUNT(question_id) AS question_count FROM qpl_questions WHERE original_id = %s",
00964                         $this->ilias->db->quote($question_id . "")
00965                 );
00966                 $result = $this->ilias->db->query($query);
00967                 $row = $result->fetchRow(DB_FETCHMODE_OBJECT);
00968                 return $row->question_count;
00969         }
00970 
00979         function isClone($question_id = "")
00980         {
00981                 if ($question_id < 1) $question_id = $this->id;
00982                 $query = sprintf("SELECT original_id FROM qpl_questions WHERE question_id = %s",
00983                         $this->ilias->db->quote($question_id . "")
00984                 );
00985                 $result = $this->ilias->db->query($query);
00986                 $row = $result->fetchRow(DB_FETCHMODE_OBJECT);
00987                 if ($row->original_id > 0)
00988                 {
00989                         return TRUE;
00990                 }
00991                 else
00992                 {
00993                         return FALSE;
00994                 }
00995         }
00996 
01005         function pcArrayShuffle($array)
01006         {
01007                 mt_srand((double)microtime()*1000000);
01008                 $i = count($array);
01009                 if ($i > 0)
01010                 {
01011                         while(--$i)
01012                         {
01013                                 $j = mt_rand(0, $i);
01014                                 if ($i != $j)
01015                                 {
01016                                         // swap elements
01017                                         $tmp = $array[$j];
01018                                         $array[$j] = $array[$i];
01019                                         $array[$i] = $tmp;
01020                                 }
01021                         }
01022                 }
01023                 return $array;
01024         }
01025 
01031         function getQuestionTypeFromDb($question_id)
01032         {
01033                 global $ilDB;
01034 
01035                 $query = sprintf("SELECT qpl_question_type.type_tag FROM qpl_question_type, qpl_questions WHERE qpl_questions.question_id = %s AND qpl_questions.question_type_fi = qpl_question_type.question_type_id",
01036                         $ilDB->quote($question_id));
01037 
01038                 $result = $ilDB->query($query);
01039                 $data = $result->fetchRow(DB_FETCHMODE_OBJECT);
01040 
01041                 return $data->type_tag;
01042         }
01043 
01052         function delete($question_id)
01053         {
01054                 if ($question_id < 1)
01055                 return;
01056 
01057                 $query = sprintf("SELECT obj_fi FROM qpl_questions WHERE question_id = %s",
01058                         $this->ilias->db->quote($question_id)
01059                         );
01060         $result = $this->ilias->db->query($query);
01061                 if ($result->numRows() == 1)
01062                 {
01063                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
01064                         $obj_id = $row["obj_fi"];
01065                 }
01066                 else
01067                 {
01068                         return;
01069                 }
01070 
01071                 $page = new ilPageObject("qpl", $question_id);
01072                 $page->delete();
01073                 
01074                 $query = sprintf("DELETE FROM qpl_questions WHERE question_id = %s",
01075                         $this->ilias->db->quote($question_id)
01076                         );
01077                 $result = $this->ilias->db->query($query);
01078                 $query = sprintf("DELETE FROM qpl_answers WHERE question_fi = %s",
01079                         $this->ilias->db->quote($question_id)
01080                         );
01081                 $result = $this->ilias->db->query($query);
01082 
01083                 // delete the question in the tst_test_question table (list of test questions)
01084                 $querydelete = sprintf("DELETE FROM tst_test_question WHERE question_fi = %s", $this->ilias->db->quote($question_id));
01085                 $deleteresult = $this->ilias->db->query($querydelete);
01086 
01087                 // delete suggested solutions contained in the question
01088                 $querydelete = sprintf("DELETE FROM qpl_suggested_solutions WHERE question_fi = %s", $this->ilias->db->quote($question_id));
01089                 $deleteresult = $this->ilias->db->query($querydelete);
01090                                 
01091                 $directory = CLIENT_WEB_DIR . "/assessment/" . $obj_id . "/$question_id";
01092                 if (preg_match("/\d+/", $obj_id) and preg_match("/\d+/", $question_id) and is_dir($directory))
01093                 {
01094                         $directory = escapeshellarg($directory);
01095                         exec("rm -rf $directory");
01096                 }
01097         }
01098 
01102         function getTotalAnswers()
01103         {
01104                 return $this->_getTotalAnswers($this->id);
01105         }
01106 
01113         function _getTotalAnswers($a_q_id)
01114         {
01115                 global $ilDB;
01116 
01117                 // get all question references to the question id
01118                 $query = sprintf("SELECT question_id FROM qpl_questions WHERE original_id = %s OR question_id = %s",
01119                         $ilDB->quote($a_q_id),
01120                         $ilDB->quote($a_q_id)
01121                 );
01122 
01123                 $result = $ilDB->query($query);
01124 
01125                 if ($result->numRows() == 0)
01126                 {
01127                         return 0;
01128                 }
01129                 $found_id = array();
01130                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
01131                 {
01132                         array_push($found_id, $row->question_id);
01133                 }
01134 
01135                 $query = sprintf("SELECT * FROM tst_test_result WHERE question_fi IN (%s)",
01136                         join($found_id, ","));
01137                 $result = $ilDB->query($query);
01138 
01139 /*              $query = sprintf("SELECT * FROM tst_solutions WHERE question_fi IN (%s) GROUP BY CONCAT(user_fi,test_fi)",
01140                         join($found_id, ","));
01141 
01142                 $result = $ilDB->query($query);
01143 */
01144                 return $result->numRows();
01145         }
01146 
01147 
01154         function _getTotalRightAnswers($a_q_id)
01155         {
01156                 global $ilDB;
01157                 $query = sprintf("SELECT question_id FROM qpl_questions WHERE original_id = %s OR question_id = %s",
01158                         $ilDB->quote($a_q_id),
01159                         $ilDB->quote($a_q_id)
01160                 );
01161                 $result = $ilDB->query($query);
01162                 if ($result->numRows() == 0)
01163                 {
01164                         return 0;
01165                 }
01166                 $found_id = array();
01167                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
01168                 {
01169                         array_push($found_id, $row->question_id);
01170                 }
01171                 $query = sprintf("SELECT * FROM tst_test_result WHERE question_fi IN (%s)",
01172                         join($found_id, ","));
01173                 $result = $ilDB->query($query);
01174 /*              $query = sprintf("SELECT * FROM tst_solutions WHERE question_fi IN (%s) GROUP BY CONCAT(user_fi,test_fi)",
01175                         join($found_id, ",")
01176                 );
01177                 $result = $ilDB->query($query);*/
01178                 $answers = array();
01179                 while ($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
01180                 {
01181                         $reached = $row->points; //ASS_Question::_getReachedPoints($row->user_fi, $row->test_fi, $row->question_fi);
01182                         $max = ASS_Question::_getMaximumPoints($row->question_fi);
01183                         array_push($answers, array("reached" => $reached, "max" => $max));
01184                 }
01185                 $max = 0.0;
01186                 $reached = 0.0;
01187                 foreach ($answers as $key => $value)
01188                 {
01189                         $max += $value["max"];
01190                         $reached += $value["reached"];
01191                 }
01192                 if ($max > 0)
01193                 {
01194                         return $reached / $max;
01195                 }
01196                 else
01197                 {
01198                         return 0;
01199                 }
01200         }
01201 
01207         function _getTitle($a_q_id)
01208         {
01209                 global $ilDB;
01210                 $query = sprintf("SELECT title FROM qpl_questions WHERE question_id = %s",
01211                         $ilDB->quote($a_q_id)
01212                 );
01213                 $result = $ilDB->query($query);
01214                 if ($result->numRows() == 1)
01215                 {
01216                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
01217                         return $row["title"];
01218                 }
01219                 else
01220                 {
01221                         return "";
01222                 }
01223         }
01224         
01225         function copyPageOfQuestion($a_q_id)
01226         {
01227                 if ($a_q_id > 0)
01228                 {
01229                         $page = new ilPageObject("qpl", $a_q_id);
01230 
01231                         $xml = str_replace("il__qst_".$a_q_id, "il__qst_".$this->id,
01232                                 $page->getXMLContent());
01233                         $this->page->setXMLContent($xml);
01234                         $this->page->saveMobUsage($xml);
01235                         $this->page->updateFromXML();
01236                 }
01237         }
01238 
01239         function getPageOfQuestion()
01240         {
01241                 $page = new ilPageObject("qpl", $this->id);
01242                 return $page->getXMLContent();
01243         }
01244 
01254   function _getQuestionType($question_id) {
01255                 global $ilDB;
01256 
01257     if ($question_id < 1)
01258       return "";
01259 
01260     $query = sprintf("SELECT type_tag FROM qpl_questions, qpl_question_type WHERE qpl_questions.question_id = %s AND qpl_questions.question_type_fi = qpl_question_type.question_type_id",
01261       $ilDB->quote($question_id)
01262     );
01263     $result = $ilDB->query($query);
01264     if ($result->numRows() == 1) {
01265       $data = $result->fetchRow(DB_FETCHMODE_OBJECT);
01266       return $data->type_tag;
01267     } else {
01268       return "";
01269     }
01270   }
01271 
01281   function _getQuestionTitle($question_id) {
01282                 global $ilDB;
01283 
01284     if ($question_id < 1)
01285       return "";
01286 
01287     $query = sprintf("SELECT title FROM qpl_questions WHERE qpl_questions.question_id = %s",
01288       $ilDB->quote($question_id)
01289     );
01290     $result = $ilDB->query($query);
01291     if ($result->numRows() == 1) {
01292       $data = $result->fetchRow(DB_FETCHMODE_ASSOC);
01293       return $data["title"];
01294     } else {
01295       return "";
01296     }
01297   }
01298 
01307         function loadFromDb($question_id)
01308         {
01309                 $query = sprintf("SELECT * FROM qpl_suggested_solutions WHERE question_fi = %s",
01310                         $this->ilias->db->quote($this->getId() . "")
01311                 );
01312                 $result = $this->ilias->db->query($query);
01313                 $this->suggested_solutions = array();
01314                 if ($result->numRows())
01315                 {
01316                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
01317                         {
01318                                 $this->suggested_solutions[$row["subquestion_index"]] = array(
01319                                         "internal_link" => $row["internal_link"],
01320                                         "import_id" => $row["import_id"]
01321                                 );
01322                         }
01323                 }
01324         }
01325 
01334         function saveToDb($original_id = "")
01335         {
01336                 require_once "./content/classes/Pages/class.ilInternalLink.php";
01337                 $query = sprintf("DELETE FROM qpl_suggested_solutions WHERE question_fi = %s",
01338                         $this->ilias->db->quote($this->getId() . "")
01339                 );
01340                 $result = $this->ilias->db->query($query);
01341                 ilInternalLink::_deleteAllLinksOfSource("qst", $this->getId());
01342                 foreach ($this->suggested_solutions as $index => $solution)
01343                 {
01344                         $query = sprintf("INSERT INTO qpl_suggested_solutions (suggested_solution_id, question_fi, internal_link, import_id, subquestion_index, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, NULL)",
01345                                 $this->ilias->db->quote($this->getId() . ""),
01346                                 $this->ilias->db->quote($solution["internal_link"] . ""),
01347                                 $this->ilias->db->quote($solution["import_id"] . ""),
01348                                 $this->ilias->db->quote($index . "")
01349                         );
01350                         $this->ilias->db->query($query);
01351                         if (preg_match("/il_(\d*?)_(\w+)_(\d+)/", $solution["internal_link"], $matches))
01352                         {
01353                                 ilInternalLink::_saveLink("qst", $this->getId(), $matches[2], $matches[3], $matches[1]);
01354                         }
01355                 }
01356         }
01357         
01365         function deleteSuggestedSolutions()
01366         {
01367                 // delete the links in the qpl_suggested_solutions table
01368                 $query = sprintf("DELETE FROM qpl_suggested_solutions WHERE question_fi = %s",
01369                         $this->ilias->db->quote($this->getId() . "")
01370                 );
01371                 $result = $this->ilias->db->query($query);
01372                 // delete the links in the int_link table
01373                 require_once "./content/classes/Pages/class.ilInternalLink.php";
01374                 ilInternalLink::_deleteAllLinksOfSource("qst", $this->getId());
01375         }
01376         
01386         function getSuggestedSolution($subquestion_index = 0)
01387         {
01388                 if (array_key_exists($subquestion_index, $this->suggested_solutions))
01389                 {
01390                         return $this->suggested_solutions[$subquestion_index];
01391                 }
01392                 else
01393                 {
01394                         return array();
01395                 }
01396         }
01397 
01408         function getSuggestedSolutionTitle($subquestion_index = 0)
01409         {
01410                 if (array_key_exists($subquestion_index, $this->suggested_solutions))
01411                 {
01412                         $title = $this->suggested_solutions[$subquestion_index]["internal_link"];
01413                         // TO DO: resolve internal link an get link type and title
01414                 }
01415                 else
01416                 {
01417                         $title = "";
01418                 }
01419                 return $title;
01420         }
01421 
01433         function setSuggestedSolution($solution_id = "", $subquestion_index = 0, $is_import = false)
01434         {
01435                 if (strcmp($solution_id, "") != 0)
01436                 {
01437                         $import_id = "";
01438                         if ($is_import)
01439                         {
01440                                 $import_id = $solution_id;
01441                                 $solution_id = $this->_resolveInternalLink($import_id);
01442                         }
01443                         $this->suggested_solutions[$subquestion_index] = array(
01444                                 "internal_link" => $solution_id,
01445                                 "import_id" => $import_id
01446                         );
01447                 }
01448         }
01449         
01450         function _resolveInternalLink($internal_link)
01451         {
01452                 if (preg_match("/il_(\d+)_(\w+)_(\d+)/", $internal_link, $matches))
01453                 {
01454                         require_once "./content/classes/Pages/class.ilInternalLink.php";
01455                         require_once "./content/classes/class.ilLMObject.php";
01456                         require_once "./content/classes/class.ilGlossaryTerm.php";
01457                         switch ($matches[2])
01458                         {
01459                                 case "lm":
01460                                         $resolved_link = ilLMObject::_getIdForImportId($internal_link);
01461                                         break;
01462                                 case "pg":
01463                                         $resolved_link = ilInternalLink::_getIdForImportId("PageObject", $internal_link);
01464                                         break;
01465                                 case "st":
01466                                         $resolved_link = ilInternalLink::_getIdForImportId("StructureObject", $internal_link);
01467                                         break;
01468                                 case "git":
01469                                         $resolved_link = ilInternalLink::_getIdForImportId("GlossaryItem", $internal_link);
01470                                         break;
01471                                 case "mob":
01472                                         $resolved_link = ilInternalLink::_getIdForImportId("MediaObject", $internal_link);
01473                                         break;
01474                         }
01475                         if (strcmp($resolved_link, "") == 0)
01476                         {
01477                                 $resolved_link = $internal_link;
01478                         }
01479                 }
01480                 else
01481                 {
01482                         $resolved_link = $internal_link;
01483                 }
01484                 return $resolved_link;
01485         }
01486         
01487         function _resolveIntLinks($question_id)
01488         {
01489                 global $ilDB;
01490                 $resolvedlinks = 0;
01491                 $query = sprintf("SELECT * FROM qpl_suggested_solutions WHERE question_fi = %s",
01492                         $ilDB->quote($question_id . "")
01493                 );
01494                 $result = $ilDB->query($query);
01495                 if ($result->numRows())
01496                 {
01497                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
01498                         {
01499                                 $internal_link = $row["internal_link"];
01500                                 $resolved_link = ASS_Question::_resolveInternalLink($internal_link);
01501                                 if (strcmp($internal_link, $resolved_link) != 0)
01502                                 {
01503                                         // internal link was resolved successfully
01504                                         $queryupdate = sprintf("UPDATE qpl_suggested_solutions SET internal_link = %s WHERE suggested_solution_id = %s",
01505                                                 $ilDB->quote($resolved_link),
01506                                                 $ilDB->quote($row["suggested_solution_id"] . "")
01507                                         );
01508                                         $updateresult = $ilDB->query($queryupdate);
01509                                         $resolvedlinks++;
01510                                 }
01511                         }
01512                 }
01513                 if ($resolvedlinks)
01514                 {
01515                         // there are resolved links -> reenter theses links to the database
01516 
01517                         // delete all internal links from the database
01518                         require_once "./content/classes/Pages/class.ilInternalLink.php";
01519                         ilInternalLink::_deleteAllLinksOfSource("qst", $question_id);
01520 
01521                         $query = sprintf("SELECT * FROM qpl_suggested_solutions WHERE question_fi = %s",
01522                                 $ilDB->quote($question_id . "")
01523                         );
01524                         $result = $ilDB->query($query);
01525                         if ($result->numRows())
01526                         {
01527                                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
01528                                 {
01529                                         if (preg_match("/il_(\d*?)_(\w+)_(\d+)/", $row["internal_link"], $matches))
01530                                         {
01531                                                 ilInternalLink::_saveLink("qst", $question_id, $matches[2], $matches[3], $matches[1]);
01532                                         }
01533                                 }
01534                         }
01535                 }
01536         }
01537         
01538         function _getInternalLinkHref($target = "")
01539         {
01540                 global $ilDB;
01541                 $linktypes = array(
01542                         "lm" => "LearningModule",
01543                         "pg" => "PageObject",
01544                         "st" => "StructureObject",
01545                         "git" => "GlossaryItem",
01546                         "mob" => "MediaObject"
01547                 );
01548                 $href = "";
01549                 if (preg_match("/il__(\w+)_(\d+)/", $target, $matches))
01550                 {
01551                         $type = $matches[1];
01552                         $target_id = $matches[2];
01553                         switch($linktypes[$matches[1]])
01554                         {
01555                                 case "LearningModule":
01556                                         $href = ilUtil::removeTrailingPathSeparators(ILIAS_HTTP_PATH) ."/goto.php?target=" . $type . "_" . $target_id;
01557                                         break;
01558                                 case "PageObject":
01559                                 case "StructureObject":
01560                                         $href = ilUtil::removeTrailingPathSeparators(ILIAS_HTTP_PATH) ."/goto.php?target=" . $type . "_" . $target_id;
01561                                         break;
01562                                 case "GlossaryItem":
01563                                         $href = ilUtil::removeTrailingPathSeparators(ILIAS_HTTP_PATH) ."/goto.php?target=" . $type . "_" . $target_id;
01564                                         break;
01565                                 case "MediaObject":
01566                                         $href = ilUtil::removeTrailingPathSeparators(ILIAS_HTTP_PATH) . "/content/lm_presentation.php?obj_type=" . $linktypes[$type] . "&cmd=media&ref_id=".$_GET["ref_id"]."&mob_id=".$target_id;
01567                                         break;
01568                         }
01569                 }
01570                 return $href;
01571         }
01572         
01582         function _getOriginalId($question_id)
01583         {
01584                 global $ilDB;
01585                 $query = sprintf("SELECT * FROM qpl_questions WHERE question_id = %s",
01586                         $ilDB->quote($question_id . "")
01587                 );
01588                 $result = $ilDB->query($query);
01589                 if ($result->numRows() > 0)
01590                 {
01591                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
01592                         if ($row["original_id"] > 0)
01593                         {
01594                                 return $row["original_id"];
01595                         }
01596                         else
01597                         {
01598                                 return $row["question_id"];
01599                         }
01600                 }
01601                 else
01602                 {
01603                         return "";
01604                 }
01605         }
01606 
01607         function syncWithOriginal()
01608         {
01609                 require_once "./content/classes/Pages/class.ilInternalLink.php";
01610                 $query = sprintf("DELETE FROM qpl_suggested_solutions WHERE question_fi = %s",
01611                         $this->ilias->db->quote($this->original_id . "")
01612                 );
01613                 $result = $this->ilias->db->query($query);
01614                 ilInternalLink::_deleteAllLinksOfSource("qst", $this->original_id);
01615                 foreach ($this->suggested_solutions as $index => $solution)
01616                 {
01617                         $query = sprintf("INSERT INTO qpl_suggested_solutions (suggested_solution_id, question_fi, internal_link, import_id, subquestion_index, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, NULL)",
01618                                 $this->ilias->db->quote($this->original_id . ""),
01619                                 $this->ilias->db->quote($solution["internal_link"] . ""),
01620                                 $this->ilias->db->quote($solution["import_id"] . ""),
01621                                 $this->ilias->db->quote($index . "")
01622                         );
01623                         $this->ilias->db->query($query);
01624                         if (preg_match("/il_(\d*?)_(\w+)_(\d+)/", $solution["internal_link"], $matches))
01625                         {
01626                                 ilInternalLink::_saveLink("qst", $this->original_id, $matches[2], $matches[3], $matches[1]);
01627                         }
01628                 }
01629         }
01630 
01631         function createRandomSolution($test_id, $user_id)
01632         {
01633         }
01634 
01644         function _questionExists($question_id)
01645         {
01646                 global $ilDB;
01647 
01648                 if ($question_id < 1)
01649                 {
01650                         return false;
01651                 }
01652                 
01653                 $query = sprintf("SELECT question_id FROM qpl_questions WHERE question_id = %s",
01654                         $ilDB->quote($question_id)
01655                 );
01656     $result = $ilDB->query($query);
01657                 if ($result->numRows() == 1)
01658                 {
01659                         return true;
01660                 }
01661                 else
01662                 {
01663                         return false;
01664                 }
01665         }
01666 
01677         function _isWriteable($question_id, $user_id)
01678         {
01679                 global $ilDB;
01680 
01681                 if (($question_id < 1) || ($user_id < 1))
01682                 {
01683                         return false;
01684                 }
01685                 
01686                 $query = sprintf("SELECT obj_fi FROM qpl_questions WHERE question_id = %s",
01687                         $ilDB->quote($question_id . "")
01688                 );
01689     $result = $ilDB->query($query);
01690                 if ($result->numRows() == 1)
01691                 {
01692                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
01693                         $qpl_object_id = $row["obj_fi"];
01694                         include_once "./assessment/classes/class.ilObjQuestionPool.php";
01695                         return ilObjQuestionPool::_isWriteable($qpl_object_id, $user_id);
01696                 }
01697                 else
01698                 {
01699                         return false;
01700                 }
01701         }
01702 
01712   function &_instanciateQuestion($question_id) 
01713         {
01714                 if (strcmp($question_id, "") != 0)
01715                 {
01716                         $question_type = ASS_Question::_getQuestionType($question_id);
01717                         switch ($question_type) {
01718                                 case "qt_cloze":
01719                                         $question = new ASS_ClozeTest();
01720                                         break;
01721                                 case "qt_matching":
01722                                         $question = new ASS_MatchingQuestion();
01723                                         break;
01724                                 case "qt_ordering":
01725                                         $question = new ASS_OrderingQuestion();
01726                                         break;
01727                                 case "qt_imagemap":
01728                                         $question = new ASS_ImagemapQuestion();
01729                                         break;
01730                                 case "qt_multiple_choice_sr":
01731                                 case "qt_multiple_choice_mr":
01732                                         $question = new ASS_MultipleChoice();
01733                                         break;
01734                                 case "qt_javaapplet":
01735                                         $question = new ASS_JavaApplet();
01736                                         break;
01737                                 case "qt_text":
01738                                         $question = new ASS_TextQuestion();
01739                                         break;
01740                         }
01741                         $question->loadFromDb($question_id);
01742                         return $question;
01743                 }
01744   }
01745         
01754         function getPoints()
01755         {
01756                 if (strcmp($this->points, "") == 0)
01757                 {
01758                         return 0;
01759                 }
01760                 else
01761                 {
01762                         return $this->points;
01763                 }
01764         }
01765 
01766         
01775         function setPoints($a_points)
01776         {
01777                 $this->points = $a_points;
01778         }
01779         
01780         function getSolutionCommentMCScoring($test_id)
01781         {
01782                 $result = "";
01783                 include_once "./assessment/classes/class.ilObjTest.php";
01784                 if (ilObjTest::_getMCScoring($test_id) == SCORE_ZERO_POINTS_WHEN_UNANSWERED)
01785                 {
01786                         $result = $this->lng->txt("solution_comment_mc_scoring");
01787                 }
01788                 return $result;
01789         }
01790 
01791         function getSolutionCommentCountSystem($test_id)
01792         {
01793                 $result = "";
01794                 include_once "./assessment/classes/class.ilObjTest.php";
01795                 if (ilObjTest::_getCountSystem($test_id) == COUNT_CORRECT_SOLUTIONS )
01796                 {
01797                         $result = $this->lng->txt("solution_comment_count_system");
01798                 }
01799                 return $result;
01800         }
01809         function _isUsedInRandomTest($question_id = "")
01810         {
01811                 global $ilDB;
01812                 
01813                 if ($question_id < 1) return 0;
01814                 $query = sprintf("SELECT test_random_question_id FROM tst_test_random_question WHERE question_fi = %s",
01815                         $ilDB->quote($question_id . "")
01816                 );
01817                 $result = $ilDB->query($query);
01818                 return $result->numRows();
01819         }
01820 
01821 }
01822 
01823 ?>

Generated on Fri Dec 13 2013 10:18:25 for ILIAS Release_3_5_x_branch .rev 46805 by  doxygen 1.7.1