ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilBulkEditQuestionsGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21use ILIAS\UI\Factory as UIFactory;
22use ILIAS\UI\Renderer as UIRenderer;
23use ILIAS\Refinery\Factory as Refinery;
25use Psr\Http\Message\ServerRequestInterface;
29
34{
35 public const PARAM_IDS = 'qids';
36 public const CMD_EDITTAUTHOR = 'bulkedit_author';
37 public const CMD_SAVEAUTHOR = 'bulksave_author';
38 public const CMD_EDITLIFECYCLE = 'bulkedit_lifecycle';
39 public const CMD_SAVELIFECYCLE = 'bulksave_lifecycle';
40 public const CMD_EDITTAXONOMIES = 'bulkedit_taxonomies';
41 public const CMD_SAVETAXONOMIES = 'bulksave_taxonomies';
42 public const CMD_SAVETAXONOMIESADD = 'bulksave_taxonomies_add';
43
44 public function __construct(
45 protected ilGlobalTemplateInterface $tpl,
46 protected ilCtrl $ctrl,
47 protected ilLanguage $lng,
48 protected UIFactory $ui_factory,
49 protected UIRenderer $ui_renderer,
50 protected Refinery $refinery,
51 protected ServerRequestInterface $request,
53 protected int $qpl_obj_id,
54 ) {
55 }
56
57 protected array $question_ids = [];
58
59 public function executeCommand(): void
60 {
61 $cmd = $this->ctrl->getCmd();
62 $this->ctrl->saveParameter($this, self::PARAM_IDS);
63
64 $this->question_ids = $this->getQuestionIds();
65
66 $out = [];
67
68 if ($this->question_ids === []) {
69 $out[] = $this->ui_factory->messageBox()->failure(
70 $this->lng->txt('qpl_bulkedit_no_ids')
71 );
72
73 } else {
74
75 switch ($cmd) {
77 $out[] = $this->getFormAuthor();
78 break;
80 $out = array_merge($out, $this->store(
81 $this->getFormAuthor(),
82 $this->getAuthorUpdater()
83 ));
84 break;
85
87 $out[] = $this->getFormLifecycle();
88 break;
90 $out = array_merge($out, $this->store(
91 $this->getFormLifecycle(),
92 $this->getLifecycleUpdater()
93 ));
94 break;
95
97 $out[] = $this->ui_factory->legacy()->content($this->getFormTaxonomies()->getHTML());
98 break;
101 $out = array_merge($out, $this->storeTaxonomies($this->getFormTaxonomies()));
102 break;
103
104 default:
105 throw new \Exception("'$cmd'" . " not implemented");
106 }
107 }
108
109 $this->tpl->setContent($this->ui_renderer->render($out));
110 }
111
112 protected function getQuestionIds(): array
113 {
114 if (!$this->request_wrapper->has(self::PARAM_IDS)) {
115 return [];
116 }
117 $trafo = $this->refinery->custom()->transformation(
118 fn($v) => array_map('intval', explode(',', $v))
119 );
120 return $this->request_wrapper->retrieve(self::PARAM_IDS, $trafo);
121 }
122
123 protected function store(Form\Standard $form, Closure $update): array
124 {
125 $out = [];
126 $form = $form->withRequest($this->request);
127 $data = $form->getData();
128 $questions = $this->getQuestions();
129 if ($data !== null && $update($questions, $data)) {
130 $this->tpl->setOnScreenMessage('success', $this->lng->txt('qpl_bulkedit_success'), true);
131 $this->ctrl->redirectByClass(ilObjQuestionPoolGUI::class, ilObjQuestionPoolGUI::DEFAULT_CMD);
132 }
133 $out[] = $form;
134 return $out;
135 }
136
137 protected function getQuestions(): array
138 {
139 $questions = [];
140 foreach ($this->question_ids as $qid) {
141 $questions[] = \assQuestion::instantiateQuestion($qid);
142 }
143 return $questions;
144 }
145
146 protected function getShiftTrafo(): Transformation
147 {
148 return $this->refinery->custom()->transformation(
149 fn(array $v) => array_shift($v)
150 );
151 }
152
153 protected function getFormAuthor(): Form\Standard
154 {
155 return $this->ui_factory->input()->container()->form()->standard(
156 $this->ctrl->getFormAction($this, self::CMD_SAVEAUTHOR),
157 [
158 $this->ui_factory->input()->field()
159 ->text($this->lng->txt('author'))
160 ->withRequired(true)
161 ]
162 )
164 }
165
166 protected function getAuthorUpdater(): \Closure
167 {
168 return function (array $questions, string $author) {
169 foreach ($questions as $q) {
170 $q->setAuthor($author);
171 $q->saveQuestionDataToDb();
172 }
173 return true;
174 };
175 }
176
177 protected function getFormLifecycle(): Form\Standard
178 {
180 $options = $lifecycle->getSelectOptions($this->lng);
181 return $this->ui_factory->input()->container()->form()->standard(
182 $this->ctrl->getFormAction($this, self::CMD_SAVELIFECYCLE),
183 [
184 $this->ui_factory->input()->field()
185 ->select($this->lng->txt('qst_lifecycle'), $options)
186 ->withRequired(true)
187 ]
188 )
189 ->withAdditionalTransformation($this->getShiftTrafo());
190 }
191
192 protected function getLifecycleUpdater(): \Closure
193 {
194 return function (array $questions, string $lifecycle) {
196 foreach ($questions as $q) {
197 $q->setLifecycle($lc);
198 $q->saveToDb();
199 }
200 return true;
201 };
202 }
203
205 {
206 $form = new ilPropertyFormGUI();
207 $form->setFormAction($this->ctrl->getFormAction($this, self::CMD_SAVETAXONOMIES));
208 $taxonomy_ids = \ilObjTaxonomy::getUsageOfObject($this->qpl_obj_id);
209
210 //taken from assQuestionGUI::populateTaxonomyFormSection
211 foreach ($taxonomy_ids as $taxonomy_id) {
212 $taxonomy = new ilObjTaxonomy($taxonomy_id);
213 $label = sprintf($this->lng->txt('qpl_qst_edit_form_taxonomy'), $taxonomy->getTitle());
214 $postvar = "tax_node_assign_$taxonomy_id";
215 // selector not working due to failing modals, actually:
216 // $taxSelect = new ilTaxSelectInputGUI($taxonomy->getId(), $postvar, true);
217 $taxSelect = new ilTaxAssignInputGUI($taxonomy->getId(), true, $label, $postvar, true);
218 $taxSelect->setTitle($label);
219 $form->addItem($taxSelect);
220 }
221 $form->addCommandButton(self::CMD_SAVETAXONOMIES, $this->lng->txt("qpl_bulk_save_overwrite"));
222 $form->addCommandButton(self::CMD_SAVETAXONOMIESADD, $this->lng->txt("qpl_bulk_save_add"));
223 return $form;
224 }
225
226 protected function storeTaxonomies(ilPropertyFormGUI $form): void
227 {
228 $questions = $this->getQuestions();
229 $post = $this->request->getParsedBody();
230 $form_cmd = array_shift($post['cmd']);
231 $overwrite = ($form_cmd === $this->lng->txt('qpl_bulk_save_overwrite'));
232
233 $taxonomy_ids = \ilObjTaxonomy::getUsageOfObject($this->qpl_obj_id);
234 foreach ($taxonomy_ids as $taxonomy_id) {
235 $postvar = "tax_node_assign_$taxonomy_id";
236 foreach ($questions as $q) {
237 $assignments = new ilTaxNodeAssignment(ilObject::_lookupType($q->getObjId()), $q->getObjId(), 'quest', $taxonomy_id);
238 $assigned_nodes = $assignments->getAssignmentsOfItem($q->getId());
239
240 $skip = [];
241 foreach ($assigned_nodes as $existing) {
242 if ($overwrite && !in_array($existing["node_id"], $post[$postvar])
243 ) {
244 $assignments->deleteAssignment((int) $existing["node_id"], $q->getId());
245 } else {
246 $skip[] = (int) $existing["node_id"];
247 }
248 }
249
250 $values = $post[$postvar];
251 foreach ($values as $value) {
252 if (!in_array((int) $value, $skip)) {
253 $assignments->addAssignment((int) $value, $q->getId());
254 }
255 }
256 }
257 }
258 $this->tpl->setOnScreenMessage('success', $this->lng->txt('qpl_bulkedit_success'), true);
259 $this->ctrl->redirectByClass(ilObjQuestionPoolGUI::class, ilObjQuestionPoolGUI::DEFAULT_CMD);
260 }
261
262}
$lifecycle
$out
Definition: buildRTE.php:24
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
Builds data types.
Definition: Factory.php:36
Transform values according to custom configuration.
static instantiateQuestion(int $question_id)
@ilCtrl_Calls ilBulkEditQuestionsGUI: ilFormPropertyDispatchGUI
store(Form\Standard $form, Closure $update)
__construct(protected ilGlobalTemplateInterface $tpl, protected ilCtrl $ctrl, protected ilLanguage $lng, protected UIFactory $ui_factory, protected UIRenderer $ui_renderer, protected Refinery $refinery, protected ServerRequestInterface $request, protected RequestWrapper $request_wrapper, protected int $qpl_obj_id,)
storeTaxonomies(ilPropertyFormGUI $form)
Class ilCtrl provides processing control methods.
language handling
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getUsageOfObject(int $a_obj_id, bool $a_include_titles=false)
static _lookupType(int $id, bool $reference=false)
This class represents a property form user interface.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Interface RequestWrapper.
withAdditionalTransformation(Transformation $trafo)
Apply a transformation to the data of the form.
This describes commonalities between all forms.
Definition: Form.php:33
This describes a standard form.
Definition: Standard.php:29
An entity that renders components to a string output.
Definition: Renderer.php:31
$post
Definition: ltitoken.php:46
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Factory.php:21
global $lng
Definition: privfeed.php:31
$q
Definition: shib_logout.php:23