ILIAS  Release_4_4_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 
21 class assJavaApplet extends assQuestion //implements ilObjQuestionScoringAdjustable
22 {
31 
40 
49 
58 
67 
76 
85 
100  function __construct(
101  $title = "",
102  $comment = "",
103  $author = "",
104  $owner = -1,
105  $question = "",
107  )
108  {
110  $this->javaapplet_filename = $javaapplet_filename;
111  $this->parameters = array();
112  }
113 
121  public function splitParams($params = "")
122  {
123  $params_array = split("<separator>", $params);
124  foreach ($params_array as $pair)
125  {
126  if (preg_match("/(.*?)\=(.*)/", $pair, $matches))
127  {
128  switch ($matches[1])
129  {
130  case "java_code" :
131  $this->java_code = $matches[2];
132  break;
133  case "java_codebase" :
134  $this->java_codebase = $matches[2];
135  break;
136  case "java_archive" :
137  $this->java_archive = $matches[2];
138  break;
139  case "java_width" :
140  $this->java_width = $matches[2];
141  break;
142  case "java_height" :
143  $this->java_height = $matches[2];
144  break;
145  }
146  if (preg_match("/param_name_(\d+)/", $matches[1], $found_key))
147  {
148  $this->parameters[$found_key[1]]["name"] = $matches[2];
149  }
150  if (preg_match("/param_value_(\d+)/", $matches[1], $found_key))
151  {
152  $this->parameters[$found_key[1]]["value"] = $matches[2];
153  }
154  }
155  }
156  }
157 
165  public function buildParams()
166  {
167  $params_array = array();
168  if ($this->java_code)
169  {
170  array_push($params_array, "java_code=$this->java_code");
171  }
172  if ($this->java_codebase)
173  {
174  array_push($params_array, "java_codebase=$this->java_codebase");
175  }
176  if ($this->java_archive)
177  {
178  array_push($params_array, "java_archive=$this->java_archive");
179  }
180  if ($this->java_width)
181  {
182  array_push($params_array, "java_width=$this->java_width");
183  }
184  if ($this->java_height)
185  {
186  array_push($params_array, "java_height=$this->java_height");
187  }
188  foreach ($this->parameters as $key => $value)
189  {
190  array_push($params_array, "param_name_$key=" . $value["name"]);
191  array_push($params_array, "param_value_$key=" . $value["value"]);
192  }
193 
194  return join($params_array, "<separator>");
195  }
196 
202  public function buildParamsOnly()
203  {
204  $params_array = array();
205  if ($this->java_code)
206  {
207  array_push($params_array, "java_code=$this->java_code");
208  array_push($params_array, "java_codebase=$this->java_codebase");
209  array_push($params_array, "java_archive=$this->java_archive");
210  }
211  foreach ($this->parameters as $key => $value)
212  {
213  array_push($params_array, "param_name_$key=" . $value["name"]);
214  array_push($params_array, "param_value_$key=" . $value["value"]);
215  }
216  return join($params_array, "<separator>");
217  }
218 
224  public function isComplete()
225  {
226  if (strlen($this->title)
227  && $this->author
228  && $this->question
229  && $this->javaapplet_filename
230  && $this->java_width
231  && $this->java_height
232  && $this->getPoints() > 0
233  )
234  {
235  return true;
236  }
237  else if (strlen($this->title)
238  && $this->author
239  && $this->question
240  && $this->getJavaArchive()
241  && $this->getJavaCodebase()
242  && $this->java_width
243  && $this->java_height
244  && $this->getPoints() > 0
245  )
246  {
247  return true;
248  }
249  return false;
250  }
251 
252 
260  public function saveToDb($original_id = "")
261  {
265  }
266 
268  {
269  global $ilDB;
270  $params = $this->buildParams();
271  // save additional data
272  $ilDB->manipulateF( "DELETE FROM " . $this->getAdditionalTableName() . " WHERE question_fi = %s",
273  array( "integer" ),
274  array( $this->getId() )
275  );
276  $ilDB->manipulateF( "INSERT INTO " . $this->getAdditionalTableName(
277  ) . " (question_fi, image_file, params) VALUES (%s, %s, %s)",
278  array( "integer", "text", "text" ),
279  array(
280  $this->getId(),
281  $this->javaapplet_filename,
282  $params
283  )
284  );
285  }
286 
293  public function loadFromDb($question_id)
294  {
295  global $ilDB;
296 
297  $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",
298  array("integer"),
299  array($question_id)
300  );
301  if ($result->numRows() == 1)
302  {
303  $data = $ilDB->fetchAssoc($result);
304  $this->setId($question_id);
305  $this->setObjId($data["obj_fi"]);
306  $this->setNrOfTries($data['nr_of_tries']);
307  $this->setTitle($data["title"]);
308  $this->setComment($data["description"]);
309  $this->setOriginalId($data["original_id"]);
310  $this->setAuthor($data["author"]);
311  $this->setPoints($data["points"]);
312  $this->setOwner($data["owner"]);
313  include_once("./Services/RTE/classes/class.ilRTE.php");
314  $this->setQuestion(ilRTE::_replaceMediaObjectImageSrc($data["question_text"], 1));
315  $this->setJavaAppletFilename($data["image_file"]);
316  $this->splitParams($data["params"]);
317  $this->setEstimatedWorkingTime(substr($data["working_time"], 0, 2), substr($data["working_time"], 3, 2), substr($data["working_time"], 6, 2));
318 
319  try
320  {
321  $this->setAdditionalContentEditingMode($data['add_cont_edit_mode']);
322  }
324  {
325  }
326  }
327  parent::loadFromDb($question_id);
328  }
329 
335  function duplicate($for_test = true, $title = "", $author = "", $owner = "", $testObjId = null)
336  {
337  if ($this->id <= 0)
338  {
339  // The question has not been saved. It cannot be duplicated
340  return;
341  }
342  // duplicate the question in database
343  $this_id = $this->getId();
344  $thisObjId = $this->getObjId();
345 
346  $clone = $this;
347  include_once ("./Modules/TestQuestionPool/classes/class.assQuestion.php");
349  $clone->id = -1;
350 
351  if( (int)$testObjId > 0 )
352  {
353  $clone->setObjId($testObjId);
354  }
355 
356  if ($title)
357  {
358  $clone->setTitle($title);
359  }
360  if ($author)
361  {
362  $clone->setAuthor($author);
363  }
364  if ($owner)
365  {
366  $clone->setOwner($owner);
367  }
368  if ($for_test)
369  {
370  $clone->saveToDb($original_id);
371  }
372  else
373  {
374  $clone->saveToDb();
375  }
376 
377  // copy question page content
378  $clone->copyPageOfQuestion($this_id);
379  // copy XHTML media objects
380  $clone->copyXHTMLMediaObjectsOfQuestion($this_id);
381  // duplicate the image
382  $clone->duplicateApplet($this_id, $thisObjId);
383 
384  $clone->onDuplicate($thisObjId, $this_id, $clone->getObjId(), $clone->getId());
385 
386  return $clone->id;
387  }
388 
396  function copyObject($target_questionpool_id, $title = "")
397  {
398  if ($this->id <= 0)
399  {
400  // The question has not been saved. It cannot be duplicated
401  return;
402  }
403  // duplicate the question in database
404  $clone = $this;
405  include_once ("./Modules/TestQuestionPool/classes/class.assQuestion.php");
407  $clone->id = -1;
408  $source_questionpool_id = $this->getObjId();
409  $clone->setObjId($target_questionpool_id);
410  if ($title)
411  {
412  $clone->setTitle($title);
413  }
414  $clone->saveToDb();
415 
416  // copy question page content
417  $clone->copyPageOfQuestion($original_id);
418  // copy XHTML media objects
419  $clone->copyXHTMLMediaObjectsOfQuestion($original_id);
420  // duplicate the image
421  $clone->copyApplet($original_id, $source_questionpool_id);
422 
423  $clone->onCopy($source_questionpool_id, $original_id, $clone->getObjId(), $clone->getId());
424 
425  return $clone->id;
426  }
427 
428  public function createNewOriginalFromThisDuplicate($targetParentId, $targetQuestionTitle = "")
429  {
430  if ($this->id <= 0)
431  {
432  // The question has not been saved. It cannot be duplicated
433  return;
434  }
435 
436  include_once ("./Modules/TestQuestionPool/classes/class.assQuestion.php");
437 
438  $sourceQuestionId = $this->id;
439  $sourceParentId = $this->getObjId();
440 
441  // duplicate the question in database
442  $clone = $this;
443  $clone->id = -1;
444 
445  $clone->setObjId($targetParentId);
446 
447  if ($targetQuestionTitle)
448  {
449  $clone->setTitle($targetQuestionTitle);
450  }
451 
452  $clone->saveToDb();
453  // copy question page content
454  $clone->copyPageOfQuestion($sourceQuestionId);
455  // copy XHTML media objects
456  $clone->copyXHTMLMediaObjectsOfQuestion($sourceQuestionId);
457  // duplicate the image
458  $clone->copyApplet($sourceQuestionId, $sourceParentId);
459 
460  $clone->onCopy($sourceParentId, $sourceQuestionId, $clone->getObjId(), $clone->getId());
461 
462  return $clone->id;
463  }
464 
465  function duplicateApplet($question_id, $objectId = null)
466  {
467  $javapath = $this->getJavaPath();
468  $javapath_original = preg_replace("/([^\d])$this->id([^\d])/", "\${1}$question_id\${2}", $javapath);
469 
470  if( (int)$objectId > 0 )
471  {
472  $javapath_original = str_replace("/$this->obj_id/", "/$objectId/", $javapath_original);
473  }
474 
475  if (!file_exists($javapath))
476  {
477  ilUtil::makeDirParents($javapath);
478  }
479  $filename = $this->getJavaAppletFilename();
480  if (!copy($javapath_original . $filename, $javapath . $filename)) {
481  print "java applet could not be duplicated!!!! ";
482  }
483  }
484 
485  function copyApplet($question_id, $source_questionpool)
486  {
487  $javapath = $this->getJavaPath();
488  $javapath_original = preg_replace("/([^\d])$this->id([^\d])/", "\${1}$question_id\${2}", $javapath);
489  $javapath_original = str_replace("/$this->obj_id/", "/$source_questionpool/", $javapath_original);
490  if (!file_exists($javapath))
491  {
492  ilUtil::makeDirParents($javapath);
493  }
494  $filename = $this->getJavaAppletFilename();
495  if (!copy($javapath_original . $filename, $javapath . $filename)) {
496  print "java applet could not be copied!!!! ";
497  }
498  }
499 
508  function getJavaCode()
509  {
510  return $this->java_code;
511  }
512 
521  function getJavaCodebase()
522  {
523  return $this->java_codebase;
524  }
525 
534  function getJavaArchive()
535  {
536  return $this->java_archive;
537  }
538 
547  function setJavaCode($java_code = "")
548  {
549  $this->java_code = $java_code;
550  }
551 
561  {
562  $this->java_codebase = $java_codebase;
563  }
564 
574  {
575  $this->java_archive = $java_archive;
576  }
577 
586  function getJavaWidth()
587  {
588  return $this->java_width;
589  }
590 
598  public function setJavaWidth($java_width = "")
599  {
600  $this->java_width = $java_width;
601  }
602 
611  function getJavaHeight()
612  {
613  return $this->java_height;
614  }
615 
625  {
626  $this->java_height = $java_height;
627  }
628 
640  public function calculateReachedPoints($active_id, $pass = NULL, $returndetails = FALSE)
641  {
642  if( $returndetails )
643  {
644  throw new ilTestException('return details not implemented for '.__METHOD__);
645  }
646 
647  global $ilDB;
648 
649  $found_values = array();
650  if (is_null($pass))
651  {
652  $pass = $this->getSolutionMaxPass($active_id);
653  }
654  $result = $ilDB->queryF("SELECT points FROM tst_solutions WHERE active_fi = %s AND question_fi = %s AND pass = %s",
655  array('integer','integer','integer'),
656  array($active_id, $this->getId(), $pass)
657  );
658  $points = 0;
659  while ($data = $ilDB->fetchAssoc($result))
660  {
661  $points += $data["points"];
662  }
663 
664  return $points;
665  }
666 
675  public function getReachedInformation($active_id, $pass = NULL)
676  {
677  global $ilDB;
678 
679  $found_values = array();
680  if (is_null($pass))
681  {
682  $pass = $this->getSolutionMaxPass($active_id);
683  }
684  $result = $ilDB->queryF("SELECT * FROM tst_solutions WHERE active_fi = %s AND question_fi = %s AND pass = %s",
685  array('integer','integer','integer'),
686  array($active_id, $this->getId(), $pass)
687  );
688  $counter = 1;
689  $user_result = array();
690  while ($data = $ilDB->fetchAssoc($result))
691  {
692  $true = 0;
693  if ($data["points"] > 0)
694  {
695  $true = 1;
696  }
697  $solution = array(
698  "order" => $counter,
699  "points" => $data["points"],
700  "true" => $true,
701  "value1" => $data["value1"],
702  "value2" => $data["value2"],
703  );
704  $counter++;
705  array_push($user_result, $solution);
706  }
707  return $user_result;
708  }
709 
718  function addParameter($name = "", $value = "")
719  {
720  $index = $this->getParameterIndex($name);
721  if ($index > -1)
722  {
723  $this->parameters[$index] = array("name" => $name, "value" => $value);
724  }
725  else
726  {
727  array_push($this->parameters, array("name" => $name, "value" => $value));
728  }
729  }
730 
731  public function addParameterAtIndex($index = 0, $name = "", $value = "")
732  {
733  if (array_key_exists($index, $this->parameters))
734  {
735  // insert parameter
736  $newparams = array();
737  for ($i = 0; $i < $index; $i++)
738  {
739  array_push($newparams, $this->parameters[$i]);
740  }
741  array_push($newparams, array($name, $value));
742  for ($i = $index; $i < count($this->parameters); $i++)
743  {
744  array_push($newparams, $this->parameters[$i]);
745  }
746  $this->parameters = $newparams;
747  }
748  else
749  {
750  array_push($this->parameters, array($name, $value));
751  }
752  }
753 
761  public function removeParameter($index)
762  {
763  if ($index < 0) return;
764  if (count($this->parameters) < 1) return;
765  if ($index >= count($this->parameters)) return;
766  unset($this->parameters[$index]);
767  $this->parameters = array_values($this->parameters);
768  }
769 
778  function getParameter($index)
779  {
780  if (($index < 0) or ($index >= count($this->parameters)))
781  {
782  return undef;
783  }
784  return $this->parameters[$index];
785  }
786 
795  function getParameterIndex($name)
796  {
797  foreach ($this->parameters as $key => $value)
798  {
799  if (array_key_exists($name, $value))
800  {
801  return $key;
802  }
803  }
804  return -1;
805  }
806 
814  function getParameterCount()
815  {
816  return count($this->parameters);
817  }
818 
825  function flushParams()
826  {
827  $this->parameters = array();
828  }
829 
838  public function saveWorkingData($active_id, $pass = NULL)
839  {
840  // nothing to save!
841 
842  //$this->getProcessLocker()->requestUserSolutionUpdateLock();
843  // store in tst_solutions
844  //$this->getProcessLocker()->releaseUserSolutionUpdateLock();
845 
846  return true;
847  }
848 
856  protected function reworkWorkingData($active_id, $pass, $obligationsAnswered)
857  {
858  // nothing to rework!
859  }
860 
869  {
871  }
872 
881  public function setJavaAppletFilename($javaapplet_filename, $javaapplet_tempfilename = "")
882  {
883  if (!empty($javaapplet_filename))
884  {
885  $this->javaapplet_filename = $javaapplet_filename;
886  }
887  if (!empty($javaapplet_tempfilename))
888  {
889  $javapath = $this->getJavaPath();
890  if (!file_exists($javapath))
891  {
892  ilUtil::makeDirParents($javapath);
893  }
894 
895  if (!ilUtil::moveUploadedFile($javaapplet_tempfilename, $javaapplet_filename, $javapath.$javaapplet_filename))
896  {
897  $ilLog->write("ERROR: java applet question: java applet not uploaded: $javaapplet_filename");
898  }
899  else
900  {
901  $this->setJavaCodebase();
902  $this->setJavaArchive();
903  }
904  }
905  }
906 
908  {
909  @unlink($this->getJavaPath() . $this->getJavaAppletFilename());
910  $this->javaapplet_filename = "";
911  }
912 
918  public function getQuestionType()
919  {
920  return "assJavaApplet";
921  }
922 
928  public function getAdditionalTableName()
929  {
930  return "qpl_qst_javaapplet";
931  }
932 
938  {
940  }
941 
954  public function setExportDetailsXLS(&$worksheet, $startrow, $active_id, $pass, &$format_title, &$format_bold)
955  {
956  include_once ("./Services/Excel/classes/class.ilExcelUtils.php");
957  $solutions = $this->getSolutionValues($active_id, $pass);
958  $worksheet->writeString($startrow, 0, ilExcelUtils::_convert_text($this->lng->txt($this->getQuestionType())), $format_title);
959  $worksheet->writeString($startrow, 1, ilExcelUtils::_convert_text($this->getTitle()), $format_title);
960  $i = 1;
961  foreach ($solutions as $solution)
962  {
963  $worksheet->write($startrow + $i, 1, ilExcelUtils::_convert_text($this->lng->txt("result") . " $i"));
964  if (strlen($solution["value1"])) $worksheet->write($startrow + $i, 1, ilExcelUtils::_convert_text($solution["value1"]));
965  if (strlen($solution["value2"])) $worksheet->write($startrow + $i, 2, ilExcelUtils::_convert_text($solution["value2"]));
966  $i++;
967  }
968  return $startrow + $i + 1;
969  }
970 
971  public function isAutosaveable()
972  {
973  return FALSE;
974  }
975 }