From 62cd708f6bf57b8a0e581a846773d1be1a12a2be Mon Sep 17 00:00:00 2001 From: Frode Petterson Date: Tue, 2 Feb 2016 17:03:53 +0100 Subject: [PATCH] Added base class for H5P events HFJ-1610 --- h5p-event-base.class.php | 114 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 h5p-event-base.class.php diff --git a/h5p-event-base.class.php b/h5p-event-base.class.php new file mode 100644 index 0000000..e5fd58c --- /dev/null +++ b/h5p-event-base.class.php @@ -0,0 +1,114 @@ + – content view + * embed – viewed through embed code + * shortcode – viewed through internal shortcode + * edit – opened in editor + * delete – deleted + * create – created through editor + * create upload – created through upload + * update – updated through editor + * update upload – updated through upload + * upgrade – upgraded + * + * results, – view own results + * content – view results for content + * set – new results inserted or updated + * + * settings, – settings page loaded + * + * library, – loaded in editor + * create – new library installed + * update – old library updated + * + * @param string $type + * Name of event type + * @param string $sub_type + * Name of event sub type + * @param string $content_id + * Identifier for content affacted by the event + * @param string $content_title + * Content title (makes it easier to know which content was deleted etc.) + * @param string $library_name + * Name of the library affacted by the event + * @param string $library_version + * Library version + */ + function __construct($type, $sub_type = NULL, $content_id = NULL, $content_title = NULL, $library_name = NULL, $library_version = NULL) { + $this->type = $type; + $this->sub_type = $sub_type; + $this->content_id = $content_id; + $this->content_title = $content_title; + $this->library_name = $library_name; + $this->library_version = $library_version; + $this->time = time(); + + if (self::validLogLevel($type, $sub_type)) { + $this->save(); + } + } + + /** + * Determines if the log type should be saved. + * @param string $type + * Name of event type + * @param string $sub_type + * Name of event sub type + */ + public static function validLogLevel($type, $sub_type) { + switch (self::$log_level) { + default: + case self::LOG_NONE: + return FALSE; + case self::LOG_ALL: + return TRUE; + + case self::LOG_EXTRAS: + // Extras help provide H5P with valuable anonymous user data! + if ( ($type === 'content' && $sub_type === 'shortcode insert') || // Log number of shortcode inserts + ($type === 'library' && $sub_type === NULL) || // Log number of times library is loaded in editor + ($type === 'results' && $sub_type === 'content') ) { // Log number of times results page has been opened + return TRUE; + } + // Fall through by intention! + case self::LOG_ACTIONS: + 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'))) ) { + return TRUE; // Log actions + } + return FALSE; + } + } + + /** + * Store the event. + */ + protected function save() { + // Does nothing by default, should be overriden by plugin + } +}