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

FormulasQuestion / moodle-qtype_formulas / 13214470503

08 Feb 2025 09:42AM UTC coverage: 76.899% (+1.9%) from 75.045%
13214470503

Pull #62

github

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

2547 of 3139 new or added lines in 22 files covered. (81.14%)

146 existing lines in 6 files now uncovered.

3006 of 3909 relevant lines covered (76.9%)

438.29 hits per line

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

69.7
/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.
65
        if ($this->shuffle) {
85✔
66
            $this->type = variable::LIST;
17✔
67
            $this->value = $this->reservoir;
17✔
68
            shuffle($this->value);
17✔
69
        } else {
70
            $i = mt_rand(0, count($this->reservoir) - 1);
68✔
71
            $this->value = $this->reservoir[$i]->value;
68✔
72
            $this->type = $this->reservoir[$i]->type;
68✔
73
        }
74
        return token::wrap($this->value, $this->type);
85✔
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

95
    /**
96
     * Return a string that can be used to set the variable to its instantiated value.
97
     * This is how Formulas question prior to version 6.x used to store their state.
98
     * We implement this for maximum backwards compatibility, i. e. in order to allow
99
     * switching back to a 5.x version. We continue to use the old format, i. e.
100
     * <variablename> = <instantiated-value>; for every random variable.
101
     *
102
     * @return string
103
     */
104
    public function get_instantiated_definition(): string {
105
        if ($this->value === null) {
85✔
NEW
106
            return '';
×
107
        }
108
        $definition = $this->name . '=';
85✔
109

110
        // If the value is a string, we wrap it in quotes. Also, we remove all existing quotes inside
111
        // the string, because in Formulas question < 6.0, it was not possible to have escaped quotes
112
        // in a string.
113
        if (is_string($this->value)) {
85✔
114
            return $definition . '"' . preg_replace('/(\\\\)?["\']/', '', $this->value) . '";';
17✔
115
        }
116

117
        // If the value is a number, we return it as it is. We do this after the string, because numeric
118
        // strings should be returned as strings, not numbers.
119
        if (is_numeric($this->value)) {
68✔
120
            return $definition . $this->value . ';';
17✔
121
        }
122

123
        // If we are still here, the value is an array. We iterate over all entries and, if necessary,
124
        // wrap them in quotes. Note that these individual values are all tokens.
125
        $values = [];
51✔
126
        foreach ($this->value as $valuetoken) {
51✔
127
            if (is_string($valuetoken->value)) {
51✔
128
                $values[] = '"' . preg_replace('/(\\\\)?["\']/', '', $valuetoken->value) . '"';
17✔
129
                continue;
17✔
130
            }
131
            if (is_numeric($valuetoken->value)) {
34✔
132
                $values[] = $valuetoken->value;
34✔
133
                continue;
34✔
134
            }
135
            // If we are still here, the element is itself an array (a list). Nested lists are not
136
            // allowed in legacy versions of Formulas question, so we simply drop the value.
137
        }
138

139
        return $definition . '[' . implode(',', $values) . '];';
51✔
140
    }
141
}
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