ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilCmiXapiStatementsReport.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 $response;
21 
25  protected $statements;
26 
30  protected $maxCount;
31 
35  protected $cmixUsersByIdent;
36 
40  protected $userLanguage;
44  protected $contentType;
45 
50 
51  public function __construct(string $responseBody, $obj)
52  {
53  global $DIC;
54  $this->userLanguage = $DIC->user()->getLanguage();
55 
56  $responseBody = json_decode($responseBody, true);
57 
58  if ($obj instanceof ilObjCmiXapi) {
59  $this->contentType = $obj->getContentType();
60 // $this->contentType = ilObjCmiXapi::getInstance($objId,false)->getContentType();
61  $this->isMixedContentType = $obj->isMixedContentType();
62 // $this->isMixedContentType = ilObjCmiXapi::getInstance($objId,false)->isMixedContentType();
63  }
64  if (count($responseBody)) {
65  $this->response = current($responseBody);
66  $this->statements = $this->response['statements'];
67  $this->maxCount = $this->response['maxcount'];
68  } else {
69  $this->response = '';
70  $this->statements = array();
71  $this->maxCount = 0;
72  }
73 
74  foreach (ilCmiXapiUser::getUsersForObject($obj->getId()) as $cmixUser) {
75  $this->cmixUsersByIdent[$cmixUser->getUsrIdent()] = $cmixUser;
76  }
77  }
78 
79  public function getMaxCount()
80  {
81  return $this->maxCount;
82  }
83 
84  public function getStatements()
85  {
86  return $this->statements;
87  }
88 
89  public function hasStatements()
90  {
91  return (bool) count($this->statements);
92  }
93 
94  public function getTableData()
95  {
96  $data = [];
97 
98  foreach ($this->statements as $index => $statement) {
99  $data[] = [
100  'date' => $this->fetchDate($statement),
101  'actor' => $this->fetchActor($statement),
102  'verb_id' => $this->fetchVerbId($statement),
103  'verb_display' => $this->fetchVerbDisplay($statement),
104  'object' => $this->fetchObjectName($statement),
105  'object_info' => $this->fetchObjectInfo($statement),
106  'statement' => json_encode($statement, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
107  ];
108  }
109 
110  return $data;
111  }
112 
113  protected function fetchDate($statement)
114  {
115  return $statement['timestamp'];
116  }
117 
118  protected function fetchActor($statement)
119  {
120  if ($this->isMixedContentType)
121  {
122  $ident = str_replace('mailto:', '', $statement['actor']['mbox']);
123  if (empty($ident)) {
124  $ident = $statement['actor']['account']['name'];
125  }
126  }
127  elseif ($this->contentType == ilObjCmiXapi::CONT_TYPE_CMI5)
128  {
129  $ident = $statement['actor']['account']['name'];
130  }
131  else
132  {
133  $ident = str_replace('mailto:', '', $statement['actor']['mbox']);
134  }
135  return $this->cmixUsersByIdent[$ident];
136  }
137 
138  protected function fetchVerbId($statement)
139  {
140  return $statement['verb']['id'];
141  }
142 
143  protected function fetchVerbDisplay($statement)
144  {
145  return $statement['verb']['display']['en-US'];
146  }
147 
148  protected function fetchObjectName($statement)
149  {
150  $ret = urldecode($statement['object']['id']);
151  $lang = self::getLanguageEntry($statement['object']['definition']['name'],$this->userLanguage);
152  $langEntry = $lang['languageEntry'];
153  if ($langEntry != '')
154  {
155  $ret = $langEntry;
156  }
157  return $ret;
158  }
159 
160  protected function fetchObjectInfo($statement)
161  {
162  return $statement['object']['definition']['description']['en-US'];
163  }
164 
170  public static function getLanguageEntry($obj,$userLanguage)
171  {
172  $defaultLanguage = 'en-US';
173  $defaultLanguageEntry = '';
174  $defaultLanguageExists = false;
175  $firstLanguage = '';
176  $firstLanguageEntry = '';
177  $firstLanguageExists = false;
178  $userLanguage = '';
179  $userLanguageEntry = '';
180  $userLanguageExists = false;
181  $language = '';
182  $languageEntry = '';
183  try {
184  foreach ($obj as $k => $v)
185  {
186  // save $firstLanguage
187  if ($firstLanguage == '')
188  {
189  $f = '/^[a-z]+\-?.*/';
190  if (preg_match($f,$k))
191  {
192  $firstLanguageExists = true;
193  $firstLanguage = $k;
194  $firstLanguageEntry = $v;
195  }
196  }
197  // check defaultLanguage
198  if ($k == $defaultLanguage)
199  {
200  $defaultLanguageExists = true;
201  $defaultLanguageEntry = $v;
202  }
203  // check userLanguage
204  $p = '/^' . $userLanguage . '\-?./';
205  preg_match($p,$k);
206  if (preg_match($p,$k))
207  {
208  $userLanguageExists = true;
209  $userLanguage = $k;
210  $userLanguageEntry = $v;
211  }
212  }
213  }
214  catch (Exception $e) {};
215 
216  if ($userLanguageExists)
217  {
218  $language = $userLanguage;
219  $languageEntry = $userLanguageEntry;
220  }
221  elseif ($defaultLanguageExists)
222  {
223  $language = $userLanguage;
224  $languageEntry = $userLanguageEntry;
225  }
226  elseif ( $firstLanguageExists)
227  {
228  $language = $firstLanguage;
229  $languageEntry = $firstLanguageEntry;
230  }
231  return ['language' => $language, 'languageEntry' => $languageEntry];
232  }
233 }
static getUsersForObject($objId, $asUsrId=false)
$data
Definition: storeScorm.php:23
static getLanguageEntry($obj, $userLanguage)
$defaultLanguage
Definition: xapiexit.php:7
$index
Definition: metadata.php:128
__construct(string $responseBody, $obj)
$lang
Definition: xapiexit.php:8
$ret
Definition: parser.php:6
$DIC
Definition: xapitoken.php:46