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

104 lines
3.6 KiB
PHP

<?php
namespace App\Models;
use CodeIgniter\Model;
class DailyLearningLogModel extends Model
{
protected $table = 'daily_learning_log';
protected $primaryKey = 'num';
protected $allowedFields = [
'user_id', 'user_email', 'todate', 'start_time', 'end_time',
'total_time', 'daily_words', 'repetition_count', 'interrupted_key','last_json_key', 'created_at'
];
public function logDailyLearning($data)
{
$existingLog = $this->where('user_id', $data['user_id'])->where('todate', $data['todate'])->first();
if (!$existingLog){
$log = $this->where('user_id', $data['user_id'])
->orderBy('todate', 'desc')
->limit(1)
->first();
if($log) {
$interruptedKeys = json_decode($log['interrupted_key'], true);
$maxkey = max(array_keys($interruptedKeys)) + 1;
}else{
$maxkey = 1;
}
$learningInfojosn = json_decode($data['learningInfo'])->learning_data;
$learningInfo = json_decode($learningInfojosn,true);
$studyData = [];
for ($i = $maxkey; $i < $maxkey + $data['daily_words']; $i++) {
if (isset($learningInfo[$i])) {
$studyData[$i] = $learningInfo[$i];
} else {
break;
}
}
$data['interrupted_key'] = json_encode($studyData);
$data['last_json_key'] = $maxkey;
$this->insert($data);
$num = $this->getInsertID();
}else{
$num = $existingLog['num'];
}
$returndata = $this->where('num', $num)->first();
return $returndata;
}
public function logDailyLearning_mod($data)
{
try {
$existingLog = $this->where('user_id', $data['user_id'])->where('todate', $data['todate'])->first();
if ($existingLog) {
$log = $this->where('user_id', $data['user_id'])->where('todate <>', $data['todate'])
->orderBy('todate', 'desc')
->limit(1)
->first();
if ($log) {
$interruptedKeys = json_decode($log['interrupted_key'], true);
$maxkey = max(array_keys($interruptedKeys)) + 1;
} else {
$maxkey = 1;
}
$learningInfo = json_decode($data['learningInfo'], true); //전체 학습데이터
$studyData = [];
for ($i = $maxkey; $i < $maxkey + $data['daily_words']; $i++) {
if (isset($learningInfo[$i])) {
$studyData[$i] = $learningInfo[$i];
} else {
break;
}
}
$data['interrupted_key'] = json_encode($studyData);
$data['last_json_key'] = $maxkey;
$this->update($existingLog['num'], $data);
$num = $existingLog['num'];
$returndata = $this->where('num', $num)->first();
return $returndata;
}else{
throw new \Exception("The 'email' index is not defined in the provided data.");
}
}catch(\Exception $e){
log_message('error', 'today date null: ' . $e->getMessage());
throw $e; // Re-throw exception to be handled by the calling code
}
}
public function getInterruptedKey($userId, $date)
{
return $this->select('interrupted_key')->where('user_id', $userId)->where('date', $date)->first();
}
}