MDL-53638 mod_feedback: structural changes
[moodle.git] / mod / feedback / view.php
CommitLineData
84973212 1<?php
9c5bc7a5
AG
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/>.
c70ad9f7 16
84973212 17/**
18 * the first page to view the feedback
19 *
20 * @author Andreas Grabs
21 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
9714baa1 22 * @package mod_feedback
84973212 23 */
24require_once("../../config.php");
25require_once("lib.php");
26
27$id = required_param('id', PARAM_INT);
28$courseid = optional_param('courseid', false, PARAM_INT);
29
84973212 30$current_tab = 'view';
31
32if (! $cm = get_coursemodule_from_id('feedback', $id)) {
33 print_error('invalidcoursemodule');
34}
35
36if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
37 print_error('coursemisconf');
38}
39
40if (! $feedback = $DB->get_record("feedback", array("id"=>$cm->instance))) {
41 print_error('invalidcoursemodule');
42}
43
2c979976 44$context = context_module::instance($cm->id);
c987dbad
AG
45
46$feedback_complete_cap = false;
47
9c5bc7a5 48if (has_capability('mod/feedback:complete', $context)) {
c987dbad
AG
49 $feedback_complete_cap = true;
50}
84973212 51
2a94a2f6 52if (!empty($CFG->feedback_allowfullanonymous)
8d8e6694 53 AND $course->id == SITEID
2a94a2f6
MG
54 AND $feedback->anonymous == FEEDBACK_ANONYMOUS_YES
55 AND (!isloggedin() OR isguestuser())) {
56 // Guests are allowed to complete fully anonymous feedback without having 'mod/feedback:complete' capability.
c987dbad 57 $feedback_complete_cap = true;
84973212 58}
59
60//check whether the feedback is located and! started from the mainsite
9c5bc7a5 61if ($course->id == SITEID AND !$courseid) {
84973212 62 $courseid = SITEID;
63}
64
4832a0b3 65require_course_login($course, true, $cm);
84973212 66
2a94a2f6
MG
67if ($course->id == SITEID) {
68 $PAGE->set_context($context);
69 $PAGE->set_cm($cm, $course);
70 $PAGE->set_pagelayout('incourse');
71}
ed724aac 72$PAGE->set_url('/mod/feedback/view.php', array('id' => $cm->id));
2a94a2f6
MG
73$PAGE->set_title($feedback->name);
74$PAGE->set_heading($course->fullname);
75
76// Check whether the feedback is mapped to the given courseid.
77if ($course->id == SITEID AND !has_capability('mod/feedback:edititems', $context)) {
78 if ($DB->get_records('feedback_sitecourse_map', array('feedbackid' => $feedback->id))) {
79 $params = array('feedbackid' => $feedback->id, 'courseid' => $courseid);
80 if (!$DB->get_record('feedback_sitecourse_map', $params)) {
81 if ($courseid == SITEID) {
82 echo $OUTPUT->header();
83 echo $OUTPUT->notification(get_string('cannotaccess', 'mod_feedback'));
84 echo $OUTPUT->footer();
85 exit;
86 } else {
87 print_error('invalidcoursemodule');
88 }
89 }
90 }
91}
92
84973212 93//check whether the given courseid exists
9c5bc7a5
AG
94if ($courseid AND $courseid != SITEID) {
95 if ($course2 = $DB->get_record('course', array('id'=>$courseid))) {
84973212 96 require_course_login($course2); //this overwrites the object $course :-(
97 $course = $DB->get_record("course", array("id"=>$cm->course)); // the workaround
9c5bc7a5 98 } else {
84973212 99 print_error('invalidcourseid');
c70ad9f7 100 }
84973212 101}
102
3f6fd941
AA
103// Trigger module viewed event.
104$event = \mod_feedback\event\course_module_viewed::create(array(
105 'objectid' => $feedback->id,
106 'context' => $context,
bc293202 107 'anonymous' => ($feedback->anonymous == FEEDBACK_ANONYMOUS_YES),
3f6fd941 108 'other' => array(
bc293202 109 'anonymous' => $feedback->anonymous // Deprecated.
fbc4b778
MG
110 )
111));
3f6fd941
AA
112$event->add_record_snapshot('course_modules', $cm);
113$event->add_record_snapshot('course', $course);
114$event->add_record_snapshot('feedback', $feedback);
115$event->trigger();
84973212 116
117/// Print the page header
118$strfeedbacks = get_string("modulenameplural", "feedback");
119$strfeedback = get_string("modulename", "feedback");
84973212 120echo $OUTPUT->header();
121
84973212 122/// Print the main part of the page
123///////////////////////////////////////////////////////////////////////////
124///////////////////////////////////////////////////////////////////////////
125///////////////////////////////////////////////////////////////////////////
126
38b5a5e2 127$previewimg = $OUTPUT->pix_icon('t/preview', get_string('preview'));
846cebe5 128$previewlnk = new moodle_url('/mod/feedback/print.php', array('id' => $id));
4832a0b3
MG
129if ($courseid) {
130 $previewlnk->param('courseid', $courseid);
131}
846cebe5 132$preview = html_writer::link($previewlnk, $previewimg);
84973212 133
846cebe5
RW
134echo $OUTPUT->heading(format_string($feedback->name) . $preview);
135
136// Print the tabs.
137require('tabs.php');
38b5a5e2 138
ed724aac
MG
139// Show description.
140echo $OUTPUT->box_start('generalbox feedback_description');
141$options = (object)array('noclean' => true);
142echo format_module_intro('feedback', $feedback, $cm->id);
143echo $OUTPUT->box_end();
144
38b5a5e2 145//show some infos to the feedback
9c5bc7a5 146if (has_capability('mod/feedback:edititems', $context)) {
ed724aac
MG
147
148 echo $OUTPUT->heading(get_string('overview', 'feedback'), 3);
149
38b5a5e2 150 //get the groupid
9c5bc7a5 151 $groupselect = groups_print_activity_menu($cm, $CFG->wwwroot.'/mod/feedback/view.php?id='.$cm->id, true);
38b5a5e2
AG
152 $mygroupid = groups_get_activity_group($cm);
153
ed724aac 154 echo $OUTPUT->box_start('boxaligncenter');
38b5a5e2
AG
155 echo $groupselect.'<div class="clearer">&nbsp;</div>';
156 $completedscount = feedback_get_completeds_group_count($feedback, $mygroupid);
157 echo $OUTPUT->box_start('feedback_info');
9c5bc7a5
AG
158 echo '<span class="feedback_info">';
159 echo get_string('completed_feedbacks', 'feedback').': ';
160 echo '</span>';
161 echo '<span class="feedback_info_value">';
162 echo $completedscount;
163 echo '</span>';
38b5a5e2 164 echo $OUTPUT->box_end();
9c5bc7a5
AG
165
166 $params = array('feedback'=>$feedback->id, 'hasvalue'=>1);
167 $itemscount = $DB->count_records('feedback_item', $params);
38b5a5e2 168 echo $OUTPUT->box_start('feedback_info');
9c5bc7a5
AG
169 echo '<span class="feedback_info">';
170 echo get_string('questions', 'feedback').': ';
171 echo '</span>';
172 echo '<span class="feedback_info_value">';
173 echo $itemscount;
174 echo '</span>';
38b5a5e2 175 echo $OUTPUT->box_end();
2396a414 176
9c5bc7a5 177 if ($feedback->timeopen) {
38b5a5e2 178 echo $OUTPUT->box_start('feedback_info');
9c5bc7a5
AG
179 echo '<span class="feedback_info">';
180 echo get_string('feedbackopen', 'feedback').': ';
181 echo '</span>';
182 echo '<span class="feedback_info_value">';
183 echo userdate($feedback->timeopen);
184 echo '</span>';
38b5a5e2
AG
185 echo $OUTPUT->box_end();
186 }
9c5bc7a5 187 if ($feedback->timeclose) {
38b5a5e2 188 echo $OUTPUT->box_start('feedback_info');
9c5bc7a5 189 echo '<span class="feedback_info">';
9b108a86 190 echo get_string('feedbackclose', 'feedback').': ';
9c5bc7a5
AG
191 echo '</span>';
192 echo '<span class="feedback_info_value">';
193 echo userdate($feedback->timeclose);
194 echo '</span>';
38b5a5e2
AG
195 echo $OUTPUT->box_end();
196 }
197 echo $OUTPUT->box_end();
198}
199
9c5bc7a5 200if (has_capability('mod/feedback:edititems', $context)) {
abd2899c
AG
201 require_once($CFG->libdir . '/filelib.php');
202
203 $page_after_submit_output = file_rewrite_pluginfile_urls($feedback->page_after_submit,
204 'pluginfile.php',
205 $context->id,
206 'mod_feedback',
207 'page_after_submit',
208 0);
209
846cebe5 210 echo $OUTPUT->heading(get_string("page_after_submit", "feedback"), 3);
ed724aac 211 echo $OUTPUT->box_start('generalbox feedback_after_submit');
abd2899c 212 echo format_text($page_after_submit_output,
9c5bc7a5
AG
213 $feedback->page_after_submitformat,
214 array('overflowdiv'=>true));
215
84973212 216 echo $OUTPUT->box_end();
217}
c70ad9f7 218
ed724aac
MG
219if (!has_capability('mod/feedback:viewreports', $context) &&
220 feedback_can_view_analysis($feedback, $context, $courseid)) {
221 $analysisurl = new moodle_url('/mod/feedback/analysis.php', array('id' => $id));
222 echo '<div class="mdl-align"><a href="'.$analysisurl->out().'">';
223 echo get_string('completed_feedbacks', 'feedback').'</a>';
224 echo '</div>';
84973212 225}
c70ad9f7 226
84973212 227//####### mapcourse-start
ed724aac
MG
228if (has_capability('mod/feedback:mapcourse', $context) && $feedback->course == SITEID) {
229 echo $OUTPUT->box_start('generalbox feedback_mapped_courses');
230 echo $OUTPUT->heading(get_string("mappedcourses", "feedback"), 3);
231 echo '<p>' . get_string('mapcourse_help', 'feedback') . '</p>';
232 $mapurl = new moodle_url('/mod/feedback/mapcourse.php', array('id' => $id));
233 echo '<p class="mdl-align">' . html_writer::link($mapurl, get_string('mapcourses', 'feedback')) . '</p>';
234 echo $OUTPUT->box_end();
84973212 235}
236//####### mapcourse-end
da329996 237
84973212 238//####### completed-start
9c5bc7a5 239if ($feedback_complete_cap) {
d4b1d58c 240 echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
84973212 241 //check, whether the feedback is open (timeopen, timeclose)
242 $checktime = time();
9c5bc7a5
AG
243 if (($feedback->timeopen > $checktime) OR
244 ($feedback->timeclose < $checktime AND $feedback->timeclose > 0)) {
245
846cebe5 246 echo $OUTPUT->notification(get_string('feedback_is_not_open', 'feedback'));
9c5bc7a5 247 echo $OUTPUT->continue_button($CFG->wwwroot.'/course/view.php?id='.$course->id);
d4b1d58c 248 echo $OUTPUT->box_end();
84973212 249 echo $OUTPUT->footer();
250 exit;
c70ad9f7 251 }
d4b1d58c 252
84973212 253 //check multiple Submit
254 $feedback_can_submit = true;
9c5bc7a5
AG
255 if ($feedback->multiple_submit == 0 ) {
256 if (feedback_is_already_submitted($feedback->id, $courseid)) {
84973212 257 $feedback_can_submit = false;
c70ad9f7 258 }
259 }
9c5bc7a5 260 if ($feedback_can_submit) {
84973212 261 //if the user is not known so we cannot save the values temporarly
9c5bc7a5 262 if (!isloggedin() or isguestuser()) {
84973212 263 $guestid = sesskey();
9c5bc7a5 264 } else {
84973212 265 $guestid = false;
c70ad9f7 266 }
9c5bc7a5 267 $url_params = array('id'=>$id, 'courseid'=>$courseid, 'gopage'=>0);
4832a0b3 268 $completeurl = new moodle_url('/mod/feedback/complete.php', $url_params);
2396a414 269
9c5bc7a5
AG
270 $feedbackcompletedtmp = feedback_get_current_completed($feedback->id, true, $courseid, $guestid);
271 if ($feedbackcompletedtmp) {
272 if ($startpage = feedback_get_page_to_continue($feedback->id, $courseid, $guestid)) {
5dee2210 273 $completeurl->param('gopage', $startpage);
c70ad9f7 274 }
5dee2210 275 echo '<a href="'.$completeurl->out().'">'.get_string('continue_the_form', 'feedback').'</a>';
9c5bc7a5 276 } else {
5dee2210 277 echo '<a href="'.$completeurl->out().'">'.get_string('complete_the_form', 'feedback').'</a>';
84973212 278 }
9c5bc7a5 279 } else {
846cebe5 280 echo $OUTPUT->notification(get_string('this_feedback_is_already_submitted', 'feedback'));
9c5bc7a5 281 if ($courseid) {
84973212 282 echo $OUTPUT->continue_button($CFG->wwwroot.'/course/view.php?id='.$courseid);
9c5bc7a5 283 } else {
84973212 284 echo $OUTPUT->continue_button($CFG->wwwroot.'/course/view.php?id='.$course->id);
c70ad9f7 285 }
c70ad9f7 286 }
84973212 287 echo $OUTPUT->box_end();
288}
289//####### completed-end
c70ad9f7 290
84973212 291/// Finish the page
292///////////////////////////////////////////////////////////////////////////
293///////////////////////////////////////////////////////////////////////////
294///////////////////////////////////////////////////////////////////////////
c70ad9f7 295
84973212 296echo $OUTPUT->footer();
c70ad9f7 297