From 9f126ac2bdca4f8fe74fce70dbff15f87a6f1e8a Mon Sep 17 00:00:00 2001 From: Juho Jaakkola Date: Tue, 22 Dec 2015 15:56:27 +0200 Subject: [PATCH] Fixes database queries related to loading content libraries --- hvp.php | 74 ++++++++++++++++++++++++++++++++++++++------------------ view.php | 5 ++-- 2 files changed, 53 insertions(+), 26 deletions(-) diff --git a/hvp.php b/hvp.php index e3068cb7..5b5b3e8b 100644 --- a/hvp.php +++ b/hvp.php @@ -539,9 +539,9 @@ public function loadLibrarySemantics($name, $majorVersion, $minorVersion) { $semantics = $DB->get_field_sql( "SELECT semantics FROM {hvp_libraries} - WHERE name = %s - AND major_version = %d - AND minor_version = %d", + WHERE machine_name = ? + AND major_version = ? + AND minor_version = ?", array($name, $majorVersion, $minorVersion) ); @@ -561,26 +561,43 @@ public function alterLibrarySemantics(&$semantics, $name, $majorVersion, $minorV public function loadContent($id) { global $DB; - // TODO: Verify that this work with other databases than MySQL. I suspect - // that camelCase won't work for e.g. PostgreSQL - $content = $DB->get_record_sql( + $data = $DB->get_record_sql( "SELECT hc.id - , hc.json_content AS params + , hc.name + , hc.json_content , hc.filtered , hc.slug - , hc.embed_type AS embedType + , hc.embed_type , hc.disable - , hl.id AS libraryId - , hl.machine_name AS libraryName - , hl.major_version AS libraryMajorVersion - , hl.minor_version AS libraryMinorVersion - , hl.embed_types AS libraryEmbedTypes - , hl.fullscreen AS libraryFullscreen + , hl.id AS library_id + , hl.machine_name + , hl.major_version + , hl.minor_version + , hl.embed_types + , hl.fullscreen FROM {hvp} hc JOIN {hvp_libraries} hl ON hl.id = hc.main_library_id WHERE hc.id = ?", array($id) ); + // Some databases do not support camelCase, so we need to manually + // map the values to the camelCase names used by the H5P core. + $content = array( + 'id' => $data->id, + 'title' => $data->name, + 'params' => $data->json_content, + 'filtered' => $data->filtered, + 'slug' => $data->slug, + 'embedType' => $data->embed_type, + 'disable' => $data->disable, + 'libraryId' => $data->library_id, + 'libraryName' => $data->machine_name, + 'libraryMajorVersion' => $data->major_version, + 'libraryMinorVersion' => $data->minor_version, + 'libraryEmbedTypes' => $data->embed_types, + 'libraryFullscreen' => $data->fullscreen, + ); + return $content; } @@ -592,14 +609,14 @@ public function loadContentDependencies($id, $type = NULL) { $query = "SELECT hl.id - , hl.machine_name AS machineName - , hl.major_version AS majorVersion - , hl.minor_version AS minorVersion - , hl.patch_version AS patchVersion - , hl.preloaded_css AS preloadedCss - , hl.preloaded_js AS preloadedJs - , hcl.drop_css AS dropCss - , hcl.dependency_type AS dependencyType + , hl.machine_name + , hl.major_version + , hl.minor_version + , hl.patch_version + , hl.preloaded_css + , hl.preloaded_js + , hcl.drop_css + , hcl.dependency_type FROM {hvp_contents_libraries} hcl JOIN {hvp_libraries} hl ON hcl.library_id = hl.id WHERE hcl.hvp_id = ?"; @@ -611,7 +628,14 @@ public function loadContentDependencies($id, $type = NULL) { } $query .= " ORDER BY hcl.weight"; - return $DB->get_records_sql($query, $queryArgs); + $data = $DB->get_records_sql($query, $queryArgs); + + $dependencies = array(); + foreach ($data as $dependency) { + $dependencies[] = H5PCore::snakeToCamel($dependency); + } + + return $dependencies; } /** @@ -639,6 +663,8 @@ public function updateContentFields($id, $fields) { global $DB; $content = new stdClass(); + $content->id = $id; + foreach ($fields as $name => $value) { $content->$name = $value; // TODO: Verify that this works, we might have to convert $name from @@ -700,7 +726,7 @@ public function saveLibraryUsage($contentId, $librariesInUse) { foreach ($librariesInUse as $dependency) { $dropCss = in_array($dependency['library']['machineName'], $dropLibraryCssList) ? 1 : 0; - $DB->insert_record('h5p_contents_libraries', array( + $DB->insert_record('hvp_contents_libraries', array( 'hvp_id' => $contentId, 'library_id' => $dependency['library']['libraryId'], 'dependency_type' => $dependency['type'], diff --git a/view.php b/view.php index 7b5030f2..06f91e3d 100644 --- a/view.php +++ b/view.php @@ -48,8 +48,7 @@ $hvp->disable |= $core->getGlobalDisable(); } -// TODO: Create Content! -throw new coding_exception('Create proper $content before filtering parameters'); +$content = $core->loadContent($cm->instance); // Filter content parameters $safe_parameters = $core->filterParameters($content); @@ -75,6 +74,8 @@ // Get assets for this content $preloaded_dependencies = $core->loadContentDependencies($hvp->id, 'preloaded'); +$preloaded_dependencies = $core->loadContentDependencies($cm->instance, 'preloaded'); + $files = $core->getDependenciesFiles($preloaded_dependencies); // TODO:Insert hook/event for altering assets?