1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* The mod_hvp user grades
|
|
|
19 |
*
|
|
|
20 |
* @package mod_hvp
|
|
|
21 |
* @copyright 2016 Joubel AS
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
namespace mod_hvp;
|
|
|
25 |
|
|
|
26 |
defined('MOODLE_INTERNAL') || die();
|
|
|
27 |
|
|
|
28 |
require(__DIR__ . '/../lib.php');
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* Handles grade storage for users
|
|
|
32 |
* @package mod_hvp
|
|
|
33 |
*/
|
|
|
34 |
class user_grades {
|
|
|
35 |
|
|
|
36 |
public static function handle_ajax() {
|
|
|
37 |
global $DB, $USER, $CFG;
|
|
|
38 |
|
|
|
39 |
if (!\H5PCore::validToken('result', required_param('token', PARAM_RAW))) {
|
|
|
40 |
\H5PCore::ajaxError(get_string('invalidtoken', 'hvp'));
|
|
|
41 |
return;
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
$cm = get_coursemodule_from_id('hvp', required_param('contextId', PARAM_INT));
|
|
|
45 |
if (!$cm) {
|
|
|
46 |
\H5PCore::ajaxError('No such content');
|
|
|
47 |
http_response_code(404);
|
|
|
48 |
return;
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
// Content parameters.
|
|
|
52 |
$score = required_param('score', PARAM_INT);
|
|
|
53 |
$maxscore = required_param('maxScore', PARAM_INT);
|
|
|
54 |
|
|
|
55 |
// Check permission.
|
|
|
56 |
$context = \context_module::instance($cm->id);
|
|
|
57 |
if (!has_capability('mod/hvp:saveresults', $context)) {
|
|
|
58 |
\H5PCore::ajaxError(get_string('nopermissiontosaveresult', 'hvp'));
|
|
|
59 |
http_response_code(403);
|
|
|
60 |
return;
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
// Get hvp data from content.
|
|
|
64 |
$hvp = $DB->get_record('hvp', array('id' => $cm->instance));
|
|
|
65 |
if (!$hvp) {
|
|
|
66 |
\H5PCore::ajaxError('No such content');
|
|
|
67 |
http_response_code(404);
|
|
|
68 |
return;
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
// Create grade object and set grades.
|
|
|
72 |
$grade = (object) array(
|
|
|
73 |
'userid' => $USER->id
|
|
|
74 |
);
|
|
|
75 |
|
|
|
76 |
// Set grade using Gradebook API.
|
|
|
77 |
$hvp->cmidnumber = $cm->idnumber;
|
|
|
78 |
$hvp->name = $cm->name;
|
|
|
79 |
$hvp->rawgrade = $score;
|
|
|
80 |
$hvp->rawgrademax = $maxscore;
|
|
|
81 |
hvp_grade_item_update($hvp, $grade);
|
|
|
82 |
|
|
|
83 |
// Get content info for log.
|
|
|
84 |
$content = $DB->get_record_sql(
|
|
|
85 |
"SELECT c.name AS title, l.machine_name AS name, l.major_version, l.minor_version
|
|
|
86 |
FROM {hvp} c
|
|
|
87 |
JOIN {hvp_libraries} l ON l.id = c.main_library_id
|
|
|
88 |
WHERE c.id = ?",
|
|
|
89 |
array($hvp->id)
|
|
|
90 |
);
|
|
|
91 |
|
|
|
92 |
require_once($CFG->libdir.'/completionlib.php');
|
|
|
93 |
|
|
|
94 |
// Load Course.
|
|
|
95 |
$course = $DB->get_record('course', array('id' => $cm->course));
|
|
|
96 |
$completion = new \completion_info( $course );
|
|
|
97 |
|
|
|
98 |
if ( $completion->is_enabled( $cm) ) {
|
|
|
99 |
$completion->update_state($cm, COMPLETION_COMPLETE);
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
// Log results set event.
|
|
|
103 |
new \mod_hvp\event(
|
|
|
104 |
'results', 'set',
|
|
|
105 |
$hvp->id, $content->title,
|
|
|
106 |
$content->name, $content->major_version . '.' . $content->minor_version
|
|
|
107 |
);
|
|
|
108 |
|
|
|
109 |
// Trigger Moodle event for async notification messages.
|
|
|
110 |
$event = \mod_hvp\event\attempt_submitted::create([
|
|
|
111 |
'context' => $context,
|
|
|
112 |
]);
|
|
|
113 |
$event->trigger();
|
|
|
114 |
|
|
|
115 |
\H5PCore::ajaxSuccess();
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
/**
|
|
|
119 |
* Since the subcontent types do not have their own row in the table,
|
|
|
120 |
* we use the hvp_results_table as a 'staging area' to set and get
|
|
|
121 |
* dynamically graded scores.
|
|
|
122 |
*/
|
|
|
123 |
public static function handle_dynamic_grading() {
|
|
|
124 |
global $DB;
|
|
|
125 |
|
|
|
126 |
if (!\H5PCore::validToken('result', required_param('token', PARAM_RAW))) {
|
|
|
127 |
\H5PCore::ajaxError(get_string('invalidtoken', 'hvp'));
|
|
|
128 |
return;
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
$cm = get_coursemodule_from_id('hvp', required_param('contextId', PARAM_INT));
|
|
|
132 |
if (!$cm) {
|
|
|
133 |
\H5PCore::ajaxError('No such content');
|
|
|
134 |
http_response_code(404);
|
|
|
135 |
return;
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
// Content parameters.
|
|
|
139 |
$subcontentid = required_param('subcontent_id', PARAM_INT);
|
|
|
140 |
$score = required_param('score', PARAM_INT);
|
|
|
141 |
|
|
|
142 |
// Update the answer's score.
|
|
|
143 |
$data = (object) [
|
|
|
144 |
'id' => $subcontentid,
|
|
|
145 |
'raw_score' => $score
|
|
|
146 |
];
|
|
|
147 |
$DB->update_record('hvp_xapi_results', $data, false);
|
|
|
148 |
|
|
|
149 |
// Load freshly updated record.
|
|
|
150 |
$answer = $DB->get_record('hvp_xapi_results', array('id' => $subcontentid));
|
|
|
151 |
|
|
|
152 |
// Get the sum of all the OEQ scores with the same parent.
|
|
|
153 |
$totalgradablesscore = intval($DB->get_field_sql(
|
|
|
154 |
"SELECT SUM(raw_score)
|
|
|
155 |
FROM {hvp_xapi_results}
|
|
|
156 |
WHERE parent_id = ?
|
|
|
157 |
AND additionals = ?", array($answer->parent_id,
|
|
|
158 |
'{"extensions":{"https:\/\/h5p.org\/x-api\/h5p-machine-name":"H5P.FreeTextQuestion"}}')
|
|
|
159 |
));
|
|
|
160 |
|
|
|
161 |
// Get the original raw score from the main content type.
|
|
|
162 |
$baseanswer = $DB->get_record('hvp_xapi_results', array(
|
|
|
163 |
'id' => $answer->parent_id
|
|
|
164 |
));
|
|
|
165 |
|
|
|
166 |
// Get hvp data from content.
|
|
|
167 |
$hvp = $DB->get_record('hvp', array('id' => $cm->instance));
|
|
|
168 |
if (!$hvp) {
|
|
|
169 |
\H5PCore::ajaxError('No such content');
|
|
|
170 |
http_response_code(404);
|
|
|
171 |
return;
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
// Set grade using Gradebook API.
|
|
|
175 |
$hvp->rawgrade = $baseanswer->raw_score + $totalgradablesscore;
|
|
|
176 |
$hvp->rawgrademax = $baseanswer->max_score;
|
|
|
177 |
hvp_grade_item_update($hvp, (object) array(
|
|
|
178 |
'userid' => $answer->user_id
|
|
|
179 |
));
|
|
|
180 |
|
|
|
181 |
// Get the num of ungraded OEQ answers.
|
|
|
182 |
$numungraded = intval($DB->get_field_sql(
|
|
|
183 |
"SELECT COUNT(*)
|
|
|
184 |
FROM {hvp_xapi_results}
|
|
|
185 |
WHERE parent_id = ?
|
|
|
186 |
AND raw_score IS NULL
|
|
|
187 |
AND additionals = ?", array($answer->parent_id,
|
|
|
188 |
'{"extensions":{"https:\/\/h5p.org\/x-api\/h5p-machine-name":"H5P.FreeTextQuestion"}}')
|
|
|
189 |
));
|
|
|
190 |
|
|
|
191 |
$response = [
|
|
|
192 |
'score' => $answer->raw_score,
|
|
|
193 |
'maxScore' => $answer->max_score,
|
|
|
194 |
'totalUngraded' => $numungraded,
|
|
|
195 |
];
|
|
|
196 |
\H5PCore::ajaxSuccess($response);
|
|
|
197 |
}
|
|
|
198 |
|
|
|
199 |
/**
|
|
|
200 |
* Fetch score/maxScore for ungraded item + number of ungraded items.
|
|
|
201 |
*/
|
|
|
202 |
public static function return_subcontent_grade() {
|
|
|
203 |
global $DB;
|
|
|
204 |
|
|
|
205 |
// Content parameters.
|
|
|
206 |
$subcontentid = required_param('subcontent_id', PARAM_INT);
|
|
|
207 |
$answer = $DB->get_record('hvp_xapi_results', array('id' => $subcontentid));
|
|
|
208 |
|
|
|
209 |
// Get the num of ungraded OEQ answers.
|
|
|
210 |
$numungraded = intval($DB->get_field_sql(
|
|
|
211 |
"SELECT COUNT(*)
|
|
|
212 |
FROM {hvp_xapi_results}
|
|
|
213 |
WHERE parent_id = ?
|
|
|
214 |
AND raw_score IS NULL
|
|
|
215 |
AND additionals = ?", array($answer->parent_id,
|
|
|
216 |
'{"extensions":{"https:\/\/h5p.org\/x-api\/h5p-machine-name":"H5P.FreeTextQuestion"}}')
|
|
|
217 |
));
|
|
|
218 |
|
|
|
219 |
$response = [
|
|
|
220 |
'score' => $answer->raw_score,
|
|
|
221 |
'maxScore' => $answer->max_score,
|
|
|
222 |
'totalUngraded' => $numungraded,
|
|
|
223 |
];
|
|
|
224 |
\H5PCore::ajaxSuccess($response);
|
|
|
225 |
}
|
|
|
226 |
}
|