ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.SurveyQuestion.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 
24 include_once "./Modules/Survey/classes/inc.SurveyConstants.php";
25 
37 {
45  var $id;
46 
54  var $title;
71  var $owner;
72 
81  var $author;
82 
91 
100 
108  var $obj_id;
109 
118 
127 
135  var $ilias;
136 
144  var $tpl;
145 
153  var $lng;
154 
163 
165 
177  function SurveyQuestion(
178  $title = "",
179  $description = "",
180  $author = "",
181  $questiontext = "",
182  $owner = -1
183  )
184 
185  {
186  global $ilias;
187  global $lng;
188  global $tpl;
189 
190  $this->ilias =& $ilias;
191  $this->lng =& $lng;
192  $this->tpl =& $tpl;
193 
194  $this->title = $title;
195  $this->description = $description;
196  $this->questiontext = $questiontext;
197  $this->author = $author;
198  if (!$this->author) {
199  $this->author = $this->ilias->account->fullname;
200  }
201  $this->owner = $owner;
202  if ($this->owner == -1) {
203  $this->owner = $this->ilias->account->id;
204  }
205  $this->id = -1;
206  $this->survey_id = -1;
207  $this->obligatory = 1;
208  $this->orientation = 0;
209  $this->materials = array();
210  $this->material = array();
211  register_shutdown_function(array(&$this, '_SurveyQuestion'));
212  }
213 
214  function _SurveyQuestion()
215  {
216  }
217 
218 
227  function isComplete()
228  {
229  return false;
230  }
231 
242  function questionTitleExists($title, $questionpool_object = "")
243  {
244  global $ilDB;
245 
246  $refwhere = "";
247  if (strcmp($questionpool_object, "") != 0)
248  {
249  $refwhere = sprintf(" AND obj_fi = %s",
250  $ilDB->quote($questionpool_object)
251  );
252  }
253  $query = sprintf("SELECT question_id FROM survey_question WHERE title = %s$refwhere",
254  $ilDB->quote($title)
255  );
256  $result = $ilDB->query($query);
257  if ($result->numRows() == 1)
258  {
259  return TRUE;
260  }
261  return FALSE;
262  }
263 
273  function setTitle($title = "")
274  {
275  $this->title = $title;
276  }
277 
287  function setObligatory($obligatory = 1)
288  {
289  if ($obligatory)
290  {
291  $this->obligatory = 1;
292  }
293  else
294  {
295  $this->obligatory = 0;
296  }
297  }
298 
309  {
310  if (strlen($orientation) == 0)
311  {
312  $this->orientation = 0;
313  }
314  else
315  {
316  $this->orientation = $orientation;
317  }
318  }
319 
329  function setId($id = -1)
330  {
331  $this->id = $id;
332  }
333 
343  function setSurveyId($id = -1)
344  {
345  $this->survey_id = $id;
346  }
347 
357  function setDescription($description = "")
358  {
359  $this->description = $description;
360  }
361 
362 
373  function addMaterials($materials_file, $materials_name="")
374  {
375  if(empty($materials_name))
376  {
377  $materials_name = $materials_file;
378  }
379  if ((!empty($materials_name))&&(!$this->keyInArray($materials_name, $this->materials)))
380  {
381  $this->materials[$materials_name] = $materials_file;
382  }
383 
384  }
385 
396  function keyInArray($searchkey, $array)
397  {
398  if ($searchKey)
399  {
400  foreach ($array as $key => $value)
401  {
402  if (strcmp($key, $searchkey)==0)
403  {
404  return true;
405  }
406  }
407  }
408  return false;
409  }
410 
420  function setMaterialsfile($materials_filename, $materials_tempfilename="", $materials_name="")
421  {
422  if (!empty($materials_filename))
423  {
424  include_once "./Services/Utilities/classes/class.ilUtil.php";
425  $materialspath = $this->getMaterialsPath();
426  if (!file_exists($materialspath))
427  {
428  ilUtil::makeDirParents($materialspath);
429  }
430  //if (!move_uploaded_file($materials_tempfilename, $materialspath . $materials_filename))
431  if (ilUtil::moveUploadedFile($materials_tempfilename, $materials_filename,
432  $materialspath.$materials_filename))
433  {
434  print "image not uploaded!!!! ";
435  }
436  else
437  {
438  $this->addMaterials($materials_filename, $materials_name);
439  }
440  }
441  }
442 
452  function deleteMaterial($materials_name = "")
453  {
454  foreach ($this->materials as $key => $value)
455  {
456  if (strcmp($key, $materials_name)==0)
457  {
458  if (file_exists($this->getMaterialsPath().$value))
459  {
460  unlink($this->getMaterialsPath().$value);
461  }
462  unset($this->materials[$key]);
463  }
464  }
465  }
466 
475  function flushMaterials()
476  {
477  $this->materials = array();
478  }
479 
489  function setAuthor($author = "")
490  {
491  if (!$author)
492  {
493  $author = $this->ilias->account->fullname;
494  }
495  $this->author = $author;
496  }
497 
508  {
509  $this->questiontext = $questiontext;
510  }
511 
521  function setOwner($owner = "")
522  {
523  $this->owner = $owner;
524  }
525 
535  function getTitle()
536  {
537  return $this->title;
538  }
539 
549  function getId()
550  {
551  return $this->id;
552  }
553 
564  function getObligatory($survey_id = "")
565  {
566  if ($survey_id > 0)
567  {
568  global $ilDB;
569 
570  $query = sprintf("SELECT * FROM survey_question_obligatory WHERE survey_fi = %s AND question_fi = %s",
571  $ilDB->quote($survey_id . ""),
572  $ilDB->quote($this->getId() . "")
573  );
574  $result = $ilDB->query($query);
575  if ($result->numRows())
576  {
577  $row = $result->fetchRow(MDB2_FETCHMODE_ASSOC);
578  return $row["obligatory"];
579  }
580  else
581  {
582  return $this->obligatory;
583  }
584  }
585  else
586  {
587  return $this->obligatory;
588  }
589  }
590 
600  function getSurveyId()
601  {
602  return $this->survey_id;
603  }
604 
614  function getOrientation()
615  {
616  switch ($this->orientation)
617  {
618  case 0:
619  case 1:
620  case 2:
621  break;
622  default:
623  $this->orientation = 0;
624  break;
625  }
626  return $this->orientation;
627  }
628 
629 
639  function getDescription()
640  {
641  return $this->description;
642  }
643 
653  function getAuthor()
654  {
655  return $this->author;
656  }
657 
667  function getOwner()
668  {
669  return $this->owner;
670  }
671 
681  function getQuestiontext()
682  {
683  return $this->questiontext;
684  }
685 
695  function getObjId() {
696  return $this->obj_id;
697  }
698 
708  function setObjId($obj_id = 0) {
709  $this->obj_id = $obj_id;
710  }
711 
719  function duplicate($for_survey = true, $title = "", $author = "", $owner = "")
720  {
721  if ($this->getId() <= 0)
722  {
723  // The question has not been saved. It cannot be duplicated
724  return;
725  }
726  // duplicate the question in database
727  $clone = $this;
728  $original_id = $this->getId();
729  $clone->setId(-1);
730  if ($title)
731  {
732  $clone->setTitle($title);
733  }
734  if ($author)
735  {
736  $clone->setAuthor($author);
737  }
738  if ($owner)
739  {
740  $clone->setOwner($owner);
741  }
742  if ($for_survey)
743  {
744  $clone->saveToDb($original_id);
745  }
746  else
747  {
748  $clone->saveToDb();
749  }
750  // duplicate the materials
751  $clone->duplicateMaterials($original_id);
752  // copy XHTML media objects
753  $clone->copyXHTMLMediaObjectsOfQuestion($original_id);
754  return $clone->getId();
755  }
756 
764  function copyObject($target_questionpool, $title = "")
765  {
766  if ($this->getId() <= 0)
767  {
768  // The question has not been saved. It cannot be copied
769  return;
770  }
771  $clone = $this;
772  $original_id = SurveyQuestion::_getOriginalId($this->getId());
773  $clone->setId(-1);
774  $source_questionpool = $this->getObjId();
775  $clone->setObjId($target_questionpool);
776  if ($title)
777  {
778  $clone->setTitle($title);
779  }
780 
781  $clone->saveToDb();
782 
783  // duplicate the materials
784  $clone->duplicateMaterials($original_id);
785  // copy XHTML media objects
786  $clone->copyXHTMLMediaObjectsOfQuestion($original_id);
787  return $clone->getId();
788  }
789 
799  {
800  include_once("./Services/MediaObjects/classes/class.ilObjMediaObject.php");
801  $mobs = ilObjMediaObject::_getMobsOfObject("spl:html", $a_q_id);
802  foreach ($mobs as $mob)
803  {
804  ilObjMediaObject::_saveUsage($mob, "spl:html", $this->getId());
805  }
806  }
807 
816  function duplicateMaterials($question_id)
817  {
818  foreach ($this->materials as $filename)
819  {
820  $materialspath = $this->getMaterialsPath();
821  $materialspath_original = preg_replace("/([^\d])$this->id([^\d])/", "\${1}$question_id\${2}", $materialspath);
822  if (!file_exists($materialspath))
823  {
824  include_once "./Services/Utilities/classes/class.ilUtil.php";
825  ilUtil::makeDirParents($materialspath);
826  }
827  if (!copy($materialspath_original . $filename, $materialspath . $filename))
828  {
829  print "material could not be duplicated!!!! ";
830  }
831  }
832  }
833 
834 
843  function loadFromDb($question_id)
844  {
845  $query = sprintf("SELECT * FROM survey_material WHERE question_fi = %s",
846  $this->ilias->db->quote($this->getId() . "")
847  );
848  $result = $this->ilias->db->query($query);
849  $this->material = array();
850  if ($result->numRows())
851  {
852  while ($row = $result->fetchRow(MDB2_FETCHMODE_ASSOC))
853  {
854  $this->material = array(
855  "internal_link" => $row["internal_link"],
856  "import_id" => $row["import_id"],
857  "title" => $row["material_title"]
858  );
859  }
860  }
861  }
862 
871  function _isComplete($question_id)
872  {
873  global $ilDB;
874 
875  $query = sprintf("SELECT complete FROM survey_question WHERE question_id = %s",
876  $ilDB->quote($question_id . "")
877  );
878  $result = $ilDB->query($query);
879  if ($result->numRows())
880  {
881  $row = $result->fetchRow(MDB2_FETCHMODE_ASSOC);
882  if ($row["complete"] == 1)
883  {
884  return TRUE;
885  }
886  }
887  return FALSE;
888  }
889 
897  function saveCompletionStatus($original_id = "")
898  {
899  $question_id = $this->getId();
900  if (strlen($original_id))
901  {
902  $question_id = $original_id;
903  }
904 
905  $complete = 0;
906  if ($this->isComplete())
907  {
908  $complete = 1;
909  }
910  if ($this->id > 0)
911  {
912  // update existing dataset
913  $query = sprintf("UPDATE survey_question SET complete = %s WHERE question_id = %s",
914  $this->ilias->db->quote("$complete"),
915  $this->ilias->db->quote($question_id . "")
916  );
917  $result = $this->ilias->db->query($query);
918  }
919  }
920 
929  function saveToDb($original_id = "")
930  {
931  include_once "./Services/COPage/classes/class.ilInternalLink.php";
932  $query = sprintf("DELETE FROM survey_material WHERE question_fi = %s",
933  $this->ilias->db->quote($this->getId() . "")
934  );
935  $result = $this->ilias->db->query($query);
937  if (count($this->material))
938  {
939  $query = sprintf("INSERT INTO survey_material (material_id, question_fi, internal_link, import_id, material_title, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, NULL)",
940  $this->ilias->db->quote($this->getId() . ""),
941  $this->ilias->db->quote($this->material["internal_link"] . ""),
942  $this->ilias->db->quote($this->material["import_id"] . ""),
943  $this->ilias->db->quote($this->material["title"] . "")
944  );
945  $this->ilias->db->query($query);
946  if (preg_match("/il_(\d*?)_(\w+)_(\d+)/", $solution["internal_link"], $matches))
947  {
948  ilInternalLink::_saveLink("sqst", $this->getId(), $matches[2], $matches[3], $matches[1]);
949  }
950  }
951  }
952 
953 
962  function saveWorkingData($limit_to = LIMIT_NO_LIMIT)
963  {
964  }
965 
974  function getImagePath()
975  {
976  return CLIENT_WEB_DIR . "/survey/$this->obj_id/$this->id/images/";
977  }
978 
987  function getMaterialsPath()
988  {
989  return CLIENT_WEB_DIR . "/survey/$this->obj_id/$this->id/materials/";
990  }
991 
1000  function getImagePathWeb()
1001  {
1002  include_once "./Services/Utilities/classes/class.ilUtil.php";
1003  $webdir = ilUtil::removeTrailingPathSeparators(CLIENT_WEB_DIR) . "/survey/$this->obj_id/$this->id/images/";
1005  }
1006 
1016  {
1017  include_once "./Services/Utilities/classes/class.ilUtil.php";
1018  $webdir = ilUtil::removeTrailingPathSeparators(CLIENT_WEB_DIR) . "/survey/$this->obj_id/$this->id/materials/";
1020  }
1021 
1031  {
1032  if ($this->id > 0)
1033  {
1034  $query = sprintf("DELETE FROM survey_question_material WHERE question_fi = %s",
1035  $this->ilias->db->quote($this->id)
1036  );
1037  $result = $this->ilias->db->query($query);
1038  if (!empty($this->materials)) {
1039  foreach ($this->materials as $key => $value)
1040  {
1041  $query = sprintf("INSERT INTO survey_question_material (question_fi, materials, materials_file) VALUES (%s, %s, %s)",
1042  $this->ilias->db->quote($this->id),
1043  $this->ilias->db->quote($key),
1044  $this->ilias->db->quote($value)
1045  );
1046  $result = $this->ilias->db->query($query);
1047  }
1048  }
1049  }
1050  }
1051 
1060  function loadMaterialFromDb($question_id)
1061  {
1062  $query = sprintf("SELECT * FROM survey_question_material WHERE question_fi = %s",
1063  $this->ilias->db->quote($question_id)
1064  );
1065  $result = $this->ilias->db->query($query);
1066  if ($result->numRows() > 0)
1067  {
1068  $this->materials = array();
1069  while ($data = $result->fetchRow(MDB2_FETCHMODE_OBJECT))
1070  {
1071  $this->addMaterials($data->materials_file, $data->materials);
1072  }
1073  }
1074  }
1075 
1086  function saveCategoryToDb($categorytext, $neutral = 0)
1087  {
1088  global $ilUser;
1089 
1090  $query = sprintf("SELECT title, category_id FROM survey_category WHERE title = %s AND neutral = %s AND owner_fi = %s",
1091  $this->ilias->db->quote($categorytext . ""),
1092  $this->ilias->db->quote($neutral . ""),
1093  $this->ilias->db->quote($ilUser->getId() . "")
1094  );
1095  $result = $this->ilias->db->query($query);
1096  $insert = FALSE;
1097  $returnvalue = "";
1098  if ($result->numRows())
1099  {
1100  $insert = TRUE;
1101  while ($row = $result->fetchRow(MDB2_FETCHMODE_OBJECT))
1102  {
1103  if (strcmp($row->title, $categorytext) == 0)
1104  {
1105  $returnvalue = $row->category_id;
1106  $insert = FALSE;
1107  }
1108  }
1109  }
1110  else
1111  {
1112  $insert = TRUE;
1113  }
1114  if ($insert)
1115  {
1116  $query = sprintf("INSERT INTO survey_category (category_id, title, neutral, owner_fi, TIMESTAMP) VALUES (NULL, %s, %s, %s, NULL)",
1117  $this->ilias->db->quote($categorytext . ""),
1118  $this->ilias->db->quote($neutral . ""),
1119  $this->ilias->db->quote($ilUser->getId() . "")
1120  );
1121  $result = $this->ilias->db->query($query);
1122  $returnvalue = $this->ilias->db->getLastInsertId();
1123  }
1124  return $returnvalue;
1125  }
1126 
1135  function deleteAdditionalTableData($question_id)
1136  {
1137  global $ilDB;
1138  $additional_table_name = $this->getAdditionalTableName();
1139  $query = sprintf("DELETE FROM $additional_table_name WHERE question_fi = %s",
1140  $ilDB->quote($question_id . "")
1141  );
1142  $result = $ilDB->query($query);
1143  }
1144 
1153  function delete($question_id)
1154  {
1155  if ($question_id < 1)
1156  return;
1157 
1158  $query = sprintf("SELECT obj_fi FROM survey_question WHERE question_id = %s",
1159  $this->ilias->db->quote($question_id)
1160  );
1161  $result = $this->ilias->db->query($query);
1162  if ($result->numRows() == 1)
1163  {
1164  $row = $result->fetchRow(MDB2_FETCHMODE_ASSOC);
1165  $obj_id = $row["obj_fi"];
1166  }
1167  else
1168  {
1169  return;
1170  }
1171 
1172  $query = sprintf("DELETE FROM survey_answer WHERE question_fi = %s",
1173  $this->ilias->db->quote($question_id)
1174  );
1175  $result = $this->ilias->db->query($query);
1176 
1177  $query = sprintf("SELECT constraint_id FROM survey_constraint WHERE question_fi = %s",
1178  $this->ilias->db->quote($question_id)
1179  );
1180  $result = $this->ilias->db->query($query);
1181  while ($row = $result->fetchRow(MDB2_FETCHMODE_OBJECT))
1182  {
1183  $query = sprintf("DELETE FROM survey_question_constraint WHERE constraint_fi = %s",
1184  $this->ilias->db->quote($row->constraint_id)
1185  );
1186  $delresult = $this->ilias->db->query($query);
1187  }
1188 
1189  $query = sprintf("DELETE FROM survey_constraint WHERE question_fi = %s",
1190  $this->ilias->db->quote($question_id)
1191  );
1192  $result = $this->ilias->db->query($query);
1193 
1194  $query = sprintf("SELECT constraint_fi FROM survey_question_constraint WHERE question_fi = %s",
1195  $this->ilias->db->quote($question_id)
1196  );
1197  $result = $this->ilias->db->query($query);
1198  while ($row = $result->fetchRow(MDB2_FETCHMODE_OBJECT))
1199  {
1200  $query = sprintf("DELETE FROM survey_constraint WHERE constraint_id = %s",
1201  $this->ilias->db->quote($row->constraint_fi)
1202  );
1203  $delresult = $this->ilias->db->query($query);
1204  }
1205  $query = sprintf("DELETE FROM survey_question_constraint WHERE question_fi = %s",
1206  $this->ilias->db->quote($question_id)
1207  );
1208  $result = $this->ilias->db->query($query);
1209 
1210  $query = sprintf("DELETE FROM survey_question_material WHERE question_fi = %s",
1211  $this->ilias->db->quote($question_id)
1212  );
1213  $result = $this->ilias->db->query($query);
1214 
1215  $query = sprintf("DELETE FROM survey_questionblock_question WHERE question_fi = %s",
1216  $this->ilias->db->quote($question_id)
1217  );
1218  $result = $this->ilias->db->query($query);
1219 
1220  $query = sprintf("DELETE FROM survey_question_obligatory WHERE question_fi = %s",
1221  $this->ilias->db->quote($question_id)
1222  );
1223  $result = $this->ilias->db->query($query);
1224 
1225  $query = sprintf("DELETE FROM survey_survey_question WHERE question_fi = %s",
1226  $this->ilias->db->quote($question_id)
1227  );
1228  $result = $this->ilias->db->query($query);
1229 
1230  $query = sprintf("DELETE FROM survey_variable WHERE question_fi = %s",
1231  $this->ilias->db->quote($question_id)
1232  );
1233  $result = $this->ilias->db->query($query);
1234 
1235  $query = sprintf("DELETE FROM survey_question WHERE question_id = %s",
1236  $this->ilias->db->quote($question_id)
1237  );
1238  $result = $this->ilias->db->query($query);
1239 
1240  $this->deleteAdditionalTableData($question_id);
1241 
1242  $query = sprintf("DELETE FROM survey_material WHERE question_fi = %s",
1243  $this->ilias->db->quote($question_id)
1244  );
1245  $result = $this->ilias->db->query($query);
1246  include_once "./Services/COPage/classes/class.ilInternalLink.php";
1247  ilInternalLink::_deleteAllLinksOfSource("sqst", $question_id);
1248 
1249  $directory = CLIENT_WEB_DIR . "/survey/" . $obj_id . "/$question_id";
1250  if (preg_match("/\d+/", $obj_id) and preg_match("/\d+/", $question_id) and is_dir($directory))
1251  {
1252  include_once "./Services/Utilities/classes/class.ilUtil.php";
1253  ilUtil::delDir($directory);
1254  }
1255 
1256  include_once("./Services/MediaObjects/classes/class.ilObjMediaObject.php");
1257  $mobs = ilObjMediaObject::_getMobsOfObject("spl:html", $question_id);
1258  // remaining usages are not in text anymore -> delete them
1259  // and media objects (note: delete method of ilObjMediaObject
1260  // checks whether object is used in another context; if yes,
1261  // the object is not deleted!)
1262  foreach($mobs as $mob)
1263  {
1264  ilObjMediaObject::_removeUsage($mob, "spl:html", $question_id);
1265  $mob_obj =& new ilObjMediaObject($mob);
1266  $mob_obj->delete();
1267  }
1268  }
1269 
1279  function _getQuestionType($question_id)
1280  {
1281  global $ilDB;
1282 
1283  if ($question_id < 1)
1284  return "";
1285 
1286  $query = sprintf("SELECT type_tag FROM survey_question, survey_questiontype WHERE survey_question.question_id = %s AND survey_question.questiontype_fi = survey_questiontype.questiontype_id",
1287  $ilDB->quote($question_id)
1288  );
1289  $result = $ilDB->query($query);
1290  if ($result->numRows() == 1)
1291  {
1292  $data = $result->fetchRow(MDB2_FETCHMODE_OBJECT);
1293  return $data->type_tag;
1294  }
1295  else
1296  {
1297  return "";
1298  }
1299  }
1300 
1310  function _getTitle($question_id)
1311  {
1312  global $ilDB;
1313 
1314  if ($question_id < 1) return "";
1315 
1316  $query = sprintf("SELECT title FROM survey_question WHERE survey_question.question_id = %s",
1317  $ilDB->quote($question_id)
1318  );
1319  $result = $ilDB->query($query);
1320  if ($result->numRows() == 1)
1321  {
1322  $data = $result->fetchRow(MDB2_FETCHMODE_ASSOC);
1323  return $data["title"];
1324  }
1325  else
1326  {
1327  return "";
1328  }
1329  }
1330 
1340  function _getOriginalId($question_id)
1341  {
1342  global $ilDB;
1343  $query = sprintf("SELECT * FROM survey_question WHERE question_id = %s",
1344  $ilDB->quote($question_id . "")
1345  );
1346  $result = $ilDB->query($query);
1347  if ($result->numRows() > 0)
1348  {
1349  $row = $result->fetchRow(MDB2_FETCHMODE_ASSOC);
1350  if ($row["original_id"] > 0)
1351  {
1352  return $row["original_id"];
1353  }
1354  else
1355  {
1356  return $row["question_id"];
1357  }
1358  }
1359  else
1360  {
1361  return "";
1362  }
1363  }
1364 
1366  {
1367  global $ilDB;
1368 
1369  $query = sprintf("SELECT ref_id FROM object_reference WHERE obj_id=%s",
1370  $ilDB->quote($obj_id)
1371 
1372  );
1373  $result = $ilDB->query($query);
1374  if ($result->numRows())
1375  {
1376  $row = $result->fetchRow(MDB2_FETCHMODE_ASSOC);
1377  return $row["ref_id"];
1378  }
1379  return 0;
1380  }
1381 
1382  function syncWithOriginal()
1383  {
1384  include_once "./Services/COPage/classes/class.ilInternalLink.php";
1385  $query = sprintf("DELETE FROM survey_material WHERE question_fi = %s",
1386  $this->ilias->db->quote($this->original_id . "")
1387  );
1388  $result = $this->ilias->db->query($query);
1389  ilInternalLink::_deleteAllLinksOfSource("sqst", $this->original_id);
1390  if (strlen($this->material["internal_link"]))
1391  {
1392  $query = sprintf("INSERT INTO survey_material (material_id, question_fi, internal_link, import_id, material_title, TIMESTAMP) VALUES (NULL, %s, %s, %s, %s, NULL)",
1393  $this->ilias->db->quote($this->original_id . ""),
1394  $this->ilias->db->quote($this->material["internal_link"] . ""),
1395  $this->ilias->db->quote($this->material["import_id"] . ""),
1396  $this->ilias->db->quote($this->material["title"] . "")
1397  );
1398  $this->ilias->db->query($query);
1399  if (preg_match("/il_(\d*?)_(\w+)_(\d+)/", $this->material["internal_link"], $matches))
1400  {
1401  ilInternalLink::_saveLink("sqst", $this->original_id, $matches[2], $matches[3], $matches[1]);
1402  }
1403  }
1404  }
1405 
1414  function getPhrase($phrase_id)
1415  {
1416  $query = sprintf("SELECT title FROM survey_phrase WHERE phrase_id = %s",
1417  $this->ilias->db->quote($phrase_id)
1418  );
1419  $result = $this->ilias->db->query($query);
1420  if ($row = $result->fetchRow(MDB2_FETCHMODE_ASSOC))
1421  {
1422  return $row["title"];
1423  }
1424  return "";
1425  }
1426 
1437  {
1438  global $ilUser;
1439 
1440  $query = sprintf("SELECT phrase_id FROM survey_phrase WHERE title = %s AND owner_fi = %s",
1441  $this->ilias->db->quote($title),
1442  $this->ilias->db->quote($ilUser->id)
1443  );
1444  $result = $this->ilias->db->query($query);
1445  if ($result->numRows() == 0)
1446  {
1447  return false;
1448  }
1449  else
1450  {
1451  return true;
1452  }
1453  }
1454 
1464  function _questionExists($question_id)
1465  {
1466  global $ilDB;
1467 
1468  if ($question_id < 1)
1469  {
1470  return false;
1471  }
1472 
1473  $query = sprintf("SELECT question_id FROM survey_question WHERE question_id = %s",
1474  $ilDB->quote($question_id)
1475  );
1476  $result = $ilDB->query($query);
1477  if ($result->numRows() == 1)
1478  {
1479  return true;
1480  }
1481  else
1482  {
1483  return false;
1484  }
1485  }
1486 
1496  function setMaterial($material_id = "", $is_import = false, $material_title = "")
1497  {
1498  if (strcmp($material_id, "") != 0)
1499  {
1500  $import_id = "";
1501  if ($is_import)
1502  {
1503  $import_id = $material_id;
1504  $material_id = $this->_resolveInternalLink($import_id);
1505  }
1506  if (strcmp($material_title, "") == 0)
1507  {
1508  if (preg_match("/il__(\w+)_(\d+)/", $material_id, $matches))
1509  {
1510  $type = $matches[1];
1511  $target_id = $matches[2];
1512  $material_title = $this->lng->txt("obj_$type") . ": ";
1513  switch ($type)
1514  {
1515  case "lm":
1516  include_once("./Modules/LearningModule/classes/class.ilObjContentObjectGUI.php");
1517  $cont_obj_gui =& new ilObjContentObjectGUI("", $target_id, true);
1518  $cont_obj = $cont_obj_gui->object;
1519  $material_title .= $cont_obj->getTitle();
1520  break;
1521  case "pg":
1522  include_once("./Modules/LearningModule/classes/class.ilLMPageObject.php");
1523  include_once("./Modules/LearningModule/classes/class.ilLMObject.php");
1525  include_once("./Modules/LearningModule/classes/class.ilObjContentObjectGUI.php");
1526  $cont_obj_gui =& new ilObjContentObjectGUI("", $lm_id, FALSE);
1527  $cont_obj = $cont_obj_gui->object;
1528  $pg_obj =& new ilLMPageObject($cont_obj, $target_id);
1529  $material_title .= $pg_obj->getTitle();
1530  break;
1531  case "st":
1532  include_once("./Modules/LearningModule/classes/class.ilStructureObject.php");
1533  include_once("./Modules/LearningModule/classes/class.ilLMObject.php");
1535  include_once("./Modules/LearningModule/classes/class.ilObjContentObjectGUI.php");
1536  $cont_obj_gui =& new ilObjContentObjectGUI("", $lm_id, FALSE);
1537  $cont_obj = $cont_obj_gui->object;
1538  $st_obj =& new ilStructureObject($cont_obj, $target_id);
1539  $material_title .= $st_obj->getTitle();
1540  break;
1541  case "git":
1542  include_once "./Modules/Glossary/classes/class.ilGlossaryTerm.php";
1543  $material_title = $this->lng->txt("glossary_term") . ": " . ilGlossaryTerm::_lookGlossaryTerm($target_id);
1544  break;
1545  case "mob":
1546  break;
1547  }
1548  }
1549  }
1550  $this->material = array(
1551  "internal_link" => $material_id,
1552  "import_id" => $import_id,
1553  "title" => $material_title
1554  );
1555  }
1556  }
1557 
1558  function _resolveInternalLink($internal_link)
1559  {
1560  if (preg_match("/il_(\d+)_(\w+)_(\d+)/", $internal_link, $matches))
1561  {
1562  include_once "./Services/COPage/classes/class.ilInternalLink.php";
1563  include_once "./Modules/LearningModule/classes/class.ilLMObject.php";
1564  include_once "./Modules/Glossary/classes/class.ilGlossaryTerm.php";
1565  switch ($matches[2])
1566  {
1567  case "lm":
1568  $resolved_link = ilLMObject::_getIdForImportId($internal_link);
1569  break;
1570  case "pg":
1571  $resolved_link = ilInternalLink::_getIdForImportId("PageObject", $internal_link);
1572  break;
1573  case "st":
1574  $resolved_link = ilInternalLink::_getIdForImportId("StructureObject", $internal_link);
1575  break;
1576  case "git":
1577  $resolved_link = ilInternalLink::_getIdForImportId("GlossaryItem", $internal_link);
1578  break;
1579  case "mob":
1580  $resolved_link = ilInternalLink::_getIdForImportId("MediaObject", $internal_link);
1581  break;
1582  }
1583  if (strcmp($resolved_link, "") == 0)
1584  {
1585  $resolved_link = $internal_link;
1586  }
1587  }
1588  else
1589  {
1590  $resolved_link = $internal_link;
1591  }
1592  return $resolved_link;
1593  }
1594 
1595  function _resolveIntLinks($question_id)
1596  {
1597  global $ilDB;
1598  $resolvedlinks = 0;
1599  $query = sprintf("SELECT * FROM survey_material WHERE question_fi = %s",
1600  $ilDB->quote($question_id . "")
1601  );
1602  $result = $ilDB->query($query);
1603  if ($result->numRows())
1604  {
1605  while ($row = $result->fetchRow(MDB2_FETCHMODE_ASSOC))
1606  {
1607  $internal_link = $row["internal_link"];
1608  include_once "./Modules/SurveyQuestionPool/classes/class.SurveyQuestion.php";
1609  $resolved_link = SurveyQuestion::_resolveInternalLink($internal_link);
1610  if (strcmp($internal_link, $resolved_link) != 0)
1611  {
1612  // internal link was resolved successfully
1613  $queryupdate = sprintf("UPDATE survey_material SET internal_link = %s WHERE material_id = %s",
1614  $ilDB->quote($resolved_link),
1615  $ilDB->quote($row["material_id"] . "")
1616  );
1617  $updateresult = $ilDB->query($queryupdate);
1618  $resolvedlinks++;
1619  }
1620  }
1621  }
1622  if ($resolvedlinks)
1623  {
1624  // there are resolved links -> reenter theses links to the database
1625 
1626  // delete all internal links from the database
1627  include_once "./Services/COPage/classes/class.ilInternalLink.php";
1628  ilInternalLink::_deleteAllLinksOfSource("sqst", $question_id);
1629 
1630  $query = sprintf("SELECT * FROM survey_material WHERE question_fi = %s",
1631  $ilDB->quote($question_id . "")
1632  );
1633  $result = $ilDB->query($query);
1634  if ($result->numRows())
1635  {
1636  while ($row = $result->fetchRow(MDB2_FETCHMODE_ASSOC))
1637  {
1638  if (preg_match("/il_(\d*?)_(\w+)_(\d+)/", $row["internal_link"], $matches))
1639  {
1640  ilInternalLink::_saveLink("sqst", $question_id, $matches[2], $matches[3], $matches[1]);
1641  }
1642  }
1643  }
1644  }
1645  }
1646 
1647  function _getInternalLinkHref($target = "")
1648  {
1649  global $ilDB;
1650  $linktypes = array(
1651  "lm" => "LearningModule",
1652  "pg" => "PageObject",
1653  "st" => "StructureObject",
1654  "git" => "GlossaryItem",
1655  "mob" => "MediaObject"
1656  );
1657  $href = "";
1658  if (preg_match("/il__(\w+)_(\d+)/", $target, $matches))
1659  {
1660  $type = $matches[1];
1661  $target_id = $matches[2];
1662  include_once "./Services/Utilities/classes/class.ilUtil.php";
1663  switch($linktypes[$matches[1]])
1664  {
1665  case "LearningModule":
1666  $href = ilUtil::removeTrailingPathSeparators(ILIAS_HTTP_PATH) ."/goto.php?target=" . $type . "_" . $target_id;
1667  break;
1668  case "PageObject":
1669  case "StructureObject":
1670  $href = ilUtil::removeTrailingPathSeparators(ILIAS_HTTP_PATH) ."/goto.php?target=" . $type . "_" . $target_id;
1671  break;
1672  case "GlossaryItem":
1673  $href = ilUtil::removeTrailingPathSeparators(ILIAS_HTTP_PATH) ."/goto.php?target=" . $type . "_" . $target_id;
1674  break;
1675  case "MediaObject":
1676  $href = ilUtil::removeTrailingPathSeparators(ILIAS_HTTP_PATH) . "/ilias.php?baseClass=ilLMPresentationGUI&obj_type=" . $linktypes[$type] . "&cmd=media&ref_id=".$_GET["ref_id"]."&mob_id=".$target_id;
1677  break;
1678  }
1679  }
1680  return $href;
1681  }
1682 
1693  function _isWriteable($question_id, $user_id)
1694  {
1695  global $ilDB;
1696 
1697  if (($question_id < 1) || ($user_id < 1))
1698  {
1699  return false;
1700  }
1701 
1702  $query = sprintf("SELECT obj_fi FROM survey_question WHERE question_id = %s",
1703  $ilDB->quote($question_id . "")
1704  );
1705  $result = $ilDB->query($query);
1706  if ($result->numRows() == 1)
1707  {
1708  $row = $result->fetchRow(MDB2_FETCHMODE_ASSOC);
1709  $qpl_object_id = $row["obj_fi"];
1710  include_once "./Modules/SurveyQuestionPool/classes/class.ilObjSurveyQuestionPool.php";
1711  return ilObjSurveyQuestionPool::_isWriteable($qpl_object_id, $user_id);
1712  }
1713  else
1714  {
1715  return false;
1716  }
1717  }
1718 
1728  {
1729  global $ilDB;
1730  $query = sprintf("SELECT questiontype_id FROM survey_questiontype WHERE type_tag = %s",
1731  $ilDB->quote($this->getQuestionType())
1732  );
1733  $result = $ilDB->query($query);
1734  if ($result->numRows() == 1)
1735  {
1736  $row = $result->fetchRow(MDB2_FETCHMODE_ASSOC);
1737  return $row["questiontype_id"];
1738  }
1739  else
1740  {
1741  return 0;
1742  }
1743  }
1744 
1753  function getQuestionType()
1754  {
1755  return "";
1756  }
1757 
1765  static function _includeClass($question_type, $gui = 0)
1766  {
1767  $type = $question_type;
1768  if ($gui) $type .= "GUI";
1769  if (file_exists("./Modules/SurveyQuestionPool/classes/class.".$type.".php"))
1770  {
1771  include_once "./Modules/SurveyQuestionPool/classes/class.".$type.".php";
1772  return true;
1773  }
1774  else
1775  {
1776  global $ilPluginAdmin;
1777  $pl_names = $ilPluginAdmin->getActivePluginsForSlot(IL_COMP_MODULE, "SurveyQuestionPool", "svyq");
1778  foreach ($pl_names as $pl_name)
1779  {
1780  $pl = ilPlugin::getPluginObject(IL_COMP_MODULE, "SurveyQuestionPool", "svyq", $pl_name);
1781  if (strcmp($pl->getQuestionType(), $question_type) == 0)
1782  {
1783  $pl->includeClass("class.".$type.".php");
1784  return true;
1785  }
1786  }
1787  }
1788  return false;
1789  }
1790 
1797  static function _getQuestionTypeName($type_tag)
1798  {
1799  if (file_exists("./Modules/SurveyQuestionPool/classes/class.".$type_tag.".php"))
1800  {
1801  global $lng;
1802  return $lng->txt($type_tag);
1803  }
1804  else
1805  {
1806  global $ilPluginAdmin;
1807  $pl_names = $ilPluginAdmin->getActivePluginsForSlot(IL_COMP_MODULE, "SurveyQuestionPool", "svyq");
1808  foreach ($pl_names as $pl_name)
1809  {
1810  $pl = ilPlugin::getPluginObject(IL_COMP_MODULE, "SurveyQuestionPool", "svyq", $pl_name);
1811  if (strcmp($pl->getQuestionType(), $type_tag) == 0)
1812  {
1813  return $pl->getQuestionTypeTranslation();
1814  }
1815  }
1816  }
1817  return "";
1818  }
1819 
1820 
1828  function &_instanciateQuestion($question_id)
1829  {
1830  $question_type = SurveyQuestion::_getQuestionType($question_id);
1831  SurveyQuestion::_includeClass($question_type);
1832  $question = new $question_type();
1833  $question->loadFromDb($question_id);
1834  return $question;
1835  }
1836 
1844  function &_instanciateQuestionGUI($question_id)
1845  {
1846  $question_type = SurveyQuestion::_getQuestionType($question_id);
1847  SurveyQuestion::_includeClass($question_type, 1);
1848  $guitype = $question_type . "GUI";
1849  $question = new $guitype($question_id);
1850  return $question;
1851  }
1852 
1861  function isHTML($a_text)
1862  {
1863  if (preg_match("/<[^>]*?>/", $a_text))
1864  {
1865  return TRUE;
1866  }
1867  else
1868  {
1869  return FALSE;
1870  }
1871  }
1872 
1880  function QTIMaterialToString($a_material)
1881  {
1882  $result = "";
1883  for ($i = 0; $i < $a_material->getMaterialCount(); $i++)
1884  {
1885  $material = $a_material->getMaterial($i);
1886  if (strcmp($material["type"], "mattext") == 0)
1887  {
1888  $result .= $material["material"]->getContent();
1889  }
1890  if (strcmp($material["type"], "matimage") == 0)
1891  {
1892  $matimage = $material["material"];
1893  if (preg_match("/(il_([0-9]+)_mob_([0-9]+))/", $matimage->getLabel(), $matches))
1894  {
1895  // import an mediaobject which was inserted using tiny mce
1896  if (!is_array($_SESSION["import_mob_xhtml"])) $_SESSION["import_mob_xhtml"] = array();
1897  array_push($_SESSION["import_mob_xhtml"], array("mob" => $matimage->getLabel(), "uri" => $matimage->getUri()));
1898  }
1899  }
1900  }
1901  return $result;
1902  }
1903 
1912  function addMaterialTag(&$a_xml_writer, $a_material, $close_material_tag = TRUE, $add_mobs = TRUE)
1913  {
1914  include_once "./Services/RTE/classes/class.ilRTE.php";
1915  include_once("./Services/MediaObjects/classes/class.ilObjMediaObject.php");
1916 
1917  $a_xml_writer->xmlStartTag("material");
1918  $attrs = array(
1919  "type" => "text/plain"
1920  );
1921  if ($this->isHTML($a_material))
1922  {
1923  $attrs["type"] = "text/xhtml";
1924  }
1925  $a_xml_writer->xmlElement("mattext", $attrs, ilRTE::_replaceMediaObjectImageSrc($a_material, 0));
1926 
1927  if ($add_mobs)
1928  {
1929  $mobs = ilObjMediaObject::_getMobsOfObject("spl:html", $this->getId());
1930  foreach ($mobs as $mob)
1931  {
1932  $mob_obj =& new ilObjMediaObject($mob);
1933  $imgattrs = array(
1934  "label" => "il_" . IL_INST_ID . "_mob_" . $mob,
1935  "uri" => "objects/" . "il_" . IL_INST_ID . "_mob_" . $mob . "/" . $mob_obj->getTitle()
1936  );
1937  $a_xml_writer->xmlElement("matimage", $imgattrs, NULL);
1938  }
1939  }
1940  if ($close_material_tag) $a_xml_writer->xmlEndTag("material");
1941  }
1942 
1949  function prepareTextareaOutput($txt_output, $prepare_for_latex_output = FALSE)
1950  {
1951  include_once "./Services/Utilities/classes/class.ilUtil.php";
1952  return ilUtil::prepareTextareaOutput($txt_output, $prepare_for_latex_output);
1953  }
1954 
1965  {
1966  return array();
1967  }
1968 
1969 
1979  {
1980  array_push($a_array, $this->getTitle());
1981  }
1982 
1992  function addUserSpecificResultsData(&$a_array, &$resultset)
1993  {
1994  // overwrite in inherited classes
1995  }
1996 
2007  {
2008  // overwrite in inherited classes
2009  return array();
2010  }
2011 
2020  function &getWorkingDataFromUserInput($post_data)
2021  {
2022  // overwrite in inherited classes
2023  $data = array();
2024  return $data;
2025  }
2026 
2037  function importAdditionalMetadata($a_meta)
2038  {
2039  // overwrite in inherited classes
2040  }
2041 
2050  function importResponses($a_data)
2051  {
2052  // overwrite in inherited classes
2053  }
2054 
2063  function importAdjectives($a_data)
2064  {
2065  // overwrite in inherited classes
2066  }
2067 
2076  function importMatrix($a_data)
2077  {
2078  // overwrite in inherited classes
2079  }
2080 
2094  function setExportCumulatedXLS(&$worksheet, &$format_title, &$format_bold, &$eval_data, $row)
2095  {
2096  include_once ("./classes/class.ilExcelUtils.php");
2097  $worksheet->writeString($row, 0, ilExcelUtils::_convert_text($this->getTitle()));
2098  $worksheet->writeString($row, 1, ilExcelUtils::_convert_text($this->getQuestiontext()));
2099  $worksheet->writeString($row, 2, ilExcelUtils::_convert_text($this->lng->txt($eval_data["QUESTION_TYPE"])));
2100  $worksheet->write($row, 3, $eval_data["USERS_ANSWERED"]);
2101  $worksheet->write($row, 4, $eval_data["USERS_SKIPPED"]);
2102  $worksheet->write($row, 5, ilExcelUtils::_convert_text($eval_data["MODE_VALUE"]));
2103  $worksheet->write($row, 6, ilExcelUtils::_convert_text($eval_data["MODE"]));
2104  $worksheet->write($row, 7, $eval_data["MODE_NR_OF_SELECTIONS"]);
2105  $worksheet->write($row, 8, ilExcelUtils::_convert_text(str_replace("<br />", " ", $eval_data["MEDIAN"])));
2106  $worksheet->write($row, 9, $eval_data["ARITHMETIC_MEAN"]);
2107  return $row + 1;
2108  }
2109 
2123  function &setExportCumulatedCVS(&$eval_data)
2124  {
2125  $csvrow = array();
2126  array_push($csvrow, $this->getTitle());
2127  array_push($csvrow, $this->getQuestiontext());
2128  array_push($csvrow, $this->lng->txt($eval_data["QUESTION_TYPE"]));
2129  array_push($csvrow, $eval_data["USERS_ANSWERED"]);
2130  array_push($csvrow, $eval_data["USERS_SKIPPED"]);
2131  array_push($csvrow, $eval_data["MODE"]);
2132  array_push($csvrow, $eval_data["MODE_NR_OF_SELECTIONS"]);
2133  array_push($csvrow, $eval_data["MEDIAN"]);
2134  array_push($csvrow, $eval_data["ARITHMETIC_MEAN"]);
2135  $result = array();
2136  array_push($result, $csvrow);
2137  return $result;
2138  }
2139 
2151  function setExportDetailsXLS(&$workbook, &$format_title, &$format_bold, &$eval_data)
2152  {
2153  // overwrite in inherited classes
2154  }
2155 
2165  {
2166  // overwrite in inherited classes
2167  return FALSE;
2168  }
2169 
2179  {
2180  // overwrite in inherited classes
2181  return array();
2182  }
2183 
2192  function getPreconditionSelectValue($default = "")
2193  {
2194  // overwrite in inherited classes
2195  }
2196 
2207  {
2208  // overwrite in inherited classes
2209  return $value;
2210  }
2211 
2222  function outChart($survey_id, $type = "")
2223  {
2224  // overwrite in inherited classes
2225  }
2226 
2227  function setOriginalId($original_id)
2228  {
2229  $this->original_id = $original_id;
2230  }
2231 
2232  function getOriginalId()
2233  {
2234  return $this->original_id;
2235  }
2236 
2237 }
2238 ?>