Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
 * View all results for H5P Content
18
 *
19
 * @package    mod_hvp
20
 * @copyright  2016 Joubel AS <contact@joubel.com>
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
 
24
require_once(dirname(__FILE__) . '/../../config.php');
25
require_once("locallib.php");
26
global $USER, $PAGE, $DB, $CFG, $OUTPUT, $COURSE;
27
 
28
$id     = required_param('id', PARAM_INT);
29
$userid = optional_param('user', (int) $USER->id, PARAM_INT);
30
 
31
if (!$cm = get_coursemodule_from_instance('hvp', $id)) {
32
    print_error('invalidcoursemodule');
33
}
34
if (!$course = $DB->get_record('course', ['id' => $cm->course])) {
35
    print_error('coursemisconf');
36
}
37
require_login($course, false, $cm);
38
 
39
// Check permission.
40
$context = \context_module::instance($cm->id);
41
hvp_require_view_results_permission($userid, $context, $cm->id);
42
 
43
// Load H5P Content.
44
$hvp = $DB->get_record_sql(
45
    "SELECT h.id,
46
                h.name AS title,
47
                hl.machine_name,
48
                hl.major_version,
49
                hl.minor_version
50
           FROM {hvp} h
51
           JOIN {hvp_libraries} hl ON hl.id = h.main_library_id
52
          WHERE h.id = ?",
53
    [$id]);
54
 
55
if ($hvp === false) {
56
    print_error('invalidhvp', 'mod_hvp');
57
}
58
 
59
// Set page properties.
60
$pageurl = new moodle_url('/mod/hvp/review.php', [
61
    'id' => $hvp->id,
62
]);
63
$basepath = \mod_hvp\view_assets::getsiteroot();
64
$PAGE->set_url($pageurl);
65
$PAGE->set_title($hvp->title);
66
$PAGE->set_heading($COURSE->fullname);
67
$PAGE->requires->css(new moodle_url($basepath . '/mod/hvp/xapi-custom-report.css'));
68
 
69
// We have to get grades from gradebook as well.
70
$xapiresults = $DB->get_records_sql("
71
    SELECT x.*, i.grademax
72
    FROM {hvp_xapi_results} x
73
    JOIN {grade_items} i ON i.iteminstance = x.content_id
74
    WHERE x.user_id = ?
75
    AND x.content_id = ?
76
    AND i.itemtype = 'mod'
77
    AND i.itemmodule = 'hvp'", [$userid, $id]
78
);
79
 
80
if (!$xapiresults) {
81
    echo $OUTPUT->header();
82
    echo "<h2>" . get_string('noanswersubmitted', 'hvp') . "</h2>";
83
    echo $OUTPUT->footer();
84
    return;
85
}
86
 
87
$totalrawscore       = null;
88
$totalmaxscore       = null;
89
$totalscaledscore    = null;
90
$scaledscoreperscore = null;
91
 
92
// Assemble our question tree.
93
$basequestion = null;
94
 
95
// Find base question.
96
foreach ($xapiresults as $question) {
97
    if ($question->parent_id === null) {
98
        // This is the root of our tree.
99
        $basequestion = $question;
100
 
101
        if (isset($question->raw_score) && isset($question->grademax) && isset($question->max_score)) {
102
            $scaledscoreperscore   = $question->max_score ? ($question->grademax / $question->max_score) : 0;
103
            $question->score_scale = round($scaledscoreperscore, 2);
104
            $totalrawscore         = $question->raw_score;
105
            $totalmaxscore         = $question->max_score;
106
            if ($question->max_score && $question->raw_score === $question->max_score) {
107
                $totalscaledscore = round($question->grademax, 2);
108
            } else {
109
                $totalscaledscore = round($question->score_scale * $question->raw_score, 2);
110
            }
111
        }
112
        break;
113
    }
114
}
115
 
116
foreach ($xapiresults as $question) {
117
    if ($question->parent_id === null) {
118
        // Already processed.
119
        continue;
120
    } else if (isset($xapiresults[$question->parent_id])) {
121
        // Add to parent.
122
        $xapiresults[$question->parent_id]->children[] = $question;
123
    }
124
 
125
    // Set scores.
126
    if (!isset($question->raw_score)) {
127
        $question->raw_score = 0;
128
    }
129
    if (isset($question->raw_score) && isset($question->grademax) && isset($question->max_score)) {
130
        $question->scaled_score_per_score = $scaledscoreperscore;
131
        $question->parent_max_score = $totalmaxscore;
132
        $question->score_scale = round($question->raw_score * $scaledscoreperscore, 2);
133
    }
134
 
135
    // Set score labels.
136
    $question->score_label            = get_string('reportingscorelabel', 'hvp');
137
    $question->scaled_score_label     = get_string('reportingscaledscorelabel', 'hvp');
138
    $question->score_delimiter        = get_string('reportingscoredelimiter', 'hvp');
139
    $question->scaled_score_delimiter = get_string('reportingscaledscoredelimiter', 'hvp');
140
    $question->questions_remaining_label = get_string('reportingquestionsremaininglabel', 'hvp');
141
}
142
 
143
// Initialize reporter.
144
$reporter   = H5PReport::getInstance();
145
$reporthtml = $reporter->generateReport($basequestion, null, count($xapiresults) <= 1);
146
$styles     = $reporter->getStylesUsed();
147
$scripts    = $reporter->getScriptsUsed();
148
foreach ($styles as $style) {
149
    $PAGE->requires->css(new moodle_url($basepath . '/mod/hvp/reporting/' . $style));
150
}
151
foreach ($scripts as $script) {
152
    $PAGE->requires->js(new moodle_url($basepath . '/mod/hvp/reporting/' . $script));
153
}
154
 
155
$PAGE->requires->js(new moodle_url($basepath . '/mod/hvp/library/js/jquery.js'), true);
156
 
157
// Send the enpoints necessary for dynamic grading to the view.
158
$setsubcontentendpoint = "{$basepath}/mod/hvp/ajax.php?contextId={$context->instanceid}&token=" .
159
    \H5PCore::createToken('result') . '&action=updatesubcontentscore';
160
$getsubcontentendpoint = "{$basepath}/mod/hvp/ajax.php?contextId={$context->instanceid}&token=" .
161
    \H5PCore::createToken('result') . '&action=getsubcontentscore';
162
$datatosend = array(
163
  'setSubContentEndpoint' => $setsubcontentendpoint,
164
  'getSubContentEndpoint' => $getsubcontentendpoint,
165
);
166
$PAGE->requires->data_for_js('data_for_page', $datatosend, true);
167
 
168
$renderer = $PAGE->get_renderer('mod_hvp');
169
 
170
// Print title and report.
171
$title = $hvp->title;
172
 
173
// Show user name if other then self.
174
if ($userid !== (int) $USER->id) {
175
    $userresult = $DB->get_record('user', ["id" => $userid], 'username');
176
    if (isset($userresult) && isset($userresult->username)) {
177
        $title .= ": {$userresult->username}";
178
    }
179
}
180
 
181
// Create title.
182
$reviewcontext = [
183
    'title'          => $title,
184
    'report'         => $reporthtml,
185
    'rawScore'       => $totalrawscore,
186
    'maxScore'       => $totalmaxscore,
187
    'scaledScore'    => round($totalscaledscore, 2),
188
    'maxScaledScore' => round($basequestion->grademax, 2),
189
];
190
 
191
// Print page HTML.
192
echo $OUTPUT->header();
193
echo $renderer->render_from_template('hvp/review', $reviewcontext);
194
echo $OUTPUT->footer();