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
|
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Start extends CI_Controller {
/*
* Constructor
*/
function __construct() {
parent::__construct();
//Load required library
$this->load->model('user', '', TRUE);
$this->load->model('admin/admin_model', '', TRUE);
}
/*
*Temporary function to be able to be logged in to reach the page
*/
public function tempLogin() {
$this->load->model('user');
$loginDetails = array(
'username' => 'tempTeacher',
'name' => 'tempFoo',
'usertype' => 'Teacher',
'ssn' => '0000',
'activeCourse' => 'DA525G');
$this->session->set_userdata('authenticated', $loginDetails);
redirect(base_url().'start', 'refresh');
}
public function tempLogout() {
$this->load->model('user');
$this->session->unset_userdata('authenticated');
redirect(base_url().'home', 'refresh');
}
/*
* This function runs when the user navigates directly to the start controller
*/
public function index() {
if($this->user->isLoggedIn()) {
//User already logged in
redirect(base_url().'cms', 'refresh');
} else {
//Display the start page
$this->drawStartPage('');
}
}
/*
* This function draws the start page.
*/
private function drawStartPage() {
$userName = $this->user->getUserName();
$userType = $this->user->getUserType();
//Creates an array with active course info.
$activeCourse = $this->user->getActiveCourse();
//Creates an array with all courses.
$courses = $this->admin_model->getCourses();
//Creates an array with the variables that the bannermenu-view is expecting.
$data = array(
'userType' => $userType,
'userName' => $userName,
'activeCourse' => $activeCourse,
'courses' => $courses
);
//Creates an array with the necessary css- and jsfiles needed for the views that are about to be shown.
$headTagData = array(
'cssFiles' => array('bannermenu', 'startview'),
'jsFiles' => array('bannermenu', 'login')
);
//Puts the array above in <head></head>
$this->load->view('headTag', array('headTagData' => $headTagData));
$this->load->view('bannermenu', $data);
$this->load->view('startview', $data);
}
/* Login and logout functionality */
public function login() {
$this->load->library('user_agent');
$this->load->library('form_validation');
//Sets validation rules
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
//Run validation
if($this->form_validation->run() == FALSE) {
//Field validation failed. Display login form (with error message).
echo validation_errors();
} else {
$username = $this->input->post('username');
$password = $this->input->post('password');
$this->load->model('user');
//Try to login
if ($this->user->login($username, $password)) {
// If the login is successful, Redirects user to the page it came from
echo "true";
} else {
echo "<p>Wrong username or password</p>";
}
}
}
public function logout() {
$this->load->model('user');
if($this->user->isLoggedIn()) {
$this->session->unset_userdata('authenticated');
redirect($_SERVER['HTTP_REFERER']);
} else {
echo "You're not logged in!";
}
}
}
?>
|