1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Statement result for xAPI structure checking and usage.
|
|
|
19 |
*
|
|
|
20 |
* @package core_xapi
|
|
|
21 |
* @copyright 2020 Ferran Recio
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
namespace core_xapi\local\statement;
|
|
|
26 |
|
|
|
27 |
use core_xapi\xapi_exception;
|
|
|
28 |
use DateInterval;
|
|
|
29 |
use Exception;
|
|
|
30 |
use stdClass;
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* Abstract xAPI result class.
|
|
|
34 |
*
|
|
|
35 |
* @copyright 2020 Ferran Recio
|
|
|
36 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
37 |
*/
|
|
|
38 |
class item_result extends item {
|
|
|
39 |
|
|
|
40 |
/** @var int The second of duration if present. */
|
|
|
41 |
protected $duration;
|
|
|
42 |
|
|
|
43 |
/** @var item_score the result score if present. */
|
|
|
44 |
protected $score;
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
* Function to create a result from part of the xAPI statement.
|
|
|
48 |
*
|
|
|
49 |
* @param stdClass $data the original xAPI element
|
|
|
50 |
* @param int $duration duration in seconds
|
|
|
51 |
* @param item_score $score the provided score
|
|
|
52 |
*/
|
|
|
53 |
protected function __construct(stdClass $data, int $duration = null, item_score $score = null) {
|
|
|
54 |
parent::__construct($data);
|
|
|
55 |
$this->duration = $duration;
|
|
|
56 |
$this->score = $score;
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
/**
|
|
|
60 |
* Function to create an item from part of the xAPI statement.
|
|
|
61 |
*
|
|
|
62 |
* @param stdClass $data the original xAPI element
|
|
|
63 |
* @return item item_result xAPI generated
|
|
|
64 |
*/
|
|
|
65 |
public static function create_from_data(stdClass $data): item {
|
|
|
66 |
|
|
|
67 |
$duration = null;
|
|
|
68 |
if (!empty($data->duration)) {
|
|
|
69 |
try {
|
|
|
70 |
// Duration uses ISO 8601 format which is ALMOST compatible with PHP DateInterval.
|
|
|
71 |
// Because we are mesuring human time we get rid of milliseconds, which are not
|
|
|
72 |
// compatible with DateInterval (More info: https://bugs.php.net/bug.php?id=53831),
|
|
|
73 |
// all other fractions like "P1.5Y" will throw an exception.
|
|
|
74 |
$value = preg_replace('/[.,][0-9]*S/', 'S', $data->duration);
|
|
|
75 |
$interval = new DateInterval($value);
|
|
|
76 |
$duration = date_create('@0')->add($interval)->getTimestamp();
|
|
|
77 |
} catch (Exception $e) {
|
|
|
78 |
throw new xapi_exception('Invalid duration format.');
|
|
|
79 |
}
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
$score = null;
|
|
|
83 |
if (!empty($data->score)) {
|
|
|
84 |
$score = item_score::create_from_data($data->score);
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
return new self($data, $duration, $score);
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
/**
|
|
|
91 |
* Returns the duration in seconds (if present).
|
|
|
92 |
*
|
|
|
93 |
* @return int|null duration in seconds
|
|
|
94 |
*/
|
|
|
95 |
public function get_duration(): ?int {
|
|
|
96 |
return $this->duration;
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
/**
|
|
|
100 |
* Returns the score.
|
|
|
101 |
*
|
|
|
102 |
* @return item_score|null the score item
|
|
|
103 |
*/
|
|
|
104 |
public function get_score(): ?item_score {
|
|
|
105 |
return $this->score;
|
|
|
106 |
}
|
|
|
107 |
}
|