first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
+79
View File
@@ -0,0 +1,79 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Statement base object for xAPI structure checking and usage.
*
* @package core_xapi
* @copyright 2020 Ferran Recio
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_xapi\local\statement;
use stdClass;
use JsonSerializable;
use core_xapi\iri;
defined('MOODLE_INTERNAL') || die();
/**
* Item class used for xAPI statement elements without validation.
*
* @copyright 2020 Ferran Recio
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class item implements JsonSerializable {
/** @var stdClass the item structure. */
protected $data;
/**
* Item constructor.
*
* @param stdClass $data from the specific xAPI element
*/
protected function __construct(stdClass $data) {
$this->data = $data;
}
/**
* Function to create an item from part of the xAPI statement.
*
* @param stdClass $data the original xAPI element
* @return item the xAPI item generated
*/
public static function create_from_data(stdClass $data): item {
return new self($data);
}
/**
* Return the data to serialize in case JSON statement is needed.
*
* @return stdClass the original data structure
*/
public function jsonSerialize(): stdClass {
return $this->get_data();
}
/**
* Return the original data from this item.
*
* @return stdClass the original data structure
*/
public function get_data(): stdClass {
return $this->data;
}
}
@@ -0,0 +1,129 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Statement activity object for xAPI structure checking and usage.
*
* @package core_xapi
* @copyright 2020 Ferran Recio
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_xapi\local\statement;
use core_xapi\xapi_exception;
use core_xapi\iri;
use stdClass;
defined('MOODLE_INTERNAL') || die();
/**
* Class that implements a xAPI activity compatible with xAPI object.
*
* @copyright 2020 Ferran Recio
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class item_activity extends item_object {
/** @var string Activity ID. */
protected $id;
/** @var item_definition Definition object. */
protected $definition;
/**
* Item activity constructor.
*
* An xAPI activity is mainly an IRI ID and an optional definition.
*
* @param stdClass $data from the specific xAPI element
* @param item_definition $definition option definition item
*/
protected function __construct(stdClass $data, item_definition $definition = null) {
parent::__construct($data);
$this->id = iri::extract($data->id, 'activity');
$this->definition = $definition;
}
/**
* Function to create an item from part of the xAPI statement.
*
* @param stdClass $data the original xAPI element
* @return item item_activity xAPI generated
*/
public static function create_from_data(stdClass $data): item {
if (!isset($data->objectType)) {
throw new xapi_exception('Missing activity objectType');
}
if ($data->objectType != 'Activity') {
throw new xapi_exception('Activity objectType must be "Activity"');
}
if (empty($data->id)) {
throw new xapi_exception("Missing Activity id");
}
if (!iri::check($data->id)) {
throw new xapi_exception("Activity id $data->id is not a valid IRI");
}
$definition = null;
if (!empty($data->definition)) {
$definition = item_definition::create_from_data($data->definition);
}
return new self($data, $definition);
}
/**
* Generate a valid item_activity from a simple ID string and an optional definition.
*
* @param string $id any string that will converted into a valid IRI
* @param item_definition|null $definition optional item_definition
* @return item_activity
*/
public static function create_from_id(string $id, item_definition $definition = null): item_activity {
$data = (object) [
'objectType' => 'Activity',
'id' => iri::generate($id, 'activity'),
];
if (!empty($definition)) {
$data->definition = $definition->get_data();
}
return new self($data, $definition);
}
/**
* Return the activity ID.
*
* If the ID was generated by iri::generate this function will return
* the iri:extract value.
*
* @return string the activity ID
*/
public function get_id(): string {
return $this->id;
}
/**
* Returns the item_definition of this item.
*
* @return item_definition|null the item definition if available
*/
public function get_definition(): ?item_definition {
return $this->definition;
}
}
@@ -0,0 +1,79 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Statement actor (user or group) object for xAPI structure checking and usage.
*
* @package core_xapi
* @copyright 2020 Ferran Recio
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_xapi\local\statement;
use core_xapi\xapi_exception;
use stdClass;
defined('MOODLE_INTERNAL') || die();
/**
* Abstract xAPI actor class.
*
* This class extends from item_object instead of basic item
* because both actors (agent and group) could be used as
* statement actor or object.
*
* @copyright 2020 Ferran Recio
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class item_actor extends item_object {
/**
* Function to create an actor from part of the xAPI statement.
*
* @param stdClass $data the original xAPI element
* @return item item_agent|item_grou|item_activity xAPI generated
*/
public static function create_from_data(stdClass $data): item {
if (!isset($data->objectType)) {
$data->objectType = 'Agent';
}
switch ($data->objectType) {
case 'Agent':
return item_agent::create_from_data($data);
break;
case 'Group':
return item_group::create_from_data($data);
break;
default:
throw new xapi_exception("Unknown Actor type '{$data->objectType}'");
}
}
/**
* Returns the moodle user represented by this item.
*
* @return stdClass user record
*/
abstract public function get_user(): stdClass;
/**
* Return all moodle users represented by this item.
*
* @return array user records
*/
abstract public function get_all_users(): array;
}
@@ -0,0 +1,142 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Statement agent (user) object for xAPI structure checking and usage.
*
* @package core_xapi
* @copyright 2020 Ferran Recio
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_xapi\local\statement;
use core_xapi\xapi_exception;
use core_user;
use stdClass;
defined('MOODLE_INTERNAL') || die();
/**
* Agent xAPI statement element representing a Moodle user.
*
* Agents can be used either as actor or object in a statement.
*
* @copyright 2020 Ferran Recio
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class item_agent extends item_actor {
/** @var stdClass The user record of this actor. */
protected $user;
/**
* Function to create an agent (user) from part of the xAPI statement.
*
* @param stdClass $data the original xAPI element
* @param stdClass $user user record
*/
protected function __construct(stdClass $data, stdClass $user) {
parent::__construct($data);
$this->user = $user;
}
/**
* Function to create an item from part of the xAPI statement.
*
* @param stdClass $data the original xAPI element
* @return item item_agentxAPI generated
*/
public static function create_from_data(stdClass $data): item {
global $CFG;
if (!isset($data->objectType)) {
throw new xapi_exception('Missing agent objectType');
}
if ($data->objectType != 'Agent') {
throw new xapi_exception("Agent objectType must be 'Agent'");
}
if (isset($data->account) && isset($data->mbox)) {
throw new xapi_exception("Agent cannot have more than one identifier");
}
$user = null;
if (!empty($data->account)) {
if ($data->account->homePage != $CFG->wwwroot) {
throw new xapi_exception("Invalid agent homePage '{$data->account->homePage}'");
}
if (!is_numeric($data->account->name)) {
throw new xapi_exception("Agent account name must be integer '{$data->account->name}' found");
}
$user = core_user::get_user($data->account->name);
if (empty($user)) {
throw new xapi_exception("Inexistent agent '{$data->account->name}'");
}
}
if (!empty($data->mbox)) {
$mbox = str_replace('mailto:', '', $data->mbox);
$user = core_user::get_user_by_email($mbox);
if (empty($user)) {
throw new xapi_exception("Inexistent agent '{$data->mbox}'");
}
}
if (empty($user)) {
throw new xapi_exception("Unsupported agent definition");
}
return new self($data, $user);
}
/**
* Create a item_agent from a existing user.
*
* @param stdClass $user A user record.
* @return item_agent
*/
public static function create_from_user(stdClass $user): item_agent {
global $CFG;
if (!isset($user->id)) {
throw new xapi_exception("Missing user id");
}
$data = (object) [
'objectType' => 'Agent',
'account' => (object) [
'homePage' => $CFG->wwwroot,
'name' => $user->id,
],
];
return new self($data, $user);
}
/**
* Returns the moodle user represented by this item.
*
* @return stdClass user record
*/
public function get_user(): stdClass {
return $this->user;
}
/**
* Return all users represented by this item.
*
* In this case the item is an agent so a single element array
* will be returned always.
*
* @return array list of users
*/
public function get_all_users(): array {
return [$this->user->id => $this->user];
}
}
@@ -0,0 +1,73 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Statement attachment for xAPI structure checking and usage.
*
* @package core_xapi
* @copyright 2020 Ferran Recio
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_xapi\local\statement;
use core_xapi\xapi_exception;
use core_xapi\iri;
use stdClass;
/**
* Abstract xAPI attachment class.
*
* @copyright 2020 Ferran Recio
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class item_attachment extends item {
/**
* Function to create an item from part of the xAPI statement.
*
* @param stdClass $data the original xAPI element
* @return item item_attachment xAPI generated
*/
public static function create_from_data(stdClass $data): item {
if (empty($data->usageType)) {
throw new xapi_exception("missing attachment usageType");
}
if (!iri::check($data->usageType)) {
throw new xapi_exception("attachment usageType $data->usageType is not a valid IRI");
}
if (empty($data->display)) {
throw new xapi_exception("missing attachment display");
}
if (empty($data->contentType)) {
throw new xapi_exception("missing attachment contentType");
}
if (empty($data->length)) {
throw new xapi_exception("missing attachment length");
}
if (!is_numeric($data->length)) {
throw new xapi_exception("invalid attachment length format");
}
if (empty($data->sha2)) {
throw new xapi_exception("missing attachment sha2");
}
// More required property checks will appear here in the future.
return new self($data);
}
}
@@ -0,0 +1,49 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Statement context for xAPI structure checking and usage.
*
* @package core_xapi
* @copyright 2020 Ferran Recio
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_xapi\local\statement;
use stdClass;
/**
* Abstract xAPI context class.
*
* @copyright 2020 Ferran Recio
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class item_context extends item {
/**
* Function to create an item from part of the xAPI statement.
*
* @param stdClass $data the original xAPI element
* @return item item_context xAPI generated
*/
public static function create_from_data(stdClass $data): item {
// Required property checks will appear here in the future.
return new self($data);
}
}
@@ -0,0 +1,93 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Statement definition object for xAPI structure checking and usage.
*
* @package core_xapi
* @copyright 2020 Ferran Recio
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_xapi\local\statement;
use core_xapi\xapi_exception;
use core_xapi\iri;
use stdClass;
defined('MOODLE_INTERNAL') || die();
/**
* Validation and usage of xAPI definition.
*
* Definition contains extra information about user interaction with
* questions and other activities inside a xAPI statement. For now
* it performs a basic validation on the provided data.
*
* @copyright 2020 Ferran Recio
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class item_definition extends item {
/** @var string The statement. */
protected $interactiontype;
/**
* Function to create a definition from part of the xAPI statement.
*
* @param stdClass $data the original xAPI element.
*/
protected function __construct(stdClass $data) {
parent::__construct($data);
$this->interactiontype = $data->interactionType ?? null;
}
/**
* Function to create an item from part of the xAPI statement.
*
* @param stdClass $data the original xAPI element
* @return item item_definition xAPI generated
*/
public static function create_from_data(stdClass $data): item {
// Interaction Type is a optopnal param.
if (!empty($data->interactionType)) {
$posiblevalues = [
'choice' => true,
'fill-in' => true,
'long-fill-in' => true,
'true-false' => true,
'matching' => true,
'performance' => true,
'sequencing' => true,
'likert' => true,
'numeric' => true,
'other' => true,
'compound' => true,
];
if (!isset($posiblevalues[$data->interactionType])) {
throw new xapi_exception("Invalid definition \"{$data->interactionType}\"");
}
}
return new self($data);
}
/**
* Return the definition interaction type.
*/
public function get_interactiontype(): ?string {
return $this->interactiontype;
}
}
@@ -0,0 +1,147 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Statement group object for xAPI structure checking and usage.
*
* @package core_xapi
* @copyright 2020 Ferran Recio
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_xapi\local\statement;
use core_xapi\xapi_exception;
use stdClass;
defined('MOODLE_INTERNAL') || die();
/**
* Group item inside a xAPI statement.
*
* Only named groups are accepted (all groups must be real groups in the
* platform) so anonymous groups will be rejected on creation. Groups can
* be used as actor or as object inside a xAPI statement.
*
* @copyright 2020 Ferran Recio
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class item_group extends item_actor {
/** @var array */
protected $users;
/** @var stdClass */
protected $group;
/**
* Function to create an group from part of the xAPI statement.
*
* @param stdClass $data the original xAPI element
* @param stdClass $group group record
*/
protected function __construct(stdClass $data, stdClass $group) {
parent::__construct($data);
$this->group = $group;
$this->users = groups_get_members($group->id);
if (!$this->users) {
$this->users = [];
}
}
/**
* Function to create an item from part of the xAPI statement.
*
* @param stdClass $data the original xAPI element
* @return item item_group xAPI item generated
*/
public static function create_from_data(stdClass $data): item {
global $CFG;
if (!isset($data->objectType)) {
throw new xapi_exception('Missing group objectType');
}
if ($data->objectType != 'Group') {
throw new xapi_exception("Group objectType must be 'Group'");
}
if (!isset($data->account)) {
throw new xapi_exception("Missing Group account");
}
if ($data->account->homePage != $CFG->wwwroot) {
throw new xapi_exception("Invalid group homePage '{$data->account->homePage}'");
}
if (!is_numeric($data->account->name)) {
throw new xapi_exception("Agent account name must be integer '{$data->account->name}' found");
}
$group = groups_get_group($data->account->name);
if (empty($group)) {
throw new xapi_exception("Inexistent group '{$data->account->name}'");
}
return new self($data, $group);
}
/**
* Create a item_group from a existing group.
*
* @param stdClass $group A group record.
* @return item_group
*/
public static function create_from_group(stdClass $group): item_group {
global $CFG;
if (!isset($group->id)) {
throw new xapi_exception("Missing group id");
}
$data = (object) [
'objectType' => 'Group',
'account' => (object) [
'homePage' => $CFG->wwwroot,
'name' => $group->id,
],
];
return new self($data, $group);
}
/**
* Returns the moodle user represented by this item.
*
* This is a group item. To avoid security problems this method
* thorws an exception when is called from a item_group class.
*
* @throws xapi_exception get_user must not be called from an item_group
* @return stdClass user record
*/
public function get_user(): stdClass {
throw new xapi_exception("Group statements cannot be used as a individual user");
}
/**
* Return all users from the group represented by this item.
*
* @return array group users
*/
public function get_all_users(): array {
return $this->users;
}
/**
* Return the moodle group represented by this item.
*
* @return stdClass a group record
*/
public function get_group(): stdClass {
return $this->group;
}
}
@@ -0,0 +1,69 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Statement object (activity, user or group) for xAPI structure checking and usage.
*
* @package core_xapi
* @copyright 2020 Ferran Recio
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_xapi\local\statement;
use core_xapi\xapi_exception;
use core_xapi\iri;
use stdClass;
defined('MOODLE_INTERNAL') || die();
/**
* Abstract object item used in xAPI statements.
*
* Object represents the object in which a xAPI verb is applied. There
* are 3 types of objects supported: agent (user), group (of users) and
* activity (defined by every plugin).
*
* @copyright 2020 Ferran Recio
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class item_object extends item {
/**
* Create a xAPI object compatible from data (Agent, Group or Activity).
*
* @param stdClass $data data structure from statement object
* @return item item_group|item_agent|item_activity resulting object
*/
public static function create_from_data(stdClass $data): item {
if (!isset($data->objectType)) {
$data->objectType = 'Activity';
}
switch ($data->objectType) {
case 'Agent':
return item_agent::create_from_data($data);
break;
case 'Group':
return item_group::create_from_data($data);
break;
case 'Activity':
return item_activity::create_from_data($data);
break;
default:
throw new xapi_exception("Unknown Object type '{$data->objectType}'");
}
}
}
@@ -0,0 +1,107 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Statement result for xAPI structure checking and usage.
*
* @package core_xapi
* @copyright 2020 Ferran Recio
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_xapi\local\statement;
use core_xapi\xapi_exception;
use DateInterval;
use Exception;
use stdClass;
/**
* Abstract xAPI result class.
*
* @copyright 2020 Ferran Recio
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class item_result extends item {
/** @var int The second of duration if present. */
protected $duration;
/** @var item_score the result score if present. */
protected $score;
/**
* Function to create a result from part of the xAPI statement.
*
* @param stdClass $data the original xAPI element
* @param int $duration duration in seconds
* @param item_score $score the provided score
*/
protected function __construct(stdClass $data, int $duration = null, item_score $score = null) {
parent::__construct($data);
$this->duration = $duration;
$this->score = $score;
}
/**
* Function to create an item from part of the xAPI statement.
*
* @param stdClass $data the original xAPI element
* @return item item_result xAPI generated
*/
public static function create_from_data(stdClass $data): item {
$duration = null;
if (!empty($data->duration)) {
try {
// Duration uses ISO 8601 format which is ALMOST compatible with PHP DateInterval.
// Because we are mesuring human time we get rid of milliseconds, which are not
// compatible with DateInterval (More info: https://bugs.php.net/bug.php?id=53831),
// all other fractions like "P1.5Y" will throw an exception.
$value = preg_replace('/[.,][0-9]*S/', 'S', $data->duration);
$interval = new DateInterval($value);
$duration = date_create('@0')->add($interval)->getTimestamp();
} catch (Exception $e) {
throw new xapi_exception('Invalid duration format.');
}
}
$score = null;
if (!empty($data->score)) {
$score = item_score::create_from_data($data->score);
}
return new self($data, $duration, $score);
}
/**
* Returns the duration in seconds (if present).
*
* @return int|null duration in seconds
*/
public function get_duration(): ?int {
return $this->duration;
}
/**
* Returns the score.
*
* @return item_score|null the score item
*/
public function get_score(): ?item_score {
return $this->score;
}
}
@@ -0,0 +1,49 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Statement score for xAPI structure checking and usage.
*
* @package core_xapi
* @copyright 2020 Ferran Recio
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_xapi\local\statement;
use stdClass;
/**
* Abstract xAPI score class.
*
* @copyright 2020 Ferran Recio
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class item_score extends item {
/**
* Function to create an item from part of the xAPI statement.
*
* @param stdClass $data the original xAPI element
* @return item item_score xAPI generated
*/
public static function create_from_data(stdClass $data): item {
// Required property checks will appear here in the future.
return new self($data);
}
}
@@ -0,0 +1,103 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Statement verb object for xAPI structure checking and usage.
*
* @package core_xapi
* @copyright 2020 Ferran Recio
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_xapi\local\statement;
use core_xapi\xapi_exception;
use core_xapi\iri;
use stdClass;
defined('MOODLE_INTERNAL') || die();
/**
* Verb xAPI statement item.
*
* Verbs represent the interaction a user/group made inside a xAPI
* compatible plugin. Internally a xAPI verb must be representad as
* in a valid IRI format (which is a less restrictive version of a
* regular URL so a moodle_url out is completelly fine). To make it
* easy for plugins to generate valid IRI, a simple string van be
* provided and the class will convert into a valid IRI internally.
*
* @copyright 2020 Ferran Recio
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class item_verb extends item {
/** @var string The statement. */
protected $id;
/**
* An xAPI verb constructor based on xAPI data structure.
*
* @param stdClass $data from the specific xAPI element
*/
protected function __construct(stdClass $data) {
parent::__construct($data);
$this->id = iri::extract($data->id, 'verb');
}
/**
* Function to create an item from part of the xAPI statement.
*
* @param stdClass $data the original xAPI element
* @return item item_verb xAPI generated
*/
public static function create_from_data(stdClass $data): item {
if (empty($data->id)) {
throw new xapi_exception("missing verb id");
}
if (!iri::check($data->id)) {
throw new xapi_exception("verb id $data->id is not a valid IRI");
}
return new self($data);
}
/**
* Create a valid item_verb from a simple verb string.
*
* @param string $id string to convert to a valid IRI (or a valid IRI)
* @return item_verb the resulting item_verb
*/
public static function create_from_id(string $id): item_verb {
$data = new stdClass();
$data->id = iri::generate($id, 'verb');
return new self($data);
}
/**
* Return the id used in this item.
*
* Id will be extracted from the provided IRI. If it's a valid IRI
* it will return all IRI value but if it is generate by the iri helper
* from this library it will extract the original value.
*
* @return string the ID (extracted from IRI value)
*/
public function get_id(): string {
return $this->id;
}
}