Merge branch 'MDL-48372-27' of git://github.com/ankitagarwal/moodle into MOODLE_27_STABLE
[moodle.git] / mod / quiz / index.php
blobf119782d81f2012ede74172dcdb09893fcc6d9e2
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 $streditquestions = '';
47 $editqcontexts = new question_edit_contexts($coursecontext);
48 if ($editqcontexts->have_one_edit_tab_cap('questions')) {
49 $streditquestions =
50 "<form target=\"_parent\" method=\"get\" action=\"$CFG->wwwroot/question/edit.php\">
51 <div>
52 <input type=\"hidden\" name=\"courseid\" value=\"$course->id\" />
53 <input type=\"submit\" value=\"".get_string("editquestions", "quiz")."\" />
54 </div>
55 </form>";
57 $PAGE->navbar->add($strquizzes);
58 $PAGE->set_title($strquizzes);
59 $PAGE->set_button($streditquestions);
60 $PAGE->set_heading($course->fullname);
61 echo $OUTPUT->header();
62 echo $OUTPUT->heading($strquizzes, 2);
64 // Get all the appropriate data.
65 if (!$quizzes = get_all_instances_in_course("quiz", $course)) {
66 notice(get_string('thereareno', 'moodle', $strquizzes), "../../course/view.php?id=$course->id");
67 die;
70 // Check if we need the closing date header.
71 $showclosingheader = false;
72 $showfeedback = false;
73 foreach ($quizzes as $quiz) {
74 if ($quiz->timeclose!=0) {
75 $showclosingheader=true;
77 if (quiz_has_feedback($quiz)) {
78 $showfeedback=true;
80 if ($showclosingheader && $showfeedback) {
81 break;
85 // Configure table for displaying the list of instances.
86 $headings = array(get_string('name'));
87 $align = array('left');
89 if ($showclosingheader) {
90 array_push($headings, get_string('quizcloses', 'quiz'));
91 array_push($align, 'left');
94 if (course_format_uses_sections($course->format)) {
95 array_unshift($headings, get_string('sectionname', 'format_'.$course->format));
96 } else {
97 array_unshift($headings, '');
99 array_unshift($align, 'center');
101 $showing = '';
103 if (has_capability('mod/quiz:viewreports', $coursecontext)) {
104 array_push($headings, get_string('attempts', 'quiz'));
105 array_push($align, 'left');
106 $showing = 'stats';
108 } else if (has_any_capability(array('mod/quiz:reviewmyattempts', 'mod/quiz:attempt'),
109 $coursecontext)) {
110 array_push($headings, get_string('grade', 'quiz'));
111 array_push($align, 'left');
112 if ($showfeedback) {
113 array_push($headings, get_string('feedback', 'quiz'));
114 array_push($align, 'left');
116 $showing = 'grades';
118 $grades = $DB->get_records_sql_menu('
119 SELECT qg.quiz, qg.grade
120 FROM {quiz_grades} qg
121 JOIN {quiz} q ON q.id = qg.quiz
122 WHERE q.course = ? AND qg.userid = ?',
123 array($course->id, $USER->id));
126 $table = new html_table();
127 $table->head = $headings;
128 $table->align = $align;
130 // Populate the table with the list of instances.
131 $currentsection = '';
132 foreach ($quizzes as $quiz) {
133 $cm = get_coursemodule_from_instance('quiz', $quiz->id);
134 $context = context_module::instance($cm->id);
135 $data = array();
137 // Section number if necessary.
138 $strsection = '';
139 if ($quiz->section != $currentsection) {
140 if ($quiz->section) {
141 $strsection = $quiz->section;
142 $strsection = get_section_name($course, $quiz->section);
144 if ($currentsection) {
145 $learningtable->data[] = 'hr';
147 $currentsection = $quiz->section;
149 $data[] = $strsection;
151 // Link to the instance.
152 $class = '';
153 if (!$quiz->visible) {
154 $class = ' class="dimmed"';
156 $data[] = "<a$class href=\"view.php?id=$quiz->coursemodule\">" .
157 format_string($quiz->name, true) . '</a>';
159 // Close date.
160 if ($quiz->timeclose) {
161 $data[] = userdate($quiz->timeclose);
162 } else if ($showclosingheader) {
163 $data[] = '';
166 if ($showing == 'stats') {
167 // The $quiz objects returned by get_all_instances_in_course have the necessary $cm
168 // fields set to make the following call work.
169 $data[] = quiz_attempt_summary_link_to_reports($quiz, $cm, $context);
171 } else if ($showing == 'grades') {
172 // Grade and feedback.
173 $attempts = quiz_get_user_attempts($quiz->id, $USER->id, 'all');
174 list($someoptions, $alloptions) = quiz_get_combined_reviewoptions(
175 $quiz, $attempts, $context);
177 $grade = '';
178 $feedback = '';
179 if ($quiz->grade && array_key_exists($quiz->id, $grades)) {
180 if ($alloptions->marks >= question_display_options::MARK_AND_MAX) {
181 $a = new stdClass();
182 $a->grade = quiz_format_grade($quiz, $grades[$quiz->id]);
183 $a->maxgrade = quiz_format_grade($quiz, $quiz->grade);
184 $grade = get_string('outofshort', 'quiz', $a);
186 if ($alloptions->overallfeedback) {
187 $feedback = quiz_feedback_for_grade($grades[$quiz->id], $quiz, $context);
190 $data[] = $grade;
191 if ($showfeedback) {
192 $data[] = $feedback;
196 $table->data[] = $data;
197 } // End of loop over quiz instances.
199 // Display the table.
200 echo html_writer::table($table);
202 // Finish the page.
203 echo $OUTPUT->footer();