Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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 activity object 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 core_xapi\iri;
29
use stdClass;
30
 
31
defined('MOODLE_INTERNAL') || die();
32
 
33
/**
34
 * Class that implements a xAPI activity compatible with xAPI object.
35
 *
36
 * @copyright  2020 Ferran Recio
37
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 */
39
class item_activity extends item_object {
40
 
41
    /** @var string Activity ID. */
42
    protected $id;
43
 
44
    /** @var item_definition Definition object. */
45
    protected $definition;
46
 
47
    /**
48
     * Item activity constructor.
49
     *
50
     * An xAPI activity is mainly an IRI ID and an optional definition.
51
     *
52
     * @param stdClass $data from the specific xAPI element
53
     * @param item_definition $definition option definition item
54
     */
55
    protected function __construct(stdClass $data, item_definition $definition = null) {
56
        parent::__construct($data);
57
        $this->id = iri::extract($data->id, 'activity');
58
        $this->definition = $definition;
59
    }
60
 
61
    /**
62
     * Function to create an item from part of the xAPI statement.
63
     *
64
     * @param stdClass $data the original xAPI element
65
     * @return item item_activity xAPI generated
66
     */
67
    public static function create_from_data(stdClass $data): item {
68
        if (!isset($data->objectType)) {
69
            throw new xapi_exception('Missing activity objectType');
70
        }
71
        if ($data->objectType != 'Activity') {
72
            throw new xapi_exception('Activity objectType must be "Activity"');
73
        }
74
        if (empty($data->id)) {
75
            throw new xapi_exception("Missing Activity id");
76
        }
77
        if (!iri::check($data->id)) {
78
            throw new xapi_exception("Activity id $data->id is not a valid IRI");
79
        }
80
 
81
        $definition = null;
82
        if (!empty($data->definition)) {
83
            $definition = item_definition::create_from_data($data->definition);
84
        }
85
 
86
        return new self($data, $definition);
87
    }
88
 
89
    /**
90
     * Generate a valid item_activity from a simple ID string and an optional definition.
91
     *
92
     * @param string $id any string that will converted into a valid IRI
93
     * @param item_definition|null $definition optional item_definition
94
     * @return item_activity
95
     */
96
    public static function create_from_id(string $id, item_definition $definition = null): item_activity {
97
        $data = (object) [
98
            'objectType' => 'Activity',
99
            'id' => iri::generate($id, 'activity'),
100
        ];
101
 
102
        if (!empty($definition)) {
103
            $data->definition = $definition->get_data();
104
        }
105
 
106
        return new self($data, $definition);
107
    }
108
 
109
    /**
110
     * Return the activity ID.
111
     *
112
     * If the ID was generated by iri::generate this function will return
113
     * the iri:extract value.
114
     *
115
     * @return string the activity ID
116
     */
117
    public function get_id(): string {
118
        return $this->id;
119
    }
120
 
121
    /**
122
     * Returns the item_definition of this item.
123
     *
124
     * @return item_definition|null the item definition if available
125
     */
126
    public function get_definition(): ?item_definition {
127
        return $this->definition;
128
    }
129
}