debfa87fa4d3572d1a5c092537b1a5e8932c81b6
[moodle.git] / mod / quiz / report / responses / report.php
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.
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/>.
17 /**
18  * This file defines the quiz responses report class.
19  *
20  * @package   quiz_responses
21  * @copyright 2006 Jean-Michel Vedrine
22  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
26 defined('MOODLE_INTERNAL') || die();
28 require_once($CFG->dirroot . '/mod/quiz/report/attemptsreport.php');
29 require_once($CFG->dirroot . '/mod/quiz/report/responses/responses_options.php');
30 require_once($CFG->dirroot . '/mod/quiz/report/responses/responses_form.php');
31 require_once($CFG->dirroot . '/mod/quiz/report/responses/last_responses_table.php');
32 require_once($CFG->dirroot . '/mod/quiz/report/responses/first_or_all_responses_table.php');
35 /**
36  * Quiz report subclass for the responses report.
37  *
38  * This report lists some combination of
39  *  * what question each student saw (this makes sense if random questions were used).
40  *  * the response they gave,
41  *  * and what the right answer is.
42  *
43  * Like the overview report, there are options for showing students with/without
44  * attempts, and for deleting selected attempts.
45  *
46  * @copyright 1999 onwards Martin Dougiamas and others {@link http://moodle.com}
47  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
48  */
49 class quiz_responses_report extends quiz_attempts_report {
51     public function display($quiz, $cm, $course) {
52         global $OUTPUT, $DB;
54         list($currentgroup, $studentsjoins, $groupstudentsjoins, $allowedjoins) = $this->init(
55                 'responses', 'quiz_responses_settings_form', $quiz, $cm, $course);
57         $options = new quiz_responses_options('responses', $quiz, $cm, $course);
59         if ($fromform = $this->form->get_data()) {
60             $options->process_settings_from_form($fromform);
62         } else {
63             $options->process_settings_from_params();
64         }
66         $this->form->set_data($options->get_initial_form_data());
68         // Load the required questions.
69         $questions = quiz_report_get_significant_questions($quiz);
71         // Prepare for downloading, if applicable.
72         $courseshortname = format_string($course->shortname, true,
73                 array('context' => context_course::instance($course->id)));
74         if ($options->whichtries === question_attempt::LAST_TRY) {
75             $tableclassname = 'quiz_last_responses_table';
76         } else {
77             $tableclassname = 'quiz_first_or_all_responses_table';
78         }
79         $table = new $tableclassname($quiz, $this->context, $this->qmsubselect,
80                 $options, $groupstudentsjoins, $studentsjoins, $questions, $options->get_url());
81         $filename = quiz_report_download_filename(get_string('responsesfilename', 'quiz_responses'),
82                 $courseshortname, $quiz->name);
83         $table->is_downloading($options->download, $filename,
84                 $courseshortname . ' ' . format_string($quiz->name, true));
85         if ($table->is_downloading()) {
86             raise_memory_limit(MEMORY_EXTRA);
87         }
89         $this->hasgroupstudents = false;
90         if (!empty($groupstudentsjoins->joins)) {
91             $sql = "SELECT DISTINCT u.id
92                       FROM {user} u
93                     $groupstudentsjoins->joins
94                      WHERE $groupstudentsjoins->wheres";
95             $this->hasgroupstudents = $DB->record_exists_sql($sql, $groupstudentsjoins->params);
96         }
97         $hasstudents = false;
98         if (!empty($studentsjoins->joins)) {
99             $sql = "SELECT DISTINCT u.id
100                     FROM {user} u
101                     $studentsjoins->joins
102                     WHERE $studentsjoins->wheres";
103             $hasstudents = $DB->record_exists_sql($sql, $studentsjoins->params);
104         }
105         if ($options->attempts == self::ALL_WITH) {
106             // This option is only available to users who can access all groups in
107             // groups mode, so setting allowed to empty (which means all quiz attempts
108             // are accessible, is not a security problem.
109             $allowedjoins = new \core\dml\sql_join();
110         }
112         $this->process_actions($quiz, $cm, $currentgroup, $groupstudentsjoins, $allowedjoins, $options->get_url());
114         // Start output.
115         if (!$table->is_downloading()) {
116             // Only print headers if not asked to download data.
117             $this->print_header_and_tabs($cm, $course, $quiz, $this->mode);
118         }
120         if ($groupmode = groups_get_activity_groupmode($cm)) {
121             // Groups are being used, so output the group selector if we are not downloading.
122             if (!$table->is_downloading()) {
123                 groups_print_activity_menu($cm, $options->get_url());
124             }
125         }
127         // Print information on the number of existing attempts.
128         if (!$table->is_downloading()) {
129             // Do not print notices when downloading.
130             if ($strattemptnum = quiz_num_attempt_summary($quiz, $cm, true, $currentgroup)) {
131                 echo '<div class="quizattemptcounts">' . $strattemptnum . '</div>';
132             }
133         }
135         $hasquestions = quiz_has_questions($quiz->id);
136         if (!$table->is_downloading()) {
137             if (!$hasquestions) {
138                 echo quiz_no_questions_message($quiz, $cm, $this->context);
139             } else if (!$hasstudents) {
140                 echo $OUTPUT->notification(get_string('nostudentsyet'));
141             } else if ($currentgroup && !$this->hasgroupstudents) {
142                 echo $OUTPUT->notification(get_string('nostudentsingroup'));
143             }
145             // Print the display options.
146             $this->form->display();
147         }
149         $hasstudents = $hasstudents && (!$currentgroup || $this->hasgroupstudents);
150         if ($hasquestions && ($hasstudents || $options->attempts == self::ALL_WITH)) {
152             list($fields, $from, $where, $params) = $table->base_sql($allowedjoins);
154             $table->set_count_sql("SELECT COUNT(1) FROM $from WHERE $where", $params);
156             $table->set_sql($fields, $from, $where, $params);
158             if (!$table->is_downloading()) {
159                 // Print information on the grading method.
160                 if ($strattempthighlight = quiz_report_highlighting_grading_method(
161                         $quiz, $this->qmsubselect, $options->onlygraded)) {
162                     echo '<div class="quizattemptcounts">' . $strattempthighlight . '</div>';
163                 }
164             }
166             // Define table columns.
167             $columns = array();
168             $headers = array();
170             if (!$table->is_downloading() && $options->checkboxcolumn) {
171                 $columns[] = 'checkbox';
172                 $headers[] = null;
173             }
175             $this->add_user_columns($table, $columns, $headers);
176             $this->add_state_column($columns, $headers);
178             if ($table->is_downloading()) {
179                 $this->add_time_columns($columns, $headers);
180             }
182             $this->add_grade_columns($quiz, $options->usercanseegrades, $columns, $headers);
184             foreach ($questions as $id => $question) {
185                 if ($options->showqtext) {
186                     $columns[] = 'question' . $id;
187                     $headers[] = get_string('questionx', 'question', $question->number);
188                 }
189                 if ($options->showresponses) {
190                     $columns[] = 'response' . $id;
191                     $headers[] = get_string('responsex', 'quiz_responses', $question->number);
192                 }
193                 if ($options->showright) {
194                     $columns[] = 'right' . $id;
195                     $headers[] = get_string('rightanswerx', 'quiz_responses', $question->number);
196                 }
197             }
199             $table->define_columns($columns);
200             $table->define_headers($headers);
201             $table->sortable(true, 'uniqueid');
203             // Set up the table.
204             $table->define_baseurl($options->get_url());
206             $this->configure_user_columns($table);
208             $table->no_sorting('feedbacktext');
209             $table->column_class('sumgrades', 'bold');
211             $table->set_attribute('id', 'responses');
213             $table->collapsible(true);
215             $table->out($options->pagesize, true);
216         }
217         return true;
218     }