summaryrefslogtreecommitdiffstats
path: root/modules-available/exams/page.inc.php
blob: e5129d38018a2bb0e92bc635a6e9b23c0323f47c (plain) (blame)
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
68
69
70
71
72
73
74
75
76
<?php

class Page_Exams extends Page
{
    var $action;
    var $exams;
    var $locations;

    protected function doPreprocess()
    {
        User::load();

        $req_action = Request::get('action', 'show');
        if (in_array($req_action, ['show', 'add', 'delete'])) {
            $this->action = $req_action;
        }

        if ($this->action === 'show') {
            $tmp = Database::simpleQuery("select * from exams NATURAL LEFT OUTER JOIN location;", []);
            while ($exam = $tmp->fetch(PDO::FETCH_ASSOC)) {
                $this->exams[] = $exam;
            }
        } elseif ($this->action === 'add') {
            $tmp = Database::simpleQuery("select * from location;", []);
            while ($loc = $tmp->fetch(PDO::FETCH_ASSOC)) {
                $this->locations[] = $loc;
            }

            if (Request::isPost()) {
                /* process form-data */
                $locationid = Request::post('location');
                $starttime  = Request::post('starttime');
                $endtime    = Request::post('endtime');

                $res = Database::exec("INSERT INTO exams(locationid, starttime, endtime) VALUES(:locationid, :starttime, :endtime);",
                    compact('locationid', 'starttime', 'endtime'));

                if ($res === false) {
                    Message::addError('exam-not-added');
                }  else {
                    Message::addInfo('exam-added-success');
                }
                Util::redirect('?do=exams');
            }

        } elseif ($this->action === 'delete') {
            if (!Request::isPost()) { die('delete only works with a post request'); }
            $examid = Request::post('examid');
            $res = Database::exec("DELETE FROM exams WHERE examid = :examid;", compact('examid'));
            if ($res === false) {
                Message::addError('exam-not-deleted-error');
            } else {
                Message::addInfo('exam-deleted-success');
            }
            Util::redirect('?do=exams');
        } else {
            Util::traceError("unknown action");
        }
    }

    protected function doRender()
    {
        // Render::setTitle(Dictionary::translate('lang_exams'));
        //Render::addTemplate('page-exams', $_POST);

        if ($this->action === "show") {
            Render::setTitle("All Exams");
            Render::addTemplate('page-exams', ['exams' => $this->exams]);
        } elseif ($this->action === "add") {
            Render::setTitle("Add Exam");
            Render::addTemplate('page-add-exam', ['locations' => $this->locations]);
        }
        // Render::output('hi');
    }

}