[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/quiz/report/ -> attemptsreport.php (source)

   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   * The file defines a base class that can be used to build a report like the
  19   * overview or responses report, that has one row per attempt.
  20   *
  21   * @package   mod_quiz
  22   * @copyright 2010 The Open University
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  require_once($CFG->libdir.'/tablelib.php');
  30  
  31  
  32  /**
  33   * Base class for quiz reports that are basically a table with one row for each attempt.
  34   *
  35   * @copyright 2010 The Open University
  36   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  abstract class quiz_attempts_report extends quiz_default_report {
  39      /** @var int default page size for reports. */
  40      const DEFAULT_PAGE_SIZE = 30;
  41  
  42      /** @var string constant used for the options, means all users with attempts. */
  43      const ALL_WITH = 'all_with';
  44      /** @var string constant used for the options, means only enrolled users with attempts. */
  45      const ENROLLED_WITH = 'enrolled_with';
  46      /** @var string constant used for the options, means only enrolled users without attempts. */
  47      const ENROLLED_WITHOUT = 'enrolled_without';
  48      /** @var string constant used for the options, means all enrolled users. */
  49      const ENROLLED_ALL = 'enrolled_any';
  50  
  51      /** @var string the mode this report is. */
  52      protected $mode;
  53  
  54      /** @var object the quiz context. */
  55      protected $context;
  56  
  57      /** @var mod_quiz_attempts_report_form The settings form to use. */
  58      protected $form;
  59  
  60      /** @var string SQL fragment for selecting the attempt that gave the final grade,
  61       * if applicable. */
  62      protected $qmsubselect;
  63  
  64      /** @var boolean caches the results of {@link should_show_grades()}. */
  65      protected $showgrades = null;
  66  
  67      /**
  68       *  Initialise various aspects of this report.
  69       *
  70       * @param string $mode
  71       * @param string $formclass
  72       * @param object $quiz
  73       * @param object $cm
  74       * @param object $course
  75       */
  76      protected function init($mode, $formclass, $quiz, $cm, $course) {
  77          $this->mode = $mode;
  78  
  79          $this->context = context_module::instance($cm->id);
  80  
  81          list($currentgroup, $students, $groupstudents, $allowed) =
  82                  $this->load_relevant_students($cm, $course);
  83  
  84          $this->qmsubselect = quiz_report_qm_filter_select($quiz);
  85  
  86          $this->form = new $formclass($this->get_base_url(),
  87                  array('quiz' => $quiz, 'currentgroup' => $currentgroup, 'context' => $this->context));
  88  
  89          return array($currentgroup, $students, $groupstudents, $allowed);
  90      }
  91  
  92      /**
  93       * Get the base URL for this report.
  94       * @return moodle_url the URL.
  95       */
  96      protected function get_base_url() {
  97          return new moodle_url('/mod/quiz/report.php',
  98                  array('id' => $this->context->instanceid, 'mode' => $this->mode));
  99      }
 100  
 101      /**
 102       * Get information about which students to show in the report.
 103       * @param object $cm the coures module.
 104       * @param object $course the course settings.
 105       * @return array with four elements:
 106       *      0 => integer the current group id (0 for none).
 107       *      1 => array ids of all the students in this course.
 108       *      2 => array ids of all the students in the current group.
 109       *      3 => array ids of all the students to show in the report. Will be the
 110       *              same as either element 1 or 2.
 111       */
 112      protected function load_relevant_students($cm, $course = null) {
 113          $currentgroup = $this->get_current_group($cm, $course, $this->context);
 114  
 115          if ($currentgroup == self::NO_GROUPS_ALLOWED) {
 116              return array($currentgroup, array(), array(), array());
 117          }
 118  
 119          if (!$students = get_users_by_capability($this->context,
 120                  array('mod/quiz:reviewmyattempts', 'mod/quiz:attempt'),
 121                  'u.id, 1', '', '', '', '', '', false)) {
 122              $students = array();
 123          } else {
 124              $students = array_keys($students);
 125          }
 126  
 127          if (empty($currentgroup)) {
 128              return array($currentgroup, $students, array(), $students);
 129          }
 130  
 131          // We have a currently selected group.
 132          if (!$groupstudents = get_users_by_capability($this->context,
 133                  array('mod/quiz:reviewmyattempts', 'mod/quiz:attempt'),
 134                  'u.id, 1', '', '', '', $currentgroup, '', false)) {
 135              $groupstudents = array();
 136          } else {
 137              $groupstudents = array_keys($groupstudents);
 138          }
 139  
 140          return array($currentgroup, $students, $groupstudents, $groupstudents);
 141      }
 142  
 143      /**
 144       * Add all the user-related columns to the $columns and $headers arrays.
 145       * @param table_sql $table the table being constructed.
 146       * @param array $columns the list of columns. Added to.
 147       * @param array $headers the columns headings. Added to.
 148       */
 149      protected function add_user_columns($table, &$columns, &$headers) {
 150          global $CFG;
 151          if (!$table->is_downloading() && $CFG->grade_report_showuserimage) {
 152              $columns[] = 'picture';
 153              $headers[] = '';
 154          }
 155          if (!$table->is_downloading()) {
 156              $columns[] = 'fullname';
 157              $headers[] = get_string('name');
 158          } else {
 159              $columns[] = 'lastname';
 160              $headers[] = get_string('lastname');
 161              $columns[] = 'firstname';
 162              $headers[] = get_string('firstname');
 163          }
 164  
 165          // When downloading, some extra fields are always displayed (because
 166          // there's no space constraint) so do not include in extra-field list.
 167          $extrafields = get_extra_user_fields($this->context,
 168                  $table->is_downloading() ? array('institution', 'department', 'email') : array());
 169          foreach ($extrafields as $field) {
 170              $columns[] = $field;
 171              $headers[] = get_user_field_name($field);
 172          }
 173  
 174          if ($table->is_downloading()) {
 175              $columns[] = 'institution';
 176              $headers[] = get_string('institution');
 177  
 178              $columns[] = 'department';
 179              $headers[] = get_string('department');
 180  
 181              $columns[] = 'email';
 182              $headers[] = get_string('email');
 183          }
 184      }
 185  
 186      /**
 187       * Set the display options for the user-related columns in the table.
 188       * @param table_sql $table the table being constructed.
 189       */
 190      protected function configure_user_columns($table) {
 191          $table->column_suppress('picture');
 192          $table->column_suppress('fullname');
 193          $table->column_suppress('idnumber');
 194  
 195          $table->column_class('picture', 'picture');
 196          $table->column_class('lastname', 'bold');
 197          $table->column_class('firstname', 'bold');
 198          $table->column_class('fullname', 'bold');
 199      }
 200  
 201      /**
 202       * Add the state column to the $columns and $headers arrays.
 203       * @param array $columns the list of columns. Added to.
 204       * @param array $headers the columns headings. Added to.
 205       */
 206      protected function add_state_column(&$columns, &$headers) {
 207          $columns[] = 'state';
 208          $headers[] = get_string('attemptstate', 'quiz');
 209      }
 210  
 211      /**
 212       * Add all the time-related columns to the $columns and $headers arrays.
 213       * @param array $columns the list of columns. Added to.
 214       * @param array $headers the columns headings. Added to.
 215       */
 216      protected function add_time_columns(&$columns, &$headers) {
 217          $columns[] = 'timestart';
 218          $headers[] = get_string('startedon', 'quiz');
 219  
 220          $columns[] = 'timefinish';
 221          $headers[] = get_string('timecompleted', 'quiz');
 222  
 223          $columns[] = 'duration';
 224          $headers[] = get_string('attemptduration', 'quiz');
 225      }
 226  
 227      /**
 228       * Add all the grade and feedback columns, if applicable, to the $columns
 229       * and $headers arrays.
 230       * @param object $quiz the quiz settings.
 231       * @param bool $usercanseegrades whether the user is allowed to see grades for this quiz.
 232       * @param array $columns the list of columns. Added to.
 233       * @param array $headers the columns headings. Added to.
 234       * @param bool $includefeedback whether to include the feedbacktext columns
 235       */
 236      protected function add_grade_columns($quiz, $usercanseegrades, &$columns, &$headers, $includefeedback = true) {
 237          if ($usercanseegrades) {
 238              $columns[] = 'sumgrades';
 239              $headers[] = get_string('grade', 'quiz') . '/' .
 240                      quiz_format_grade($quiz, $quiz->grade);
 241          }
 242  
 243          if ($includefeedback && quiz_has_feedback($quiz)) {
 244              $columns[] = 'feedbacktext';
 245              $headers[] = get_string('feedback', 'quiz');
 246          }
 247      }
 248  
 249      /**
 250       * Set up the table.
 251       * @param table_sql $table the table being constructed.
 252       * @param array $columns the list of columns.
 253       * @param array $headers the columns headings.
 254       * @param moodle_url $reporturl the URL of this report.
 255       * @param mod_quiz_attempts_report_options $options the display options.
 256       * @param bool $collapsible whether to allow columns in the report to be collapsed.
 257       */
 258      protected function set_up_table_columns($table, $columns, $headers, $reporturl,
 259              mod_quiz_attempts_report_options $options, $collapsible) {
 260          $table->define_columns($columns);
 261          $table->define_headers($headers);
 262          $table->sortable(true, 'uniqueid');
 263  
 264          $table->define_baseurl($options->get_url());
 265  
 266          $this->configure_user_columns($table);
 267  
 268          $table->no_sorting('feedbacktext');
 269          $table->column_class('sumgrades', 'bold');
 270  
 271          $table->set_attribute('id', 'attempts');
 272  
 273          $table->collapsible($collapsible);
 274      }
 275  
 276      /**
 277       * Process any submitted actions.
 278       * @param object $quiz the quiz settings.
 279       * @param object $cm the cm object for the quiz.
 280       * @param int $currentgroup the currently selected group.
 281       * @param array $groupstudents the students in the current group.
 282       * @param array $allowed the users whose attempt this user is allowed to modify.
 283       * @param moodle_url $redirecturl where to redircet to after a successful action.
 284       */
 285      protected function process_actions($quiz, $cm, $currentgroup, $groupstudents, $allowed, $redirecturl) {
 286          if (empty($currentgroup) || $groupstudents) {
 287              if (optional_param('delete', 0, PARAM_BOOL) && confirm_sesskey()) {
 288                  if ($attemptids = optional_param_array('attemptid', array(), PARAM_INT)) {
 289                      require_capability('mod/quiz:deleteattempts', $this->context);
 290                      $this->delete_selected_attempts($quiz, $cm, $attemptids, $allowed);
 291                      redirect($redirecturl);
 292                  }
 293              }
 294          }
 295      }
 296  
 297      /**
 298       * Delete the quiz attempts
 299       * @param object $quiz the quiz settings. Attempts that don't belong to
 300       * this quiz are not deleted.
 301       * @param object $cm the course_module object.
 302       * @param array $attemptids the list of attempt ids to delete.
 303       * @param array $allowed This list of userids that are visible in the report.
 304       *      Users can only delete attempts that they are allowed to see in the report.
 305       *      Empty means all users.
 306       */
 307      protected function delete_selected_attempts($quiz, $cm, $attemptids, $allowed) {
 308          global $DB;
 309  
 310          foreach ($attemptids as $attemptid) {
 311              $attempt = $DB->get_record('quiz_attempts', array('id' => $attemptid));
 312              if (!$attempt || $attempt->quiz != $quiz->id || $attempt->preview != 0) {
 313                  // Ensure the attempt exists, and belongs to this quiz. If not skip.
 314                  continue;
 315              }
 316              if ($allowed && !in_array($attempt->userid, $allowed)) {
 317                  // Ensure the attempt belongs to a student included in the report. If not skip.
 318                  continue;
 319              }
 320  
 321              // Set the course module id before calling quiz_delete_attempt().
 322              $quiz->cmid = $cm->id;
 323              quiz_delete_attempt($attempt, $quiz);
 324          }
 325      }
 326  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1