Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310]

   1  <?php
   2  
   3  namespace Moodle;
   4  
   5  /**
   6   * The base class for H5P events. Extend to track H5P events in your system.
   7   *
   8   * @package    H5P
   9   * @copyright  2016 Joubel AS
  10   * @license    MIT
  11   */
  12  abstract class H5PEventBase {
  13    // Constants
  14    const LOG_NONE = 0;
  15    const LOG_ALL = 1;
  16    const LOG_ACTIONS = 2;
  17  
  18    // Static options
  19    public static $log_level = self::LOG_ACTIONS;
  20    public static $log_time = 2592000; // 30 Days
  21  
  22    // Protected variables
  23    protected $id, $type, $sub_type, $content_id, $content_title, $library_name, $library_version, $time;
  24  
  25    /**
  26     * Adds event type, h5p library and timestamp to event before saving it.
  27     *
  28     * Common event types with sub type:
  29     *  content, <none> – content view
  30     *           embed – viewed through embed code
  31     *           shortcode – viewed through internal shortcode
  32     *           edit – opened in editor
  33     *           delete – deleted
  34     *           create – created through editor
  35     *           create upload – created through upload
  36     *           update – updated through editor
  37     *           update upload – updated through upload
  38     *           upgrade – upgraded
  39     *
  40     *  results, <none> – view own results
  41     *           content – view results for content
  42     *           set – new results inserted or updated
  43     *
  44     *  settings, <none> – settings page loaded
  45     *
  46     *  library, <none> – loaded in editor
  47     *           create – new library installed
  48     *           update – old library updated
  49     *
  50     * @param string $type
  51     *  Name of event type
  52     * @param string $sub_type
  53     *  Name of event sub type
  54     * @param string $content_id
  55     *  Identifier for content affected by the event
  56     * @param string $content_title
  57     *  Content title (makes it easier to know which content was deleted etc.)
  58     * @param string $library_name
  59     *  Name of the library affected by the event
  60     * @param string $library_version
  61     *  Library version
  62     */
  63    function __construct($type, $sub_type = NULL, $content_id = NULL, $content_title = NULL, $library_name = NULL, $library_version = NULL) {
  64      $this->type = $type;
  65      $this->sub_type = $sub_type;
  66      $this->content_id = $content_id;
  67      $this->content_title = $content_title;
  68      $this->library_name = $library_name;
  69      $this->library_version = $library_version;
  70      $this->time = time();
  71  
  72      if (self::validLogLevel($type, $sub_type)) {
  73        $this->save();
  74      }
  75      if (self::validStats($type, $sub_type)) {
  76        $this->saveStats();
  77      }
  78    }
  79  
  80    /**
  81     * Determines if the event type should be saved/logged.
  82     *
  83     * @param string $type
  84     *  Name of event type
  85     * @param string $sub_type
  86     *  Name of event sub type
  87     * @return boolean
  88     */
  89    private static function validLogLevel($type, $sub_type) {
  90      switch (self::$log_level) {
  91        default:
  92        case self::LOG_NONE:
  93          return FALSE;
  94        case self::LOG_ALL:
  95          return TRUE; // Log everything
  96        case self::LOG_ACTIONS:
  97          if (self::isAction($type, $sub_type)) {
  98            return TRUE; // Log actions
  99          }
 100          return FALSE;
 101      }
 102    }
 103  
 104    /**
 105     * Check if the event should be included in the statistics counter.
 106     *
 107     * @param string $type
 108     *  Name of event type
 109     * @param string $sub_type
 110     *  Name of event sub type
 111     * @return boolean
 112     */
 113    private static function validStats($type, $sub_type) {
 114      if ( ($type === 'content' && $sub_type === 'shortcode insert') || // Count number of shortcode inserts
 115           ($type === 'library' && $sub_type === NULL) || // Count number of times library is loaded in editor
 116           ($type === 'results' && $sub_type === 'content') ) { // Count number of times results page has been opened
 117        return TRUE;
 118      }
 119      elseif (self::isAction($type, $sub_type)) { // Count all actions
 120        return TRUE;
 121      }
 122      return FALSE;
 123    }
 124  
 125    /**
 126     * Check if event type is an action.
 127     *
 128     * @param string $type
 129     *  Name of event type
 130     * @param string $sub_type
 131     *  Name of event sub type
 132     * @return boolean
 133     */
 134    private static function isAction($type, $sub_type) {
 135      if ( ($type === 'content' && in_array($sub_type, array('create', 'create upload', 'update', 'update upload', 'upgrade', 'delete'))) ||
 136           ($type === 'library' && in_array($sub_type, array('create', 'update'))) ) {
 137        return TRUE; // Log actions
 138      }
 139      return FALSE;
 140    }
 141  
 142    /**
 143     * A helper which makes it easier for systems to save the data.
 144     * Add all relevant properties to a assoc. array.
 145     * There are no NULL values. Empty string or 0 is used instead.
 146     * Used by both Drupal and WordPress.
 147     *
 148     * @return array with keyed values
 149     */
 150    protected function getDataArray() {
 151      return array(
 152        'created_at' => $this->time,
 153        'type' => $this->type,
 154        'sub_type' => empty($this->sub_type) ? '' : $this->sub_type,
 155        'content_id' => empty($this->content_id) ? 0 : $this->content_id,
 156        'content_title' => empty($this->content_title) ? '' : $this->content_title,
 157        'library_name' => empty($this->library_name) ? '' : $this->library_name,
 158        'library_version' => empty($this->library_version) ? '' : $this->library_version
 159      );
 160    }
 161  
 162    /**
 163     * A helper which makes it easier for systems to save the data.
 164     * Used in WordPress.
 165     *
 166     * @return array with strings
 167     */
 168    protected function getFormatArray() {
 169      return array(
 170        '%d',
 171        '%s',
 172        '%s',
 173        '%d',
 174        '%s',
 175        '%s',
 176        '%s'
 177      );
 178    }
 179  
 180    /**
 181     * Stores the event data in the database.
 182     *
 183     * Must be overridden by plugin.
 184     */
 185    abstract protected function save();
 186  
 187    /**
 188     * Add current event data to statistics counter.
 189     *
 190     * Must be overridden by plugin.
 191     */
 192    abstract protected function saveStats();
 193  }