ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
class.ilPCAnswerForm.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22use ILIAS\Questions\AnswerForm\Properties as AnswerFormProperties;
28use ILIAS\Refinery\Factory as Refinery;
30use ILIAS\UI\Factory as UIFactory;
31use ILIAS\UI\Renderer as UIRenderer;
32use ILIAS\Data\UUID\Factory as UuidFactory;
33
35{
36 public const string ANSWER_FORM_ELEMENT_TAG = 'AnswerForm';
37 public const string ANSWER_FORM_ID_ATTRIBUTE = 'Uuid';
38 private const string ANSWER_FORM_PLACEHOLDER = '\[\[\[ANSWER_FORM_([0-9a-f\-]+)\]\]\]';
39
40 public function init(): void
41 {
42 $this->setType('answf');
43 }
44
45 #[\Override]
46 public static function getLangVars(): array
47 {
48 return ['ed_insert_pcqst', 'empty_question', 'pc_qst'];
49 }
50
51 #[\Override]
52 public function modifyPageContentPostXsl(
53 string $output,
54 string $mode,
55 bool $abstract_only = false
56 ): string {
57 if ($this->pg_obj::class !== QstsQuestionPage::class) {
58 return $output;
59 }
60
61 global $DIC;
62 $global_tpl = $DIC['tpl'];
63 $lng = $DIC['lng'];
64 $ui_factory = $DIC['ui.factory'];
65 $ui_renderer = $DIC['ui.renderer'];
66 $refinery = $DIC['refinery'];
67
68 return mb_ereg_replace_callback(
69 self::ANSWER_FORM_PLACEHOLDER,
70 fn(array $matches): string => $this->renderAnswerForm(
71 $global_tpl,
72 $lng,
73 $ui_factory,
76 $this->pg_obj->getQuestion()->getAnswerFormPropertiesByIdString($matches[1]),
77 $this->pg_obj->getAttemptData()
78 ),
79 $output
80 );
81 }
82
83 #[\Override]
84 public static function afterPageUpdate(
86 DOMDocument $domdoc,
87 string $xml,
88 bool $creation
89 ): void {
90 if ($page::class !== QstsQuestionPage::class || $creation) {
91 return;
92 }
93
94 global $DIC;
95 $dom_util = $DIC->copage()->internal()->domain()->domUtil();
97 $question_repository = LocalDIC::dic()[Repository::class];
98
99 $uuid_factory = new UuidFactory();
100
102 $question = $page->getQuestion();
103
104 $answer_form_mapping = \QstsQuestionPage::getAnswerFormMapping();
106
107 $answer_forms = [];
108 foreach ($dom_util->path($domdoc, '//AnswerForm') as $node) {
109 $answer_form_id = $node->getAttribute(self::ANSWER_FORM_ID_ATTRIBUTE);
110 $answer_form = $question->getAnswerFormPropertiesByIdString($answer_form_id);
111
112 if ($answer_form === null && in_array($answer_form_id, $answer_form_mapping)) {
113 $old_answer_form_id = array_search($answer_form_id, $answer_form_mapping);
114 $old_answer_form = $question->getAnswerFormPropertiesByIdString(
115 $old_answer_form_id
116 );
117
118 if ($old_answer_form === null) {
119 $old_answer_form_uuid = $uuid_factory->fromString($answer_form_id);
120 $old_answer_form = $question_repository->getQuestionForAnswerFormId(
121 $old_answer_form_uuid
122 )->getAnswerFormPropertiesByIdString(
123 $old_answer_form_uuid
124 );
125 }
126
127 $question = $question->withAnswerFormProperties(
128 $old_answer_form->clone(
129 $uuid_factory,
130 [
131 'new_question_id' => $question->getId(),
132 'answer_form_id' => $uuid_factory->fromString(
133 $answer_form_id
134 )
135 ]
136 )
137 );
138 }
139
140 $answer_forms[] = $answer_form_id;
141 }
142
143 $page->setQuestion($question->withoutDeletedAnswerForms($answer_forms));
144
145 $question_repository->update(
146 [$page->getQuestion()]
147 );
148 }
149
150 #[\Override]
151 public static function handleCopiedContent(
152 DOMDocument $domdoc,
153 bool $self_ass = true,
154 bool $clone_mobs = false,
155 int $new_parent_id = 0,
156 int $obj_copy_id = 0
157 ): void {
158 global $DIC;
159 $dom_util = $DIC->copage()->internal()->domain()->domUtil();
160
161 $answer_form_mapping = \QstsQuestionPage::getAnswerFormMapping();
162 if ($answer_form_mapping === []) {
163 return;
164 }
165
166 foreach ($dom_util->path($domdoc, '//AnswerForm') as $node) {
167 $old_answer_form_id = $node->getAttribute(self::ANSWER_FORM_ID_ATTRIBUTE);
168 if (isset($answer_form_mapping[$old_answer_form_id])) {
169 $node->setAttribute(
170 self::ANSWER_FORM_ID_ATTRIBUTE,
171 $answer_form_mapping[$old_answer_form_id]
172 );
173 }
174 }
175 }
176
177 public function create(
178 Uuid $answer_form_id
179 ): void {
180 $this->createInitialChildNode(
181 $this->hier_id,
182 '',
183 self::ANSWER_FORM_ELEMENT_TAG,
184 [self::ANSWER_FORM_ID_ATTRIBUTE => $answer_form_id->toString()]
185 );
186 }
187
188 public function getAnswerFormIdStringFromAttribute(): string
189 {
190 return $this->getChildNode()->attributes
191 ->getNamedItem(self::ANSWER_FORM_ID_ATTRIBUTE)->nodeValue;
192 }
193
194 private function renderAnswerForm(
195 GlobalTemplate $global_tpl,
197 UIFactory $ui_factory,
198 UIRenderer $ui_renderer,
199 Refinery $refinery,
200 ?AnswerFormProperties $answer_form_properties,
201 ?Attempt $attempt_data
202 ): string {
203 if ($answer_form_properties === null) {
204 return $lng->txt('broken_answer_form');
205 }
206
208 $required_capabilities = $this->pg_obj->getRequiredCapabilities();
209
210 $answer_form_response = $attempt_data?->getResponseForQuestion(
211 $answer_form_properties->getQuestionId()
212 )?->getAnswerFormResponse(
213 $answer_form_properties->getAnswerFormId()
214 );
215
216 return $ui_renderer->render(
217 $required_capabilities
218 ->getParticipantViewProvider()
219 ->getParticipantView($answer_form_properties)
220 ->show(
221 $global_tpl,
222 $lng,
223 $refinery,
224 $ui_factory,
225 $required_capabilities,
226 $this->pg_obj->getViewConfiguration(),
227 $answer_form_properties,
228 $attempt_data,
229 $answer_form_response
230 )
231 );
232 }
233}
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
Builds data types.
Definition: Factory.php:36
getResponseForQuestion(Uuid $question_id)
Definition: Attempt.php:81
static setAnswerFormMapping(array $answer_form_mapping)
static getAnswerFormMapping()
2026-06-30, sk:This is awfull, but as there are statics used in the copy process this is the only way...
modifyPageContentPostXsl(string $output, string $mode, bool $abstract_only=false)
Modify page content after xsl.
static getLangVars()
Get lang vars needed for editing.
const string ANSWER_FORM_PLACEHOLDER
static handleCopiedContent(DOMDocument $domdoc, bool $self_ass=true, bool $clone_mobs=false, int $new_parent_id=0, int $obj_copy_id=0)
Handle copied content.
const string ANSWER_FORM_ID_ATTRIBUTE
const string ANSWER_FORM_ELEMENT_TAG
create(Uuid $answer_form_id)
Content object of ilPageObject (see ILIAS DTD).
setType(string $a_type)
Set Type.
Class ilPageObject Handles PageObjects of ILIAS Learning Modules (see ILIAS DTD)
An entity that renders components to a string output.
Definition: Renderer.php:31
global $lng
Definition: privfeed.php:26
if(!file_exists('../ilias.ini.php'))
global $DIC
Definition: shib_login.php:26