Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / mod / quiz / index.php
blobb28e766026872b7b4aa77a30867bc303c0c99cf9
1 <?php // $Id$
2 /**
3 * This page lists all the instances of quiz in a particular course
5 * @author Martin Dougiamas and many others.
6 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
7 * @package quiz
8 */
9 require_once("../../config.php");
10 require_once("locallib.php");
12 $id = required_param('id', PARAM_INT);
13 if (!$course = get_record("course", "id", $id)) {
14 error("Course ID is incorrect");
16 $coursecontext = get_context_instance(CONTEXT_COURSE, $id);
17 require_login($course->id);
18 add_to_log($course->id, "quiz", "view all", "index.php?id=$course->id", "");
20 // Print the header
21 $strquizzes = get_string("modulenameplural", "quiz");
22 $streditquestions = '';
23 $editqcontexts = new question_edit_contexts($coursecontext);
24 if ($editqcontexts->have_one_edit_tab_cap('questions')) {
25 $streditquestions =
26 "<form target=\"_parent\" method=\"get\" action=\"$CFG->wwwroot/question/edit.php\">
27 <div>
28 <input type=\"hidden\" name=\"courseid\" value=\"$course->id\" />
29 <input type=\"submit\" value=\"".get_string("editquestions", "quiz")."\" />
30 </div>
31 </form>";
33 $navlinks = array();
34 $navlinks[] = array('name' => $strquizzes, 'link' => '', 'type' => 'activity');
35 $navigation = build_navigation($navlinks);
37 print_header_simple($strquizzes, '', $navigation,
38 '', '', true, $streditquestions, navmenu($course));
40 // Get all the appropriate data
41 if (!$quizzes = get_all_instances_in_course("quiz", $course)) {
42 notice(get_string('thereareno', 'moodle', $strquizzes), "../../course/view.php?id=$course->id");
43 die;
46 // Configure table for displaying the list of instances.
47 $headings = array(get_string('name'), get_string('quizcloses', 'quiz'));
48 $align = array('left', 'left');
49 if ($course->format == 'weeks' or $course->format == 'weekscss') {
50 array_unshift($headings, get_string('week'));
51 } else {
52 array_unshift($headings, get_string('section'));
54 array_unshift($align, 'center');
56 $showing = 'scores'; // default
58 if (has_capability('mod/quiz:viewreports', $coursecontext)) {
59 array_push($headings, get_string('attempts', 'quiz'));
60 array_push($align, 'left');
61 $showing = 'stats';
62 } else if (has_capability('mod/quiz:attempt', $coursecontext)) {
63 array_push($headings, get_string('bestgrade', 'quiz'), get_string('feedback', 'quiz'));
64 array_push($align, 'left', 'left');
67 $table->head = $headings;
68 $table->align = $align;
70 /// Populate the table with the list of instances.
71 $currentsection = '';
72 foreach ($quizzes as $quiz) {
73 $cm = get_coursemodule_from_instance('quiz', $quiz->id);
74 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
75 $data = array();
77 // Section number if necessary.
78 $strsection = '';
79 if ($quiz->section != $currentsection) {
80 if ($quiz->section) {
81 $strsection = $quiz->section;
83 if ($currentsection) {
84 $learningtable->data[] = 'hr';
86 $currentsection = $quiz->section;
88 $data[] = $strsection;
90 // Link to the instance.
91 $class = '';
92 if (!$quiz->visible) {
93 $class = ' class="dimmed"';
95 $data[] = "<a$class href=\"view.php?id=$quiz->coursemodule\">" . format_string($quiz->name, true) . '</a>';
97 // Close date.
98 if ($quiz->timeclose) {
99 $data[] = userdate($quiz->timeclose);
100 } else {
101 $data[] = '';
104 if ($showing == 'stats') {
105 // The $quiz objects returned by get_all_instances_in_course have the necessary $cm
106 // fields set to make the following call work.
107 $attemptcount = quiz_num_attempt_summary($quiz, $quiz);
108 if ($attemptcount) {
109 $data[] = "<a$class href=\"report.php?id=$quiz->coursemodule\">$attemptcount</a>";
110 } else {
111 $data[] = '';
113 } else if ($showing == 'scores') {
115 // Grade and feedback.
116 $bestgrade = quiz_get_best_grade($quiz, $USER->id);
117 $attempts = quiz_get_user_attempts($quiz->id, $USER->id, 'all');
118 list($someoptions, $alloptions) = quiz_get_combined_reviewoptions($quiz, $attempts, $context);
120 $grade = '';
121 $feedback = '';
122 if ($quiz->grade && !is_null($bestgrade)) {
123 if ($alloptions->scores) {
124 $grade = "$bestgrade / $quiz->grade";
126 if ($alloptions->overallfeedback) {
127 $feedback = quiz_feedback_for_grade($bestgrade, $quiz->id);
130 $data[] = $grade;
131 $data[] = $feedback;
134 $table->data[] = $data;
135 } // End of loop over quiz instances.
137 // Display the table.
138 echo '<br />';
139 print_table($table);
141 // Finish the page
142 print_footer($course);