| 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 | 
           namespace core_xapi\local;
  | 
        
        
            | 
            | 
           18 | 
              | 
        
        
            | 
            | 
           19 | 
           use core_xapi\local\statement\item_agent;
  | 
        
        
            | 
            | 
           20 | 
           use core_xapi\local\statement\item_activity;
  | 
        
        
            | 
            | 
           21 | 
           use JsonSerializable;
  | 
        
        
            | 
            | 
           22 | 
           use stdClass;
  | 
        
        
            | 
            | 
           23 | 
              | 
        
        
            | 
            | 
           24 | 
           /**
  | 
        
        
            | 
            | 
           25 | 
            * State resource object for xAPI structure checking and validation.
  | 
        
        
            | 
            | 
           26 | 
            *
  | 
        
        
            | 
            | 
           27 | 
            * @package    core_xapi
  | 
        
        
            | 
            | 
           28 | 
            * @since      Moodle 4.2
  | 
        
        
            | 
            | 
           29 | 
            * @copyright  2023 Ferran Recio
  | 
        
        
            | 
            | 
           30 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           31 | 
            */
  | 
        
        
            | 
            | 
           32 | 
           class state implements JsonSerializable {
  | 
        
        
            | 
            | 
           33 | 
              | 
        
        
            | 
            | 
           34 | 
               /** @var item_agent The state agent (user). */
  | 
        
        
            | 
            | 
           35 | 
               protected $agent = null;
  | 
        
        
            | 
            | 
           36 | 
              | 
        
        
            | 
            | 
           37 | 
               /** @var item_activity The state activity owner (the plugin instance). */
  | 
        
        
            | 
            | 
           38 | 
               protected $activity = null;
  | 
        
        
            | 
            | 
           39 | 
              | 
        
        
            | 
            | 
           40 | 
               /** @var string The state identifier. */
  | 
        
        
            | 
            | 
           41 | 
               protected $stateid = null;
  | 
        
        
            | 
            | 
           42 | 
              | 
        
        
            | 
            | 
           43 | 
               /** @var stdClass|null The state data. */
  | 
        
        
            | 
            | 
           44 | 
               protected $statedata = null;
  | 
        
        
            | 
            | 
           45 | 
              | 
        
        
            | 
            | 
           46 | 
               /** @var string|null The state registration. */
  | 
        
        
            | 
            | 
           47 | 
               protected $registration = null;
  | 
        
        
            | 
            | 
           48 | 
              | 
        
        
            | 
            | 
           49 | 
               /**
  | 
        
        
            | 
            | 
           50 | 
                * State constructor.
  | 
        
        
            | 
            | 
           51 | 
                *
  | 
        
        
            | 
            | 
           52 | 
                * @param item_agent $agent The state agent (user)
  | 
        
        
            | 
            | 
           53 | 
                * @param item_activity $activity The state activity owner
  | 
        
        
            | 
            | 
           54 | 
                * @param string $stateid The state identifier
  | 
        
        
            | 
            | 
           55 | 
                * @param stdClass|null $statedata The state data
  | 
        
        
            | 
            | 
           56 | 
                * @param string|null $registration The state registration
  | 
        
        
            | 
            | 
           57 | 
                */
  | 
        
        
            | 
            | 
           58 | 
               public function __construct(
  | 
        
        
            | 
            | 
           59 | 
                   item_agent $agent,
  | 
        
        
            | 
            | 
           60 | 
                   item_activity $activity,
  | 
        
        
            | 
            | 
           61 | 
                   string $stateid,
  | 
        
        
            | 
            | 
           62 | 
                   ?stdClass $statedata,
  | 
        
        
            | 
            | 
           63 | 
                   ?string $registration
  | 
        
        
            | 
            | 
           64 | 
               ) {
  | 
        
        
            | 
            | 
           65 | 
                   $this->agent = $agent;
  | 
        
        
            | 
            | 
           66 | 
                   $this->activity = $activity;
  | 
        
        
            | 
            | 
           67 | 
                   $this->stateid = $stateid;
  | 
        
        
            | 
            | 
           68 | 
                   $this->statedata = $statedata;
  | 
        
        
            | 
            | 
           69 | 
                   $this->registration = $registration;
  | 
        
        
            | 
            | 
           70 | 
               }
  | 
        
        
            | 
            | 
           71 | 
              | 
        
        
            | 
            | 
           72 | 
               /**
  | 
        
        
            | 
            | 
           73 | 
                * Return the data to serialize in case JSON state when needed.
  | 
        
        
            | 
            | 
           74 | 
                *
  | 
        
        
            | 
            | 
           75 | 
                * @return stdClass The state data structure. If statedata is null, this method will return an empty class.
  | 
        
        
            | 
            | 
           76 | 
                */
  | 
        
        
            | 
            | 
           77 | 
               public function jsonSerialize(): stdClass {
  | 
        
        
            | 
            | 
           78 | 
                   if ($this->statedata) {
  | 
        
        
            | 
            | 
           79 | 
                       return $this->statedata;
  | 
        
        
            | 
            | 
           80 | 
                   }
  | 
        
        
            | 
            | 
           81 | 
              | 
        
        
            | 
            | 
           82 | 
                   return new stdClass();
  | 
        
        
            | 
            | 
           83 | 
               }
  | 
        
        
            | 
            | 
           84 | 
              | 
        
        
            | 
            | 
           85 | 
               /**
  | 
        
        
            | 
            | 
           86 | 
                * Return the record data of this state.
  | 
        
        
            | 
            | 
           87 | 
                *
  | 
        
        
            | 
            | 
           88 | 
                * @return stdClass the record data structure
  | 
        
        
            | 
            | 
           89 | 
                */
  | 
        
        
            | 
            | 
           90 | 
               public function get_record_data(): stdClass {
  | 
        
        
            | 
            | 
           91 | 
                   $result = (object) [
  | 
        
        
            | 
            | 
           92 | 
                       'userid' => $this->get_user()->id,
  | 
        
        
            | 
            | 
           93 | 
                       'itemid' => $this->get_activity_id(),
  | 
        
        
            | 
            | 
           94 | 
                       'stateid' => $this->stateid,
  | 
        
        
            | 
            | 
           95 | 
                       'statedata' => json_encode($this),
  | 
        
        
            | 
            | 
           96 | 
                       'registration' => $this->registration,
  | 
        
        
            | 
            | 
           97 | 
                   ];
  | 
        
        
            | 
            | 
           98 | 
                   return $result;
  | 
        
        
            | 
            | 
           99 | 
               }
  | 
        
        
            | 
            | 
           100 | 
              | 
        
        
            | 
            | 
           101 | 
               /**
  | 
        
        
            | 
            | 
           102 | 
                * Returns a minified version of a given state.
  | 
        
        
            | 
            | 
           103 | 
                *
  | 
        
        
            | 
            | 
           104 | 
                * The returned structure is suitable to store in the "other" field
  | 
        
        
            | 
            | 
           105 | 
                * of logstore. xAPI standard specifies a list of attributes that can be calculated
  | 
        
        
            | 
            | 
           106 | 
                * instead of stored literally. This function get rid of these attributes.
  | 
        
        
            | 
            | 
           107 | 
                *
  | 
        
        
            | 
            | 
           108 | 
                * Note: it also converts stdClass to assoc array to make it compatible
  | 
        
        
            | 
            | 
           109 | 
                * with "other" field in the logstore
  | 
        
        
            | 
            | 
           110 | 
                *
  | 
        
        
            | 
            | 
           111 | 
                * @return array the minimal state needed to be stored a part from logstore data
  | 
        
        
            | 
            | 
           112 | 
                */
  | 
        
        
            | 
            | 
           113 | 
               public function minify(): ?array {
  | 
        
        
            | 
            | 
           114 | 
                   $result = [];
  | 
        
        
            | 
            | 
           115 | 
                   $fields = ['activity', 'stateid', 'statedata', 'registration'];
  | 
        
        
            | 
            | 
           116 | 
                   foreach ($fields as $field) {
  | 
        
        
            | 
            | 
           117 | 
                       if (!empty($this->$field)) {
  | 
        
        
            | 
            | 
           118 | 
                           $result[$field] = $this->$field;
  | 
        
        
            | 
            | 
           119 | 
                       }
  | 
        
        
            | 
            | 
           120 | 
                   }
  | 
        
        
            | 
            | 
           121 | 
                   return json_decode(json_encode($result), true);
  | 
        
        
            | 
            | 
           122 | 
               }
  | 
        
        
            | 
            | 
           123 | 
              | 
        
        
            | 
            | 
           124 | 
               /**
  | 
        
        
            | 
            | 
           125 | 
                * Set the state data.
  | 
        
        
            | 
            | 
           126 | 
                *
  | 
        
        
            | 
            | 
           127 | 
                * @param stdClass|null $statedata the state data
  | 
        
        
            | 
            | 
           128 | 
                */
  | 
        
        
            | 
            | 
           129 | 
               public function set_state_data(?stdClass $statedata): void {
  | 
        
        
            | 
            | 
           130 | 
                   $this->statedata = $statedata;
  | 
        
        
            | 
            | 
           131 | 
               }
  | 
        
        
            | 
            | 
           132 | 
              | 
        
        
            | 
            | 
           133 | 
               /**
  | 
        
        
            | 
            | 
           134 | 
                * Returns the state data.
  | 
        
        
            | 
            | 
           135 | 
                * For getting the JSON representation of this state data, use jsonSerialize().
  | 
        
        
            | 
            | 
           136 | 
                *
  | 
        
        
            | 
            | 
           137 | 
                * @return stdClass|null The state data object.
  | 
        
        
            | 
            | 
           138 | 
                */
  | 
        
        
            | 
            | 
           139 | 
               public function get_state_data(): ?stdClass {
  | 
        
        
            | 
            | 
           140 | 
                   return $this->statedata;
  | 
        
        
            | 
            | 
           141 | 
               }
  | 
        
        
            | 
            | 
           142 | 
              | 
        
        
            | 
            | 
           143 | 
               /**
  | 
        
        
            | 
            | 
           144 | 
                * Returns the moodle user represented by this state agent.
  | 
        
        
            | 
            | 
           145 | 
                *
  | 
        
        
            | 
            | 
           146 | 
                * @return stdClass user record
  | 
        
        
            | 
            | 
           147 | 
                */
  | 
        
        
            | 
            | 
           148 | 
               public function get_user(): stdClass {
  | 
        
        
            | 
            | 
           149 | 
                   return $this->agent->get_user();
  | 
        
        
            | 
            | 
           150 | 
               }
  | 
        
        
            | 
            | 
           151 | 
              | 
        
        
            | 
            | 
           152 | 
               /**
  | 
        
        
            | 
            | 
           153 | 
                * Returns the state activity ID.
  | 
        
        
            | 
            | 
           154 | 
                *
  | 
        
        
            | 
            | 
           155 | 
                * @return string activity ID
  | 
        
        
            | 
            | 
           156 | 
                */
  | 
        
        
            | 
            | 
           157 | 
               public function get_activity_id(): string {
  | 
        
        
            | 
            | 
           158 | 
                   return $this->activity->get_id();
  | 
        
        
            | 
            | 
           159 | 
               }
  | 
        
        
            | 
            | 
           160 | 
              | 
        
        
            | 
            | 
           161 | 
               /**
  | 
        
        
            | 
            | 
           162 | 
                * Return the state agent.
  | 
        
        
            | 
            | 
           163 | 
                *
  | 
        
        
            | 
            | 
           164 | 
                * @return item_agent
  | 
        
        
            | 
            | 
           165 | 
                */
  | 
        
        
            | 
            | 
           166 | 
               public function get_agent(): item_agent {
  | 
        
        
            | 
            | 
           167 | 
                   return $this->agent;
  | 
        
        
            | 
            | 
           168 | 
               }
  | 
        
        
            | 
            | 
           169 | 
              | 
        
        
            | 
            | 
           170 | 
               /**
  | 
        
        
            | 
            | 
           171 | 
                * Return the state object if it is defined.
  | 
        
        
            | 
            | 
           172 | 
                *
  | 
        
        
            | 
            | 
           173 | 
                * @return item_activity|null
  | 
        
        
            | 
            | 
           174 | 
                */
  | 
        
        
            | 
            | 
           175 | 
               public function get_activity(): ?item_activity {
  | 
        
        
            | 
            | 
           176 | 
                   return $this->activity;
  | 
        
        
            | 
            | 
           177 | 
               }
  | 
        
        
            | 
            | 
           178 | 
              | 
        
        
            | 
            | 
           179 | 
               /**
  | 
        
        
            | 
            | 
           180 | 
                * Returns the state id.
  | 
        
        
            | 
            | 
           181 | 
                *
  | 
        
        
            | 
            | 
           182 | 
                * @return string state identifier
  | 
        
        
            | 
            | 
           183 | 
                */
  | 
        
        
            | 
            | 
           184 | 
               public function get_state_id(): string {
  | 
        
        
            | 
            | 
           185 | 
                   return $this->stateid;
  | 
        
        
            | 
            | 
           186 | 
               }
  | 
        
        
            | 
            | 
           187 | 
              | 
        
        
            | 
            | 
           188 | 
               /**
  | 
        
        
            | 
            | 
           189 | 
                * Returns the state registration if any.
  | 
        
        
            | 
            | 
           190 | 
                *
  | 
        
        
            | 
            | 
           191 | 
                * @return string|null state registration
  | 
        
        
            | 
            | 
           192 | 
                */
  | 
        
        
            | 
            | 
           193 | 
               public function get_registration(): ?string {
  | 
        
        
            | 
            | 
           194 | 
                   return $this->registration;
  | 
        
        
            | 
            | 
           195 | 
               }
  | 
        
        
            | 
            | 
           196 | 
              | 
        
        
            | 
            | 
           197 | 
           }
  |