Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
/mod/quiz/ -> index.php (source)

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]

   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  /**
  18   * This script lists all the instances of quiz in a particular course
  19   *
  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
  23   */
  24  
  25  
  26  require_once("../../config.php");
  27  require_once ("locallib.php");
  28  
  29  $id = required_param('id', PARAM_INT);
  30  
  31  $PAGE->set_url('/mod/quiz/index.php', ['id' => $id]);
  32  $course = get_course($id);
  33  $coursecontext = context_course::instance($id);
  34  require_login($course);
  35  $PAGE->set_pagelayout('incourse');
  36  
  37  $params = [
  38      'context' => $coursecontext
  39  ];
  40  $event = \mod_quiz\event\course_module_instance_list_viewed::create($params);
  41  $event->trigger();
  42  
  43  // Print the header.
  44  $strquizzes = get_string("modulenameplural", "quiz");
  45  $PAGE->navbar->add($strquizzes);
  46  $PAGE->set_title($strquizzes);
  47  $PAGE->set_heading($course->fullname);
  48  echo $OUTPUT->header();
  49  echo $OUTPUT->heading($strquizzes, 2);
  50  
  51  // Get all the appropriate data.
  52  if (!$quizzes = get_all_instances_in_course("quiz", $course)) {
  53      notice(get_string('thereareno', 'moodle', $strquizzes), "../../course/view.php?id=$course->id");
  54      die;
  55  }
  56  
  57  // Check if we need the feedback header.
  58  $showfeedback = false;
  59  foreach ($quizzes as $quiz) {
  60      if (quiz_has_feedback($quiz)) {
  61          $showfeedback=true;
  62      }
  63      if ($showfeedback) {
  64          break;
  65      }
  66  }
  67  
  68  // Configure table for displaying the list of instances.
  69  $headings = [get_string('name')];
  70  $align = ['left'];
  71  
  72  array_push($headings, get_string('quizcloses', 'quiz'));
  73  array_push($align, 'left');
  74  
  75  if (course_format_uses_sections($course->format)) {
  76      array_unshift($headings, get_string('sectionname', 'format_'.$course->format));
  77  } else {
  78      array_unshift($headings, '');
  79  }
  80  array_unshift($align, 'center');
  81  
  82  $showing = '';
  83  
  84  if (has_capability('mod/quiz:viewreports', $coursecontext)) {
  85      array_push($headings, get_string('attempts', 'quiz'));
  86      array_push($align, 'left');
  87      $showing = 'stats';
  88  
  89  } else if (has_any_capability(['mod/quiz:reviewmyattempts', 'mod/quiz:attempt'],
  90          $coursecontext)) {
  91      array_push($headings, get_string('grade', 'quiz'));
  92      array_push($align, 'left');
  93      if ($showfeedback) {
  94          array_push($headings, get_string('feedback', 'quiz'));
  95          array_push($align, 'left');
  96      }
  97      $showing = 'grades';
  98  
  99      $grades = $DB->get_records_sql_menu('
 100              SELECT qg.quiz, qg.grade
 101              FROM {quiz_grades} qg
 102              JOIN {quiz} q ON q.id = qg.quiz
 103              WHERE q.course = ? AND qg.userid = ?',
 104              [$course->id, $USER->id]);
 105  }
 106  
 107  $table = new html_table();
 108  $table->head = $headings;
 109  $table->align = $align;
 110  
 111  // Populate the table with the list of instances.
 112  $currentsection = '';
 113  // Get all closing dates.
 114  $timeclosedates = quiz_get_user_timeclose($course->id);
 115  foreach ($quizzes as $quiz) {
 116      $cm = get_coursemodule_from_instance('quiz', $quiz->id);
 117      $context = context_module::instance($cm->id);
 118      $data = [];
 119  
 120      // Section number if necessary.
 121      $strsection = '';
 122      if ($quiz->section != $currentsection) {
 123          if ($quiz->section) {
 124              $strsection = $quiz->section;
 125              $strsection = get_section_name($course, $quiz->section);
 126          }
 127          if ($currentsection !== "") {
 128              $table->data[] = 'hr';
 129          }
 130          $currentsection = $quiz->section;
 131      }
 132      $data[] = $strsection;
 133  
 134      // Link to the instance.
 135      $class = '';
 136      if (!$quiz->visible) {
 137          $class = ' class="dimmed"';
 138      }
 139      $data[] = "<a$class href=\"view.php?id=$quiz->coursemodule\">" .
 140              format_string($quiz->name, true) . '</a>';
 141  
 142      // Close date.
 143      if (($timeclosedates[$quiz->id]->usertimeclose != 0)) {
 144          $data[] = userdate($timeclosedates[$quiz->id]->usertimeclose);
 145      } else {
 146          $data[] = get_string('noclose', 'quiz');
 147      }
 148  
 149      if ($showing == 'stats') {
 150          // The $quiz objects returned by get_all_instances_in_course have the necessary $cm
 151          // fields set to make the following call work.
 152          $data[] = quiz_attempt_summary_link_to_reports($quiz, $cm, $context);
 153  
 154      } else if ($showing == 'grades') {
 155          // Grade and feedback.
 156          $attempts = quiz_get_user_attempts($quiz->id, $USER->id, 'all');
 157          list($someoptions, $alloptions) = quiz_get_combined_reviewoptions(
 158                  $quiz, $attempts);
 159  
 160          $grade = '';
 161          $feedback = '';
 162          if ($quiz->grade && array_key_exists($quiz->id, $grades)) {
 163              if ($alloptions->marks >= question_display_options::MARK_AND_MAX) {
 164                  $a = new stdClass();
 165                  $a->grade = quiz_format_grade($quiz, $grades[$quiz->id]);
 166                  $a->maxgrade = quiz_format_grade($quiz, $quiz->grade);
 167                  $grade = get_string('outofshort', 'quiz', $a);
 168              }
 169              if ($alloptions->overallfeedback) {
 170                  $feedback = quiz_feedback_for_grade($grades[$quiz->id], $quiz, $context);
 171              }
 172          }
 173          $data[] = $grade;
 174          if ($showfeedback) {
 175              $data[] = $feedback;
 176          }
 177      }
 178  
 179      $table->data[] = $data;
 180  } // End of loop over quiz instances.
 181  
 182  // Display the table.
 183  echo html_writer::table($table);
 184  
 185  // Finish the page.
 186  echo $OUTPUT->footer();