MDL-62543 mod_forum: Add RSS Unit tests
[moodle.git] / mod / quiz / index.php
blob38828822a6b96a38057ac850b05cd4f65ad8d0f7
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.
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 script lists all the instances of quiz in a particular course
20 * @package mod_quiz
21 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require_once("../../config.php");
27 require_once("locallib.php");
29 $id = required_param('id', PARAM_INT);
30 $PAGE->set_url('/mod/quiz/index.php', array('id'=>$id));
31 if (!$course = $DB->get_record('course', array('id' => $id))) {
32 print_error('invalidcourseid');
34 $coursecontext = context_course::instance($id);
35 require_login($course);
36 $PAGE->set_pagelayout('incourse');
38 $params = array(
39 'context' => $coursecontext
41 $event = \mod_quiz\event\course_module_instance_list_viewed::create($params);
42 $event->trigger();
44 // Print the header.
45 $strquizzes = get_string("modulenameplural", "quiz");
46 $PAGE->navbar->add($strquizzes);
47 $PAGE->set_title($strquizzes);
48 $PAGE->set_heading($course->fullname);
49 echo $OUTPUT->header();
50 echo $OUTPUT->heading($strquizzes, 2);
52 // Get all the appropriate data.
53 if (!$quizzes = get_all_instances_in_course("quiz", $course)) {
54 notice(get_string('thereareno', 'moodle', $strquizzes), "../../course/view.php?id=$course->id");
55 die;
58 // Check if we need the closing date header.
59 $showclosingheader = false;
60 $showfeedback = false;
61 foreach ($quizzes as $quiz) {
62 if ($quiz->timeclose!=0) {
63 $showclosingheader=true;
65 if (quiz_has_feedback($quiz)) {
66 $showfeedback=true;
68 if ($showclosingheader && $showfeedback) {
69 break;
73 // Configure table for displaying the list of instances.
74 $headings = array(get_string('name'));
75 $align = array('left');
77 if ($showclosingheader) {
78 array_push($headings, get_string('quizcloses', 'quiz'));
79 array_push($align, 'left');
82 if (course_format_uses_sections($course->format)) {
83 array_unshift($headings, get_string('sectionname', 'format_'.$course->format));
84 } else {
85 array_unshift($headings, '');
87 array_unshift($align, 'center');
89 $showing = '';
91 if (has_capability('mod/quiz:viewreports', $coursecontext)) {
92 array_push($headings, get_string('attempts', 'quiz'));
93 array_push($align, 'left');
94 $showing = 'stats';
96 } else if (has_any_capability(array('mod/quiz:reviewmyattempts', 'mod/quiz:attempt'),
97 $coursecontext)) {
98 array_push($headings, get_string('grade', 'quiz'));
99 array_push($align, 'left');
100 if ($showfeedback) {
101 array_push($headings, get_string('feedback', 'quiz'));
102 array_push($align, 'left');
104 $showing = 'grades';
106 $grades = $DB->get_records_sql_menu('
107 SELECT qg.quiz, qg.grade
108 FROM {quiz_grades} qg
109 JOIN {quiz} q ON q.id = qg.quiz
110 WHERE q.course = ? AND qg.userid = ?',
111 array($course->id, $USER->id));
114 $table = new html_table();
115 $table->head = $headings;
116 $table->align = $align;
118 // Populate the table with the list of instances.
119 $currentsection = '';
120 // Get all closing dates.
121 $timeclosedates = quiz_get_user_timeclose($course->id);
122 foreach ($quizzes as $quiz) {
123 $cm = get_coursemodule_from_instance('quiz', $quiz->id);
124 $context = context_module::instance($cm->id);
125 $data = array();
127 // Section number if necessary.
128 $strsection = '';
129 if ($quiz->section != $currentsection) {
130 if ($quiz->section) {
131 $strsection = $quiz->section;
132 $strsection = get_section_name($course, $quiz->section);
134 if ($currentsection) {
135 $learningtable->data[] = 'hr';
137 $currentsection = $quiz->section;
139 $data[] = $strsection;
141 // Link to the instance.
142 $class = '';
143 if (!$quiz->visible) {
144 $class = ' class="dimmed"';
146 $data[] = "<a$class href=\"view.php?id=$quiz->coursemodule\">" .
147 format_string($quiz->name, true) . '</a>';
149 // Close date.
150 if ($quiz->timeclose) {
151 if (($timeclosedates[$quiz->id]->usertimeclose == 0) AND ($timeclosedates[$quiz->id]->usertimelimit == 0)) {
152 $data[] = get_string('noclose', 'quiz');
153 } else {
154 $data[] = userdate($timeclosedates[$quiz->id]->usertimeclose);
156 } else if ($showclosingheader) {
157 $data[] = '';
160 if ($showing == 'stats') {
161 // The $quiz objects returned by get_all_instances_in_course have the necessary $cm
162 // fields set to make the following call work.
163 $data[] = quiz_attempt_summary_link_to_reports($quiz, $cm, $context);
165 } else if ($showing == 'grades') {
166 // Grade and feedback.
167 $attempts = quiz_get_user_attempts($quiz->id, $USER->id, 'all');
168 list($someoptions, $alloptions) = quiz_get_combined_reviewoptions(
169 $quiz, $attempts);
171 $grade = '';
172 $feedback = '';
173 if ($quiz->grade && array_key_exists($quiz->id, $grades)) {
174 if ($alloptions->marks >= question_display_options::MARK_AND_MAX) {
175 $a = new stdClass();
176 $a->grade = quiz_format_grade($quiz, $grades[$quiz->id]);
177 $a->maxgrade = quiz_format_grade($quiz, $quiz->grade);
178 $grade = get_string('outofshort', 'quiz', $a);
180 if ($alloptions->overallfeedback) {
181 $feedback = quiz_feedback_for_grade($grades[$quiz->id], $quiz, $context);
184 $data[] = $grade;
185 if ($showfeedback) {
186 $data[] = $feedback;
190 $table->data[] = $data;
191 } // End of loop over quiz instances.
193 // Display the table.
194 echo html_writer::table($table);
196 // Finish the page.
197 echo $OUTPUT->footer();