ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilCmiXapiStatementsGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
31{
34 private \ilGlobalTemplateInterface $main_tpl;
35 private \ILIAS\DI\Container $dic;
36
38 {
39 global $DIC;
40 $this->dic = $DIC;
41 $this->main_tpl = $DIC->ui()->mainTemplate();
42 $this->object = $object;
43
44 $this->access = ilCmiXapiAccess::getInstance($this->object);
45 }
46
50 public function executeCommand(): void
51 {
52 global $DIC; /* @var \ILIAS\DI\Container $DIC */
53
54 if (!$this->access->hasStatementsAccess()) {
55 throw new ilCmiXapiException('access denied!');
56 }
57
58 switch ($DIC->ctrl()->getNextClass($this)) {
59 default:
60 $cmd = $DIC->ctrl()->getCmd('show') . 'Cmd';
61 $this->{$cmd}();
62 }
63 }
64
65 protected function resetFilterCmd(): void
66 {
67 $table = $this->buildTableGUI();
68 $table->resetFilter();
69 $table->resetOffset();
70 $this->showCmd();
71 }
72
73 protected function applyFilterCmd(): void
74 {
75 $table = $this->buildTableGUI();
76 $table->writeFilterToSession();
77 $table->resetOffset();
78 $this->showCmd();
79 }
80
81 protected function showCmd(): void
82 {
83 $table = $this->buildTableGUI();
84
85 try {
86 $statementsFilter = new ilCmiXapiStatementsReportFilter();
87 $statementsFilter->setActivityId($this->object->getActivityId());
88 $this->initLimitingAndOrdering($statementsFilter, $table);
89 $this->initActorFilter($statementsFilter, $table);
90 $this->initVerbFilter($statementsFilter, $table);
91 $this->initPeriodFilter($statementsFilter, $table);
92
93 $this->initTableData($table, $statementsFilter);
94 } catch (Exception $e) {
95 $this->main_tpl->setOnScreenMessage('failure', $e->getMessage());
96 $table->setData(array());
97 $table->setMaxCount(0);
98 $table->resetOffset();
99 }
100
101 $this->dic->ui()->mainTemplate()->setContent($table->getHTML());
102 }
103
105 {
106 $table->determineOffsetAndOrder();
107
108 $filter->setLimit($table->getLimit());
109 $filter->setOffset($table->getOffset());
110
111 $filter->setOrderField($table->getOrderField());
112 $filter->setOrderDirection($table->getOrderDirection());
113 }
114
115 protected function initActorFilter(
118 ): void {
119 if ($this->access->hasOutcomesAccess()) {
120 $actor = $table->getFilterItemByPostVar('actor')->getValue();
121 if ($actor && strlen($actor)) {
122 $usrId = ilObjUser::getUserIdByLogin($actor);
123 if ($usrId) {
124 $filter->setActor(new ilCmiXapiUser($this->object->getId(), $usrId, $this->object->getPrivacyIdent()));
125 } else {
127 "given actor ({$actor}) is not a valid actor for object ({$this->object->getId()})"
128 );
129 }
130 }
131 } else {
132 $filter->setActor(new ilCmiXapiUser($this->object->getId(), $this->dic->user()->getId(), $this->object->getPrivacyIdent()));
133 }
134 }
135
136 protected function initVerbFilter(
139 ): void {
140 if ($table->getFilterItemByPostVar('verb') != null) {
141 $verb = urldecode($table->getFilterItemByPostVar('verb')->getValue());
142
143 if (ilCmiXapiVerbList::getInstance()->isValidVerb($verb)) {
144 $filter->setVerb($verb);
145 }
146 }
147 }
148
149 protected function initPeriodFilter(
152 ): void {
153 if ($table->getFilterItemByPostVar('period') != null) {
154 $period = $table->getFilterItemByPostVar('period');
155
156 if ($period->getStartXapiDateTime()) {
157 $filter->setStartDate($period->getStartXapiDateTime());
158 }
159
160 if ($period->getEndXapiDateTime()) {
161 $filter->setEndDate($period->getEndXapiDateTime());
162 }
163 }
164 }
165
166 public function asyncUserAutocompleteCmd(): void
167 {
168 $auto = new ilCmiXapiUserAutocomplete($this->object->getId());
169 $auto->setSearchFields(array('login','firstname','lastname','email'));
170 $auto->setResultField('login');
171 $auto->enableFieldSearchableCheck(true);
172 $auto->setMoreLinkAvailable(true);
173
174 //$auto->setLimit(ilUserAutoComplete::MAX_ENTRIES);
175 $term = '';
176 if ($this->dic->http()->wrapper()->query()->has('term')) {
177 $term = $this->dic->http()->wrapper()->query()->retrieve('term', $this->dic->refinery()->kindlyTo()->string());
178 } elseif ($this->dic->http()->wrapper()->post()->has('term')) {
179 $term = $this->dic->http()->wrapper()->post()->retrieve('term', $this->dic->refinery()->kindlyTo()->string());
180 }
181 if ($term != '') {
182 $result = json_decode($auto->getList(ilUtil::stripSlashes($term)), true);
183 echo json_encode($result);
184 }
185 exit();
186 }
187
188 protected function initTableData(
191 ): void {
192 global $DIC;
193 if ($this->access->hasOutcomesAccess()) {
194 if (!ilCmiXapiUser::getUsersForObject($this->object->getId())) {
195 $table->setData(array());
196 $table->setMaxCount(0);
197 $table->resetOffset();
198 return;
199 }
200 } else {
201 $usrId = $DIC->user()->getId();
202 // if (!ilCmiXapiUser::getUsersForObject($this->object->getId(), $usrId)) {
203 if (!ilCmiXapiUser::getUsersForObject($this->object->getId())) {
204 $table->setData(array());
205 $table->setMaxCount(0);
206 $table->resetOffset();
207 return;
208 }
209 }
210 $linkBuilder = new ilCmiXapiStatementsReportLinkBuilder(
211 $this->object->getId(),
212 $this->object->getLrsType()->getLrsEndpointStatementsAggregationLink(),
213 $filter
214 );
215
217 $this->object->getLrsType()->getBasicAuth(),
218 $linkBuilder
219 );
220 $statementsReport = $request->queryReport($this->object->getId());
221 $data = $statementsReport->getTableData();
222 $table->setData($data);
223 $table->setMaxCount($statementsReport->getMaxCount());
224 }
225
227 {
228 $isMultiActorReport = $this->access->hasOutcomesAccess();
229 $table = new ilCmiXapiStatementsTableGUI($this, 'show', $isMultiActorReport);
230 $table->setFilterCommand('applyFilter');
231 $table->setResetCommand('resetFilter');
232
233 return $table;
234 }
235 //dynamic verbs
236 public function getVerbs(): ?array
237 {
238 $lrsType = $this->object->getLrsType();
239
240 //$this->getLrsEndpoint())) . '/api/' . self::ENDPOINT_AGGREGATE_SUFFIX;
241 $defaultLrs = $lrsType->getLrsEndpointStatementsAggregationLink();
242 //$fallbackLrs = $lrsType->getLrsFallbackEndpoint();
243 $defaultBasicAuth = $lrsType->getBasicAuth();
244 //$fallbackBasicAuth = $lrsType->getFallbackBasicAuth();
245 $defaultHeaders = [
246 'X-Experience-API-Version' => '1.0.3',
247 'Authorization' => $defaultBasicAuth,
248 'Cache-Control' => 'no-cache, no-store, must-revalidate'
249 ];
250 // $fallbackHeaders = [
251 // 'X-Experience-API-Version' => '1.0.3',
252 // 'Authorization' => $fallbackBasicAuth,
253 // 'Content-Type' => 'application/json;charset=utf-8',
254 // 'Cache-Control' => 'no-cache, no-store, must-revalidate'
255 // ];
256 $pipeline = json_encode($this->getVerbsPipline());
257
258 $defaultVerbsUrl = $defaultLrs . "?pipeline=" . urlencode($pipeline);
259
260 $client = new GuzzleHttp\Client();
261 $req_opts = array(
262 GuzzleHttp\RequestOptions::VERIFY => true,
263 GuzzleHttp\RequestOptions::CONNECT_TIMEOUT => 10,
264 GuzzleHttp\RequestOptions::HTTP_ERRORS => false
265 );
266 //new GuzzleHttp\Psr7\Request('POST', $defaultUrl, $this->defaultHeaders, $body);
267 $defaultVerbsRequest = new GuzzleHttp\Psr7\Request(
268 'GET',
269 $defaultVerbsUrl,
270 $defaultHeaders
271 );
272 $promises = array();
273 $promises['defaultVerbs'] = $client->sendAsync($defaultVerbsRequest, $req_opts);
274 try {
275 $responses = GuzzleHttp\Promise\Utils::settle($promises)->wait();
276 $body = '';
277 ilCmiXapiAbstractRequest::checkResponse($responses['defaultVerbs'], $body, [200]);
278 return json_decode($body, (bool) JSON_OBJECT_AS_ARRAY);
279 } catch (Exception $e) {
280 //$this->log()->error('error:' . $e->getMessage());
281 return null;
282 }
283 return null;
284 }
285
286 public function getVerbsPipline(): array
287 {
288 $pipeline = array();
289
290 // filter activityId
291 $match = array();
292 $match['statement.object.objectType'] = 'Activity';
293 $match['statement.actor.objectType'] = 'Agent';
294
295 $activityId = array();
296
297 if ($this->object->getContentType() == ilObjCmiXapi::CONT_TYPE_CMI5 && !$this->object->isMixedContentType()) {
298 // https://github.com/AICC/CMI-5_Spec_Current/blob/quartz/cmi5_spec.md#963-extensions
299 $activityId['statement.context.extensions.https://ilias&46;de/cmi5/activityid'] = $this->object->getActivityId();
300 } else {
301 $activityQuery = [
302 '$regex' => '^' . preg_quote($this->object->getActivityId()) . ''
303 ];
304 $activityId['$or'] = [];
305 $activityId['$or'][] = ['statement.object.id' => $activityQuery];
306 $activityId['$or'][] = ['statement.context.contextActivities.parent.id' => $activityQuery];
307 }
308 $match['$and'] = [];
309 $match['$and'][] = $activityId;
310
311 $sort = array();
312 $sort['statement.verb.id'] = 1;
313
314 // project distinct verbs
315 $group = array('_id' => '$statement.verb.id');
316 // $project = array('statement.verb.id' => 1);
317 // project distinct verbs
318
319 $pipeline[] = array('$match' => $match);
320 $pipeline[] = array('$group' => $group);
321 $pipeline[] = array('$sort' => $sort);
322 //$pipeline[] = array('$project' => $project);
323
324 return $pipeline;
325 }
326}
static checkResponse(array $response, &$body, array $allowedStatus=[200, 204])
static getInstance(ilObjCmiXapi $object)
initActorFilter(ilCmiXapiStatementsReportFilter $filter, ilCmiXapiStatementsTableGUI $table)
ilGlobalTemplateInterface $main_tpl
initVerbFilter(ilCmiXapiStatementsReportFilter $filter, ilCmiXapiStatementsTableGUI $table)
initTableData(ilCmiXapiStatementsTableGUI $table, ilCmiXapiStatementsReportFilter $filter)
initLimitingAndOrdering(ilCmiXapiStatementsReportFilter $filter, ilCmiXapiStatementsTableGUI $table)
initPeriodFilter(ilCmiXapiStatementsReportFilter $filter, ilCmiXapiStatementsTableGUI $table)
static getUsersForObject(int $objId, bool $asUsrId=false)
static getUserIdByLogin(string $a_login)
setFilterCommand(string $a_val, string $a_caption="")
getFilterItemByPostVar(string $a_post_var)
determineOffsetAndOrder(bool $a_omit_offset=false)
resetOffset(bool $a_in_determination=false)
setResetCommand(string $a_val, string $a_caption="")
setData(array $a_data)
Set table data.
setMaxCount(int $a_max_count)
set max.
static stripSlashes(string $a_str, bool $a_strip_html=true, string $a_allow="")
exit
$client
if(!file_exists('../ilias.ini.php'))
global $DIC
Definition: shib_login.php:26