ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
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;
28
33{
34 public const PARAM_IDS = 'qids';
35 public const CMD_EDITTAUTHOR = 'bulkedit_author';
36 public const CMD_SAVEAUTHOR = 'bulksave_author';
37 public const CMD_EDITLIFECYCLE = 'bulkedit_lifecycle';
38 public const CMD_SAVELIFECYCLE = 'bulksave_lifecycle';
39 public const CMD_EDITTAXONOMIES = 'bulkedit_taxonomies';
40 public const CMD_SAVETAXONOMIES = 'bulksave_taxonomies';
41 public const CMD_SAVETAXONOMIESADD = 'bulksave_taxonomies_add';
42
43 public function __construct(
44 protected ilGlobalTemplateInterface $tpl,
45 protected ilCtrl $ctrl,
46 protected ilLanguage $lng,
47 protected UIFactory $ui_factory,
48 protected UIRenderer $ui_renderer,
49 protected Refinery $refinery,
50 protected ServerRequestInterface $request,
52 protected int $qpl_obj_id,
53 ) {
54 }
55
56 protected array $question_ids = [];
57
58 public function executeCommand(): void
59 {
60 $cmd = $this->ctrl->getCmd();
61 $this->ctrl->saveParameter($this, self::PARAM_IDS);
62
63 $this->question_ids = $this->getQuestionIds();
64
65 $out = [];
66
67 if ($this->question_ids === []) {
68 $out[] = $this->ui_factory->messageBox()->failure(
69 $this->lng->txt('qpl_bulkedit_no_ids')
70 );
71
72 } else {
73
74 switch ($cmd) {
76 $out[] = $this->getFormAuthor();
77 break;
79 $out = array_merge($out, $this->store(
80 $this->getFormAuthor(),
81 $this->getAuthorUpdater()
82 ));
83 break;
84
86 $out[] = $this->getFormLifecycle();
87 break;
89 $out = array_merge($out, $this->store(
90 $this->getFormLifecycle(),
91 $this->getLifecycleUpdater()
92 ));
93 break;
94
96 $out[] = $this->ui_factory->legacy()->content($this->getFormTaxonomies()->getHTML());
97 break;
100 $out = array_merge($out, $this->storeTaxonomies($this->getFormTaxonomies()));
101 break;
102
103 default:
104 throw new \Exception("'$cmd'" . " not implemented");
105 }
106 }
107
108 $this->tpl->setContent($this->ui_renderer->render($out));
109 }
110
111 protected function getQuestionIds(): array
112 {
113 if (!$this->request_wrapper->has(self::PARAM_IDS)) {
114 return [];
115 }
116 $trafo = $this->refinery->custom()->transformation(
117 fn($v) => array_map('intval', explode(',', $v))
118 );
119 return $this->request_wrapper->retrieve(self::PARAM_IDS, $trafo);
120 }
121
122 protected function store(Form\Standard $form, Closure $update): array
123 {
124 $out = [];
125 $form = $form->withRequest($this->request);
126 $data = $form->getData();
127 $questions = $this->getQuestions();
128 if ($data !== null && $update($questions, $data)) {
129 $this->tpl->setOnScreenMessage('success', $this->lng->txt('qpl_bulkedit_success'), true);
130 $this->ctrl->redirectByClass(ilObjQuestionPoolGUI::class, ilObjQuestionPoolGUI::DEFAULT_CMD);
131 }
132 $out[] = $form;
133 return $out;
134 }
135
136 protected function getQuestions(): array
137 {
138 $questions = [];
139 foreach ($this->question_ids as $qid) {
140 $questions[] = \assQuestion::instantiateQuestion($qid);
141 }
142 return $questions;
143 }
144
145 protected function getShiftTrafo(): Transformation
146 {
147 return $this->refinery->custom()->transformation(
148 fn(array $v) => array_shift($v)
149 );
150 }
151
152 protected function getFormAuthor(): Form\Standard
153 {
154 return $this->ui_factory->input()->container()->form()->standard(
155 $this->ctrl->getFormAction($this, self::CMD_SAVEAUTHOR),
156 [
157 $this->ui_factory->input()->field()
158 ->text($this->lng->txt('author'))
159 ->withRequired(true)
160 ]
161 )
163 }
164
165 protected function getAuthorUpdater(): \Closure
166 {
167 return function (array $questions, string $author) {
168 foreach ($questions as $q) {
169 $q->setAuthor($author);
170 $q->saveQuestionDataToDb();
171 }
172 return true;
173 };
174 }
175
176 protected function getFormLifecycle(): Form\Standard
177 {
179 $options = $lifecycle->getSelectOptions($this->lng);
180 return $this->ui_factory->input()->container()->form()->standard(
181 $this->ctrl->getFormAction($this, self::CMD_SAVELIFECYCLE),
182 [
183 $this->ui_factory->input()->field()
184 ->select($this->lng->txt('qst_lifecycle'), $options)
185 ->withRequired(true)
186 ]
187 )
188 ->withAdditionalTransformation($this->getShiftTrafo());
189 }
190
191 protected function getLifecycleUpdater(): \Closure
192 {
193 return function (array $questions, string $lifecycle) {
195 foreach ($questions as $q) {
196 $q->setLifecycle($lc);
197 $q->saveToDb();
198 }
199 return true;
200 };
201 }
202
204 {
205 $form = new ilPropertyFormGUI();
206 $form->setFormAction($this->ctrl->getFormAction($this, self::CMD_SAVETAXONOMIES));
207 $taxonomy_ids = \ilObjTaxonomy::getUsageOfObject($this->qpl_obj_id);
208
209 //taken from assQuestionGUI::populateTaxonomyFormSection
210 foreach ($taxonomy_ids as $taxonomy_id) {
211 $taxonomy = new ilObjTaxonomy($taxonomy_id);
212 $label = sprintf($this->lng->txt('qpl_qst_edit_form_taxonomy'), $taxonomy->getTitle());
213 $postvar = "tax_node_assign_$taxonomy_id";
214 // selector not working due to failing modals, actually:
215 // $taxSelect = new ilTaxSelectInputGUI($taxonomy->getId(), $postvar, true);
216 $taxSelect = new ilTaxAssignInputGUI($taxonomy->getId(), true, $label, $postvar, true);
217 $taxSelect->setTitle($label);
218 $form->addItem($taxSelect);
219 }
220 $form->addCommandButton(self::CMD_SAVETAXONOMIES, $this->lng->txt("qpl_bulk_save_overwrite"));
221 $form->addCommandButton(self::CMD_SAVETAXONOMIESADD, $this->lng->txt("qpl_bulk_save_add"));
222 return $form;
223 }
224
225 protected function storeTaxonomies(ilPropertyFormGUI $form): void
226 {
227 $questions = $this->getQuestions();
228 $post = $this->request->getParsedBody();
229 $form_cmd = array_shift($post['cmd']);
230 $overwrite = ($form_cmd === $this->lng->txt('qpl_bulk_save_overwrite'));
231
232 $taxonomy_ids = \ilObjTaxonomy::getUsageOfObject($this->qpl_obj_id);
233 foreach ($taxonomy_ids as $taxonomy_id) {
234 $postvar = "tax_node_assign_$taxonomy_id";
235 foreach ($questions as $q) {
236 $assignments = new ilTaxNodeAssignment(ilObject::_lookupType($q->getObjId()), $q->getObjId(), 'quest', $taxonomy_id);
237 $assigned_nodes = $assignments->getAssignmentsOfItem($q->getId());
238
239 $skip = [];
240 foreach ($assigned_nodes as $existing) {
241 if ($overwrite && !in_array($existing["node_id"], $post[$postvar])
242 ) {
243 $assignments->deleteAssignment((int) $existing["node_id"], $q->getId());
244 } else {
245 $skip[] = (int) $existing["node_id"];
246 }
247 }
248
249 $values = $post[$postvar];
250 foreach ($values as $value) {
251 if (!in_array((int) $value, $skip)) {
252 $assignments->addAssignment((int) $value, $q->getId());
253 }
254 }
255 }
256 }
257 $this->tpl->setOnScreenMessage('success', $this->lng->txt('qpl_bulkedit_success'), true);
258 $this->ctrl->redirectByClass(ilObjQuestionPoolGUI::class, ilObjQuestionPoolGUI::DEFAULT_CMD);
259 }
260
261}
$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:34
This describes a standard form.
Definition: Standard.php:30
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:25