ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.assJavaApplet.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 
22 {
31 
40 
49 
58 
67 
76 
85 
101  function __construct(
102  $title = "",
103  $comment = "",
104  $author = "",
105  $owner = -1,
106  $question = "",
108  )
109  {
111  $this->javaapplet_filename = $javaapplet_filename;
112  $this->parameters = array();
113  }
114 
123  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 
168  function buildParams()
169  {
170  $params_array = array();
171  if ($this->java_code)
172  {
173  array_push($params_array, "java_code=$this->java_code");
174  }
175  if ($this->java_codebase)
176  {
177  array_push($params_array, "java_codebase=$this->java_codebase");
178  }
179  if ($this->java_archive)
180  {
181  array_push($params_array, "java_archive=$this->java_archive");
182  }
183  if ($this->java_width)
184  {
185  array_push($params_array, "java_width=$this->java_width");
186  }
187  if ($this->java_height)
188  {
189  array_push($params_array, "java_height=$this->java_height");
190  }
191  foreach ($this->parameters as $key => $value)
192  {
193  array_push($params_array, "param_name_$key=" . $value["name"]);
194  array_push($params_array, "param_value_$key=" . $value["value"]);
195  }
196  return join($params_array, "<separator>");
197  }
198 
207  function buildParamsOnly()
208  {
209  $params_array = array();
210  if ($this->java_code)
211  {
212  array_push($params_array, "java_code=$this->java_code");
213  array_push($params_array, "java_codebase=$this->java_codebase");
214  array_push($params_array, "java_archive=$this->java_archive");
215  }
216  foreach ($this->parameters as $key => $value)
217  {
218  array_push($params_array, "param_name_$key=" . $value["name"]);
219  array_push($params_array, "param_value_$key=" . $value["value"]);
220  }
221  return join($params_array, "<separator>");
222  }
223 
230  function isComplete()
231  {
232  if (strlen($this->title) and ($this->author) and ($this->question) and ($this->javaapplet_filename) and ($this->java_width) and ($this->java_height) and ($this->getPoints() > 0))
233  {
234  return true;
235  }
236  else if (strlen($this->title) and ($this->author) and ($this->question) and ($this->getJavaArchive()) and ($this->getJavaCodebase()) and ($this->java_width) and ($this->java_height) and ($this->getPoints() > 0))
237  {
238  return true;
239  }
240  else
241  {
242  return false;
243  }
244  }
245 
246 
255  function saveToDb($original_id = "")
256  {
257  global $ilDB;
258 
260 
261  $params = $this->buildParams();
262  // save additional data
263  $affectedRows = $ilDB->manipulateF("DELETE FROM " . $this->getAdditionalTableName() . " WHERE question_fi = %s",
264  array("integer"),
265  array($this->getId())
266  );
267  $affectedRows = $ilDB->manipulateF("INSERT INTO " . $this->getAdditionalTableName() . " (question_fi, image_file, params) VALUES (%s, %s, %s)",
268  array("integer", "text", "text"),
269  array(
270  $this->getId(),
271  $this->javaapplet_filename,
272  $params
273  )
274  );
275 
277  }
278 
288  function loadFromDb($question_id)
289  {
290  global $ilDB;
291 
292  $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",
293  array("integer"),
294  array($question_id)
295  );
296  if ($result->numRows() == 1)
297  {
298  $data = $ilDB->fetchAssoc($result);
299  $this->setId($question_id);
300  $this->setObjId($data["obj_fi"]);
301  $this->setNrOfTries($data['nr_of_tries']);
302  $this->setTitle($data["title"]);
303  $this->setComment($data["description"]);
304  $this->setOriginalId($data["original_id"]);
305  $this->setAuthor($data["author"]);
306  $this->setPoints($data["points"]);
307  $this->setOwner($data["owner"]);
308  include_once("./Services/RTE/classes/class.ilRTE.php");
309  $this->setQuestion(ilRTE::_replaceMediaObjectImageSrc($data["question_text"], 1));
310  $this->setJavaAppletFilename($data["image_file"]);
311  $this->splitParams($data["params"]);
312  $this->setEstimatedWorkingTime(substr($data["working_time"], 0, 2), substr($data["working_time"], 3, 2), substr($data["working_time"], 6, 2));
313  }
314  parent::loadFromDb($question_id);
315  }
316 
322  function duplicate($for_test = true, $title = "", $author = "", $owner = "", $testObjId = null)
323  {
324  if ($this->id <= 0)
325  {
326  // The question has not been saved. It cannot be duplicated
327  return;
328  }
329  // duplicate the question in database
330  $this_id = $this->getId();
331 
332  if( (int)$testObjId > 0 )
333  {
334  $thisObjId = $this->getObjId();
335  }
336 
337  $clone = $this;
338  include_once ("./Modules/TestQuestionPool/classes/class.assQuestion.php");
340  $clone->id = -1;
341 
342  if( (int)$testObjId > 0 )
343  {
344  $clone->setObjId($testObjId);
345  }
346 
347  if ($title)
348  {
349  $clone->setTitle($title);
350  }
351  if ($author)
352  {
353  $clone->setAuthor($author);
354  }
355  if ($owner)
356  {
357  $clone->setOwner($owner);
358  }
359  if ($for_test)
360  {
361  $clone->saveToDb($original_id);
362  }
363  else
364  {
365  $clone->saveToDb();
366  }
367 
368  // copy question page content
369  $clone->copyPageOfQuestion($this_id);
370  // copy XHTML media objects
371  $clone->copyXHTMLMediaObjectsOfQuestion($this_id);
372  // duplicate the generic feedback
373  $clone->duplicateGenericFeedback($this_id);
374 
375  // duplicate the image
376  $clone->duplicateApplet($this_id, $thisObjId);
377 
378  $clone->onDuplicate($thisObjId, $this_id, $clone->getObjId(), $clone->getId());
379 
380  return $clone->id;
381  }
382 
390  function copyObject($target_questionpool, $title = "")
391  {
392  if ($this->id <= 0)
393  {
394  // The question has not been saved. It cannot be duplicated
395  return;
396  }
397  // duplicate the question in database
398  $clone = $this;
399  include_once ("./Modules/TestQuestionPool/classes/class.assQuestion.php");
401  $clone->id = -1;
402  $source_questionpool = $this->getObjId();
403  $clone->setObjId($target_questionpool);
404  if ($title)
405  {
406  $clone->setTitle($title);
407  }
408  $clone->saveToDb();
409 
410  // copy question page content
411  $clone->copyPageOfQuestion($original_id);
412  // copy XHTML media objects
413  $clone->copyXHTMLMediaObjectsOfQuestion($original_id);
414  // duplicate the generic feedback
415  $clone->duplicateGenericFeedback($original_id);
416 
417  // duplicate the image
418  $clone->copyApplet($original_id, $source_questionpool);
419  $clone->onCopy($this->getObjId(), $this->getId());
420  return $clone->id;
421  }
422 
423  function duplicateApplet($question_id, $objectId = null)
424  {
425  $javapath = $this->getJavaPath();
426  $javapath_original = preg_replace("/([^\d])$this->id([^\d])/", "\${1}$question_id\${2}", $javapath);
427 
428  if( (int)$objectId > 0 )
429  {
430  $javapath_original = str_replace("/$this->obj_id/", "/$objectId/", $javapath_original);
431  }
432 
433  if (!file_exists($javapath))
434  {
435  ilUtil::makeDirParents($javapath);
436  }
437  $filename = $this->getJavaAppletFilename();
438  if (!copy($javapath_original . $filename, $javapath . $filename)) {
439  print "java applet could not be duplicated!!!! ";
440  }
441  }
442 
443  function copyApplet($question_id, $source_questionpool)
444  {
445  $javapath = $this->getJavaPath();
446  $javapath_original = preg_replace("/([^\d])$this->id([^\d])/", "\${1}$question_id\${2}", $javapath);
447  $javapath_original = str_replace("/$this->obj_id/", "/$source_questionpool/", $javapath_original);
448  if (!file_exists($javapath))
449  {
450  ilUtil::makeDirParents($javapath);
451  }
452  $filename = $this->getJavaAppletFilename();
453  if (!copy($javapath_original . $filename, $javapath . $filename)) {
454  print "java applet could not be copied!!!! ";
455  }
456  }
457 
466  function getJavaCode()
467  {
468  return $this->java_code;
469  }
470 
479  function getJavaCodebase()
480  {
481  return $this->java_codebase;
482  }
483 
492  function getJavaArchive()
493  {
494  return $this->java_archive;
495  }
496 
505  function setJavaCode($java_code = "")
506  {
507  $this->java_code = $java_code;
508  }
509 
519  {
520  $this->java_codebase = $java_codebase;
521  }
522 
532  {
533  $this->java_archive = $java_archive;
534  }
535 
544  function getJavaWidth()
545  {
546  return $this->java_width;
547  }
548 
557  function setJavaWidth($java_width = "")
558  {
559  $this->java_width = $java_width;
560  }
561 
570  function getJavaHeight()
571  {
572  return $this->java_height;
573  }
574 
584  {
585  $this->java_height = $java_height;
586  }
587 
598  public function calculateReachedPoints($active_id, $pass = NULL, $returndetails = FALSE)
599  {
600  if( $returndetails )
601  {
602  throw new ilTestException('return details not implemented for '.__METHOD__);
603  }
604 
605  global $ilDB;
606 
607  $found_values = array();
608  if (is_null($pass))
609  {
610  $pass = $this->getSolutionMaxPass($active_id);
611  }
612  $result = $ilDB->queryF("SELECT points FROM tst_solutions WHERE active_fi = %s AND question_fi = %s AND pass = %s",
613  array('integer','integer','integer'),
614  array($active_id, $this->getId(), $pass)
615  );
616  $points = 0;
617  while ($data = $ilDB->fetchAssoc($result))
618  {
619  $points += $data["points"];
620  }
621 
622  return $points;
623  }
624 
634  function getReachedInformation($active_id, $pass = NULL)
635  {
636  global $ilDB;
637 
638  $found_values = array();
639  if (is_null($pass))
640  {
641  $pass = $this->getSolutionMaxPass($active_id);
642  }
643  $result = $ilDB->queryF("SELECT * FROM tst_solutions WHERE active_fi = %s AND question_fi = %s AND pass = %s",
644  array('integer','integer','integer'),
645  array($active_id, $this->getId(), $pass)
646  );
647  $counter = 1;
648  $user_result = array();
649  while ($data = $ilDB->fetchAssoc($result))
650  {
651  $true = 0;
652  if ($data["points"] > 0)
653  {
654  $true = 1;
655  }
656  $solution = array(
657  "order" => $counter,
658  "points" => $data["points"],
659  "true" => $true,
660  "value1" => $data["value1"],
661  "value2" => $data["value2"],
662  );
663  $counter++;
664  array_push($user_result, $solution);
665  }
666  return $user_result;
667  }
668 
677  function addParameter($name = "", $value = "")
678  {
679  $index = $this->getParameterIndex($name);
680  if ($index > -1)
681  {
682  $this->parameters[$index] = array("name" => $name, "value" => $value);
683  }
684  else
685  {
686  array_push($this->parameters, array("name" => $name, "value" => $value));
687  }
688  }
689 
690  public function addParameterAtIndex($index = 0, $name = "", $value = "")
691  {
692  if (array_key_exists($index, $this->parameters))
693  {
694  // insert parameter
695  $newparams = array();
696  for ($i = 0; $i < $index; $i++)
697  {
698  array_push($newparams, $this->parameters[$i]);
699  }
700  array_push($newparams, array($name, $value));
701  for ($i = $index; $i < count($this->parameters); $i++)
702  {
703  array_push($newparams, $this->parameters[$i]);
704  }
705  $this->parameters = $newparams;
706  }
707  else
708  {
709  array_push($this->parameters, array($name, $value));
710  }
711  }
712 
720  public function removeParameter($index)
721  {
722  if ($index < 0) return;
723  if (count($this->parameters) < 1) return;
724  if ($index >= count($this->parameters)) return;
725  unset($this->parameters[$index]);
726  $this->parameters = array_values($this->parameters);
727  }
728 
737  function getParameter($index)
738  {
739  if (($index < 0) or ($index >= count($this->parameters)))
740  {
741  return undef;
742  }
743  return $this->parameters[$index];
744  }
745 
754  function getParameterIndex($name)
755  {
756  foreach ($this->parameters as $key => $value)
757  {
758  if (array_key_exists($name, $value))
759  {
760  return $key;
761  }
762  }
763  return -1;
764  }
765 
773  function getParameterCount()
774  {
775  return count($this->parameters);
776  }
777 
784  function flushParams()
785  {
786  $this->parameters = array();
787  }
788 
797  public function saveWorkingData($active_id, $pass = NULL)
798  {
799  // nothing to save!
800 
801  return true;
802  }
803 
812  protected function reworkWorkingData($active_id, $pass, $obligationsAnswered)
813  {
814  // nothing to rework!
815  }
816 
825  {
827  }
828 
836  function setJavaAppletFilename($javaapplet_filename, $javaapplet_tempfilename = "")
837  {
838  if (!empty($javaapplet_filename))
839  {
840  $this->javaapplet_filename = $javaapplet_filename;
841  }
842  if (!empty($javaapplet_tempfilename))
843  {
844  $javapath = $this->getJavaPath();
845  if (!file_exists($javapath))
846  {
847  ilUtil::makeDirParents($javapath);
848  }
849 
850  if (!ilUtil::moveUploadedFile($javaapplet_tempfilename, $javaapplet_filename, $javapath.$javaapplet_filename))
851  {
852  $ilLog->write("ERROR: java applet question: java applet not uploaded: $javaapplet_filename");
853  }
854  else
855  {
856  $this->setJavaCodebase();
857  $this->setJavaArchive();
858  }
859  }
860  }
861 
863  {
864  @unlink($this->getJavaPath() . $this->getJavaAppletFilename());
865  $this->javaapplet_filename = "";
866  }
867 
874  function getQuestionType()
875  {
876  return "assJavaApplet";
877  }
878 
886  {
887  return "qpl_qst_javaapplet";
888  }
889 
895  {
897  }
898 
911  public function setExportDetailsXLS(&$worksheet, $startrow, $active_id, $pass, &$format_title, &$format_bold)
912  {
913  include_once ("./Services/Excel/classes/class.ilExcelUtils.php");
914  $solutions = $this->getSolutionValues($active_id, $pass);
915  $worksheet->writeString($startrow, 0, ilExcelUtils::_convert_text($this->lng->txt($this->getQuestionType())), $format_title);
916  $worksheet->writeString($startrow, 1, ilExcelUtils::_convert_text($this->getTitle()), $format_title);
917  $i = 1;
918  foreach ($solutions as $solution)
919  {
920  $worksheet->write($startrow + $i, 1, ilExcelUtils::_convert_text($this->lng->txt("result") . " $i"));
921  if (strlen($solution["value1"])) $worksheet->write($startrow + $i, 1, ilExcelUtils::_convert_text($solution["value1"]));
922  if (strlen($solution["value2"])) $worksheet->write($startrow + $i, 2, ilExcelUtils::_convert_text($solution["value2"]));
923  $i++;
924  }
925  return $startrow + $i + 1;
926  }
927 
928  public function isAutosaveable()
929  {
930  return FALSE;
931  }
932 }