init backend presensi

This commit is contained in:
mwpn
2026-03-05 14:37:36 +07:00
commit b4fda6b9c9
319 changed files with 27261 additions and 0 deletions

View File

@@ -0,0 +1,103 @@
<?php
namespace App\Modules\Notification\Controllers;
use App\Core\BaseApiController;
use CodeIgniter\HTTP\ResponseInterface;
/**
* Telegram Webhook Controller
*
* Handles incoming webhook requests from Telegram.
*/
class TelegramWebhookController extends BaseApiController
{
/**
* Handle Telegram webhook
*
* POST /api/telegram/webhook
*
* For now, just parse updates and log into writable/logs
*
* @return ResponseInterface
*/
public function index(): ResponseInterface
{
// Get JSON input from Telegram
$input = $this->request->getJSON(true);
// Log the webhook data
$logData = [
'timestamp' => date('Y-m-d H:i:s'),
'update_id' => $input['update_id'] ?? null,
'message' => $input['message'] ?? null,
'callback_query' => $input['callback_query'] ?? null,
'raw_data' => $input,
];
// Write to log file
$logFile = WRITEPATH . 'logs/telegram_webhook_' . date('Y-m-d') . '.log';
$logMessage = date('Y-m-d H:i:s') . ' - ' . json_encode($logData, JSON_PRETTY_PRINT) . PHP_EOL . PHP_EOL;
file_put_contents($logFile, $logMessage, FILE_APPEND);
// Parse update
$update = $this->parseUpdate($input);
// For now, just return success
// In the future, this will process commands and messages
return $this->successResponse(
[
'update_id' => $update['update_id'] ?? null,
'type' => $update['type'] ?? 'unknown',
'processed' => true,
],
'Webhook received and logged'
);
}
/**
* Parse Telegram update
*
* @param array $input Raw webhook data
* @return array Parsed update data
*/
protected function parseUpdate(array $input): array
{
$update = [
'update_id' => $input['update_id'] ?? null,
'type' => 'unknown',
];
// Check for message
if (isset($input['message'])) {
$update['type'] = 'message';
$update['message'] = [
'message_id' => $input['message']['message_id'] ?? null,
'from' => $input['message']['from'] ?? null,
'chat' => $input['message']['chat'] ?? null,
'date' => $input['message']['date'] ?? null,
'text' => $input['message']['text'] ?? null,
];
}
// Check for callback query
if (isset($input['callback_query'])) {
$update['type'] = 'callback_query';
$update['callback_query'] = [
'id' => $input['callback_query']['id'] ?? null,
'from' => $input['callback_query']['from'] ?? null,
'message' => $input['callback_query']['message'] ?? null,
'data' => $input['callback_query']['data'] ?? null,
];
}
// Check for edited message
if (isset($input['edited_message'])) {
$update['type'] = 'edited_message';
$update['edited_message'] = $input['edited_message'];
}
return $update;
}
}

View File

@@ -0,0 +1,34 @@
<?php
namespace App\Modules\Notification\Entities;
use CodeIgniter\Entity\Entity;
/**
* Parent Entity
*
* Represents a parent in the system.
*/
class Parent extends Entity
{
/**
* Attributes that can be mass assigned
*
* @var array<string>
*/
protected $allowedFields = [
'name',
'phone_number',
];
/**
* Attributes that should be cast to specific types
*
* @var array<string, string>
*/
protected $casts = [
'id' => 'integer',
'created_at' => 'datetime',
'updated_at' => 'datetime',
];
}

View File

@@ -0,0 +1,44 @@
<?php
namespace App\Modules\Notification\Entities;
use CodeIgniter\Entity\Entity;
/**
* Student Parent Entity
*
* Represents the relationship between a student and a parent.
*/
class StudentParent extends Entity
{
/**
* Relationship constants
*/
public const RELATIONSHIP_AYAH = 'AYAH';
public const RELATIONSHIP_IBU = 'IBU';
public const RELATIONSHIP_WALI = 'WALI';
/**
* Attributes that can be mass assigned
*
* @var array<string>
*/
protected $allowedFields = [
'student_id',
'parent_id',
'relationship',
];
/**
* Attributes that should be cast to specific types
*
* @var array<string, string>
*/
protected $casts = [
'id' => 'integer',
'student_id' => 'integer',
'parent_id' => 'integer',
'created_at' => 'datetime',
'updated_at' => 'datetime',
];
}

View File

@@ -0,0 +1,51 @@
<?php
namespace App\Modules\Notification\Entities;
use CodeIgniter\Entity\Entity;
/**
* Telegram Account Entity
*
* Represents a Telegram account linked to the system.
*/
class TelegramAccount extends Entity
{
/**
* Attributes that can be mass assigned
*
* @var array<string>
*/
protected $allowedFields = [
'telegram_user_id',
'username',
'first_name',
'last_name',
'is_verified',
'parent_id',
];
/**
* Attributes that should be cast to specific types
*
* @var array<string, string>
*/
protected $casts = [
'id' => 'integer',
'telegram_user_id' => 'integer',
'parent_id' => 'integer',
'is_verified' => 'boolean',
'created_at' => 'datetime',
'updated_at' => 'datetime',
];
/**
* Check if account is verified
*
* @return bool
*/
public function isVerified(): bool
{
return (bool) $this->attributes['is_verified'];
}
}

View File

@@ -0,0 +1,53 @@
<?php
namespace App\Modules\Notification\Models;
use App\Modules\Notification\Entities\Parent as ParentEntity;
use CodeIgniter\Model;
/**
* Parent Model
*
* Handles database operations for parents.
*/
class ParentModel extends Model
{
protected $table = 'parents';
protected $primaryKey = 'id';
protected $useAutoIncrement = true;
protected $returnType = ParentEntity::class;
protected $useSoftDeletes = false;
protected $protectFields = true;
protected $allowedFields = [
'name',
'phone_number',
];
// Dates
protected $useTimestamps = true;
protected $dateFormat = 'datetime';
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
protected $deletedField = 'deleted_at';
// Validation
protected $validationRules = [
'name' => 'required|max_length[255]',
'phone_number' => 'permit_empty|max_length[20]',
];
protected $validationMessages = [];
protected $skipValidation = false;
protected $cleanValidationRules = true;
// Callbacks
protected $allowCallbacks = true;
protected $beforeInsert = [];
protected $afterInsert = [];
protected $beforeUpdate = [];
protected $afterUpdate = [];
protected $beforeFind = [];
protected $afterFind = [];
protected $beforeDelete = [];
protected $afterDelete = [];
}

View File

@@ -0,0 +1,55 @@
<?php
namespace App\Modules\Notification\Models;
use App\Modules\Notification\Entities\StudentParent;
use CodeIgniter\Model;
/**
* Student Parent Model
*
* Handles database operations for student-parent relationships.
*/
class StudentParentModel extends Model
{
protected $table = 'student_parents';
protected $primaryKey = 'id';
protected $useAutoIncrement = true;
protected $returnType = StudentParent::class;
protected $useSoftDeletes = false;
protected $protectFields = true;
protected $allowedFields = [
'student_id',
'parent_id',
'relationship',
];
// Dates
protected $useTimestamps = true;
protected $dateFormat = 'datetime';
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
protected $deletedField = 'deleted_at';
// Validation
protected $validationRules = [
'student_id' => 'required|integer|is_not_unique[students.id]',
'parent_id' => 'required|integer|is_not_unique[parents.id]',
'relationship' => 'required|in_list[AYAH,IBU,WALI]',
];
protected $validationMessages = [];
protected $skipValidation = false;
protected $cleanValidationRules = true;
// Callbacks
protected $allowCallbacks = true;
protected $beforeInsert = [];
protected $afterInsert = [];
protected $beforeUpdate = [];
protected $afterUpdate = [];
protected $beforeFind = [];
protected $afterFind = [];
protected $beforeDelete = [];
protected $afterDelete = [];
}

View File

@@ -0,0 +1,93 @@
<?php
namespace App\Modules\Notification\Models;
use App\Modules\Notification\Entities\TelegramAccount;
use CodeIgniter\Model;
/**
* Telegram Account Model
*
* Handles database operations for Telegram accounts.
*/
class TelegramAccountModel extends Model
{
protected $table = 'telegram_accounts';
protected $primaryKey = 'id';
protected $useAutoIncrement = true;
protected $returnType = TelegramAccount::class;
protected $useSoftDeletes = false;
protected $protectFields = true;
protected $allowedFields = [
'telegram_user_id',
'username',
'first_name',
'last_name',
'is_verified',
'parent_id',
];
// Dates
protected $useTimestamps = true;
protected $dateFormat = 'datetime';
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
protected $deletedField = 'deleted_at';
// Validation
protected $validationRules = [
'telegram_user_id' => 'required|integer|is_unique[telegram_accounts.telegram_user_id,id,{id}]',
'username' => 'permit_empty|max_length[255]',
'first_name' => 'permit_empty|max_length[255]',
'last_name' => 'permit_empty|max_length[255]',
'is_verified' => 'permit_empty|in_list[0,1]',
];
protected $validationMessages = [];
protected $skipValidation = false;
protected $cleanValidationRules = true;
// Callbacks
protected $allowCallbacks = true;
protected $beforeInsert = [];
protected $afterInsert = [];
protected $beforeUpdate = [];
protected $afterUpdate = [];
protected $beforeFind = [];
protected $afterFind = [];
protected $beforeDelete = [];
protected $afterDelete = [];
/**
* Find Telegram account by telegram_user_id
*
* @param int $telegramUserId
* @return TelegramAccount|null
*/
public function findByTelegramUserId(int $telegramUserId): ?TelegramAccount
{
return $this->where('telegram_user_id', $telegramUserId)->first();
}
/**
* Get telegram_user_id list for all linked parents of a student
*
* @param int $studentId
* @return array<int> List of telegram_user_id
*/
public function getTelegramUserIdsByStudentId(int $studentId): array
{
$db = \Config\Database::connect();
$builder = $db->table('student_parents AS sp');
$builder->select('t.telegram_user_id');
$builder->join('telegram_accounts AS t', 't.parent_id = sp.parent_id AND t.is_verified = 1', 'inner');
$builder->where('sp.student_id', $studentId);
$rows = $builder->get()->getResultArray();
$ids = [];
foreach ($rows as $row) {
$ids[] = (int) $row['telegram_user_id'];
}
return array_values(array_unique($ids));
}
}

View File

@@ -0,0 +1,13 @@
<?php
/**
* Notification Module Routes
*
* This file is automatically loaded by ModuleLoader.
* Define your notification management routes here.
*
* @var RouteCollection $routes
*/
// Telegram webhook route
$routes->post('api/telegram/webhook', '\App\Modules\Notification\Controllers\TelegramWebhookController::index');

View File

@@ -0,0 +1,146 @@
<?php
namespace App\Modules\Notification\Services;
use App\Modules\Academic\Models\StudentModel;
use App\Modules\Notification\Models\StudentParentModel;
use App\Modules\Notification\Models\TelegramAccountModel;
/**
* Parent Link Service
*
* Handles parent linking logic for students via Telegram.
*/
class ParentLinkService
{
protected StudentModel $studentModel;
protected TelegramAccountModel $telegramAccountModel;
protected StudentParentModel $studentParentModel;
/**
* Link code length
*/
protected int $linkCodeLength = 16;
public function __construct()
{
$this->studentModel = new StudentModel();
$this->telegramAccountModel = new TelegramAccountModel();
$this->studentParentModel = new StudentParentModel();
}
/**
* Generate unique link code for student
*
* @param int $studentId Student ID
* @return string Generated link code
*/
public function generateLinkCode(int $studentId): string
{
// Check if student exists
$student = $this->studentModel->find($studentId);
if (!$student) {
throw new \InvalidArgumentException("Student with ID {$studentId} not found");
}
// Generate unique link code
do {
$linkCode = $this->generateRandomCode($this->linkCodeLength);
$existing = $this->studentModel->where('parent_link_code', $linkCode)->first();
} while ($existing !== null);
// Update student with link code
$this->studentModel->update($studentId, [
'parent_link_code' => $linkCode,
]);
return $linkCode;
}
/**
* Link Telegram account to student using link code
*
* @param string $linkCode Link code from student
* @param int $telegramUserId Telegram user ID
* @param array $profileData Telegram profile data (username, first_name, last_name)
* @return array Result with student_id, parent_id, telegram_account_id
*/
public function linkTelegramToStudent(string $linkCode, int $telegramUserId, array $profileData): array
{
// Find student by link code
$student = $this->studentModel->where('parent_link_code', $linkCode)->first();
if (!$student) {
throw new \InvalidArgumentException("Invalid link code");
}
// Check if Telegram account already exists
$telegramAccount = $this->telegramAccountModel->findByTelegramUserId($telegramUserId);
// Create parent first so we can set parent_id on telegram account
$parentModel = new \App\Modules\Notification\Models\ParentModel();
$parentId = $parentModel->insert([
'name' => trim(($profileData['first_name'] ?? '') . ' ' . ($profileData['last_name'] ?? '')),
'phone_number' => null, // Can be updated later
]);
if (!$telegramAccount) {
// Create new Telegram account linked to parent
$telegramAccountId = $this->telegramAccountModel->insert([
'telegram_user_id' => $telegramUserId,
'username' => $profileData['username'] ?? null,
'first_name' => $profileData['first_name'] ?? null,
'last_name' => $profileData['last_name'] ?? null,
'is_verified' => 1,
'parent_id' => $parentId,
]);
} else {
$telegramAccountId = $telegramAccount->id;
// Update profile data and link to parent
$this->telegramAccountModel->update($telegramAccountId, [
'username' => $profileData['username'] ?? $telegramAccount->username,
'first_name' => $profileData['first_name'] ?? $telegramAccount->first_name,
'last_name' => $profileData['last_name'] ?? $telegramAccount->last_name,
'is_verified' => 1,
'parent_id' => $parentId,
]);
}
// Create student-parent relationship
$studentParentId = $this->studentParentModel->insert([
'student_id' => $student->id,
'parent_id' => $parentId,
'relationship' => 'WALI',
]);
// Clear link code after successful linking
$this->studentModel->update($student->id, [
'parent_link_code' => null,
]);
return [
'student_id' => $student->id,
'parent_id' => $parentId,
'telegram_account_id' => $telegramAccountId,
'student_parent_id' => $studentParentId,
];
}
/**
* Generate random alphanumeric code
*
* @param int $length Code length
* @return string Generated code
*/
protected function generateRandomCode(int $length): string
{
$characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$code = '';
for ($i = 0; $i < $length; $i++) {
$code .= $characters[random_int(0, strlen($characters) - 1)];
}
return $code;
}
}

View File

@@ -0,0 +1,76 @@
<?php
namespace App\Modules\Notification\Services;
/**
* Telegram Bot Service
*
* Sends messages via Telegram Bot API.
*/
class TelegramBotService
{
protected string $apiBase = 'https://api.telegram.org/bot';
protected ?string $token;
public function __construct()
{
$this->token = env('telegram.bot_token') ?: env('telegram_bot_token');
}
/**
* Send a text message to a Telegram user
*
* @param int $telegramUserId Telegram user ID (chat_id)
* @param string $message Text message to send (HTML supported)
* @return bool True on success, false on failure
*/
public function sendMessage(int $telegramUserId, string $message): bool
{
if (empty($this->token)) {
log_message('error', 'TelegramBotService: telegram.bot_token is not set in .env');
return false;
}
$url = $this->apiBase . $this->token . '/sendMessage';
$payload = [
'chat_id' => $telegramUserId,
'text' => $message,
'parse_mode' => 'HTML',
'disable_web_page_preview' => true,
];
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($payload),
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 10,
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlError = curl_error($ch);
curl_close($ch);
if ($curlError) {
log_message('error', 'TelegramBotService sendMessage curl error: ' . $curlError . ' for chat_id=' . $telegramUserId);
return false;
}
if ($httpCode !== 200) {
log_message('error', 'TelegramBotService sendMessage HTTP ' . $httpCode . ' for chat_id=' . $telegramUserId . ' response=' . $response);
return false;
}
$data = json_decode($response, true);
if (isset($data['ok']) && $data['ok'] === true) {
return true;
}
log_message('error', 'TelegramBotService sendMessage API error: ' . ($response ?: 'empty'));
return false;
}
}