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\local\access_rule_base;
18
use mod_quiz\quiz_settings;
19
 
20
/**
21
 * A rule imposing the delay between attempts settings.
22
 *
23
 * @package   quizaccess_delaybetweenattempts
24
 * @copyright 2009 Tim Hunt
25
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26
 */
27
class quizaccess_delaybetweenattempts extends access_rule_base {
28
 
29
    public static function make(quiz_settings $quizobj, $timenow, $canignoretimelimits) {
30
        if (empty($quizobj->get_quiz()->delay1) && empty($quizobj->get_quiz()->delay2)) {
31
            return null;
32
        }
33
 
34
        return new self($quizobj, $timenow);
35
    }
36
 
37
    public function prevent_new_attempt($numprevattempts, $lastattempt) {
38
        if ($this->quiz->attempts > 0 && $numprevattempts >= $this->quiz->attempts) {
39
            // No more attempts allowed anyway.
40
            return false;
41
        }
42
        if ($this->quiz->timeclose != 0 && $this->timenow > $this->quiz->timeclose) {
43
            // No more attempts allowed anyway.
44
            return false;
45
        }
46
        $nextstarttime = $this->compute_next_start_time($numprevattempts, $lastattempt);
47
        if ($this->timenow < $nextstarttime) {
48
            if ($this->quiz->timeclose == 0 || $nextstarttime <= $this->quiz->timeclose) {
49
                return get_string('youmustwait', 'quizaccess_delaybetweenattempts',
50
                        userdate($nextstarttime));
51
            } else {
52
                return get_string('youcannotwait', 'quizaccess_delaybetweenattempts');
53
            }
54
        }
55
        return false;
56
    }
57
 
58
    /**
59
     * Compute the next time a student would be allowed to start an attempt,
60
     * according to this rule.
61
     * @param int $numprevattempts number of previous attempts.
62
     * @param stdClass $lastattempt information about the previous attempt.
63
     * @return number the time.
64
     */
65
    protected function compute_next_start_time($numprevattempts, $lastattempt) {
66
        if ($numprevattempts == 0) {
67
            return 0;
68
        }
69
 
70
        $lastattemptfinish = $lastattempt->timefinish;
71
        if ($this->quiz->timelimit > 0) {
72
            $lastattemptfinish = min($lastattemptfinish,
73
                    $lastattempt->timestart + $this->quiz->timelimit);
74
        }
75
 
76
        if ($numprevattempts == 1 && $this->quiz->delay1) {
77
            return $lastattemptfinish + $this->quiz->delay1;
78
        } else if ($numprevattempts > 1 && $this->quiz->delay2) {
79
            return $lastattemptfinish + $this->quiz->delay2;
80
        }
81
        return 0;
82
    }
83
 
84
    public function is_finished($numprevattempts, $lastattempt) {
85
        $nextstarttime = $this->compute_next_start_time($numprevattempts, $lastattempt);
86
        return $this->timenow <= $nextstarttime &&
87
        $this->quiz->timeclose != 0 && $nextstarttime >= $this->quiz->timeclose;
88
    }
89
}