ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilObjSurveyQuestionPool.php
Go to the documentation of this file.
1 <?php
2 
21 
28 {
31  protected ilObjUser $user;
32  public bool $online = false;
34  private \ilGlobalTemplateInterface $main_tpl;
35 
36  public function __construct(
37  int $a_id = 0,
38  bool $a_call_by_reference = true
39  ) {
40  global $DIC;
41  $this->main_tpl = $DIC->ui()->mainTemplate();
42 
43  $this->log = $DIC["ilLog"];
44  $this->db = $DIC->database();
45  $this->user = $DIC->user();
46  $this->component_repository = $DIC["component.repository"];
47  $this->type = "spl";
48  parent::__construct($a_id, $a_call_by_reference);
49  $this->edit_manager = $DIC->surveyQuestionPool()
50  ->internal()
51  ->domain()
52  ->editing();
53  $this->domain = $DIC->surveyQuestionPool()->internal()->domain();
54  }
55 
56  public function create($a_upload = false): int
57  {
58  $id = parent::create();
59  if (!$a_upload) {
60  $this->createMetaData();
61  }
62  return $id;
63  }
64 
65  public function update(): bool
66  {
67  $this->updateMetaData();
68  if (!parent::update()) {
69  return false;
70  }
71  return true;
72  }
73 
74  public function read(): void
75  {
76  parent::read();
77  $this->loadFromDb();
78  }
79 
80  public function cloneObject(int $target_id, int $copy_id = 0, bool $omit_tree = false): ?ilObject
81  {
82  $newObj = parent::cloneObject($target_id, $copy_id, $omit_tree);
83 
84  //copy online status if object is not the root copy object
85  $cp_options = ilCopyWizardOptions::_getInstance($copy_id);
86 
87  if (!$cp_options->isRootNode($this->getRefId())) {
88  $newObj->setOnline($this->getOnline());
89  }
90 
91  $newObj->saveToDb();
92  // clone the questions in the question pool
93  $questions = $this->getQuestions();
94  foreach ($questions as $question_id) {
95  $newObj->copyQuestion($question_id, $newObj->getId());
96  }
97 
98  // clone meta data
99  $md = new ilMD($this->getId(), 0, $this->getType());
100  $new_md = $md->cloneMD($newObj->getId(), 0, $newObj->getType());
101 
102  // update the metadata with the new title of the question pool
103  $newObj->updateMetaData();
104  return $newObj;
105  }
106 
110  public function createQuestion(
111  string $question_type,
112  int $question_id = -1
113  ): SurveyQuestionGUI {
114  if ((!$question_type) and ($question_id > 0)) {
115  $question_type = $this->getQuestiontype($question_id);
116  }
117 
118  $question_type_gui = $question_type . "GUI";
119  $question = new $question_type_gui();
120 
121  if ($question_id > 0) {
122  $question->object->loadFromDb($question_id);
123  }
124 
125  return $question;
126  }
127 
132  public function copyQuestion(
133  int $question_id,
134  int $questionpool_to
135  ): void {
136  $question_gui = $this->createQuestion("", $question_id);
137  if ($question_gui->object->getObjId() === $questionpool_to) {
138  // the question is copied into the same question pool
139  $this->duplicateQuestion($question_id);
140  } else {
141  // the question is copied into another question pool
142  $newtitle = $question_gui->object->getTitle();
143  if ($question_gui->object->questionTitleExists($question_gui->object->getTitle(), $questionpool_to)) {
144  $counter = 2;
145  while ($question_gui->object->questionTitleExists($question_gui->object->getTitle() . " ($counter)", $questionpool_to)) {
146  $counter++;
147  }
148  $newtitle = $question_gui->object->getTitle() . " ($counter)";
149  }
150  $question_gui->object->copyObject($this->getId(), $newtitle);
151  }
152  }
153 
154  public function loadFromDb(): void
155  {
156  $ilDB = $this->db;
157 
158  $result = $ilDB->queryF(
159  "SELECT isonline FROM svy_qpl WHERE obj_fi = %s",
160  array('integer'),
161  array($this->getId())
162  );
163  if ($result->numRows() === 1) {
164  $row = $ilDB->fetchAssoc($result);
165  $this->setOnline((bool) $row["isonline"]);
166  }
167  }
168 
169  public function saveToDb(): void
170  {
171  $ilDB = $this->db;
172 
173  parent::update();
174 
175  $result = $ilDB->queryF(
176  "SELECT * FROM svy_qpl WHERE obj_fi = %s",
177  array('integer'),
178  array($this->getId())
179  );
180  if ($result->numRows() === 1) {
181  $affectedRows = $ilDB->manipulateF(
182  "UPDATE svy_qpl SET isonline = %s, tstamp = %s WHERE obj_fi = %s",
183  array('text','integer','integer'),
184  array($this->getOnline(), time(), $this->getId())
185  );
186  } else {
187  $next_id = $ilDB->nextId('svy_qpl');
188  $query = $ilDB->manipulateF(
189  "INSERT INTO svy_qpl (id_questionpool, isonline, obj_fi, tstamp) VALUES (%s, %s, %s, %s)",
190  array('integer', 'text', 'integer', 'integer'),
191  array($next_id, $this->getOnline(), $this->getId(), time())
192  );
193  }
194  }
195 
196  public function delete(): bool
197  {
198  $remove = parent::delete();
199  // always call parent delete function first!!
200  if (!$remove) {
201  return false;
202  }
203 
204  // delete all related questions
205  $this->deleteAllData();
206 
207  // delete meta data
208  $this->deleteMetaData();
209 
210  return true;
211  }
212 
213  public function deleteAllData(): void
214  {
215  $ilDB = $this->db;
216  $result = $ilDB->queryF(
217  "SELECT question_id FROM svy_question WHERE obj_fi = %s AND original_id IS NULL",
218  array('integer'),
219  array($this->getId())
220  );
221  $found_questions = array();
222  while ($row = $ilDB->fetchAssoc($result)) {
223  $this->removeQuestion($row["question_id"]);
224  }
225 
226  // delete export files
227  $spl_data_dir = ilFileUtils::getDataDir() . "/spl_data";
228  $directory = $spl_data_dir . "/spl_" . $this->getId();
229  if (is_dir($directory)) {
230  ilFileUtils::delDir($directory);
231  }
232  }
233 
237  public function removeQuestion(int $question_id): void
238  {
239  if ($question_id < 1) {
240  return;
241  }
242  $question = SurveyQuestion::_instanciateQuestion($question_id);
243  $question->delete($question_id);
244  }
245 
249  public function getQuestiontype(
250  int $question_id
251  ): ?string {
252  $ilDB = $this->db;
253  if ($question_id < 1) {
254  return null;
255  }
256  $result = $ilDB->queryF(
257  "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",
258  array('integer'),
259  array($question_id)
260  );
261  if ($result->numRows() === 1) {
262  $data = $ilDB->fetchAssoc($result);
263  return $data["type_tag"];
264  } else {
265  return null;
266  }
267  }
268 
274  public function isInUse(int $question_id): ?array
275  {
276  $ilDB = $this->db;
277  // check out the already answered questions
278  $result = $ilDB->queryF(
279  "SELECT answer_id FROM svy_answer WHERE question_fi = %s",
280  array('integer'),
281  array($question_id)
282  );
283  $answered = $result->numRows();
284 
285  // check out the questions inserted in surveys
286  $result = $ilDB->queryF(
287  "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",
288  array('integer'),
289  array($question_id)
290  );
291  $inserted = $result->numRows();
292  if (($inserted + $answered) === 0) {
293  return null;
294  }
295  $result_array = array();
296  while ($row = $ilDB->fetchObject($result)) {
297  $result_array[] = $row;
298  }
299  return $result_array;
300  }
301 
305  public function paste(int $question_id): void
306  {
307  $this->duplicateQuestion($question_id, $this->getId());
308  }
309 
314  public function getQuestionsInfo(
315  array $question_array
316  ): array {
317  $ilDB = $this->db;
318  $result_array = array();
319  $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'));
320  while ($row = $ilDB->fetchAssoc($result)) {
321  if ($row["plugin"]) {
322  continue;
323  } else {
324  $result_array[] = $row;
325  }
326  }
327  return $result_array;
328  }
329 
333  public function duplicateQuestion(
334  int $question_id,
335  int $obj_id = 0
336  ): void {
337  $ilUser = $this->user;
338  $question = SurveyQuestion::_instanciateQuestion($question_id);
339  $suffix = "";
340  $counter = 1;
341  while ($question->questionTitleExists($question->getTitle() . $suffix, $obj_id)) {
342  $counter++;
343  if ($counter > 1) {
344  $suffix = " ($counter)";
345  }
346  }
347  if ($obj_id) {
348  $question->setObjId($obj_id);
349  }
350  $max_len = 195;
351  if (strlen($question->getTitle() . $suffix) > $max_len) {
352  $title = substr($question->getTitle(), 0, $max_len - strlen($suffix)) . $suffix;
353  } else {
354  $title = $question->getTitle() . $suffix;
355  }
356  $question->duplicate(false, $title, $ilUser->fullname, $ilUser->id);
357  }
358 
363  public function getQuestionsData(
364  array $arrFilter
365  ): array {
366  $ilDB = $this->db;
367  $where = "";
368  if (count($arrFilter) > 0) {
369  foreach ($arrFilter as $key => $value) {
370  $arrFilter[$key] = str_replace('%', '', $value);
371  }
372  if (array_key_exists('title', $arrFilter) && strlen($arrFilter['title'] ?? "")) {
373  $where .= " AND " . $ilDB->like('svy_question.title', 'text', "%%" . $arrFilter['title'] . "%%");
374  }
375  if (array_key_exists('description', $arrFilter) && strlen($arrFilter['description'] ?? "")) {
376  $where .= " AND " . $ilDB->like('svy_question.description', 'text', "%%" . $arrFilter['description'] . "%%");
377  }
378  if (array_key_exists('author', $arrFilter) && strlen($arrFilter['author'] ?? "")) {
379  $where .= " AND " . $ilDB->like('svy_question.author', 'text', "%%" . $arrFilter['author'] . "%%");
380  }
381  if (array_key_exists('type', $arrFilter) && strlen($arrFilter['type'] ?? "")) {
382  $where .= " AND svy_qtype.type_tag = " . $ilDB->quote($arrFilter['type'], 'text');
383  }
384  }
385  $query_result = $ilDB->queryF(
386  "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,
387  array('integer'),
388  array($this->getId())
389  );
390  $rows = array();
391  if ($query_result->numRows()) {
392  while ($row = $ilDB->fetchAssoc($query_result)) {
393  if ($row["plugin"]) {
394  continue;
395  } else {
396  $rows[] = $row;
397  }
398  }
399  }
400  return $rows;
401  }
402 
408  public function createExportDirectory(): void
409  {
410  $spl_data_dir = ilFileUtils::getDataDir() . "/spl_data";
411  ilFileUtils::makeDir($spl_data_dir);
412  if (!is_writable($spl_data_dir)) {
413  throw new ilSurveyException("Survey Questionpool Data Directory (" . $spl_data_dir . ") not writeable.");
414  }
415 
416  // create learning module directory (data_dir/lm_data/lm_<id>)
417  $spl_dir = $spl_data_dir . "/spl_" . $this->getId();
418  ilFileUtils::makeDir($spl_dir);
419  if (!is_dir($spl_dir)) {
420  throw new ilSurveyException("Creation of Survey Questionpool Directory failed.");
421  }
422  // create Export subdirectory (data_dir/lm_data/lm_<id>/Export)
423  $export_dir = $spl_dir . "/export";
424  ilFileUtils::makeDir($export_dir);
425  if (!is_dir($export_dir)) {
426  throw new ilSurveyException("Creation of Survey Questionpool Export Directory failed.");
427  }
428  }
429 
433  public function getExportDirectory(): string
434  {
435  $export_dir = ilFileUtils::getDataDir() . "/spl_data" . "/spl_" . $this->getId() . "/export";
436  return $export_dir;
437  }
438 
442  public function getExportFiles(string $dir): array
443  {
444  // quit if import dir not available
445  if (!is_dir($dir) or
446  !is_writable($dir)) {
447  return array();
448  }
449 
450  // open directory
451  $dir = dir($dir);
452 
453  // initialize array
454  $file = array();
455 
456  // get files and save the in the array
457  while ($entry = $dir->read()) {
458  if ($entry !== "." &&
459  $entry !== ".." &&
460  preg_match("/^[0-9]{10}__[0-9]+__(spl_)*[0-9]+\.[A-Za-z]{3}$/", $entry)) {
461  $file[] = $entry;
462  }
463  }
464 
465  // close import directory
466  $dir->close();
467  // sort files
468  sort($file);
469 
470  return $file;
471  }
472 
478  public function createImportDirectory(): void
479  {
480  $spl_data_dir = ilFileUtils::getDataDir() . "/spl_data";
481  ilFileUtils::makeDir($spl_data_dir);
482 
483  if (!is_writable($spl_data_dir)) {
484  throw new ilSurveyException("Survey Questionpool Data Directory (" . $spl_data_dir . ") not writeable.");
485  }
486 
487  // create test directory (data_dir/spl_data/spl_<id>)
488  $spl_dir = $spl_data_dir . "/spl_" . $this->getId();
489  ilFileUtils::makeDir($spl_dir);
490  if (!is_dir($spl_dir)) {
491  throw new ilSurveyException("Creation of Survey Questionpool Directory failed.");
492  }
493 
494  // create import subdirectory (data_dir/spl_data/spl_<id>/import)
495  $import_dir = $spl_dir . "/import";
496  ilFileUtils::makeDir($import_dir);
497  if (!is_dir($import_dir)) {
498  throw new ilSurveyException("Creation of Survey Questionpool Import Directory failed.");
499  }
500  }
501 
502  public function getImportDirectory(): string
503  {
504  return ilFileUtils::getDataDir() . "/spl_data" .
505  "/spl_" . $this->getId() . "/import";
506  }
507 
512  public function toXML(?array $questions): string
513  {
514  if (is_null($questions) || count($questions) === 0) {
515  $questions = $this->getQuestions();
516  }
517  $a_xml_writer = new ilXmlWriter();
518  // set xml header
519  $a_xml_writer->xmlHeader();
520  $attrs = array(
521  "xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance",
522  "xsi:noNamespaceSchemaLocation" => "https://www.ilias.de/download/xsd/ilias_survey_4_2.xsd"
523  );
524  $a_xml_writer->xmlStartTag("surveyobject", $attrs);
525  $attrs = array(
526  "id" => "qpl_" . $this->getId(),
527  "label" => $this->getTitle(),
528  "online" => $this->getOnline()
529  );
530  $a_xml_writer->xmlStartTag("surveyquestions", $attrs);
531  $a_xml_writer->xmlElement("dummy", null, "dummy");
532  // add ILIAS specific metadata
533  $a_xml_writer->xmlStartTag("metadata");
534  $a_xml_writer->xmlStartTag("metadatafield");
535  $a_xml_writer->xmlElement("fieldlabel", null, "SCORM");
536  $md = new ilMD($this->getId(), 0, $this->getType());
537  $writer = new ilXmlWriter();
538  $md->toXML($writer);
539  $metadata = $writer->xmlDumpMem();
540  $a_xml_writer->xmlElement("fieldentry", null, $metadata);
541  $a_xml_writer->xmlEndTag("metadatafield");
542  $a_xml_writer->xmlEndTag("metadata");
543 
544  $a_xml_writer->xmlEndTag("surveyquestions");
545  $a_xml_writer->xmlEndTag("surveyobject");
546 
547  $xml = $a_xml_writer->xmlDumpMem(false);
548 
549  $questionxml = "";
550  foreach ($questions as $key => $value) {
551  $questiontype = $this->getQuestiontype($value);
552  SurveyQuestion::_includeClass($questiontype);
553  $question = new $questiontype();
554  $question->loadFromDb($value);
555  $questionxml .= $question->toXML(false);
556  }
557 
558  $xml = str_replace("<dummy>dummy</dummy>", $questionxml, $xml);
559  return $xml;
560  }
561 
562  public function getQuestions(): array
563  {
564  $ilDB = $this->db;
565  $questions = array();
566  $result = $ilDB->queryF(
567  "SELECT question_id FROM svy_question WHERE obj_fi = %s AND svy_question.tstamp > 0 AND original_id IS NULL",
568  array('integer'),
569  array($this->getId())
570  );
571  if ($result->numRows()) {
572  while ($row = $ilDB->fetchAssoc($result)) {
573  $questions[] = $row["question_id"];
574  }
575  }
576  return $questions;
577  }
578 
584  public function importObject(
585  string $source,
586  bool $spl_exists = false
587  ): void {
588  if (is_file($source)) {
589  $isZip = (strcmp(strtolower(substr($source, -3)), 'zip') === 0);
590  if ($isZip) {
591  // unzip file
592  $this->domain->resources()->zip()->unzipFile($source);
593 
594  // determine filenames of xml files
595  $subdir = basename($source, ".zip");
596  $source = dirname($source) . "/" . $subdir . "/" . $subdir . ".xml";
597  }
598 
599  $fh = fopen($source, 'rb') or die("");
600  $xml = fread($fh, filesize($source));
601  fclose($fh) or die("");
602  if ($isZip) {
603  $subdir = basename($source, ".zip");
604  if (is_dir(dirname($source) . "/" . $subdir)) {
605  ilFileUtils::delDir(dirname($source) . "/" . $subdir);
606  }
607  }
608  if (strpos($xml, "questestinterop") > 0) {
609  throw new ilInvalidSurveyImportFileException("Unsupported survey version (< 3.8) found.");
610  }
611 
612  // survey questions for ILIAS >= 3.8
613  $import = new SurveyImportParser($this->getId(), "", $spl_exists);
614  $import->setXMLContent($xml);
615  $import->startParsing();
616  }
617  }
618 
619  public static function _setOnline(
620  int $a_obj_id,
621  bool $a_online_status
622  ): void {
623  global $DIC;
624 
625  $status = (string) (int) $a_online_status;
626  $db = $DIC->database();
627 
628  $db->manipulateF(
629  "UPDATE svy_qpl SET isonline = %s WHERE obj_fi = %s",
630  array('text','integer'),
631  array($status, $a_obj_id)
632  );
633  }
634 
635  public function setOnline(bool $a_online_status): void
636  {
637  $this->online = $a_online_status;
638  }
639 
640  public function getOnline(): bool
641  {
642  return $this->online;
643  }
644 
645  public static function _lookupOnline(int $a_obj_id): bool
646  {
647  global $DIC;
648 
649  $ilDB = $DIC->database();
650 
651  $result = $ilDB->queryF(
652  "SELECT isonline FROM svy_qpl WHERE obj_fi = %s",
653  array('integer'),
654  array($a_obj_id)
655  );
656  if ($row = $ilDB->fetchAssoc($result)) {
657  return (bool) $row["isonline"];
658  }
659  return false;
660  }
661 
666  public static function _isWriteable(
667  int $object_id
668  ): bool {
669  global $DIC;
670 
671  $rbacsystem = $DIC->rbac()->system();
672  $refs = ilObject::_getAllReferences($object_id);
673  $result = false;
674  foreach ($refs as $ref) {
675  if ($rbacsystem->checkAccess("write", $ref) && (ilObject::_hasUntrashedReference($object_id))) {
676  $result = true;
677  }
678  }
679  return $result;
680  }
681 
686  public static function _getQuestiontypes(): array
687  {
688  global $DIC;
689 
690  $ilDB = $DIC->database();
691  $lng = $DIC->language();
692 
693  $lng->loadLanguageModule("survey");
694  $types = array();
695  $query_result = $ilDB->query("SELECT * FROM svy_qtype ORDER BY type_tag");
696  while ($row = $ilDB->fetchAssoc($query_result)) {
697  //array_push($questiontypes, $row["type_tag"]);
698  if ((int) $row["plugin"] === 0) {
699  $types[$lng->txt($row["type_tag"])] = $row;
700  }
701  }
702  ksort($types);
703 
704 
705  // #14263 - default sorting
706 
707  $default_sorting = array_flip(array(
708  "SurveySingleChoiceQuestion",
709  "SurveyMultipleChoiceQuestion",
710  "SurveyMatrixQuestion",
711  "SurveyMetricQuestion",
712  "SurveyTextQuestion"
713  ));
714 
715  $sorted = array();
716  $idx = count($default_sorting);
717  foreach ($types as $caption => $item) {
718  $type = $item["type_tag"];
719  $item["caption"] = $caption;
720 
721  // default
722  if (array_key_exists($type, $default_sorting)) {
723  $sorted[$default_sorting[$type]] = $item;
724  }
725  // plugin (append alphabetically sorted)
726  else {
727  $sorted[$idx] = $item;
728  $idx++;
729  }
730  }
731  ksort($sorted);
732 
733  // redo captions as index
734  $types = array();
735  foreach ($sorted as $item) {
736  $types[$item["caption"]] = $item;
737  }
738 
739  return $types;
740  }
741 
742  public static function _getQuestionClasses(): array
743  {
744  $classes = array_map(
745  static function (array $c): string {
746  return $c["type_tag"];
747  },
748  self::_getQuestiontypes()
749  );
750  return $classes;
751  }
752 
756  public static function _getQuestionTypeTranslations(): array
757  {
758  global $DIC;
759 
760  $ilDB = $DIC->database();
761  $lng = $DIC->language();
762 
763  $component_factory = $DIC["component.factory"];
764 
765  $lng->loadLanguageModule("survey");
766  $result = $ilDB->query("SELECT * FROM svy_qtype");
767  $types = array();
768  while ($row = $ilDB->fetchAssoc($result)) {
769  if ((int) $row["plugin"] === 0) {
770  $types[$row['type_tag']] = $lng->txt($row["type_tag"]);
771  }
772  }
773  ksort($types);
774  return $types;
775  }
776 
781  public static function _getAvailableQuestionpools(
782  bool $use_object_id = false,
783  bool $could_be_offline = false,
784  bool $showPath = false,
785  string $permission = "read"
786  ): array {
787  global $DIC;
788 
789  $ilUser = $DIC->user();
790  global $DIC;
791 
792  $ilDB = $DIC->database();
793 
794  $result_array = array();
795  $qpls = ilUtil::_getObjectsByOperations("spl", $permission, $ilUser->getId(), -1);
796  $titles = ilObject::_prepareCloneSelection($qpls, "spl", $showPath);
797  $allqpls = array();
798  $result = $ilDB->query("SELECT obj_fi, isonline FROM svy_qpl");
799  while ($row = $ilDB->fetchAssoc($result)) {
800  $allqpls[$row['obj_fi']] = $row['isonline'];
801  }
802  foreach ($qpls as $ref_id) {
803  $obj_id = ilObject::_lookupObjectId($ref_id);
804  if ($could_be_offline || ($allqpls[$obj_id] ?? 0) == 1) {
805  if ($use_object_id) {
806  $result_array[$obj_id] = $titles[$ref_id];
807  } else {
808  $result_array[(int) $ref_id] = $titles[$ref_id];
809  }
810  }
811  }
812  return $result_array;
813  }
814 
821  public function getQuestionInfos(array $question_ids): array
822  {
823  $ilDB = $this->db;
824 
825  $found = array();
826  $query_result = $ilDB->query("SELECT svy_question.*, svy_qtype.type_tag FROM svy_question, svy_qtype " .
827  "WHERE svy_question.questiontype_fi = svy_qtype.questiontype_id " .
828  "AND svy_question.tstamp > 0 AND " . $ilDB->in('svy_question.question_id', $question_ids, false, 'integer') . " " .
829  "ORDER BY svy_question.title");
830  if ($query_result->numRows() > 0) {
831  while ($data = $ilDB->fetchAssoc($query_result)) {
832  if (in_array($data["question_id"], $question_ids)) {
833  $found[] = array('id' => $data["question_id"],
834  'title' => $data["title"],
835  'description' => $data["description"],
836  'type_tag' => $data["type_tag"]
837  );
838  }
839  }
840  }
841  return $found;
842  }
843 
847  public function purgeQuestions(): void
848  {
849  $ilDB = $this->db;
850  $ilUser = $this->user;
851 
852  $result = $ilDB->queryF(
853  "SELECT question_id FROM svy_question WHERE owner_fi = %s AND tstamp = %s",
854  array("integer", "integer"),
855  array($ilUser->getId(), 0)
856  );
857  while ($data = $ilDB->fetchAssoc($result)) {
858  $this->removeQuestion($data["question_id"]);
859  }
860  }
861 
866  public function copyToClipboard(
867  int $question_id
868  ): void {
869  $this->edit_manager->addQuestionToClipboard($question_id, "copy");
870  }
871 
875  public function moveToClipboard(
876  int $question_id
877  ): void {
878  $this->edit_manager->addQuestionToClipboard($question_id, "move");
879  }
880 
884  public function pasteFromClipboard(): void
885  {
886  $ilDB = $this->db;
887 
888  $qentries = $this->edit_manager->getQuestionsFromClipboard();
889  if (count($qentries) > 0) {
890  foreach ($qentries as $question_object) {
891  if (strcmp($question_object["action"], "move") === 0) {
892  $result = $ilDB->queryF(
893  "SELECT obj_fi FROM svy_question WHERE question_id = %s",
894  array('integer'),
895  array($question_object["question_id"])
896  );
897  if ($result->numRows() === 1) {
898  $row = $ilDB->fetchAssoc($result);
899  $source_questionpool = $row["obj_fi"];
900  if ($this->getId() != $source_questionpool) {
901  // change the questionpool id in the qpl_questions table
902  $affectedRows = $ilDB->manipulateF(
903  "UPDATE svy_question SET obj_fi = %s WHERE question_id = %s",
904  array('integer','integer'),
905  array($this->getId(), $question_object["question_id"])
906  );
907 
908  // move question data to the new target directory
909  $source_path = CLIENT_WEB_DIR . "/survey/" . $source_questionpool . "/" . $question_object["question_id"] . "/";
910  if (is_dir($source_path)) {
911  $target_path = CLIENT_WEB_DIR . "/survey/" . $this->getId() . "/";
912  if (!is_dir($target_path)) {
913  ilFileUtils::makeDirParents($target_path);
914  }
915  ilFileUtils::rename($source_path, $target_path . $question_object["question_id"]);
916  }
917  } else {
918  $this->main_tpl->setOnScreenMessage('failure', $this->lng->txt("spl_move_same_pool"), true);
919  return;
920  }
921  }
922  } else {
923  $this->copyQuestion($question_object["question_id"], $this->getId());
924  }
925  }
926  }
927  $this->main_tpl->setOnScreenMessage('success', $this->lng->txt("spl_paste_success"), true);
928  $this->edit_manager->clearClipboardQuestions();
929  }
930 
934  public function setObligatoryStates(
935  array $obligatory_questions
936  ): void {
937  $ilDB = $this->db;
938  foreach ($this->getQuestions() as $question_id) {
939  $status = (int) (isset($obligatory_questions["$question_id"]));
940 
941  $ilDB->manipulate("UPDATE svy_question" .
942  " SET obligatory = " . $ilDB->quote($status, "integer") .
943  " WHERE question_id = " . $ilDB->quote($question_id, "integer"));
944  }
945  }
946 }
removeQuestion(int $question_id)
Removes a question from the question pool.
string $title
cloneObject(int $target_id, int $copy_id=0, bool $omit_tree=false)
Readable part of repository interface to ilComponentDataDB.
string $type
manipulateF(string $query, array $types, array $values)
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
Class ilObjSurveyQuestionPool.
getQuestionInfos(array $question_ids)
Returns title, description and type for an array of question id&#39;s.
__construct(int $a_id=0, bool $a_call_by_reference=true)
static _getAllReferences(int $id)
get all reference ids for object ID
createImportDirectory()
creates data directory for import files (data_dir/spl_data/spl_<id>/import
getExportDirectory()
get export directory of survey
duplicateQuestion(int $question_id, int $obj_id=0)
Duplicates a question for a question pool.
copyQuestion(int $question_id, int $questionpool_to)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _hasUntrashedReference(int $obj_id)
checks whether an object has at least one reference that is not in trash
static _getAvailableQuestionpools(bool $use_object_id=false, bool $could_be_offline=false, bool $showPath=false, string $permission="read")
Returns the available question pools for the active user.
isInUse(int $question_id)
Checks if a question is in use by a survey.
loadLanguageModule(string $a_module)
Load language module.
static _setOnline(int $a_obj_id, bool $a_online_status)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
purgeQuestions()
Remove all questions with tstamp = 0.
ilGlobalTemplateInterface $main_tpl
static makeDirParents(string $a_dir)
Create a new directory and all parent directories.
createQuestion(string $question_type, int $question_id=-1)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _isWriteable(int $object_id)
Returns true, if the question pool is writeable for the current user.
global $DIC
Definition: feed.php:28
moveToClipboard(int $question_id)
Moves a question to the clipboard.
__construct(VocabulariesInterface $vocabularies)
static _prepareCloneSelection(array $ref_ids, string $new_type, bool $show_path=true)
Prepare copy wizard object selection.
ilLanguage $lng
static delDir(string $a_dir, bool $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
ilDBInterface $db
string $key
Consumer key/client ID value.
Definition: System.php:193
static _lookupObjectId(int $ref_id)
static _includeClass(string $question_type, int $gui=0)
Include the php class file for a given question type.
const CLIENT_WEB_DIR
Definition: constants.php:47
static _getObjectsByOperations( $a_obj_type, string $a_operation, int $a_usr_id=0, int $limit=0)
Get all objects of a specific type and check access This function is not recursive, instead it parses the serialized rbac_pa entries.
static getDataDir()
get data directory (outside webspace)
importObject(string $source, bool $spl_exists=false)
Imports survey questions into ILIAS.
pasteFromClipboard()
Copies/Moves a question from the clipboard.
paste(int $question_id)
Pastes a duplicate of a question in the question pool.
getQuestionsData(array $arrFilter)
Retrieve the data for the output of the question pool.
getExportFiles(string $dir)
get export files
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _getQuestiontypes()
Get all available question types.
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
setObligatoryStates(array $obligatory_questions)
copyToClipboard(int $question_id)
Copies a question to the clipboard.
static rename(string $a_source, string $a_target)
static _getInstance(int $a_copy_id)
toXML(?array $questions)
export questions to xml
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _instanciateQuestion(int $question_id)
Get question object.
Basic class for all survey question types The SurveyQuestionGUI class defines and encapsulates basic ...
createExportDirectory()
creates data directory for export files data_dir/spl_data/spl_<id>/export
ilComponentRepository $component_repository
static makeDir(string $a_dir)
creates a new directory and inherits all filesystem permissions of the parent directory You may pass ...