| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- <?php
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- * Description of numeracy
- *
- * @author gregory
- */
- class numeracy {
-
- public $state = "error";
- private $debug = false;
- private $sql;
- private $user;
- private $difficultyValue;
- private $returnData;
- /**
- * Setup new numbers engine
- *
- * @param bool $test Enable debug mode.
- */
- public function __construct($test = false) {
-
- if ($test) {
- $this->debug = true;
- }
- $this->sql = e107::getDB();
- $this->user = e107::getUser()->getLoginName();
- $this->state = "ready";
- }
- public function getNumeracy() {
- $this->difficultyValue = $this->difficulty();
- $switchValue = rand(0,(int)$this->difficultyValue/16.6);
-
- if($this->debug) {
- $this->returnData['difficulty'] = $this->difficultyValue;
- $this->returnData['switchValue'] = $switchValue;
- }
-
- $this->numeracySwitch($switchValue);
-
- return $this->returnData;
- }
-
- private function numeracySwitch($switchValue) {
- switch ($switchValue) {
-
- case 0:
- $this->returnData['question'] = $this->basic('+');
- break;
- case 1:
- $this->returnData['question'] = $this->basic('-');
- break;
- case 2:
- $this->returnData['question'] = $this->basic('x');
- break;
- case 3:
- $this->returnData['question'] = $this->Rounding();
- break;
- case 4:
- case 5:
- $this->returnData['question'] = $this->Division();
- break;
- default:
- break;
- }
- }
- /**
- * Return the difficulty based on previously answered questions.
- *
- * @return int Difficulty as percent.
- */
- private function difficulty() {
- $query = "SELECT (SUM(correct)/COUNT(correct)*100) as percent
- FROM e107.`e107_tkReport` WHERE
- answereddatestamp >= unix_timestamp((NOW()) - INTERVAL 1 MONTH)
- AND username = '$this->user' AND subject = '".__CLASS__."';";
- if($this->debug) {
- $this->returnData['difficultySQL'] = $query;
- }
-
- $this->sql->gen($query);
- while ($row = $this->sql->fetch()) {
- if (is_numeric($row["percent"])) {
- return (int)$row["percent"];
- } else {
- return 1;
- }
- }
-
- }
-
- private function basic($sign) {
-
- $firstNumber = rand(1, (int) ($this->difficultyValue * ($this->difficultyValue/5)));
- $secondNumber = rand(1, (int) ($this->difficultyValue * ($this->difficultyValue/5)));
- $TempQuestion = [];
-
- $TempQuestion[] = $firstNumber;
- $TempQuestion[] = $sign;
- $TempQuestion[] = $secondNumber;
-
- $solution['+'] = ($firstNumber + $secondNumber);
- $solution['-'] = ($firstNumber - $secondNumber);
- $solution['x'] = ($firstNumber * $secondNumber);
-
- return [ 'answer1'=>$solution[$sign], 'question'=>$TempQuestion, 'type'=>__CLASS__];
-
- }
- private function Division() {
-
- $firstNumber = rand(1, (int) ($this->difficultyValue * ($this->difficultyValue/13)));
- $secondNumber = rand(1, (int) ($this->difficultyValue * ($this->difficultyValue/13)));
- $divNumber = $firstNumber * $secondNumber;
- $TempQuestion = [];
-
- $TempQuestion[] = $divNumber;
- $TempQuestion[] = '/';
- $TempQuestion[] = $secondNumber;
-
- $solution['/'] = ($divNumber / $secondNumber);
-
- return [ 'answer1'=>$solution['/'], 'question'=>$TempQuestion, 'type'=>__CLASS__];
-
- }
-
- private function Rounding() {
-
- $firstNumber = sprintf("%01.2f", (float) rand(100, (int) ($this->difficultyValue) * 100) / 100);
- $TempQuestion = [];
-
- $TempQuestion[] = $firstNumber;
- $TempQuestion[] = 'r';
-
- $solution['r'] = (sprintf("%01.2f", round($firstNumber / 5, 2) * 5));
-
- return [ 'answer1'=>$solution['r'], 'question'=>$TempQuestion, 'type'=>__CLASS__];
- }
- }
|