ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilBadgeGUIRequest.php
Go to the documentation of this file.
1<?php
2
19use ILIAS\HTTP;
21
23{
24 private const ACTION_PARAMETER_TOKEN = 'tid_id';
25 private const ACTION_PARAMETER_TOKEN_ID = 'id';
26
28 protected Refinery\Factory $refinery;
29
30 public function __construct(
33 ) {
34 $this->initRequest(
35 $http,
37 );
38 }
39
40 protected function initRequest(
43 ): void {
44 $this->http = $http;
45 $this->refinery = $refinery;
46 }
47
48 // get string parameter kindly
49 protected function str(string $key): string
50 {
51 $t = $this->refinery->kindlyTo()->string();
52 return \ilUtil::stripSlashes((string) ($this->get($key, $t) ?? ""));
53 }
54
55 // get integer parameter kindly
56 protected function int(string $key): int
57 {
58 $t = $this->refinery->kindlyTo()->int();
59 return (int) ($this->get($key, $t) ?? 0);
60 }
61
62 // get integer array kindly
63 protected function intArray(string $key): array
64 {
65 if (!$this->isArray($key)) {
66 return [];
67 }
68 $t = $this->refinery->custom()->transformation(
69 static function (array $arr): array {
70 // keep keys(!), transform all values to int
71 return array_column(
72 array_map(
73 static function ($k, $v): array {
74 return [$k, (int) $v];
75 },
76 array_keys($arr),
77 $arr
78 ),
79 1,
80 0
81 );
82 }
83 );
84 return (array) ($this->get($key, $t) ?? []);
85 }
86
87 // get string array kindly
88 protected function strArray($key): array
89 {
90 if (!$this->isArray($key)) {
91 return [];
92 }
93 $t = $this->refinery->custom()->transformation(
94 function ($arr) {
95 // keep keys(!), transform all values to string
96 return array_column(
97 array_map(
98 function ($k, $v) {
99 if (is_array($v)) {
100 $v = "";
101 }
102 return [$k, \ilUtil::stripSlashes((string) $v)];
103 },
104 array_keys($arr),
105 $arr
106 ),
107 1,
108 0
109 );
110 }
111 );
112 return (array) ($this->get($key, $t) ?? []);
113 }
114
118 protected function isArray(string $key): bool
119 {
120 $no_transform = $this->refinery->identity();
121 $w = $this->http->wrapper();
122 if ($w->post()->has($key)) {
123 return is_array($w->post()->retrieve($key, $no_transform));
124 }
125 if ($w->query()->has($key)) {
126 return is_array($w->query()->retrieve($key, $no_transform));
127 }
128 return false;
129 }
130
134 protected function get(string $key, Refinery\Transformation $t)
135 {
136 $w = $this->http->wrapper();
137 if ($w->post()->has($key)) {
138 return $w->post()->retrieve($key, $t);
139 }
140 if ($w->query()->has($key)) {
141 return $w->query()->retrieve($key, $t);
142 }
143 return null;
144 }
145
147 public function getBadgeIds(): array
148 {
149 $badge_ids = $this->intArray("badge_id");
150 if (count($badge_ids) === 0 && $this->int("badge_id") > 0) {
151 $badge_ids = [$this->int("badge_id")];
152 }
153 return $badge_ids;
154 }
155
156 public function getBadgeIdFromUrl(): int
157 {
158 $id = 0;
159 $query = $this->http->wrapper()->query();
160 if ($query->has(self::ACTION_PARAMETER_TOKEN)) {
161 $tmpl_ids = $query->retrieve(
162 self::ACTION_PARAMETER_TOKEN,
163 $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->string())
164 );
165 if (count($tmpl_ids) === 1) {
166 $id = (int) array_pop($tmpl_ids);
167 }
168 }
169
170 return $id;
171 }
172
176 public function getMultiActionBadgeIdsFromUrl(): array
177 {
178 $tmpl_ids = [];
179 $query = $this->http->wrapper()->query();
180 if ($query->has(self::ACTION_PARAMETER_TOKEN)) {
181 $tmpl_ids = $query->retrieve(
182 self::ACTION_PARAMETER_TOKEN,
183 $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->string())
184 );
185 }
186
187 return $tmpl_ids;
188 }
189
193 public function getMultiActionBadgeIdsFromPost(): array
194 {
195 $tmpl_ids = [];
196 $query = $this->http->wrapper()->post();
197 if ($query->has(self::ACTION_PARAMETER_TOKEN_ID)) {
198 $tmpl_ids = $query->retrieve(
199 self::ACTION_PARAMETER_TOKEN_ID,
200 $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->string())
201 );
202 }
203
204 return $tmpl_ids;
205 }
206
207
208 public function getBadgeId(): int
209 {
210 return $this->int("bid");
211 }
212
213 public function getId(): int
214 {
215 return $this->int("id");
216 }
217
219 public function getIds(): array
220 {
221 return $this->strArray("id");
222 }
223
224 public function getType(): string
225 {
226 return $this->str("type");
227 }
228
229 public function getTgt(): string
230 {
231 return $this->str("tgt");
232 }
233
234 public function getTemplateId(): int
235 {
236 return $this->int("tid");
237 }
238
239 public function getParentId(): int
240 {
241 return $this->int("pid");
242 }
243
244 public function getBadgeAssignableUsers(): array
245 {
246 return $this->strArray("tid_id");
247 }
248}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Builds data types.
Definition: Factory.php:36
Class Services.
Definition: Services.php:38
isArray(string $key)
Check if parameter is an array.
initRequest(HTTP\Services $http, Refinery\Factory $refinery)
Refinery Factory $refinery
__construct(HTTP\Services $http, Refinery\Factory $refinery)
static stripSlashes(string $a_str, bool $a_strip_html=true, string $a_allow="")
static http()
Fetches the global http state from ILIAS.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
isArray(string $key)
Check if parameter is an array.