Закончил разработку обучения
This commit is contained in:
@@ -20,7 +20,7 @@ class TrainingManager
|
||||
public function __construct(int $user_id)
|
||||
{
|
||||
$this->user_id = $user_id;
|
||||
|
||||
$this->generateToken();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -71,14 +71,16 @@ class TrainingManager
|
||||
|
||||
public function render()
|
||||
{
|
||||
$path = $_SERVER['DOCUMENT_ROOT'] . '/modules_data/steps/' . $this->short_name . '.php';
|
||||
if(!$this->isComplete) {
|
||||
$path = $_SERVER['DOCUMENT_ROOT'] . '/modules_data/steps/' . $this->short_name . '.php';
|
||||
|
||||
if(file_exists($path)) {
|
||||
require $path;
|
||||
return;
|
||||
if (file_exists($path)) {
|
||||
require $path;
|
||||
return;
|
||||
}
|
||||
|
||||
throw TrainingException::noRenderingFile();
|
||||
}
|
||||
|
||||
throw TrainingException::noRenderingFile();
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -87,19 +89,61 @@ class TrainingManager
|
||||
|
||||
private function stepData(string $short_name): object
|
||||
{
|
||||
return json_decode($this->getDatabaseRecords()->data)->$short_name;
|
||||
$data = $this->getDatabaseRecords()->data;
|
||||
|
||||
if(!isset($data->$short_name)) {
|
||||
$this->updateDatabaseRecord($short_name);
|
||||
$data = $this->getDatabaseRecords()->data;
|
||||
}
|
||||
|
||||
return $data->$short_name;
|
||||
}
|
||||
|
||||
private function getDatabaseRecords()
|
||||
private function generateToken($length = 16)
|
||||
{
|
||||
$letters = 'abcdefgmikiHGJKLjkGASysj7603456';
|
||||
|
||||
$token = '';
|
||||
|
||||
for ($i = 0; $i <= $length; $i++) {
|
||||
$token .= $letters[rand(0, strlen($letters))];
|
||||
}
|
||||
|
||||
Db::run('UPDATE user_training SET api_token = ? WHERE user_id = ?', [
|
||||
$token,
|
||||
$this->user_id
|
||||
]);
|
||||
}
|
||||
|
||||
public function getDatabaseRecords(): stdClass
|
||||
{
|
||||
if(!$this->database_records) {
|
||||
$this->database_records = Db::run('SELECT * FROM user_training WHERE user_id = ?', [$this->user_id])
|
||||
$data = Db::run('SELECT * FROM user_training WHERE user_id = ?', [$this->user_id])
|
||||
->fetch(PDO::FETCH_OBJ);
|
||||
|
||||
$this->database_records = new stdClass();
|
||||
|
||||
$this->database_records->user_id = $data->user_id;
|
||||
$this->database_records->api_token = $data->api_token;
|
||||
$this->database_records->data = json_decode($data->data);
|
||||
}
|
||||
|
||||
return $this->database_records;
|
||||
}
|
||||
|
||||
public function updateDatabaseRecord(string $short_name)
|
||||
{
|
||||
if($this->getDatabaseRecords()) {
|
||||
|
||||
$this->database_records->data->$short_name = $this->firstRecordData()->$short_name;
|
||||
|
||||
Db::run('UPDATE user_training SET data = ? WHERE user_id = ?', [
|
||||
json_encode($this->database_records->data),
|
||||
$this->user_id
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
public function createDatabaseRecord()
|
||||
{
|
||||
if(!$this->getDatabaseRecords()) {
|
||||
@@ -110,19 +154,22 @@ class TrainingManager
|
||||
}
|
||||
}
|
||||
|
||||
private function firstRecordData(): array
|
||||
private function firstRecordData(): stdClass
|
||||
{
|
||||
return [
|
||||
'first_step' => [
|
||||
'complete' => 0,
|
||||
'progress' => [
|
||||
'current' => 0,
|
||||
'need' => 0,
|
||||
]
|
||||
]
|
||||
$data = new stdClass();
|
||||
$short_names = [
|
||||
'first_step',
|
||||
'second_step'
|
||||
];
|
||||
}
|
||||
|
||||
foreach ($short_names as $name) {
|
||||
$data->$name->complete = false;
|
||||
$data->$name->progress->current = 0;
|
||||
$data->$name->progress->need = 1;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
public function __get(string $name)
|
||||
{
|
||||
return $this->steps[$name];
|
||||
@@ -130,11 +177,25 @@ class TrainingManager
|
||||
|
||||
public function addPoint(string $short_name)
|
||||
{
|
||||
$this->$short_name->progress++;
|
||||
$this->database_records->data->$short_name->progress->current++;
|
||||
}
|
||||
|
||||
public function nextStep()
|
||||
{
|
||||
foreach ($this->database_records->data as $step) {
|
||||
if($step->complete === false && $step->progress->current >= $step->progress->need) {
|
||||
$step->complete = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function store()
|
||||
{
|
||||
$this->nextStep();
|
||||
|
||||
Db::run('UPDATE user_training SET data = ? WHERE user_id = ?', [
|
||||
json_encode($this->database_records->data),
|
||||
$this->user_id
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user