| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- <?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 numeracy2 {
-
- private $user;
- private $subSubject;
- /**
- * Setup new numbers engine
- *
- * @param bool $test Enable debug mode.
- */
- public function __construct($user,$sub) {
-
- $this->user = $user;
- $this->subSubject = $sub;
- }
- public function getNumeracy() {
-
- //print_a(['user'=>$this->user,'subSubject'=>$this->subSubject]);
- $difficulty = $this->difficulty();
- $question = $this->numeracySwitch($difficulty);
- $questionHtml = $this->htmlify($question);
-
- e107::getDb()->insert('tkLastQuestion', ['user'=>$this->user,'LastQuestionHtml'=>base64_encode($questionHtml)]);
-
- e107::getRender()->tablerender('Numeracy', $questionHtml);
- }
-
- private function numeracySwitch($difficulty) {
- switch ($this->subSubject) {
-
- case 'addition':
- return $this->basic('+',$difficulty);
- case 'subtraction':
- return $this->basic('-',$difficulty);
- case 'multiplication':
- return $this->basic('x',$difficulty);
- case 'rounding':
- return $this->Rounding($difficulty);
- case 'division':
- return $this->Division($difficulty);
- }
- }
- /**
- * Return the difficulty based on previously answered questions.
- *
- * @return int Difficulty as percent.
- */
- private function difficulty() {
- $stats = e107::getDb()->retrieve("SELECT (SUM(correct)/COUNT(correct)*100) as percent,COUNT(correct) as count
- FROM e107.`e107_tkReport` WHERE
- answereddatestamp >= unix_timestamp((NOW()) - INTERVAL 1 MONTH)
- AND username = '$this->user' AND subject = '$this->subSubject';");
-
- if ($stats["count"]==0) {
- return 1;
- } else {
- return $stats;
- }
-
- }
- private function getNumbers($difficulty,$sign){
- //TODO add finer grain control for multiplication here
- if($sign=='x'||$sign=='/')
- {
- $difficulty['count'] = (int)$difficulty['count']/4;
- $difficulty['percent'] = (int)$difficulty['percent']/6;
- }
-
- if($difficulty['count']>=300) {
- $startingNumber = mt_rand(0, (int) pow($difficulty['count'],($difficulty['percent']/100)*2));
- $endingNumber = mt_rand(0, (int) pow($difficulty['count'],($difficulty['percent']/100)*2));
- $returnNumbers = [$startingNumber,$endingNumber];
- }elseif($difficulty['count']>=200){
- $startingNumber = mt_rand(0, (int) $difficulty['count']+($difficulty['percent']/100)*2);
- $endingNumber = mt_rand(0, (int) $difficulty['count']+($difficulty['percent']/100)*2);
- $returnNumbers = $this->flipNumbers($startingNumber,$endingNumber);
- }elseif($difficulty['count']>=100){
- $startingNumber = mt_rand(0, (int) ($difficulty['percent'])*2);
- $endingNumber = mt_rand(0, (int) ($difficulty['percent'])*2);
- $returnNumbers = $this->flipNumbers($startingNumber,$endingNumber);
- }else{
- $startingNumber = mt_rand(0, $difficulty['count']+1);
- $endingNumber = mt_rand(0, $difficulty['count']+1);
- $returnNumbers = $this->flipNumbers($startingNumber,$endingNumber);
- }
- //print_a(['Original'=>['1'=>$startingNumber,'2'=>$endingNumber],'FlipNumbers'=>$returnNumbers,'user'=>$this->user,'diff'=>$difficulty]);
- return $returnNumbers;
- }
- private function flipNumbers($startingNumber,$endingNumber){
- if($startingNumber>$endingNumber){
- return [$startingNumber,$endingNumber];
- }else{
- return [$endingNumber,$startingNumber];
- }
- }
-
- private function basic($sign,$difficulty) {
-
- $numbers = $this->getNumbers($difficulty,$sign);
- $firstNumber = $numbers[0];
- $secondNumber = $numbers[1];
- $TempQuestion = [];
-
- $TempQuestion[] = $firstNumber;
- $TempQuestion[] = $sign;
- $TempQuestion[] = $secondNumber;
-
- $solution['+'] = ($firstNumber + $secondNumber);
- $solution['-'] = ($firstNumber - $secondNumber);
- $solution['x'] = ($firstNumber * $secondNumber);
-
- return [ 'answer1'=>$solution[$sign], 'question'=>$TempQuestion, 'type'=>$this->subSubject];
-
- }
- private function Division($difficulty) {
-
- $numbers = $this->getNumbers($difficulty,'/');
- $firstNumber = $numbers[1];
- $secondNumber = $numbers[2];
- $divNumber = $firstNumber * $secondNumber;
- $TempQuestion = [];
-
- $TempQuestion[] = $divNumber;
- $TempQuestion[] = '/';
- $TempQuestion[] = $secondNumber;
-
- $solution['/'] = ($divNumber / $secondNumber);
-
- return [ 'answer1'=>$solution['/'], 'question'=>$TempQuestion, 'type'=>$this->subSubject];
-
- }
-
- private function Rounding($difficulty) {
-
- $firstNumber = sprintf("%01.2f", (float) rand(100, (int) ($difficulty) * 100) / 100);
- $TempQuestion = [];
-
- $TempQuestion[] = $firstNumber;
- $TempQuestion[] = 'r';
-
- $solution['r'] = (sprintf("%01.2f", round($firstNumber / 5, 2) * 5));
-
- return [ 'answer1'=>$solution['r'], 'question'=>$TempQuestion, 'type'=>$this->subSubject];
- }
- private function htmlify($question) {
-
- $frm = e107::getForm();
- $fString = $frm->open('questionForm', 'post', '', ['autocomplete' => 'off', 'class' => 'formclass']);
- $fString .= $frm->label('<h4>'.ucfirst($this->subSubject).'</h4>') . "</br>\n";
- $fString .= $frm->label('Solve the following equation.') . "\n";
- $fString .= '</br>';
-
- foreach ($question['question'] as $seq) {
- $fString .= $frm->label($seq . " ") . "\n";
- }
-
- $fString .= '</br></br>';
- $fString .= $frm->text('a1', '', 80, ['placeholder' => 'Write answer here!', 'autocomplete' => "off", 'spellcheck' => "false"]) . "\n";
- $fString .= $frm->hidden('checkerData', TkAnswerStore::issue($this->user, $question), ['id' => 'checkerData']);
- $fString .= $frm->hidden('subject', $this->subSubject, ['id' => 'subject']);
- $fString .= '</br>' . "\n";
- $fString .= e107::getForm()->button('Answer', 'Check answer') . "\n";
- $fString .= e107::getForm()->close();
- $fString .= '</br></br>';
-
- return $fString;
-
- }
- }
|