MDL-81717 h5p: Improve robustness content type fetching
[moodle.git] / mod / h5pactivity / report.php
blob65e5b60ec5a4f0285fac2d56363a32edfd25893c
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.
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/>.
17 /**
18 * Prints an instance of mod_h5pactivity.
20 * @package mod_h5pactivity
21 * @copyright 2020 Ferran Recio <ferran@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 use mod_h5pactivity\local\manager;
26 use mod_h5pactivity\event\report_viewed;
28 require(__DIR__.'/../../config.php');
29 require_once(__DIR__.'/lib.php');
31 $userid = optional_param('userid', null, PARAM_INT);
32 $attemptid = optional_param('attemptid', null, PARAM_INT);
34 // Attempts have only the instance id information but system events
35 // have only cmid. To prevent unnecesary db queries, this page accept both.
36 $id = optional_param('id', null, PARAM_INT);
37 if (empty($id)) {
38 $a = required_param('a', PARAM_INT);
39 list ($course, $cm) = get_course_and_cm_from_instance($a, 'h5pactivity');
40 } else {
41 list ($course, $cm) = get_course_and_cm_from_cmid($id, 'h5pactivity');
44 require_login($course, true, $cm);
46 $currentgroup = groups_get_activity_group($cm, true);
48 $manager = manager::create_from_coursemodule($cm);
50 $report = $manager->get_report($userid, $attemptid, $currentgroup);
51 if (!$report) {
52 throw new \moodle_exception('permissiondenied');
55 $user = $report->get_user();
56 $attempt = $report->get_attempt();
58 $moduleinstance = $manager->get_instance();
60 $context = $manager->get_context();
62 $params = ['a' => $cm->instance];
63 if ($user) {
64 $params['userid'] = $user->id;
66 if ($attempt) {
67 $params['attemptid'] = $attempt->get_id();
69 $PAGE->set_url('/mod/h5pactivity/report.php', $params);
71 // Trigger event.
72 $other = [
73 'instanceid' => $params['a'],
74 'userid' => $params['userid'] ?? null,
75 'attemptid' => $params['attemptid'] ?? null,
77 $event = report_viewed::create([
78 'objectid' => $moduleinstance->id,
79 'context' => $context,
80 'other' => $other,
81 ]);
82 $event->add_record_snapshot('course', $course);
83 $event->add_record_snapshot('h5pactivity', $moduleinstance);
84 $event->trigger();
86 $shortname = format_string($course->shortname, true, ['context' => $context]);
87 $pagetitle = strip_tags($shortname.': '.format_string($moduleinstance->name));
88 $PAGE->set_title(format_string($pagetitle));
89 $PAGE->activityheader->disable();
91 $navbar = [];
92 if ($manager->can_view_all_attempts()) {
93 // Report navbar have 3 levels for teachers:
94 // - Participants list
95 // - Participant attempts list
96 // - Individual attempt details.
97 $nav = [get_string('attempts', 'mod_h5pactivity'), null];
98 if ($user) {
99 $nav[1] = new moodle_url('/mod/h5pactivity/report.php', ['a' => $cm->instance]);
100 $navbar[] = $nav;
102 $nav = [fullname($user), null];
103 if ($attempt) {
104 $nav[1] = new moodle_url('/mod/h5pactivity/report.php', ['a' => $cm->instance, 'userid' => $user->id]);
107 $navbar[] = $nav;
108 } else {
109 // Report navbar have 2 levels for a regular participant:
110 // - My attempts
111 // - Individual attempt details.
112 $nav = [get_string('myattempts', 'mod_h5pactivity'), null];
113 if ($attempt) {
114 $nav[1] = new moodle_url('/mod/h5pactivity/report.php', ['a' => $cm->instance]);
116 $navbar[] = $nav;
119 if ($attempt) {
120 $navbar[] = [get_string('attempt_number', 'mod_h5pactivity', $attempt->get_attempt()), null];
122 foreach ($navbar as $nav) {
123 $PAGE->navbar->add($nav[0], $nav[1]);
126 $PAGE->set_heading(format_string($course->fullname));
127 $PAGE->set_context($context);
129 echo $OUTPUT->header();
131 groups_print_activity_menu($cm, $PAGE->url);
133 echo html_writer::start_div('mt-4');
134 echo $report->print();
135 echo html_writer::end_div();
137 echo $OUTPUT->footer();