ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilCmiXapiStatementsGUI.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2019 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
5 
16 {
20  protected $object;
21 
25  protected $access;
26 
30  public function __construct(ilObjCmiXapi $object)
31  {
32  $this->object = $object;
33 
34  $this->access = ilCmiXapiAccess::getInstance($this->object);
35  }
36 
40  public function executeCommand()
41  {
42  global $DIC; /* @var \ILIAS\DI\Container $DIC */
43 
44  if (!$this->access->hasStatementsAccess()) {
45  throw new ilCmiXapiException('access denied!');
46  }
47 
48  switch ($DIC->ctrl()->getNextClass($this)) {
49  default:
50  $cmd = $DIC->ctrl()->getCmd('show') . 'Cmd';
51  $this->{$cmd}();
52  }
53  }
54 
55  protected function resetFilterCmd()
56  {
57  $table = $this->buildTableGUI();
58  $table->resetFilter();
59  $table->resetOffset();
60  $this->showCmd();
61  }
62 
63  protected function applyFilterCmd()
64  {
65  $table = $this->buildTableGUI();
66  $table->writeFilterToSession();
67  $table->resetOffset();
68  $this->showCmd();
69  }
70 
71  protected function showCmd()
72  {
73  global $DIC; /* @var \ILIAS\DI\Container $DIC */
74 
75  $table = $this->buildTableGUI();
76 
77  try {
78  $statementsFilter = new ilCmiXapiStatementsReportFilter();
79  $statementsFilter->setActivityId($this->object->getActivityId());
80  $this->initLimitingAndOrdering($statementsFilter, $table);
81  $this->initActorFilter($statementsFilter, $table);
82  $this->initVerbFilter($statementsFilter, $table);
83  $this->initPeriodFilter($statementsFilter, $table);
84 
85  $this->initTableData($table, $statementsFilter);
86  } catch (Exception $e) {
87  ilUtil::sendFailure($e->getMessage());
88  $table->setData(array());
89  $table->setMaxCount(0);
90  $table->resetOffset();
91  }
92 
93  $DIC->ui()->mainTemplate()->setContent($table->getHTML());
94  }
95 
97  {
98  $table->determineOffsetAndOrder();
99 
100  $filter->setLimit($table->getLimit());
101  $filter->setOffset($table->getOffset());
102 
103  $filter->setOrderField($table->getOrderField());
104  $filter->setOrderDirection($table->getOrderDirection());
105  }
106 
108  {
109  global $DIC;
110  if ($this->access->hasOutcomesAccess()) {
111  $actor = $table->getFilterItemByPostVar('actor')->getValue();
112  if (strlen($actor)) {
113  $usrId = ilObjUser::getUserIdByLogin($actor);
114  if ($usrId) {
115  $filter->setActor(new ilCmiXapiUser($this->object->getId(), $usrId, $this->object->getPrivacyIdent()));
116  } else {
118  "given actor ({$actor}) is not a valid actor for object ({$this->object->getId()})"
119  );
120  }
121  }
122  } else {
123  $filter->setActor(new ilCmiXapiUser($this->object->getId(), $DIC->user()->getId(), $this->object->getPrivacyIdent()));
124  }
125  }
126 
128  {
129  $verb = urldecode($table->getFilterItemByPostVar('verb')->getValue());
130 
131  if (ilCmiXapiVerbList::getInstance()->isValidVerb($verb)) {
132  $filter->setVerb($verb);
133  }
134  }
135 
137  {
138  $period = $table->getFilterItemByPostVar('period');
139 
140  if ($period->getStartXapiDateTime()) {
141  $filter->setStartDate($period->getStartXapiDateTime());
142  }
143 
144  if ($period->getEndXapiDateTime()) {
145  $filter->setEndDate($period->getEndXapiDateTime());
146  }
147  }
148 
149  public function asyncUserAutocompleteCmd()
150  {
151  $auto = new ilCmiXapiUserAutocomplete($this->object->getId());
152  $auto->setSearchFields(array('login','firstname','lastname','email'));
153  $auto->setResultField('login');
154  $auto->enableFieldSearchableCheck(true);
155  $auto->setMoreLinkAvailable(true);
156 
157  //$auto->setLimit(ilUserAutoComplete::MAX_ENTRIES);
158 
159  $result = json_decode($auto->getList(ilUtil::stripSlashes($_REQUEST['term'])), true);
160 
161  echo json_encode($result);
162  exit();
163  }
164 
170  {
171  global $DIC;
172  if ($this->access->hasOutcomesAccess()) {
173  if (!ilCmiXapiUser::getUsersForObject($this->object->getId())) {
174  $table->setData(array());
175  $table->setMaxCount(0);
176  $table->resetOffset();
177  return;
178  }
179  } else {
180  $usrId = $DIC->user()->getId();
181  if (!ilCmiXapiUser::getUsersForObject($this->object->getId(), $usrId)) {
182  $table->setData(array());
183  $table->setMaxCount(0);
184  $table->resetOffset();
185  return;
186  }
187  }
188  $linkBuilder = new ilCmiXapiStatementsReportLinkBuilder(
189  $this->object,
190  $this->object->getLrsType()->getLrsEndpointStatementsAggregationLink(),
191  $filter
192  );
193 
194  $request = new ilCmiXapiStatementsReportRequest(
195  $this->object->getLrsType()->getBasicAuth(),
196  $linkBuilder
197  );
198  $statementsReport = $request->queryReport($this->object);
199  $data = $statementsReport->getTableData();
200  $table->setData($data);
201  $table->setMaxCount($statementsReport->getMaxCount());
202  }
203 
208  {
209  $isMultiActorReport = $this->access->hasOutcomesAccess();
210  $table = new ilCmiXapiStatementsTableGUI($this, 'show', $isMultiActorReport);
211  $table->setFilterCommand('applyFilter');
212  $table->setResetCommand('resetFilter');
213  return $table;
214  }
215 
216  //dynamic verbs
217  public function getVerbs()
218  {
219  global $DIC;
220  $lrsType = $this->object->getLrsType();
221 
222  //$this->getLrsEndpoint())) . '/api/' . self::ENDPOINT_AGGREGATE_SUFFIX;
223  $defaultLrs = $lrsType->getLrsEndpointStatementsAggregationLink();
224  //$fallbackLrs = $lrsType->getLrsFallbackEndpoint();
225  $defaultBasicAuth = $lrsType->getBasicAuth();
226  //$fallbackBasicAuth = $lrsType->getFallbackBasicAuth();
227  $defaultHeaders = [
228  'X-Experience-API-Version' => '1.0.3',
229  'Authorization' => $defaultBasicAuth,
230  'Cache-Control' => 'no-cache, no-store, must-revalidate'
231  ];
232 // $fallbackHeaders = [
233 // 'X-Experience-API-Version' => '1.0.3',
234 // 'Authorization' => $fallbackBasicAuth,
235 // 'Content-Type' => 'application/json;charset=utf-8',
236 // 'Cache-Control' => 'no-cache, no-store, must-revalidate'
237 // ];
238  $pipeline = json_encode($this->getVerbsPipline());
239 // $pipeline2 = json_encode($this->getVerbsPipline(),JSON_PRETTY_PRINT);
240  //$DIC->logger()->root()->log($pipeline2);
241 
242  $defaultVerbsUrl = $defaultLrs . "?pipeline=" . urlencode($pipeline);
243  //$DIC->logger()->root()->log($defaultVerbsUrl);
244 
245  $client = new GuzzleHttp\Client();
246  $req_opts = array(
247  GuzzleHttp\RequestOptions::VERIFY => true,
248  GuzzleHttp\RequestOptions::CONNECT_TIMEOUT => 10,
249  GuzzleHttp\RequestOptions::HTTP_ERRORS => false
250  );
251  //new GuzzleHttp\Psr7\Request('POST', $defaultUrl, $this->defaultHeaders, $body);
252  $defaultVerbsRequest = new GuzzleHttp\Psr7\Request(
253  'GET',
254  $defaultVerbsUrl,
255  $defaultHeaders
256  );
257  $promises = array();
258  $promises['defaultVerbs'] = $client->sendAsync($defaultVerbsRequest, $req_opts);
259  try {
260  $responses = GuzzleHttp\Promise\Utils::settle($promises)->wait();
261  $body = '';
262  //$DIC->logger()->root()->log(var_export($responses['defaultVerbs'],TRUE));
263  ilCmiXapiAbstractRequest::checkResponse($responses['defaultVerbs'], $body, [200]);
264  return json_decode($body, JSON_OBJECT_AS_ARRAY);
265  } catch (Exception $e) {
266  $this->log()->error('error:' . $e->getMessage());
267  return null;
268  }
269  return null;
270  }
271 
272  public function getVerbsPipline()
273  {
274  global $DIC;
275  $pipeline = array();
276 
277  // filter activityId
278  $match = array();
279  $match['statement.object.objectType'] = 'Activity';
280  $match['statement.actor.objectType'] = 'Agent';
281 
282  $activityId = array();
283 
284  if ($this->object->getContentType() == ilObjCmiXapi::CONT_TYPE_CMI5 && !$this->object->isMixedContentType()) {
285  // https://github.com/AICC/CMI-5_Spec_Current/blob/quartz/cmi5_spec.md#963-extensions
286  $activityId['statement.context.extensions.https://ilias&46;de/cmi5/activityid'] = $this->object->getActivityId();
287  } else {
288  $activityQuery = [
289  '$regex' => '^' . preg_quote($this->object->getActivityId()) . ''
290  ];
291  $activityId['$or'] = [];
292  $activityId['$or'][] = ['statement.object.id' => $activityQuery];
293  $activityId['$or'][] = ['statement.context.contextActivities.parent.id' => $activityQuery];
294  }
295  $match['$and'] = [];
296  $match['$and'][] = $activityId;
297 
298  $sort = array();
299  $sort['statement.verb.id'] = 1;
300 
301  // project distinct verbs
302  $group = array('_id' => '$statement.verb.id');
303  // $project = array('statement.verb.id' => 1);
304  // project distinct verbs
305 
306  $pipeline[] = array('$match' => $match);
307  $pipeline[] = array('$group' => $group);
308  $pipeline[] = array('$sort' => $sort);
309  //$pipeline[] = array('$project' => $project);
310 
311  return $pipeline;
312  }
313 }
static getUserIdByLogin($a_login)
static getInstance(ilObjCmiXapi $object)
static getUsersForObject($objId, $asUsrId=false)
exit
Definition: login.php:29
$data
Definition: storeScorm.php:23
$result
initActorFilter(ilCmiXapiStatementsReportFilter $filter, ilCmiXapiStatementsTableGUI $table)
initVerbFilter(ilCmiXapiStatementsReportFilter $filter, ilCmiXapiStatementsTableGUI $table)
initTableData(ilCmiXapiStatementsTableGUI $table, ilCmiXapiStatementsReportFilter $filter)
resetOffset($a_in_determination=false)
Reset offset.
if($_SERVER['argc']< 4) $client
Definition: cron.php:12
getFilterItemByPostVar($a_post_var)
getOrderDirection()
Get order direction.
initPeriodFilter(ilCmiXapiStatementsReportFilter $filter, ilCmiXapiStatementsTableGUI $table)
initLimitingAndOrdering(ilCmiXapiStatementsReportFilter $filter, ilCmiXapiStatementsTableGUI $table)
getOffset()
Get offset.
global $DIC
Definition: goto.php:24
static stripSlashes($a_str, $a_strip_html=true, $a_allow="")
strip slashes if magic qoutes is enabled
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
determineOffsetAndOrder($a_omit_offset=false)
Determine offset and order.
static checkResponse($response, &$body, $allowedStatus=[200, 204])
setSearchFields($a_fields)
Set searchable fields.
getLimit()
Get limit.
setMaxCount($a_max_count)
set max.