public function execute() { global $USER; // Check to make sure external communications hasn't been disabled $extcom = !!get_config('mod_hvp', 'external_communication'); $extcomnotify = !!get_config('mod_hvp', 'external_communication_notify'); if ($extcom || !$extcomnotify) { $core = \mod_hvp\framework::instance(); $core->fetchLibrariesMetadata(!$extcom); set_config('external_communication_notify', $extcom ? false : time(), 'mod_hvp'); // Notify admin if there are updates available! $update_available = \get_config('mod_hvp', 'update_available'); $current_update = \get_config('mod_hvp', 'current_update'); $admin_notified = \get_config('mod_hvp', 'admin_notified'); if ($admin_notified !== $update_available && $update_available !== false && $current_update !== false && $current_update < $update_available) { // New update is available // Send message $updatesurl = new \moodle_url('/mod/hvp/library_list.php'); $message = new \stdClass(); $message->component = 'mod_hvp'; $message->name = 'updates'; $message->userfrom = $USER; $message->userto = get_admin(); $message->subject = get_string('updatesavailabletitle', 'mod_hvp'); $message->fullmessage = get_string('updatesavailablemsgpt1', 'mod_hvp') . ' ' . get_string('updatesavailablemsgpt2', 'mod_hvp') . "\n\n" . get_string('updatesavailablemsgpt3', 'mod_hvp', date('Y-m-d', $update_available)) . "\n" . get_string('updatesavailablemsgpt4', 'mod_hvp', date('Y-m-d', $current_update)) . "\n\n" . $updatesurl; $message->fullmessageformat = FORMAT_PLAIN; $message->fullmessagehtml = '<p>' . get_string('updatesavailablemsgpt1', 'mod_hvp') . '<br/>' . get_string('updatesavailablemsgpt2', 'mod_hvp') . '</p>' . '<p>' . get_string('updatesavailablemsgpt3', 'mod_hvp', '<b>' . date('Y-m-d', $update_available) . '</b>') . '<br/>' . get_string('updatesavailablemsgpt4', 'mod_hvp', '<b>' . date('Y-m-d', $current_update) . '</b>') . '</p>' . '<a href="' . $updatesurl . '" target="_blank">' . $updatesurl . '</a>'; $message->smallmessage = ''; $message->notification = 1; message_send($message); // Keep track of which version we've notfied about \set_config('admin_notified', $update_available, 'mod_hvp'); } } }
function xmldb_hvp_install() { // Try to install all the default content types require_once __DIR__ . '/../autoloader.php'; // Override permission check for the install process, since caps hasn't // been set yet. $interface = \mod_hvp\framework::instance('interface'); $interface->mayUpdateLibraries(true); // Fetch info about library updates $core = \mod_hvp\framework::instance('core'); $core->fetchLibrariesMetadata(); // Download default libraries and try to install $error = \mod_hvp\framework::downloadH5pLibraries(); if ($error !== null) { \mod_hvp\framework::messages('error', $error); } // Print any messages echo '<h3>' . get_string('welcomeheader', 'hvp') . '</h3>' . '<p>' . get_string('welcomegettingstarted', 'hvp', array('moodle_tutorial' => 'href="https://h5p.org/moodle" target="_blank"', 'example_content' => 'href="https://h5p.org/content-types-and-applications" target="_blank"')) . '</p>' . '<p>' . get_string('welcomecommunity', 'hvp', array('forums' => 'href="https://h5p.org/forum" target="_blank"', 'gitter' => 'href="https://gitter.im/h5p/CommunityChat" target="_blank"')) . '</p>' . '<p>' . get_string('welcomecontactus', 'hvp', 'href="https://h5p.org/contact" target="_blank"') . '</p>'; \mod_hvp\framework::printMessages('info', \mod_hvp\framework::messages('info')); \mod_hvp\framework::printMessages('error', \mod_hvp\framework::messages('error')); }
/** * Make it easy to download and install H5P libraries. * * @param boolean $onlyupdate Prevent install of new libraries * @return string|null Error or null if everything's OK. */ public static function downloadH5pLibraries($onlyupdate = false) { global $CFG; $update_available = \get_config('mod_hvp', 'update_available'); $current_update = \get_config('mod_hvp', 'current_update'); if ($update_available === $current_update) { // Prevent re-submission of forms/action return null; } // URL for file to download $download_url = \get_config('mod_hvp', 'update_available_path'); if (!$download_url) { return get_string('missingh5purl', 'hvp'); } // Generate local tmp file path $local_folder = $CFG->tempdir . uniqid('/hvp-'); $local_file = $local_folder . '.h5p'; if (!\download_file_content($download_url, null, null, false, 300, 20, false, $local_file)) { return get_string('unabletodownloadh5p', 'hvp'); } // Add folder and file paths to H5P Core $interface = \mod_hvp\framework::instance('interface'); $interface->getUploadedH5pFolderPath($local_folder); $interface->getUploadedH5pPath($local_file); // Validate package $h5pValidator = \mod_hvp\framework::instance('validator'); if (!$h5pValidator->isValidPackage(true, $onlyupdate)) { @unlink($local_file); $messages = \mod_hvp\framework::messages('error'); return implode('<br/>', $messages); } // Install H5P file into Moodle $storage = \mod_hvp\framework::instance('storage'); $storage->savePackage(null, null, true); \set_config('current_update', $update_available, 'mod_hvp'); return null; }
/** * Removes an instance of the hvp from the database * * Given an ID of an instance of this module, * this function will permanently delete the instance * and any data that depends on it. * * @param int $id Id of the module instance * @return boolean Success/Failure */ function hvp_delete_instance($id) { global $DB; // Load content record if (!($hvp = $DB->get_record('hvp', array('id' => "{$id}")))) { return false; } // Delete content $h5pstorage = \mod_hvp\framework::instance('storage'); $h5pstorage->deletePackage(array('id' => $hvp->id, 'slug' => $hvp->slug)); // Get library details $library = $DB->get_record_sql("SELECT machine_name AS name, major_version, minor_version\n FROM {hvp_libraries}\n WHERE id = ?", array($hvp->main_library_id)); // Log content delete new \mod_hvp\event('content', 'delete', $hvp->id, $hvp->name, $library->name, $library->major_version . '.' . $library->minor_version); return true; }
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ require_once "../../config.php"; require_once "locallib.php"; $id = required_param('id', PARAM_INT); $url = new \moodle_url('/mod/hvp/view.php', array('id' => $id)); $PAGE->set_url($url); if (!($cm = get_coursemodule_from_id('hvp', $id))) { print_error('invalidcoursemodule'); } if (!($course = $DB->get_record('course', array('id' => $cm->course)))) { print_error('coursemisconf'); } require_course_login($course, false, $cm); // Load H5P Core. $core = \mod_hvp\framework::instance(); // Load H5P Content. $content = $core->loadContent($cm->instance); if ($content === null) { print_error('invalidhvp'); } // Log view new \mod_hvp\event('content', NULL, $content['id'], $content['title'], $content['library']['name'], $content['library']['majorVersion'] . '.' . $content['library']['minorVersion']); $PAGE->set_title(format_string($content['title'])); $PAGE->set_heading($course->fullname); // Mark viewed by user (if required). $completion = new completion_info($course); $completion->set_module_viewed($cm); // Attach scripts, styles, etc. from core. $settings = hvp_get_core_assets(); // Add global disable settings.
public function validation($data, $files) { global $CFG; $errors = parent::validation($data, $files); if ($data['h5paction'] === 'upload') { // Validate uploaded H5P file if (empty($data['h5pfile'])) { // Field missing $errors['h5pfile'] = get_string('required'); } else { $files = $this->get_draft_files('h5pfile'); if (count($files) < 1) { // No file uploaded $errors['h5pfile'] = get_string('required'); } else { // Prepare to validate package $file = reset($files); $interface = \mod_hvp\framework::instance('interface'); $path = $CFG->tempdir . uniqid('/hvp-'); $interface->getUploadedH5pFolderPath($path); $path .= '.h5p'; $interface->getUploadedH5pPath($path); $file->copy_content_to($path); $h5pvalidator = \mod_hvp\framework::instance('validator'); if (!$h5pvalidator->isValidPackage()) { // Errors while validating the package $infomessages = implode('<br/>', \mod_hvp\framework::messages('info')); $errormessages = implode('<br/>', \mod_hvp\framework::messages('error')); $errors['h5pfile'] = ($errormessages ? $errormessages . '<br/>' : '') . $infomessages; } } } } else { // Validate library and params used in editor $core = \mod_hvp\framework::instance(); // Get library array from string $library = H5PCore::libraryFromString($data['h5plibrary']); if (!$library) { $errors['h5peditor'] = get_string('invalidlibrary', 'hvp'); } else { // Check that library exists $library['libraryId'] = $core->h5pF->getLibraryId($library['machineName'], $library['majorVersion'], $library['minorVersion']); if (!$library['libraryId']) { $errors['h5peditor'] = get_string('nosuchlibrary', 'hvp'); } else { $data['h5plibrary'] = $library; // Verify that parameters are valid if (empty($data['h5pparams'])) { $errors['h5peditor'] = get_string('noparameters', 'hvp'); } else { $params = json_decode($data['h5pparams']); if ($params === NULL) { $errors['h5peditor'] = get_string('invalidparameters', 'hvp'); } else { $data['h5pparams'] = $params; } } } } } return $errors; }
/** * Gets the information needed when content is upgraded * * @method hvp_get_library_upgrade_info * @param string $name * @param int $major * @param int $minor * @return object Library metadata including name, version, semantics and path * to upgrade script */ function hvp_get_library_upgrade_info($name, $major, $minor) { global $CFG; $library = (object) array('name' => $name, 'version' => (object) array('major' => $major, 'minor' => $minor)); $core = \mod_hvp\framework::instance(); $library->semantics = $core->loadLibrarySemantics($library->name, $library->version->major, $library->version->minor); if ($library->semantics === null) { http_response_code(404); return; } $context = \context_system::instance(); $libraryfoldername = "{$library->name}-{$library->version->major}.{$library->version->minor}"; if (\mod_hvp\file_storage::fileExists($context->id, 'libraries', '/' . $libraryfoldername . '/', 'upgrades.js')) { $basepath = $CFG->httpswwwroot . '/'; $library->upgradesScript = "{$basepath}pluginfile.php/{$context->id}/mod_hvp/libraries/{$libraryfoldername}/upgrades.js"; } return $library; }
/** * Validate incoming data * * @param array $data array of ("fieldname"=>value) of submitted data * @param array $files array of uploaded files "element_name"=>tmp_file_path * @return array of "element_name"=>"error_description" if there are errors, * or an empty array if everything is OK (true allowed for backwards compatibility too). */ function validation($data, $files) { global $CFG; $errors = array(); // Check for file if (empty($data['h5pfile'])) { $errors['h5pfile'] = get_string('required'); return $errors; } $files = $this->get_draft_files('h5pfile'); if (count($files) < 1) { $errors['h5pfile'] = get_string('required'); return $errors; } // Add file so that core framework can find it $file = reset($files); $interface = \mod_hvp\framework::instance('interface'); $path = $CFG->tempdir . uniqid('/hvp-'); $interface->getUploadedH5pFolderPath($path); $path .= '.h5p'; $interface->getUploadedH5pPath($path); $file->copy_content_to($path); // Validate package $h5pValidator = \mod_hvp\framework::instance('validator'); if (!$h5pValidator->isValidPackage(true, isset($data['onlyupdate']))) { $infomessages = implode('<br/>', \mod_hvp\framework::messages('info')); $errormessages = implode('<br/>', \mod_hvp\framework::messages('error')); $errors['h5pfile'] = ($errormessages ? $errormessages . '<br/>' : '') . $infomessages; } return $errors; }