PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/quiz/index.php

https://github.com/kpike/moodle
PHP | 196 lines | 135 code | 26 blank | 35 comment | 30 complexity | 9a4fa5e3cc4a1618a71b16714d060eec MD5 | raw file
  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/>.
  16. /**
  17. * This script lists all the instances of quiz in a particular course
  18. *
  19. * @package mod
  20. * @subpackage 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
  23. */
  24. require_once("../../config.php");
  25. require_once("locallib.php");
  26. $id = required_param('id', PARAM_INT);
  27. $PAGE->set_url('/mod/quiz/index.php', array('id'=>$id));
  28. if (!$course = $DB->get_record('course', array('id' => $id))) {
  29. print_error('invalidcourseid');
  30. }
  31. $coursecontext = get_context_instance(CONTEXT_COURSE, $id);
  32. require_login($course->id);
  33. $PAGE->set_pagelayout('incourse');
  34. add_to_log($course->id, "quiz", "view all", "index.php?id=$course->id", "");
  35. // Print the header
  36. $strquizzes = get_string("modulenameplural", "quiz");
  37. $streditquestions = '';
  38. $editqcontexts = new question_edit_contexts($coursecontext);
  39. if ($editqcontexts->have_one_edit_tab_cap('questions')) {
  40. $streditquestions =
  41. "<form target=\"_parent\" method=\"get\" action=\"$CFG->wwwroot/question/edit.php\">
  42. <div>
  43. <input type=\"hidden\" name=\"courseid\" value=\"$course->id\" />
  44. <input type=\"submit\" value=\"".get_string("editquestions", "quiz")."\" />
  45. </div>
  46. </form>";
  47. }
  48. $PAGE->navbar->add($strquizzes);
  49. $PAGE->set_title($strquizzes);
  50. $PAGE->set_button($streditquestions);
  51. $PAGE->set_heading($course->fullname);
  52. echo $OUTPUT->header();
  53. // Get all the appropriate data
  54. if (!$quizzes = get_all_instances_in_course("quiz", $course)) {
  55. notice(get_string('thereareno', 'moodle', $strquizzes), "../../course/view.php?id=$course->id");
  56. die;
  57. }
  58. $sections = get_all_sections($course->id);
  59. // Check if we need the closing date header
  60. $showclosingheader = false;
  61. $showfeedback = false;
  62. foreach ($quizzes as $quiz) {
  63. if ($quiz->timeclose!=0) {
  64. $showclosingheader=true;
  65. }
  66. if (quiz_has_feedback($quiz)) {
  67. $showfeedback=true;
  68. }
  69. if ($showclosingheader && $showfeedback) {
  70. break;
  71. }
  72. }
  73. // Configure table for displaying the list of instances.
  74. $headings = array(get_string('name'));
  75. $align = array('left');
  76. if ($showclosingheader) {
  77. array_push($headings, get_string('quizcloses', 'quiz'));
  78. array_push($align, 'left');
  79. }
  80. array_unshift($headings, get_string('sectionname', 'format_'.$course->format));
  81. array_unshift($align, 'center');
  82. $showing = ''; // default
  83. if (has_capability('mod/quiz:viewreports', $coursecontext)) {
  84. array_push($headings, get_string('attempts', 'quiz'));
  85. array_push($align, 'left');
  86. $showing = 'stats';
  87. } else if (has_any_capability(array('mod/quiz:reviewmyattempts', 'mod/quiz:attempt'),
  88. $coursecontext)) {
  89. array_push($headings, get_string('grade', 'quiz'));
  90. array_push($align, 'left');
  91. if ($showfeedback) {
  92. array_push($headings, get_string('feedback', 'quiz'));
  93. array_push($align, 'left');
  94. }
  95. $showing = 'grades'; // default
  96. $grades = $DB->get_records_sql_menu('
  97. SELECT qg.quiz, qg.grade
  98. FROM {quiz_grades} qg
  99. JOIN {quiz} q ON q.id = qg.quiz
  100. WHERE q.course = ? AND qg.userid = ?',
  101. array($course->id, $USER->id));
  102. }
  103. $table = new html_table();
  104. $table->head = $headings;
  105. $table->align = $align;
  106. // Populate the table with the list of instances.
  107. $currentsection = '';
  108. foreach ($quizzes as $quiz) {
  109. $cm = get_coursemodule_from_instance('quiz', $quiz->id);
  110. $context = get_context_instance(CONTEXT_MODULE, $cm->id);
  111. $data = array();
  112. // Section number if necessary.
  113. $strsection = '';
  114. if ($quiz->section != $currentsection) {
  115. if ($quiz->section) {
  116. $strsection = $quiz->section;
  117. $strsection = get_section_name($course, $sections[$quiz->section]);
  118. }
  119. if ($currentsection) {
  120. $learningtable->data[] = 'hr';
  121. }
  122. $currentsection = $quiz->section;
  123. }
  124. $data[] = $strsection;
  125. // Link to the instance.
  126. $class = '';
  127. if (!$quiz->visible) {
  128. $class = ' class="dimmed"';
  129. }
  130. $data[] = "<a$class href=\"view.php?id=$quiz->coursemodule\">" .
  131. format_string($quiz->name, true) . '</a>';
  132. // Close date.
  133. if ($quiz->timeclose) {
  134. $data[] = userdate($quiz->timeclose);
  135. } else if ($showclosingheader) {
  136. $data[] = '';
  137. }
  138. if ($showing == 'stats') {
  139. // The $quiz objects returned by get_all_instances_in_course have the necessary $cm
  140. // fields set to make the following call work.
  141. $data[] = quiz_attempt_summary_link_to_reports($quiz, $cm, $context);
  142. } else if ($showing == 'grades') {
  143. // Grade and feedback.
  144. $attempts = quiz_get_user_attempts($quiz->id, $USER->id, 'all');
  145. list($someoptions, $alloptions) = quiz_get_combined_reviewoptions(
  146. $quiz, $attempts, $context);
  147. $grade = '';
  148. $feedback = '';
  149. if ($quiz->grade && array_key_exists($quiz->id, $grades)) {
  150. if ($alloptions->marks >= question_display_options::MARK_AND_MAX) {
  151. $a = new stdClass();
  152. $a->grade = quiz_format_grade($quiz, $grades[$quiz->id]);
  153. $a->maxgrade = quiz_format_grade($quiz, $quiz->grade);
  154. $grade = get_string('outofshort', 'quiz', $a);
  155. }
  156. if ($alloptions->overallfeedback) {
  157. $feedback = quiz_feedback_for_grade($grades[$quiz->id], $quiz, $context);
  158. }
  159. }
  160. $data[] = $grade;
  161. if ($showfeedback) {
  162. $data[] = $feedback;
  163. }
  164. }
  165. $table->data[] = $data;
  166. } // End of loop over quiz instances.
  167. // Display the table.
  168. echo html_writer::table($table);
  169. // Finish the page
  170. echo $OUTPUT->footer();