From 1645ec88effaa4e2ce9518cdaadd6bdc691ce196 Mon Sep 17 00:00:00 2001
From: thomasmars <thomasmars87@gmail.com>
Date: Thu, 22 Nov 2018 13:37:45 +0100
Subject: [PATCH] Fix content slug available check and legacy upload title
 check

- isContentSlugAvailable() could have identical slugs, which would cause the get_field_sql to return an error, since it should only return a single field.
- title is a property of the metadata object, not an index
---
 classes/framework.php | 2 +-
 mod_form.php          | 6 +++---
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/classes/framework.php b/classes/framework.php
index 35df169..c7ed7b1 100644
--- a/classes/framework.php
+++ b/classes/framework.php
@@ -1366,7 +1366,7 @@ class framework implements \H5PFrameworkInterface {
     public function isContentSlugAvailable($slug) {
         global $DB;
 
-        return !$DB->get_field_sql("SELECT slug FROM {hvp} WHERE slug = ?", array($slug));
+        return !$DB->get_records_sql("SELECT id, slug FROM {hvp} WHERE slug = ?", array($slug));
     }
 
     /**
diff --git a/mod_form.php b/mod_form.php
index 20fe20b..2e98050 100644
--- a/mod_form.php
+++ b/mod_form.php
@@ -352,13 +352,13 @@ class mod_hvp_mod_form extends moodleform_mod {
         unset($data->h5pparams);
 
         if ($data->h5paction === 'upload') {
-            if (empty($data->metadata) || empty($data->metadata['title'])) {
+            if (empty($data->metadata) || empty($data->metadata->title)) {
                 // Fix for legacy content upload to work.
                 // Fetch title from h5p.json or use a default string if not available
                 $h5pvalidator = \mod_hvp\framework::instance('validator');
-                $data->metadata['title'] = empty($h5pvalidator->h5pC->mainJsonData['title']) ? 'Uploaded Content' : $h5pvalidator->h5pC->mainJsonData['title'];
+                $data->metadata->title = empty($h5pvalidator->h5pC->mainJsonData['title']) ? 'Uploaded Content' : $h5pvalidator->h5pC->mainJsonData['title'];
             }
-            $data->name = $data->metadata['title']; // Sort of a hack, but there is no JavaScript that sets the value when there is no editor...
+            $data->name = $data->metadata->title; // Sort of a hack, but there is no JavaScript that sets the value when there is no editor...
         }
     }
 }
-- 
GitLab