| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- <?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 numbers
- *
- * @author gregory
- */
- class numbers {
- 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 getNumbers() {
- $this->difficultyValue = $this->difficulty();
- if($this->debug) {
- $this->returnData['difficulty'] = $this->difficultyValue;
- }
-
- $this->returnData['question'] = $this->getSequence();
-
- return $this->returnData;
- }
- /**
- * 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 getSequence() {
-
- $startingNumber = rand(0, (int) ($this->difficultyValue * ($this->difficultyValue/5)));
- $incrementalNumber = rand(1, (int) ($this->difficultyValue * ($this->difficultyValue/10)));
- $missingNumber = rand(1, 10);
- $TempQuestion = [];
-
- for ($i = 1; $i <= 10; $i++) {
- if ($i == $missingNumber) {
- $TempQuestion[] = "___,";
- $solution = $startingNumber;
- } elseif ($i == 10) {
- $TempQuestion[] = $startingNumber;
- } else {
- $TempQuestion[] = $startingNumber . ",";
- }
- $startingNumber += $incrementalNumber;
- }
- return [ 'answer1'=>$solution, 'question'=>$TempQuestion, 'type'=>__CLASS__];
- }
- }
|