2026-01-19 06:37:37 +07:00
|
|
|
<?php
|
|
|
|
|
namespace App\Models\Qc;
|
|
|
|
|
|
|
|
|
|
use App\Models\BaseModel;
|
|
|
|
|
|
|
|
|
|
class ResultCommentsModel extends BaseModel {
|
|
|
|
|
protected $table = 'result_comments';
|
|
|
|
|
protected $primaryKey = 'result_comment_id';
|
|
|
|
|
protected $allowedFields = [
|
2026-01-21 13:41:37 +07:00
|
|
|
'result_id',
|
|
|
|
|
'comment_text',
|
2026-01-19 06:37:37 +07:00
|
|
|
'created_at',
|
|
|
|
|
'updated_at',
|
|
|
|
|
'deleted_at'
|
|
|
|
|
];
|
|
|
|
|
protected $useTimestamps = true;
|
|
|
|
|
protected $useSoftDeletes = true;
|
|
|
|
|
|
|
|
|
|
public function search($keyword = null) {
|
|
|
|
|
if ($keyword) {
|
|
|
|
|
return $this->groupStart()
|
2026-01-21 13:41:37 +07:00
|
|
|
->like('comment_text', $keyword)
|
2026-01-19 06:37:37 +07:00
|
|
|
->groupEnd()
|
|
|
|
|
->findAll();
|
|
|
|
|
}
|
|
|
|
|
return $this->findAll();
|
|
|
|
|
}
|
2026-01-20 16:47:11 +07:00
|
|
|
|
|
|
|
|
/**
|
2026-01-21 13:41:37 +07:00
|
|
|
* Get comments by result_id
|
|
|
|
|
*/
|
|
|
|
|
public function getByResult(int $resultId): ?array {
|
|
|
|
|
return $this->where('result_id', $resultId)
|
|
|
|
|
->where('deleted_at', null)
|
|
|
|
|
->first();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get all comments for a control+test combination (via results)
|
|
|
|
|
*/
|
|
|
|
|
public function getByControlTest(int $controlId, int $testId): ?array {
|
|
|
|
|
// First get result IDs for this control+test
|
|
|
|
|
$db = \Config\Database::connect();
|
|
|
|
|
$results = $db->table('results')
|
|
|
|
|
->select('result_id')
|
|
|
|
|
->where('control_id', $controlId)
|
|
|
|
|
->where('test_id', $testId)
|
|
|
|
|
->where('deleted_at', null)
|
|
|
|
|
->get()
|
|
|
|
|
->getResultArray();
|
|
|
|
|
|
|
|
|
|
if (empty($results)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$resultIds = array_column($results, 'result_id');
|
|
|
|
|
return $this->whereIn('result_id', $resultIds)
|
|
|
|
|
->where('deleted_at', null)
|
|
|
|
|
->orderBy('created_at', 'DESC')
|
|
|
|
|
->findAll();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get all comments for a test (via results)
|
|
|
|
|
*/
|
|
|
|
|
public function getByTest(int $testId): array {
|
|
|
|
|
$db = \Config\Database::connect();
|
|
|
|
|
$results = $db->table('results')
|
|
|
|
|
->select('result_id')
|
|
|
|
|
->where('test_id', $testId)
|
|
|
|
|
->where('deleted_at', null)
|
|
|
|
|
->get()
|
|
|
|
|
->getResultArray();
|
|
|
|
|
|
|
|
|
|
if (empty($results)) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$resultIds = array_column($results, 'result_id');
|
|
|
|
|
return $this->whereIn('result_id', $resultIds)
|
|
|
|
|
->where('deleted_at', null)
|
|
|
|
|
->findAll();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get comments by control, test, and month (for reports)
|
2026-01-20 16:47:11 +07:00
|
|
|
*/
|
|
|
|
|
public function getByControlTestMonth(int $controlId, int $testId, string $month): ?array {
|
2026-01-21 13:41:37 +07:00
|
|
|
$db = \Config\Database::connect();
|
|
|
|
|
$results = $db->table('results')
|
|
|
|
|
->select('result_id')
|
|
|
|
|
->where('control_id', $controlId)
|
2026-01-20 16:47:11 +07:00
|
|
|
->where('test_id', $testId)
|
2026-01-21 13:41:37 +07:00
|
|
|
->where('res_date >=', $month . '-01')
|
|
|
|
|
->where('res_date <=', $month . '-31')
|
2026-01-20 16:47:11 +07:00
|
|
|
->where('deleted_at', null)
|
2026-01-21 13:41:37 +07:00
|
|
|
->get()
|
|
|
|
|
->getResultArray();
|
|
|
|
|
|
|
|
|
|
if (empty($results)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$resultIds = array_column($results, 'result_id');
|
|
|
|
|
$comments = $this->whereIn('result_id', $resultIds)
|
|
|
|
|
->where('deleted_at', null)
|
|
|
|
|
->orderBy('created_at', 'DESC')
|
|
|
|
|
->findAll();
|
|
|
|
|
|
|
|
|
|
return $comments ?: null;
|
2026-01-20 16:47:11 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-01-21 13:41:37 +07:00
|
|
|
* Get comments for multiple results
|
2026-01-20 16:47:11 +07:00
|
|
|
*/
|
2026-01-21 13:41:37 +07:00
|
|
|
public function getByResultIds(array $resultIds): array {
|
|
|
|
|
if (empty($resultIds)) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
return $this->whereIn('result_id', $resultIds)
|
2026-01-20 16:47:11 +07:00
|
|
|
->where('deleted_at', null)
|
|
|
|
|
->findAll();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-01-21 13:41:37 +07:00
|
|
|
* Upsert comment for a result
|
2026-01-20 16:47:11 +07:00
|
|
|
*/
|
|
|
|
|
public function upsertComment(array $data): int {
|
2026-01-21 13:41:37 +07:00
|
|
|
if (!isset($data['result_id'])) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$existing = $this->where('result_id', $data['result_id'])
|
2026-01-20 16:47:11 +07:00
|
|
|
->where('deleted_at', null)
|
|
|
|
|
->first();
|
|
|
|
|
|
|
|
|
|
if ($existing) {
|
2026-01-21 13:41:37 +07:00
|
|
|
if (empty($data['comment_text'])) {
|
2026-01-20 16:47:11 +07:00
|
|
|
// If text is empty, soft delete
|
|
|
|
|
$this->update($existing['result_comment_id'], ['deleted_at' => date('Y-m-d H:i:s')]);
|
|
|
|
|
return $existing['result_comment_id'];
|
|
|
|
|
}
|
|
|
|
|
$this->update($existing['result_comment_id'], $data);
|
|
|
|
|
return $existing['result_comment_id'];
|
|
|
|
|
} else {
|
2026-01-21 13:41:37 +07:00
|
|
|
if (empty($data['comment_text'])) {
|
2026-01-20 16:47:11 +07:00
|
|
|
return 0; // Don't insert empty comments
|
|
|
|
|
}
|
|
|
|
|
return $this->insert($data, true);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-01-19 06:37:37 +07:00
|
|
|
}
|