37970-vm/api/v1/Controllers/CollaborationController.php
2026-01-30 16:28:00 +00:00

53 lines
1.7 KiB
PHP

<?php
namespace Api\Controllers;
use Api\Core\Controller;
use Api\Core\Response;
use Api\Core\Auth;
class CollaborationController extends Controller {
public function resources() {
$user = Auth::getUser();
if (!$user) return Response::error('Unauthorized', 401);
$db = db();
// Show resources from same school OR public resources from other schools
$sql = "SELECT r.*, u.email as teacher_email
FROM resources r
JOIN users u ON r.teacher_id = u.id
WHERE r.school_id = :school_id OR r.is_public = 1
ORDER BY r.created_at DESC";
$stmt = $db->prepare($sql);
$stmt->execute(['school_id' => $user['school_id']]);
$resources = $stmt->fetchAll();
Response::json($resources);
}
public function storeResource() {
$user = Auth::getUser();
if (!$user) return Response::error('Unauthorized', 401);
$data = $this->getRequestData();
$db = db();
$sql = "INSERT INTO resources (title, description, teacher_id, school_id, is_public, grade, subject)
VALUES (:title, :description, :teacher_id, :school_id, :is_public, :grade, :subject)";
$stmt = $db->prepare($sql);
$stmt->execute([
'title' => $data['title'],
'description' => $data['description'],
'teacher_id' => $user['id'],
'school_id' => $user['school_id'],
'is_public' => $data['is_public'] ?? 0,
'grade' => $data['grade'] ?? null,
'subject' => $data['subject'] ?? null
]);
Response::json(['id' => $db->lastInsertId(), 'message' => 'Resource shared']);
}
}