• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

FormulasQuestion / moodle-qtype_formulas / 13200038469

07 Feb 2025 12:40PM UTC coverage: 76.583% (+1.5%) from 75.045%
13200038469

Pull #62

github

web-flow
Merge 27bf7cac9 into acd272945
Pull Request #62: Rewrite the parser

2517 of 3116 new or added lines in 22 files covered. (80.78%)

146 existing lines in 6 files now uncovered.

2976 of 3886 relevant lines covered (76.58%)

431.97 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/classes/local/random_variable.php
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
namespace qtype_formulas\local;
18
use Exception;
19

20
/**
21
 * Parser for qtype_formulas
22
 *
23
 * @package    qtype_formulas
24
 * @copyright  2022 Philipp Imhof
25
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26
 */
27
class random_variable extends variable {
28
    /** @var string the identifier used to refer to this variable */
29
    public string $name;
30

31
    /** @var array the set of possible values to choose from */
32
    public array $reservoir = [];
33

34
    /** @var int the variable's data type */
35
    public int $type;
36

37
    /** @var mixed the variable's content */
38
    public $value = null;
39

40
    /** @var bool if the variable is a shuffled array */
41
    private bool $shuffle;
42

43
    /**
44
     * Constructor.
45
     *
46
     * @param string $name identifier used to refer to this variable
47
     * @param array $reservoir set of possible values to choose from
48
     * @param bool $useshuffle whether the variable is a shuffled array
49
     * @param int $seed the seed for the PRNG
50
     */
51
    public function __construct(string $name, array $reservoir, bool $useshuffle, int $seed = 1) {
NEW
52
        $this->name = $name;
×
NEW
53
        $this->shuffle = $useshuffle;
×
NEW
54
        $this->reservoir = $reservoir;
×
55
    }
56

57
    /**
58
     * Instantiate the random variable, e. g. assigning one of the possible values to it.
59
     *
60
     * @return token assigned value
61
     */
62
    public function instantiate(): token {
63
        // We have two types of random variables. One is a list that will be shuffled.
64
        // The other is a set where we pick one random element.
NEW
65
        if ($this->shuffle) {
×
NEW
66
            $this->type = variable::LIST;
×
NEW
67
            $this->value = $this->reservoir;
×
NEW
68
            shuffle($this->value);
×
69
        } else {
NEW
70
            $i = mt_rand(0, count($this->reservoir) - 1);
×
NEW
71
            $this->value = $this->reservoir[$i]->value;
×
NEW
72
            $this->type = $this->reservoir[$i]->type;
×
73
        }
NEW
74
        return token::wrap($this->value, $this->type);
×
75
    }
76

77
    /**
78
     * Calculate the number of possible values for this random variable.
79
     *
80
     * @return int
81
     */
82
    public function how_many(): int {
NEW
83
        if ($this->shuffle) {
×
84
            try {
NEW
85
                $result = functions::fact(count($this->reservoir));
×
NEW
86
            } catch (Exception $e) {
×
87
                // TODO: non-capturing catch.
NEW
88
                return PHP_INT_MAX;
×
89
            }
NEW
90
            return $result;
×
91
        }
NEW
92
        return count($this->reservoir);
×
93
    }
94
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc