ILIAS  Release_4_2_x_branch Revision 61807
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.assNumeric.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 
37 class assNumeric extends assQuestion
38 {
39  protected $lower_limit;
40  protected $upper_limit;
41 
47  var $maxchars;
48 
62  function __construct(
63  $title = "",
64  $comment = "",
65  $author = "",
66  $owner = -1,
67  $question = ""
68  )
69  {
71  $this->maxchars = 6;
72  }
73 
80  function isComplete()
81  {
82  if (strlen($this->title) and ($this->author) and ($this->question) and ($this->getMaximumPoints() > 0))
83  {
84  return true;
85  }
86  else
87  {
88  return false;
89  }
90  }
91 
98  function saveToDb($original_id = "")
99  {
100  global $ilDB;
101 
103 
104  // save additional data
105  $affectedRows = $ilDB->manipulateF("DELETE FROM " . $this->getAdditionalTableName() . " WHERE question_fi = %s",
106  array("integer"),
107  array($this->getId())
108  );
109 
110  $affectedRows = $ilDB->manipulateF("INSERT INTO " . $this->getAdditionalTableName() . " (question_fi, maxnumofchars) VALUES (%s, %s)",
111  array("integer", "integer"),
112  array(
113  $this->getId(),
114  ($this->getMaxChars()) ? $this->getMaxChars() : 0
115  )
116  );
117 
118  // Write range to the database
119 
120  // 1. delete old range
121  $result = $ilDB->manipulateF("DELETE FROM qpl_num_range WHERE question_fi = %s",
122  array('integer'),
123  array($this->getId())
124  );
125 
126  // 2. write range
127  $next_id = $ilDB->nextId('qpl_num_range');
128  $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)",
129  array('integer','integer', 'text', 'text', 'float', 'integer', 'integer'),
130  array($next_id, $this->id, $this->getLowerLimit(), $this->getUpperLimit(), $this->getPoints(), 0, time())
131  );
132 
134  }
135 
143  function loadFromDb($question_id)
144  {
145  global $ilDB;
146 
147  $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",
148  array("integer"),
149  array($question_id)
150  );
151  if ($result->numRows() == 1)
152  {
153  $data = $ilDB->fetchAssoc($result);
154  $this->setId($question_id);
155  $this->setObjId($data["obj_fi"]);
156  $this->setTitle($data["title"]);
157  $this->setComment($data["description"]);
158  $this->setNrOfTries($data['nr_of_tries']);
159  $this->setOriginalId($data["original_id"]);
160  $this->setAuthor($data["author"]);
161  $this->setPoints($data["points"]);
162  $this->setOwner($data["owner"]);
163  include_once("./Services/RTE/classes/class.ilRTE.php");
164  $this->setQuestion(ilRTE::_replaceMediaObjectImageSrc($data["question_text"], 1));
165  $this->setMaxChars($data["maxnumofchars"]);
166  $this->setEstimatedWorkingTime(substr($data["working_time"], 0, 2), substr($data["working_time"], 3, 2), substr($data["working_time"], 6, 2));
167  }
168 
169 
170  $result = $ilDB->queryF("SELECT * FROM qpl_num_range WHERE question_fi = %s ORDER BY aorder ASC",
171  array('integer'),
172  array($question_id)
173  );
174 
175  include_once "./Modules/TestQuestionPool/classes/class.assNumericRange.php";
176  if ($result->numRows() > 0)
177  {
178  while ($data = $ilDB->fetchAssoc($result))
179  {
180  $this->setPoints($data['points']);
181  $this->setLowerLimit($data['lowerlimit']);
182  $this->setUpperLimit($data['upperlimit']);
183  }
184  }
185 
186  parent::loadFromDb($question_id);
187  }
188 
194  function duplicate($for_test = true, $title = "", $author = "", $owner = "", $testObjId = null)
195  {
196  if ($this->id <= 0)
197  {
198  // The question has not been saved. It cannot be duplicated
199  return;
200  }
201  // duplicate the question in database
202  $this_id = $this->getId();
203 
204  if( (int)$testObjId > 0 )
205  {
206  $thisObjId = $this->getObjId();
207  }
208 
209  $clone = $this;
210  include_once ("./Modules/TestQuestionPool/classes/class.assQuestion.php");
212  $clone->id = -1;
213 
214  if( (int)$testObjId > 0 )
215  {
216  $clone->setObjId($testObjId);
217  }
218 
219  if ($title)
220  {
221  $clone->setTitle($title);
222  }
223 
224  if ($author)
225  {
226  $clone->setAuthor($author);
227  }
228  if ($owner)
229  {
230  $clone->setOwner($owner);
231  }
232 
233  if ($for_test)
234  {
235  $clone->saveToDb($original_id);
236  }
237  else
238  {
239  $clone->saveToDb();
240  }
241 
242  // copy question page content
243  $clone->copyPageOfQuestion($this_id);
244  // copy XHTML media objects
245  $clone->copyXHTMLMediaObjectsOfQuestion($this_id);
246  // duplicate the generic feedback
247  $clone->duplicateFeedbackGeneric($this_id);
248 
249  $clone->onDuplicate($thisObjId, $this_id, $clone->getObjId(), $clone->getId());
250 
251  return $clone->id;
252  }
253 
259  function copyObject($target_questionpool, $title = "")
260  {
261  if ($this->id <= 0)
262  {
263  // The question has not been saved. It cannot be duplicated
264  return;
265  }
266  // duplicate the question in database
267  $clone = $this;
268  include_once ("./Modules/TestQuestionPool/classes/class.assQuestion.php");
270  $clone->id = -1;
271  $source_questionpool = $this->getObjId();
272  $clone->setObjId($target_questionpool);
273  if ($title)
274  {
275  $clone->setTitle($title);
276  }
277  $clone->saveToDb();
278 
279  // copy question page content
280  $clone->copyPageOfQuestion($original_id);
281  // copy XHTML media objects
282  $clone->copyXHTMLMediaObjectsOfQuestion($original_id);
283  // duplicate the generic feedback
284  $clone->duplicateFeedbackGeneric($original_id);
285 
286  $clone->onCopy($this->getObjId(), $this->getId());
287  return $clone->id;
288  }
289 
290  function getLowerLimit()
291  {
292  return $this->lower_limit;
293  }
294 
295  function getUpperLimit()
296  {
297  return $this->upper_limit;
298  }
299 
300  function setLowerLimit($a_limit)
301  {
302  $a_limit = str_replace(',', '.', $a_limit);
303  $this->lower_limit = $a_limit;
304  }
305 
306  function setUpperLimit($a_limit)
307  {
308  $a_limit = str_replace(',', '.', $a_limit);
309  $this->upper_limit = $a_limit;
310  }
311 
318  function getMaximumPoints()
319  {
320  return $this->getPoints();
321  }
322 
332  function calculateReachedPoints($active_id, $pass = NULL)
333  {
334  global $ilDB;
335 
336  $found_values = array();
337  if (is_null($pass))
338  {
339  $pass = $this->getSolutionMaxPass($active_id);
340  }
341  $result = $ilDB->queryF("SELECT * FROM tst_solutions WHERE active_fi = %s AND question_fi = %s AND pass = %s",
342  array('integer','integer','integer'),
343  array($active_id, $this->getId(), $pass)
344  );
345  $data = $ilDB->fetchAssoc($result);
346 
347  $enteredvalue = $data["value1"];
348 
349  $points = 0;
350  if ($this->contains($enteredvalue))
351  {
352  $points = $this->getPoints();
353  }
354 
355  $points = parent::calculateReachedPoints($active_id, $pass = NULL, $points);
356  return $points;
357  }
358 
368  function contains($value)
369  {
370  include_once "./Services/Math/classes/class.EvalMath.php";
371  $eval = new EvalMath();
372  $eval->suppress_errors = TRUE;
373  $result = $eval->e($value);
374  if (($result === FALSE) || ($result === TRUE)) return FALSE;
375  if (($result >= $eval->e($this->getLowerLimit())) && ($result <= $eval->e($this->getUpperLimit())))
376  {
377  return TRUE;
378  }
379  else
380  {
381  return FALSE;
382  }
383  }
384 
391  function saveWorkingData($active_id, $pass = NULL)
392  {
393  global $ilDB;
394  global $ilUser;
395 
396  if (is_null($pass))
397  {
398  include_once "./Modules/Test/classes/class.ilObjTest.php";
399  $pass = ilObjTest::_getPass($active_id);
400  }
401  $entered_values = 0;
402  $numeric_result = str_replace(",",".",$_POST["numeric_result"]);
403 
404  include_once "./Services/Math/classes/class.EvalMath.php";
405  $math = new EvalMath();
406  $math->suppress_errors = TRUE;
407  $result = $math->evaluate($numeric_result);
408  $returnvalue = true;
409  if ((($result === FALSE) || ($result === TRUE)) && (strlen($result) > 0))
410  {
411  ilUtil::sendInfo($this->lng->txt("err_no_numeric_value"), true);
412  $returnvalue = false;
413  }
414  $result = $ilDB->queryF("SELECT solution_id FROM tst_solutions WHERE active_fi = %s AND question_fi = %s AND pass = %s",
415  array('integer','integer','integer'),
416  array($active_id, $this->getId(), $pass)
417  );
418  $row = $ilDB->fetchAssoc($result);
419  $update = $row["solution_id"];
420  if ($update)
421  {
422  if (strlen($numeric_result))
423  {
424  $affectedRows = $ilDB->update("tst_solutions", array(
425  "value1" => array("clob", trim($numeric_result)),
426  "tstamp" => array("integer", time())
427  ), array(
428  "solution_id" => array("integer", $update)
429  ));
430  $entered_values++;
431  }
432  else
433  {
434  $affectedRows = $ilDB->manipulateF("DELETE FROM tst_solutions WHERE solution_id = %s",
435  array('integer'),
436  array($update)
437  );
438  }
439  }
440  else
441  {
442  if (strlen($numeric_result))
443  {
444  $next_id = $ilDB->nextId('tst_solutions');
445  $affectedRows = $ilDB->insert("tst_solutions", array(
446  "solution_id" => array("integer", $next_id),
447  "active_fi" => array("integer", $active_id),
448  "question_fi" => array("integer", $this->getId()),
449  "value1" => array("clob", trim($numeric_result)),
450  "value2" => array("clob", null),
451  "pass" => array("integer", $pass),
452  "tstamp" => array("integer", time())
453  ));
454  $entered_values++;
455  }
456  }
457  if ($entered_values)
458  {
459  include_once ("./Modules/Test/classes/class.ilObjAssessmentFolder.php");
461  {
462  $this->logAction($this->lng->txtlng("assessment", "log_user_entered_values", ilObjAssessmentFolder::_getLogLanguage()), $active_id, $this->getId());
463  }
464  }
465  else
466  {
467  include_once ("./Modules/Test/classes/class.ilObjAssessmentFolder.php");
469  {
470  $this->logAction($this->lng->txtlng("assessment", "log_user_not_entered_values", ilObjAssessmentFolder::_getLogLanguage()), $active_id, $this->getId());
471  }
472  }
473  parent::saveWorkingData($active_id, $pass);
474 
475  return $returnvalue;
476  }
477 
484  function getQuestionType()
485  {
486  return "assNumeric";
487  }
488 
495  function getMaxChars()
496  {
497  return $this->maxchars;
498  }
499 
507  {
508  $this->maxchars = $maxchars;
509  }
510 
518  {
519  return "qpl_qst_numeric";
520  }
521 
527  {
529  }
530 
543  public function setExportDetailsXLS(&$worksheet, $startrow, $active_id, $pass, &$format_title, &$format_bold)
544  {
545  include_once ("./Services/Excel/classes/class.ilExcelUtils.php");
546  $solutions = $this->getSolutionValues($active_id, $pass);
547  $worksheet->writeString($startrow, 0, ilExcelUtils::_convert_text($this->lng->txt($this->getQuestionType())), $format_title);
548  $worksheet->writeString($startrow, 1, ilExcelUtils::_convert_text($this->getTitle()), $format_title);
549  $i = 1;
550  $worksheet->writeString($startrow + $i, 0, ilExcelUtils::_convert_text($this->lng->txt("result")), $format_bold);
551  if (strlen($solutions[0]["value1"]))
552  {
553  $worksheet->write($startrow + $i, 1, ilExcelUtils::_convert_text($solutions[0]["value1"]));
554  }
555  $i++;
556  return $startrow + $i + 1;
557  }
558 }
559 
560 ?>