-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlib.php
More file actions
239 lines (198 loc) · 6.82 KB
/
lib.php
File metadata and controls
239 lines (198 loc) · 6.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Serve question type files
*
* @since 2.6
* @package qtype_calculatedformat
* @copyright Dongsheng Cai <dongsheng@moodle.com>
* @copyright 2014 Daniel P. Seemuth
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Checks file access for calculatedformat questions.
*
* @package qtype_calculatedformat
* @category files
* @param stdClass $course course object
* @param stdClass $cm course module object
* @param stdClass $context context object
* @param string $filearea file area
* @param array $args extra arguments
* @param bool $forcedownload whether or not force download
* @param array $options additional options affecting the file serving
* @return bool
*/
function qtype_calculatedformat_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
global $CFG;
require_once($CFG->libdir . '/questionlib.php');
question_pluginfile($course, $context, 'qtype_calculatedformat', $filearea, $args, $forcedownload, $options);
}
/**
* Display a number properly formatted in a certain base, with a certain
* number of digits before and after the radix point.
* @param number $x the number to format
* @param int $base render number in this base (2, 8, 10, or 16)
* @param int $lengthint expand to this many digits before the radix point
* @param int $lengthfrac restrict to this many digits after the radix point
* @param int $groupdigits optionally separate groups of this many digits
* @param int $showprefix if true, then include base prefix
* @return string formatted number.
*/
function qtype_calculatedformat_format_in_base($x, $base = 10, $lengthint = 1, $lengthfrac = 0, $groupdigits = 0, $showprefix = 0) {
$digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
if (($base != 2) && ($base != 8) && ($base != 10) && ($base != 16)) {
throw new moodle_exception('illegalbase', 'qtype_calculatedformat', $base);
}
$base = intval($base);
$lengthint = intval($lengthint);
$lengthfrac = intval($lengthfrac);
$masklengthint = $lengthint;
if ($lengthint < 1) {
$lengthint = 1;
$masklengthint = 0;
} else if ($lengthint > 64) {
$lengthint = 64;
$masklengthint = 64;
}
if ($lengthfrac < 0) {
$lengthfrac = 0;
} else if ($lengthfrac > 64) {
$lengthfrac = 64;
}
if ($groupdigits < 0) {
$groupdigits = 0;
}
$answer = $x;
$sign = '';
if (($base == 2) || ($base == 8) || ($base == 16)) {
// Mask to exact number of digits, if required.
$answer = qtype_calculatedformat_mask_value(
$answer, $base, $masklengthint, $lengthfrac
);
// Round properly to correct # of digits.
$answer *= pow($base, $lengthfrac);
$answer = intval(round($answer));
} else {
// Convert to positive answer.
if ($answer < 0) {
$answer = -$answer;
$sign = '-';
}
}
if ($base == 2) {
$x = sprintf('%0' . ($lengthint + $lengthfrac) . 'b', $answer);
} else if ($base == 8) {
$x = sprintf('%0' . ($lengthint + $lengthfrac) . 'o', $answer);
} else if ($base == 16) {
$x = sprintf('%0' . ($lengthint + $lengthfrac) . 'X', $answer);
} else {
$width = $lengthint;
if ($lengthfrac > 0) {
// Include fractional digits and decimal point.
$width += $lengthfrac + 1;
}
$x = sprintf('%0' . $width . '.' . $lengthfrac . 'f', $answer);
}
if ($base != 10) {
// Insert radix point if there are fractional digits.
if ($lengthfrac > 0) {
$x = substr_replace($x, '.', -$lengthfrac, 0);
}
}
if (($base == 2) || ($base == 8) || ($base == 16)) {
if ($masklengthint < 1) {
// Strip leading zeros.
$x = ltrim($x, '0');
if (strlen($x) < 1) {
$x = '0';
} else if ($x[0] == '.') {
$x = '0' . $x;
}
}
}
if ($groupdigits > 0) {
$parts = explode('.', $x, 2);
if (count($parts) > 1) {
$integer = $parts[0];
$fraction = $parts[1];
} else {
$integer = $x;
$fraction = '';
}
// Add group separator(s).
$nextgrouppos = strlen($integer) - $groupdigits;
while ($nextgrouppos > 0) {
$integer = substr_replace($integer, '_', $nextgrouppos, 0);
$nextgrouppos -= $groupdigits;
}
if (strlen($fraction) > 0) {
$x = $integer . '.' . $fraction;
} else {
$x = $integer;
}
}
$prefix = '';
if ($showprefix) {
if ($base == 2) {
$prefix = '0b';
} else if ($base == 8) {
$prefix = '0o';
} else if ($base == 10) {
$prefix = '0d';
} else if ($base == 16) {
$prefix = '0x';
}
}
return $sign . $prefix . $x;
}
/**
* Fix a number to exactly the required number of digits in binary, octal, or
* hexadecimal.
* @param number $x the number to fix
* @param int $lengthint restrict to this many digits before the radix point
* @param int $lengthfrac restrict to this many digits after the radix point
* @return number number fitted to required number of digits
*/
function qtype_calculatedformat_mask_value($x, $base, $lengthint, $lengthfrac) {
if (($base != 2) && ($base != 8) && ($base != 16)) {
throw new moodle_exception('illegalbase', 'qtype_calculatedformat', $base);
}
$numbits = 0;
for ($mask = 1; $mask < $base; $mask <<= 1) {
$numbits++;
}
if ($lengthint < 1) {
return $x;
}
$powbase = pow($base, $lengthfrac);
// Round properly to correct # of digits.
$x *= $powbase;
$x = intval(round($x));
$numbits *= ($lengthint + $lengthfrac);
// Construct mask with exact bit length.
$mask = 0;
for ($i = 0; $i < $numbits; $i++) {
$mask <<= 1;
$mask |= 1;
}
// Mask off extra bits.
$x &= $mask;
// Convert back to fractional number.
$x /= $powbase;
return $x;
}