/lenasys/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/lenasys/trunk
36.3.1 by Daniel Hermansson
Added login functionality
1
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2
3
	class Login extends CI_Controller {
4
		/*
5
		 *	Constructor
6
		 */
7
		function __construct() {
8
			parent::__construct();
9
			//Load required library
10
			$this->load->model('user', '', TRUE);
64.1.1 by b11johgu
ExamplesController:
11
			$this->load->model('Stats');
12
36.3.1 by Daniel Hermansson
Added login functionality
13
		}
14
	
15
		
16
		/*
17
		 *	This function runs when the user navigates directly to the login controller
18
		 */
19
		public function index() {
20
			//If user is already logged in
21
			if($this->user->isLoggedIn()) {
22
				//User already logged in
42.1.2 by Albin Larsson
Fixed bad URL formatting
23
				redirect(base_url().'home', 'refresh');
36.3.1 by Daniel Hermansson
Added login functionality
24
			} else {
25
				//Display the login form
26
				$this->drawLoginForm('');
27
			}
28
		}
29
		
30
		
31
		/*
32
		 *	This function validate the user input from login form using the library "form_validation".
33
		 *  NOTICE: This does NOT mean that it validates it against the database.
34
		 */
35
		public function validate() {
36
			//Load required library
37
			$this->load->library('form_validation');
38
			
39
			//Sets validation rules
40
			$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
41
			$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
42
			
43
			//Run validation
44
			if($this->form_validation->run() == FALSE) {
45
				//Field validation failed. Display login form (with error message).
46
				$this->drawLoginForm(validation_errors());
47
			} else {
48
				$username = $this->input->post('username');
49
				$password = $this->input->post('password');
50
				
51
				//Try to login
52
				if ($this->user->login($username, $password)) {
42.1.2 by Albin Larsson
Fixed bad URL formatting
53
					redirect(base_url().'home', 'refresh');
64.1.1 by b11johgu
ExamplesController:
54
36.3.1 by Daniel Hermansson
Added login functionality
55
				} else {
56
					$this->drawLoginForm('Access denied!');
57
				}
58
			}	
59
		}
60
		
61
	
62
		/*
63
		 *	This function draws the login form.
64
		 */
65
		private function drawLoginForm($errors) {
66
			//Load required library
67
			$this->load->helper(array('form'));
68
			
69
			//Display the view
70
			$data['error'] = $errors;
64.1.1 by b11johgu
ExamplesController:
71
			$stats = $this->Stats->statsTeacher();
72
			$data['browser'] = $stats['browser'];
73
			$data['version'] = $stats['version'];
74
			$data['platform'] = $stats['platform'];
75
			$data['numberOfCourses'] = $stats['numberOfCourses'];
76
77
			$headTagData = array(
78
				'cssFiles' => array('header', 'login_view'),
79
				'jsFiles' => array('header', 'userControls')
80
			);
81
			$this->load->view('headTag', array('headTagData' => $headTagData));	
82
			$userInfo = array(
83
				'userType' => $this->user->getUserType(),
84
				'userName' => $this->user->getUserName()
85
			);
86
			$this->load->view('header', $userInfo);
87
			
36.3.1 by Daniel Hermansson
Added login functionality
88
			$this->load->view('login_view', $data);
64.1.1 by b11johgu
ExamplesController:
89
36.3.1 by Daniel Hermansson
Added login functionality
90
		}
91
	}
92
?>