ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.assTextQuestion.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 include_once "./Modules/TestQuestionPool/classes/class.assQuestion.php";
24 include_once "./Modules/Test/classes/inc.AssessmentConstants.php";
25 
36 {
45 
54  var $keywords;
55 
62 
63  /* method for automatic string matching */
64  private $matchcondition;
65 
79  function __construct(
80  $title = "",
81  $comment = "",
82  $author = "",
83  $owner = -1,
84  $question = ""
85  )
86  {
88  $this->maxNumOfChars = 0;
89  $this->points = 0;
90  $this->keywords = "";
91  $this->matchcondition = 0;
92  }
93 
100  function isComplete()
101  {
102  if (($this->title) and ($this->author) and ($this->question) and ($this->getMaximumPoints() > 0))
103  {
104  return true;
105  }
106  else
107  {
108  return false;
109  }
110  }
111 
118  function saveToDb($original_id = "")
119  {
120  global $ilDB;
121 
123 
124  // save additional data
125  $affectedRows = $ilDB->manipulateF("DELETE FROM " . $this->getAdditionalTableName() . " WHERE question_fi = %s",
126  array("integer"),
127  array($this->getId())
128  );
129 
130  $affectedRows = $ilDB->manipulateF("INSERT INTO " . $this->getAdditionalTableName() . " (question_fi, maxnumofchars, keywords, textgap_rating, matchcondition) VALUES (%s, %s, %s, %s, %s)",
131  array("integer", "integer", "text", "text", 'integer'),
132  array(
133  $this->getId(),
134  $this->getMaxNumOfChars(),
135  (strlen($this->getKeywords())) ? $this->getKeywords() : NULL,
136  $this->getTextRating(),
137  $this->matchcondition
138  )
139  );
140 
142  }
143 
151  function loadFromDb($question_id)
152  {
153  global $ilDB;
154 
155  $result = $ilDB->queryF("SELECT qpl_questions.*, " . $this->getAdditionalTableName() . ".* FROM qpl_questions LEFT JOIN " . $this->getAdditionalTableName() . " ON " . $this->getAdditionalTableName() . ".question_fi = qpl_questions.question_id WHERE qpl_questions.question_id = %s",
156  array("integer"),
157  array($question_id)
158  );
159  if ($result->numRows() == 1)
160  {
161  $data = $ilDB->fetchAssoc($result);
162  $this->setId($question_id);
163  $this->setObjId($data["obj_fi"]);
164  $this->setTitle($data["title"]);
165  $this->setComment($data["description"]);
166  $this->setOriginalId($data["original_id"]);
167  $this->setNrOfTries($data['nr_of_tries']);
168  $this->setAuthor($data["author"]);
169  $this->setPoints($data["points"]);
170  $this->setOwner($data["owner"]);
171  include_once("./Services/RTE/classes/class.ilRTE.php");
172  $this->setQuestion(ilRTE::_replaceMediaObjectImageSrc($data["question_text"], 1));
173  $this->setShuffle($data["shuffle"]);
174  $this->setMaxNumOfChars($data["maxnumofchars"]);
175  $this->setKeywords($data["keywords"]);
176  $this->setTextRating($data["textgap_rating"]);
177  $this->matchcondition = (strlen($data['matchcondition'])) ? $data['matchcondition'] : 0;
178  $this->setEstimatedWorkingTime(substr($data["working_time"], 0, 2), substr($data["working_time"], 3, 2), substr($data["working_time"], 6, 2));
179  }
180  parent::loadFromDb($question_id);
181  }
182 
188  function duplicate($for_test = true, $title = "", $author = "", $owner = "")
189  {
190  if ($this->id <= 0)
191  {
192  // The question has not been saved. It cannot be duplicated
193  return;
194  }
195  // duplicate the question in database
196  $this_id = $this->getId();
197  $clone = $this;
198  include_once ("./Modules/TestQuestionPool/classes/class.assQuestion.php");
200  $clone->id = -1;
201  if ($title)
202  {
203  $clone->setTitle($title);
204  }
205 
206  if ($author)
207  {
208  $clone->setAuthor($author);
209  }
210  if ($owner)
211  {
212  $clone->setOwner($owner);
213  }
214 
215  if ($for_test)
216  {
217  $clone->saveToDb($original_id);
218  }
219  else
220  {
221  $clone->saveToDb();
222  }
223 
224  // copy question page content
225  $clone->copyPageOfQuestion($this_id);
226  // copy XHTML media objects
227  $clone->copyXHTMLMediaObjectsOfQuestion($this_id);
228  // duplicate the generic feedback
229  $clone->duplicateFeedbackGeneric($this_id);
230  $clone->onDuplicate($this_id);
231 
232  return $clone->id;
233  }
234 
240  function copyObject($target_questionpool, $title = "")
241  {
242  if ($this->id <= 0)
243  {
244  // The question has not been saved. It cannot be duplicated
245  return;
246  }
247  // duplicate the question in database
248  $clone = $this;
249  include_once ("./Modules/TestQuestionPool/classes/class.assQuestion.php");
251  $clone->id = -1;
252  $source_questionpool = $this->getObjId();
253  $clone->setObjId($target_questionpool);
254  if ($title)
255  {
256  $clone->setTitle($title);
257  }
258  $clone->saveToDb();
259 
260  // copy question page content
261  $clone->copyPageOfQuestion($original_id);
262  // copy XHTML media objects
263  $clone->copyXHTMLMediaObjectsOfQuestion($original_id);
264  // duplicate the generic feedback
265  $clone->duplicateFeedbackGeneric($original_id);
266  $clone->onCopy($this->getObjId(), $this->getId());
267 
268  return $clone->id;
269  }
270 
278  function getMaxNumOfChars()
279  {
280  if (strcmp($this->maxNumOfChars, "") == 0)
281  {
282  return 0;
283  }
284  else
285  {
286  return $this->maxNumOfChars;
287  }
288  }
289 
297  function setMaxNumOfChars($maxchars = 0)
298  {
299  $this->maxNumOfChars = $maxchars;
300  }
301 
308  function getMaximumPoints()
309  {
310  return $this->points;
311  }
312 
322  function setReachedPoints($active_id, $points, $pass = NULL)
323  {
324  global $ilDB;
325 
326  if (($points > 0) && ($points <= $this->getPoints()))
327  {
328  if (is_null($pass))
329  {
330  $pass = $this->getSolutionMaxPass($active_id);
331  }
332  $affectedRows = $ilDB->manipulateF("UPDATE tst_test_result SET points = %s WHERE active_fi = %s AND question_fi = %s AND pass = %s",
333  array('float','integer','integer','integer'),
334  array($points, $active_id, $this->getId(), $pass)
335  );
336  $this->_updateTestPassResults($active_id, $pass);
337  return TRUE;
338  }
339  else
340  {
341  return TRUE;
342  }
343  }
344 
353  function isKeywordMatching($answertext, $a_keyword)
354  {
355  $result = FALSE;
356  $textrating = $this->getTextRating();
357  include_once "./Services/Utilities/classes/class.ilStr.php";
358  switch ($textrating)
359  {
361  if (ilStr::strPos(ilStr::strToLower($answertext), ilStr::strToLower($a_keyword)) !== false) return TRUE;
362  break;
364  if (ilStr::strPos($answertext, $a_keyword) !== false) return TRUE;
365  break;
366  }
367  $answerwords = array();
368  if (preg_match_all("/([^\s.]+)/", $answertext, $matches))
369  {
370  foreach ($matches[1] as $answerword)
371  {
372  array_push($answerwords, trim($answerword));
373  }
374  }
375  foreach ($answerwords as $a_original)
376  {
377  switch ($textrating)
378  {
380  if (levenshtein($a_original, $a_keyword) <= 1) return TRUE;
381  break;
383  if (levenshtein($a_original, $a_keyword) <= 2) return TRUE;
384  break;
386  if (levenshtein($a_original, $a_keyword) <= 3) return TRUE;
387  break;
389  if (levenshtein($a_original, $a_keyword) <= 4) return TRUE;
390  break;
392  if (levenshtein($a_original, $a_keyword) <= 5) return TRUE;
393  break;
394  }
395  }
396  return $result;
397  }
398 
408  function calculateReachedPoints($active_id, $pass = NULL)
409  {
410  global $ilDB;
411 
412  $points = 0;
413  if (is_null($pass))
414  {
415  $pass = $this->getSolutionMaxPass($active_id);
416  }
417  $result = $ilDB->queryF("SELECT * FROM tst_solutions WHERE active_fi = %s AND question_fi = %s AND pass = %s",
418  array('integer','integer','integer'),
419  array($active_id, $this->getId(), $pass)
420  );
421  if ($result->numRows() == 1)
422  {
423  $row = $ilDB->fetchAssoc($result);
424  if ($row["points"])
425  {
426  $points = $row["points"];
427  }
428  else
429  {
430  $keywords =& $this->getKeywordList();
431  if (count($keywords))
432  {
433  if ($this->matchcondition == 0)
434  {
435  $foundkeyword = false;
436  }
437  else
438  {
439  $foundkeyword = true;
440  }
441  foreach ($keywords as $keyword)
442  {
443  if ($this->matchcondition == 0)
444  {
445  // OR: only one keyword needs to match
446  $foundkeyword = $foundkeyword | $this->isKeywordMatching($row["value1"], $keyword);
447  }
448  else
449  {
450  // AND: all keywords needs to match
451  $foundkeyword = $foundkeyword & $this->isKeywordMatching($row["value1"], $keyword);
452  }
453  }
454  if ($foundkeyword) $points = $this->getMaximumPoints();
455  }
456  }
457  }
458 
459  $points = parent::calculateReachedPoints($active_id, $pass = NULL, $points);
460  return $points;
461  }
462 
471  function saveWorkingData($active_id, $pass = NULL)
472  {
473  global $ilDB;
474  global $ilUser;
475 
476  include_once "./Services/Utilities/classes/class.ilStr.php";
477  if (is_null($pass))
478  {
479  include_once "./Modules/Test/classes/class.ilObjTest.php";
480  $pass = ilObjTest::_getPass($active_id);
481  }
482  $affectedRows = $ilDB->manipulateF("DELETE FROM tst_solutions WHERE active_fi = %s AND question_fi = %s AND pass = %s",
483  array('integer','integer','integer'),
484  array($active_id, $this->getId(), $pass)
485  );
486  $text = ilUtil::stripSlashes($_POST["TEXT"], FALSE);
487  if ($this->getMaxNumOfChars())
488  {
489  include_once "./Services/Utilities/classes/class.ilStr.php";
490  $text_without_tags = preg_replace("/<[^>*?]>/is", "", $text);
491  $len_with_tags = ilStr::strLen($text);
492  $len_without_tags = ilStr::strLen($text_without_tags);
493  if ($this->getMaxNumOfChars() < $len_without_tags)
494  {
495  if (!$this->isHTML($text))
496  {
497  $text = ilStr::subStr($text, 0, $this->getMaxNumOfChars());
498  }
499  }
500  }
501  if ($this->isHTML($text))
502  {
503  $text = preg_replace("/<[^>]*$/ims", "", $text);
504  }
505  else
506  {
507  //$text = htmlentities($text, ENT_QUOTES, "UTF-8");
508  }
509  $entered_values = 0;
510  if (strlen($text))
511  {
512  $next_id = $ilDB->nextId('tst_solutions');
513  $affectedRows = $ilDB->insert("tst_solutions", array(
514  "solution_id" => array("integer", $next_id),
515  "active_fi" => array("integer", $active_id),
516  "question_fi" => array("integer", $this->getId()),
517  "value1" => array("clob", trim($text)),
518  "value2" => array("clob", null),
519  "pass" => array("integer", $pass),
520  "tstamp" => array("integer", time())
521  ));
522  $entered_values++;
523  }
524  if ($entered_values)
525  {
526  include_once ("./Modules/Test/classes/class.ilObjAssessmentFolder.php");
528  {
529  $this->logAction($this->lng->txtlng("assessment", "log_user_entered_values", ilObjAssessmentFolder::_getLogLanguage()), $active_id, $this->getId());
530  }
531  }
532  else
533  {
534  include_once ("./Modules/Test/classes/class.ilObjAssessmentFolder.php");
536  {
537  $this->logAction($this->lng->txtlng("assessment", "log_user_not_entered_values", ilObjAssessmentFolder::_getLogLanguage()), $active_id, $this->getId());
538  }
539  }
540  parent::saveWorkingData($active_id, $pass);
541  return true;
542  }
543 
544  function createRandomSolution($test_id, $user_id)
545  {
546  }
547 
554  function getQuestionType()
555  {
556  return "assTextQuestion";
557  }
558 
565  function getKeywords()
566  {
567  return $this->keywords;
568  }
569 
576  function setKeywords($a_keywords)
577  {
578  $this->keywords = $a_keywords;
579  }
580 
587  function &getKeywordList()
588  {
589  $keywords = array();
590  if (preg_match_all("/([^\s]+)/", $this->keywords, $matches))
591  {
592  foreach ($matches[1] as $keyword)
593  {
594  array_push($keywords, trim($keyword));
595  }
596  }
597  return $keywords;
598  }
599 
607  function getTextRating()
608  {
609  return $this->text_rating;
610  }
611 
619  function setTextRating($a_text_rating)
620  {
621  switch ($a_text_rating)
622  {
630  $this->text_rating = $a_text_rating;
631  break;
632  default:
633  $this->text_rating = TEXTGAP_RATING_CASEINSENSITIVE;
634  break;
635  }
636  }
637 
645  {
646  return "qpl_qst_essay";
647  }
648 
654  {
656  }
657 
667  public function setExportDetailsXLS(&$adapter, $startrow, $active_id, $pass)
668  {
669  $solutions = $this->getSolutionValues($active_id, $pass);
670  $adapter->setCellValue($startrow, 0, $this->lng->txt($this->getQuestionType()), CELL_FORMAT_TITLE);
671  $adapter->setCellValue($startrow, 1, $this->getTitle(), CELL_FORMAT_TITLE);
672  $i = 1;
673  $adapter->setCellValue($startrow + $i, 0, $this->lng->txt("result"), CELL_FORMAT_BOLD);
674  if (strlen($solutions[0]["value1"]))
675  {
676  $adapter->setCellValue($startrow + $i, 1, $solutions[0]["value1"]);
677  }
678  $i++;
679  return $startrow + $i + 1;
680  }
681 
685  public function toJSON()
686  {
687  include_once("./Services/RTE/classes/class.ilRTE.php");
688  $result = array();
689  $result['id'] = (int) $this->getId();
690  $result['type'] = (string) $this->getQuestionType();
691  $result['title'] = (string) $this->getTitle();
692  $result['question'] = (string) ilRTE::_replaceMediaObjectImageSrc($this->getQuestion(), 0);
693  $result['nr_of_tries'] = (int) $this->getNrOfTries();
694  $result['shuffle'] = (bool) $this->getShuffle();
695  $result['maxlength'] = (int) $this->getMaxNumOfChars();
696  return json_encode($result);
697  }
698 
699 }
700 
701 ?>