ILIAS  Release_4_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 /*
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 (($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 (($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 = "")
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  $clone = $this;
346  include_once ("./Modules/TestQuestionPool/classes/class.assQuestion.php");
348  $clone->id = -1;
349  if ($title)
350  {
351  $clone->setTitle($title);
352  }
353  if ($author)
354  {
355  $clone->setAuthor($author);
356  }
357  if ($owner)
358  {
359  $clone->setOwner($owner);
360  }
361  if ($for_test)
362  {
363  $clone->saveToDb($original_id);
364  }
365  else
366  {
367  $clone->saveToDb();
368  }
369 
370  // copy question page content
371  $clone->copyPageOfQuestion($this_id);
372  // copy XHTML media objects
373  $clone->copyXHTMLMediaObjectsOfQuestion($this_id);
374  // duplicate the generic feedback
375  $clone->duplicateFeedbackGeneric($this_id);
376 
377  // duplicate the image
378  $clone->duplicateApplet($this_id);
379  $clone->onDuplicate($this_id);
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->duplicateFeedbackGeneric($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)
424  {
425  $javapath = $this->getJavaPath();
426  $javapath_original = preg_replace("/([^\d])$this->id([^\d])/", "\${1}$question_id\${2}", $javapath);
427  if (!file_exists($javapath))
428  {
429  ilUtil::makeDirParents($javapath);
430  }
431  $filename = $this->getJavaAppletFilename();
432  if (!copy($javapath_original . $filename, $javapath . $filename)) {
433  print "java applet could not be duplicated!!!! ";
434  }
435  }
436 
437  function copyApplet($question_id, $source_questionpool)
438  {
439  $javapath = $this->getJavaPath();
440  $javapath_original = preg_replace("/([^\d])$this->id([^\d])/", "\${1}$question_id\${2}", $javapath);
441  $javapath_original = str_replace("/$this->obj_id/", "/$source_questionpool/", $javapath_original);
442  if (!file_exists($javapath))
443  {
444  ilUtil::makeDirParents($javapath);
445  }
446  $filename = $this->getJavaAppletFilename();
447  if (!copy($javapath_original . $filename, $javapath . $filename)) {
448  print "java applet could not be copied!!!! ";
449  }
450  }
451 
460  function getJavaCode()
461  {
462  return $this->java_code;
463  }
464 
473  function getJavaCodebase()
474  {
475  return $this->java_codebase;
476  }
477 
486  function getJavaArchive()
487  {
488  return $this->java_archive;
489  }
490 
499  function setJavaCode($java_code = "")
500  {
501  $this->java_code = $java_code;
502  }
503 
513  {
514  $this->java_codebase = $java_codebase;
515  }
516 
526  {
527  $this->java_archive = $java_archive;
528  }
529 
538  function getJavaWidth()
539  {
540  return $this->java_width;
541  }
542 
551  function setJavaWidth($java_width = "")
552  {
553  $this->java_width = $java_width;
554  }
555 
564  function getJavaHeight()
565  {
566  return $this->java_height;
567  }
568 
578  {
579  $this->java_height = $java_height;
580  }
581 
593  function calculateReachedPoints($active_id, $pass = NULL)
594  {
595  global $ilDB;
596 
597  $found_values = array();
598  if (is_null($pass))
599  {
600  $pass = $this->getSolutionMaxPass($active_id);
601  }
602  $result = $ilDB->queryF("SELECT points FROM tst_solutions WHERE active_fi = %s AND question_fi = %s AND pass = %s",
603  array('integer','integer','integer'),
604  array($active_id, $this->getId(), $pass)
605  );
606  $points = 0;
607  while ($data = $ilDB->fetchAssoc($result))
608  {
609  $points += $data["points"];
610  }
611 
612  $points = parent::calculateReachedPoints($active_id, $pass = NULL, $points);
613  return $points;
614  }
615 
625  function getReachedInformation($active_id, $pass = NULL)
626  {
627  global $ilDB;
628 
629  $found_values = array();
630  if (is_null($pass))
631  {
632  $pass = $this->getSolutionMaxPass($active_id);
633  }
634  $result = $ilDB->queryF("SELECT * FROM tst_solutions WHERE active_fi = %s AND question_fi = %s AND pass = %s",
635  array('integer','integer','integer'),
636  array($active_id, $this->getId(), $pass)
637  );
638  $counter = 1;
639  $user_result = array();
640  while ($data = $ilDB->fetchAssoc($result))
641  {
642  $true = 0;
643  if ($data["points"] > 0)
644  {
645  $true = 1;
646  }
647  $solution = array(
648  "order" => $counter,
649  "points" => $data["points"],
650  "true" => $true,
651  "value1" => $data["value1"],
652  "value2" => $data["value2"],
653  );
654  $counter++;
655  array_push($user_result, $solution);
656  }
657  return $user_result;
658  }
659 
668  function addParameter($name = "", $value = "")
669  {
670  $index = $this->getParameterIndex($name);
671  if ($index > -1)
672  {
673  $this->parameters[$index] = array("name" => $name, "value" => $value);
674  }
675  else
676  {
677  array_push($this->parameters, array("name" => $name, "value" => $value));
678  }
679  }
680 
681  public function addParameterAtIndex($index = 0, $name = "", $value = "")
682  {
683  if (array_key_exists($index, $this->parameters))
684  {
685  // insert parameter
686  $newparams = array();
687  for ($i = 0; $i < $index; $i++)
688  {
689  array_push($newparams, $this->parameters[$i]);
690  }
691  array_push($newparams, array($name, $value));
692  for ($i = $index; $i < count($this->parameters); $i++)
693  {
694  array_push($newparams, $this->parameters[$i]);
695  }
696  $this->parameters = $newparams;
697  }
698  else
699  {
700  array_push($this->parameters, array($name, $value));
701  }
702  }
703 
711  public function removeParameter($index)
712  {
713  if ($index < 0) return;
714  if (count($this->parameters) < 1) return;
715  if ($index >= count($this->parameters)) return;
716  unset($this->parameters[$index]);
717  $this->parameters = array_values($this->parameters);
718  }
719 
728  function getParameter($index)
729  {
730  if (($index < 0) or ($index >= count($this->parameters)))
731  {
732  return undef;
733  }
734  return $this->parameters[$index];
735  }
736 
746  {
747  foreach ($this->parameters as $key => $value)
748  {
749  if (array_key_exists($name, $value))
750  {
751  return $key;
752  }
753  }
754  return -1;
755  }
756 
764  function getParameterCount()
765  {
766  return count($this->parameters);
767  }
768 
775  function flushParams()
776  {
777  $this->parameters = array();
778  }
779 
788  function saveWorkingData($active_id, $pass = NULL)
789  {
790  parent::saveWorkingData($active_id, $pass);
791  return true;
792  }
793 
802  {
804  }
805 
813  function setJavaAppletFilename($javaapplet_filename, $javaapplet_tempfilename = "")
814  {
815  if (!empty($javaapplet_filename))
816  {
817  $this->javaapplet_filename = $javaapplet_filename;
818  }
819  if (!empty($javaapplet_tempfilename))
820  {
821  $javapath = $this->getJavaPath();
822  if (!file_exists($javapath))
823  {
824  ilUtil::makeDirParents($javapath);
825  }
826 
827  if (!ilUtil::moveUploadedFile($javaapplet_tempfilename, $javaapplet_filename, $javapath.$javaapplet_filename))
828  {
829  $ilLog->write("ERROR: java applet question: java applet not uploaded: $javaapplet_filename");
830  }
831  else
832  {
833  $this->setJavaCodebase();
834  $this->setJavaArchive();
835  }
836  }
837  }
838 
840  {
841  @unlink($this->getJavaPath() . $this->getJavaAppletFilename());
842  $this->javaapplet_filename = "";
843  }
844 
851  function getQuestionType()
852  {
853  return "assJavaApplet";
854  }
855 
863  {
864  return "qpl_qst_javaapplet";
865  }
866 
872  {
874  }
875 
888  public function setExportDetailsXLS(&$worksheet, $startrow, $active_id, $pass, &$format_title, &$format_bold)
889  {
890  include_once ("./Services/Excel/classes/class.ilExcelUtils.php");
891  $solutions = $this->getSolutionValues($active_id, $pass);
892  $worksheet->writeString($startrow, 0, ilExcelUtils::_convert_text($this->lng->txt($this->getQuestionType())), $format_title);
893  $worksheet->writeString($startrow, 1, ilExcelUtils::_convert_text($this->getTitle()), $format_title);
894  $i = 1;
895  foreach ($solutions as $solution)
896  {
897  $worksheet->write($startrow + $i, 1, ilExcelUtils::_convert_text($this->lng->txt("result") . " $i"));
898  if (strlen($solution["value1"])) $worksheet->write($startrow + $i, 1, ilExcelUtils::_convert_text($solution["value1"]));
899  if (strlen($solution["value2"])) $worksheet->write($startrow + $i, 2, ilExcelUtils::_convert_text($solution["value2"]));
900  $i++;
901  }
902  return $startrow + $i + 1;
903  }
904 }
905 
906 ?>