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