function qformat_webct_convert_formula in Quiz 6.5
Same name and namespace in other branches
- 6.6 includes/moodle/question/format/webct/format.php \qformat_webct_convert_formula()
1 call to qformat_webct_convert_formula()
- qformat_webct::readquestions in includes/
moodle/ question/ format/ webct/ format.php - Parses an array of lines into an array of questions, where each item is a question object as defined by readquestion(). Questions are defined as anything between blank lines.
File
- includes/
moodle/ question/ format/ webct/ format.php, line 67
Code
function qformat_webct_convert_formula($formula) {
// Remove empty space, as it would cause problems otherwise:
$formula = str_replace(' ', '', $formula);
// Remove paranthesis after e,E and *10**:
while (ereg('[0-9.](e|E|\\*10\\*\\*)\\([+-]?[0-9]+\\)', $formula, $regs)) {
$formula = str_replace($regs[0], ereg_replace('[)(]', '', $regs[0]), $formula);
}
// Replace *10** with e where possible
while (ereg('(^[+-]?|[^eE][+-]|[^0-9eE+-])[0-9.]+\\*10\\*\\*[+-]?[0-9]+([^0-9.eE]|$)', $formula, $regs)) {
$formula = str_replace($regs[0], str_replace('*10**', 'e', $regs[0]), $formula);
}
// Replace other 10** with 1e where possible
while (ereg('(^|[^0-9.eE])10\\*\\*[+-]?[0-9]+([^0-9.eE]|$)', $formula, $regs)) {
$formula = str_replace($regs[0], str_replace('10**', '1e', $regs[0]), $formula);
}
// Replace all other base**exp with the PHP equivalent function pow(base,exp)
// (Pretty tricky to exchange an operator with a function)
while (2 == count($splits = explode('**', $formula, 2))) {
// Find $base
if (ereg('^(.*[^0-9.eE])?(([0-9]+(\\.[0-9]*)?|\\.[0-9]+)([eE][+-]?[0-9]+)?|\\{[^}]*\\})$', $splits[0], $regs)) {
// The simple cases
$base = $regs[2];
$splits[0] = $regs[1];
}
else {
if (ereg('\\)$', $splits[0])) {
// Find the start of this parenthesis
$deep = 1;
for ($i = 1; $deep; ++$i) {
if (!ereg('^(.*[^[:alnum:]_])?([[:alnum:]_]*([)(])([^)(]*[)(]){' . $i . '})$', $splits[0], $regs)) {
error("Parenthesis before ** is not properly started in {$splits[0]}**");
}
if ('(' == $regs[3]) {
--$deep;
}
else {
if (')' == $regs[3]) {
++$deep;
}
else {
error("Impossible character {$regs[3]} detected as parenthesis character");
}
}
}
$base = $regs[2];
$splits[0] = $regs[1];
}
else {
error("Bad base before **: {$splits[0]}**");
}
}
// Find $exp (similar to above but a little easier)
if (ereg('^([+-]?(\\{[^}]\\}|([0-9]+(\\.[0-9]*)?|\\.[0-9]+)([eE][+-]?[0-9]+)?))(.*)', $splits[1], $regs)) {
// The simple case
$exp = $regs[1];
$splits[1] = $regs[6];
}
else {
if (ereg('^[+-]?[[:alnum:]_]*\\(', $splits[1])) {
// Find the end of the parenthesis
$deep = 1;
for ($i = 1; $deep; ++$i) {
if (!ereg('^([+-]?[[:alnum:]_]*([)(][^)(]*){' . $i . '}([)(]))(.*)', $splits[1], $regs)) {
error("Parenthesis after ** is not properly closed in **{$splits[1]}");
}
if (')' == $regs[3]) {
--$deep;
}
else {
if ('(' == $regs[3]) {
++$deep;
}
else {
error("Impossible character {$regs[3]} detected as parenthesis character");
}
}
}
$exp = $regs[1];
$splits[1] = $regs[4];
}
}
// Replace it!
$formula = "{$splits[0]}pow({$base},{$exp}){$splits[1]}";
}
// Nothing more is known to need to be converted
return $formula;
}