ILIAS  release_8 Revision v8.24
class.ilCharSelectorGUI.php
Go to the documentation of this file.
1<?php
2
24{
25 protected ilLanguage $lng;
26 protected ilCtrl $ctrl;
28 protected stdClass $jsconfig;
29 protected stdClass $jstexts;
30
35 private static array $allowed_guis = array(
36 'assQuestionGUI',
37 'ilAssQuestionFeedbackEditingGUI',
38 'ilAssQuestionHintGUI',
39 'ilObjTestSettingsGeneralGUI',
40 'ilTestScoringGUI'
41 );
42
43 // instance used for the current selector
46
47 // selector is already added to the page
48 private bool $added_to_page = false;
49
50 protected \ILIAS\Refinery\Factory $refinery;
51 protected \ILIAS\HTTP\Wrapper\WrapperFactory $wrapper;
52
56 public function __construct(
57 string $a_context = ilCharSelectorConfig::CONTEXT_NONE
58 ) {
60 global $DIC;
61
62 $this->lng = $DIC->language();
63 $this->ctrl = $DIC->ctrl();
64 $this->tpl = $DIC["tpl"];
65 $this->config = new ilCharSelectorConfig($a_context);
66 $this->refinery = $DIC->refinery();
67 $this->wrapper = $DIC->http()->wrapper();
68 }
69
73 public static function _isAllowed(): bool
74 {
75 global $DIC;
76
77 $ilCtrl = $DIC->ctrl();
78
79 // get the command class
80 // with correct case for checking parent classes
81 $class = false;
82 foreach ($ilCtrl->getCallHistory() as $call) {
83 if (($call['mode'] ?? "") === 'execComm') {
84 $class = $call['class'];
85 }
86 }
87
88 // check the class and her parent classes
89 while ($class != false) {
90 if (in_array($class, self::$allowed_guis)) {
91 return true;
92 }
93 $class = get_parent_class($class);
94 }
95
96 return false;
97 }
98
103 public static function _getCurrentGUI(ilObjTest $a_test_obj = null): self
104 {
105 if (!isset(self::$current_gui)) {
106 self::$current_gui = new ilCharSelectorGUI();
107 self::$current_gui->setConfig(ilCharSelectorConfig::_getCurrentConfig($a_test_obj));
108 }
109 return self::$current_gui;
110 }
111
112 public function setConfig(ilCharSelectorConfig $a_config): void
113 {
114 $this->config = $a_config;
115 }
116
118 {
119 return $this->config;
120 }
121
125 public function addFormProperties(ilPropertyFormGUI $a_form): void
126 {
128 $lng->loadLanguageModule('adve');
129
130 $availability = new ilCharSelectorRadioGroupInputGUI($lng->txt('char_selector_' . $this->config->getContext()), 'char_selector_availability');
131 $inactive = new ilRadioOption($lng->txt('char_selector_inactive_' . $this->config->getContext()), ilCharSelectorConfig::INACTIVE);
132 $inactive->setInfo($lng->txt('char_selector_inactive_info_' . $this->config->getContext()));
133 $inherit = new ilRadioOption($lng->txt('char_selector_inherit_' . $this->config->getContext()), ilCharSelectorConfig::INHERIT);
134 $inherit->setInfo($lng->txt('char_selector_inherit_info_' . $this->config->getContext()));
135 $enabled = new ilRadioOption($lng->txt('char_selector_enabled_' . $this->config->getContext()), ilCharSelectorConfig::ENABLED);
136 $enabled->setInfo($lng->txt('char_selector_enabled_info_' . $this->config->getContext()));
137 $disabled = new ilRadioOption($lng->txt('char_selector_disabled_' . $this->config->getContext()), ilCharSelectorConfig::DISABLED);
138 $disabled->setInfo($lng->txt('char_selector_disabled_info_' . $this->config->getContext()));
139
140 $blocks = new ilSelectInputGUI($lng->txt('char_selector_blocks'), 'char_selector_blocks');
141 $blocks->setInfo($lng->txt('char_selector_blocks_info'));
142 $blocks->setOptions($this->config->getBlockOptions());
143 $blocks->setMulti(true);
144 $enabled->addSubItem($blocks);
145
146 $custom_items = new ilTextAreaInputGUI($lng->txt('char_selector_custom_items'), 'char_selector_custom_items');
147 $tpl = new ilTemplate("tpl.char_selector_custom_info.html", true, true, "Services/UIComponent/CharSelector");
148 $tpl->setVariable('1', $lng->txt('char_selector_custom_items_info1'));
149 $tpl->setVariable('2a', $lng->txt('char_selector_custom_items_info2a'));
150 $tpl->setVariable('2b', $lng->txt('char_selector_custom_items_info2b'));
151 $tpl->setVariable('3a', $lng->txt('char_selector_custom_items_info3a'));
152 $tpl->setVariable('3b', $lng->txt('char_selector_custom_items_info3b'));
153 $tpl->setVariable('4a', $lng->txt('char_selector_custom_items_info4a'));
154 $tpl->setVariable('4b', $lng->txt('char_selector_custom_items_info4b'));
155 $tpl->setVariable('5a', $lng->txt('char_selector_custom_items_info5a'));
156 $tpl->setVariable('5b', $lng->txt('char_selector_custom_items_info5b'));
157 $tpl->setVariable('6a', $lng->txt('char_selector_custom_items_info6a'));
158 $tpl->setVariable('6b', $lng->txt('char_selector_custom_items_info6b'));
159 $custom_items->setInfo($tpl->get());
160 $enabled->addSubItem($custom_items);
161
162 switch ($this->config->getContext()) {
164 $availability->addOption($inactive);
165 $availability->addOption($enabled);
166 $availability->addOption($disabled);
167 $a_form->addItem($availability);
168 break;
169
172 $availability->addOption($inherit);
173 $availability->addOption($enabled);
174 $availability->addOption($disabled);
175 $a_form->addItem($availability);
176 break;
177 }
178 }
179
180
184 public function setFormValues(ilPropertyFormGUI $a_form): void
185 {
186 $a_form->getItemByPostVar('char_selector_availability')->setValue($this->config->getAvailability());
187 $a_form->getItemByPostVar('char_selector_blocks')->setValue($this->config->getAddedBlocks());
188 $a_form->getItemByPostVar('char_selector_custom_items')->setValue($this->config->getCustomItems());
189 }
190
191
195 public function getFormValues(ilPropertyFormGUI $a_form): void
196 {
197 $this->config->setAvailability((int) $a_form->getInput('char_selector_availability'));
198 $this->config->setAddedBlocks((array) $a_form->getInput('char_selector_blocks'));
199 $this->config->setCustomItems((string) $a_form->getInput('char_selector_custom_items'));
200 }
201
207 public function addToPage(): void
208 {
209 $ilCtrl = $this->ctrl;
212
213 // don't add the panel twice
214 if ($this->added_to_page) {
215 return;
216 }
217
218 $lng->loadLanguageModule('adve');
219
220 // prepare the configuration for the js script
221 $this->jsconfig = new stdClass();
222 $this->jsconfig->pages = $this->config->getCharPages();
223 $this->jsconfig->ajax_url = $ilCtrl->getLinkTargetByClass("ilcharselectorgui", "saveState", "", true);
224 $this->jsconfig->open = (int) ilSession::get('char_selector_open');
225 $this->jsconfig->current_page = (int) ilSession::get('char_selector_current_page');
226 $this->jsconfig->current_subpage = (int) ilSession::get('char_selector_current_subpage');
227
228 // provide texts to be dynamically rendered in the js script
229 $this->jstexts = new stdClass();
230 $this->jstexts->page = $lng->txt('page');
231 // fau: testNav - add texts for open/close char selector actions in the question menu
232 $this->jstexts->open = $lng->txt('char_selector_menu_open');
233 $this->jstexts->close = $lng->txt('char_selector_menu_close');
234 // fau.
235
236 // add everything neded to the page
237 // addLightbox() is just used to add the panel template outside the body
238 // The panel template is added as <script> to be not included in the DOM by default
239 // It will be included by js below the main header when the selector is switched on
240 $tpl->addCss(ilUtil::getStyleSheetLocation('', 'char_selector_style.css', 'Services/UIComponent/CharSelector'));
241 $tpl->addJavaScript('./Services/UIComponent/CharSelector/js/ilCharSelector.js');
242 $tpl->addLightbox($this->getSelectorHTML(), 2);
243 $tpl->addOnLoadCode(
244 'il.CharSelector.init(' .
245 json_encode($this->jsconfig, JSON_THROW_ON_ERROR) . ',' .
246 json_encode($this->jstexts, JSON_THROW_ON_ERROR) . ')'
247 );
248 $this->added_to_page = true;
249 }
250
254 public function getSelectorHTML(): string
255 {
257 $tpl = new ilTemplate("tpl.char_selector_panel.html", true, true, "Services/UIComponent/CharSelector");
258
259 if (count($this->jsconfig->pages) > 1) {
260 $index = 0;
261 foreach ($this->jsconfig->pages as $page) {
262 $tpl->setCurrentBlock('page_option');
263 $tpl->setVariable("PAGE_INDEX", $index);
264 $tpl->setVariable("PAGE_NAME", $page[0]);
266 $index++;
267 }
268 }
269
270 $tpl->setVariable('TXT_PREVIOUS_PAGE', $lng->txt('previous'));
271 $tpl->setVariable('TXT_NEXT_PAGE', $lng->txt('next'));
272 $tpl->setVariable('TXT_PAGE', $lng->txt('page'));
273
274 $tpl->touchBlock('chars');
275 return '<script type="text/html" id="ilCharSelectorTemplate">' . $tpl->get() . '</script>';
276 }
277
278
284 public function saveState(): void
285 {
286 $int = $this->refinery->kindlyTo()->int();
288 'char_selector_open',
289 $this->wrapper->query()->retrieve("open", $int)
290 );
292 'char_selector_current_page',
293 $this->wrapper->query()->retrieve("current_page", $int)
294 );
296 'char_selector_current_subpage',
297 $this->wrapper->query()->retrieve("current_subpage", $int)
298 );
299
300 // debugging output (normally ignored by the js part)
301 echo json_encode(array(
302 'open' => ilSession::get('char_selector_open'),
303 'current_page' => ilSession::get('char_selector_current_page'),
304 'current_subpage' => ilSession::get('char_selector_current_subpage'),
305 ), JSON_THROW_ON_ERROR);
306 exit;
307 }
308
309 public function executeCommand(): void
310 {
311 $ilCtrl = $this->ctrl;
312 $cmd = $ilCtrl->getCmd("saveState");
313 switch ($cmd) {
314 case 'saveState':
315 $this->$cmd();
316 break;
317 default:
318 break;
319 }
320 }
321}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const INACTIVE
Availabilities INACTIVE/INHERIT corresponds to an unconfigured selector (no database entries)
static _getCurrentConfig(ilObjTest $a_test_obj=null)
Get the configuration that should be used for the current selector.
const CONTEXT_NONE
Configuration contexts.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
ilGlobalTemplateInterface $tpl
static array $allowed_guis
list of command classes for which the char selector is allowed (can also be a parent class of the act...
static ilCharSelectorGUI $current_gui
addToPage()
Adds the the character selector to the ilias page Initializes the selector according to the state sav...
static _isAllowed()
Check if the CharSelector is allowed for the current GUI.
ilCharSelectorConfig $config
saveState()
Save the selector panel state in the user session (This keeps the panel state between page moves)
ILIAS HTTP Wrapper WrapperFactory $wrapper
ILIAS Refinery Factory $refinery
getSelectorHTML()
Get the HTML code of the selector panel.
getFormValues(ilPropertyFormGUI $a_form)
Set the configuration based on the values of a property form.
addFormProperties(ilPropertyFormGUI $a_form)
add the configuration elements to a property form
setConfig(ilCharSelectorConfig $a_config)
static _getCurrentGUI(ilObjTest $a_test_obj=null)
Get the GUI that is used for the currently available selector (other GUI instances may exist for conf...
setFormValues(ilPropertyFormGUI $a_form)
Set the values in a property form based on the configuration.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class ilCtrl provides processing control methods.
getCmd(string $fallback_command=null)
@inheritDoc
language handling
loadLanguageModule(string $a_module)
Load language module.
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...
This class represents a property form user interface.
getInput(string $a_post_var, bool $ensureValidation=true)
Returns the input of an item, if item provides getInput method and as fallback the value of the HTTP-...
getItemByPostVar(string $a_post_var)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This class represents a selection list property in a property form.
static get(string $a_var)
static set(string $a_var, $a_val)
Set a value.
special template class to simplify handling of ITX/PEAR
This class represents a text area property in a property form.
static getStyleSheetLocation(string $mode="output", string $a_css_name="", string $a_css_location="")
get full style sheet file name (path inclusive) of current user
global $DIC
Definition: feed.php:28
Interface ilCtrlBaseClassInterface describes ilCtrl base classes.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
addJavaScript(string $a_js_file, bool $a_add_version_parameter=true, int $a_batch=2)
Add a javascript file that should be included in the header.
touchBlock(string $block)
overwrites ITX::touchBlock.
setCurrentBlock(string $part=self::DEFAULT_BLOCK)
Sets the template to the given block.
addLightbox(string $a_html, string $a_id)
Add a lightbox html to the template.
addOnLoadCode(string $a_code, int $a_batch=2)
Add on load code.
parseCurrentBlock(string $block_name=self::DEFAULT_BLOCK)
Parses the given block.
setVariable(string $variable, $value='')
Sets the given variable to the given value.
addCss(string $a_css_file, string $media="screen")
Add a css file that should be included in the header.
get(string $part=self::DEFAULT_BLOCK)
Renders the given block and returns the html string.
exit
Definition: login.php:28
$index
Definition: metadata.php:145
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
bool $enabled
Whether the system instance is enabled to accept connection requests.
Definition: System.php:123