Abstract event base, usability enhancements

pull/17/head
Frode Petterson 2016-02-19 13:38:38 +01:00
parent 7a87735f02
commit 12ba0d2da5
1 changed files with 47 additions and 40 deletions

View File

@ -7,15 +7,14 @@
* @copyright 2016 Joubel AS * @copyright 2016 Joubel AS
* @license MIT * @license MIT
*/ */
class H5PEventBase { abstract class H5PEventBase {
// Constants // Constants
const LOG_NONE = 0; const LOG_NONE = 0;
const LOG_ALL = 1; const LOG_ALL = 1;
const LOG_ACTIONS = 2; const LOG_ACTIONS = 2;
const LOG_EXTRAS = 4;
// Static options // Static options
public static $log_level = self::LOG_EXTRAS; public static $log_level = self::LOG_ACTIONS;
public static $log_time = 2592000; // 30 Days public static $log_time = 2592000; // 30 Days
// Protected variables // Protected variables
@ -71,8 +70,8 @@ class H5PEventBase {
if (self::validLogLevel($type, $sub_type)) { if (self::validLogLevel($type, $sub_type)) {
$this->save(); $this->save();
} }
if (self::validCount($type, $sub_type)) { if (self::validStats($type, $sub_type)) {
$this->count(); $this->saveStats();
} }
} }
@ -85,7 +84,7 @@ class H5PEventBase {
* Name of event sub type * Name of event sub type
* @return boolean * @return boolean
*/ */
public static function validLogLevel($type, $sub_type) { private static function validLogLevel($type, $sub_type) {
switch (self::$log_level) { switch (self::$log_level) {
default: default:
case self::LOG_NONE: case self::LOG_NONE:
@ -101,7 +100,7 @@ class H5PEventBase {
} }
/** /**
* Check if event type should be added to counter. * Check if the event should be included in the statistics counter.
* *
* @param string $type * @param string $type
* Name of event type * Name of event type
@ -109,7 +108,7 @@ class H5PEventBase {
* Name of event sub type * Name of event sub type
* @return boolean * @return boolean
*/ */
public static function validCount($type, $sub_type) { private static function validStats($type, $sub_type) {
if ( ($type === 'content' && $sub_type === 'shortcode insert') || // Count number of shortcode inserts if ( ($type === 'content' && $sub_type === 'shortcode insert') || // Count number of shortcode inserts
($type === 'library' && $sub_type === NULL) || // Count number of times library is loaded in editor ($type === 'library' && $sub_type === NULL) || // Count number of times library is loaded in editor
($type === 'results' && $sub_type === 'content') ) { // Count number of times results page has been opened ($type === 'results' && $sub_type === 'content') ) { // Count number of times results page has been opened
@ -122,7 +121,7 @@ class H5PEventBase {
} }
/** /**
* Check if event type is action. * Check if event type is an action.
* *
* @param string $type * @param string $type
* Name of event type * Name of event type
@ -130,7 +129,7 @@ class H5PEventBase {
* Name of event sub type * Name of event sub type
* @return boolean * @return boolean
*/ */
public static function isAction($type, $sub_type) { private static function isAction($type, $sub_type) {
if ( ($type === 'content' && in_array($sub_type, array('create', 'create upload', 'update', 'update upload', 'upgrade', 'delete'))) || if ( ($type === 'content' && in_array($sub_type, array('create', 'create upload', 'update', 'update upload', 'upgrade', 'delete'))) ||
($type === 'library' && in_array($sub_type, array('create', 'update'))) ) { ($type === 'library' && in_array($sub_type, array('create', 'update'))) ) {
return TRUE; // Log actions return TRUE; // Log actions
@ -139,15 +138,15 @@ class H5PEventBase {
} }
/** /**
* A helper which makes it easier for some systems to save the data. * A helper which makes it easier for systems to save the data.
* No NULL values, empty string or 0 used instead. * Add all relevant properties to a assoc. array.
* Used in e.g. WordPress. * There are no NULL values. Empty string or 0 is used instead.
* Used by both Drupal and WordPress.
* *
* @return array with two arrays: $data, $format * @return array with keyed values
*/ */
protected function toArray() { protected function getDataArray() {
return array( return array(
array(
'created_at' => $this->time, 'created_at' => $this->time,
'type' => $this->type, 'type' => $this->type,
'sub_type' => empty($this->sub_type) ? '' : $this->sub_type, 'sub_type' => empty($this->sub_type) ? '' : $this->sub_type,
@ -155,8 +154,17 @@ class H5PEventBase {
'content_title' => empty($this->content_title) ? '' : $this->content_title, 'content_title' => empty($this->content_title) ? '' : $this->content_title,
'library_name' => empty($this->library_name) ? '' : $this->library_name, 'library_name' => empty($this->library_name) ? '' : $this->library_name,
'library_version' => empty($this->library_version) ? '' : $this->library_version 'library_version' => empty($this->library_version) ? '' : $this->library_version
), );
array( }
/**
* A helper which makes it easier for systems to save the data.
* Used in WordPress.
*
* @return array with strings
*/
protected function getFormatArray() {
return array(
'%d', '%d',
'%s', '%s',
'%s', '%s',
@ -164,21 +172,20 @@ class H5PEventBase {
'%s', '%s',
'%s', '%s',
'%s' '%s'
)
); );
} }
/** /**
* Store the event. * Stores the event data in the database.
*
* Must be overriden by plugin.
*/ */
protected function save() { abstract protected function save();
// Does nothing by default, should be overriden by plugin
}
/** /**
* Count number of events. * Add current event data to statistics counter.
*
* Must be overriden by plugin.
*/ */
protected function count() { abstract protected function saveStats();
// Does nothing by default, should be overriden by plugin
}
} }