ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilCmiXapiHighscoreReport.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  private $tableData = [];
26 
30  private $userRank = null;
31 
35  protected $cmixUsersByIdent;
36 
40  protected $objId;
41 
42  protected $obj;
47  public function __construct(string $responseBody, $obj)
48  {
49  $this->obj = $obj;
50  $this->objId = $obj->getId();
51  $responseBody = json_decode($responseBody, true);
52 
53  if (count($responseBody)) {
54  $this->response = $responseBody;
55  } else {
56  $this->response = array();
57  }
58 
59  foreach (ilCmiXapiUser::getUsersForObject($this->objId) as $cmixUser) {
60  $this->cmixUsersByIdent[$cmixUser->getUsrIdent()] = $cmixUser;
61  }
62  }
63 
67  public function initTableData()
68  {
69  global $DIC; /* @var \ILIAS\DI\Container $DIC */
70 
71  $rows = [];
72 
73  if ($this->obj instanceof ilObjCmiXapi && $this->obj->isMixedContentType())
74  {
75  foreach ($this->response as $item) {
76  $userIdent = str_replace('mailto:', '', $item['mbox']);
77  if (empty($userIdent))
78  {
79  $userIdent = $item['account'];
80  }
81  $cmixUser = $this->cmixUsersByIdent[$userIdent];
82  $rows[] = [
83  'user_ident' => $userIdent,
84  'user' => '',
85  'date' => $this->formatRawTimestamp($item['timestamp']),
86  'duration' => $this->fetchTotalDuration($item['duration']),
87  'score' => $item['score']['scaled'],
88  'ilias_user_id' => $cmixUser->getUsrId()
89  ];
90  }
91  }
92  elseif ($this->obj instanceof ilObjCmiXapi && $this->obj->getContentType() == ilObjCmiXapi::CONT_TYPE_CMI5)
93  {
94  foreach ($this->response as $item) {
95  $userIdent = $item['account'];
96  $cmixUser = $this->cmixUsersByIdent[$userIdent];
97  $rows[] = [
98  'user_ident' => $userIdent,
99  'user' => '',
100  'date' => $this->formatRawTimestamp($item['timestamp']),
101  'duration' => $this->fetchTotalDuration($item['duration']),
102  'score' => $item['score']['scaled'],
103  'ilias_user_id' => $cmixUser->getUsrId()
104  ];
105  }
106  }
107  else
108  {
109  foreach ($this->response as $item) {
110  $userIdent = str_replace('mailto:', '', $item['mbox']);
111  $cmixUser = $this->cmixUsersByIdent[$userIdent];
112  $rows[] = [
113  'user_ident' => $userIdent,
114  'user' => '',
115  'date' => $this->formatRawTimestamp($item['timestamp']),
116  'duration' => $this->fetchTotalDuration($item['duration']),
117  'score' => $item['score']['scaled'],
118  'ilias_user_id' => $cmixUser->getUsrId()
119  ];
120  }
121  }
122  usort($rows, function ($a, $b) {
123  return $a['score'] != $b['score'] ? $a['score'] > $b['score'] ? -1 : 1 : 0;
124  });
125 
126  $i = 0;
127  $prevScore = null;
128  //$userRank = null;
129  $retArr = [];
130  foreach ($rows as $key => $item) {
131  if ($prevScore !== $item['score']) {
132  $i++;
133  }
134  $rows[$key]['rank'] = $i;
135  $prevScore = $rows[$key]['score'];
136  /* instantiate userObj until loginUserRank is unknown */
137  if (null === $this->userRank) {
138  /* just boolean */
139  $userIdent = str_replace('mailto:', '', $rows[$key]['user_ident']);
140  $cmixUser = $this->cmixUsersByIdent[$userIdent];
141  if ($cmixUser->getUsrId() == $DIC->user()->getId()) {
142  $this->userRank = $key; //$rows[$key]['rank'];
143  $userObj = ilObjectFactory::getInstanceByObjId($cmixUser->getUsrId());
144  $rows[$key]['user'] = $userObj->getFullname();
145  }
146  $retArr[$key] = $rows[$key];
147  } else {
148  /* same same */
149  $rows[$key]['user_ident'] = false;
150  $retArr[$key] = $rows[$key];
151  } // EOF if( null === $this->userRank )
152  } // EOF foreach ($rows as $key => $item)
153  $this->tableData = $retArr;
154  return true;
155  }
156 
157  private function identUser($userIdent)
158  {
159  global $DIC; /* @var \ILIAS\DI\Container $DIC */
160 
161  $cmixUser = $this->cmixUsersByIdent[$userIdent];
162 
163  if ($cmixUser->getUsrId() == $DIC->user()->getId()) {
164  return true;
165  }
166  return false;
167  }
168 
169  protected function fetchTotalDuration($allDurations)
170  {
171  $totalDuration = 0;
172 
173  foreach ($allDurations as $duration) {
174  $totalDuration += ilObjSCORM2004LearningModule::_ISODurationToCentisec($duration) / 100;
175  }
176 
177  $hours = floor($totalDuration / 3600);
178  $hours = strlen($hours) < 2 ? "0" . $hours : $hours;
179  $totalDuration = $hours . ":" . date('i:s', $totalDuration);
180 
181  return $totalDuration;
182  }
183 
184  private function formatRawTimestamp($rawTimestamp)
185  {
186  $dateTime = ilCmiXapiDateTime::fromXapiTimestamp($rawTimestamp);
187  return ilDatePresentation::formatDate($dateTime);
188  }
189 
190  public function getTableData()
191  {
192  return $this->tableData;
193  }
194 
195  public function getUserRank()
196  {
197  return $this->userRank;
198  }
199 
200  public function getResponseDebug()
201  {
202  /*
203  foreach($this->response as $key => $item)
204  {
205  $user = ilCmiXapiUser::getUserFromIdent(
206  ilObjectFactory::getInstanceByRefId($_GET['ref_id']),
207  $tableRowData['mbox']
208  );
209 
210  $this->response[$key]['realname'] = $user->getFullname();
211  }
212  */
213  return '<pre>' . json_encode($this->response, JSON_PRETTY_PRINT) . '</pre>';
214  }
215 }
static getUsersForObject($objId, $asUsrId=false)
__construct(string $responseBody, $obj)
ilCmiXapiHighscoreReport constructor.
static formatDate(ilDateTime $date, $a_skip_day=false, $a_include_wd=false, $include_seconds=false)
Format a date public.
static getInstanceByObjId($a_obj_id, $stop_on_error=true)
get an instance of an Ilias object by object id
$rows
Definition: xhr_table.php:10
$DIC
Definition: xapitoken.php:46
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
static _ISODurationToCentisec($str)
convert ISO 8601 Timeperiods to centiseconds ta
static fromXapiTimestamp($xapiTimestamp)
$i
Definition: metadata.php:24