Search moodle.org's
Developer Documentation


  • Bug fixes for general core bugs in 2.8.x ended 9 November 2015 (12 months).
  • Bug fixes for security issues in 2.8.x ended 9 May 2016 (18 months).
  • minimum PHP 5.4.4 (always use latest PHP 5.4.x or 5.5.x on Windows - http://windows.php.net/download/), PHP 7 is NOT supported
  • Differences Between: [Versions 28 and 32] [Versions 28 and 33] [Versions 28 and 34] [Versions 28 and 35] [Versions 28 and 36] [Versions 28 and 37]

       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 page handles editing and creation of quiz overrides
      19   *
      20   * @package   mod_quiz
      21   * @copyright 2010 Matt Petro
      22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
      23   */
      24  
      25  
      26  require_once(dirname(__FILE__) . '/../../config.php');
      27  require_once($CFG->dirroot.'/mod/quiz/lib.php');
      28  require_once($CFG->dirroot.'/mod/quiz/locallib.php');
      29  require_once($CFG->dirroot.'/mod/quiz/override_form.php');
      30  
      31  
      32  $cmid = optional_param('cmid', 0, PARAM_INT);
      33  $overrideid = optional_param('id', 0, PARAM_INT);
      34  $action = optional_param('action', null, PARAM_ALPHA);
      35  $reset = optional_param('reset', false, PARAM_BOOL);
      36  
      37  $override = null;
      38  if ($overrideid) {
      39  
      40      if (! $override = $DB->get_record('quiz_overrides', array('id' => $overrideid))) {
      41          print_error('invalidoverrideid', 'quiz');
      42      }
      43      if (! $quiz = $DB->get_record('quiz', array('id' => $override->quiz))) {
      44          print_error('invalidcoursemodule');
      45      }
      46      list($course, $cm) = get_course_and_cm_from_instance($quiz, 'quiz');
      47  
      48  } else if ($cmid) {
      49      list($course, $cm) = get_course_and_cm_from_cmid($cmid, 'quiz');
      50      $quiz = $DB->get_record('quiz', array('id' => $cm->instance), '*', MUST_EXIST);
      51  
      52  } else {
      53      print_error('invalidcoursemodule');
      54  }
      55  $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
      56  
      57  $url = new moodle_url('/mod/quiz/overrideedit.php');
      58  if ($action) {
      59      $url->param('action', $action);
      60  }
      61  if ($overrideid) {
      62      $url->param('id', $overrideid);
      63  } else {
      64      $url->param('cmid', $cmid);
      65  }
      66  
      67  $PAGE->set_url($url);
      68  
      69  require_login($course, false, $cm);
      70  
      71  $context = context_module::instance($cm->id);
      72  
      73  // Add or edit an override.
      74  require_capability('mod/quiz:manageoverrides', $context);
      75  
      76  if ($overrideid) {
      77      // Editing an override.
      78      $data = clone $override;
      79  } else {
      80      // Creating a new override.
      81      $data = new stdClass();
      82  }
      83  
      84  // Merge quiz defaults with data.
      85  $keys = array('timeopen', 'timeclose', 'timelimit', 'attempts', 'password');
      86  foreach ($keys as $key) {
      87      if (!isset($data->{$key}) || $reset) {
      88          $data->{$key} = $quiz->{$key};
      89      }
      90  }
      91  
      92  // If we are duplicating an override, then clear the user/group and override id
      93  // since they will change.
      94  if ($action === 'duplicate') {
      95      $override->id = null;
      96      $override->userid = null;
      97      $override->groupid = null;
      98  }
      99  
     100  // True if group-based override.
     101  $groupmode = !empty($data->groupid) || ($action === 'addgroup' && empty($overrideid));
     102  
     103  $overridelisturl = new moodle_url('/mod/quiz/overrides.php', array('cmid'=>$cm->id));
     104  if (!$groupmode) {
     105      $overridelisturl->param('mode', 'user');
     106  }
     107  
     108  // Setup the form.
     109  $mform = new quiz_override_form($url, $cm, $quiz, $context, $groupmode, $override);
     110  $mform->set_data($data);
     111  
     112  if ($mform->is_cancelled()) {
     113      redirect($overridelisturl);
     114  
     115  } else if (optional_param('resetbutton', 0, PARAM_ALPHA)) {
     116      $url->param('reset', true);
     117      redirect($url);
     118  
     119  } else if ($fromform = $mform->get_data()) {
     120      // Process the data.
     121      $fromform->quiz = $quiz->id;
     122  
     123      // Replace unchanged values with null.
     124      foreach ($keys as $key) {
     125          if ($fromform->{$key} == $quiz->{$key}) {
     126              $fromform->{$key} = null;
     127          }
     128      }
     129  
     130      // See if we are replacing an existing override.
     131      $userorgroupchanged = false;
     132      if (empty($override->id)) {
     133          $userorgroupchanged = true;
     134      } else if (!empty($fromform->userid)) {
     135          $userorgroupchanged = $fromform->userid !== $override->userid;
     136      } else {
     137          $userorgroupchanged = $fromform->groupid !== $override->groupid;
     138      }
     139  
     140      if ($userorgroupchanged) {
     141          $conditions = array(
     142                  'quiz' => $quiz->id,
     143                  'userid' => empty($fromform->userid)? null : $fromform->userid,
     144                  'groupid' => empty($fromform->groupid)? null : $fromform->groupid);
     145          if ($oldoverride = $DB->get_record('quiz_overrides', $conditions)) {
     146              // There is an old override, so we merge any new settings on top of
     147              // the older override.
     148              foreach ($keys as $key) {
     149                  if (is_null($fromform->{$key})) {
     150                      $fromform->{$key} = $oldoverride->{$key};
     151                  }
     152              }
     153              // Set the course module id before calling quiz_delete_override().
     154              $quiz->cmid = $cm->id;
     155              quiz_delete_override($quiz, $oldoverride->id);
     156          }
     157      }
     158  
     159      // Set the common parameters for one of the events we may be triggering.
     160      $params = array(
     161          'context' => $context,
     162          'other' => array(
     163              'quizid' => $quiz->id
     164          )
     165      );
     166      if (!empty($override->id)) {
     167          $fromform->id = $override->id;
     168          $DB->update_record('quiz_overrides', $fromform);
     169  
     170          // Determine which override updated event to fire.
     171          $params['objectid'] = $override->id;
     172          if (!$groupmode) {
     173              $params['relateduserid'] = $fromform->userid;
     174              $event = \mod_quiz\event\user_override_updated::create($params);
     175          } else {
     176              $params['other']['groupid'] = $fromform->groupid;
     177              $event = \mod_quiz\event\group_override_updated::create($params);
     178          }
     179  
     180          // Trigger the override updated event.
     181          $event->trigger();
     182      } else {
     183          unset($fromform->id);
     184          $fromform->id = $DB->insert_record('quiz_overrides', $fromform);
     185  
     186          // Determine which override created event to fire.
     187          $params['objectid'] = $fromform->id;
     188          if (!$groupmode) {
     189              $params['relateduserid'] = $fromform->userid;
     190              $event = \mod_quiz\event\user_override_created::create($params);
     191          } else {
     192              $params['other']['groupid'] = $fromform->groupid;
     193              $event = \mod_quiz\event\group_override_created::create($params);
     194          }
     195  
     196          // Trigger the override created event.
     197          $event->trigger();
     198      }
     199  
     200      quiz_update_open_attempts(array('quizid'=>$quiz->id));
     201      quiz_update_events($quiz, $fromform);
     202  
     203      if (!empty($fromform->submitbutton)) {
     204          redirect($overridelisturl);
     205      }
     206  
     207      // The user pressed the 'again' button, so redirect back to this page.
     208      $url->remove_params('cmid');
     209      $url->param('action', 'duplicate');
     210      $url->param('id', $fromform->id);
     211      redirect($url);
     212  
     213  }
     214  
     215  // Print the form.
     216  $pagetitle = get_string('editoverride', 'quiz');
     217  $PAGE->navbar->add($pagetitle);
     218  $PAGE->set_pagelayout('admin');
     219  $PAGE->set_title($pagetitle);
     220  $PAGE->set_heading($course->fullname);
     221  echo $OUTPUT->header();
     222  echo $OUTPUT->heading(format_string($quiz->name, true, array('context' => $context)));
     223  
     224  $mform->display();
     225  
     226  echo $OUTPUT->footer();
    

    Search This Site: