englishtokorea/web/app/Models/UserModel.php
2025-05-28 14:26:49 +09:00

120 lines
4.0 KiB
PHP

<?php namespace App\Models;
use CodeIgniter\Model;
class UserModel extends Model
{
protected $table = 'user';
protected $primaryKey = 'id';
protected $allowedFields = ['name', 'email', 'password', 'profile_pic','daily_words','learning_days','daily_words','repetition','review_percentage','screenlayout','test_mode','updated_at','created_at'];
protected $useTimestamps = true;
public function getUserByEmail($email)
{
return $this->where('email', $email)->first();
}
public function getUser($id)
{
return $this->where('id', $id)->first();
}
public function updateUserLearning($id, $data)
{
return $this->update($id, $data);
}
public function createUser($data)
{
$this->insert($data);
$id = $this->getInsertID();
// 학습 정보 생성
$learningData = $this->generateLearningData();
$learningInfo = [
'id' => $id,
'email' => $data['email'],
'learning_data' => json_encode($learningData),
'last_studied_key' => null
];
$learningModel = new \App\Models\UserLearningInfoModel();
$learningModel->insert($learningInfo);
return $id;
}
private function generateLearningData()
{
$numbers = range(1, 1000);
shuffle($numbers);
$learningData = [];
foreach ($numbers as $key => $value) {
$learningData[$key + 1] = $value;
}
return $learningData;
}
public function setSessionData($num)
{
try {
if (!isset($num)) {
throw new \Exception("The 'id' index is not defined in the provided data.");
}
$data = $this->where('id', $num)->first();
if (!isset($data['email'])) {
throw new \Exception("The 'email' index is not defined in the provided data.");
}
$learningInfoModel = new \App\Models\UserLearningInfoModel();
$learningInfo = $learningInfoModel->getLearningInfoByUserId($data['email']);
if (!$learningInfo) {
throw new \Exception("Failed to retrieve learning information for user ID: " . $data['email']);
}
$dailydata = [
'user_id' => $num,
'user_email' => $data['email'],
'todate' => date('Y-m-d'),
'daily_words' => $data['daily_words'], // 학습 단어 수 가져오기
'repetition_count' => 0,
'learningInfo' => json_encode($learningInfo), // 전체 학습 json
];
$DailyLearningLogModel = new \App\Models\DailyLearningLogModel();
$LearningData = $DailyLearningLogModel->logDailyLearning($dailydata);
$ses_data = [
'num' => $num,
'userid' => $data['email'],
'useremail' => $data['email'],
'username' => $data['name'],
'profile_pic' => $data['profile_pic'],
'auth_provider' => $data['auth_provider'],
'repetition' => $data['repetition'],
'review_percentage' => $data['review_percentage'],
'screenlayout' => $data['screenlayout'],
'test_mode' => $data['test_mode'],
'todate' => date('Y-m-d'),
'dailyLearningLog_num' => $LearningData['num'],
'repetition_count' => $LearningData['repetition_count'],
'daily_words' => $LearningData['daily_words'],
'interrupted_key' => json_decode($LearningData['interrupted_key'], true),
'last_studied_key' => $LearningData['last_json_key'],
'study_data' => json_decode($LearningData['last_json_key']),
'logged_in' => TRUE
];
session()->set($ses_data);
} catch (\Exception $e) {
log_message('error', 'Error in setSessionData: ' . $e->getMessage());
throw $e; // Re-throw exception to be handled by the calling code
}
}
}