ILIAS  trunk Revision v11.0_alpha-1731-gff9cd7e2bd3
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilCmiXapiStatementsReport.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
31 {
32  protected array $response;
33 
34  protected array $statements;
35 
36  protected int $maxCount;
37 
41  protected array $cmixUsersByIdent;
42 
43  protected string $userLanguage;
47  protected string $contentType;
48 
49  protected bool $isMixedContentType;
50 
51  public function __construct(string $responseBody, int $objId)
52  {
53  global $DIC;
54  $this->userLanguage = $DIC->user()->getLanguage();
55 
56  $responseBody = json_decode($responseBody, true);
57 
58  if (ilObject::_lookupType($objId) == 'lti') {
59  $this->contentType = ilObjCmiXapi::CONT_TYPE_GENERIC;
60  $this->isMixedContentType = ilObjLTIConsumer::getInstance($objId, false)->isMixedContentType();
61  } else {
62  $this->contentType = ilObjCmiXapi::getInstance($objId, false)->getContentType();
63  $this->isMixedContentType = ilObjCmiXapi::getInstance($objId, false)->isMixedContentType();
64  }
65 
66  if (is_array($responseBody) && count($responseBody) > 0) {
67  $this->response = current($responseBody);
68  $this->statements = $this->response['statements'];
69  $this->maxCount = $this->response['maxcount'];
70  } else {
71  $this->response = array();
72  $this->statements = array();
73  $this->maxCount = 0;
74  }
75 
76  foreach (ilCmiXapiUser::getUsersForObject($objId) as $cmixUser) {
77  $this->cmixUsersByIdent[$cmixUser->getUsrIdent()] = $cmixUser;
78  }
79  }
80 
81  public function getMaxCount(): int
82  {
83  return $this->maxCount;
84  }
85 
89  public function getStatements(): array
90  {
91  return $this->statements;
92  }
93 
94  public function hasStatements(): bool
95  {
96  return (bool) count($this->statements);
97  }
98 
102  public function getTableData(): array
103  {
104  $data = [];
105 
106  foreach ($this->statements as $statement) {
107  $data[] = [
108  'date' => $this->fetchDate($statement),
109  'actor' => $this->fetchActor($statement),
110  'verb_id' => $this->fetchVerbId($statement),
111  'verb_display' => $this->fetchVerbDisplay($statement),
112  'object' => $this->fetchObjectName($statement),
113  'object_info' => $this->fetchObjectInfo($statement),
114  'statement' => json_encode($statement, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
115  ];
116  }
117 
118  return $data;
119  }
120 
124  protected function fetchDate(array $statement): string
125  {
126  return $statement['timestamp'];
127  }
128 
129  protected function fetchActor(array $statement): \ilCmiXapiUser
130  {
131  if ($this->isMixedContentType) {
132  $ident = str_replace('mailto:', '', $statement['actor']['mbox']);
133  if (empty($ident)) {
134  $ident = $statement['actor']['account']['name'];
135  }
136  } elseif ($this->contentType == ilObjCmiXapi::CONT_TYPE_CMI5) {
137  $ident = $statement['actor']['account']['name'];
138  } else {
139  $ident = str_replace('mailto:', '', $statement['actor']['mbox']);
140  }
141  return $this->cmixUsersByIdent[$ident];
142  }
143 
144  protected function fetchVerbId(array $statement): string
145  {
146  return $statement['verb']['id'];
147  }
148 
149  protected function fetchVerbDisplay(array $statement): string
150  {
151  try {
152  return $statement['verb']['display']['en-US'];
153  } catch (Exception $e) {
154  return $statement['verb']['id'];
155  }
156  }
157 
158  protected function fetchObjectName(array $statement): string
159  {
160  try {
161  $ret = urldecode($statement['object']['id']);
162  if (array_key_exists('definition', $statement['object'])) {
163  if (array_key_exists('name', $statement['object']['definition'])) {
164  $lang = self::getLanguageEntry($statement['object']['definition']['name'], $this->userLanguage);
165  if (array_key_exists('languageEntry', $lang)) {
166  $langEntry = $lang['languageEntry'];
167  if ($langEntry != '') {
168  $ret = $langEntry;
169  }
170  }
171  }
172  }
173  return $ret;
174  } catch (Exception $e) {
175  ilObjCmiXapi::log()->error('error:' . $e->getMessage());
176  return "";
177  }
178  }
179 
180  protected function fetchObjectInfo(array $statement): ?string
181  {
182  try {
183  return $statement['object']['definition']['description']['en-US'];
184  } catch (Exception $e) {
185  ilObjCmiXapi::log()->debug('debug:' . $e->getMessage());
186  return "";
187  }
188  }
189 
194  public static function getLanguageEntry(array $obj, string $userLanguage): array
195  {
196  $defaultLanguage = 'en-US';
197  $defaultLanguageEntry = '';
198  $defaultLanguageExists = false;
199  $firstLanguage = '';
200  $firstLanguageEntry = '';
201  $firstLanguageExists = false;
202  $userLanguage = '';
203  $userLanguageEntry = '';
204  $userLanguageExists = false;
205  $language = '';
206  $languageEntry = '';
207  try {
208  foreach ($obj as $k => $v) {
209  // save $firstLanguage
210  if ($firstLanguage == '') {
211  $f = '/^[a-z]+-?.*/';
212  if (preg_match($f, $k)) {
213  $firstLanguageExists = true;
214  $firstLanguage = $k;
215  $firstLanguageEntry = $v;
216  }
217  }
218  // check defaultLanguage
219  if ($k == $defaultLanguage) {
220  $defaultLanguageExists = true;
221  $defaultLanguageEntry = $v;
222  }
223  // check userLanguage
224  $p = '/^' . $userLanguage . '-?./';
225  preg_match($p, $k);
226  if (preg_match($p, $k)) {
227  $userLanguageExists = true;
228  $userLanguage = $k;
229  $userLanguageEntry = $v;
230  }
231  }
232  } catch (Exception $e) {
233  };
234 
235  if ($userLanguageExists) {
236  $language = $userLanguage;
237  $languageEntry = $userLanguageEntry;
238  } elseif ($defaultLanguageExists) {
239  $language = $userLanguage;
240  $languageEntry = $userLanguageEntry;
241  } elseif ($firstLanguageExists) {
242  $language = $firstLanguage;
243  $languageEntry = $firstLanguageEntry;
244  }
245  return ['language' => $language, 'languageEntry' => $languageEntry];
246  }
247 }
__construct(string $responseBody, int $objId)
$defaultLanguage
Definition: xapiexit.php:23
static getInstance(int $a_id=0, bool $a_reference=true)
$objId
Definition: xapitoken.php:57
static getInstance(int $a_id=0, bool $a_reference=true)
static getLanguageEntry(array $obj, string $userLanguage)
with multiple language keys like [de-DE] [en-US]
global $DIC
Definition: shib_login.php:22
$lang
Definition: xapiexit.php:25
static _lookupType(int $id, bool $reference=false)
static getUsersForObject(int $objId, bool $asUsrId=false)