Merge branch 'master' of github.com:h5p/h5p-php-library

pull/17/head
Frode Petterson 2016-03-21 12:51:04 +01:00
commit 6b31d467a0
3 changed files with 290 additions and 32 deletions

191
h5p-event-base.class.php Normal file
View File

@ -0,0 +1,191 @@
<?php
/**
* The base class for H5P events. Extend to track H5P events in your system.
*
* @package H5P
* @copyright 2016 Joubel AS
* @license MIT
*/
abstract class H5PEventBase {
// Constants
const LOG_NONE = 0;
const LOG_ALL = 1;
const LOG_ACTIONS = 2;
// Static options
public static $log_level = self::LOG_ACTIONS;
public static $log_time = 2592000; // 30 Days
// Protected variables
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.
*
* Common event types with sub type:
* content, <none> 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, <none> view own results
* content view results for content
* set new results inserted or updated
*
* settings, <none> settings page loaded
*
* library, <none> 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();
}
if (self::validStats($type, $sub_type)) {
$this->saveStats();
}
}
/**
* 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
*/
private static function validLogLevel($type, $sub_type) {
switch (self::$log_level) {
default:
case self::LOG_NONE:
return FALSE;
case self::LOG_ALL:
return TRUE; // Log everything
case self::LOG_ACTIONS:
if (self::isAction($type, $sub_type)) {
return TRUE; // Log actions
}
return FALSE;
}
}
/**
* Check if the event should be included in the statistics counter.
*
* @param string $type
* Name of event type
* @param string $sub_type
* Name of event sub type
* @return boolean
*/
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
return TRUE;
}
elseif (self::isAction($type, $sub_type)) { // Count all actions
return TRUE;
}
return FALSE;
}
/**
* Check if event type is an action.
*
* @param string $type
* Name of event type
* @param string $sub_type
* Name of event sub type
* @return boolean
*/
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
}
return FALSE;
}
/**
* 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 keyed values
*/
protected function getDataArray() {
return 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
);
}
/**
* 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',
'%s',
'%s',
'%d',
'%s',
'%s',
'%s'
);
}
/**
* Stores the event data in the database.
*
* Must be overriden by plugin.
*/
abstract protected function save();
/**
* Add current event data to statistics counter.
*
* Must be overriden by plugin.
*/
abstract protected function saveStats();
}

View File

@ -538,6 +538,20 @@ interface H5PFrameworkInterface {
*/ */
public function isContentSlugAvailable($slug); public function isContentSlugAvailable($slug);
/**
* Generates statistics from the event log per library
*
* @param string $type Type of event to generate stats for
* @return array Number values indexed by library name and version
*/
public function getLibraryStats($type);
/**
* Aggregate the current number of H5P authors
* @return int
*/
public function getNumAuthors();
/** /**
* Stores hash keys for cached assets, aggregated JavaScripts and * Stores hash keys for cached assets, aggregated JavaScripts and
* stylesheets, and connects it to libraries so that we know which cache file * stylesheets, and connects it to libraries so that we know which cache file
@ -1713,6 +1727,9 @@ class H5PCore {
if ($development_mode & H5PDevelopment::MODE_LIBRARY) { if ($development_mode & H5PDevelopment::MODE_LIBRARY) {
$this->h5pD = new H5PDevelopment($this->h5pF, $path . '/', $language); $this->h5pD = new H5PDevelopment($this->h5pF, $path . '/', $language);
} }
$this->detectSiteType();
$this->fullPluginPath = preg_replace('/\/[^\/]+[\/]?$/', '' , dirname(__FILE__));
} }
/** /**
@ -2398,10 +2415,8 @@ class H5PCore {
$type = $this->h5pF->getOption('site_type', 'local'); $type = $this->h5pF->getOption('site_type', 'local');
// Determine remote/visitor origin // Determine remote/visitor origin
$localhostPattern = '/^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$/i'; if ($type === 'network' ||
($type === 'local' && !preg_match('/^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$/i', $_SERVER['REMOTE_ADDR']))) {
// localhost
if ($type !== 'internet' && !preg_match($localhostPattern, $_SERVER['REMOTE_ADDR'])) {
if (filter_var($_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE)) { if (filter_var($_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE)) {
// Internet // Internet
$this->h5pF->setOption('site_type', 'internet'); $this->h5pF->setOption('site_type', 'internet');
@ -2421,48 +2436,100 @@ class H5PCore {
* A distinct array of installed libraries * A distinct array of installed libraries
*/ */
public function getLibrariesInstalled() { public function getLibrariesInstalled() {
$librariesInstalled = []; $librariesInstalled = array();
$libs = $this->h5pF->loadLibraries(); $libs = $this->h5pF->loadLibraries();
foreach($libs as $libName => $library) { foreach($libs as $libName => $library) {
foreach($library as $libVersion) { foreach($library as $libVersion) {
$librariesInstalled[] = $libName.' '.$libVersion->major_version.'.'.$libVersion->minor_version.'.'.$libVersion->patch_version; $librariesInstalled[$libName.' '.$libVersion->major_version.'.'.$libVersion->minor_version] = $libVersion->patch_version;
} }
} }
return $librariesInstalled; 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. * Fetch a list of libraries' metadata from h5p.org.
* Save URL tutorial to database. Each platform implementation * Save URL tutorial to database. Each platform implementation
* is responsible for invoking this, eg using cron * is responsible for invoking this, eg using cron
*/ */
public function fetchLibrariesMetadata($fetchingDisabled = FALSE) { public function fetchLibrariesMetadata($fetchingDisabled = FALSE) {
$platformInfo = $this->h5pF->getPlatformInfo(); // Gather data
$platformInfo['autoFetchingDisabled'] = $fetchingDisabled; $uuid = $this->h5pF->getOption('site_uuid', '');
$platformInfo['uuid'] = $this->h5pF->getOption('site_uuid', ''); $platform = $this->h5pF->getPlatformInfo();
// Adding random string to GET to be sure nothing is cached $data = array(
$random = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 5); 'api_version' => 2,
$json = $this->h5pF->fetchExternalData('http://h5p.org/libraries-metadata.json?api=1&platform=' . urlencode(json_encode($platformInfo)) . '&x=' . urlencode($random)); 'uuid' => $uuid,
if ($json !== NULL) { 'platform_name' => $platform['name'],
$json = json_decode($json); 'platform_version' => $platform['version'],
if (isset($json->libraries)) { 'h5p_version' => $platform['h5pVersion'],
foreach ($json->libraries as $machineName => $libInfo) { 'disabled' => $fetchingDisabled ? 1 : 0,
$this->h5pF->setLibraryTutorialUrl($machineName, $libInfo->tutorialUrl); 'local_id' => hash('crc32', $this->fullPluginPath),
} 'type' => $this->h5pF->getOption('site_type', 'local'),
} 'num_authors' => $this->h5pF->getNumAuthors(),
if($platformInfo['uuid'] === '' && isset($json->uuid)) { 'libraries' => json_encode($this->combineArrayValues(array(
$this->h5pF->setOption('site_uuid', $json->uuid); 'patch' => $this->getLibrariesInstalled(),
} 'content' => $this->h5pF->getLibraryContentCount(),
if (isset($json->latest) && !empty($json->latest)) { 'loaded' => $this->h5pF->getLibraryStats('library'),
$this->h5pF->setOption('update_available', $json->latest->releasedAt); 'created' => $this->h5pF->getLibraryStats('content create'),
$this->h5pF->setOption('update_available_path', $json->latest->path); 'createdUpload' => $this->h5pF->getLibraryStats('content create upload'),
'deleted' => $this->h5pF->getLibraryStats('content delete'),
'resultViews' => $this->h5pF->getLibraryStats('results content'),
'shortcodeInserts' => $this->h5pF->getLibraryStats('content shortcode insert')
)))
);
// 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() { public function getGlobalDisable() {
$disable = self::DISABLE_NONE; $disable = self::DISABLE_NONE;

View File

@ -139,7 +139,7 @@ H5P.init = function (target) {
// Check if we should add and display a fullscreen button for this H5P. // Check if we should add and display a fullscreen button for this H5P.
if (contentData.fullScreen == 1 && H5P.canHasFullScreen) { if (contentData.fullScreen == 1 && H5P.canHasFullScreen) {
H5P.jQuery('<div class="h5p-content-controls"><div role="button" tabindex="1" class="h5p-enable-fullscreen" title="' + H5P.t('fullscreen') + '"></div></div>').prependTo($container).children().click(function () { H5P.jQuery('<div class="h5p-content-controls"><div role="button" tabindex="0" class="h5p-enable-fullscreen" title="' + H5P.t('fullscreen') + '"></div></div>').prependTo($container).children().click(function () {
H5P.fullScreen($container, instance); H5P.fullScreen($container, instance);
}); });
} }
@ -148,7 +148,7 @@ H5P.init = function (target) {
var $actions = H5P.jQuery('<ul class="h5p-actions"></ul>'); var $actions = H5P.jQuery('<ul class="h5p-actions"></ul>');
if (!(contentData.disable & H5P.DISABLE_DOWNLOAD)) { if (!(contentData.disable & H5P.DISABLE_DOWNLOAD)) {
// Add export button // Add export button
H5P.jQuery('<li class="h5p-button h5p-export" role="button" tabindex="1" title="' + H5P.t('downloadDescription') + '">' + H5P.t('download') + '</li>').appendTo($actions).click(function () { H5P.jQuery('<li class="h5p-button h5p-export" role="button" tabindex="0" title="' + H5P.t('downloadDescription') + '">' + H5P.t('download') + '</li>').appendTo($actions).click(function () {
window.location.href = contentData.exportUrl; window.location.href = contentData.exportUrl;
}); });
} }
@ -157,7 +157,7 @@ H5P.init = function (target) {
if (copyright) { if (copyright) {
// Add copyright dialog button // Add copyright dialog button
H5P.jQuery('<li class="h5p-button h5p-copyrights" role="button" tabindex="1" title="' + H5P.t('copyrightsDescription') + '">' + H5P.t('copyrights') + '</li>').appendTo($actions).click(function () { H5P.jQuery('<li class="h5p-button h5p-copyrights" role="button" tabindex="0" title="' + H5P.t('copyrightsDescription') + '">' + H5P.t('copyrights') + '</li>').appendTo($actions).click(function () {
// Open dialog with copyright information // Open dialog with copyright information
var dialog = new H5P.Dialog('copyrights', H5P.t('copyrightInformation'), copyright, $container); var dialog = new H5P.Dialog('copyrights', H5P.t('copyrightInformation'), copyright, $container);
dialog.open(); dialog.open();
@ -166,7 +166,7 @@ H5P.init = function (target) {
} }
if (!(contentData.disable & H5P.DISABLE_EMBED)) { if (!(contentData.disable & H5P.DISABLE_EMBED)) {
// Add embed button // Add embed button
H5P.jQuery('<li class="h5p-button h5p-embed" role="button" tabindex="1" title="' + H5P.t('embedDescription') + '">' + H5P.t('embed') + '</li>').appendTo($actions).click(function () { H5P.jQuery('<li class="h5p-button h5p-embed" role="button" tabindex="0" title="' + H5P.t('embedDescription') + '">' + H5P.t('embed') + '</li>').appendTo($actions).click(function () {
H5P.openEmbedDialog($actions, contentData.embedCode, contentData.resizeCode, { H5P.openEmbedDialog($actions, contentData.embedCode, contentData.resizeCode, {
width: $element.width(), width: $element.width(),
height: $element.height() height: $element.height()
@ -545,7 +545,7 @@ H5P.fullScreen = function ($element, instance, exitCallback, body) {
} }
before('h5p-semi-fullscreen'); before('h5p-semi-fullscreen');
var $disable = H5P.jQuery('<div role="button" tabindex="1" class="h5p-disable-fullscreen" title="' + H5P.t('disableFullscreen') + '"></div>').appendTo($container.find('.h5p-content-controls')); var $disable = H5P.jQuery('<div role="button" tabindex="0" class="h5p-disable-fullscreen" title="' + H5P.t('disableFullscreen') + '"></div>').appendTo($container.find('.h5p-content-controls'));
var keyup, disableSemiFullscreen = H5P.exitFullScreen = function () { var keyup, disableSemiFullscreen = H5P.exitFullScreen = function () {
if (prevViewportContent) { if (prevViewportContent) {
// Use content from the previous viewport tag // Use content from the previous viewport tag
@ -887,7 +887,7 @@ H5P.Dialog = function (name, title, content, $element) {
<div class="h5p-inner">\ <div class="h5p-inner">\
<h2>' + title + '</h2>\ <h2>' + title + '</h2>\
<div class="h5p-scroll-content">' + content + '</div>\ <div class="h5p-scroll-content">' + content + '</div>\
<div class="h5p-close" role="button" tabindex="1" title="' + H5P.t('close') + '">\ <div class="h5p-close" role="button" tabindex="0" title="' + H5P.t('close') + '">\
</div>\ </div>\
</div>') </div>')
.insertAfter($element) .insertAfter($element)