Skip to content

Instantly share code, notes, and snippets.

@joel-extremo
Created July 3, 2018 20:57
Show Gist options
  • Save joel-extremo/c5f8ff59b4ab3913270d1f3d18d70ff4 to your computer and use it in GitHub Desktop.
Save joel-extremo/c5f8ff59b4ab3913270d1f3d18d70ff4 to your computer and use it in GitHub Desktop.
block
<?php
namespace App\Blocks_V2\MAT\MAT_4\MAT_4_A\MAT_4_A_02;
use App\Blocks\Repository\BlockBase;
use App\Blocks\Repository\TextProblems\TextTemplate;
class MAT_4_A_02_01 extends BlockBase
{
protected $config = [
'es' => [
'instruction' => 'Selecciona la respuesta correcta.',
'problem' => '{{comparasion_string}} cantidad de {{object_name}}.',
'objects' => [
[
'name' => 'manzanas',
'img' => 'apple'
],
[
'name' => 'hamburguesas',
'img' => 'hamburger'
],
[
'name' => 'tazas de cafe',
'img' => 'cup_of_coffe'
],
[
'name' => 'cerezas',
'img' => 'cherries'
]
],
'operators' => [
'>' => 'Mayor',
'<' => 'Menor',
]
],
'en' => [
'instruction' => 'Select de correct answer.',
'problem' => '{{comparasion_string}} amount of {{name}}.',
'fruits' => [
[
'name' => 'apples',
'img' => 'apple'
],
[
'name' => 'hamburgers',
'img' => 'hamburger'
],
[
'name' => 'cup of coffer',
'img' => 'cup_of_coffe'
],
[
'name' => 'cherries',
'img' => 'cherries'
]
],
'operators' => [
'>' => 'Grater',
'<' => 'Smaller',
]
],
];
protected $difficulty = [
'easy' => [
'number-range' => [2, 4],
'comparison-operator' => ['>']
],
'medium' => [
'number-range' => [4, 6],
'comparison-operator' => ['<']
],
'hard' => [
'number-range' => [4, 7],
'comparison-operator' => ['<', '>']
],
];
public function __construct()
{
parent::__construct();
$this->textTemplate = new TextTemplate;
}
public function generate()
{
$object = array_random($this->config['objects']);
$result = $this->getBoolean();
$comparison = $this->getComparison();
$numbers = $this->getNumbers($result, $comparison['operator']);
$textProblem = $this->textTemplate
->setText($this->config['problem'])
->replace([
'object_name' => $object['name'],
'comparasion_string' => $comparison['string']
]);
return [
'problem' => $textProblem,
'result' => (int)$result,
'options' => $numbers,
'img_type' => $object['img']
];
}
/**
* return and array with two number
* @return array
*/
private function getNumbers(bool $result, string $comparisonOperator) : array
{
do {
$number_1 = rand(...$this->get('number-range'));
$number_2 = rand(...$this->get('number-range'));
} while ($this->areValidNumbers($result, $comparisonOperator, $number_1, $number_2));
return [
$number_1,
$number_2
];
}
private function areValidNumbers(bool $result, string $comparisonOperator, int $number1, int $number2)
{
if($number1 == $number2)
return true;#we return true when the numbers are equal for the while generate another number
$comparisonIsValid = eval("return {$number1} {$comparisonOperator} {$number2};");
return $result == $comparisonIsValid;
}
/**
* get a random comparation operator and get the string
* @return array example(["operator" => ">", "string" => "Mayor"])
*/
private function getComparison() : array
{
$operator = array_random($this->get('comparison-operator'));
$string = $this->config['operators'][$operator];
return compact('operator', 'string');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment