Example #1
0
     $context = \context_system::instance();
     if (!has_capability('mod/hvp:restrictlibraries', $context)) {
         \H5PCore::ajaxError(get_string('nopermissiontorestrict', 'hvp'));
         http_response_code(403);
         break;
     }
     $library_id = required_param('library_id', PARAM_INT);
     $restrict = required_param('restrict', PARAM_INT);
     if (!\H5PCore::validToken('library_' . $library_id, required_param('token', PARAM_RAW))) {
         \H5PCore::ajaxError(get_string('invalidtoken', 'hvp'));
         exit;
     }
     hvp_restrict_library($library_id, $restrict);
     header('Cache-Control', 'no-cache');
     header('Content-Type: application/json');
     echo json_encode(array('url' => (new moodle_url('/mod/hvp/ajax.php', array('action' => 'restrict_library', 'token' => \H5PCore::createToken('library_' . $library_id), 'restrict' => $restrict === '1' ? 0 : 1, 'library_id' => $library_id)))->out(false)));
     break;
     /*
      * Collecting data needed by H5P content upgrade
      *
      * Type: HTTP GET
      *
      * Parameters:
      *  - library (Format: /<machine-name>/<major-version>/<minor-version>)
      */
 /*
  * Collecting data needed by H5P content upgrade
  *
  * Type: HTTP GET
  *
  * Parameters:
    $h5pStorage->savePackage(null, null, true);
}
$core = \mod_hvp\framework::instance();
$numNotFiltered = $core->h5pF->getNumNotFiltered();
$libraries = $core->h5pF->loadLibraries();
// Add settings for each library
$settings = array();
$i = 0;
foreach ($libraries as $versions) {
    foreach ($versions as $library) {
        $usage = $core->h5pF->getLibraryUsage($library->id, $numNotFiltered ? true : false);
        if ($library->runnable) {
            $upgrades = $core->getUpgrades($library, $versions);
            $upgradeUrl = empty($upgrades) ? false : (new moodle_url('/mod/hvp/upgrade_content_page.php', array('library_id' => $library->id)))->out(false);
            $restricted = isset($library->restricted) && $library->restricted == 1 ? true : false;
            $restricted_url = (new moodle_url('/mod/hvp/ajax.php', array('action' => 'restrict_library', 'token' => \H5PCore::createToken('library_' . $library->id), 'restrict' => $restricted ? 0 : 1, 'library_id' => $library->id)))->out(false);
        } else {
            $upgradeUrl = null;
            $restricted = null;
            $restricted_url = null;
        }
        $settings['libraryList']['listData'][] = array('title' => $library->title . ' (' . \H5PCore::libraryVersion($library) . ')', 'restricted' => $restricted, 'restrictedUrl' => $restricted_url, 'numContent' => $core->h5pF->getNumContent($library->id), 'numContentDependencies' => $usage['content'] === -1 ? '' : $usage['content'], 'numLibraryDependencies' => $usage['libraries'], 'upgradeUrl' => $upgradeUrl, 'detailsUrl' => null, 'deleteUrl' => null);
        $i++;
    }
}
// All translations are made server side
$settings['libraryList']['listHeaders'] = array(get_string('librarylisttitle', 'hvp'), get_string('librarylistrestricted', 'hvp'), get_string('librarylistinstances', 'hvp'), get_string('librarylistinstancedependencies', 'hvp'), get_string('librarylistlibrarydependencies', 'hvp'), get_string('librarylistactions', 'hvp'));
// Add js
$lib_url = $CFG->httpswwwroot . '/mod/hvp/library/';
hvp_admin_add_generic_css_and_js($PAGE, $lib_url, $settings);
$PAGE->requires->js(new moodle_url($lib_url . 'js/h5p-library-list.js' . hvp_get_cache_buster()), true);
Example #3
0
/**
 * Handle content upgrade progress
 *
 * @method hvp_content_upgrade_progress
 * @param  int $library_id
 * @return object An object including the json content for the H5P instances
 *                (maximum 40) that should be upgraded.
 */
function hvp_content_upgrade_progress($libraryid)
{
    global $DB;
    $tolibraryid = filter_input(INPUT_POST, 'libraryId');
    // Verify security token.
    if (!\H5PCore::validToken('contentupgrade', required_param('token', PARAM_RAW))) {
        print get_string('upgradeinvalidtoken', 'hvp');
        return;
    }
    // Get the library we're upgrading to.
    $tolibrary = $DB->get_record('hvp_libraries', array('id' => $tolibraryid));
    if (!$tolibrary) {
        print get_string('upgradelibrarymissing', 'hvp');
        return;
    }
    // Prepare response.
    $out = new stdClass();
    $out->params = array();
    $out->token = \H5PCore::createToken('contentupgrade');
    // Prepare our interface.
    $interface = \mod_hvp\framework::instance('interface');
    // Get updated params.
    $params = filter_input(INPUT_POST, 'params');
    if ($params !== null) {
        // Update params.
        $params = json_decode($params);
        foreach ($params as $id => $param) {
            $DB->update_record('hvp', (object) array('id' => $id, 'main_library_id' => $tolibrary->id, 'json_content' => $param, 'filtered' => ''));
            // Log content upgrade successful
            new \mod_hvp\event('content', 'upgrade', $id, $DB->get_field_sql("SELECT name FROM {hvp} WHERE id = ?", array($id)), $tolibrary->machine_name, $tolibrary->major_version . '.' . $tolibrary->minor_version);
        }
    }
    // Get number of contents for this library.
    $out->left = $interface->getNumContent($libraryid);
    if ($out->left) {
        // Find the 40 first contents using this library version and add to params.
        $results = $DB->get_records_sql("SELECT id, json_content as params\n               FROM {hvp}\n              WHERE main_library_id = ?\n           ORDER BY name ASC", array($libraryid), 0, 40);
        foreach ($results as $content) {
            $out->params[$content->id] = $content->params;
        }
    }
    return $out;
}
                                   FROM {hvp_libraries} hl1 JOIN {hvp_libraries} hl2 ON hl1.machine_name = hl2.machine_name
                                  WHERE hl1.id = ?
                               ORDER BY hl2.title ASC, hl2.major_version ASC, hl2.minor_version ASC', array($libraryid));
$versions = array();
foreach ($results as $result) {
    $versions[$result->id] = $result;
}
$library = $versions[$libraryid];
$upgrades = $core->getUpgrades($library, $versions);
$PAGE->set_heading(get_string('upgradeheading', 'hvp', $library->title . ' (' . \H5PCore::libraryVersion($library) . ')'));
// Get num of contents that can be upgraded.
$numcontents = $core->h5pF->getNumContent($libraryid);
if (count($versions) < 2) {
    echo $OUTPUT->header();
    echo get_string('upgradenoavailableupgrades', 'hvp');
} else {
    if ($numcontents === 0) {
        echo $OUTPUT->header();
        echo get_string('upgradenothingtodo', 'hvp');
    } else {
        $settings = array('libraryInfo' => array('message' => get_string('upgrademessage', 'hvp', $numcontents), 'inProgress' => get_string('upgradeinprogress', 'hvp'), 'error' => get_string('upgradeerror', 'hvp'), 'errorData' => get_string('upgradeerrordata', 'hvp'), 'errorScript' => get_string('upgradeerrorscript', 'hvp'), 'errorContent' => get_string('upgradeerrorcontent', 'hvp'), 'errorParamsBroken' => get_string('upgradeerrorparamsbroken', 'hvp'), 'done' => get_string('upgradedone', 'hvp', $numcontents) . ' <a href="' . (new moodle_url('/mod/hvp/library_list.php'))->out(false) . '">' . get_string('upgradereturn', 'hvp') . '</a>', 'library' => array('name' => $library->name, 'version' => $library->major_version . '.' . $library->minor_version), 'libraryBaseUrl' => (new moodle_url('/mod/hvp/ajax.php', array('action' => 'getlibrarydataforupgrade')))->out(false) . '&library=', 'scriptBaseUrl' => (new moodle_url('/mod/hvp/library/js'))->out(false), 'buster' => hvp_get_cache_buster(), 'versions' => $upgrades, 'contents' => $numcontents, 'buttonLabel' => get_string('upgradebuttonlabel', 'hvp'), 'infoUrl' => (new moodle_url('/mod/hvp/ajax.php', array('action' => 'libraryupgradeprogress', 'library_id' => $libraryid)))->out(false), 'total' => $numcontents, 'token' => \H5PCore::createToken('contentupgrade')));
        // Add JavaScripts.
        $liburl = $CFG->httpswwwroot . '/mod/hvp/library/';
        hvp_admin_add_generic_css_and_js($PAGE, $liburl, $settings);
        $PAGE->requires->js(new moodle_url($liburl . 'js/h5p-version.js' . hvp_get_cache_buster()), true);
        $PAGE->requires->js(new moodle_url($liburl . 'js/h5p-content-upgrade.js' . hvp_get_cache_buster()), true);
        echo $OUTPUT->header();
        echo '<div id="h5p-admin-container">' . get_string('enablejavascript', 'hvp') . '</div>';
    }
}
echo $OUTPUT->footer();
Example #5
0
    }
    // Schedule stylesheets for loading through Moodle.
    foreach ($files['styles'] as $style) {
        $url = $hvppath . $style->path . $style->version;
        $settings['loadedCss'][] = $url;
        $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);
// H5P JS xAPI event listener & Moodle event log dispatcher.
$jsparams = array('hvpid' => $id, 'courseid' => $course->id, 'debug' => $CFG->debug, 'token' => \H5PCore::createToken('logxapievent'));
$PAGE->requires->js_call_amd('mod_hvp/xapi-stmt-dispatcher', 'init', array($jsparams));
// 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