ILIAS  Release_3_10_x_branch Revision 61812
 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 assJavaApplet(
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 
246  function isComplete()
247  {
248  if (($this->title) and ($this->author) and ($this->question) and ($this->javaapplet_filename) and ($this->java_width) and ($this->java_height) and ($this->getMaximumPoints() > 0))
249  {
250  return true;
251  }
252  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->getMaximumPoints() > 0))
253  {
254  return true;
255  }
256  else
257  {
258  return false;
259  }
260  }
261 
262 
271  function saveToDb($original_id = "")
272  {
273  global $ilDB;
274 
275  $complete = 0;
276  if ($this->isComplete())
277  {
278  $complete = 1;
279  }
280 
281  $params = $this->buildParams();
282  $estw_time = $this->getEstimatedWorkingTime();
283  $estw_time = sprintf("%02d:%02d:%02d", $estw_time['h'], $estw_time['m'], $estw_time['s']);
284 
285  if ($original_id)
286  {
287  $original_id = $ilDB->quote($original_id);
288  }
289  else
290  {
291  $original_id = "NULL";
292  }
293 
294  // cleanup RTE images which are not inserted into the question text
295  include_once("./Services/RTE/classes/class.ilRTE.php");
296  if ($this->id == -1)
297  {
298  // Neuen Datensatz schreiben
299  $now = getdate();
300  $question_type = $this->getQuestionTypeID();
301  $created = sprintf("%04d%02d%02d%02d%02d%02d", $now['year'], $now['mon'], $now['mday'], $now['hours'], $now['minutes'], $now['seconds']);
302  $query = sprintf("INSERT INTO qpl_questions (question_id, question_type_fi, obj_fi, title, comment, author, owner, question_text, points, working_time, complete, created, original_id, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, NULL)",
303  $ilDB->quote($question_type . ""),
304  $ilDB->quote($this->obj_id . ""),
305  $ilDB->quote($this->title . ""),
306  $ilDB->quote($this->comment . ""),
307  $ilDB->quote($this->author . ""),
308  $ilDB->quote($this->owner . ""),
309  $ilDB->quote(ilRTE::_replaceMediaObjectImageSrc($this->question, 0)),
310  $ilDB->quote($this->points . ""),
311  $ilDB->quote($estw_time . ""),
312  $ilDB->quote($complete . ""),
313  $ilDB->quote($created . ""),
315  );
316 
317  $result = $ilDB->query($query);
318  if (PEAR::isError($result))
319  {
320  global $ilias;
321  $ilias->raiseError($result->getMessage());
322  }
323  else
324  {
325  $this->id = $ilDB->getLastInsertId();
326  $query = sprintf("INSERT INTO qpl_question_javaapplet (question_fi, image_file, params) VALUES (%s, %s, %s)",
327  $ilDB->quote($this->id . ""),
328  $ilDB->quote($this->javaapplet_filename . ""),
329  $ilDB->quote($params . "")
330  );
331  $ilDB->query($query);
332 
333  // create page object of question
334  $this->createPageObject();
335 
336  if ($this->getTestId() > 0)
337  {
338  $this->insertIntoTest($this->getTestId());
339  }
340  }
341  }
342  else
343  {
344  // Vorhandenen Datensatz aktualisieren
345  $query = sprintf("UPDATE qpl_questions SET obj_fi = %s, title = %s, comment = %s, author = %s, question_text = %s, points = %s, working_time=%s, complete = %s WHERE question_id = %s",
346  $ilDB->quote($this->obj_id. ""),
347  $ilDB->quote($this->title . ""),
348  $ilDB->quote($this->comment . ""),
349  $ilDB->quote($this->author . ""),
350  $ilDB->quote(ilRTE::_replaceMediaObjectImageSrc($this->question, 0)),
351  $ilDB->quote($this->points . ""),
352  $ilDB->quote($estw_time . ""),
353  $ilDB->quote($complete . ""),
354  $ilDB->quote($this->id . "")
355  );
356  $result = $ilDB->query($query);
357  $query = sprintf("UPDATE qpl_question_javaapplet SET image_file = %s, params = %s WHERE question_fi = %s",
358  $ilDB->quote($this->javaapplet_filename . ""),
359  $ilDB->quote($params . ""),
360  $ilDB->quote($this->id . "")
361  );
362  $result = $ilDB->query($query);
363  }
365  }
366 
376  function loadFromDb($question_id)
377  {
378  global $ilDB;
379 
380  $query = sprintf("SELECT qpl_questions.*, qpl_question_javaapplet.* FROM qpl_questions, qpl_question_javaapplet WHERE question_id = %s AND qpl_questions.question_id = qpl_question_javaapplet.question_fi",
381  $ilDB->quote($question_id)
382  );
383  $result = $ilDB->query($query);
384 
385  if ($result->numRows() == 1)
386  {
387  $data = $result->fetchRow(MDB2_FETCHMODE_OBJECT);
388  $this->id = $question_id;
389  $this->title = $data->title;
390  $this->comment = $data->comment;
391  $this->obj_id = $data->obj_fi;
392  $this->author = $data->author;
393  $this->points = $data->points;
394  $this->owner = $data->owner;
395  $this->original_id = $data->original_id;
396  $this->javaapplet_filename = $data->image_file;
397  include_once("./Services/RTE/classes/class.ilRTE.php");
398  $this->question = ilRTE::_replaceMediaObjectImageSrc($data->question_text, 1);
399  $this->solution_hint = $data->solution_hint;
400  $this->splitParams($data->params);
401  $this->setEstimatedWorkingTime(substr($data->working_time, 0, 2), substr($data->working_time, 3, 2), substr($data->working_time, 6, 2));
402  }
403  parent::loadFromDb($question_id);
404  }
405 
413  function duplicate($for_test = true, $title = "", $author = "", $owner = "")
414  {
415  if ($this->id <= 0)
416  {
417  // The question has not been saved. It cannot be duplicated
418  return;
419  }
420  // duplicate the question in database
421  $this_id = $this->getId();
422  $clone = $this;
423  include_once ("./Modules/TestQuestionPool/classes/class.assQuestion.php");
425  $clone->id = -1;
426  if ($title)
427  {
428  $clone->setTitle($title);
429  }
430  if ($author)
431  {
432  $clone->setAuthor($author);
433  }
434  if ($owner)
435  {
436  $clone->setOwner($owner);
437  }
438  if ($for_test)
439  {
440  $clone->saveToDb($original_id);
441  }
442  else
443  {
444  $clone->saveToDb();
445  }
446 
447  // copy question page content
448  $clone->copyPageOfQuestion($this_id);
449  // copy XHTML media objects
450  $clone->copyXHTMLMediaObjectsOfQuestion($this_id);
451  // duplicate the generic feedback
452  $clone->duplicateFeedbackGeneric($this_id);
453 
454  // duplicate the image
455  $clone->duplicateApplet($this_id);
456  return $clone->id;
457  }
458 
466  function copyObject($target_questionpool, $title = "")
467  {
468  if ($this->id <= 0)
469  {
470  // The question has not been saved. It cannot be duplicated
471  return;
472  }
473  // duplicate the question in database
474  $clone = $this;
475  include_once ("./Modules/TestQuestionPool/classes/class.assQuestion.php");
477  $clone->id = -1;
478  $source_questionpool = $this->getObjId();
479  $clone->setObjId($target_questionpool);
480  if ($title)
481  {
482  $clone->setTitle($title);
483  }
484  $clone->saveToDb();
485 
486  // copy question page content
487  $clone->copyPageOfQuestion($original_id);
488  // copy XHTML media objects
489  $clone->copyXHTMLMediaObjectsOfQuestion($original_id);
490  // duplicate the generic feedback
491  $clone->duplicateFeedbackGeneric($original_id);
492 
493  // duplicate the image
494  $clone->copyApplet($original_id, $source_questionpool);
495  return $clone->id;
496  }
497 
498  function duplicateApplet($question_id)
499  {
500  $javapath = $this->getJavaPath();
501  $javapath_original = preg_replace("/([^\d])$this->id([^\d])/", "\${1}$question_id\${2}", $javapath);
502  if (!file_exists($javapath))
503  {
504  ilUtil::makeDirParents($javapath);
505  }
506  $filename = $this->getJavaAppletFilename();
507  if (!copy($javapath_original . $filename, $javapath . $filename)) {
508  print "java applet could not be duplicated!!!! ";
509  }
510  }
511 
512  function copyApplet($question_id, $source_questionpool)
513  {
514  $javapath = $this->getJavaPath();
515  $javapath_original = preg_replace("/([^\d])$this->id([^\d])/", "\${1}$question_id\${2}", $javapath);
516  $javapath_original = str_replace("/$this->obj_id/", "/$source_questionpool/", $javapath_original);
517  if (!file_exists($javapath))
518  {
519  ilUtil::makeDirParents($javapath);
520  }
521  $filename = $this->getJavaAppletFilename();
522  if (!copy($javapath_original . $filename, $javapath . $filename)) {
523  print "java applet could not be copied!!!! ";
524  }
525  }
526 
535  function getMaximumPoints()
536  {
537  return $this->points;
538  }
539 
548  function getJavaCode()
549  {
550  return $this->java_code;
551  }
552 
561  function getJavaCodebase()
562  {
563  return $this->java_codebase;
564  }
565 
574  function getJavaArchive()
575  {
576  return $this->java_archive;
577  }
578 
587  function setJavaCode($java_code = "")
588  {
589  $this->java_code = $java_code;
590  }
591 
601  {
602  $this->java_codebase = $java_codebase;
603  }
604 
614  {
615  $this->java_archive = $java_archive;
616  }
617 
626  function getJavaWidth()
627  {
628  return $this->java_width;
629  }
630 
639  function setJavaWidth($java_width = "")
640  {
641  $this->java_width = $java_width;
642  }
643 
652  function getJavaHeight()
653  {
654  return $this->java_height;
655  }
656 
666  {
667  $this->java_height = $java_height;
668  }
669 
681  function calculateReachedPoints($active_id, $pass = NULL)
682  {
683  global $ilDB;
684 
685  $found_values = array();
686  if (is_null($pass))
687  {
688  $pass = $this->getSolutionMaxPass($active_id);
689  }
690  $query = sprintf("SELECT * FROM tst_solutions WHERE active_fi = %s AND question_fi = %s AND pass = %s",
691  $ilDB->quote($active_id . ""),
692  $ilDB->quote($this->getId() . ""),
693  $ilDB->quote($pass . "")
694  );
695  $result = $ilDB->query($query);
696  $points = 0;
697  while ($data = $result->fetchRow(MDB2_FETCHMODE_OBJECT))
698  {
699  $points += $data->points;
700  }
701 
702  $points = parent::calculateReachedPoints($active_id, $pass = NULL, $points);
703  return $points;
704  }
705 
715  function getReachedInformation($active_id, $pass = NULL)
716  {
717  global $ilDB;
718 
719  $found_values = array();
720  if (is_null($pass))
721  {
722  $pass = $this->getSolutionMaxPass($active_id);
723  }
724  $query = sprintf("SELECT * FROM tst_solutions WHERE active_fi = %s AND question_fi = %s AND pass = %s",
725  $ilDB->quote($active_id . ""),
726  $ilDB->quote($this->getId() . ""),
727  $ilDB->quote($pass . "")
728  );
729  $result = $ilDB->query($query);
730  $counter = 1;
731  $user_result = array();
732  while ($data = $result->fetchRow(MDB2_FETCHMODE_OBJECT))
733  {
734  $true = 0;
735  if ($data->points > 0)
736  {
737  $true = 1;
738  }
739  $solution = array(
740  "order" => "$counter",
741  "points" => "$data->points",
742  "true" => "$true",
743  "value1" => "$data->value1",
744  "value2" => "$data->value2",
745  );
746  $counter++;
747  array_push($user_result, $solution);
748  }
749  return $user_result;
750  }
751 
762  function addParameter($name = "", $value = "")
763  {
764  $index = $this->getParameterIndex($name);
765  if ($index > -1)
766  {
767  $this->parameters[$index] = array("name" => $name, "value" => $value);
768  }
769  else
770  {
771  array_push($this->parameters, array("name" => $name, "value" => $value));
772  }
773  }
774 
786  function addParameterAtIndex($index = 0, $name = "", $value = "")
787  {
788  $this->parameters[$index] = array("name" => $name, "value" => $value);
789  }
790 
800  function removeParameter($name)
801  {
802  foreach ($this->parameters as $key => $value)
803  {
804  if (strcmp($name, $value["name"]) == 0)
805  {
806  array_splice($this->parameters, $key, 1);
807  return;
808  }
809  }
810  }
811 
822  function getParameter($index)
823  {
824  if (($index < 0) or ($index >= count($this->parameters)))
825  {
826  return undef;
827  }
828  return $this->parameters[$index];
829  }
830 
841  function getParameterIndex($name)
842  {
843  foreach ($this->parameters as $key => $value)
844  {
845  if (array_key_exists($name, $value))
846  {
847  return $key;
848  }
849  }
850  return -1;
851  }
852 
862  function getParameterCount()
863  {
864  return count($this->parameters);
865  }
866 
875  function flushParams()
876  {
877  $this->parameters = array();
878  }
879 
890  function saveWorkingData($active_id, $pass = NULL)
891  {
892  parent::saveWorkingData($active_id, $pass);
893  return true;
894  }
895 
906  {
908  }
909 
919  function setJavaAppletFilename($javaapplet_filename, $javaapplet_tempfilename = "")
920  {
921  if (!empty($javaapplet_filename))
922  {
923  $this->javaapplet_filename = $javaapplet_filename;
924  }
925  if (!empty($javaapplet_tempfilename))
926  {
927  $javapath = $this->getJavaPath();
928  if (!file_exists($javapath))
929  {
930  ilUtil::makeDirParents($javapath);
931  }
932 
933  //if (!move_uploaded_file($javaapplet_tempfilename, $javapath . $javaapplet_filename))
934  if (!ilUtil::moveUploadedFile($javaapplet_tempfilename, $javaapplet_filename, $javapath.$javaapplet_filename))
935  {
936  print "java applet not uploaded!!!! ";
937  }
938  else
939  {
940  $this->setJavaCodebase();
941  $this->setJavaArchive();
942  }
943  }
944  }
945 
947  {
948  unlink($this->getJavaPath() . $this->getJavaAppletFilename());
949  $this->javaapplet_filename = "";
950  }
951 
960  function getQuestionType()
961  {
962  return "assJavaApplet";
963  }
964 
974  {
975  return "qpl_question_javaapplet";
976  }
977 
983  {
985  }
986 
999  public function setExportDetailsXLS(&$worksheet, $startrow, $active_id, $pass, &$format_title, &$format_bold)
1000  {
1001  include_once ("./classes/class.ilExcelUtils.php");
1002  $solutions = $this->getSolutionValues($active_id, $pass);
1003  $worksheet->writeString($startrow, 0, ilExcelUtils::_convert_text($this->lng->txt($this->getQuestionType())), $format_title);
1004  $worksheet->writeString($startrow, 1, ilExcelUtils::_convert_text($this->getTitle()), $format_title);
1005  $i = 1;
1006  foreach ($solutions as $solution)
1007  {
1008  $worksheet->write($startrow + $i, 1, ilExcelUtils::_convert_text($this->lng->txt("result") . " $i"));
1009  if (strlen($solution["value1"])) $worksheet->write($startrow + $i, 1, ilExcelUtils::_convert_text($solution["value1"]));
1010  if (strlen($solution["value2"])) $worksheet->write($startrow + $i, 2, ilExcelUtils::_convert_text($solution["value2"]));
1011  $i++;
1012  }
1013  return $startrow + $i + 1;
1014  }
1015 }
1016 
1017 ?>