-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.php
More file actions
194 lines (167 loc) · 5.22 KB
/
Copy pathcontroller.php
File metadata and controls
194 lines (167 loc) · 5.22 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
require('model.php');
require('view.php');
class Gradebook_controller
{
private $model;
private $views;
private $view = '';
private $action = '';
private $message = '';
private $data = array();
public function __construct()
{
$this->model = new Gradebook_model();
$this->views = new Gradebook_views();
$this->view = $_GET['view'] ? $_GET['view'] : 'gradesList';
$this->action = $_POST['action'];
}
public function __destruct()
{
$this->model = null;
$this->views = null;
}
public function run()
{
$this->processLogout();
switch ($this->action) {
case 'login':
$this->handleLogin();
break;
case 'delete_grade':
$this->handleRemoveGrade();
break;
case 'edit_grade':
$this->handleEditGrade();
break;
case 'add_grade':
$this->handleAddGrade();
break;
case 'add_student':
$this->handleAddStudent();
break;
default:
$this->verifyLogin();
}
switch ($this->view) {
case 'loginForm':
print $this->views->loginFormView($this->data, $this->message);
break;
case 'gradeFormAdd':
print $this->views->gradeFormAddView($this->data, $this->message);
break;
case 'gradeFormEdit':
print $this->views->gradeFormEditView($this->data, $this->message);
break;
case 'studentForm':
print $this->views->studentFormView($this->data, $this->message);
break;
case 'addStudent':
print $this->views->addStudentView();
break;
default: // 'gradesList'
$grades = $this->model->viewGrades();
$students = $this->model->viewStudents();
print $this->views->gradesListView($students, $grades, $this->message);
}
}
// GRADES ---
// create grade
private function handleAddGrade()
{
if (!$this->verifyLogin()) {
return;
}
if ($_POST['cancel']) {
$this->view = 'gradesList';
return;
}
$error = $this->model->addGrade($_POST,$_GET);
if ($error) {
$this->message = $error;
$this->view = 'gradeFormAdd';
$this->data = $_POST;
}
}
// update grade
private function handleEditGrade()
{
if (!$this->verifyLogin()) {
return;
}
if ($_POST['cancel']) {
$this->view = 'gradesList';
return;
}
$error = $this->model->editGrade($_POST);
if ($error) {
$this->message = $error;
$this->view = 'gradeFormEdit';
$this->data = $_POST;
}
}
// delete grade
private function handleRemoveGrade()
{
if (!$this->verifyLogin()) {
return;
}
if ($_POST['cancel']) {
$this->view = 'gradesList';
return;
}
$error = $this->model->removeGrade($_POST);
if ($error) {
$this->message = $error;
$this->view = 'gradesList';
$this->data = $_POST;
}
}
// STUDENT ---
// create student
private function handleAddStudent()
{
if (!$this->verifyLogin()) {
return;
}
if ($_POST['cancel']) {
$this->view = 'gradesList';
return;
}
$error = $this->model->addStudent($_POST);
if ($error) {
$this->message = $error;
$this->view = 'addStudent';
$this->data = $_POST;
}
}
// TEACHER ---
// LOGIN ---
private function processLogout()
{
if ($_GET['logout']) {
$this->model->logout();
} else {
}
}
private function verifyLogin()
{
if (!$this->model->getStatus()) {
$this->view = 'loginForm';
return false;
} else {
return true;
}
}
private function handleLogin()
{
list($success, $message) = $this->model->check_login($_POST);
if ($success) {
$this->view = 'gradesList';
} else {
$this->message = $message;
$this->view = 'loginForm';
$this->data = $_POST;
}
}
}