ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilObjSurveyQuestionPool.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 
34 include_once "./Services/Object/classes/class.ilObject.php";
35 
37 {
43  var $online;
44 
51  function ilObjSurveyQuestionPool($a_id = 0,$a_call_by_reference = true)
52  {
53  $this->type = "spl";
54  $this->ilObject($a_id,$a_call_by_reference);
55  }
56 
60  function create($a_upload = false)
61  {
63  if(!$a_upload)
64  {
65  $this->createMetaData();
66  }
67  }
68 
75  function update()
76  {
77  $this->updateMetaData();
78  if (!parent::update())
79  {
80  return false;
81  }
82 
83  // put here object specific stuff
84 
85  return true;
86  }
87 
93  function read($a_force_db = false)
94  {
95  parent::read($a_force_db);
96  $this->loadFromDb();
97  }
98 
104  function cloneObject($a_target_id,$a_copy_id = 0)
105  {
106  global $ilLog;
107  $newObj = parent::cloneObject($a_target_id,$a_copy_id);
108 
109  //copy online status if object is not the root copy object
110  $cp_options = ilCopyWizardOptions::_getInstance($a_copy_id);
111 
112  if(!$cp_options->isRootNode($this->getRefId()))
113  {
114  $newObj->setOnline($this->getOnline());
115  }
116 
117  $newObj->saveToDb();
118  // clone the questions in the question pool
119  $questions =& $this->getQuestions();
120  foreach ($questions as $question_id)
121  {
122  $newObj->copyQuestion($question_id, $newObj->getId());
123  }
124 
125  // clone meta data
126  include_once "./Services/MetaData/classes/class.ilMD.php";
127  $md = new ilMD($this->getId(),0,$this->getType());
128  $new_md =& $md->cloneMD($newObj->getId(),0,$newObj->getType());
129 
130  // update the metadata with the new title of the question pool
131  $newObj->updateMetaData();
132  return $newObj;
133  }
134 
135  function &createQuestion($question_type, $question_id = -1)
136  {
137  if ((!$question_type) and ($question_id > 0))
138  {
139  $question_type = $this->getQuestiontype($question_id);
140  }
141 
142  include_once "./Modules/SurveyQuestionPool/classes/class.".$question_type."GUI.php";
143  $question_type_gui = $question_type . "GUI";
144  $question =& new $question_type_gui();
145 
146  if ($question_id > 0)
147  {
148  $question->object->loadFromDb($question_id);
149  }
150 
151  return $question;
152  }
153 
161  function copyQuestion($question_id, $questionpool_to)
162  {
163  $question_gui =& $this->createQuestion("", $question_id);
164  if ($question_gui->object->getObjId() == $questionpool_to)
165  {
166  // the question is copied into the same question pool
167  $this->duplicateQuestion($question_id);
168  }
169  else
170  {
171  // the question is copied into another question pool
172  $newtitle = $question_gui->object->getTitle();
173  if ($question_gui->object->questionTitleExists($question_gui->object->getTitle(), $questionpool_to))
174  {
175  $counter = 2;
176  while ($question_gui->object->questionTitleExists($question_gui->object->getTitle() . " ($counter)", $questionpool_to))
177  {
178  $counter++;
179  }
180  $newtitle = $question_gui->object->getTitle() . " ($counter)";
181  }
182  $question_gui->object->copyObject($this->getId(), $newtitle);
183  }
184  }
185 
191  function loadFromDb()
192  {
193  global $ilDB;
194 
195  $result = $ilDB->queryF("SELECT * FROM svy_qpl WHERE obj_fi = %s",
196  array('integer'),
197  array($this->getId())
198  );
199  if ($result->numRows() == 1)
200  {
201  $row = $ilDB->fetchAssoc($result);
202  $this->setOnline($row["isonline"]);
203  }
204  }
205 
211  function saveToDb()
212  {
213  global $ilDB;
214 
215  $result = $ilDB->queryF("SELECT * FROM svy_qpl WHERE obj_fi = %s",
216  array('integer'),
217  array($this->getId())
218  );
219  if ($result->numRows() == 1)
220  {
221  $affectedRows = $ilDB->manipulateF("UPDATE svy_qpl SET isonline = %s, tstamp = %s WHERE obj_fi = %s",
222  array('text','integer','integer'),
223  array($this->getOnline(), time(), $this->getId())
224  );
225  }
226  else
227  {
228  $next_id = $ilDB->nextId('svy_qpl');
229  $query = $ilDB->manipulateF("INSERT INTO svy_qpl (id_questionpool, isonline, obj_fi, tstamp) VALUES (%s, %s, %s, %s)",
230  array('integer', 'text', 'integer', 'integer'),
231  array($next_id, $this->getOnline(), $this->getId(), time())
232  );
233  }
234  }
235 
242  function delete()
243  {
244  $remove = parent::delete();
245  // always call parent delete function first!!
246  if (!$remove)
247  {
248  return false;
249  }
250 
251  // delete all related questions
252  $this->deleteAllData();
253 
254  // delete meta data
255  $this->deleteMetaData();
256 
257  return true;
258  }
259 
260  function deleteAllData()
261  {
262  global $ilDB;
263  $result = $ilDB->queryF("SELECT question_id FROM svy_question WHERE obj_fi = %s AND original_id IS NULL",
264  array('integer'),
265  array($this->getId())
266  );
267  $found_questions = array();
268  while ($row = $ilDB->fetchAssoc($result))
269  {
270  $this->removeQuestion($row["question_id"]);
271  }
272 
273  // delete export files
274  include_once "./Services/Utilities/classes/class.ilUtil.php";
275  $spl_data_dir = ilUtil::getDataDir()."/spl_data";
276  $directory = $spl_data_dir."/spl_".$this->getId();
277  if (is_dir($directory))
278  {
279  include_once "./Services/Utilities/classes/class.ilUtil.php";
280  ilUtil::delDir($directory);
281  }
282  }
283 
297  function notify($a_event,$a_ref_id,$a_parent_non_rbac_id,$a_node_id,$a_params = 0)
298  {
299  global $tree;
300 
301  switch ($a_event)
302  {
303  case "link":
304 
305  //var_dump("<pre>",$a_params,"</pre>");
306  //echo "Module name ".$this->getRefId()." triggered by link event. Objects linked into target object ref_id: ".$a_ref_id;
307  //exit;
308  break;
309 
310  case "cut":
311 
312  //echo "Module name ".$this->getRefId()." triggered by cut event. Objects are removed from target object ref_id: ".$a_ref_id;
313  //exit;
314  break;
315 
316  case "copy":
317 
318  //var_dump("<pre>",$a_params,"</pre>");
319  //echo "Module name ".$this->getRefId()." triggered by copy event. Objects are copied into target object ref_id: ".$a_ref_id;
320  //exit;
321  break;
322 
323  case "paste":
324 
325  //echo "Module name ".$this->getRefId()." triggered by paste (cut) event. Objects are pasted into target object ref_id: ".$a_ref_id;
326  //exit;
327  break;
328 
329  case "new":
330 
331  //echo "Module name ".$this->getRefId()." triggered by paste (new) event. Objects are applied to target object ref_id: ".$a_ref_id;
332  //exit;
333  break;
334  }
335 
336  // At the beginning of the recursive process it avoids second call of the notify function with the same parameter
337  if ($a_node_id==$_GET["ref_id"])
338  {
339  $parent_obj =& $this->ilias->obj_factory->getInstanceByRefId($a_node_id);
340  $parent_type = $parent_obj->getType();
341  if($parent_type == $this->getType())
342  {
343  $a_node_id = (int) $tree->getParentId($a_node_id);
344  }
345  }
346 
347  parent::notify($a_event,$a_ref_id,$a_parent_non_rbac_id,$a_node_id,$a_params);
348  }
349 
355  function getTitle()
356  {
357  //return $this->title;
358  return parent::getTitle();
359  }
360 
364  function setTitle($a_title)
365  {
366  parent::setTitle($a_title);
367  }
368 
375  function removeQuestion($question_id)
376  {
377  if ($question_id < 1) return;
378  include_once "./Modules/SurveyQuestionPool/classes/class.SurveyQuestion.php";
379  $question =& SurveyQuestion::_instanciateQuestion($question_id);
380  $question->delete($question_id);
381  }
382 
390  function getQuestiontype($question_id)
391  {
392  global $ilDB;
393  if ($question_id < 1) return;
394  $result = $ilDB->queryF("SELECT svy_qtype.type_tag FROM svy_question, svy_qtype WHERE svy_question.questiontype_fi = svy_qtype.questiontype_id AND svy_question.question_id = %s",
395  array('integer'),
396  array($question_id)
397  );
398  if ($result->numRows() == 1)
399  {
400  $data = $ilDB->fetchAssoc($result);
401  return $data["type_tag"];
402  }
403  else
404  {
405  return;
406  }
407  }
408 
416  function isInUse($question_id)
417  {
418  global $ilDB;
419  // check out the already answered questions
420  $result = $ilDB->queryF("SELECT answer_id FROM svy_answer WHERE question_fi = %s",
421  array('integer'),
422  array($question_id)
423  );
424  $answered = $result->numRows();
425 
426  // check out the questions inserted in surveys
427  $result = $ilDB->queryF("SELECT svy_svy.* FROM svy_svy, svy_svy_qst WHERE svy_svy_qst.survey_fi = svy_svy.survey_id AND svy_svy_qst.question_fi = %s",
428  array('integer'),
429  array($question_id)
430  );
431  $inserted = $result->numRows();
432  if (($inserted + $answered) == 0)
433  {
434  return false;
435  }
436  $result_array = array();
437  while ($row = $ilDB->fetchObject($result))
438  {
439  array_push($result_array, $row);
440  }
441  return $result_array;
442  }
443 
450  function paste($question_id)
451  {
452  $this->duplicateQuestion($question_id, $this->getId());
453  }
454 
462  function &getQuestionsInfo($question_array)
463  {
464  global $ilDB;
465  $result_array = array();
466  $result = $ilDB->query("SELECT svy_question.*, svy_qtype.type_tag, svy_qtype.plugin FROM svy_question, svy_qtype WHERE svy_question.questiontype_fi = svy_qtype.questiontype_id AND svy_question.tstamp > 0 AND " . $ilDB->in('svy_question.question_id', $question_array, false, 'integer'));
467  while ($row = $ilDB->fetchAssoc($result))
468  {
469  if ($row["plugin"])
470  {
471  if ($this->isPluginActive($row["type_tag"]))
472  {
473  array_push($result_array, $row);
474  }
475  }
476  else
477  {
478  array_push($result_array, $row);
479  }
480  }
481  return $result_array;
482  }
483 
490  function duplicateQuestion($question_id, $obj_id = "")
491  {
492  global $ilUser;
493  include_once "./Modules/SurveyQuestionPool/classes/class.SurveyQuestion.php";
494  $question = SurveyQuestion::_instanciateQuestion($question_id);
495  $suffix = "";
496  $counter = 1;
497  while ($question->questionTitleExists($question->getTitle().$suffix, $obj_id))
498  {
499  $counter++;
500  if ($counter > 1) $suffix = " ($counter)";
501  }
502  if ($obj_id)
503  {
504  $question->setObjId($obj_id);
505  }
506  $question->duplicate(false, $question->getTitle() . $suffix, $ilUser->fullname, $ilUser->id);
507  }
508 
514  function getQuestionsData($arrFilter)
515  {
516  global $ilUser;
517  global $ilDB;
518  $where = "";
519  if (is_array($arrFilter))
520  {
521  foreach ($arrFilter as $key => $value)
522  {
523  $arrFilter[$key] = str_replace('%', '', $arrFilter[$key]);
524  }
525  if (array_key_exists('title', $arrFilter) && strlen($arrFilter['title']))
526  {
527  $where .= " AND " . $ilDB->like('svy_question.title', 'text', "%%" . $arrFilter['title'] . "%%");
528  }
529  if (array_key_exists('description', $arrFilter) && strlen($arrFilter['description']))
530  {
531  $where .= " AND " . $ilDB->like('svy_question.description', 'text', "%%" . $arrFilter['description'] . "%%");
532  }
533  if (array_key_exists('author', $arrFilter) && strlen($arrFilter['author']))
534  {
535  $where .= " AND " . $ilDB->like('svy_question.author', 'text', "%%" . $arrFilter['author'] . "%%");
536  }
537  if (array_key_exists('type', $arrFilter) && strlen($arrFilter['type']))
538  {
539  $where .= " AND svy_qtype.type_tag = " . $ilDB->quote($arrFilter['type'], 'text');
540  }
541  }
542  $query_result = $ilDB->queryF("SELECT svy_question.*, svy_qtype.type_tag, svy_qtype.plugin FROM svy_question, svy_qtype WHERE svy_question.original_id IS NULL AND svy_question.tstamp > 0 AND svy_question.questiontype_fi = svy_qtype.questiontype_id AND svy_question.obj_fi = %s" . $where,
543  array('integer'),
544  array($this->getId())
545  );
546  $rows = array();
547  if ($query_result->numRows())
548  {
549  while ($row = $ilDB->fetchAssoc($query_result))
550  {
551  if ($row["plugin"])
552  {
553  if ($this->isPluginActive($row["type_tag"]))
554  {
555  array_push($rows, $row);
556  }
557  }
558  else
559  {
560  array_push($rows, $row);
561  }
562  }
563  }
564  return $rows;
565  }
566 
573  {
574  include_once "./Services/Utilities/classes/class.ilUtil.php";
575  $spl_data_dir = ilUtil::getDataDir()."/spl_data";
576  ilUtil::makeDir($spl_data_dir);
577  if(!is_writable($spl_data_dir))
578  {
579  $this->ilias->raiseError("Survey Questionpool Data Directory (".$spl_data_dir
580  .") not writeable.",$this->ilias->error_obj->FATAL);
581  }
582 
583  // create learning module directory (data_dir/lm_data/lm_<id>)
584  $spl_dir = $spl_data_dir."/spl_".$this->getId();
585  ilUtil::makeDir($spl_dir);
586  if(!@is_dir($spl_dir))
587  {
588  $this->ilias->raiseError("Creation of Survey Questionpool Directory failed.",$this->ilias->error_obj->FATAL);
589  }
590  // create Export subdirectory (data_dir/lm_data/lm_<id>/Export)
591  $export_dir = $spl_dir."/export";
592  ilUtil::makeDir($export_dir);
593  if(!@is_dir($export_dir))
594  {
595  $this->ilias->raiseError("Creation of Export Directory failed.",$this->ilias->error_obj->FATAL);
596  }
597  }
598 
603  {
604  include_once "./Services/Utilities/classes/class.ilUtil.php";
605  $export_dir = ilUtil::getDataDir()."/spl_data"."/spl_".$this->getId()."/export";
606  return $export_dir;
607  }
608 
612  function getExportFiles($dir)
613  {
614  // quit if import dir not available
615  if (!@is_dir($dir) or
616  !is_writeable($dir))
617  {
618  return array();
619  }
620 
621  // open directory
622  $dir = dir($dir);
623 
624  // initialize array
625  $file = array();
626 
627  // get files and save the in the array
628  while ($entry = $dir->read())
629  {
630  if ($entry != "." &&
631  $entry != ".." &&
632  ereg("^[0-9]{10}_{2}[0-9]+_{2}(spl_)*[0-9]+\.[A-Za-z]{3}\$", $entry))
633  {
634  $file[] = $entry;
635  }
636  }
637 
638  // close import directory
639  $dir->close();
640  // sort files
641  sort ($file);
642  reset ($file);
643 
644  return $file;
645  }
646 
653  {
654  include_once "./Services/Utilities/classes/class.ilUtil.php";
655  $spl_data_dir = ilUtil::getDataDir()."/spl_data";
656  ilUtil::makeDir($spl_data_dir);
657 
658  if(!is_writable($spl_data_dir))
659  {
660  $this->ilias->raiseError("Survey Questionpool Data Directory (".$spl_data_dir
661  .") not writeable.",$this->ilias->error_obj->FATAL);
662  }
663 
664  // create test directory (data_dir/spl_data/spl_<id>)
665  $spl_dir = $spl_data_dir."/spl_".$this->getId();
666  ilUtil::makeDir($spl_dir);
667  if(!@is_dir($spl_dir))
668  {
669  $this->ilias->raiseError("Creation of Survey Questionpool Directory failed.",$this->ilias->error_obj->FATAL);
670  }
671 
672  // create import subdirectory (data_dir/spl_data/spl_<id>/import)
673  $import_dir = $spl_dir."/import";
674  ilUtil::makeDir($import_dir);
675  if(!@is_dir($import_dir))
676  {
677  $this->ilias->raiseError("Creation of Import Directory failed.",$this->ilias->error_obj->FATAL);
678  }
679  }
680 
685  {
686  include_once "./Services/Utilities/classes/class.ilUtil.php";
687  $import_dir = ilUtil::getDataDir()."/spl_data".
688  "/spl_".$this->getId()."/import";
689  if(@is_dir($import_dir))
690  {
691  return $import_dir;
692  }
693  else
694  {
695  return false;
696  }
697  }
698 
702  function toXML($questions)
703  {
704  if (!is_array($questions))
705  {
706  $questions =& $this->getQuestions();
707  }
708  if (count($questions) == 0)
709  {
710  $questions =& $this->getQuestions();
711  }
712  $xml = "";
713 
714  include_once("./Services/Xml/classes/class.ilXmlWriter.php");
715  $a_xml_writer = new ilXmlWriter;
716  // set xml header
717  $a_xml_writer->xmlHeader();
718  $attrs = array(
719  "xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance",
720  "xsi:noNamespaceSchemaLocation" => "http://www.ilias.de/download/xsd/ilias_survey_4_2.xsd"
721  );
722  $a_xml_writer->xmlStartTag("surveyobject", $attrs);
723  $attrs = array(
724  "id" => "qpl_" . $this->getId(),
725  "label" => $this->getTitle(),
726  "online" => $this->getOnline()
727  );
728  $a_xml_writer->xmlStartTag("surveyquestions", $attrs);
729  $a_xml_writer->xmlElement("dummy", NULL, "dummy");
730  // add ILIAS specific metadata
731  $a_xml_writer->xmlStartTag("metadata");
732  $a_xml_writer->xmlStartTag("metadatafield");
733  $a_xml_writer->xmlElement("fieldlabel", NULL, "SCORM");
734  include_once "./Services/MetaData/classes/class.ilMD.php";
735  $md = new ilMD($this->getId(),0, $this->getType());
736  $writer = new ilXmlWriter();
737  $md->toXml($writer);
738  $metadata = $writer->xmlDumpMem();
739  $a_xml_writer->xmlElement("fieldentry", NULL, $metadata);
740  $a_xml_writer->xmlEndTag("metadatafield");
741  $a_xml_writer->xmlEndTag("metadata");
742 
743  $a_xml_writer->xmlEndTag("surveyquestions");
744  $a_xml_writer->xmlEndTag("surveyobject");
745 
746  $xml = $a_xml_writer->xmlDumpMem(FALSE);
747 
748  $questionxml = "";
749  foreach ($questions as $key => $value)
750  {
751  $questiontype = $this->getQuestiontype($value);
752  include_once "./Modules/SurveyQuestionPool/classes/class.SurveyQuestion.php";
753  SurveyQuestion::_includeClass($questiontype);
754  $question = new $questiontype();
755  $question->loadFromDb($value);
756  $questionxml .= $question->toXML(false);
757  }
758 
759  $xml = str_replace("<dummy>dummy</dummy>", $questionxml, $xml);
760  return $xml;
761  }
762 
763  function &getQuestions()
764  {
765  global $ilDB;
766  $questions = array();
767  $result = $ilDB->queryF("SELECT question_id FROM svy_question WHERE obj_fi = %s AND svy_question.tstamp > 0 AND original_id IS NULL",
768  array('integer'),
769  array($this->getId())
770  );
771  if ($result->numRows())
772  {
773  while ($row = $ilDB->fetchAssoc($result))
774  {
775  array_push($questions, $row["question_id"]);
776  }
777  }
778  return $questions;
779  }
780 
787  function importObject($source, $spl_exists = FALSE)
788  {
789  if (is_file($source))
790  {
791  $isZip = (strcmp(strtolower(substr($source, -3)), 'zip') == 0);
792  if ($isZip)
793  {
794  // unzip file
795  ilUtil::unzip($source);
796 
797  // determine filenames of xml files
798  $subdir = basename($source, ".zip");
799  $source = dirname($source)."/".$subdir."/".$subdir.".xml";
800  }
801 
802  $fh = fopen($source, "r") or die("");
803  $xml = fread($fh, filesize($source));
804  fclose($fh) or die("");
805  if ($isZip)
806  {
807  $subdir = basename($source, ".zip");
808  if (@is_dir(dirname($source)."/".$subdir))
809  {
810  ilUtil::delDir(dirname($source)."/".$subdir);
811  }
812  }
813  if (strpos($xml, "questestinterop") > 0)
814  {
815  // survey questions for ILIAS < 3.8
816  include_once "./Services/Survey/classes/class.SurveyImportParserPre38.php";
817  $import = new SurveyImportParserPre38($this->getId(), "", $spl_exists);
818  $import->setXMLContent($xml);
819  $import->startParsing();
820  }
821  else
822  {
823  // survey questions for ILIAS >= 3.8
824  include_once "./Services/Survey/classes/class.SurveyImportParser.php";
825  $import = new SurveyImportParser($this->getId(), "", $spl_exists);
826  $import->setXMLContent($xml);
827  $import->startParsing();
828  }
829  }
830  }
831 
832  public static function _setOnline($a_obj_id, $a_online_status)
833  {
834  global $ilDB;
835 
836  $status = "0";
837  switch ($a_online_status)
838  {
839  case 0:
840  case 1:
841  $status = "$a_online_status";
842  break;
843  }
844  $affectedRows = $ilDB->manipulateF("UPDATE svy_qpl SET isonline = %s WHERE obj_fi = %s",
845  array('text','integer'),
846  array($status, $a_obj_id)
847  );
848  }
849 
857  function setOnline($a_online_status)
858  {
859  switch ($a_online_status)
860  {
861  case 0:
862  case 1:
863  $this->online = $a_online_status;
864  break;
865  default:
866  $this->online = 0;
867  break;
868  }
869  }
870 
871  function getOnline()
872  {
873  if (strcmp($this->online, "") == 0) $this->online = "0";
874  return $this->online;
875  }
876 
877  function _lookupOnline($a_obj_id)
878  {
879  global $ilDB;
880 
881  $result = $ilDB->queryF("SELECT isonline FROM svy_qpl WHERE obj_fi = %s",
882  array('integer'),
883  array($a_obj_id)
884  );
885  if ($result->numRows() == 1)
886  {
887  $row = $ilDB->fetchAssoc($result);
888  return $row["isonline"];
889  }
890  return 0;
891  }
892 
900  function _isWriteable($object_id, $user_id)
901  {
902  global $rbacsystem;
903  global $ilDB;
904 
905  $refs = ilObject::_getAllReferences($object_id);
906  $result = false;
907  foreach ($refs as $ref)
908  {
909  if ($rbacsystem->checkAccess("write", $ref) && (ilObject::_hasUntrashedReference($object_id)))
910  {
911  $result = true;
912  }
913  }
914  return $result;
915  }
916 
923  function &_getQuestiontypes()
924  {
925  global $ilDB;
926  global $lng;
927 
928  $lng->loadLanguageModule("survey");
929  $types = array();
930  $query_result = $ilDB->query("SELECT * FROM svy_qtype ORDER BY type_tag");
931  while ($row = $ilDB->fetchAssoc($query_result))
932  {
933  //array_push($questiontypes, $row["type_tag"]);
934  if ($row["plugin"] == 0)
935  {
936  $types[$lng->txt($row["type_tag"])] = $row;
937  }
938  else
939  {
940  global $ilPluginAdmin;
941  $pl_names = $ilPluginAdmin->getActivePluginsForSlot(IL_COMP_MODULE, "SurveyQuestionPool", "svyq");
942  foreach ($pl_names as $pl_name)
943  {
944  $pl = ilPlugin::getPluginObject(IL_COMP_MODULE, "SurveyQuestionPool", "svyq", $pl_name);
945  if (strcmp($pl->getQuestionType(), $row["type_tag"]) == 0)
946  {
947  $types[$pl->getQuestionTypeTranslation()] = $row;
948  }
949  }
950  }
951  }
952  ksort($types);
953 
954 
955  // #14263 - default sorting
956 
957  $default_sorting = array_flip(array(
958  "SurveySingleChoiceQuestion",
959  "SurveyMultipleChoiceQuestion",
960  "SurveyMatrixQuestion",
961  "SurveyMetricQuestion",
962  "SurveyTextQuestion"
963  ));
964 
965  $sorted = array();
966  $idx = sizeof($default_sorting);
967  foreach($types as $caption => $item)
968  {
969  $type = $item["type_tag"];
970  $item["caption"] = $caption;
971 
972  // default
973  if(array_key_exists($type, $default_sorting))
974  {
975  $sorted[$default_sorting[$type]] = $item;
976  }
977  // plugin (append alphabetically sorted)
978  else
979  {
980  $sorted[$idx] = $item;
981  $idx++;
982  }
983  }
984  ksort($sorted);
985 
986  // redo captions as index
987  $types = array();
988  foreach($sorted as $item)
989  {
990  $types[$item["caption"]] = $item;
991  }
992 
993  return $types;
994  }
995 
996  public static function &_getQuestionTypeTranslations()
997  {
998  global $ilDB;
999  global $lng;
1000  global $ilLog;
1001  global $ilPluginAdmin;
1002 
1003  $lng->loadLanguageModule("survey");
1004  $result = $ilDB->query("SELECT * FROM svy_qtype");
1005  $types = array();
1006  while ($row = $ilDB->fetchAssoc($result))
1007  {
1008  if ($row["plugin"] == 0)
1009  {
1010  $types[$row['type_tag']] = $lng->txt($row["type_tag"]);
1011  }
1012  else
1013  {
1014  $pl_names = $ilPluginAdmin->getActivePluginsForSlot(IL_COMP_MODULE, "SurveyQuestionPool", "svyq");
1015  foreach ($pl_names as $pl_name)
1016  {
1017  $pl = ilPlugin::getPluginObject(IL_COMP_MODULE, "SurveyQuestionPool", "svyq", $pl_name);
1018  if (strcmp($pl->getQuestionType(), $row["type_tag"]) == 0)
1019  {
1020  $types[$row['type_tag']] = $pl->getQuestionTypeTranslation();
1021  }
1022  }
1023  }
1024  }
1025  ksort($types);
1026  return $types;
1027  }
1028 
1035  function _getAvailableQuestionpools($use_object_id = FALSE, $could_be_offline = FALSE, $showPath = FALSE, $permission = "read")
1036  {
1037  global $ilUser;
1038  global $ilDB;
1039 
1040  $result_array = array();
1041  $qpls = ilUtil::_getObjectsByOperations("spl", $permission, $ilUser->getId(), -1);
1042  $titles = ilObject::_prepareCloneSelection($qpls, "spl", $showPath);
1043  $allqpls = array();
1044  $result = $ilDB->query("SELECT obj_fi, isonline FROM svy_qpl");
1045  while ($row = $ilDB->fetchAssoc($result))
1046  {
1047  $allqpls[$row['obj_fi']] = $row['isonline'];
1048  }
1049  foreach ($qpls as $ref_id)
1050  {
1051  $obj_id = ilObject::_lookupObjectId($ref_id);
1052  if ($could_be_offline || $allqpls[$obj_id] == 1)
1053  {
1054  if ($use_object_id)
1055  {
1056  $result_array[$obj_id] = $titles[$ref_id];
1057  }
1058  else
1059  {
1060  $result_array[$ref_id] = $titles[$ref_id];
1061  }
1062  }
1063  }
1064  return $result_array;
1065  }
1066 
1073  function isPluginActive($a_pname)
1074  {
1075  global $ilPluginAdmin;
1076  if ($ilPluginAdmin->isActive(IL_COMP_MODULE, "SurveyQuestionPool", "svyq", $a_pname))
1077  {
1078  return TRUE;
1079  }
1080  else
1081  {
1082  return FALSE;
1083  }
1084  }
1085 
1092  public function getQuestionInfos($question_ids)
1093  {
1094  global $ilDB;
1095 
1096  $found = array();
1097  $query_result = $ilDB->query("SELECT svy_question.*, svy_qtype.type_tag FROM svy_question, svy_qtype " .
1098  "WHERE svy_question.questiontype_fi = svy_qtype.questiontype_id " .
1099  "AND svy_question.tstamp > 0 AND " . $ilDB->in('svy_question.question_id', $question_ids, false, 'integer') . " " .
1100  "ORDER BY svy_question.title");
1101  if ($query_result->numRows() > 0)
1102  {
1103  while ($data = $ilDB->fetchAssoc($query_result))
1104  {
1105  if (in_array($data["question_id"], $question_ids))
1106  {
1107  array_push($found, array('id' => $data["question_id"],
1108  'title' => $data["title"],
1109  'description' => $data["description"],
1110  'type_tag' => $data["type_tag"]));
1111  }
1112  }
1113  }
1114  return $found;
1115  }
1116 
1117  /*
1118  * Remove all questions with tstamp = 0
1119  */
1120  public function purgeQuestions()
1121  {
1122  global $ilDB, $ilUser;
1123 
1124  $result = $ilDB->queryF("SELECT question_id FROM svy_question WHERE owner_fi = %s AND tstamp = %s",
1125  array("integer", "integer"),
1126  array($ilUser->getId(), 0)
1127  );
1128  while ($data = $ilDB->fetchAssoc($result))
1129  {
1130  $this->removeQuestion($data["question_id"]);
1131  }
1132  }
1133 
1139  public function copyToClipboard($question_id)
1140  {
1141  if (!array_key_exists("spl_clipboard", $_SESSION))
1142  {
1143  $_SESSION["spl_clipboard"] = array();
1144  }
1145  $_SESSION["spl_clipboard"][$question_id] = array("question_id" => $question_id, "action" => "copy");
1146  }
1147 
1153  public function moveToClipboard($question_id)
1154  {
1155  if (!array_key_exists("spl_clipboard", $_SESSION))
1156  {
1157  $_SESSION["spl_clipboard"] = array();
1158  }
1159  $_SESSION["spl_clipboard"][$question_id] = array("question_id" => $question_id, "action" => "move");
1160  }
1161 
1165  public function pasteFromClipboard()
1166  {
1167  global $ilDB;
1168 
1169  if (array_key_exists("spl_clipboard", $_SESSION))
1170  {
1171  foreach ($_SESSION["spl_clipboard"] as $question_object)
1172  {
1173  if (strcmp($question_object["action"], "move") == 0)
1174  {
1175  $result = $ilDB->queryF("SELECT obj_fi FROM svy_question WHERE question_id = %s",
1176  array('integer'),
1177  array($question_object["question_id"])
1178  );
1179  if ($result->numRows() == 1)
1180  {
1181  $row = $ilDB->fetchAssoc($result);
1182  $source_questionpool = $row["obj_fi"];
1183  if ($this->getId() != $source_questionpool)
1184  {
1185  // change the questionpool id in the qpl_questions table
1186  $affectedRows = $ilDB->manipulateF("UPDATE svy_question SET obj_fi = %s WHERE question_id = %s",
1187  array('integer','integer'),
1188  array($this->getId(), $question_object["question_id"])
1189  );
1190 
1191  // move question data to the new target directory
1192  $source_path = CLIENT_WEB_DIR . "/survey/" . $source_questionpool . "/" . $question_object["question_id"] . "/";
1193  if (@is_dir($source_path))
1194  {
1195  $target_path = CLIENT_WEB_DIR . "/survey/" . $this->getId() . "/";
1196  if (!@is_dir($target_path))
1197  {
1198  include_once "./Services/Utilities/classes/class.ilUtil.php";
1199  ilUtil::makeDirParents($target_path);
1200  }
1201  @rename($source_path, $target_path . $question_object["question_id"]);
1202  }
1203  }
1204  else
1205  {
1206  ilUtil::sendFailure($this->lng->txt("spl_move_same_pool"), true);
1207  return;
1208  }
1209  }
1210  }
1211  else
1212  {
1213  $this->copyQuestion($question_object["question_id"], $this->getId());
1214  }
1215  }
1216  }
1217  ilUtil::sendSuccess($this->lng->txt("spl_paste_success"), true);
1218  unset($_SESSION["spl_clipboard"]);
1219  }
1220 
1227  function setObligatoryStates($obligatory_questions)
1228  {
1229  global $ilDB;
1230 
1231  foreach($this->getQuestions() as $question_id)
1232  {
1233  $status = (int)(in_array($question_id, $obligatory_questions));
1234 
1235  $ilDB->manipulate("UPDATE svy_question".
1236  " SET obligatory = ".$ilDB->quote($status, "integer").
1237  " WHERE question_id = ".$ilDB->quote($question_id, "integer"));
1238  }
1239  }
1240 } // END class.ilSurveyObjQuestionPool
1241 ?>