ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.assTextSubsetImport.php
Go to the documentation of this file.
1<?php
2
29{
30 public function fromXML(
31 string $importdirectory,
32 int $user_id,
33 ilQTIItem $item,
34 int $questionpool_id,
35 ?int $tst_id,
36 ?ilObject &$tst_object,
37 int &$question_counter,
38 array $import_mapping
39 ): array {
40 // empty session variable for imported xhtml mobs
41 ilSession::clear('import_mob_xhtml');
42 ;
43 $presentation = $item->getPresentation();
44 $shuffle = 0;
45 $idents = [];
46 $now = getdate();
47 $created = sprintf("%04d%02d%02d%02d%02d%02d", $now['year'], $now['mon'], $now['mday'], $now['hours'], $now['minutes'], $now['seconds']);
48 $gaps = [];
49 foreach ($presentation->order as $entry) {
50 switch ($entry["type"]) {
51 case "response":
52 $response = $presentation->response[$entry["index"]];
53 if ($response->getResponseType() == ilQTIResponse::RT_RESPONSE_STR) {
54 array_push($idents, $response->getIdent());
55 }
56 break;
57 }
58 }
59 $responses = [];
60 $feedbacksgeneric = [];
61 foreach ($item->resprocessing as $resprocessing) {
62 foreach ($resprocessing->respcondition as $respcondition) {
63 $ident = "";
64 $correctness = 1;
65 $conditionvar = $respcondition->getConditionvar();
66 foreach ($conditionvar->order as $order) {
67 switch ($order["field"]) {
68 case "varsubset":
69 $respident = $conditionvar->varsubset[$order["index"]]->getRespident();
70 $content = $conditionvar->varsubset[$order["index"]]->getContent();
71 if (!isset($responses[$respident]) || !is_array($responses[$respident])) {
72 $responses[$respident] = [];
73 }
74 $vars = explode(",", $content);
75 foreach ($vars as $var) {
76 array_push($responses[$respident], ["solution" => $var, "points" => ""]);
77 }
78 break;
79 }
80 }
81 foreach ($respcondition->setvar as $setvar) {
82 if ((strcmp($setvar->getVarname(), "matches") == 0) && ($setvar->getAction() == ilQTISetvar::ACTION_ADD)) {
83 foreach ($responses[$respident] as $idx => $solutionarray) {
84 if (strlen($solutionarray["points"]) == 0) {
85 $responses[$respident][$idx]["points"] = $setvar->getContent();
86 }
87 }
88 }
89 }
90 foreach ($resprocessing->respcondition as $e_respcondition) {
91 foreach ($e_respcondition->displayfeedback as $feedbackpointer) {
92 if (strlen($feedbackpointer->getLinkrefid())) {
93 foreach ($item->itemfeedback as $ifb) {
94 if (strcmp($ifb->getIdent(), "response_allcorrect") == 0) {
95 // found a feedback for the identifier
96 if (count($ifb->material)) {
97 foreach ($ifb->material as $material) {
98 $feedbacksgeneric[1] = $material;
99 }
100 }
101 if ((count($ifb->flow_mat) > 0)) {
102 foreach ($ifb->flow_mat as $fmat) {
103 if (count($fmat->material)) {
104 foreach ($fmat->material as $material) {
105 $feedbacksgeneric[1] = $material;
106 }
107 }
108 }
109 }
110 } elseif (strcmp($ifb->getIdent(), "response_onenotcorrect") == 0) {
111 // found a feedback for the identifier
112 if (count($ifb->material)) {
113 foreach ($ifb->material as $material) {
114 $feedbacksgeneric[0] = $material;
115 }
116 }
117 if ((count($ifb->flow_mat) > 0)) {
118 foreach ($ifb->flow_mat as $fmat) {
119 if (count($fmat->material)) {
120 foreach ($fmat->material as $material) {
121 $feedbacksgeneric[0] = $material;
122 }
123 }
124 }
125 }
126 }
127 }
128 }
129 }
130 }
131 }
132 }
133 $this->addGeneralMetadata($item);
134 $this->object->setTitle($item->getTitle());
135 $this->object->setNrOfTries((int) $item->getMaxattempts());
136 $this->object->setComment($item->getComment());
137 $this->object->setAuthor($item->getAuthor());
138 $this->object->setOwner($user_id);
139 $this->object->setQuestion($this->QTIMaterialToString($item->getQuestiontext()));
140 $this->object->setObjId($questionpool_id);
141 $textrating = $item->getMetadataEntry("textrating");
142 if (strlen($textrating) == 0) {
143 $textrating = "ci";
144 }
145 $this->object->setTextRating($textrating);
146 $this->object->setCorrectAnswers($item->getMetadataEntry("correctanswers"));
147 $response = current($responses);
148 $counter = 0;
149 if (is_array($response)) {
150 foreach ($response as $answer) {
151 $this->object->addAnswer($answer["solution"], $answer["points"], $counter);
152 $counter++;
153 }
154 }
155 // additional content editing mode information
156 $this->object->setAdditionalContentEditingMode(
158 );
159 $this->object->saveToDb();
160 $this->importSuggestedSolutions($this->object->getId(), $item->suggested_solutions);
161 foreach ($feedbacksgeneric as $correctness => $material) {
162 $m = $this->QTIMaterialToString($material);
163 $feedbacksgeneric[$correctness] = $m;
164 }
165 // handle the import of media objects in XHTML code
166 $questiontext = $this->object->getQuestion();
167 if (is_array(ilSession::get("import_mob_xhtml"))) {
168 foreach (ilSession::get("import_mob_xhtml") as $mob) {
169 $importfile = $importdirectory . DIRECTORY_SEPARATOR . $mob["uri"];
170
171 global $DIC; /* @var ILIAS\DI\Container $DIC */
172 $DIC['ilLog']->write(__METHOD__ . ': import mob from dir: ' . $importfile);
173
174 $media_object = ilObjMediaObject::_saveTempFileAsMediaObject(basename($importfile), $importfile, false);
175 ilObjMediaObject::_saveUsage($media_object->getId(), "qpl:html", $this->object->getId());
176 $questiontext = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $questiontext);
177 foreach ($feedbacksgeneric as $correctness => $material) {
178 $feedbacksgeneric[$correctness] = str_replace("src=\"" . $mob["mob"] . "\"", "src=\"" . "il_" . IL_INST_ID . "_mob_" . $media_object->getId() . "\"", $material);
179 }
180 }
181 }
182 $this->object->setQuestion(ilRTE::_replaceMediaObjectImageSrc($questiontext, 1));
183 foreach ($feedbacksgeneric as $correctness => $material) {
184 $this->object->feedbackOBJ->importGenericFeedback(
185 $this->object->getId(),
186 $correctness,
188 );
189 }
190 $this->object->saveToDb();
191 $import_mapping[$item->getIdent()] = $this->addQuestionToParentObjectAndBuildMappingEntry(
192 $questionpool_id,
193 $tst_id,
194 $question_counter,
195 $tst_object
196 );
197 return $import_mapping;
198 }
199}
Class for question imports.
fetchAdditionalContentEditingModeInformation($qtiItem)
fetches the "additional content editing mode" information from qti item and falls back to ADDITIONAL_...
addQuestionToParentObjectAndBuildMappingEntry(int $questionpool_id, ?int $tst_id, int &$question_counter, ?ilObjTest &$tst_object)
QTIMaterialToString(ilQTIMaterial $a_material)
Reads an QTI material tag and creates a text or XHTML string.
addGeneralMetadata(ilQTIItem $item)
importSuggestedSolutions(int $question_id, array $solution_from_import)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
fromXML(string $importdirectory, int $user_id, ilQTIItem $item, int $questionpool_id, ?int $tst_id, ?ilObject &$tst_object, int &$question_counter, array $import_mapping)
static _saveTempFileAsMediaObject(string $name, string $tmp_name, bool $upload=true)
static _saveUsage(int $a_mob_id, string $a_type, int $a_id, int $a_usage_hist_nr=0, string $a_lang="-")
Save usage of mob within another container (e.g.
Class ilObject Basic functions for all objects.
getMetadataEntry(string $a_label)
static _replaceMediaObjectImageSrc(string $a_text, int $a_direction=0, string $nic='')
Replaces image source from mob image urls with the mob id or replaces mob id with the correct image s...
static get(string $a_var)
const IL_INST_ID
Definition: constants.php:40
global $DIC
Definition: shib_login.php:26
$counter
$response
Definition: xapitoken.php:93