Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 81
n/a
0 / 0
CRAP
n/a
0 / 0
1<?php
2// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
16
17/**
18 * Prints an instance of mod_driprelease.
19 *
20 * @package     tool_driprelease
21 * @copyright   2022 Marcus Green
22 * @license     https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25require(__DIR__.'/../../../config.php');
26require_once(__DIR__.'/lib.php');
27
28use tool_driprelease\event\driprelease_updated;
29use tool_driprelease\event\driprelease_viewed;
30
31// Course module id.
32$courseid = optional_param('courseid', 0, PARAM_INT);
33$modtype = optional_param('modtype', 'quiz', PARAM_RAW);
34
35if (!$courseid) {
36    redirect(new moodle_url('/'));
37}
38$context = context_course::instance($courseid);
39
40if (!has_capability('moodle/course:update', $context)) {
41    redirect(new moodle_url('/course/view.php', ['id' => $courseid]));
42}
43$cancel = optional_param('cancel', null, PARAM_TEXT);
44if (isset($cancel)) {
45    redirect (new moodle_url('/course/view.php', ['id' => $courseid]));
46}
47require_login($courseid);
48
49require_once($CFG->dirroot . '/admin/tool/driprelease/driprelease_form.php');
50
51$PAGE->set_context($context);
52
53$PAGE->set_url('/admin/tool/driprelease/view.php', ['courseid' => $courseid]);
54
55if (!$course = $DB->get_record('course', ['id' => $courseid])) {
56    throw new moodle_exception('invalid course id');
57}
58
59$PAGE->set_course($course);
60global $DB, $USER;
61
62$driprelease = $DB->get_record('tool_driprelease' , ['courseid' => $courseid], '*', IGNORE_MISSING);
63if (!$driprelease) {
64    $config = get_config('tool_driprelease');
65    $driprelease = (object)[
66        'courseid' => $courseid,
67        'modtype' => $modtype,
68        'activitiespersession' => $config->activitiespersession ?? 0,
69        'schedulestart' => time(),
70        'coursegroup' => '',
71        'sessionlength' => $config->sessionlength ?? 0,
72    ];
73} else {
74    $driprelease->modtype = $modtype;
75}
76
77if (!$driprelease) {
78    $driprelease = (object) ['courseid' => $courseid];
79}
80
81$mform = new tool_driprelease_form(null, ['driprelease' => $driprelease]);
82
83navigation_node::override_active_url(new moodle_url('admin/tool/driprelease/view.php', ['courseid' => $courseid]));
84$eventdata = [
85    'context' => context_course::instance($courseid),
86    'other' => [
87        'username' => $USER->username,
88        'course' => $course->shortname,
89    ],
90];
91
92if ($fromform = $mform->get_data()) {
93
94    if (isset($fromform->submitbutton) || isset($fromform->submitbutton2) || isset($fromform->refresh)) {
95        $driprelease->schedulestart = $fromform->schedulestart;
96        $driprelease->stayavailable = $fromform->stayavailable;
97        $driprelease->hideunselected = $fromform->hideunselected;
98        $driprelease->coursegroup = $fromform->coursegroup;
99        xdebug_break();
100        $driprelease->moduletype = $fromform->modtype;
101        $driprelease->refresh = true;
102        list($selections, $driprelease) = driprelease_update($fromform, $courseid);
103        if (count($selections) == 0 && !isset($fromform->refresh)) {
104            $msg = get_string('noselections', 'tool_driprelease');
105            \core\notification::add($msg, \core\notification::WARNING);
106        }
107
108        $tabledata = get_table_data($driprelease);
109        update_availability($tabledata, $driprelease);
110
111        $event = driprelease_updated::create($eventdata);
112        $event->trigger();
113    }
114    if (isset($fromform->submitbutton2)) {
115        redirect (new moodle_url('/course/view.php', ['id' => $courseid]));
116    }
117}
118
119$tabledata = get_table_data($driprelease);
120
121$templates = [];
122$iterator = new DirectoryIterator(__DIR__ . '/templates');
123foreach ($iterator as $item) {
124    if ($item->isDot()) {
125        continue;
126    }
127    $templates[] = strtok($item->getFilename(), ".");
128}
129
130$templatefile = $modtype;
131if (!in_array($modtype, $templates)) {
132    $templatefile = 'genericmod';
133}
134
135$out = $OUTPUT->render_from_template('tool_driprelease/'.$templatefile,
136     ['tabledata' => $tabledata, 'modtype' => get_string("pluginname", $modtype)]);
137
138$mform->set_data($driprelease);
139
140$event = driprelease_viewed::create($eventdata);
141$event->trigger();
142
143echo $OUTPUT->header();
144$mform->display();
145echo $out;
146echo $OUTPUT->footer();
147