From 62cd708f6bf57b8a0e581a846773d1be1a12a2be Mon Sep 17 00:00:00 2001 From: Frode Petterson Date: Tue, 2 Feb 2016 17:03:53 +0100 Subject: [PATCH 01/17] 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 + } +} From 530ad8ee3344afd1aef70c23112848ad52f8edd1 Mon Sep 17 00:00:00 2001 From: Frode Petterson Date: Wed, 3 Feb 2016 10:42:14 +0100 Subject: [PATCH 02/17] Corrected usage of static HFJ-1610 --- h5p-event-base.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/h5p-event-base.class.php b/h5p-event-base.class.php index e5fd58c..2398818 100644 --- a/h5p-event-base.class.php +++ b/h5p-event-base.class.php @@ -16,7 +16,7 @@ class H5PEventBase { // Static options public static $log_level = self::LOG_EXTRAS; - public static $log_time = 86400 * 30; // 30 Days + public static $log_time = 2592000; // 30 Days // Protected variables protected $type, $sub_type, $content_id, $content_title, $library_name, $library_version, $time; From f5f89f23954ee8fce2e80632a502e24e09865515 Mon Sep 17 00:00:00 2001 From: Frode Petterson Date: Thu, 4 Feb 2016 14:13:59 +0100 Subject: [PATCH 03/17] Helper for storing events HFJ-1610 --- h5p-event-base.class.php | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/h5p-event-base.class.php b/h5p-event-base.class.php index 2398818..f8a9cac 100644 --- a/h5p-event-base.class.php +++ b/h5p-event-base.class.php @@ -19,7 +19,7 @@ class H5PEventBase { public static $log_time = 2592000; // 30 Days // Protected variables - protected $type, $sub_type, $content_id, $content_title, $library_name, $library_version, $time; + protected $id, $type, $sub_type, $content_id, $content_title, $library_name, $library_version, $time; /** * Adds event type, h5p library and timestamp to event before saving it. @@ -105,6 +105,36 @@ class H5PEventBase { } } + /** + * A helper which makes it easier for some systems to save the data. + * No NULL values, empty string or 0 used instead. + * Used in e.g. WordPress. + * + * @return array with two arrays: $data, $format + */ + protected function toArray() { + return array( + array( + 'created_at' => $this->time, + 'type' => $this->type, + 'sub_type' => empty($this->sub_type) ? '' : $this->sub_type, + 'content_id' => empty($this->content_id) ? 0 : $this->content_id, + 'content_title' => empty($this->content_title) ? '' : $this->content_title, + 'library_name' => empty($this->library_name) ? '' : $this->library_name, + 'library_version' => empty($this->library_version) ? '' : $this->library_version + ), + array( + '%d', + '%s', + '%s', + '%d', + '%s', + '%s', + '%s' + ) + ); + } + /** * Store the event. */ From 971e0356263c4ef5bb38c5696e01ad7ead28db17 Mon Sep 17 00:00:00 2001 From: Frode Petterson Date: Thu, 4 Feb 2016 15:15:35 +0100 Subject: [PATCH 04/17] Skip local check if network HFJ-1610 --- h5p.classes.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/h5p.classes.php b/h5p.classes.php index c8662a1..57b5243 100644 --- a/h5p.classes.php +++ b/h5p.classes.php @@ -2390,10 +2390,8 @@ class H5PCore { $type = $this->h5pF->getOption('site_type', 'local'); // Determine remote/visitor origin - $localhostPattern = '/^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$/i'; - - // localhost - if ($type !== 'internet' && !preg_match($localhostPattern, $_SERVER['REMOTE_ADDR'])) { + if ($type === 'network' || + ($type === 'local' && !preg_match('/^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$/i', $_SERVER['REMOTE_ADDR']))) { if (filter_var($_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE)) { // Internet $this->h5pF->setOption('site_type', 'internet'); From 292e04de5d9bfa4ac37781ad2d64657439639890 Mon Sep 17 00:00:00 2001 From: Frode Petterson Date: Thu, 4 Feb 2016 16:34:32 +0100 Subject: [PATCH 05/17] Combine library statistics HFJ-1610 --- h5p.classes.php | 43 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/h5p.classes.php b/h5p.classes.php index 57b5243..1373ad1 100644 --- a/h5p.classes.php +++ b/h5p.classes.php @@ -547,6 +547,15 @@ interface H5PFrameworkInterface { * @return boolean */ public function isContentSlugAvailable($slug); + + /** + * Generates statistics from the event log per library + * + * @param string $type Type of event to generate stats for + * @param string $sub_type + * @return array Number values indexed by library name and version + */ + public function getLibraryStats($type, $sub_type = ''); } /** @@ -2411,20 +2420,34 @@ class H5PCore { * A distinct array of installed libraries */ public function getLibrariesInstalled() { - $librariesInstalled = []; - + $librariesInstalled = array(); $libs = $this->h5pF->loadLibraries(); foreach($libs as $library) { foreach($library as $libVersion) { - - $librariesInstalled[] = $libVersion->name.' '.$libVersion->major_version.'.'.$libVersion->minor_version.'.'.$libVersion->patch_version; + $librariesInstalled[$libVersion->name.' '.$libVersion->major_version.'.'.$libVersion->minor_version] = $libVersion->patch_version; } } return $librariesInstalled; } + /** + * Easy way to combine smiliar data sets. + * + * @param array $inputs Multiple arrays with data + * @return array + */ + public function combineArrayValues($inputs) { + $results = array(); + foreach ($inputs as $index => $values) { + foreach ($values as $key => $value) { + $results[$key][$index] = $value; + } + } + return $results; + } + /** * Fetch a list of libraries' metadata from h5p.org. * Save URL tutorial to database. Each platform implementation @@ -2435,8 +2458,16 @@ class H5PCore { $platformInfo['autoFetchingDisabled'] = $fetchingDisabled; $platformInfo['uuid'] = $this->h5pF->getOption('site_uuid', ''); $platformInfo['siteType'] = $this->h5pF->getOption('site_type', 'local'); - $platformInfo['libraryContentCount'] = $this->h5pF->getLibraryContentCount(); - $platformInfo['librariesInstalled'] = $this->getLibrariesInstalled(); + $platformInfo['libraryStats'] = $core->combineArrayValues(array( + 'patch' => $this->getLibrariesInstalled(), + 'content' => $this->h5pF->getLibraryContentCount(), + 'loaded' => $this->h5pF->getLibraryStats('library'), + 'created' => $this->h5pF->getLibraryStats('content', 'create'), + 'createdUpload' => $this->h5pF->getLibraryStats('content', 'create upload'), + 'deleted' => $this->h5pF->getLibraryStats('content', 'deleted'), + 'resultViews' => $this->h5pF->getLibraryStats('results', 'content'), + 'shortcodeInserts' => $this->h5pF->getLibraryStats('content', 'shortcode insert') + )) // Adding random string to GET to be sure nothing is cached $random = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 5); From fa90b0a1c32f9f60d639602f0ef5757ba6d3db27 Mon Sep 17 00:00:00 2001 From: Frode Petterson Date: Fri, 5 Feb 2016 11:09:57 +0100 Subject: [PATCH 06/17] Use correct variable --- h5p.classes.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/h5p.classes.php b/h5p.classes.php index 1373ad1..ce9d726 100644 --- a/h5p.classes.php +++ b/h5p.classes.php @@ -2458,7 +2458,7 @@ class H5PCore { $platformInfo['autoFetchingDisabled'] = $fetchingDisabled; $platformInfo['uuid'] = $this->h5pF->getOption('site_uuid', ''); $platformInfo['siteType'] = $this->h5pF->getOption('site_type', 'local'); - $platformInfo['libraryStats'] = $core->combineArrayValues(array( + $platformInfo['libraryStats'] = $this->combineArrayValues(array( 'patch' => $this->getLibrariesInstalled(), 'content' => $this->h5pF->getLibraryContentCount(), 'loaded' => $this->h5pF->getLibraryStats('library'), @@ -2467,7 +2467,7 @@ class H5PCore { 'deleted' => $this->h5pF->getLibraryStats('content', 'deleted'), 'resultViews' => $this->h5pF->getLibraryStats('results', 'content'), 'shortcodeInserts' => $this->h5pF->getLibraryStats('content', 'shortcode insert') - )) + )); // Adding random string to GET to be sure nothing is cached $random = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 5); From 70baeb55a11e8aa4d23ad0b2e72d322870780754 Mon Sep 17 00:00:00 2001 From: Frode Petterson Date: Mon, 8 Feb 2016 10:38:53 +0100 Subject: [PATCH 07/17] Add local ID from path HFJ-1610 --- h5p.classes.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/h5p.classes.php b/h5p.classes.php index ce9d726..f165f62 100644 --- a/h5p.classes.php +++ b/h5p.classes.php @@ -1721,6 +1721,7 @@ class H5PCore { } $this->detectSiteType(); + $this->fullPluginPath = preg_replace('/\/[^\/]+[\/]?$/', '' , dirname(__FILE__)); } /** @@ -2458,6 +2459,7 @@ class H5PCore { $platformInfo['autoFetchingDisabled'] = $fetchingDisabled; $platformInfo['uuid'] = $this->h5pF->getOption('site_uuid', ''); $platformInfo['siteType'] = $this->h5pF->getOption('site_type', 'local'); + $platformInfo['localID'] = hash('crc32', $this->fullPluginPath); $platformInfo['libraryStats'] = $this->combineArrayValues(array( 'patch' => $this->getLibrariesInstalled(), 'content' => $this->h5pF->getLibraryContentCount(), From c4011e3bbb57753ff9306412e0d6bc817babd954 Mon Sep 17 00:00:00 2001 From: Frode Petterson Date: Mon, 8 Feb 2016 11:39:21 +0100 Subject: [PATCH 08/17] Use POST to get H5P updates HFJ-1610 --- h5p.classes.php | 81 ++++++++++++++++++++++++++++++------------------- 1 file changed, 50 insertions(+), 31 deletions(-) diff --git a/h5p.classes.php b/h5p.classes.php index f165f62..a54596b 100644 --- a/h5p.classes.php +++ b/h5p.classes.php @@ -2455,42 +2455,61 @@ class H5PCore { * is responsible for invoking this, eg using cron */ public function fetchLibrariesMetadata($fetchingDisabled = FALSE) { - $platformInfo = $this->h5pF->getPlatformInfo(); - $platformInfo['autoFetchingDisabled'] = $fetchingDisabled; - $platformInfo['uuid'] = $this->h5pF->getOption('site_uuid', ''); - $platformInfo['siteType'] = $this->h5pF->getOption('site_type', 'local'); - $platformInfo['localID'] = hash('crc32', $this->fullPluginPath); - $platformInfo['libraryStats'] = $this->combineArrayValues(array( - 'patch' => $this->getLibrariesInstalled(), - 'content' => $this->h5pF->getLibraryContentCount(), - 'loaded' => $this->h5pF->getLibraryStats('library'), - 'created' => $this->h5pF->getLibraryStats('content', 'create'), - 'createdUpload' => $this->h5pF->getLibraryStats('content', 'create upload'), - 'deleted' => $this->h5pF->getLibraryStats('content', 'deleted'), - 'resultViews' => $this->h5pF->getLibraryStats('results', 'content'), - 'shortcodeInserts' => $this->h5pF->getLibraryStats('content', 'shortcode insert') - )); + // Gather data + $uuid = $this->h5pF->getOption('site_uuid', ''); + $data = array( + 'api_version' => 2, + 'platform' => $this->h5pF->getPlatformInfo(), + 'uuid' => $uuid, + 'local_id' => hash('crc32', $this->fullPluginPath), + 'type' => $this->h5pF->getOption('site_type', 'local'), + 'libraries' => $this->combineArrayValues(array( + 'patch' => $this->getLibrariesInstalled(), + 'content' => $this->h5pF->getLibraryContentCount(), + 'loaded' => $this->h5pF->getLibraryStats('library'), + 'created' => $this->h5pF->getLibraryStats('content', 'create'), + 'createdUpload' => $this->h5pF->getLibraryStats('content', 'create upload'), + 'deleted' => $this->h5pF->getLibraryStats('content', 'deleted'), + 'resultViews' => $this->h5pF->getLibraryStats('results', 'content'), + 'shortcodeInserts' => $this->h5pF->getLibraryStats('content', 'shortcode insert') + )) + ); - // Adding random string to GET to be sure nothing is cached - $random = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 5); - $json = $this->h5pF->fetchExternalData('http://h5p.org/libraries-metadata.json?api=1&platform=' . urlencode(json_encode($platformInfo)) . '&x=' . urlencode($random)); - if ($json !== NULL) { - $json = json_decode($json); - if (isset($json->libraries)) { - foreach ($json->libraries as $machineName => $libInfo) { - $this->h5pF->setLibraryTutorialUrl($machineName, $libInfo->tutorialUrl); - } - } - if($platformInfo['uuid'] === '' && isset($json->uuid)) { - $this->h5pF->setOption('site_uuid', $json->uuid); - } - if (isset($json->latest) && !empty($json->latest)) { - $this->h5pF->setOption('update_available', $json->latest->releasedAt); - $this->h5pF->setOption('update_available_path', $json->latest->path); + // Send request + $protocol = (extension_loaded('openssl') ? 'https' : 'http'); + $result = $this->h5pF->fetchExternalData("{$protocol}://h5p.org/libraries-metadata.json", $data); + if (empty($result)) { + return; + } + + // Process results + $json = json_decode($result); + if (empty($json)) { + return; + } + + // Handle libraries metadata + if (isset($json->libraries)) { + foreach ($json->libraries as $machineName => $libInfo) { + $this->h5pF->setLibraryTutorialUrl($machineName, $libInfo->tutorialUrl); } } + + // Handle new uuid + if ($uuid === '' && isset($json->uuid)) { + $this->h5pF->setOption('site_uuid', $json->uuid); + } + + // Handle lastest version of H5P + if (!empty($json->latest)) { + $this->h5pF->setOption('update_available', $json->latest->releasedAt); + $this->h5pF->setOption('update_available_path', $json->latest->path); + } } + /** + * + */ public function getGlobalDisable() { $disable = self::DISABLE_NONE; From cf3373d4ee1a0d55b6bd7bf98df9729bc9fec44b Mon Sep 17 00:00:00 2001 From: Frode Petterson Date: Tue, 9 Feb 2016 10:54:04 +0100 Subject: [PATCH 09/17] Changes in format to be more POST friendly HFJ-1610 --- h5p.classes.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/h5p.classes.php b/h5p.classes.php index a54596b..afad52f 100644 --- a/h5p.classes.php +++ b/h5p.classes.php @@ -2457,13 +2457,18 @@ class H5PCore { public function fetchLibrariesMetadata($fetchingDisabled = FALSE) { // Gather data $uuid = $this->h5pF->getOption('site_uuid', ''); + $platform = $this->h5pF->getPlatformInfo(); $data = array( 'api_version' => 2, - 'platform' => $this->h5pF->getPlatformInfo(), 'uuid' => $uuid, + 'platform_name' => $platform->name, + 'platform_version' => $platform->version, + 'h5p_version' => $platform->h5pVersion, + 'disabled' => $fetchingDisabled, 'local_id' => hash('crc32', $this->fullPluginPath), 'type' => $this->h5pF->getOption('site_type', 'local'), - 'libraries' => $this->combineArrayValues(array( + 'num_authors' => $this->h5pF->getNumAuthors(), + 'libraries' => json_encode($this->combineArrayValues(array( 'patch' => $this->getLibrariesInstalled(), 'content' => $this->h5pF->getLibraryContentCount(), 'loaded' => $this->h5pF->getLibraryStats('library'), @@ -2472,7 +2477,7 @@ class H5PCore { 'deleted' => $this->h5pF->getLibraryStats('content', 'deleted'), 'resultViews' => $this->h5pF->getLibraryStats('results', 'content'), 'shortcodeInserts' => $this->h5pF->getLibraryStats('content', 'shortcode insert') - )) + ))) ); // Send request From b16b0c1e0ba5ebf58e421f29f133a7cef4494254 Mon Sep 17 00:00:00 2001 From: Frode Petterson Date: Tue, 9 Feb 2016 10:55:44 +0100 Subject: [PATCH 10/17] We're going to need authors --- h5p.classes.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/h5p.classes.php b/h5p.classes.php index afad52f..6085b70 100644 --- a/h5p.classes.php +++ b/h5p.classes.php @@ -556,6 +556,12 @@ interface H5PFrameworkInterface { * @return array Number values indexed by library name and version */ public function getLibraryStats($type, $sub_type = ''); + + /** + * Aggregate the current number of H5P authors + * @return int + */ + public function getNumAuthors(); } /** From 2a20dc23ae856ffe8050242d9e8fcb6021fc64f8 Mon Sep 17 00:00:00 2001 From: Frode Petterson Date: Tue, 9 Feb 2016 13:52:35 +0100 Subject: [PATCH 11/17] Use correct format HFJ-1610 --- h5p.classes.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/h5p.classes.php b/h5p.classes.php index 6085b70..2741687 100644 --- a/h5p.classes.php +++ b/h5p.classes.php @@ -2467,10 +2467,10 @@ class H5PCore { $data = array( 'api_version' => 2, 'uuid' => $uuid, - 'platform_name' => $platform->name, - 'platform_version' => $platform->version, - 'h5p_version' => $platform->h5pVersion, - 'disabled' => $fetchingDisabled, + 'platform_name' => $platform['name'], + 'platform_version' => $platform['version'], + 'h5p_version' => $platform['h5pVersion'], + 'disabled' => $fetchingDisabled ? 1 : 0, 'local_id' => hash('crc32', $this->fullPluginPath), 'type' => $this->h5pF->getOption('site_type', 'local'), 'num_authors' => $this->h5pF->getNumAuthors(), From dc8b70748fd4654debc9746ec12fe42e10b76510 Mon Sep 17 00:00:00 2001 From: Frode Petterson Date: Tue, 9 Feb 2016 13:53:17 +0100 Subject: [PATCH 12/17] Fixed once() HFJ-1646 --- js/h5p-event-dispatcher.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/js/h5p-event-dispatcher.js b/js/h5p-event-dispatcher.js index 5300e6d..fb8dd66 100644 --- a/js/h5p-event-dispatcher.js +++ b/js/h5p-event-dispatcher.js @@ -139,8 +139,8 @@ H5P.EventDispatcher = (function () { } var once = function (event) { - self.off(event, once); - listener.apply(this, event); + self.off(event.type, once); + listener.call(this, event); }; self.on(type, once, thisArg); From 58f3617fc12c863fce1697ec50e52ae6e0142eb9 Mon Sep 17 00:00:00 2001 From: Frode Petterson Date: Wed, 10 Feb 2016 11:49:20 +0100 Subject: [PATCH 13/17] Added counters HFJ-1610 --- h5p-event-base.class.php | 66 ++++++++++++++++++++++++++++++++-------- 1 file changed, 53 insertions(+), 13 deletions(-) diff --git a/h5p-event-base.class.php b/h5p-event-base.class.php index f8a9cac..ea0ce2a 100644 --- a/h5p-event-base.class.php +++ b/h5p-event-base.class.php @@ -71,14 +71,19 @@ class H5PEventBase { if (self::validLogLevel($type, $sub_type)) { $this->save(); } + if (self::validCount($type, $sub_type)) { + $this->count(); + } } /** - * Determines if the log type should be saved. + * Determines if the event type should be saved/logged. + * * @param string $type * Name of event type * @param string $sub_type * Name of event sub type + * @return boolean */ public static function validLogLevel($type, $sub_type) { switch (self::$log_level) { @@ -86,25 +91,53 @@ class H5PEventBase { 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! + return TRUE; // Log everything 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'))) ) { + if (self::isAction($type, $sub_type)) { return TRUE; // Log actions } return FALSE; } } + /** + * Check if event type should be added to counter. + * + * @param string $type + * Name of event type + * @param string $sub_type + * Name of event sub type + * @return boolean + */ + public static function validCount($type, $sub_type) { + 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 === 'results' && $sub_type === 'content') ) { // Count number of times results page has been opened + return TRUE; + } + elseif (self::isAction($type, $sub_type)) { // Count all actions + return TRUE; + } + return FALSE; + } + + /** + * Check if event type is action. + * + * @param string $type + * Name of event type + * @param string $sub_type + * Name of event sub type + * @return boolean + */ + public static function isAction($type, $sub_type) { + 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; + } + /** * A helper which makes it easier for some systems to save the data. * No NULL values, empty string or 0 used instead. @@ -141,4 +174,11 @@ class H5PEventBase { protected function save() { // Does nothing by default, should be overriden by plugin } + + /** + * Count number of events. + */ + protected function count() { + // Does nothing by default, should be overriden by plugin + } } From 4cf7a32b4acfaa66a651ed9c300a024f04183572 Mon Sep 17 00:00:00 2001 From: Frode Petterson Date: Wed, 10 Feb 2016 11:54:20 +0100 Subject: [PATCH 14/17] Get data from counters instead of log HFJ-1610 --- h5p.classes.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/h5p.classes.php b/h5p.classes.php index 2741687..94f0af4 100644 --- a/h5p.classes.php +++ b/h5p.classes.php @@ -2478,11 +2478,11 @@ class H5PCore { 'patch' => $this->getLibrariesInstalled(), 'content' => $this->h5pF->getLibraryContentCount(), 'loaded' => $this->h5pF->getLibraryStats('library'), - 'created' => $this->h5pF->getLibraryStats('content', 'create'), - 'createdUpload' => $this->h5pF->getLibraryStats('content', 'create upload'), - 'deleted' => $this->h5pF->getLibraryStats('content', 'deleted'), - 'resultViews' => $this->h5pF->getLibraryStats('results', 'content'), - 'shortcodeInserts' => $this->h5pF->getLibraryStats('content', 'shortcode insert') + 'created' => $this->h5pF->getLibraryStats('content create'), + 'createdUpload' => $this->h5pF->getLibraryStats('content create upload'), + 'deleted' => $this->h5pF->getLibraryStats('content deleted'), + 'resultViews' => $this->h5pF->getLibraryStats('results content'), + 'shortcodeInserts' => $this->h5pF->getLibraryStats('content shortcode insert') ))) ); From 7a87735f0226ab8f45b6ee2e0f38d17f1bd012b0 Mon Sep 17 00:00:00 2001 From: Frode Petterson Date: Thu, 11 Feb 2016 09:53:16 +0100 Subject: [PATCH 15/17] Removed sub_type from stats HFJ-1610 --- h5p.classes.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/h5p.classes.php b/h5p.classes.php index 94f0af4..b17c418 100644 --- a/h5p.classes.php +++ b/h5p.classes.php @@ -552,10 +552,9 @@ interface H5PFrameworkInterface { * Generates statistics from the event log per library * * @param string $type Type of event to generate stats for - * @param string $sub_type * @return array Number values indexed by library name and version */ - public function getLibraryStats($type, $sub_type = ''); + public function getLibraryStats($type); /** * Aggregate the current number of H5P authors From 12ba0d2da5925b669a8c1db4f1da4ff7dd401e7e Mon Sep 17 00:00:00 2001 From: Frode Petterson Date: Fri, 19 Feb 2016 13:38:38 +0100 Subject: [PATCH 16/17] Abstract event base, usability enhancements --- h5p-event-base.class.php | 87 ++++++++++++++++++++++------------------ 1 file changed, 47 insertions(+), 40 deletions(-) diff --git a/h5p-event-base.class.php b/h5p-event-base.class.php index ea0ce2a..bccee9c 100644 --- a/h5p-event-base.class.php +++ b/h5p-event-base.class.php @@ -7,15 +7,14 @@ * @copyright 2016 Joubel AS * @license MIT */ -class H5PEventBase { +abstract class H5PEventBase { // Constants const LOG_NONE = 0; const LOG_ALL = 1; const LOG_ACTIONS = 2; - const LOG_EXTRAS = 4; // Static options - public static $log_level = self::LOG_EXTRAS; + public static $log_level = self::LOG_ACTIONS; public static $log_time = 2592000; // 30 Days // Protected variables @@ -71,8 +70,8 @@ class H5PEventBase { if (self::validLogLevel($type, $sub_type)) { $this->save(); } - if (self::validCount($type, $sub_type)) { - $this->count(); + if (self::validStats($type, $sub_type)) { + $this->saveStats(); } } @@ -85,7 +84,7 @@ class H5PEventBase { * Name of event sub type * @return boolean */ - public static function validLogLevel($type, $sub_type) { + private static function validLogLevel($type, $sub_type) { switch (self::$log_level) { default: 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 * Name of event type @@ -109,7 +108,7 @@ class H5PEventBase { * Name of event sub type * @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 ($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 @@ -122,7 +121,7 @@ class H5PEventBase { } /** - * Check if event type is action. + * Check if event type is an action. * * @param string $type * Name of event type @@ -130,7 +129,7 @@ class H5PEventBase { * Name of event sub type * @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'))) || ($type === 'library' && in_array($sub_type, array('create', 'update'))) ) { return TRUE; // Log actions @@ -139,46 +138,54 @@ class H5PEventBase { } /** - * A helper which makes it easier for some systems to save the data. - * No NULL values, empty string or 0 used instead. - * Used in e.g. WordPress. + * A helper which makes it easier for systems to save the data. + * Add all relevant properties to a assoc. array. + * 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( - array( - 'created_at' => $this->time, - 'type' => $this->type, - 'sub_type' => empty($this->sub_type) ? '' : $this->sub_type, - 'content_id' => empty($this->content_id) ? 0 : $this->content_id, - 'content_title' => empty($this->content_title) ? '' : $this->content_title, - 'library_name' => empty($this->library_name) ? '' : $this->library_name, - 'library_version' => empty($this->library_version) ? '' : $this->library_version - ), - array( - '%d', - '%s', - '%s', - '%d', - '%s', - '%s', - '%s' - ) + 'created_at' => $this->time, + 'type' => $this->type, + 'sub_type' => empty($this->sub_type) ? '' : $this->sub_type, + 'content_id' => empty($this->content_id) ? 0 : $this->content_id, + 'content_title' => empty($this->content_title) ? '' : $this->content_title, + 'library_name' => empty($this->library_name) ? '' : $this->library_name, + 'library_version' => empty($this->library_version) ? '' : $this->library_version ); } /** - * Store the event. + * A helper which makes it easier for systems to save the data. + * Used in WordPress. + * + * @return array with strings */ - protected function save() { - // Does nothing by default, should be overriden by plugin + protected function getFormatArray() { + return array( + '%d', + '%s', + '%s', + '%d', + '%s', + '%s', + '%s' + ); } /** - * Count number of events. + * Stores the event data in the database. + * + * Must be overriden by plugin. */ - protected function count() { - // Does nothing by default, should be overriden by plugin - } + abstract protected function save(); + + /** + * Add current event data to statistics counter. + * + * Must be overriden by plugin. + */ + abstract protected function saveStats(); } From 93fd710509539f9eb0d5115d03062ca59f6a1173 Mon Sep 17 00:00:00 2001 From: Frode Petterson Date: Fri, 4 Mar 2016 13:56:05 +0100 Subject: [PATCH 17/17] Fixed typo HFJ-1610 --- h5p.classes.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/h5p.classes.php b/h5p.classes.php index b17c418..d004f2c 100644 --- a/h5p.classes.php +++ b/h5p.classes.php @@ -2479,7 +2479,7 @@ class H5PCore { 'loaded' => $this->h5pF->getLibraryStats('library'), 'created' => $this->h5pF->getLibraryStats('content create'), 'createdUpload' => $this->h5pF->getLibraryStats('content create upload'), - 'deleted' => $this->h5pF->getLibraryStats('content deleted'), + 'deleted' => $this->h5pF->getLibraryStats('content delete'), 'resultViews' => $this->h5pF->getLibraryStats('results content'), 'shortcodeInserts' => $this->h5pF->getLibraryStats('content shortcode insert') )))