/**
  * Retrieves ajax parameters for content and update or delete
  * user data depending on params.
  *
  * @throws \coding_exception
  */
 public static function handle_ajax()
 {
     global $DB;
     // Query String Parameters.
     $content_id = required_param('content_id', PARAM_INT);
     $data_id = required_param('data_type', PARAM_RAW);
     $sub_content_id = required_param('sub_content_id', PARAM_INT);
     // Form Data.
     $data = optional_param('data', null, PARAM_RAW);
     $pre_load = optional_param('preload', null, PARAM_INT);
     $invalidate = optional_param('invalidate', null, PARAM_INT);
     if ($content_id === null || $data_id === null || $sub_content_id === null) {
         \H5PCore::ajaxError(get_string('missingparameters', 'hvp'));
         exit;
         // Missing parameters.
     }
     // Saving data
     if ($data !== NULL && $pre_load !== NULL && $invalidate !== NULL) {
         // Validate token
         if (!\H5PCore::validToken('contentuserdata', required_param('token', PARAM_RAW))) {
             \H5PCore::ajaxError(get_string('invalidtoken', 'hvp'));
             exit;
         }
         // Use context id if supplied
         $context_id = optional_param('contextId', null, PARAM_INT);
         if ($context_id) {
             $context = \context::instance_by_id($context_id);
         } else {
             // Otherwise try to find it from content id
             $context = \context_course::instance($DB->get_field('hvp', 'course', array('id' => $content_id)));
         }
         // Check permissions
         if (!has_capability('mod/hvp:savecontentuserdata', $context)) {
             \H5PCore::ajaxError(get_string('nopermissiontosavecontentuserdata', 'hvp'));
             http_response_code(403);
             exit;
         }
         if ($data === '0') {
             // Delete user data.
             self::delete_user_data($content_id, $sub_content_id, $data_id);
         } else {
             // Save user data.
             self::save_user_data($content_id, $sub_content_id, $data_id, $pre_load, $invalidate, $data);
         }
         \H5PCore::ajaxSuccess();
     } else {
         // Fetch user data
         $user_data = self::get_user_data($content_id, $sub_content_id, $data_id);
         if ($user_data === false) {
             // Did not find data, return nothing
             \H5PCore::ajaxSuccess();
         } else {
             // Found data, return encoded data
             \H5PCore::ajaxSuccess($user_data->data);
         }
     }
     exit;
 }
Ejemplo n.º 2
0
 public static function handle_ajax()
 {
     global $DB, $USER;
     if (!\H5PCore::validToken('result', required_param('token', PARAM_RAW))) {
         \H5PCore::ajaxError(get_string('invalidtoken', 'hvp'));
         exit;
     }
     // Content parameters
     $content_id = required_param('contentId', PARAM_INT);
     $score = required_param('score', PARAM_INT);
     $max_score = required_param('maxScore', PARAM_INT);
     // Time values not usable by gradebook
     // $opened = required_param('opened', PARAM_INT);
     // $finished = required_param('finished', PARAM_INT);
     // Get hvp data from contentId
     $hvp = $DB->get_record('hvp', array('id' => $content_id));
     // Check permissions
     $context = \context_course::instance($hvp->course);
     if (!has_capability('mod/hvp:saveresults', $context)) {
         \H5PCore::ajaxError(get_string('nopermissiontosaveresult', 'hvp'));
         http_response_code(403);
         exit;
     }
     // Create grade object and set grades
     $grade = (object) array('userid' => $USER->id);
     // Get course module id from db, required for grade item
     $cm_id_sql = "SELECT cm.id, h.name\n            FROM {course_modules} cm, {hvp} h, {modules} m\n            WHERE cm.instance = h.id AND h.id = ? AND m.name = 'hvp' AND m.id = cm.module";
     $result = $DB->get_record_sql($cm_id_sql, array($content_id));
     // Set grade using Gradebook API
     $hvp->cmidnumber = $result->id;
     $hvp->name = $result->name;
     $hvp->rawgrade = $score;
     $hvp->rawgrademax = $max_score;
     hvp_grade_item_update($hvp, $grade);
     // Get content info for log
     $content = $DB->get_record_sql("SELECT c.name AS title, l.machine_name AS name, l.major_version, l.minor_version\n                   FROM {hvp} c\n                   JOIN {hvp_libraries} l ON l.id = c.main_library_id\n                  WHERE c.id = ?", array($content_id));
     // Log view
     new \mod_hvp\event('results', 'set', $content_id, $content->title, $content->name, $content->major_version . '.' . $content->minor_version);
     \H5PCore::ajaxSuccess();
     exit;
 }
Ejemplo n.º 3
0
      *
      * Parameters:
      *  int contentId
      *  int contextId
      */
 /*
  * Handle file upload through the editor.
  *
  * Parameters:
  *  int contentId
  *  int contextId
  */
 case 'files':
     global $DB;
     // TODO: Check permissions
     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');
Ejemplo n.º 4
0
     }
     // 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;
 case 'logxapievent':
     global $CFG;
     // Trigger a Moodle log event for each xAPI statement
     // that is dispatched by the H5P (hvp) object.
     if (!\H5PCore::validToken('logxapievent', required_param('token', PARAM_RAW))) {
         \H5PCore::ajaxError(get_string('invalidtoken', 'hvp'));
         exit;
     }
     $hvpid = optional_param('hvpid', null, PARAM_INT);
     $courseid = optional_param('courseid', null, PARAM_INT);
     $jsonxapistatement = optional_param('xapistatement', null, PARAM_RAW);
     $xapistatement = json_decode($jsonxapistatement, true);
     $context = \context_module::instance($hvpid);
     $event = \mod_hvp\event\hvp_xapi::create(array('objectid' => $hvpid, 'context' => $context, 'other' => array('statement' => $xapistatement['data']['statement']), 'courseid' => $courseid));
     $event->trigger();
     // Debugging...
     if (!empty($CFG->debug) and $CFG->debug >= DEBUG_DEVELOPER) {
         $msg = "xAPI '" . $xapistatement['data']['statement']['verb']['display']['en-US'] . "' statement dispatched";
         \H5PCore::ajaxSuccess($msg);
         http_response_code(200);
Ejemplo n.º 5
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;
}