MDL-68955 mod_h5pactivity: Enable dndupload to H5P activity
authorMathew May <mathewm@hotmail.co.nz>
Mon, 8 Jun 2020 04:13:09 +0000 (12:13 +0800)
committerMathew May <mathewm@hotmail.co.nz>
Wed, 10 Jun 2020 02:10:51 +0000 (10:10 +0800)
mod/h5pactivity/lang/en/h5pactivity.php
mod/h5pactivity/lib.php

index 6d48fa0..9c35dbd 100644 (file)
@@ -61,6 +61,7 @@ $string['deleteallattempts'] = 'Delete all H5P attempts';
 $string['displayexport'] = 'Allow download';
 $string['displayembed'] = 'Embed button';
 $string['displaycopyright'] = 'Copyright button';
+$string['dnduploadh5pactivity'] = 'Add an H5P activity';
 $string['duration'] = 'Duration';
 $string['enabletracking'] = 'Enable attempt tracking';
 $string['false'] = 'False';
index 36d0515..1b66faf 100644 (file)
@@ -487,3 +487,57 @@ function h5pactivity_set_mainfile(stdClass $data): void {
             0, ['subdirs' => 0, 'maxfiles' => 1]);
     }
 }
+
+/**
+ * Register the ability to handle drag and drop file uploads
+ * @return array containing details of the files / types the mod can handle
+ */
+function h5pactivity_dndupload_register(): array {
+    return [
+        'files' => [
+            [
+                'extension' => 'h5p',
+                'message' => get_string('dnduploadh5pactivity', 'h5pactivity')
+            ]
+        ]
+    ];
+}
+
+/**
+ * Handle a file that has been uploaded
+ * @param object $uploadinfo details of the file / content that has been uploaded
+ * @return int instance id of the newly created mod
+ */
+function h5pactivity_dndupload_handle($uploadinfo): int {
+    global $CFG;
+
+    $context = context_module::instance($uploadinfo->coursemodule);
+    file_save_draft_area_files($uploadinfo->draftitemid, $context->id, 'mod_h5pactivity', 'package', 0);
+    $fs = get_file_storage();
+    $files = $fs->get_area_files($context->id, 'mod_h5pactivity', 'package', 0, 'sortorder, itemid, filepath, filename', false);
+    $file = reset($files);
+
+    // Create a default h5pactivity object to pass to h5pactivity_add_instance()!
+    $h5p = get_config('h5pactivity');
+    $h5p->intro = '';
+    $h5p->introformat = FORMAT_HTML;
+    $h5p->course = $uploadinfo->course->id;
+    $h5p->coursemodule = $uploadinfo->coursemodule;
+    $h5p->grade = $CFG->gradepointdefault;
+
+    // Add some special handling for the H5P options checkboxes.
+    $factory = new \core_h5p\factory();
+    $core = $factory->get_core();
+    if (isset($uploadinfo->displayopt)) {
+        $config = (object) $uploadinfo->displayopt;
+    } else {
+        $config = \core_h5p\helper::decode_display_options($core);
+    }
+    $h5p->displayoptions = \core_h5p\helper::get_display_options($core, $config);
+
+    $h5p->cmidnumber = '';
+    $h5p->name = $uploadinfo->displayname;
+    $h5p->reference = $file->get_filename();
+
+    return h5pactivity_add_instance($h5p, null);
+}