Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 78 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| TaskAssignment | |
0.00% |
0 / 78 |
|
0.00% |
0 / 6 |
342 | |
0.00% |
0 / 1 |
| save | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
12 | |||
| delete | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 | |||
| getForTask | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
12 | |||
| getForEmployee | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
20 | |||
| fromArray | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
20 | |||
| toArray | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace BuyerKiosk\Workbook; |
| 4 | |
| 5 | use \PDO; |
| 6 | use \PDOException; |
| 7 | |
| 8 | /** |
| 9 | * TaskAssignment - Model for task assignments to employees |
| 10 | * |
| 11 | * Manages assignment of tasks to specific employees or all employees (when employeeId is null) |
| 12 | * with optional due dates and times. |
| 13 | */ |
| 14 | class TaskAssignment |
| 15 | { |
| 16 | /** |
| 17 | * @var int|null Assignment ID |
| 18 | */ |
| 19 | public $id; |
| 20 | |
| 21 | /** |
| 22 | * @var int Task ID |
| 23 | */ |
| 24 | public $taskId; |
| 25 | |
| 26 | /** |
| 27 | * @var int|null Employee ID (null = assigned to all employees) |
| 28 | */ |
| 29 | public $employeeId; |
| 30 | |
| 31 | /** |
| 32 | * @var string|null Due date (Y-m-d format) |
| 33 | */ |
| 34 | public $dueDate; |
| 35 | |
| 36 | /** |
| 37 | * @var string|null Due time (H:i:s format) |
| 38 | */ |
| 39 | public $dueTime; |
| 40 | |
| 41 | /** |
| 42 | * @var string Created timestamp |
| 43 | */ |
| 44 | public $createdAt; |
| 45 | |
| 46 | /** |
| 47 | * @var int|null Employee ID who created the assignment |
| 48 | */ |
| 49 | public $createdBy; |
| 50 | |
| 51 | /** |
| 52 | * Save task assignment (insert) |
| 53 | * |
| 54 | * @param PDO $db Database connection |
| 55 | * @return int The ID of the created assignment |
| 56 | * @throws \Exception If taskId is missing or insert fails |
| 57 | */ |
| 58 | public function save(PDO $db): int |
| 59 | { |
| 60 | if (!isset($this->taskId)) { |
| 61 | throw new \Exception('taskId is required'); |
| 62 | } |
| 63 | |
| 64 | try { |
| 65 | $stmt = $db->prepare(" |
| 66 | INSERT INTO workbook_task_assignments |
| 67 | (taskId, employeeId, dueDate, dueTime, createdBy) |
| 68 | VALUES |
| 69 | (:taskId, :employeeId, :dueDate, :dueTime, :createdBy) |
| 70 | "); |
| 71 | |
| 72 | $stmt->execute([ |
| 73 | ':taskId' => $this->taskId, |
| 74 | ':employeeId' => $this->employeeId, |
| 75 | ':dueDate' => $this->dueDate, |
| 76 | ':dueTime' => $this->dueTime, |
| 77 | ':createdBy' => $this->createdBy |
| 78 | ]); |
| 79 | |
| 80 | $this->id = (int) $db->lastInsertId(); |
| 81 | return $this->id; |
| 82 | } catch (PDOException $e) { |
| 83 | error_log("TaskAssignment::save error: " . $e->getMessage()); |
| 84 | throw new \Exception('Failed to save task assignment: ' . $e->getMessage()); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Delete task assignment |
| 90 | * |
| 91 | * @param PDO $db Database connection |
| 92 | * @return bool True if successful |
| 93 | */ |
| 94 | public function delete(PDO $db): bool |
| 95 | { |
| 96 | if (!isset($this->id)) { |
| 97 | error_log("TaskAssignment::delete error: id is required"); |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | try { |
| 102 | $stmt = $db->prepare(" |
| 103 | DELETE FROM workbook_task_assignments |
| 104 | WHERE id = :id |
| 105 | "); |
| 106 | |
| 107 | return $stmt->execute([':id' => $this->id]); |
| 108 | } catch (PDOException $e) { |
| 109 | error_log("TaskAssignment::delete error: " . $e->getMessage()); |
| 110 | return false; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Get all assignments for a specific task |
| 116 | * |
| 117 | * @param PDO $db Database connection |
| 118 | * @param int $taskId Task ID |
| 119 | * @return array Array of TaskAssignment objects |
| 120 | */ |
| 121 | public static function getForTask(PDO $db, int $taskId): array |
| 122 | { |
| 123 | try { |
| 124 | $stmt = $db->prepare(" |
| 125 | SELECT dta.*, e.employeeFirstName, e.employeeLastName |
| 126 | FROM workbook_task_assignments dta |
| 127 | LEFT JOIN employees e ON dta.employeeId = e.employeeID |
| 128 | WHERE dta.taskId = :taskId |
| 129 | ORDER BY dta.dueDate ASC, dta.dueTime ASC |
| 130 | "); |
| 131 | $stmt->execute([':taskId' => $taskId]); |
| 132 | |
| 133 | $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); |
| 134 | $assignments = []; |
| 135 | |
| 136 | foreach ($rows as $row) { |
| 137 | $assignment = self::fromArray($row); |
| 138 | // Add employee name for convenience |
| 139 | $assignment->employeeFirstName = $row['employeeFirstName'] ?? null; |
| 140 | $assignment->employeeLastName = $row['employeeLastName'] ?? null; |
| 141 | $assignments[] = $assignment; |
| 142 | } |
| 143 | |
| 144 | return $assignments; |
| 145 | } catch (PDOException $e) { |
| 146 | error_log("TaskAssignment::getForTask error: " . $e->getMessage()); |
| 147 | return []; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Get all assignments for a specific employee |
| 153 | * |
| 154 | * Optionally filter by date |
| 155 | * |
| 156 | * @param PDO $db Database connection |
| 157 | * @param int $employeeId Employee ID |
| 158 | * @param string|null $date Optional date filter (Y-m-d format) |
| 159 | * @return array Array of TaskAssignment objects |
| 160 | */ |
| 161 | public static function getForEmployee(PDO $db, int $employeeId, ?string $date = null): array |
| 162 | { |
| 163 | try { |
| 164 | $sql = " |
| 165 | SELECT dta.*, t.taskName, t.comment, tg.groupName |
| 166 | FROM workbook_task_assignments dta |
| 167 | INNER JOIN tasks t ON dta.taskId = t.id |
| 168 | LEFT JOIN taskGroups tg ON t.taskGroup = tg.id |
| 169 | WHERE (dta.employeeId = :employeeId OR dta.employeeId IS NULL) |
| 170 | "; |
| 171 | |
| 172 | $params = [':employeeId' => $employeeId]; |
| 173 | |
| 174 | if ($date !== null) { |
| 175 | $sql .= " AND (dta.dueDate IS NULL OR dta.dueDate = :date)"; |
| 176 | $params[':date'] = $date; |
| 177 | } |
| 178 | |
| 179 | $sql .= " ORDER BY dta.dueDate ASC, dta.dueTime ASC"; |
| 180 | |
| 181 | $stmt = $db->prepare($sql); |
| 182 | $stmt->execute($params); |
| 183 | |
| 184 | $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); |
| 185 | $assignments = []; |
| 186 | |
| 187 | foreach ($rows as $row) { |
| 188 | $assignment = self::fromArray($row); |
| 189 | // Add task info for convenience |
| 190 | $assignment->taskName = $row['taskName'] ?? null; |
| 191 | $assignment->comment = $row['comment'] ?? null; |
| 192 | $assignment->groupName = $row['groupName'] ?? null; |
| 193 | $assignments[] = $assignment; |
| 194 | } |
| 195 | |
| 196 | return $assignments; |
| 197 | } catch (PDOException $e) { |
| 198 | error_log("TaskAssignment::getForEmployee error: " . $e->getMessage()); |
| 199 | return []; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Create a TaskAssignment object from an array |
| 205 | * |
| 206 | * @param array $data Associative array of task assignment data |
| 207 | * @return self |
| 208 | */ |
| 209 | public static function fromArray(array $data): self |
| 210 | { |
| 211 | $assignment = new self(); |
| 212 | $assignment->id = isset($data['id']) ? (int) $data['id'] : null; |
| 213 | $assignment->taskId = (int) $data['taskId']; |
| 214 | $assignment->employeeId = isset($data['employeeId']) ? (int) $data['employeeId'] : null; |
| 215 | $assignment->dueDate = $data['dueDate'] ?? null; |
| 216 | $assignment->dueTime = $data['dueTime'] ?? null; |
| 217 | $assignment->createdAt = $data['createdAt'] ?? null; |
| 218 | $assignment->createdBy = isset($data['createdBy']) ? (int) $data['createdBy'] : null; |
| 219 | |
| 220 | return $assignment; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Convert TaskAssignment to array |
| 225 | * |
| 226 | * @return array Associative array representation |
| 227 | */ |
| 228 | public function toArray(): array |
| 229 | { |
| 230 | return [ |
| 231 | 'id' => $this->id, |
| 232 | 'taskId' => $this->taskId, |
| 233 | 'employeeId' => $this->employeeId, |
| 234 | 'dueDate' => $this->dueDate, |
| 235 | 'dueTime' => $this->dueTime, |
| 236 | 'createdAt' => $this->createdAt, |
| 237 | 'createdBy' => $this->createdBy |
| 238 | ]; |
| 239 | } |
| 240 | } |