moodle-mod_hvp/classes/user_grades.php

101 lines
3.1 KiB
PHP

<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* The mod_hvp user grades
*
* @package mod_hvp
* @copyright 2016 Joubel AS
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_hvp;
defined('MOODLE_INTERNAL') || die();
require(__DIR__ . '/../lib.php');
/**
* Handles grade storage for users
* @package mod_hvp
*/
class user_grades {
public static function handle_ajax() {
global $DB, $USER;
if (!\H5PCore::validToken('result', required_param('token', PARAM_RAW))) {
\H5PCore::ajaxError(get_string('invalidtoken', 'hvp'));
return;
}
$cm = get_coursemodule_from_id('hvp', required_param('contextId', PARAM_INT));
if (!$cm) {
\H5PCore::ajaxError('No such content');
http_response_code(404);
return;
}
// Content parameters.
$score = required_param('score', PARAM_INT);
$maxscore = required_param('maxScore', PARAM_INT);
// Check permission.
$context = \context_module::instance($cm->id);
if (!has_capability('mod/hvp:saveresults', $context)) {
\H5PCore::ajaxError(get_string('nopermissiontosaveresult', 'hvp'));
http_response_code(403);
return;
}
// Get hvp data from content.
$hvp = $DB->get_record('hvp', array('id' => $cm->instance));
if (!$hvp) {
\H5PCore::ajaxError('No such content');
http_response_code(404);
return;
}
// Create grade object and set grades.
$grade = (object) array(
'userid' => $USER->id
);
// Set grade using Gradebook API.
$hvp->cmidnumber = $cm->idnumber;
$hvp->name = $cm->name;
$hvp->rawgrade = $score;
$hvp->rawgrademax = $maxscore;
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
FROM {hvp} c
JOIN {hvp_libraries} l ON l.id = c.main_library_id
WHERE c.id = ?",
array($hvp->id)
);
// Log results set event.
new \mod_hvp\event(
'results', 'set',
$hvp->id, $content->title,
$content->name, $content->major_version . '.' . $content->minor_version
);
\H5PCore::ajaxSuccess();
}
}