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