debug = true; } $this->sql = e107::getDB(); $this->user = e107::getUser()->getLoginName(); $this->state = "ready"; } public function getProblem() { $this->difficultyValue = $this->difficulty(); if($this->debug) { $this->returnData['difficulty'] = $this->difficultyValue; } $this->questionAssembler(); return $this->returnData; } private function questionAssembler() { //return [ 'answer1'=>$solution['/'], 'question'=>$TempQuestion, 'type'=>__CLASS__]; $questionIndexSelected = array_rand($this->questionData()); $question = $this->questionData()[$questionIndexSelected]; $this->returnData['question'] = $this->substitutor($question); //print_a([__CLASS__.' '.__FUNCTION__=>['rawQuestion'=>$question,'processedQuestion'=>$this->returnData['question']]]); } private function questionData() { $wordedMath = [ ['{name1} used {int,1,1,diff} meters of cloth to sew {int,2,1,diff} pillow cases. How much cloth would be used to sew {int,3,2,diff} pillow cases?','(({int,1,1,diff}/{int,2,1,diff})*{int,3,2,diff})'], //['A mother makes {int,1,1,diff} sandwiches for a party. Each child received {int,2,1,diff} sandwiches. How many children were at the party?','({int,1,1,diff}/{int,2,1,diff})'], ['The mass of a sponge is {int,1,1,diff} g. When put in water it expanded and gained a mass of {int,2,1,diff} g. Find the mass difference if {int,3,1,diff} similar sponges were used in the experiment?','({int,2,1,diff}*{int,3,1,diff})'], //['{name1} is {int,1,1,diff} years old. His brother is {int,2,1,diff} months older than him. How old is {name1}’s brother?','({int,1,1,diff}+({int,1,1,diff}/12))'], ['An elephant eats {int,1,1,diff} kg of food a week. How much will it eat in {int,2,1,10} years?','(({int,1,1,diff}*52)*{int,2,1,10})'], // ['A cake recipe uses 60 g of flour. How many cakes can I bake with 4.2 kg of flour?'], // ['On a scale there is 88 grams on side A and 1.3 kg on side B. How much more weight do I need to put on side A to balance the scale?','()'], // ['{name1} baked 400 cupcakes and she sold 2 fifths at the market. How many cupcakes does she have left?'], // ['A baker baked 720 slices of pizza. He sold 5 nineths of the pizzas and threw away 45 slices. How many slices were left?'], // ['A cake recipe uses 1 quarter kilogram of sugar. How many cakes can I bake with 750 grams of sugar?'], // ['There were 280 marbles in a jar. {name1} dropped the jar and lost 3 quarters of the marbles. How many marbles were left?'], // ['At the beach there were 675 people. One fifth were children and the rest were adults. How many adults were at the beach?'], // ['{name1} is 56 years old and has a brother who is 84 months younger. How old is his brother?'], // ['{name1} has to pack 28.35 kg of sugar into 10 bags equally. How much sugar would each bag contain?'], // ['There was 310.4 kg of sand. It has to be divided into 100 tubs equally. How much sand would each tub contain?'], // ['A packet contains 2.39 kg of flour. {name1} bought 1000 packets for her cake shop. How much flour does she have in total?'], ]; return $wordedMath; } /** * 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 substitutor($question) { $questionStr = $this->names($question[0]); $solutionStr = $this->names($question[1]); $questionArray = preg_split("/{|}/",$questionStr); foreach ($questionArray as $questionPart) { if($this->contains('int', $questionPart)) { $substituteString = $this->ints(explode(',',$questionPart)); $questionStr = str_replace('{'.$questionPart.'}', $substituteString, $questionStr); $solutionStr = str_replace('{'.$questionPart.'}', $substituteString, $solutionStr); } } // eval needed return [ 'answer1'=>(int)eval("return ".$solutionStr.";"), 'question'=>$questionStr, 'type'=>__CLASS__]; } private function names($questionString) { $freeNames = ['Anna','George','Marie','Elliot','Paul','Stacey','Maureen']; for ($int = 1; $int <= 10; $int++) { $randomName = array_rand($freeNames); if($this->contains('{name'.$int.'}', $questionString)) { $name = $freeNames[$randomName]; $questionString = str_replace('{name'.$int.'}', $name, $questionString); unset($freeNames[$randomName]); }else{ break; } } return $questionString; } private function ints($dataBlock) { foreach ($dataBlock as $key=>&$data) { if($this->contains('diff', $data)) { $data = (int)$this->difficultyValue; if($data<$dataBlock[($key-1)]) { $data = $dataBlock[($key-1)]; } } } return mt_rand($dataBlock[2],$dataBlock[3]); } /** * contains($needle, $haystack) * * * Example;
* $haystack = 'Hello how are you';
* $needle = 'are';
* contains($needle, $haystack); // == true
*
* @param string $needle * @param string $haystack * @return bool */ function contains($needle, $haystack) { return strpos($haystack, $needle) !== false; } }