Merge branch 'MDL-53115-master' of git://github.com/andrewnicols/moodle
[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}
72$PAGE->set_url('/mod/feedback/view.php', array('id'=>$cm->id, 'do_show'=>'view'));
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
AG
138
139//show some infos to the feedback
9c5bc7a5 140if (has_capability('mod/feedback:edititems', $context)) {
38b5a5e2 141 //get the groupid
9c5bc7a5 142 $groupselect = groups_print_activity_menu($cm, $CFG->wwwroot.'/mod/feedback/view.php?id='.$cm->id, true);
38b5a5e2
AG
143 $mygroupid = groups_get_activity_group($cm);
144
145 echo $OUTPUT->box_start('boxaligncenter boxwidthwide');
146 echo $groupselect.'<div class="clearer">&nbsp;</div>';
147 $completedscount = feedback_get_completeds_group_count($feedback, $mygroupid);
148 echo $OUTPUT->box_start('feedback_info');
9c5bc7a5
AG
149 echo '<span class="feedback_info">';
150 echo get_string('completed_feedbacks', 'feedback').': ';
151 echo '</span>';
152 echo '<span class="feedback_info_value">';
153 echo $completedscount;
154 echo '</span>';
38b5a5e2 155 echo $OUTPUT->box_end();
9c5bc7a5
AG
156
157 $params = array('feedback'=>$feedback->id, 'hasvalue'=>1);
158 $itemscount = $DB->count_records('feedback_item', $params);
38b5a5e2 159 echo $OUTPUT->box_start('feedback_info');
9c5bc7a5
AG
160 echo '<span class="feedback_info">';
161 echo get_string('questions', 'feedback').': ';
162 echo '</span>';
163 echo '<span class="feedback_info_value">';
164 echo $itemscount;
165 echo '</span>';
38b5a5e2 166 echo $OUTPUT->box_end();
2396a414 167
9c5bc7a5 168 if ($feedback->timeopen) {
38b5a5e2 169 echo $OUTPUT->box_start('feedback_info');
9c5bc7a5
AG
170 echo '<span class="feedback_info">';
171 echo get_string('feedbackopen', 'feedback').': ';
172 echo '</span>';
173 echo '<span class="feedback_info_value">';
174 echo userdate($feedback->timeopen);
175 echo '</span>';
38b5a5e2
AG
176 echo $OUTPUT->box_end();
177 }
9c5bc7a5 178 if ($feedback->timeclose) {
38b5a5e2 179 echo $OUTPUT->box_start('feedback_info');
9c5bc7a5 180 echo '<span class="feedback_info">';
9b108a86 181 echo get_string('feedbackclose', 'feedback').': ';
9c5bc7a5
AG
182 echo '</span>';
183 echo '<span class="feedback_info_value">';
184 echo userdate($feedback->timeclose);
185 echo '</span>';
38b5a5e2
AG
186 echo $OUTPUT->box_end();
187 }
188 echo $OUTPUT->box_end();
189}
190
9c5bc7a5 191if (has_capability('mod/feedback:edititems', $context)) {
846cebe5 192 echo $OUTPUT->heading(get_string('description', 'feedback'), 3);
38b5a5e2 193}
846cebe5 194echo $OUTPUT->box_start('generalbox boxwidthwide');
84973212 195$options = (object)array('noclean'=>true);
196echo format_module_intro('feedback', $feedback, $cm->id);
197echo $OUTPUT->box_end();
198
9c5bc7a5 199if (has_capability('mod/feedback:edititems', $context)) {
abd2899c
AG
200 require_once($CFG->libdir . '/filelib.php');
201
202 $page_after_submit_output = file_rewrite_pluginfile_urls($feedback->page_after_submit,
203 'pluginfile.php',
204 $context->id,
205 'mod_feedback',
206 'page_after_submit',
207 0);
208
846cebe5 209 echo $OUTPUT->heading(get_string("page_after_submit", "feedback"), 3);
84973212 210 echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
abd2899c 211 echo format_text($page_after_submit_output,
9c5bc7a5
AG
212 $feedback->page_after_submitformat,
213 array('overflowdiv'=>true));
214
84973212 215 echo $OUTPUT->box_end();
216}
c70ad9f7 217
9c5bc7a5 218if ( (intval($feedback->publish_stats) == 1) AND
2396a414 219 ( has_capability('mod/feedback:viewanalysepage', $context)) AND
c987dbad 220 !( has_capability('mod/feedback:viewreports', $context)) ) {
9c5bc7a5
AG
221
222 $params = array('userid'=>$USER->id, 'feedback'=>$feedback->id);
223 if ($multiple_count = $DB->count_records('feedback_tracking', $params)) {
224 $url_params = array('id'=>$id, 'courseid'=>$courseid);
225 $analysisurl = new moodle_url('/mod/feedback/analysis.php', $url_params);
5dee2210 226 echo '<div class="mdl-align"><a href="'.$analysisurl->out().'">';
84973212 227 echo get_string('completed_feedbacks', 'feedback').'</a>';
228 echo '</div>';
c70ad9f7 229 }
84973212 230}
c70ad9f7 231
84973212 232//####### mapcourse-start
9c5bc7a5
AG
233if (has_capability('mod/feedback:mapcourse', $context)) {
234 if ($feedback->course == SITEID) {
84973212 235 echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
236 echo '<div class="mdl-align">';
237 echo '<form action="mapcourse.php" method="get">';
9d092aa9 238 echo '<fieldset>';
84973212 239 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
240 echo '<input type="hidden" name="id" value="'.$id.'" />';
241 echo '<button type="submit">'.get_string('mapcourses', 'feedback').'</button>';
e8790d59 242 echo $OUTPUT->help_icon('mapcourse', 'feedback');
9d092aa9 243 echo '</fieldset>';
84973212 244 echo '</form>';
245 echo '<br />';
246 echo '</div>';
247 echo $OUTPUT->box_end();
da329996 248 }
84973212 249}
250//####### mapcourse-end
da329996 251
84973212 252//####### completed-start
9c5bc7a5 253if ($feedback_complete_cap) {
d4b1d58c 254 echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
84973212 255 //check, whether the feedback is open (timeopen, timeclose)
256 $checktime = time();
9c5bc7a5
AG
257 if (($feedback->timeopen > $checktime) OR
258 ($feedback->timeclose < $checktime AND $feedback->timeclose > 0)) {
259
846cebe5 260 echo $OUTPUT->notification(get_string('feedback_is_not_open', 'feedback'));
9c5bc7a5 261 echo $OUTPUT->continue_button($CFG->wwwroot.'/course/view.php?id='.$course->id);
d4b1d58c 262 echo $OUTPUT->box_end();
84973212 263 echo $OUTPUT->footer();
264 exit;
c70ad9f7 265 }
d4b1d58c 266
84973212 267 //check multiple Submit
268 $feedback_can_submit = true;
9c5bc7a5
AG
269 if ($feedback->multiple_submit == 0 ) {
270 if (feedback_is_already_submitted($feedback->id, $courseid)) {
84973212 271 $feedback_can_submit = false;
c70ad9f7 272 }
273 }
9c5bc7a5 274 if ($feedback_can_submit) {
84973212 275 //if the user is not known so we cannot save the values temporarly
9c5bc7a5 276 if (!isloggedin() or isguestuser()) {
84973212 277 $guestid = sesskey();
9c5bc7a5 278 } else {
84973212 279 $guestid = false;
c70ad9f7 280 }
9c5bc7a5 281 $url_params = array('id'=>$id, 'courseid'=>$courseid, 'gopage'=>0);
4832a0b3 282 $completeurl = new moodle_url('/mod/feedback/complete.php', $url_params);
2396a414 283
9c5bc7a5
AG
284 $feedbackcompletedtmp = feedback_get_current_completed($feedback->id, true, $courseid, $guestid);
285 if ($feedbackcompletedtmp) {
286 if ($startpage = feedback_get_page_to_continue($feedback->id, $courseid, $guestid)) {
5dee2210 287 $completeurl->param('gopage', $startpage);
c70ad9f7 288 }
5dee2210 289 echo '<a href="'.$completeurl->out().'">'.get_string('continue_the_form', 'feedback').'</a>';
9c5bc7a5 290 } else {
5dee2210 291 echo '<a href="'.$completeurl->out().'">'.get_string('complete_the_form', 'feedback').'</a>';
84973212 292 }
9c5bc7a5 293 } else {
846cebe5 294 echo $OUTPUT->notification(get_string('this_feedback_is_already_submitted', 'feedback'));
9c5bc7a5 295 if ($courseid) {
84973212 296 echo $OUTPUT->continue_button($CFG->wwwroot.'/course/view.php?id='.$courseid);
9c5bc7a5 297 } else {
84973212 298 echo $OUTPUT->continue_button($CFG->wwwroot.'/course/view.php?id='.$course->id);
c70ad9f7 299 }
c70ad9f7 300 }
84973212 301 echo $OUTPUT->box_end();
302}
303//####### completed-end
c70ad9f7 304
84973212 305/// Finish the page
306///////////////////////////////////////////////////////////////////////////
307///////////////////////////////////////////////////////////////////////////
308///////////////////////////////////////////////////////////////////////////
c70ad9f7 309
84973212 310echo $OUTPUT->footer();
c70ad9f7 311