Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

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