Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   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   * Implementaton of the quizaccess_offlineattempts plugin.
  19   *
  20   * @package    quizaccess_offlineattempts
  21   * @copyright  2016 Juan Leyva
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  require_once($CFG->dirroot . '/mod/quiz/accessrule/accessrulebase.php');
  29  
  30  /**
  31   * A rule implementing the offlineattempts check.
  32   *
  33   * @copyright  2016 Juan Leyva
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   * @since      Moodle 3.2
  36   */
  37  class quizaccess_offlineattempts extends quiz_access_rule_base {
  38  
  39      public static function make(quiz $quizobj, $timenow, $canignoretimelimits) {
  40          global $CFG;
  41  
  42          // If mobile services are off, the user won't be able to use any external app.
  43          if (empty($CFG->enablemobilewebservice) or empty($quizobj->get_quiz()->allowofflineattempts)) {
  44              return null;
  45          }
  46  
  47          return new self($quizobj, $timenow);
  48      }
  49  
  50      public function is_preflight_check_required($attemptid) {
  51          global $SESSION, $DB;
  52  
  53          // First, check if the user did something offline.
  54          if (!empty($attemptid)) {
  55              $timemodifiedoffline = $DB->get_field('quiz_attempts', 'timemodifiedoffline', array('id' => $attemptid));
  56              if (empty($timemodifiedoffline)) {
  57                  return false;
  58              }
  59              return empty($SESSION->offlineattemptscheckedquizzes[$this->quiz->id]);
  60          } else {
  61              // Starting a new attempt, we don't have to check anything here.
  62              return false;
  63          }
  64      }
  65  
  66      public function add_preflight_check_form_fields(mod_quiz_preflight_check_form $quizform,
  67              MoodleQuickForm $mform, $attemptid) {
  68          global $DB;
  69  
  70          $timemodifiedoffline = $DB->get_field('quiz_attempts', 'timemodifiedoffline', array('id' => $attemptid));
  71          $lasttime = format_time(time() - $timemodifiedoffline);
  72  
  73          $mform->addElement('header', 'offlineattemptsheader', get_string('mobileapp', 'quizaccess_offlineattempts'));
  74          $mform->addElement('static', 'offlinedatamessage', '',
  75                  get_string('offlinedatamessage', 'quizaccess_offlineattempts', $lasttime));
  76          $mform->addElement('advcheckbox', 'confirmdatasaved', null,
  77                  get_string('confirmdatasaved', 'quizaccess_offlineattempts'));
  78      }
  79  
  80      public function validate_preflight_check($data, $files, $errors, $attemptid) {
  81  
  82          // The user confirmed that he doesn't have unsaved work.
  83          if (!empty($data['confirmdatasaved'])) {
  84              return $errors;
  85          }
  86  
  87          $errors['confirmdatasaved'] = get_string('pleaseconfirm', 'quizaccess_offlineattempts');
  88          return $errors;
  89      }
  90  
  91      public function notify_preflight_check_passed($attemptid) {
  92          global $SESSION;
  93          $SESSION->offlineattemptscheckedquizzes[$this->quiz->id] = true;
  94      }
  95  
  96      public function current_attempt_finished() {
  97          global $SESSION;
  98          // Clear the flag in the session that says that the user has already agreed to the notice.
  99          if (!empty($SESSION->offlineattemptscheckedquizzes[$this->quiz->id])) {
 100              unset($SESSION->offlineattemptscheckedquizzes[$this->quiz->id]);
 101          }
 102      }
 103  
 104      public static function add_settings_form_fields(
 105              mod_quiz_mod_form $quizform, MoodleQuickForm $mform) {
 106          global $CFG;
 107  
 108          // Allow to enable the access rule only if the Mobile services are enabled.
 109          if ($CFG->enablemobilewebservice) {
 110              $mform->addElement('selectyesno', 'allowofflineattempts',
 111                                  get_string('allowofflineattempts', 'quizaccess_offlineattempts'));
 112              $mform->addHelpButton('allowofflineattempts', 'allowofflineattempts', 'quizaccess_offlineattempts');
 113              $mform->setDefault('allowofflineattempts', 0);
 114              $mform->setAdvanced('allowofflineattempts');
 115              $mform->disabledIf('allowofflineattempts', 'timelimit[number]', 'neq', 0);
 116              $mform->disabledIf('allowofflineattempts', 'subnet', 'neq', '');
 117          }
 118      }
 119  
 120      public static function validate_settings_form_fields(array $errors,
 121              array $data, $files, mod_quiz_mod_form $quizform) {
 122          global $CFG;
 123  
 124          if ($CFG->enablemobilewebservice) {
 125              // Do not allow offline attempts if:
 126              // - The quiz uses a timer.
 127              // - The quiz is restricted by subnet.
 128              // - The question behaviour is not deferred feedback or deferred feedback with CBM.
 129              if (!empty($data['allowofflineattempts']) and
 130                      (!empty($data['timelimit']) or !empty($data['subnet']) or
 131                      ($data['preferredbehaviour'] != 'deferredfeedback' and $data['preferredbehaviour'] != 'deferredcbm'))) {
 132  
 133                  $errors['allowofflineattempts'] = get_string('offlineattemptserror', 'quizaccess_offlineattempts');
 134              }
 135          }
 136  
 137          return $errors;
 138      }
 139  }