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');
         }
     }
 }
 /**
  * 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;
     require_once $CFG->libdir . '/filelib.php';
     $errors = array();
     $error = \mod_hvp\framework::downloadH5pLibraries(true);
     if ($error !== null) {
         $errors['h5pintromsg'] = $error;
     }
     return $errors;
 }
Example #3
0
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'));
}
Example #4
0
 /**
  * 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;
 }
Example #5
0
/**
 * 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;
}
Example #6
0
        $PAGE->requires->css(new moodle_url($CFG->httpswwwroot . $url));
    }
} else {
    // JavaScripts and stylesheets will be loaded through h5p.js.
    $settings['contents'][$cid]['scripts'] = $core->getAssetsUrls($files['scripts']);
    $settings['contents'][$cid]['styles'] = $core->getAssetsUrls($files['styles']);
}
// Print JavaScript settings to page.
$PAGE->requires->data_for_js('H5PIntegration', $settings, true);
// Print page HTML.
echo $OUTPUT->header();
echo $OUTPUT->heading(format_string($content['title']));
echo '<div class="clearer"></div>';
// Print any messages.
\mod_hvp\framework::printMessages('info', \mod_hvp\framework::messages('info'));
\mod_hvp\framework::printMessages('error', \mod_hvp\framework::messages('error'));
// Print intro.
if (trim(strip_tags($content['intro']))) {
    echo $OUTPUT->box_start('mod_introbox', 'hvpintro');
    echo format_module_intro('hvp', (object) array('intro' => $content['intro'], 'introformat' => $content['introformat']), $cm->id);
    echo $OUTPUT->box_end();
}
// Print H5P Content
if ($embedtype === 'div') {
    echo '<div class="h5p-content" data-content-id="' . $content['id'] . '"></div>';
} else {
    echo '<div class="h5p-iframe-wrapper"><iframe id="h5p-iframe-' . $content['id'] . '" class="h5p-iframe" data-content-id="' . $content['id'] . '" style="height:1px" src="about:blank" frameBorder="0" scrolling="no"></iframe></div>';
}
// Find cm context
$context = \context_module::instance($cm->id);
// Trigger module viewed event.
Example #7
0
 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;
 }
Example #8
0
     if (!\H5PCore::validToken('editorajax', required_param('token', PARAM_RAW))) {
         \H5PCore::ajaxError(get_string('invalidtoken', 'hvp'));
         exit;
     }
     // Get Content ID and Context ID for upload
     $contentid = required_param('contentId', PARAM_INT);
     $contextid = required_param('contextId', PARAM_INT);
     // Create file
     $file = new H5peditorFile(\mod_hvp\framework::instance('interface'));
     if (!$file->isLoaded()) {
         H5PCore::ajaxError(get_string('filenotfound', 'hvp'));
         break;
     }
     // Make sure file is valid
     if ($file->validate()) {
         $core = \mod_hvp\framework::instance('core');
         // Save the valid file
         $file_id = $core->fs->saveFile($file, $contentid, $contextid);
         // Track temporary files for later cleanup
         $DB->insert_record_raw('hvp_tmpfiles', array('id' => $file_id), false, false, true);
     }
     $file->printResult();
     break;
     /*
      * Throw error if AJAX isnt handeled
      */
 /*
  * Throw error if AJAX isnt handeled
  */
 default:
     throw new coding_exception('Unhandled AJAX');
Example #9
0
/**
 * 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;
 }