ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.assNumeric.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 
5 include_once "./Modules/TestQuestionPool/classes/class.assQuestion.php";
6 include_once "./Modules/Test/classes/inc.AssessmentConstants.php";
7 
23 class assNumeric extends assQuestion
24 {
25  protected $lower_limit;
26  protected $upper_limit;
27 
33  var $maxchars;
34 
48  function __construct(
49  $title = "",
50  $comment = "",
51  $author = "",
52  $owner = -1,
53  $question = ""
54  )
55  {
57  $this->maxchars = 6;
58  }
59 
66  function isComplete()
67  {
68  if (strlen($this->title) and ($this->author) and ($this->question) and ($this->getMaximumPoints() > 0))
69  {
70  return true;
71  }
72  else
73  {
74  return false;
75  }
76  }
77 
84  function saveToDb($original_id = "")
85  {
86  global $ilDB;
87 
89 
90  // save additional data
91  $affectedRows = $ilDB->manipulateF("DELETE FROM " . $this->getAdditionalTableName() . " WHERE question_fi = %s",
92  array("integer"),
93  array($this->getId())
94  );
95 
96  $affectedRows = $ilDB->manipulateF("INSERT INTO " . $this->getAdditionalTableName() . " (question_fi, maxnumofchars) VALUES (%s, %s)",
97  array("integer", "integer"),
98  array(
99  $this->getId(),
100  ($this->getMaxChars()) ? $this->getMaxChars() : 0
101  )
102  );
103 
104  // Write range to the database
105 
106  // 1. delete old range
107  $result = $ilDB->manipulateF("DELETE FROM qpl_num_range WHERE question_fi = %s",
108  array('integer'),
109  array($this->getId())
110  );
111 
112  // 2. write range
113  $next_id = $ilDB->nextId('qpl_num_range');
114  $answer_result = $ilDB->manipulateF("INSERT INTO qpl_num_range (range_id, question_fi, lowerlimit, upperlimit, points, aorder, tstamp) VALUES (%s, %s, %s, %s, %s, %s, %s)",
115  array('integer','integer', 'text', 'text', 'float', 'integer', 'integer'),
116  array($next_id, $this->id, $this->getLowerLimit(), $this->getUpperLimit(), $this->getPoints(), 0, time())
117  );
118 
120  }
121 
129  function loadFromDb($question_id)
130  {
131  global $ilDB;
132 
133  $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",
134  array("integer"),
135  array($question_id)
136  );
137  if ($result->numRows() == 1)
138  {
139  $data = $ilDB->fetchAssoc($result);
140  $this->setId($question_id);
141  $this->setObjId($data["obj_fi"]);
142  $this->setTitle($data["title"]);
143  $this->setComment($data["description"]);
144  $this->setNrOfTries($data['nr_of_tries']);
145  $this->setOriginalId($data["original_id"]);
146  $this->setAuthor($data["author"]);
147  $this->setPoints($data["points"]);
148  $this->setOwner($data["owner"]);
149  include_once("./Services/RTE/classes/class.ilRTE.php");
150  $this->setQuestion(ilRTE::_replaceMediaObjectImageSrc($data["question_text"], 1));
151  $this->setMaxChars($data["maxnumofchars"]);
152  $this->setEstimatedWorkingTime(substr($data["working_time"], 0, 2), substr($data["working_time"], 3, 2), substr($data["working_time"], 6, 2));
153  }
154 
155 
156  $result = $ilDB->queryF("SELECT * FROM qpl_num_range WHERE question_fi = %s ORDER BY aorder ASC",
157  array('integer'),
158  array($question_id)
159  );
160 
161  include_once "./Modules/TestQuestionPool/classes/class.assNumericRange.php";
162  if ($result->numRows() > 0)
163  {
164  while ($data = $ilDB->fetchAssoc($result))
165  {
166  $this->setPoints($data['points']);
167  $this->setLowerLimit($data['lowerlimit']);
168  $this->setUpperLimit($data['upperlimit']);
169  }
170  }
171 
172  parent::loadFromDb($question_id);
173  }
174 
180  function duplicate($for_test = true, $title = "", $author = "", $owner = "", $testObjId = null)
181  {
182  if ($this->id <= 0)
183  {
184  // The question has not been saved. It cannot be duplicated
185  return;
186  }
187  // duplicate the question in database
188  $this_id = $this->getId();
189 
190  if( (int)$testObjId > 0 )
191  {
192  $thisObjId = $this->getObjId();
193  }
194 
195  $clone = $this;
196  include_once ("./Modules/TestQuestionPool/classes/class.assQuestion.php");
198  $clone->id = -1;
199 
200  if( (int)$testObjId > 0 )
201  {
202  $clone->setObjId($testObjId);
203  }
204 
205  if ($title)
206  {
207  $clone->setTitle($title);
208  }
209 
210  if ($author)
211  {
212  $clone->setAuthor($author);
213  }
214  if ($owner)
215  {
216  $clone->setOwner($owner);
217  }
218 
219  if ($for_test)
220  {
221  $clone->saveToDb($original_id);
222  }
223  else
224  {
225  $clone->saveToDb();
226  }
227 
228  // copy question page content
229  $clone->copyPageOfQuestion($this_id);
230  // copy XHTML media objects
231  $clone->copyXHTMLMediaObjectsOfQuestion($this_id);
232  // duplicate the generic feedback
233  $clone->duplicateGenericFeedback($this_id);
234 
235  $clone->onDuplicate($thisObjId, $this_id, $clone->getObjId(), $clone->getId());
236 
237  return $clone->id;
238  }
239 
245  function copyObject($target_questionpool, $title = "")
246  {
247  if ($this->id <= 0)
248  {
249  // The question has not been saved. It cannot be duplicated
250  return;
251  }
252  // duplicate the question in database
253  $clone = $this;
254  include_once ("./Modules/TestQuestionPool/classes/class.assQuestion.php");
256  $clone->id = -1;
257  $source_questionpool = $this->getObjId();
258  $clone->setObjId($target_questionpool);
259  if ($title)
260  {
261  $clone->setTitle($title);
262  }
263  $clone->saveToDb();
264 
265  // copy question page content
266  $clone->copyPageOfQuestion($original_id);
267  // copy XHTML media objects
268  $clone->copyXHTMLMediaObjectsOfQuestion($original_id);
269  // duplicate the generic feedback
270  $clone->duplicateGenericFeedback($original_id);
271 
272  $clone->onCopy($this->getObjId(), $this->getId());
273  return $clone->id;
274  }
275 
276  function getLowerLimit()
277  {
278  return $this->lower_limit;
279  }
280 
281  function getUpperLimit()
282  {
283  return $this->upper_limit;
284  }
285 
286  function setLowerLimit($a_limit)
287  {
288  $a_limit = str_replace(',', '.', $a_limit);
289  $this->lower_limit = $a_limit;
290  }
291 
292  function setUpperLimit($a_limit)
293  {
294  $a_limit = str_replace(',', '.', $a_limit);
295  $this->upper_limit = $a_limit;
296  }
297 
304  function getMaximumPoints()
305  {
306  return $this->getPoints();
307  }
308 
319  public function calculateReachedPoints($active_id, $pass = NULL, $returndetails = FALSE)
320  {
321  if( $returndetails )
322  {
323  throw new ilTestException('return details not implemented for '.__METHOD__);
324  }
325 
326  global $ilDB;
327 
328  $found_values = array();
329  if (is_null($pass))
330  {
331  $pass = $this->getSolutionMaxPass($active_id);
332  }
333  $result = $ilDB->queryF("SELECT * FROM tst_solutions WHERE active_fi = %s AND question_fi = %s AND pass = %s",
334  array('integer','integer','integer'),
335  array($active_id, $this->getId(), $pass)
336  );
337  $data = $ilDB->fetchAssoc($result);
338 
339  $enteredvalue = $data["value1"];
340 
341  $points = 0;
342  if ($this->contains($enteredvalue))
343  {
344  $points = $this->getPoints();
345  }
346 
347  return $points;
348  }
349 
359  function contains($value)
360  {
361  include_once "./Services/Math/classes/class.EvalMath.php";
362  $eval = new EvalMath();
363  $eval->suppress_errors = TRUE;
364  $result = $eval->e($value);
365  if (($result === FALSE) || ($result === TRUE)) return FALSE;
366  if (($result >= $eval->e($this->getLowerLimit())) && ($result <= $eval->e($this->getUpperLimit())))
367  {
368  return TRUE;
369  }
370  else
371  {
372  return FALSE;
373  }
374  }
375 
384  public function saveWorkingData($active_id, $pass = NULL)
385  {
386  global $ilDB;
387  global $ilUser;
388 
389  if (is_null($pass))
390  {
391  include_once "./Modules/Test/classes/class.ilObjTest.php";
392  $pass = ilObjTest::_getPass($active_id);
393  }
394  $entered_values = 0;
395  $numeric_result = str_replace(",",".",$_POST["numeric_result"]);
396 
397  include_once "./Services/Math/classes/class.EvalMath.php";
398  $math = new EvalMath();
399  $math->suppress_errors = TRUE;
400  $result = $math->evaluate($numeric_result);
401  $returnvalue = true;
402  if ((($result === FALSE) || ($result === TRUE)) && (strlen($result) > 0))
403  {
404  ilUtil::sendInfo($this->lng->txt("err_no_numeric_value"), true);
405  $returnvalue = false;
406  }
407  $result = $ilDB->queryF("SELECT solution_id FROM tst_solutions WHERE active_fi = %s AND question_fi = %s AND pass = %s",
408  array('integer','integer','integer'),
409  array($active_id, $this->getId(), $pass)
410  );
411  $row = $ilDB->fetchAssoc($result);
412  $update = $row["solution_id"];
413  if ($update)
414  {
415  if (strlen($numeric_result))
416  {
417  $affectedRows = $ilDB->update("tst_solutions", array(
418  "value1" => array("clob", trim($numeric_result)),
419  "tstamp" => array("integer", time())
420  ), array(
421  "solution_id" => array("integer", $update)
422  ));
423  $entered_values++;
424  }
425  else
426  {
427  $affectedRows = $ilDB->manipulateF("DELETE FROM tst_solutions WHERE solution_id = %s",
428  array('integer'),
429  array($update)
430  );
431  }
432  }
433  else
434  {
435  if (strlen($numeric_result))
436  {
437  $next_id = $ilDB->nextId('tst_solutions');
438  $affectedRows = $ilDB->insert("tst_solutions", array(
439  "solution_id" => array("integer", $next_id),
440  "active_fi" => array("integer", $active_id),
441  "question_fi" => array("integer", $this->getId()),
442  "value1" => array("clob", trim($numeric_result)),
443  "value2" => array("clob", null),
444  "pass" => array("integer", $pass),
445  "tstamp" => array("integer", time())
446  ));
447  $entered_values++;
448  }
449  }
450  if ($entered_values)
451  {
452  include_once ("./Modules/Test/classes/class.ilObjAssessmentFolder.php");
454  {
455  $this->logAction($this->lng->txtlng("assessment", "log_user_entered_values", ilObjAssessmentFolder::_getLogLanguage()), $active_id, $this->getId());
456  }
457  }
458  else
459  {
460  include_once ("./Modules/Test/classes/class.ilObjAssessmentFolder.php");
462  {
463  $this->logAction($this->lng->txtlng("assessment", "log_user_not_entered_values", ilObjAssessmentFolder::_getLogLanguage()), $active_id, $this->getId());
464  }
465  }
466 
467  return $returnvalue;
468  }
469 
478  protected function reworkWorkingData($active_id, $pass, $obligationsAnswered)
479  {
480  // nothing to rework!
481  }
482 
489  function getQuestionType()
490  {
491  return "assNumeric";
492  }
493 
500  function getMaxChars()
501  {
502  return $this->maxchars;
503  }
504 
512  {
513  $this->maxchars = $maxchars;
514  }
515 
523  {
524  return "qpl_qst_numeric";
525  }
526 
532  {
534  }
535 
548  public function setExportDetailsXLS(&$worksheet, $startrow, $active_id, $pass, &$format_title, &$format_bold)
549  {
550  include_once ("./Services/Excel/classes/class.ilExcelUtils.php");
551  $solutions = $this->getSolutionValues($active_id, $pass);
552  $worksheet->writeString($startrow, 0, ilExcelUtils::_convert_text($this->lng->txt($this->getQuestionType())), $format_title);
553  $worksheet->writeString($startrow, 1, ilExcelUtils::_convert_text($this->getTitle()), $format_title);
554  $i = 1;
555  $worksheet->writeString($startrow + $i, 0, ilExcelUtils::_convert_text($this->lng->txt("result")), $format_bold);
556  if (strlen($solutions[0]["value1"]))
557  {
558  $worksheet->write($startrow + $i, 1, ilExcelUtils::_convert_text($solutions[0]["value1"]));
559  }
560  $i++;
561  return $startrow + $i + 1;
562  }
563 }
564 
565 ?>