/var/www/(Del)pathways.org.hk/MIS_bk/event/lesson_student_delete.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
require_once(__DIR__ '/../checkuser.php');
$post $_POST['event_lesson'];

$sql "SELECT * FROM mis_event_lesson WHERE id = ? AND deleted = ?";
$parameters = array($post['id'], 0);
$sth Db\Util::execute($dbh$sql$parameters);
$event_lesson $sth->fetch(PDO::FETCH_ASSOC);
if (!
$event_lesson) {
    throw new 
Exception('Event Lesson not found!');
}

Db\Util::transaction($dbh, function() use ($dbh, &$post, &$event_lesson) {
    
$columns Db\Util::columns($dbh'mis_lesson_student');
    
    
$delete_lesson_student = function($lesson_student_id) use ($dbh, &$columns) {
        
$lesson_student = array(
            
'id' => $lesson_student_id,
            
'deleted' => 1,
        );
        return 
Db\Util::update($dbh'mis_lesson_student'$lesson_student$columns);
    };
    
    
$sql "SELECT * FROM mis_event WHERE id = ? FOR UPDATE";
    
$parameters = array($event_lesson['event_id']);
    
$sth Db\Util::execute($dbh$sql$parameters);
    
    switch (
$post['type']) {
        
// Only this lesson
        
case 1: {
            
$delete_lesson_student($post['lesson_student_id']);
            break;
        }
        
// This lesson and after
        
case 2: {
            
$sql "
SELECT student.*
FROM mis_student student
INNER JOIN mis_lesson_student lesson_student ON lesson_student.student_id = student.id
WHERE lesson_student.id = ?"
;
            
$parameters = array($post['lesson_student_id']);
            
$sth Db\Util::execute($dbh$sql$parameters);
            
$student $sth->fetch(PDO::FETCH_ASSOC);
            
            
$sql "
SELECT lesson_student.*
FROM mis_lesson_student lesson_student
INNER JOIN mis_student student ON student.id = lesson_student.student_id
INNER JOIN mis_event_lesson lesson ON lesson.id = lesson_student.lesson_id
WHERE lesson.event_id = ? AND lesson.number >= ? AND student.root_id = ? AND lesson_student.deleted = ?"
;
            
$parameters = array($event_lesson['event_id'], $event_lesson['number'], $student['root_id'], 0);
            
$sth Db\Util::execute($dbh$sql$parameters);
            
$lesson_students $sth->fetchAll();
            
            foreach (
$lesson_students as $lesson_student) {
                
$delete_lesson_student($lesson_student['id']);
            }
            break;
        }
    }
});

$data = array(
    
'id' => $event_lesson['id'],
    
'message' => 'Deleted',
);
redirectAndExit(Util::link(__DIR__ '/lesson_student.php') . '?' http_build_query($data));