ILIAS  Release_4_2_x_branch Revision 61807
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.assJavaApplet.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 
36 {
45 
54 
63 
72 
81 
90 
99 
115  function __construct(
116  $title = "",
117  $comment = "",
118  $author = "",
119  $owner = -1,
120  $question = "",
122  )
123  {
125  $this->javaapplet_filename = $javaapplet_filename;
126  $this->parameters = array();
127  }
128 
137  function splitParams($params = "")
138  {
139  $params_array = split("<separator>", $params);
140  foreach ($params_array as $pair)
141  {
142  if (preg_match("/(.*?)\=(.*)/", $pair, $matches))
143  {
144  switch ($matches[1])
145  {
146  case "java_code" :
147  $this->java_code = $matches[2];
148  break;
149  case "java_codebase" :
150  $this->java_codebase = $matches[2];
151  break;
152  case "java_archive" :
153  $this->java_archive = $matches[2];
154  break;
155  case "java_width" :
156  $this->java_width = $matches[2];
157  break;
158  case "java_height" :
159  $this->java_height = $matches[2];
160  break;
161  }
162  if (preg_match("/param_name_(\d+)/", $matches[1], $found_key))
163  {
164  $this->parameters[$found_key[1]]["name"] = $matches[2];
165  }
166  if (preg_match("/param_value_(\d+)/", $matches[1], $found_key))
167  {
168  $this->parameters[$found_key[1]]["value"] = $matches[2];
169  }
170  }
171  }
172  }
173 
182  function buildParams()
183  {
184  $params_array = array();
185  if ($this->java_code)
186  {
187  array_push($params_array, "java_code=$this->java_code");
188  }
189  if ($this->java_codebase)
190  {
191  array_push($params_array, "java_codebase=$this->java_codebase");
192  }
193  if ($this->java_archive)
194  {
195  array_push($params_array, "java_archive=$this->java_archive");
196  }
197  if ($this->java_width)
198  {
199  array_push($params_array, "java_width=$this->java_width");
200  }
201  if ($this->java_height)
202  {
203  array_push($params_array, "java_height=$this->java_height");
204  }
205  foreach ($this->parameters as $key => $value)
206  {
207  array_push($params_array, "param_name_$key=" . $value["name"]);
208  array_push($params_array, "param_value_$key=" . $value["value"]);
209  }
210  return join($params_array, "<separator>");
211  }
212 
221  function buildParamsOnly()
222  {
223  $params_array = array();
224  if ($this->java_code)
225  {
226  array_push($params_array, "java_code=$this->java_code");
227  array_push($params_array, "java_codebase=$this->java_codebase");
228  array_push($params_array, "java_archive=$this->java_archive");
229  }
230  foreach ($this->parameters as $key => $value)
231  {
232  array_push($params_array, "param_name_$key=" . $value["name"]);
233  array_push($params_array, "param_value_$key=" . $value["value"]);
234  }
235  return join($params_array, "<separator>");
236  }
237 
244  function isComplete()
245  {
246  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))
247  {
248  return true;
249  }
250  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))
251  {
252  return true;
253  }
254  else
255  {
256  return false;
257  }
258  }
259 
260 
269  function saveToDb($original_id = "")
270  {
271  global $ilDB;
272 
274 
275  $params = $this->buildParams();
276  // save additional data
277  $affectedRows = $ilDB->manipulateF("DELETE FROM " . $this->getAdditionalTableName() . " WHERE question_fi = %s",
278  array("integer"),
279  array($this->getId())
280  );
281  $affectedRows = $ilDB->manipulateF("INSERT INTO " . $this->getAdditionalTableName() . " (question_fi, image_file, params) VALUES (%s, %s, %s)",
282  array("integer", "text", "text"),
283  array(
284  $this->getId(),
285  $this->javaapplet_filename,
286  $params
287  )
288  );
289 
291  }
292 
302  function loadFromDb($question_id)
303  {
304  global $ilDB;
305 
306  $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",
307  array("integer"),
308  array($question_id)
309  );
310  if ($result->numRows() == 1)
311  {
312  $data = $ilDB->fetchAssoc($result);
313  $this->setId($question_id);
314  $this->setObjId($data["obj_fi"]);
315  $this->setNrOfTries($data['nr_of_tries']);
316  $this->setTitle($data["title"]);
317  $this->setComment($data["description"]);
318  $this->setOriginalId($data["original_id"]);
319  $this->setAuthor($data["author"]);
320  $this->setPoints($data["points"]);
321  $this->setOwner($data["owner"]);
322  include_once("./Services/RTE/classes/class.ilRTE.php");
323  $this->setQuestion(ilRTE::_replaceMediaObjectImageSrc($data["question_text"], 1));
324  $this->setJavaAppletFilename($data["image_file"]);
325  $this->splitParams($data["params"]);
326  $this->setEstimatedWorkingTime(substr($data["working_time"], 0, 2), substr($data["working_time"], 3, 2), substr($data["working_time"], 6, 2));
327  }
328  parent::loadFromDb($question_id);
329  }
330 
336  function duplicate($for_test = true, $title = "", $author = "", $owner = "", $testObjId = null)
337  {
338  if ($this->id <= 0)
339  {
340  // The question has not been saved. It cannot be duplicated
341  return;
342  }
343  // duplicate the question in database
344  $this_id = $this->getId();
345 
346  if( (int)$testObjId > 0 )
347  {
348  $thisObjId = $this->getObjId();
349  }
350 
351  $clone = $this;
352  include_once ("./Modules/TestQuestionPool/classes/class.assQuestion.php");
354  $clone->id = -1;
355 
356  if( (int)$testObjId > 0 )
357  {
358  $clone->setObjId($testObjId);
359  }
360 
361  if ($title)
362  {
363  $clone->setTitle($title);
364  }
365  if ($author)
366  {
367  $clone->setAuthor($author);
368  }
369  if ($owner)
370  {
371  $clone->setOwner($owner);
372  }
373  if ($for_test)
374  {
375  $clone->saveToDb($original_id);
376  }
377  else
378  {
379  $clone->saveToDb();
380  }
381 
382  // copy question page content
383  $clone->copyPageOfQuestion($this_id);
384  // copy XHTML media objects
385  $clone->copyXHTMLMediaObjectsOfQuestion($this_id);
386  // duplicate the generic feedback
387  $clone->duplicateFeedbackGeneric($this_id);
388 
389  // duplicate the image
390  $clone->duplicateApplet($this_id, $thisObjId);
391 
392  $clone->onDuplicate($thisObjId, $this_id, $clone->getObjId(), $clone->getId());
393 
394  return $clone->id;
395  }
396 
404  function copyObject($target_questionpool, $title = "")
405  {
406  if ($this->id <= 0)
407  {
408  // The question has not been saved. It cannot be duplicated
409  return;
410  }
411  // duplicate the question in database
412  $clone = $this;
413  include_once ("./Modules/TestQuestionPool/classes/class.assQuestion.php");
415  $clone->id = -1;
416  $source_questionpool = $this->getObjId();
417  $clone->setObjId($target_questionpool);
418  if ($title)
419  {
420  $clone->setTitle($title);
421  }
422  $clone->saveToDb();
423 
424  // copy question page content
425  $clone->copyPageOfQuestion($original_id);
426  // copy XHTML media objects
427  $clone->copyXHTMLMediaObjectsOfQuestion($original_id);
428  // duplicate the generic feedback
429  $clone->duplicateFeedbackGeneric($original_id);
430 
431  // duplicate the image
432  $clone->copyApplet($original_id, $source_questionpool);
433  $clone->onCopy($this->getObjId(), $this->getId());
434  return $clone->id;
435  }
436 
437  function duplicateApplet($question_id, $objectId = null)
438  {
439  $javapath = $this->getJavaPath();
440  $javapath_original = preg_replace("/([^\d])$this->id([^\d])/", "\${1}$question_id\${2}", $javapath);
441 
442  if( (int)$objectId > 0 )
443  {
444  $javapath_original = str_replace("/$this->obj_id/", "/$objectId/", $javapath_original);
445  }
446 
447  if (!file_exists($javapath))
448  {
449  ilUtil::makeDirParents($javapath);
450  }
451  $filename = $this->getJavaAppletFilename();
452  if (!copy($javapath_original . $filename, $javapath . $filename)) {
453  print "java applet could not be duplicated!!!! ";
454  }
455  }
456 
457  function copyApplet($question_id, $source_questionpool)
458  {
459  $javapath = $this->getJavaPath();
460  $javapath_original = preg_replace("/([^\d])$this->id([^\d])/", "\${1}$question_id\${2}", $javapath);
461  $javapath_original = str_replace("/$this->obj_id/", "/$source_questionpool/", $javapath_original);
462  if (!file_exists($javapath))
463  {
464  ilUtil::makeDirParents($javapath);
465  }
466  $filename = $this->getJavaAppletFilename();
467  if (!copy($javapath_original . $filename, $javapath . $filename)) {
468  print "java applet could not be copied!!!! ";
469  }
470  }
471 
480  function getJavaCode()
481  {
482  return $this->java_code;
483  }
484 
493  function getJavaCodebase()
494  {
495  return $this->java_codebase;
496  }
497 
506  function getJavaArchive()
507  {
508  return $this->java_archive;
509  }
510 
519  function setJavaCode($java_code = "")
520  {
521  $this->java_code = $java_code;
522  }
523 
533  {
534  $this->java_codebase = $java_codebase;
535  }
536 
546  {
547  $this->java_archive = $java_archive;
548  }
549 
558  function getJavaWidth()
559  {
560  return $this->java_width;
561  }
562 
571  function setJavaWidth($java_width = "")
572  {
573  $this->java_width = $java_width;
574  }
575 
584  function getJavaHeight()
585  {
586  return $this->java_height;
587  }
588 
598  {
599  $this->java_height = $java_height;
600  }
601 
613  function calculateReachedPoints($active_id, $pass = NULL)
614  {
615  global $ilDB;
616 
617  $found_values = array();
618  if (is_null($pass))
619  {
620  $pass = $this->getSolutionMaxPass($active_id);
621  }
622  $result = $ilDB->queryF("SELECT points FROM tst_solutions WHERE active_fi = %s AND question_fi = %s AND pass = %s",
623  array('integer','integer','integer'),
624  array($active_id, $this->getId(), $pass)
625  );
626  $points = 0;
627  while ($data = $ilDB->fetchAssoc($result))
628  {
629  $points += $data["points"];
630  }
631 
632  $points = parent::calculateReachedPoints($active_id, $pass = NULL, $points);
633  return $points;
634  }
635 
645  function getReachedInformation($active_id, $pass = NULL)
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 * 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  $counter = 1;
659  $user_result = array();
660  while ($data = $ilDB->fetchAssoc($result))
661  {
662  $true = 0;
663  if ($data["points"] > 0)
664  {
665  $true = 1;
666  }
667  $solution = array(
668  "order" => $counter,
669  "points" => $data["points"],
670  "true" => $true,
671  "value1" => $data["value1"],
672  "value2" => $data["value2"],
673  );
674  $counter++;
675  array_push($user_result, $solution);
676  }
677  return $user_result;
678  }
679 
688  function addParameter($name = "", $value = "")
689  {
690  $index = $this->getParameterIndex($name);
691  if ($index > -1)
692  {
693  $this->parameters[$index] = array("name" => $name, "value" => $value);
694  }
695  else
696  {
697  array_push($this->parameters, array("name" => $name, "value" => $value));
698  }
699  }
700 
701  public function addParameterAtIndex($index = 0, $name = "", $value = "")
702  {
703  if (array_key_exists($index, $this->parameters))
704  {
705  // insert parameter
706  $newparams = array();
707  for ($i = 0; $i < $index; $i++)
708  {
709  array_push($newparams, $this->parameters[$i]);
710  }
711  array_push($newparams, array($name, $value));
712  for ($i = $index; $i < count($this->parameters); $i++)
713  {
714  array_push($newparams, $this->parameters[$i]);
715  }
716  $this->parameters = $newparams;
717  }
718  else
719  {
720  array_push($this->parameters, array($name, $value));
721  }
722  }
723 
731  public function removeParameter($index)
732  {
733  if ($index < 0) return;
734  if (count($this->parameters) < 1) return;
735  if ($index >= count($this->parameters)) return;
736  unset($this->parameters[$index]);
737  $this->parameters = array_values($this->parameters);
738  }
739 
748  function getParameter($index)
749  {
750  if (($index < 0) or ($index >= count($this->parameters)))
751  {
752  return undef;
753  }
754  return $this->parameters[$index];
755  }
756 
765  function getParameterIndex($name)
766  {
767  foreach ($this->parameters as $key => $value)
768  {
769  if (array_key_exists($name, $value))
770  {
771  return $key;
772  }
773  }
774  return -1;
775  }
776 
784  function getParameterCount()
785  {
786  return count($this->parameters);
787  }
788 
795  function flushParams()
796  {
797  $this->parameters = array();
798  }
799 
808  function saveWorkingData($active_id, $pass = NULL)
809  {
810  parent::saveWorkingData($active_id, $pass);
811  return true;
812  }
813 
822  {
824  }
825 
833  function setJavaAppletFilename($javaapplet_filename, $javaapplet_tempfilename = "")
834  {
835  if (!empty($javaapplet_filename))
836  {
837  $this->javaapplet_filename = $javaapplet_filename;
838  }
839  if (!empty($javaapplet_tempfilename))
840  {
841  $javapath = $this->getJavaPath();
842  if (!file_exists($javapath))
843  {
844  ilUtil::makeDirParents($javapath);
845  }
846 
847  if (!ilUtil::moveUploadedFile($javaapplet_tempfilename, $javaapplet_filename, $javapath.$javaapplet_filename))
848  {
849  $ilLog->write("ERROR: java applet question: java applet not uploaded: $javaapplet_filename");
850  }
851  else
852  {
853  $this->setJavaCodebase();
854  $this->setJavaArchive();
855  }
856  }
857  }
858 
860  {
861  @unlink($this->getJavaPath() . $this->getJavaAppletFilename());
862  $this->javaapplet_filename = "";
863  }
864 
871  function getQuestionType()
872  {
873  return "assJavaApplet";
874  }
875 
883  {
884  return "qpl_qst_javaapplet";
885  }
886 
892  {
894  }
895 
908  public function setExportDetailsXLS(&$worksheet, $startrow, $active_id, $pass, &$format_title, &$format_bold)
909  {
910  include_once ("./Services/Excel/classes/class.ilExcelUtils.php");
911  $solutions = $this->getSolutionValues($active_id, $pass);
912  $worksheet->writeString($startrow, 0, ilExcelUtils::_convert_text($this->lng->txt($this->getQuestionType())), $format_title);
913  $worksheet->writeString($startrow, 1, ilExcelUtils::_convert_text($this->getTitle()), $format_title);
914  $i = 1;
915  foreach ($solutions as $solution)
916  {
917  $worksheet->write($startrow + $i, 1, ilExcelUtils::_convert_text($this->lng->txt("result") . " $i"));
918  if (strlen($solution["value1"])) $worksheet->write($startrow + $i, 1, ilExcelUtils::_convert_text($solution["value1"]));
919  if (strlen($solution["value2"])) $worksheet->write($startrow + $i, 2, ilExcelUtils::_convert_text($solution["value2"]));
920  $i++;
921  }
922  return $startrow + $i + 1;
923  }
924 }
925 
926 ?>