/** * Get settings for given content * * @since 1.5.0 * @param array $content * @return array */ public function get_content_settings($content) { global $wpdb; $core = $this->get_h5p_instance('core'); // Add global disable settings $content['disable'] |= $core->getGlobalDisable(); $safe_parameters = $core->filterParameters($content); if (has_action('h5p_alter_filtered_parameters')) { // Parse the JSON parameters $decoded_parameters = json_decode($safe_parameters); /** * Allows you to alter the H5P content parameters after they have been * filtered. This hook only fires before view. * * @since 1.5.3 * * @param object &$parameters * @param string $libraryName * @param int $libraryMajorVersion * @param int $libraryMinorVersion */ do_action_ref_array('h5p_alter_filtered_parameters', array(&$decoded_parameters, $content['library']['name'], $content['library']['majorVersion'], $content['library']['minorVersion'])); // Stringify the JSON parameters $safe_parameters = json_encode($decoded_parameters); } // Add JavaScript settings for this content $settings = array('library' => H5PCore::libraryToString($content['library']), 'jsonContent' => $safe_parameters, 'fullScreen' => $content['library']['fullscreen'], 'exportUrl' => get_option('h5p_export', TRUE) ? $this->get_h5p_url() . '/exports/' . ($content['slug'] ? $content['slug'] . '-' : '') . $content['id'] . '.h5p' : '', 'embedCode' => '<iframe src="' . admin_url('admin-ajax.php?action=h5p_embed&id=' . $content['id']) . '" width=":w" height=":h" frameborder="0" allowfullscreen="allowfullscreen"></iframe>', 'resizeCode' => '<script src="' . plugins_url('h5p/h5p-php-library/js/h5p-resizer.js') . '"></script>', 'url' => admin_url('admin-ajax.php?action=h5p_embed&id=' . $content['id']), 'disable' => $content['disable'], 'contentUserData' => array(0 => array('state' => '{}'))); // Get preloaded user data for the current user $current_user = wp_get_current_user(); if (get_option('h5p_save_content_state', FALSE) && $current_user->ID) { $results = $wpdb->get_results($wpdb->prepare("SELECT hcud.sub_content_id,\n hcud.data_id,\n hcud.data\n FROM {$wpdb->prefix}h5p_contents_user_data hcud\n WHERE user_id = %d\n AND content_id = %d\n AND preload = 1", $current_user->ID, $content['id'])); if ($results) { foreach ($results as $result) { $settings['contentUserData'][$result->sub_content_id][$result->data_id] = $result->data; } } } return $settings; }
/** * Recursive. Goes through the dependency tree for the given library and * adds all the dependencies to the given array in a flat format. * * @param array $librariesUsed Flat list of all dependencies. * @param array $library To find all dependencies for. * @param int $nextWeight An integer determining the order of the libraries * when they are loaded * @param bool $editor Used interally to force all preloaded sub dependencies * of an editor dependecy to be editor dependencies. */ public function findLibraryDependencies(&$dependencies, $library, $nextWeight = 1, $editor = FALSE) { foreach (array('dynamic', 'preloaded', 'editor') as $type) { $property = $type . 'Dependencies'; if (!isset($library[$property])) { continue; // Skip, no such dependencies. } if ($type === 'preloaded' && $editor === TRUE) { // All preloaded dependencies of an editor library is set to editor. $type = 'editor'; } foreach ($library[$property] as $dependency) { $dependencyKey = $type . '-' . $dependency['machineName']; if (isset($dependencies[$dependencyKey]) === TRUE) { continue; // Skip, already have this. } $dependencyLibrary = $this->loadLibrary($dependency['machineName'], $dependency['majorVersion'], $dependency['minorVersion']); if ($dependencyLibrary) { $dependencies[$dependencyKey] = array('library' => $dependencyLibrary, 'type' => $type); $nextWeight = $this->findLibraryDependencies($dependencies, $dependencyLibrary, $nextWeight, $type === 'editor'); $dependencies[$dependencyKey]['weight'] = $nextWeight++; } else { // This site is missing a dependency! $this->h5pF->setErrorMessage($this->h5pF->t('Missing dependency @dep required by @lib.', array('@dep' => H5PCore::libraryToString($dependency), '@lib' => H5PCore::libraryToString($library)))); } } } return $nextWeight; }
/** * Move uploaded files, remove old files and update library usage. * * @param string $oldLibrary * @param string $oldParameters * @param array $newLibrary * @param string $newParameters */ public function processParameters($contentId, $newLibrary, $newParameters, $oldLibrary = NULL, $oldParameters = NULL) { $newFiles = array(); $oldFiles = array(); // Find new libraries/content dependencies and files. // Start by creating a fake library field to process. This way we get all the dependencies of the main library as well. $field = (object) array('type' => 'library'); $libraryParams = (object) array('library' => H5PCore::libraryToString($newLibrary), 'params' => $newParameters); $this->processField($field, $libraryParams, $newFiles); if ($oldLibrary !== NULL) { // Find old files and libraries. $this->processSemantics($oldFiles, $this->h5p->loadLibrarySemantics($oldLibrary['name'], $oldLibrary['majorVersion'], $oldLibrary['minorVersion']), $oldParameters); // Remove old files. for ($i = 0, $s = count($oldFiles); $i < $s; $i++) { if (!in_array($oldFiles[$i], $newFiles) && preg_match('/^(\\w+:\\/\\/|\\.\\.\\/)/i', $oldFiles[$i]) === 0) { $removeFile = $this->content_directory . $oldFiles[$i]; unlink($removeFile); $this->storage->removeFile($removeFile); } } } }
/** * Display a form for adding and editing h5p content. * * @since 1.1.0 */ public function display_new_content_page() { $contentExists = $this->content !== NULL; $plugin = H5P_Plugin::get_instance(); $core = $plugin->get_h5p_instance('core'); // Prepare form $title = $this->get_input('title', $contentExists ? $this->content['title'] : ''); $library = $this->get_input('library', $contentExists ? H5PCore::libraryToString($this->content['library']) : 0); $parameters = $this->get_input('parameters', $contentExists ? $core->filterParameters($this->content) : '{}'); // Determine upload or create if (!$contentExists && !$this->has_libraries()) { $upload = TRUE; } else { $upload = filter_input(INPUT_POST, 'action') === 'upload'; } // Filter/escape parameters, double escape that is... $safe_text = wp_check_invalid_utf8($parameters); $safe_text = _wp_specialchars($safe_text, ENT_QUOTES, false, true); $parameters = apply_filters('attribute_escape', $safe_text, $parameters); include_once 'views/new-content.php'; $this->add_editor_assets($contentExists ? $this->content['id'] : NULL); H5P_Plugin_Admin::add_script('jquery', 'h5p-php-library/js/jquery.js'); H5P_Plugin_Admin::add_script('disable', 'h5p-php-library/js/disable.js'); H5P_Plugin_Admin::add_script('toggle', 'admin/scripts/h5p-toggle.js'); // Log editor opened if ($contentExists) { new H5P_Event('content', 'edit', $this->content['id'], $this->content['title'], $this->content['library']['name'], $this->content['library']['majorVersion'] . '.' . $this->content['library']['minorVersion']); } else { new H5P_Event('content', 'new'); } }
/** * Fetch library folder and save in target directory. * * @param array $library * Library properties * @param string $target * Where the library folder will be saved */ public function exportLibrary($library, $target) { $folder = \H5PCore::libraryToString($library, true); $context = \context_system::instance(); self::exportFileTree("{$target}/{$folder}", $context->id, 'libraries', "/{$folder}/"); }
$context = \context_course::instance($course->id); if (has_capability('mod/hvp:getexport', $context)) { $hvppath = "{$CFG->httpswwwroot}/pluginfile.php/{$context->id}/mod_hvp"; $exportfilename = ($content['slug'] ? $content['slug'] . '-' : '') . $content['id'] . '.h5p'; $export = "{$hvppath}/exports/{$exportfilename}"; } } if (empty($export)) { // Remove Download button when there's no export URL $content['disable'] |= \H5PCore::DISABLE_DOWNLOAD; } // Find cm context $context = \context_module::instance($cm->id); // Add JavaScript settings for this content. $cid = 'cid-' . $content['id']; $settings['contents'][$cid] = array('library' => \H5PCore::libraryToString($content['library']), 'jsonContent' => $safeparameters, 'fullScreen' => $content['library']['fullscreen'], 'exportUrl' => $export, 'title' => $content['title'], 'disable' => $content['disable'], 'url' => "{$CFG->httpswwwroot}/mod/hvp/view.php?id={$id}", 'contentUrl' => "{$CFG->httpswwwroot}/pluginfile.php/{$context->id}/mod_hvp/content/" . $content['id'], 'contentUserData' => array(0 => \mod_hvp\content_user_data::load_pre_loaded_user_data($content['id']))); // Get assets for this content. $preloadeddependencies = $core->loadContentDependencies($content['id'], 'preloaded'); $files = $core->getDependenciesFiles($preloadeddependencies); // Determine embed type. $embedtype = \H5PCore::determineEmbedType($content['embedType'], $content['library']['embedTypes']); if ($embedtype === 'div') { $context = \context_system::instance(); $hvppath = "/pluginfile.php/{$context->id}/mod_hvp"; // Schedule JavaScripts for loading through Moodle. foreach ($files['scripts'] as $script) { $url = $hvppath . $script->path . $script->version; $settings['loadedJs'][] = $url; $PAGE->requires->js(new moodle_url($CFG->httpswwwroot . $url), true); } // Schedule stylesheets for loading through Moodle.
public function data_preprocessing(&$defaultvalues) { global $DB; $content = null; if (!empty($defaultvalues['id'])) { // Load Content $core = \mod_hvp\framework::instance(); $content = $core->loadContent($defaultvalues['id']); if ($content === null) { print_error('invalidhvp'); } } // Aaah.. we meet again h5pfile! $draftitemid = file_get_submitted_draft_itemid('h5pfile'); file_prepare_draft_area($draftitemid, $this->context->id, 'mod_hvp', 'package', 0); $defaultvalues['h5pfile'] = $draftitemid; // Individual display options are not stored, must be extracted from disable. if (isset($defaultvalues['disable'])) { // Extract disable options. \mod_hvp\framework::instance(); foreach (\H5PCore::$disable as $bit => $option) { if ($defaultvalues['disable'] & $bit) { // Disable. $defaultvalues[$option] = 0; } else { // Enable. $defaultvalues[$option] = 1; } } } // Determine default action if ($content === null && $DB->get_field_sql("SELECT id FROM {hvp_libraries} WHERE runnable = 1", null, IGNORE_MULTIPLE) === false) { $defaultvalues['h5paction'] = 'upload'; } // Set editor defaults $defaultvalues['h5plibrary'] = $content === null ? 0 : H5PCore::libraryToString($content['library']); $defaultvalues['h5pparams'] = $content === null ? '{}' : $core->filterParameters($content); // Add required editor assets. require_once 'locallib.php'; \hvp_add_editor_assets($content === null ? null : $defaultvalues['id']); // Log editor opened if ($content === null) { new \mod_hvp\event('content', 'new'); } else { new \mod_hvp\event('content', 'edit', $content['id'], $content['title'], $content['library']['name'], $content['library']['majorVersion'] . '.' . $content['library']['minorVersion']); } }
/** * Fetch library folder and save in target directory. * * @param array $library * Library properties * @param string $target * Where the library folder will be saved * @param string $developmentPath * Folder that library resides in */ public function exportLibrary($library, $target, $developmentPath = NULL) { $folder = \H5PCore::libraryToString($library, TRUE); $srcPath = $developmentPath === NULL ? "/libraries/{$folder}" : $developmentPath; self::copyFileTree("{$this->path}{$srcPath}", "{$target}/{$folder}"); }