From aec115e8245fdefbd25398015bf4b38817bdcb6d Mon Sep 17 00:00:00 2001
From: Frode Petterson <frode.andre.petterson@gmail.com>
Date: Wed, 14 Sep 2016 10:47:00 +0200
Subject: [PATCH] Get CM ID the proper way when adding new content

No relation between Instance and CM until after content is saved.
h5p/h5p-moodle-plugin#112
---
 classes/file_storage.php | 24 +++++++++++-------------
 lib.php                  |  5 ++++-
 library                  |  2 +-
 3 files changed, 16 insertions(+), 15 deletions(-)

diff --git a/classes/file_storage.php b/classes/file_storage.php
index 66b6e38..260ea67 100644
--- a/classes/file_storage.php
+++ b/classes/file_storage.php
@@ -65,21 +65,20 @@ class file_storage implements \H5PFileStorage {
      *
      * @param string $source
      *  Path on file system to content directory.
-     * @param int $id
-     *  What makes this content unique.
+     * @param array $content
+     *  Content properties
      */
-    public function saveContent($source, $id) {
+    public function saveContent($source, $content) {
         // Remove any old content.
-        $this->deleteContent($id);
+        $this->deleteContent($content);
 
         // Contents are stored in a course context.
-        $cm = \get_coursemodule_from_instance('hvp', $id);
-        $context = \context_module::instance($cm->id);
+        $context = \context_module::instance($content['coursemodule']);
         $options = array(
             'contextid' => $context->id,
             'component' => 'mod_hvp',
             'filearea' => 'content',
-            'itemid' => $id,
+            'itemid' => $content['id'],
             'filepath' => '/',
         );
 
@@ -90,13 +89,12 @@ class file_storage implements \H5PFileStorage {
     /**
      * Remove content folder.
      *
-     * @param int $id
-     *  Content identifier
+     * @param array $content
+     *  Content properties
      */
-    public function deleteContent($id) {
-        $cm = \get_coursemodule_from_instance('hvp', $id);
-        $context = \context_module::instance($cm->id);
-        self::deleteFileTree($context->id, 'content', '/', $id);
+    public function deleteContent($content) {
+        $context = \context_module::instance($content['coursemodule']);
+        self::deleteFileTree($context->id, 'content', '/', $content['id']);
     }
 
     /**
diff --git a/lib.php b/lib.php
index 99734e0..19c8a36 100644
--- a/lib.php
+++ b/lib.php
@@ -201,9 +201,12 @@ function hvp_delete_instance($id) {
         return false;
     }
 
+    // Load CM
+    $cm = \get_coursemodule_from_instance('hvp', $id);
+
     // Delete content
     $h5pstorage = \mod_hvp\framework::instance('storage');
-    $h5pstorage->deletePackage(array('id' => $hvp->id, 'slug' => $hvp->slug));
+    $h5pstorage->deletePackage(array('id' => $hvp->id, 'slug' => $hvp->slug, 'coursemodule' => $cm->id));
 
     // Get library details
     $library = $DB->get_record_sql(
diff --git a/library b/library
index c88c049..fd34af6 160000
--- a/library
+++ b/library
@@ -1 +1 @@
-Subproject commit c88c049438257b30e61801a940167d8805732ff0
+Subproject commit fd34af6964857e8a2f9f49423d6bf1a109576ac6
-- 
GitLab