Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

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