Commit a9130526 authored by falcon's avatar falcon

Add development modes

parent fda70e68
......@@ -46,6 +46,20 @@ function h5p_admin_settings() {
'#default_value' => variable_get('h5p_library_whitelist_extras', H5PCore::$defaultLibraryWhitelistExtras),
'#description' => t("Libraries might need to accept more files that should be allowed in normal contents. Add extra files here. Changing this list has security implications. Do not change it if you don't know what you're doing. Adding php to the list is for instance a security risk."),
);
$form['h5p_dev_mode'] = array(
'#type' => 'checkbox',
'#title' => t('Enable H5P development mode'),
'#default_value' => variable_get('h5p_dev_mode', '0'),
'#description' => t("With this feature enabled libraries with the same patch version will overwrite existing libraries in the database, and semantics.json will be read from file."),
);
$form['h5p_content_dev_mode'] = array(
'#type' => 'checkbox',
'#title' => t('Enable H5P content development mode'),
'#default_value' => variable_get('h5p_content_dev_mode', '0'),
'#description' => t("With this feature enabled content.json will be read from file. Changes to the content made using the editor won't be visible when this mode is actice."),
);
// Make changes to the settings before passing them off to
// system_settings_form_submit().
......
......@@ -66,16 +66,24 @@ class H5PDrupal implements H5PFrameworkInterface {
* Implements isPatchedLibrary
*/
public function isPatchedLibrary($library) {
$operator = $this->isInDevMode() ? '<=' : '<';
$result = db_result(db_query(
"SELECT 1
FROM {h5p_libraries}
WHERE machine_name = '%s'
AND major_version = %d
AND minor_version = %d
AND patch_version < %d",
AND patch_version $operator %d",
$library['machineName'], $library['majorVersion'], $library['minorVersion'], $library['patchVersion']));
return $result === '1';
}
/**
* Implements isInDevMode
*/
public function isInDevMode() {
return (bool) variable_get('h5p_dev_mode', '0');
}
/**
* Implements mayUpdateLibraries
......@@ -292,6 +300,12 @@ class H5PDrupal implements H5PFrameworkInterface {
'minorVersion' => $dependency['minorVersion'],
);
}
if ($this->isInDevMode()) {
$semantics = $this->getSemanticsFromFile($library['machineName'], $library['majorVersion'], $library['minorVersion']);
if ($semantics) {
$library['semantics'] = $semantics;
}
}
return $library;
}
......@@ -301,19 +315,35 @@ class H5PDrupal implements H5PFrameworkInterface {
* Calls modules implementing hook_alter_h5p_semantics().
*/
public function getLibrarySemantics($machineName, $majorVersion, $minorVersion) {
$library = db_fetch_array(db_query(
"SELECT semantics
if ($this->isInDevMode()) {
$semantics = $this->getSemanticsFromFile($machineName, $majorVersion, $minorVersion);
}
else {
$semantics = db_result(db_query(
"SELECT semantics
FROM {h5p_libraries}
WHERE machine_name = '%s'
AND major_version = %d
AND minor_version = %d",
$machineName, $majorVersion, $minorVersion));
$semantics = json_decode($library['semantics']);
$machineName, $majorVersion, $minorVersion));
}
$semantics = json_decode($semantics);
drupal_alter('h5p_semantics', $semantics);
return $semantics;
}
private function getSemanticsFromFile($machineName, $majorVersion, $minorVersion) {
$semanticsPath = file_create_path(file_directory_path() . '/' . variable_get('h5p_default_path', 'h5p') . '/libraries/' . $machineName . '-' . $majorVersion . '.' . $minorVersion . '/semantics.json');
if (file_exists($semanticsPath)) {
$semantics = file_get_contents($semanticsPath);
if (!json_decode($semantics, TRUE)) {
drupal_set_message(t('Invalid json in semantics for %library', array('%library' => $machineName)), 'warning');
}
return $semantics;
}
return FALSE;
}
}
?>
......@@ -244,7 +244,7 @@ function h5p_delete($node) {
*/
function h5p_load($node) {
$h5p = db_fetch_object(db_query(
"SELECT hn.json_content, hn.embed_type, hn.main_library_id, hl.machine_name, hl.major_version, hl.minor_version
"SELECT hn.json_content, hn.embed_type, hn.main_library_id, hl.machine_name, hl.major_version, hl.minor_version
FROM {h5p_nodes} hn
JOIN {h5p_libraries} hl ON hn.main_library_id = hl.library_id
WHERE content_id = %d", h5p_get_content_id($node)
......@@ -258,6 +258,16 @@ function h5p_load($node) {
);
unset($h5p->machine_name, $h5p->major_version, $h5p->minor_version);
}
if (variable_get('h5p_content_dev_mode', 0)) {
$json_content_path = file_create_path(file_directory_path() . '/' . variable_get('h5p_default_path', 'h5p') . '/content/' . h5p_get_content_id($node) . '/content.json');
if (file_exists($json_content_path)) {
$json_content = file_get_contents($json_content_path);
if (!json_decode($json_content, TRUE)) {
drupal_set_message(t('Invalid json in json content'), 'warning');
}
$h5p->json_content = $json_content;
}
}
return $h5p;
}
......@@ -378,7 +388,7 @@ function h5p_get_filtered_json_content($node) {
if (isset($node->vid)) {
$cid = 'validated_json_' . $node->vid;
$cache = cache_get($cid);
if (isset($cache->data)) {
if (isset($cache->data) && !variable_get('h5p_content_dev_mode', 0)) {
return $cache->data;
}
}
......
......@@ -90,6 +90,15 @@ interface H5PFrameworkInterface {
* FALSE otherwise
*/
public function isPatchedLibrary($library);
/**
* Is H5P in development mode?
*
* @return boolean
* TRUE if H5P development mode is active
* FALSE otherwise
*/
public function isInDevMode();
/**
* Is the current user allowed to update libraries?
......
......@@ -5,13 +5,8 @@ class H5peditorDrupalStorage implements H5peditorStorage {
function __construct() { }
public function getSemantics($machineName, $majorVersion, $minorVersion) {
return db_result(db_query(
"SELECT semantics
FROM {h5p_libraries}
WHERE machine_name = '%s'
AND major_version = %d
AND minor_version = %d",
$machineName, $majorVersion, $minorVersion));
$coreInterface = _h5p_get_instance('interface');
return $coreInterface->getLibrarySemantics($machineName, $majorVersion, $minorVersion);
}
public function getLanguage($machineName, $majorVersion, $minorVersion) {
......
......@@ -91,7 +91,7 @@ ns.loadLibrary = function (libraryName, callback) {
ns.$.ajax({
url: ns.ajaxPath + 'libraries/' + library.machineName + '/' + library.majorVersion + '/' + library.minorVersion,
success: function (libraryData) {
var semantics = JSON.parse(libraryData.semantics);
var semantics = libraryData.semantics;
if (libraryData.language !== undefined) {
var language = JSON.parse(libraryData.language);
semantics = ns.$.extend(true, [], semantics, language.semantics);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment