ILIAS  Release_4_1_x_branch Revision 61804
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.assMatchingQuestion.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 include_once "./Modules/TestQuestionPool/classes/class.assQuestion.php";
25 include_once "./Modules/Test/classes/inc.AssessmentConstants.php";
26 
37 {
46 
56 
62  protected $terms;
63  protected $definitions;
64 
70  var $thumb_geometry = 100;
71 
78 
91  function __construct(
92  $title = "",
93  $comment = "",
94  $author = "",
95  $owner = -1,
96  $question = "",
98  )
99  {
101  $this->matchingpairs = array();
102  $this->matching_type = $matching_type;
103  $this->terms = array();
104  $this->definitions = array();
105  }
106 
113  function isComplete()
114  {
115  if (($this->title) and ($this->author) and ($this->question) and (count($this->matchingpairs)) and ($this->getMaximumPoints() > 0))
116  {
117  return true;
118  }
119  else
120  {
121  return false;
122  }
123  }
124 
130  public function saveToDb($original_id = "")
131  {
132  global $ilDB;
133 
135 
136  // save additional data
137  $affectedRows = $ilDB->manipulateF("DELETE FROM " . $this->getAdditionalTableName() . " WHERE question_fi = %s",
138  array("integer"),
139  array($this->getId())
140  );
141  $affectedRows = $ilDB->manipulateF("INSERT INTO " . $this->getAdditionalTableName() . " (question_fi, shuffle, matching_type, thumb_geometry, element_height) VALUES (%s, %s, %s, %s, %s)",
142  array("integer", "text", "text","integer","integer"),
143  array(
144  $this->getId(),
145  $this->shuffle,
146  $this->matching_type,
147  $this->getThumbGeometry(),
148  ($this->getElementHeight() >= 20) ? $this->getElementHeight() : NULL
149  )
150  );
151 
152  // delete old terms
153  $affectedRows = $ilDB->manipulateF("DELETE FROM qpl_a_mterm WHERE question_fi = %s",
154  array('integer'),
155  array($this->getId())
156  );
157 
158  // delete old definitions
159  $affectedRows = $ilDB->manipulateF("DELETE FROM qpl_a_mdef WHERE question_fi = %s",
160  array('integer'),
161  array($this->getId())
162  );
163 
164  $termids = array();
165  // write terms
166  foreach ($this->terms as $key => $term)
167  {
168  $next_id = $ilDB->nextId('qpl_a_mterm');
169  $affectedRows = $ilDB->manipulateF("INSERT INTO qpl_a_mterm (term_id, question_fi, picture, term) VALUES (%s, %s, %s, %s)",
170  array('integer','integer','text', 'text'),
171  array($next_id, $this->getId(), $term->picture, $term->text)
172  );
173  $termids[$term->identifier] = $next_id;
174  }
175 
176  $definitionids = array();
177  // write definitions
178  foreach ($this->definitions as $key => $definition)
179  {
180  $next_id = $ilDB->nextId('qpl_a_mdef');
181  $affectedRows = $ilDB->manipulateF("INSERT INTO qpl_a_mdef (def_id, question_fi, picture, definition, morder) VALUES (%s, %s, %s, %s, %s)",
182  array('integer','integer','text', 'text', 'integer'),
183  array($next_id, $this->getId(), $definition->picture, $definition->text, $definition->identifier)
184  );
185  $definitionids[$definition->identifier] = $next_id;
186  }
187 
188  $affectedRows = $ilDB->manipulateF("DELETE FROM qpl_a_matching WHERE question_fi = %s",
189  array('integer'),
190  array($this->getId())
191  );
192  $matchingpairs = $this->getMatchingPairs();
193  foreach ($matchingpairs as $key => $pair)
194  {
195  $next_id = $ilDB->nextId('qpl_a_matching');
196  $affectedRows = $ilDB->manipulateF("INSERT INTO qpl_a_matching (answer_id, question_fi, points, term_fi, definition_fi) VALUES (%s, %s, %s, %s, %s)",
197  array('integer','integer','float','integer','integer'),
198  array(
199  $next_id,
200  $this->getId(),
201  $pair->points,
202  $termids[$pair->term->identifier],
203  $definitionids[$pair->definition->identifier]
204  )
205  );
206  }
207 
208  $this->rebuildThumbnails();
209 
211  }
212 
219  public function loadFromDb($question_id)
220  {
221  global $ilDB;
222 
223  $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",
224  array("integer"),
225  array($question_id)
226  );
227  if ($result->numRows() == 1)
228  {
229  $data = $ilDB->fetchAssoc($result);
230  $this->setId($question_id);
231  $this->setObjId($data["obj_fi"]);
232  $this->setTitle($data["title"]);
233  $this->setComment($data["description"]);
234  $this->setOriginalId($data["original_id"]);
235  $this->setNrOfTries($data['nr_of_tries']);
236  $this->setAuthor($data["author"]);
237  $this->setPoints($data["points"]);
238  $this->setOwner($data["owner"]);
239  include_once("./Services/RTE/classes/class.ilRTE.php");
240  $this->setQuestion(ilRTE::_replaceMediaObjectImageSrc($data["question_text"], 1));
241  $this->setThumbGeometry($data["thumb_geometry"]);
242  $this->setElementHeight($data["element_height"]);
243  $this->setShuffle($data["shuffle"]);
244  $this->setEstimatedWorkingTime(substr($data["working_time"], 0, 2), substr($data["working_time"], 3, 2), substr($data["working_time"], 6, 2));
245  }
246 
247  $termids = array();
248  $result = $ilDB->queryF("SELECT * FROM qpl_a_mterm WHERE question_fi = %s ORDER BY term_id ASC",
249  array('integer'),
250  array($question_id)
251  );
252  include_once "./Modules/TestQuestionPool/classes/class.assAnswerMatchingTerm.php";
253  $this->terms = array();
254  if ($result->numRows() > 0)
255  {
256  while ($data = $ilDB->fetchAssoc($result))
257  {
258  $term = new assAnswerMatchingTerm($data['term'], $data['picture'], $data['term_id']);
259  array_push($this->terms, $term);
260  $termids[$data['term_id']] = $term;
261  }
262  }
263 
264  $definitionids = array();
265  $result = $ilDB->queryF("SELECT * FROM qpl_a_mdef WHERE question_fi = %s ORDER BY def_id ASC",
266  array('integer'),
267  array($question_id)
268  );
269  include_once "./Modules/TestQuestionPool/classes/class.assAnswerMatchingDefinition.php";
270  $this->definitions = array();
271  if ($result->numRows() > 0)
272  {
273  while ($data = $ilDB->fetchAssoc($result))
274  {
275  $definition = new assAnswerMatchingDefinition($data['definition'], $data['picture'], $data['morder']);
276  array_push($this->definitions, $definition);
277  $definitionids[$data['def_id']] = $definition;
278  }
279  }
280 
281  $this->matchingpairs = array();
282  $result = $ilDB->queryF("SELECT * FROM qpl_a_matching WHERE question_fi = %s ORDER BY answer_id",
283  array('integer'),
284  array($question_id)
285  );
286  include_once "./Modules/TestQuestionPool/classes/class.assAnswerMatchingPair.php";
287  if ($result->numRows() > 0)
288  {
289  while ($data = $ilDB->fetchAssoc($result))
290  {
291  array_push($this->matchingpairs, new assAnswerMatchingPair($termids[$data['term_fi']], $definitionids[$data['definition_fi']], $data['points']));
292  }
293  }
294  parent::loadFromDb($question_id);
295  }
296 
297 
301  public function duplicate($for_test = true, $title = "", $author = "", $owner = "")
302  {
303  if ($this->id <= 0)
304  {
305  // The question has not been saved. It cannot be duplicated
306  return;
307  }
308  // duplicate the question in database
309  $this_id = $this->getId();
310  $clone = $this;
311  include_once ("./Modules/TestQuestionPool/classes/class.assQuestion.php");
313  $clone->id = -1;
314  if ($title)
315  {
316  $clone->setTitle($title);
317  }
318  if ($author)
319  {
320  $clone->setAuthor($author);
321  }
322  if ($owner)
323  {
324  $clone->setOwner($owner);
325  }
326  if ($for_test)
327  {
328  $clone->saveToDb($original_id);
329  }
330  else
331  {
332  $clone->saveToDb();
333  }
334 
335  // copy question page content
336  $clone->copyPageOfQuestion($this_id);
337  // copy XHTML media objects
338  $clone->copyXHTMLMediaObjectsOfQuestion($this_id);
339  // duplicate the generic feedback
340  $clone->duplicateFeedbackGeneric($this_id);
341 
342  // duplicate the image
343  $clone->duplicateImages($this_id);
344  $clone->onDuplicate($this_id);
345  return $clone->id;
346  }
347 
351  public function copyObject($target_questionpool, $title = "")
352  {
353  if ($this->id <= 0)
354  {
355  // The question has not been saved. It cannot be duplicated
356  return;
357  }
358  // duplicate the question in database
359  $clone = $this;
360  include_once ("./Modules/TestQuestionPool/classes/class.assQuestion.php");
362  $clone->id = -1;
363  if ($title)
364  {
365  $clone->setTitle($title);
366  }
367  $source_questionpool = $this->getObjId();
368  $clone->setObjId($target_questionpool);
369  $clone->saveToDb();
370 
371  // copy question page content
372  $clone->copyPageOfQuestion($original_id);
373  // copy XHTML media objects
374  $clone->copyXHTMLMediaObjectsOfQuestion($original_id);
375  // duplicate the generic feedback
376  $clone->duplicateFeedbackGeneric($original_id);
377 
378  // duplicate the image
379  $clone->copyImages($original_id, $source_questionpool);
380  $clone->onCopy($this->getObjId(), $this->getId());
381  return $clone->id;
382  }
383 
384  public function duplicateImages($question_id)
385  {
386  global $ilLog;
387  $imagepath = $this->getImagePath();
388  $imagepath_original = str_replace("/$this->id/images", "/$question_id/images", $imagepath);
389  foreach ($this->terms as $term)
390  {
391  if (strlen($term->picture))
392  {
393  $filename = $term->picture;
394  if (!file_exists($imagepath))
395  {
396  ilUtil::makeDirParents($imagepath);
397  }
398  if (!@copy($imagepath_original . $filename, $imagepath . $filename))
399  {
400  $ilLog->write("matching question image could not be duplicated: $imagepath_original$filename");
401  }
402  if (@file_exists($imagepath_original . $this->getThumbPrefix() . $filename))
403  {
404  if (!@copy($imagepath_original . $this->getThumbPrefix() . $filename, $imagepath . $this->getThumbPrefix() . $filename))
405  {
406  $ilLog->write("matching question image thumbnail could not be duplicated: $imagepath_original" . $this->getThumbPrefix() . $filename);
407  }
408  }
409  }
410  }
411  foreach ($this->definitions as $definition)
412  {
413  if (strlen($definition->picture))
414  {
415  $filename = $definition->picture;
416  if (!file_exists($imagepath))
417  {
418  ilUtil::makeDirParents($imagepath);
419  }
420  if (!@copy($imagepath_original . $filename, $imagepath . $filename))
421  {
422  $ilLog->write("matching question image could not be duplicated: $imagepath_original$filename");
423  }
424  if (@file_exists($imagepath_original . $this->getThumbPrefix() . $filename))
425  {
426  if (!@copy($imagepath_original . $this->getThumbPrefix() . $filename, $imagepath . $this->getThumbPrefix() . $filename))
427  {
428  $ilLog->write("matching question image thumbnail could not be duplicated: $imagepath_original" . $this->getThumbPrefix() . $filename);
429  }
430  }
431  }
432  }
433  }
434 
435  public function copyImages($question_id, $source_questionpool)
436  {
437  global $ilLog;
438 
439  $imagepath = $this->getImagePath();
440  $imagepath_original = str_replace("/$this->id/images", "/$question_id/images", $imagepath);
441  $imagepath_original = str_replace("/$this->obj_id/", "/$source_questionpool/", $imagepath_original);
442  foreach ($this->terms as $term)
443  {
444  if (strlen($term->picture))
445  {
446  if (!file_exists($imagepath))
447  {
448  ilUtil::makeDirParents($imagepath);
449  }
450  $filename = $term->picture;
451  if (!@copy($imagepath_original . $filename, $imagepath . $filename))
452  {
453  $ilLog->write("matching question image could not be copied: $imagepath_original$filename");
454  }
455  if (!@copy($imagepath_original . $this->getThumbPrefix() . $filename, $imagepath . $this->getThumbPrefix() . $filename))
456  {
457  $ilLog->write("matching question image thumbnail could not be copied: $imagepath_original" . $this->getThumbPrefix() . $filename);
458  }
459  }
460  }
461  foreach ($this->definitions as $definition)
462  {
463  if (strlen($definition->picture))
464  {
465  $filename = $definition->picture;
466  if (!file_exists($imagepath))
467  {
468  ilUtil::makeDirParents($imagepath);
469  }
470  if (!copy($imagepath_original . $filename, $imagepath . $filename))
471  {
472  $ilLog->write("matching question image could not be copied: $imagepath_original$filename");
473  }
474  if (!copy($imagepath_original . $this->getThumbPrefix() . $filename, $imagepath . $this->getThumbPrefix() . $filename))
475  {
476  $ilLog->write("matching question image thumbnail could not be copied: $imagepath_original" . $this->getThumbPrefix() . $filename);
477  }
478  }
479  }
480  }
481 
492  public function insertMatchingPair($position, $term = null, $definition = null, $points = 0.0)
493  {
494  include_once "./Modules/TestQuestionPool/classes/class.assAnswerMatchingPair.php";
495  include_once "./Modules/TestQuestionPool/classes/class.assAnswerMatchingTerm.php";
496  include_once "./Modules/TestQuestionPool/classes/class.assAnswerMatchingDefinition.php";
497  if (is_null($term)) $term = new assAnswerMatchingTerm();
498  if (is_null($definition)) $definition = new assAnswerMatchingDefinition();
499  $pair = new assAnswerMatchingPair($term, $definition, $points);
500  if ($position < count($this->matchingpairs))
501  {
502  $part1 = array_slice($this->matchingpairs, 0, $position);
503  $part2 = array_slice($this->matchingpairs, $position);
504  $this->matchingpairs = array_merge($part1, array($pair), $part2);
505  }
506  else
507  {
508  array_push($this->matchingpairs, $pair);
509  }
510  }
520  public function addMatchingPair($term = null, $definition = null, $points = 0.0)
521  {
522  include_once "./Modules/TestQuestionPool/classes/class.assAnswerMatchingPair.php";
523  include_once "./Modules/TestQuestionPool/classes/class.assAnswerMatchingTerm.php";
524  include_once "./Modules/TestQuestionPool/classes/class.assAnswerMatchingDefinition.php";
525  if (is_null($term)) $term = new assAnswerMatchingTerm();
526  if (is_null($definition)) $definition = new assAnswerMatchingDefinition();
527  $pair = new assAnswerMatchingPair($term, $definition, $points);
528  array_push($this->matchingpairs, $pair);
529  }
530 
534  public function getTermWithIdentifier($a_identifier)
535  {
536  foreach ($this->terms as $term)
537  {
538  if ($term->identifier == $a_identifier) return $term;
539  }
540  return null;
541  }
542 
546  public function getDefinitionWithIdentifier($a_identifier)
547  {
548  foreach ($this->definitions as $definition)
549  {
550  if ($definition->identifier == $a_identifier) return $definition;
551  }
552  return null;
553  }
554 
563  public function getMatchingPair($index = 0)
564  {
565  if ($index < 0)
566  {
567  return NULL;
568  }
569  if (count($this->matchingpairs) < 1)
570  {
571  return NULL;
572  }
573  if ($index >= count($this->matchingpairs))
574  {
575  return NULL;
576  }
577  return $this->matchingpairs[$index];
578  }
579 
587  public function deleteMatchingPair($index = 0)
588  {
589  if ($index < 0)
590  {
591  return;
592  }
593  if (count($this->matchingpairs) < 1)
594  {
595  return;
596  }
597  if ($index >= count($this->matchingpairs))
598  {
599  return;
600  }
601  unset($this->matchingpairs[$index]);
602  $this->matchingpairs = array_values($this->matchingpairs);
603  }
604 
609  public function flushMatchingPairs()
610  {
611  $this->matchingpairs = array();
612  }
613 
620  public function getMatchingPairCount()
621  {
622  return count($this->matchingpairs);
623  }
624 
631  public function getTerms()
632  {
633  return $this->terms;
634  }
635 
642  public function getDefinitions()
643  {
644  return $this->definitions;
645  }
646 
653  public function getTermCount()
654  {
655  return count($this->terms);
656  }
657 
664  public function getDefinitionCount()
665  {
666  return count($this->definitions);
667  }
668 
675  public function addTerm($term)
676  {
677  array_push($this->terms, $term);
678  }
679 
686  public function addDefinition($definition)
687  {
688  array_push($this->definitions, $definition);
689  }
690 
697  public function insertTerm($position, $term = null)
698  {
699  if (is_null($term))
700  {
701  include_once "./Modules/TestQuestionPool/classes/class.assAnswerMatchingTerm.php";
702  $term = new assAnswerMatchingTerm();
703  }
704  if ($position < count($this->terms))
705  {
706  $part1 = array_slice($this->terms, 0, $position);
707  $part2 = array_slice($this->terms, $position);
708  $this->terms = array_merge($part1, array($term), $part2);
709  }
710  else
711  {
712  array_push($this->terms, $term);
713  }
714  }
715 
722  public function insertDefinition($position, $definition = null)
723  {
724  if (is_null($definition))
725  {
726  include_once "./Modules/TestQuestionPool/classes/class.assAnswerMatchingDefinition.php";
727  $definition = new assAnswerMatchingDefinition();
728  }
729  if ($position < count($this->definitions))
730  {
731  $part1 = array_slice($this->definitions, 0, $position);
732  $part2 = array_slice($this->definitions, $position);
733  $this->definitions = array_merge($part1, array($definition), $part2);
734  }
735  else
736  {
737  array_push($this->definitions, $definition);
738  }
739  }
740 
745  public function flushTerms()
746  {
747  $this->terms = array();
748  }
749 
754  public function flushDefinitions()
755  {
756  $this->definitions = array();
757  }
758 
765  public function deleteTerm($position)
766  {
767  unset($this->terms[$position]);
768  $this->terms = array_values($this->terms);
769  }
770 
777  public function deleteDefinition($position)
778  {
779  unset($this->definitions[$position]);
780  $this->definitions = array_values($this->definitions);
781  }
782 
790  public function setTerm($term, $index)
791  {
792  $this->terms[$index] = $term;
793  }
794 
804  function calculateReachedPoints($active_id, $pass = NULL)
805  {
806  global $ilDB;
807 
808  $found_value1 = array();
809  $found_value2 = array();
810  if (is_null($pass))
811  {
812  $pass = $this->getSolutionMaxPass($active_id);
813  }
814  $result = $ilDB->queryF("SELECT * FROM tst_solutions WHERE active_fi = %s AND question_fi = %s AND pass = %s",
815  array('integer','integer','integer'),
816  array($active_id, $this->getId(), $pass)
817  );
818  while ($data = $ilDB->fetchAssoc($result))
819  {
820  if (strcmp($data["value1"], "") != 0)
821  {
822  array_push($found_value1, $data["value1"]);
823  array_push($found_value2, $data["value2"]);
824  }
825  }
826  $points = 0;
827  foreach ($found_value2 as $key => $value)
828  {
829  foreach ($this->matchingpairs as $pair)
830  {
831  if (($pair->definition->identifier == $value) && ($pair->term->identifier == $found_value1[$key]))
832  {
833  $points += $pair->points;
834  }
835  }
836  }
837 
838  $points = parent::calculateReachedPoints($active_id, $pass = NULL, $points);
839  return $points;
840  }
841 
845  function getMaximumPoints()
846  {
847  $points = 0;
848  foreach ($this->matchingpairs as $key => $pair)
849  {
850  if ($pair->points > 0)
851  {
852  $points += $pair->points;
853  }
854  }
855  return $points;
856  }
857 
867  {
868  $extension = "";
869  if (preg_match("/.*\\.(\\w+)$/", $filename, $matches))
870  {
871  $extension = $matches[1];
872  }
873  return md5($filename) . "." . $extension;
874  }
875 
876  public function removeTermImage($index)
877  {
878  $term = $this->terms[$index];
879  if (is_object($term))
880  {
881  $this->deleteImagefile($term->picture);
882  $term->picture = null;
883  }
884  }
885 
886  public function removeDefinitionImage($index)
887  {
888  $definition = $this->definitions[$index];
889  if (is_object($definition))
890  {
891  $this->deleteImagefile($definition->picture);
892  $definition->picture = null;
893  }
894  }
895 
896 
903  public function deleteImagefile($filename)
904  {
905  $deletename = $filename;
906  $result = @unlink($this->getImagePath().$deletename);
907  $result = $result & @unlink($this->getImagePath().$this->getThumbPrefix() . $deletename);
908  return $result;
909  }
910 
919  function setImageFile($image_tempfilename, $image_filename, $previous_filename = '')
920  {
921  $result = TRUE;
922  if (strlen($image_tempfilename))
923  {
924  $image_filename = str_replace(" ", "_", $image_filename);
925  $imagepath = $this->getImagePath();
926  if (!file_exists($imagepath))
927  {
928  ilUtil::makeDirParents($imagepath);
929  }
930  $savename = $image_filename;
931  if (!ilUtil::moveUploadedFile($image_tempfilename, $savename, $imagepath.$savename))
932  {
933  $result = FALSE;
934  }
935  else
936  {
937  // create thumbnail file
938  $thumbpath = $imagepath . $this->getThumbPrefix() . $savename;
939  ilUtil::convertImage($imagepath.$savename, $thumbpath, "JPEG", $this->getThumbGeometry());
940  }
941  if ($result && (strcmp($image_filename, $previous_filename) != 0) && (strlen($previous_filename)))
942  {
943  $this->deleteImagefile($previous_filename);
944  }
945  }
946  return $result;
947  }
948 
955  function checkSaveData()
956  {
957  $result = true;
958  $matching_values = array();
959  foreach ($_POST['matching'][$this->getId()] as $definition => $term)
960  {
961  if ($term > 0)
962  {
963  array_push($matching_values, $term);
964  }
965  }
966 
967  $check_matching = array_flip($matching_values);
968  if (count($check_matching) != count($matching_values))
969  {
970  $result = false;
971  ilUtil::sendFailure($this->lng->txt("duplicate_matching_values_selected"), true);
972  }
973  return $result;
974  }
975 
982  function saveWorkingData($active_id, $pass = NULL)
983  {
984  global $ilDB;
985  global $ilUser;
986  $saveWorkingDataResult = $this->checkSaveData();
987  $entered_values = 0;
988  if ($saveWorkingDataResult)
989  {
990  if (is_null($pass))
991  {
992  include_once "./Modules/Test/classes/class.ilObjTest.php";
993  $pass = ilObjTest::_getPass($active_id);
994  }
995 
996  $affectedRows = $ilDB->manipulateF("DELETE FROM tst_solutions WHERE active_fi = %s AND question_fi = %s AND pass = %s",
997  array('integer','integer','integer'),
998  array($active_id, $this->getId(), $pass)
999  );
1000 
1001  foreach ($_POST['matching'][$this->getId()] as $definition => $term)
1002  {
1003  $entered_values++;
1004  $next_id = $ilDB->nextId('tst_solutions');
1005  $affectedRows = $ilDB->insert("tst_solutions", array(
1006  "solution_id" => array("integer", $next_id),
1007  "active_fi" => array("integer", $active_id),
1008  "question_fi" => array("integer", $this->getId()),
1009  "value1" => array("clob", $term),
1010  "value2" => array("clob", $definition),
1011  "pass" => array("integer", $pass),
1012  "tstamp" => array("integer", time())
1013  ));
1014  }
1015  $saveWorkingDataResult = true;
1016  }
1017  if ($entered_values)
1018  {
1019  include_once ("./Modules/Test/classes/class.ilObjAssessmentFolder.php");
1021  {
1022  $this->logAction($this->lng->txtlng("assessment", "log_user_entered_values", ilObjAssessmentFolder::_getLogLanguage()), $active_id, $this->getId());
1023  }
1024  }
1025  else
1026  {
1027  include_once ("./Modules/Test/classes/class.ilObjAssessmentFolder.php");
1029  {
1030  $this->logAction($this->lng->txtlng("assessment", "log_user_not_entered_values", ilObjAssessmentFolder::_getLogLanguage()), $active_id, $this->getId());
1031  }
1032  }
1033  parent::saveWorkingData($active_id, $pass);
1034  return $saveWorkingDataResult;
1035  }
1036 
1037  public function getRandomId()
1038  {
1039  mt_srand((double)microtime()*1000000);
1040  $random_number = mt_rand(1, 100000);
1041  $found = FALSE;
1042  while ($found)
1043  {
1044  $found = FALSE;
1045  foreach ($this->matchingpairs as $key => $pair)
1046  {
1047  if (($pair->term->identifier == $random_number) || ($pair->definition->identifier == $random_number))
1048  {
1049  $found = TRUE;
1050  $random_number++;
1051  }
1052  }
1053  }
1054  return $random_number;
1055  }
1056 
1063  public function setShuffle($shuffle)
1064  {
1065  switch ($shuffle)
1066  {
1067  case 0:
1068  case 1:
1069  case 2:
1070  case 3:
1071  $this->shuffle = $shuffle;
1072  break;
1073  default:
1074  $this->shuffle = 1;
1075  break;
1076  }
1077  }
1078 
1084  public function getQuestionType()
1085  {
1086  return "assMatchingQuestion";
1087  }
1088 
1094  public function getAdditionalTableName()
1095  {
1096  return "qpl_qst_matching";
1097  }
1098 
1104  public function getAnswerTableName()
1105  {
1106  return array("qpl_a_matching", "qpl_a_mterm");
1107  }
1108 
1113  public function getRTETextWithMediaObjects()
1114  {
1116  }
1117 
1121  public function &getMatchingPairs()
1122  {
1123  return $this->matchingpairs;
1124  }
1125 
1131  public function supportsJavascriptOutput()
1132  {
1133  return TRUE;
1134  }
1135 
1148  public function setExportDetailsXLS(&$worksheet, $startrow, $active_id, $pass, &$format_title, &$format_bold)
1149  {
1150  include_once ("./Services/Excel/classes/class.ilExcelUtils.php");
1151  $solutions = $this->getSolutionValues($active_id, $pass);
1152  $worksheet->writeString($startrow, 0, ilExcelUtils::_convert_text($this->lng->txt($this->getQuestionType())), $format_title);
1153  $worksheet->writeString($startrow, 1, ilExcelUtils::_convert_text($this->getTitle()), $format_title);
1154  $imagepath = $this->getImagePath();
1155  $i = 1;
1156  foreach ($solutions as $solution)
1157  {
1158  $matches_written = FALSE;
1159  foreach ($this->getMatchingPairs() as $idx => $pair)
1160  {
1161  if (!$matches_written) $worksheet->writeString($startrow + $i, 1, ilExcelUtils::_convert_text($this->lng->txt("matches")));
1162  $matches_written = TRUE;
1163  if ($pair->definition->identifier == $solution["value2"])
1164  {
1165  if (strlen($pair->definition->text))
1166  {
1167  $worksheet->writeString($startrow + $i, 0, ilExcelUtils::_convert_text($pair->definition->text));
1168  }
1169  else
1170  {
1171  $worksheet->writeString($startrow + $i, 0, ilExcelUtils::_convert_text($pair->definition->picture));
1172  }
1173  }
1174  if ($pair->term->identifier == $solution["value1"])
1175  {
1176  if (strlen($pair->term->text))
1177  {
1178  $worksheet->writeString($startrow + $i, 2, ilExcelUtils::_convert_text($pair->term->text));
1179  }
1180  else
1181  {
1182  $worksheet->writeString($startrow + $i, 2, ilExcelUtils::_convert_text($pair->term->picture));
1183  }
1184  }
1185  }
1186  $i++;
1187  }
1188  return $startrow + $i + 1;
1189  }
1190 
1196  public function getThumbGeometry()
1197  {
1198  return $this->thumb_geometry;
1199  }
1200 
1206  public function getThumbSize()
1207  {
1208  return $this->getThumbGeometry();
1209  }
1210 
1216  public function setThumbGeometry($a_geometry)
1217  {
1218  $this->thumb_geometry = ($a_geometry < 1) ? 100 : $a_geometry;
1219  }
1220 
1226  public function getElementHeight()
1227  {
1228  return $this->element_height;
1229  }
1230 
1236  public function setElementHeight($a_height)
1237  {
1238  $this->element_height = ($a_height < 20) ? "" : $a_height;
1239  }
1240 
1244  public function rebuildThumbnails()
1245  {
1246  foreach ($this->terms as $term)
1247  {
1248  if (strlen($term->picture)) $this->generateThumbForFile($this->getImagePath(), $term->picture);
1249  }
1250  foreach ($this->definitions as $definition)
1251  {
1252  if (strlen($definition->picture)) $this->generateThumbForFile($this->getImagePath(), $definition->picture);
1253  }
1254  }
1255 
1256  public function getThumbPrefix()
1257  {
1258  return "thumb.";
1259  }
1260 
1261  protected function generateThumbForFile($path, $file)
1262  {
1263  $filename = $path . $file;
1264  if (@file_exists($filename))
1265  {
1266  $thumbpath = $path . $this->getThumbPrefix() . $file;
1267  $path_info = @pathinfo($filename);
1268  $ext = "";
1269  switch (strtoupper($path_info['extension']))
1270  {
1271  case 'PNG':
1272  $ext = 'PNG';
1273  break;
1274  case 'GIF':
1275  $ext = 'GIF';
1276  break;
1277  default:
1278  $ext = 'JPEG';
1279  break;
1280  }
1281  ilUtil::convertImage($filename, $thumbpath, $ext, $this->getThumbGeometry());
1282  }
1283  }
1284 
1285  public function getEstimatedElementHeight()
1286  {
1287  $hasImages = false;
1288  foreach ($this->terms as $term)
1289  {
1290  if (strlen($term->picture))
1291  {
1292  $hasImages = true;
1293  }
1294  }
1295  foreach ($this->definitions as $definition)
1296  {
1297  if (strlen($definition->picture))
1298  {
1299  $hasImages = true;
1300  }
1301  }
1302  if ($hasImages)
1303  { // 40 is approx. the height of the preview image
1304  return max($this->getElementHeight(), $this->getThumbSize() + 40);
1305  }
1306  else
1307  {
1308  return ($this->getElementHeight()) ? $this->getElementHeight() : 0;
1309  }
1310  }
1311 
1316  public function toJSON()
1317  {
1318  include_once("./Services/RTE/classes/class.ilRTE.php");
1319  $result = array();
1320  $result['id'] = (int) $this->getId();
1321  $result['type'] = (string) $this->getQuestionType();
1322  $result['title'] = (string) $this->getTitle();
1323  $result['question'] = (string) ilRTE::_replaceMediaObjectImageSrc($this->getQuestion(), 0);
1324  $result['nr_of_tries'] = (int) $this->getNrOfTries();
1325  $result['shuffle'] = true;
1326  $result['feedback'] = array(
1327  "onenotcorrect" => nl2br(ilRTE::_replaceMediaObjectImageSrc($this->getFeedbackGeneric(0), 0)),
1328  "allcorrect" => nl2br(ilRTE::_replaceMediaObjectImageSrc($this->getFeedbackGeneric(1), 0))
1329  );
1330  $terms = array();
1331  foreach ($this->getMatchingPairs() as $pair)
1332  {
1333  $terms[(int)$pair->term->identifier] = array(
1334  "term" => $pair->term->text,
1335  "id" =>(int)$pair->term->identifier
1336  );
1337  }
1338 
1339  $terms = $this->pcArrayShuffle($terms);
1340  $pairs = array();
1341 
1342  // alex 9.9.2010 as a fix for bug 6513 I added the question id
1343  // to the "def_id" in the array. The $pair->definition->identifier is not
1344  // unique, since it gets it value from the morder table field
1345  // this value is not changed, when a question is copied.
1346  // thus copying the same question on a page results in problems
1347  // when the second one (the copy) is answered.
1348 
1349  foreach ($this->getMatchingPairs() as $pair)
1350  {
1351  array_push($pairs, array(
1352  "term_id" => (int) $pair->term->identifier,
1353  "points" => (float) $pair->points,
1354  "definition" => (string) $pair->definition->text,
1355  "def_id" => (int) $this->getId().$pair->definition->identifier,
1356  "terms" => $terms
1357  ));
1358  }
1359  $result['pairs'] = $pairs;
1360  $mobs = ilObjMediaObject::_getMobsOfObject("qpl:html", $this->getId());
1361  $result['mobs'] = $mobs;
1362 
1363  return json_encode($result);
1364  }
1365 
1366 }
1367 
1368 ?>