1<?php
2/**
3 * Helper Plugin: Create, Read, Update and Delete on submissions
4 *
5 * @license GPL 3 (http://www.gnu.org/licenses/gpl.html)
6 * @author  Masoud Sadrnezhaad <masoud@sadrnezhaad.ir>
7 */
8
9
10if (!defined('DOKU_INC')) {
11    define('DOKU_INC', dirname(__FILE__) . '/../../../../');
12    include_once DOKU_INC . 'inc/init.php';
13    include_once DOKU_INC . 'inc/plugin.php';
14}
15
16class helper_plugin_judge_crud extends DokuWiki_Plugin
17{
18
19    function __construct()
20    {
21        define('DBFILE', dirname(dirname(__FILE__)) . '/submissions.sqlite');
22        date_default_timezone_set('Asia/Tehran');
23        $this->db = new SQLite3(DBFILE);
24    }
25
26    public function tableRender($data, $mode, $count = 1, $sort = "timestamp")
27    {
28        /**
29         * Find submissions
30         */
31        $results = $this->getSubmissions($data, $sort);
32
33        if (!$results) {
34            return Array('submissions_table' => null, 'count' => 0);
35        }
36
37        /**
38         * Building result table
39         */
40        $table = '';
41        $i = $count;
42        $table_data = Array();
43        if ($data["type"] == "output-only") {
44            while ($row = $results->fetchArray()) {
45                if ($mode == "html" && is_null($data["problem_name"])) {
46                    $table_data[] = Array($i, '<a href="' . $row["problem_name"] . '">' . $row["problem_name"] . '</a>', $this->convert_time($row["timestamp"]), ($row["status_code"] == '1' ? "درست" : "نادرست"));
47                } elseif ($mode == "html" && !is_null($data["problem_name"])) {
48                    $table_data[] = Array($i, $this->convert_time($row["timestamp"]), ($row["status_code"] == '1' ? "درست" : "نادرست"));
49                } elseif ($mode == "csv") {
50                    $table .= $i . "\toutput-only\t" . $row["problem_name"] . "\t" . $this->convert_time($row["timestamp"]) . "\t \t" . ($row["status_code"] == '1' ? "درست" : "نادرست") . "\n";
51                }
52                $i += 1;
53            }
54        } elseif ($data["type"] == "test-case") {
55            while ($row = $results->fetchArray()) {
56                $language = $this->
57                db->query('SELECT language FROM submission_test_case WHERE submit_id = ' . $row["submit_id"] . ';')->fetchArray();
58                if ($row["status_code"] == '0') {
59                    $valid_text = '<div class="loader"></div>';
60                } else {
61                    $valid_text = $this->valid_text($row["submit_id"]);
62                }
63
64                /**
65                 * table rendering
66                 */
67                if ($mode == "html" && is_null($data["user"])) {
68                    $table_data[] = Array($i, '<a href="' . $row["problem_name"] . '">' . $row["problem_name"] . '</a>', $row['username'], $this->convert_time($row["timestamp"]), $language[0], $valid_text);
69                } elseif ($mode == "html" && is_null($data["problem_name"])) {
70                    $table_data[] = Array($i, '<a href="' . $row["problem_name"] . '">' . $row["problem_name"] . '</a>', $this->convert_time($row["timestamp"]), $language[0], $valid_text);
71                } elseif ($mode == "html" && !is_null($data["problem_name"])) {
72                    $table_data[] = Array($i, $this->convert_time($row["timestamp"]), $language[0], $valid_text);
73                } elseif ($mode == "csv") {
74                    $table .= $i . "\ttest-case\t" . $row["problem_name"] . "\t" . $this->convert_time($row["timestamp"]) . "\t" . $language[0] . "\t" . $this->valid_text($row["submit_id"]) . "\n";
75                }
76                $i += 1;
77            }
78        } else {
79            while ($row = $results->fetchArray()) {
80                if ($row["type"] == "output-only") {
81                    $table_data[] = Array($i, '<a href="' . $row["problem_name"] . '">' . $row["problem_name"] . '</a>', $this->getLang('outputonly_question'), $this->convert_time($row["timestamp"]), ($row["status_code"] == '1' ? "درست" : "نادرست"));
82                } else {
83                    if (!$row["status_code"]) {
84                        $table_data[] = Array($i, '<a href="' . $row["problem_name"] . '">' . $row["problem_name"] . '</a>', $this->getLang('programming_question'), $this->convert_time($row["timestamp"]), '<div class="loader"></div>');
85                    } else {
86                        $table_data[] = Array($i, '<a href="' . $row["problem_name"] . '">' . $row["problem_name"] . '</a>', $this->getLang('programming_question'), $this->convert_time($row["timestamp"]), $this->valid_text($row["submit_id"]));
87                    }
88
89                }
90                $i += 1;
91            }
92        }
93        if ($mode == "html") {
94            $table_row = Array();
95            foreach ($table_data as &$data) {
96                $table_row[] = join($data, '</td><td class="col0">');
97            }
98            $table = '<tr class="row0"><td class="col0">' . join($table_row, '</td></tr><tr class="row0"><td class="col0">') . '</td></tr>';
99        }
100
101        return array('submissions_table' => $table, 'count' => $i);
102    }
103
104    public function getSubmissions($data, $sort = "timestamp")
105    {
106
107        /**
108         * Building the query
109         */
110        $query = array();
111        if (!empty($data["problem_name"])) {
112            $query[] = 'problem_name = "' . $data["problem_name"] . '"';
113        }
114        if (!empty($data["user"])) {
115            $query[] = 'username = "' . $data["user"] . '"';
116        }
117        if (!empty($data["type"])) {
118            $query[] = 'type = "' . $data["type"] . '"';
119        }
120
121        if (empty($data["problem_name"]) && empty($data["user"]) && empty($data["type"])) {
122            $query = 'SELECT * FROM submissions ORDER BY "' . $sort . '" ASC ;';
123        } else {
124            $query = 'SELECT * FROM submissions WHERE ' . join($query, " AND ") . ' ORDER BY "' . $sort . '" ASC ;';
125        }
126
127        /**
128         * Running the query
129         */
130        $results = $this->db->query($query);
131        if (!is_array($results->fetchArray())) {
132            return false;
133        }
134        $results->reset();
135        return $results;
136    }
137
138    public function convert_time($timestamp)
139    {
140        global $conf;
141        include_once dirname(__FILE__) . '/jdatetime.class.php';
142        $pdate = new jDateTime(false, true, 'Asia/Tehran');
143        if ($conf['lang'] == "fa") {
144            return $pdate->date("l j F Y H:i:s", $timestamp);
145        } else {
146            return date('l j F Y H:i:s', $timestamp);
147        }
148    }
149
150    public function valid_text($id)
151    {
152        $valid = $this->db->query('SELECT "valid" FROM submission_test_case WHERE submit_id = ' . $id . ';')->fetchArray();
153        switch ($valid[0]) {
154            case "0":
155                return "درست";
156            case "1":
157                return "نادرست";
158            case "2":
159                return "خطای زمان کامپایل";
160            case "3":
161                return "خطای زمان اجرا";
162            case "4":
163                return "خطای مدت اجرا";
164        }
165    }
166
167    public function delSubmissions($data)
168    {
169
170        /**
171         * Get Submissions
172         */
173        $results = $this->getSubmissions($data);
174
175        if (!$results) {
176            return $this->getLang("empty_result");
177        }
178
179        while ($row = $results->fetchArray()) {
180            /**
181             * Remove Uploaded Codes
182             */
183            if ($row["type"] == "test-case") {
184                $targetdir = $this->getConf('upload_path');
185                if (substr($targetdir, -1) != "/") {
186                    $targetdir .= "/";
187                }
188                if (substr($targetdir, 1) != "/") {
189                    $targetdir = "/" . $targetdir;
190                }
191                $file_pattern = $targetdir . $row["submit_id"] . ".*";
192                array_map("unlink", glob("$file_pattern"));
193
194                $this->db->query('DELETE FROM submission_test_case WHERE submit_id ="' . $row["submit_id"] . '";');
195            }
196
197            $this->db->query('DELETE FROM submissions WHERE submit_id ="' . $row["submit_id"] . '";');
198
199        }
200
201        return $this->getLang("delete_success");
202    }
203
204}