debug = true; } $this->sql = e107::getDB(); $this->user = e107::getUser()->getLoginName(); $this->state = "ready"; } public function getElements() { $this->difficultyValue = $this->difficulty(); $switchValue = rand(0,(int)$this->difficultyValue/22.4); if($this->debug) { $this->returnData['difficulty'] = $this->difficultyValue; $this->returnData['switchValue'] = $switchValue; } $this->elementSwitch($switchValue); return $this->returnData; } private function elementSwitch($switchValue) { switch ($switchValue) { case 0: $this->returnData['question'] = $this->elementValues('symbol'); break; case 1: $this->returnData['question'] = $this->elementValues('phase'); break; case 2: $this->returnData['question'] = $this->elementValues('name'); break; case 3: $this->returnData['question'] = $this->elementValues('number'); break; case 4: default: $this->returnData['question'] = $this->elementValues('atomic_mass'); 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 elementValues($sign) { $elements = json_decode(file_get_contents('resources/periodicTable.json'), true)['elements']; $selectedElement = rand(0, (int) ($this->difficultyValue * 1.18)); $TempQuestion = []; $TempQuestion['element'] = $elements[$selectedElement]; $TempQuestion[] = $selectedElement; $TempQuestion[] = $sign; return [ 'answer1'=>$TempQuestion['element'][$sign], 'question'=>$TempQuestion, 'type'=>__CLASS__]; } }