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