ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.assJavaApplet.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once './Modules/TestQuestionPool/classes/class.assQuestion.php';
5 require_once './Modules/Test/classes/inc.AssessmentConstants.php';
6 require_once './Modules/TestQuestionPool/interfaces/interface.ilObjQuestionScoringAdjustable.php';
7 require_once './Modules/TestQuestionPool/interfaces/interface.iQuestionCondition.php';
8 require_once './Modules/TestQuestionPool/classes/class.ilUserQuestionResult.php';
9 
24 {
33 
42 
51 
60 
69 
78 
87 
102  function __construct(
103  $title = "",
104  $comment = "",
105  $author = "",
106  $owner = -1,
107  $question = "",
109  )
110  {
112  $this->javaapplet_filename = $javaapplet_filename;
113  $this->parameters = array();
114  }
115 
123  public function splitParams($params = "")
124  {
125  $params_array = split("<separator>", $params);
126  foreach ($params_array as $pair)
127  {
128  if (preg_match("/(.*?)\=(.*)/", $pair, $matches))
129  {
130  switch ($matches[1])
131  {
132  case "java_code" :
133  $this->java_code = $matches[2];
134  break;
135  case "java_codebase" :
136  $this->java_codebase = $matches[2];
137  break;
138  case "java_archive" :
139  $this->java_archive = $matches[2];
140  break;
141  case "java_width" :
142  $this->java_width = $matches[2];
143  break;
144  case "java_height" :
145  $this->java_height = $matches[2];
146  break;
147  }
148  if (preg_match("/param_name_(\d+)/", $matches[1], $found_key))
149  {
150  $this->parameters[$found_key[1]]["name"] = $matches[2];
151  }
152  if (preg_match("/param_value_(\d+)/", $matches[1], $found_key))
153  {
154  $this->parameters[$found_key[1]]["value"] = $matches[2];
155  }
156  }
157  }
158  }
159 
167  public function buildParams()
168  {
169  $params_array = array();
170  if ($this->java_code)
171  {
172  array_push($params_array, "java_code=$this->java_code");
173  }
174  if ($this->java_codebase)
175  {
176  array_push($params_array, "java_codebase=$this->java_codebase");
177  }
178  if ($this->java_archive)
179  {
180  array_push($params_array, "java_archive=$this->java_archive");
181  }
182  if ($this->java_width)
183  {
184  array_push($params_array, "java_width=$this->java_width");
185  }
186  if ($this->java_height)
187  {
188  array_push($params_array, "java_height=$this->java_height");
189  }
190  foreach ($this->parameters as $key => $value)
191  {
192  array_push($params_array, "param_name_$key=" . $value["name"]);
193  array_push($params_array, "param_value_$key=" . $value["value"]);
194  }
195 
196  return join($params_array, "<separator>");
197  }
198 
204  public function buildParamsOnly()
205  {
206  $params_array = array();
207  if ($this->java_code)
208  {
209  array_push($params_array, "java_code=$this->java_code");
210  array_push($params_array, "java_codebase=$this->java_codebase");
211  array_push($params_array, "java_archive=$this->java_archive");
212  }
213  foreach ($this->parameters as $key => $value)
214  {
215  array_push($params_array, "param_name_$key=" . $value["name"]);
216  array_push($params_array, "param_value_$key=" . $value["value"]);
217  }
218  return join($params_array, "<separator>");
219  }
220 
226  public function isComplete()
227  {
228  if (strlen($this->title)
229  && $this->author
230  && $this->question
231  && $this->javaapplet_filename
232  && $this->java_width
233  && $this->java_height
234  && $this->getPoints() > 0
235  )
236  {
237  return true;
238  }
239  else if (strlen($this->title)
240  && $this->author
241  && $this->question
242  && $this->getJavaArchive()
243  && $this->getJavaCodebase()
244  && $this->java_width
245  && $this->java_height
246  && $this->getPoints() > 0
247  )
248  {
249  return true;
250  }
251  return false;
252  }
253 
254 
262  public function saveToDb($original_id = "")
263  {
267  }
268 
270  {
271  global $ilDB;
272  $params = $this->buildParams();
273  // save additional data
274  $ilDB->manipulateF( "DELETE FROM " . $this->getAdditionalTableName() . " WHERE question_fi = %s",
275  array( "integer" ),
276  array( $this->getId() )
277  );
278  $ilDB->manipulateF( "INSERT INTO " . $this->getAdditionalTableName(
279  ) . " (question_fi, image_file, params) VALUES (%s, %s, %s)",
280  array( "integer", "text", "text" ),
281  array(
282  $this->getId(),
283  $this->javaapplet_filename,
284  $params
285  )
286  );
287  }
288 
295  public function loadFromDb($question_id)
296  {
297  global $ilDB;
298 
299  $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",
300  array("integer"),
301  array($question_id)
302  );
303  if ($result->numRows() == 1)
304  {
305  $data = $ilDB->fetchAssoc($result);
306  $this->setId($question_id);
307  $this->setObjId($data["obj_fi"]);
308  $this->setNrOfTries($data['nr_of_tries']);
309  $this->setTitle($data["title"]);
310  $this->setComment($data["description"]);
311  $this->setOriginalId($data["original_id"]);
312  $this->setAuthor($data["author"]);
313  $this->setPoints($data["points"]);
314  $this->setOwner($data["owner"]);
315  include_once("./Services/RTE/classes/class.ilRTE.php");
316  $this->setQuestion(ilRTE::_replaceMediaObjectImageSrc($data["question_text"], 1));
317  $this->setJavaAppletFilename($data["image_file"]);
318  $this->splitParams($data["params"]);
319  $this->setEstimatedWorkingTime(substr($data["working_time"], 0, 2), substr($data["working_time"], 3, 2), substr($data["working_time"], 6, 2));
320 
321  try
322  {
323  $this->setAdditionalContentEditingMode($data['add_cont_edit_mode']);
324  }
326  {
327  }
328  }
329  parent::loadFromDb($question_id);
330  }
331 
337  function duplicate($for_test = true, $title = "", $author = "", $owner = "", $testObjId = null)
338  {
339  if ($this->id <= 0)
340  {
341  // The question has not been saved. It cannot be duplicated
342  return;
343  }
344  // duplicate the question in database
345  $this_id = $this->getId();
346  $thisObjId = $this->getObjId();
347 
348  $clone = $this;
349  include_once ("./Modules/TestQuestionPool/classes/class.assQuestion.php");
351  $clone->id = -1;
352 
353  if( (int)$testObjId > 0 )
354  {
355  $clone->setObjId($testObjId);
356  }
357 
358  if ($title)
359  {
360  $clone->setTitle($title);
361  }
362  if ($author)
363  {
364  $clone->setAuthor($author);
365  }
366  if ($owner)
367  {
368  $clone->setOwner($owner);
369  }
370  if ($for_test)
371  {
372  $clone->saveToDb($original_id);
373  }
374  else
375  {
376  $clone->saveToDb();
377  }
378 
379  // copy question page content
380  $clone->copyPageOfQuestion($this_id);
381  // copy XHTML media objects
382  $clone->copyXHTMLMediaObjectsOfQuestion($this_id);
383  // duplicate the image
384  $clone->duplicateApplet($this_id, $thisObjId);
385 
386  $clone->onDuplicate($thisObjId, $this_id, $clone->getObjId(), $clone->getId());
387 
388  return $clone->id;
389  }
390 
398  function copyObject($target_questionpool_id, $title = "")
399  {
400  if ($this->id <= 0)
401  {
402  // The question has not been saved. It cannot be duplicated
403  return;
404  }
405  // duplicate the question in database
406  $clone = $this;
407  include_once ("./Modules/TestQuestionPool/classes/class.assQuestion.php");
409  $clone->id = -1;
410  $source_questionpool_id = $this->getObjId();
411  $clone->setObjId($target_questionpool_id);
412  if ($title)
413  {
414  $clone->setTitle($title);
415  }
416  $clone->saveToDb();
417 
418  // copy question page content
419  $clone->copyPageOfQuestion($original_id);
420  // copy XHTML media objects
421  $clone->copyXHTMLMediaObjectsOfQuestion($original_id);
422  // duplicate the image
423  $clone->copyApplet($original_id, $source_questionpool_id);
424 
425  $clone->onCopy($source_questionpool_id, $original_id, $clone->getObjId(), $clone->getId());
426 
427  return $clone->id;
428  }
429 
430  public function createNewOriginalFromThisDuplicate($targetParentId, $targetQuestionTitle = "")
431  {
432  if ($this->id <= 0)
433  {
434  // The question has not been saved. It cannot be duplicated
435  return;
436  }
437 
438  include_once ("./Modules/TestQuestionPool/classes/class.assQuestion.php");
439 
440  $sourceQuestionId = $this->id;
441  $sourceParentId = $this->getObjId();
442 
443  // duplicate the question in database
444  $clone = $this;
445  $clone->id = -1;
446 
447  $clone->setObjId($targetParentId);
448 
449  if ($targetQuestionTitle)
450  {
451  $clone->setTitle($targetQuestionTitle);
452  }
453 
454  $clone->saveToDb();
455  // copy question page content
456  $clone->copyPageOfQuestion($sourceQuestionId);
457  // copy XHTML media objects
458  $clone->copyXHTMLMediaObjectsOfQuestion($sourceQuestionId);
459  // duplicate the image
460  $clone->copyApplet($sourceQuestionId, $sourceParentId);
461 
462  $clone->onCopy($sourceParentId, $sourceQuestionId, $clone->getObjId(), $clone->getId());
463 
464  return $clone->id;
465  }
466 
467  function duplicateApplet($question_id, $objectId = null)
468  {
469  $javapath = $this->getJavaPath();
470  $javapath_original = preg_replace("/([^\d])$this->id([^\d])/", "\${1}$question_id\${2}", $javapath);
471 
472  if( (int)$objectId > 0 )
473  {
474  $javapath_original = str_replace("/$this->obj_id/", "/$objectId/", $javapath_original);
475  }
476 
477  if (!file_exists($javapath))
478  {
479  ilUtil::makeDirParents($javapath);
480  }
481  $filename = $this->getJavaAppletFilename();
482  if (!copy($javapath_original . $filename, $javapath . $filename)) {
483  print "java applet could not be duplicated!!!! ";
484  }
485  }
486 
487  function copyApplet($question_id, $source_questionpool)
488  {
489  $javapath = $this->getJavaPath();
490  $javapath_original = preg_replace("/([^\d])$this->id([^\d])/", "\${1}$question_id\${2}", $javapath);
491  $javapath_original = str_replace("/$this->obj_id/", "/$source_questionpool/", $javapath_original);
492  if (!file_exists($javapath))
493  {
494  ilUtil::makeDirParents($javapath);
495  }
496  $filename = $this->getJavaAppletFilename();
497  if (!copy($javapath_original . $filename, $javapath . $filename)) {
498  print "java applet could not be copied!!!! ";
499  }
500  }
501 
510  function getJavaCode()
511  {
512  return $this->java_code;
513  }
514 
523  function getJavaCodebase()
524  {
525  return $this->java_codebase;
526  }
527 
536  function getJavaArchive()
537  {
538  return $this->java_archive;
539  }
540 
549  function setJavaCode($java_code = "")
550  {
551  $this->java_code = $java_code;
552  }
553 
563  {
564  $this->java_codebase = $java_codebase;
565  }
566 
576  {
577  $this->java_archive = $java_archive;
578  }
579 
588  function getJavaWidth()
589  {
590  return $this->java_width;
591  }
592 
600  public function setJavaWidth($java_width = "")
601  {
602  $this->java_width = $java_width;
603  }
604 
613  function getJavaHeight()
614  {
615  return $this->java_height;
616  }
617 
627  {
628  $this->java_height = $java_height;
629  }
630 
642  public function calculateReachedPoints($active_id, $pass = NULL, $returndetails = FALSE)
643  {
644  if( $returndetails )
645  {
646  throw new ilTestException('return details not implemented for '.__METHOD__);
647  }
648 
649  global $ilDB;
650 
651  $found_values = array();
652  if (is_null($pass))
653  {
654  $pass = $this->getSolutionMaxPass($active_id);
655  }
656  $result = $ilDB->queryF("SELECT points FROM tst_solutions WHERE active_fi = %s AND question_fi = %s AND pass = %s",
657  array('integer','integer','integer'),
658  array($active_id, $this->getId(), $pass)
659  );
660  $points = 0;
661  while ($data = $ilDB->fetchAssoc($result))
662  {
663  $points += $data["points"];
664  }
665 
666  return $points;
667  }
668 
670  {
671  $points = 0;
672  foreach($previewSession->getParticipantsSolution() as $solution)
673  {
674  if( isset($solution['points']) )
675  {
676  $points += $solution['points'];
677  }
678  }
679  return $points;
680  }
681 
690  public function getReachedInformation($active_id, $pass = NULL)
691  {
692  global $ilDB;
693 
694  $found_values = array();
695  if (is_null($pass))
696  {
697  $pass = $this->getSolutionMaxPass($active_id);
698  }
699  $result = $ilDB->queryF("SELECT * FROM tst_solutions WHERE active_fi = %s AND question_fi = %s AND pass = %s",
700  array('integer','integer','integer'),
701  array($active_id, $this->getId(), $pass)
702  );
703  $counter = 1;
704  $user_result = array();
705  while ($data = $ilDB->fetchAssoc($result))
706  {
707  $true = 0;
708  if ($data["points"] > 0)
709  {
710  $true = 1;
711  }
712  $solution = array(
713  "order" => $counter,
714  "points" => $data["points"],
715  "true" => $true,
716  "value1" => $data["value1"],
717  "value2" => $data["value2"],
718  );
719  $counter++;
720  array_push($user_result, $solution);
721  }
722  return $user_result;
723  }
724 
733  function addParameter($name = "", $value = "")
734  {
735  $index = $this->getParameterIndex($name);
736  if ($index > -1)
737  {
738  $this->parameters[$index] = array("name" => $name, "value" => $value);
739  }
740  else
741  {
742  array_push($this->parameters, array("name" => $name, "value" => $value));
743  }
744  }
745 
746  public function addParameterAtIndex($index = 0, $name = "", $value = "")
747  {
748  if (array_key_exists($index, $this->parameters))
749  {
750  // insert parameter
751  $newparams = array();
752  for ($i = 0; $i < $index; $i++)
753  {
754  array_push($newparams, $this->parameters[$i]);
755  }
756  array_push($newparams, array($name, $value));
757  for ($i = $index; $i < count($this->parameters); $i++)
758  {
759  array_push($newparams, $this->parameters[$i]);
760  }
761  $this->parameters = $newparams;
762  }
763  else
764  {
765  array_push($this->parameters, array($name, $value));
766  }
767  }
768 
776  public function removeParameter($index)
777  {
778  if ($index < 0) return;
779  if (count($this->parameters) < 1) return;
780  if ($index >= count($this->parameters)) return;
781  unset($this->parameters[$index]);
782  $this->parameters = array_values($this->parameters);
783  }
784 
793  function getParameter($index)
794  {
795  if (($index < 0) or ($index >= count($this->parameters)))
796  {
797  return undef;
798  }
799  return $this->parameters[$index];
800  }
801 
810  function getParameterIndex($name)
811  {
812  foreach ($this->parameters as $key => $value)
813  {
814  if (array_key_exists($name, $value))
815  {
816  return $key;
817  }
818  }
819  return -1;
820  }
821 
829  function getParameterCount()
830  {
831  return count($this->parameters);
832  }
833 
840  function flushParams()
841  {
842  $this->parameters = array();
843  }
844 
853  public function saveWorkingData($active_id, $pass = NULL)
854  {
855  // nothing to save!
856 
857  //$this->getProcessLocker()->requestUserSolutionUpdateLock();
858  // store in tst_solutions
859  //$this->getProcessLocker()->releaseUserSolutionUpdateLock();
860 
861  return true;
862  }
863 
864  protected function savePreviewData(ilAssQuestionPreviewSession $previewSession)
865  {
866  // nothing to save!
867 
868  return true;
869  }
870 
878  protected function reworkWorkingData($active_id, $pass, $obligationsAnswered)
879  {
880  // nothing to rework!
881  }
882 
891  {
893  }
894 
903  public function setJavaAppletFilename($javaapplet_filename, $javaapplet_tempfilename = "")
904  {
905  if (!empty($javaapplet_filename))
906  {
907  $this->javaapplet_filename = $javaapplet_filename;
908  }
909  if (!empty($javaapplet_tempfilename))
910  {
911  $javapath = $this->getJavaPath();
912  if (!file_exists($javapath))
913  {
914  ilUtil::makeDirParents($javapath);
915  }
916 
917  if (!ilUtil::moveUploadedFile($javaapplet_tempfilename, $javaapplet_filename, $javapath.$javaapplet_filename))
918  {
919  $ilLog->write("ERROR: java applet question: java applet not uploaded: $javaapplet_filename");
920  }
921  else
922  {
923  $this->setJavaCodebase();
924  $this->setJavaArchive();
925  }
926  }
927  }
928 
930  {
931  @unlink($this->getJavaPath() . $this->getJavaAppletFilename());
932  $this->javaapplet_filename = "";
933  }
934 
940  public function getQuestionType()
941  {
942  return "assJavaApplet";
943  }
944 
950  public function getAdditionalTableName()
951  {
952  return "qpl_qst_javaapplet";
953  }
954 
960  {
962  }
963 
976  public function setExportDetailsXLS(&$worksheet, $startrow, $active_id, $pass, &$format_title, &$format_bold)
977  {
978  include_once ("./Services/Excel/classes/class.ilExcelUtils.php");
979  $solutions = $this->getSolutionValues($active_id, $pass);
980  $worksheet->writeString($startrow, 0, ilExcelUtils::_convert_text($this->lng->txt($this->getQuestionType())), $format_title);
981  $worksheet->writeString($startrow, 1, ilExcelUtils::_convert_text($this->getTitle()), $format_title);
982  $i = 1;
983  foreach ($solutions as $solution)
984  {
985  $worksheet->write($startrow + $i, 1, ilExcelUtils::_convert_text($this->lng->txt("result") . " $i"));
986  if (strlen($solution["value1"])) $worksheet->write($startrow + $i, 1, ilExcelUtils::_convert_text($solution["value1"]));
987  if (strlen($solution["value2"])) $worksheet->write($startrow + $i, 2, ilExcelUtils::_convert_text($solution["value2"]));
988  $i++;
989  }
990  return $startrow + $i + 1;
991  }
992 
993  public function isAutosaveable()
994  {
995  return FALSE;
996  }
997 
1006  public function getOperators($expression)
1007  {
1008  require_once "./Modules/TestQuestionPool/classes/class.ilOperatorsExpressionMapping.php";
1010  }
1011 
1016  public function getExpressionTypes()
1017  {
1018  return array(
1021  );
1022  }
1023 
1032  public function getUserQuestionResult($active_id, $pass)
1033  {
1034  $result = new ilUserQuestionResult($this, $active_id, $pass);
1035 
1036  $points = $this->calculateReachedPoints($active_id, $pass);
1037  $max_points = $this->getMaximumPoints();
1038 
1039  $result->setReachedPercentage(($points/$max_points) * 100);
1040 
1041  return $result;
1042  }
1043 
1052  public function getAvailableAnswerOptions($index = null)
1053  {
1054  return array();
1055  }
1056 }