Quote from: Taff at May 08, 2008, 04:41 AM
That Maths idea is great, have you documented how you did it anywhere?
I did it a while ago but here’s the gist of it. I abstracted the veriword class from manager/includes and put it in a separate file. Then I modified it to accept an argument. If the argument is "mathstring" it uses the following mathstring class. If the argument is null, it picks from the default words. If the argument is anything else, it uses the argument for the display string.
Here’s the code that actually creates the math string and calculates the correct answer:
class MathString {
var $_displayString="";
var $_value;
function MathString($s1="34",$s2="312") { // Constructor creates the math expression as a string
$i1 = rand(0,1);
$i2 = rand(0,2);
$operators = "+-x";
$this->_displayString = substr($s1,$i1,1).' '.substr($operators,rand(0,2),1).' '.substr($s2,$i2,1);
}
function getDisplayString() { // returns the math expression as a string
return($this->_displayString);
}
function getValue() { // returns the solution as an integer
$ds=$this->_displayString;
$v1 = intval(substr($ds,0,1));
$v2 = intval(substr($ds,4,1));
$op = substr($ds,2,1);
switch ($op) {
case "+":
return($v1 + $v2);
break;
case "-":
return($v1 - $v2);
break;
case "x":
return($v1 * $v2);
break;
}
}
}
Then I added this function to the veriword class:
function set_veriword($useWord="") { /* create session variable for verification,
you may change the session variable name */
if ($useWord == "") { // no word sent, use random word
$this->word = $this->pick_word();
$_SESSION['veriword'] = $this->word;
} elseif ($useWord == "MathString") { // use math string for veriword
require "mathstringclass.inc.php";
$ms = new MathString();
$this->word = $ms->getDisplayString();
$_SESSION['veriword'] = $ms->getValue();
} else { // word sent as argument - use it.
$this->word = $useWord;
$_SESSION['veriword'] = $this->word;
}
}
The only files involved are:
mathstringclass.inc.php
spfveriword.php
spfvericlass.php
They’re in the assets/snippets/spform directory.
Bob