Merge branch 'MDL-51803-master-mouse' of git://github.com/marinaglancy/moodle
[moodle.git] / mod / quiz / index.php
blobf15c30304e75cd762c03890d10377c20031765c5
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 feedback header.
59 $showfeedback = false;
60 foreach ($quizzes as $quiz) {
61 if (quiz_has_feedback($quiz)) {
62 $showfeedback=true;
64 if ($showfeedback) {
65 break;
69 // Configure table for displaying the list of instances.
70 $headings = array(get_string('name'));
71 $align = array('left');
73 array_push($headings, get_string('quizcloses', 'quiz'));
74 array_push($align, 'left');
76 if (course_format_uses_sections($course->format)) {
77 array_unshift($headings, get_string('sectionname', 'format_'.$course->format));
78 } else {
79 array_unshift($headings, '');
81 array_unshift($align, 'center');
83 $showing = '';
85 if (has_capability('mod/quiz:viewreports', $coursecontext)) {
86 array_push($headings, get_string('attempts', 'quiz'));
87 array_push($align, 'left');
88 $showing = 'stats';
90 } else if (has_any_capability(array('mod/quiz:reviewmyattempts', 'mod/quiz:attempt'),
91 $coursecontext)) {
92 array_push($headings, get_string('grade', 'quiz'));
93 array_push($align, 'left');
94 if ($showfeedback) {
95 array_push($headings, get_string('feedback', 'quiz'));
96 array_push($align, 'left');
98 $showing = 'grades';
100 $grades = $DB->get_records_sql_menu('
101 SELECT qg.quiz, qg.grade
102 FROM {quiz_grades} qg
103 JOIN {quiz} q ON q.id = qg.quiz
104 WHERE q.course = ? AND qg.userid = ?',
105 array($course->id, $USER->id));
108 $table = new html_table();
109 $table->head = $headings;
110 $table->align = $align;
112 // Populate the table with the list of instances.
113 $currentsection = '';
114 // Get all closing dates.
115 $timeclosedates = quiz_get_user_timeclose($course->id);
116 foreach ($quizzes as $quiz) {
117 $cm = get_coursemodule_from_instance('quiz', $quiz->id);
118 $context = context_module::instance($cm->id);
119 $data = array();
121 // Section number if necessary.
122 $strsection = '';
123 if ($quiz->section != $currentsection) {
124 if ($quiz->section) {
125 $strsection = $quiz->section;
126 $strsection = get_section_name($course, $quiz->section);
128 if ($currentsection) {
129 $learningtable->data[] = 'hr';
131 $currentsection = $quiz->section;
133 $data[] = $strsection;
135 // Link to the instance.
136 $class = '';
137 if (!$quiz->visible) {
138 $class = ' class="dimmed"';
140 $data[] = "<a$class href=\"view.php?id=$quiz->coursemodule\">" .
141 format_string($quiz->name, true) . '</a>';
143 // Close date.
144 if (($timeclosedates[$quiz->id]->usertimeclose != 0)) {
145 $data[] = userdate($timeclosedates[$quiz->id]->usertimeclose);
146 } else {
147 $data[] = get_string('noclose', 'quiz');
150 if ($showing == 'stats') {
151 // The $quiz objects returned by get_all_instances_in_course have the necessary $cm
152 // fields set to make the following call work.
153 $data[] = quiz_attempt_summary_link_to_reports($quiz, $cm, $context);
155 } else if ($showing == 'grades') {
156 // Grade and feedback.
157 $attempts = quiz_get_user_attempts($quiz->id, $USER->id, 'all');
158 list($someoptions, $alloptions) = quiz_get_combined_reviewoptions(
159 $quiz, $attempts);
161 $grade = '';
162 $feedback = '';
163 if ($quiz->grade && array_key_exists($quiz->id, $grades)) {
164 if ($alloptions->marks >= question_display_options::MARK_AND_MAX) {
165 $a = new stdClass();
166 $a->grade = quiz_format_grade($quiz, $grades[$quiz->id]);
167 $a->maxgrade = quiz_format_grade($quiz, $quiz->grade);
168 $grade = get_string('outofshort', 'quiz', $a);
170 if ($alloptions->overallfeedback) {
171 $feedback = quiz_feedback_for_grade($grades[$quiz->id], $quiz, $context);
174 $data[] = $grade;
175 if ($showfeedback) {
176 $data[] = $feedback;
180 $table->data[] = $data;
181 } // End of loop over quiz instances.
183 // Display the table.
184 echo html_writer::table($table);
186 // Finish the page.
187 echo $OUTPUT->footer();