New_account::index()   B
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 103
Code Lines 69

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 69
nc 4
nop 0
dl 0
loc 103
rs 8.1463
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php defined('BASEPATH') OR exit('No direct script access allowed');
2
3
class New_account extends My_Force_Login {
4
5
    public function __construct() {
6
        parent::__construct();
7
        $this->load->helper('download');
8
        $this->load->library('grocery_CRUD');
9
        $this->load->model('computing-support/New_account_model', 'new_account_model');
10
        
11
    }
12
13
    public function index() {
14
        
15
        if (in_array('CN=Dashboard_New_Staff_Account,OU=Dashboard_Group,OU=Intranet_Group,OU=Groups,DC=cant-col,DC=ac,DC=uk', $_SESSION['ldap']['groups']) || 
16
            in_array('CN=DG06,OU=Distribution Groups,OU=Email Groups,OU=Accounts,DC=cant-col,DC=ac,DC=uk', $_SESSION['ldap']['groups']) ||
17
            in_array('CN=Intranet_Edit_HR,OU=Intranet_Group,OU=Groups,DC=cant-col,DC=ac,DC=uk', $_SESSION['ldap']['groups'])) {
18
        
19
            $this->load->helper('form');
20
            $this->load->library('form_validation');
21
22
            $data = array();
23
            $data['faculty'] = $this->new_account_model->get_faculty();
24
            $data['department'] = $this->new_account_model->get_department();
25
26
            // validation rules
27
            $this->form_validation->set_rules('faculty', 'Faculty', 'trim|required');
28
            $this->form_validation->set_rules('first_name', 'First name', 'trim|required|min_length[3]');
29
            $this->form_validation->set_rules('last_name', 'Last name', 'trim|required|min_length[3]');
30
            $this->form_validation->set_rules('ern', 'Employee Reference no.', 'trim|required|min_length[7]');
31
            $this->form_validation->set_rules('position', 'Position', 'required');
32
            $this->form_validation->set_rules('room', 'Room', 'trim|required|min_length[3]');
33
            $this->form_validation->set_rules('ext', 'Phone Number', 'trim|required|numeric|min_length[4]');
34
            $this->form_validation->set_rules('con_start', 'Contract start date', 'trim|required|min_length[3]');
35
            $this->form_validation->set_rules('site', 'Site', 'trim|required|min_length[5]');
36
37
            if ($this->form_validation->run() === false) {
38
39
                $this->load->view('templates/header');
40
                $this->load->view('computing-support/new-account/view', $data);
41
                $this->load->view('templates/footer');
42
43
            } else {
44
45
                $first_name = $this->input->post('first_name');
46
                $last_name = $this->input->post('last_name');
47
                $ern = $this->input->post('ern');
48
                $position = $this->input->post('position');
49
                $faculty = $this->input->post('faculty');
50
                $department = $this->input->post('department');
51
                $room = $this->input->post('room');
52
                $ext = $this->input->post('ext');
53
                $con_start = $this->input->post('con_start');
54
                $con_end = $this->input->post('con_end');
55
                $password = $this->input->post('password');
56
                $site = $this->input->post('site');
57
58
                if ($this->new_account_model->create($first_name, $last_name, $ern, $position, $faculty, $department, $room, $ext, $con_start, $con_end, $password, $site)) {
59
60
                    $function = 'new_staff_account_submitted';
61
                    $this->user_model->function_log($function);
62
                 
63
                    $this->email->from('[email protected]', 'New Staff Account');
64
                    $this->email->to('[email protected]');
65
                    $this->email->bcc('[email protected]');
66
                    $this->email->subject('New Staff Account');
67
                    $this->email->message('A new staff account has been requested by '.$_SESSION['username'].'
68
                       Name: ' . $this->input->post('first_name') . ' ' . $this->input->post('last_name') . '
69
                       Position: ' . $this->input->post('position') . '
70
                       Department: ' . $this->input->post('department') . '
71
                           
72
                       By: ' . $_SESSION['ldap']['full_name'] . '
73
74
75
                       The script will automaticly download user details and mark as created.
76
                       Please create T-Drive when you recieve the PowerStaffScript email.');
77
                    $this->email->send();
78
                    
79
                    $this->email->from('[email protected]', 'New Staff Account');
80
                    $this->email->to($_SESSION['ldap']['email']);
81
                    $this->email->subject('New Staff Account');
82
                    $this->email->message('You have submitted a new staff account request for
83
                       Name: ' . $this->input->post('first_name') . ' ' . $this->input->post('last_name') . '
84
                       Position: ' . $this->input->post('position') . '
85
                           
86
                       There password is: '.$password.'
87
                       Check their username here: https://intranet.cant-col.ac.uk/dashboard/computing-support/new-account/pending
88
89
                       This account will be available for the user tomorrow.');
90
                    $this->email->send();
91
92
                    // user created
93
                    $this->load->view('templates/header');
94
                    $this->load->view('computing-support/new-account/created');
95
                    $this->load->view('templates/footer');
96
97
                } else {
98
                    
99
                    $function = 'new_staff_account_error';
100
                    $this->user_model->function_log($function);
101
102
                    $data = new stdClass();
103
                    $data->error = 'There was a problem creating this account. Please try again.';
104
105
                    // failed to create user
106
                    $this->load->view('templates/header');
107
                    $this->load->view('computing-support/new-account/view', $data);
108
                    $this->load->view('templates/footer');
109
110
                }	
111
            }    
112
        } else {
113
            redirect('permissions');
114
        }
115
    }
116
    
117
    public function history() {
118
        
119
        if (in_array('CN=Dashboard_New_Staff_Account,OU=Dashboard_Group,OU=Intranet_Group,OU=Groups,DC=cant-col,DC=ac,DC=uk', $_SESSION['ldap']['groups']) || 
120
            in_array('CN=DG06,OU=Distribution Groups,OU=Email Groups,OU=Accounts,DC=cant-col,DC=ac,DC=uk', $_SESSION['ldap']['groups']) ||
121
            in_array('CN=Intranet_Edit_HR,OU=Intranet_Group,OU=Groups,DC=cant-col,DC=ac,DC=uk', $_SESSION['ldap']['groups'])) {
122
        
123
            $crud = new grocery_CRUD();
124
            $crud->set_table('new_account');
125
            $crud->set_subject('new_account', 'New Account');
126
            $crud->display_as('ern','ER no.')->display_as('ext','Phone Number')->display_as('con_start','Contract Start')->display_as('con_end','Contract End');
127
            $crud->edit_fields('complete');
128
            $crud->unset_add();
129
            $crud->unset_read();
130
            $crud->unset_delete();
131
            $output = $crud->render();
132
133
            $this->load->view('templates/header.php');
134
            $this->load->view('computing-support/new-account/history', $output);
135
            $this->load->view('templates/footer.php');
136
            $this->load->view('templates/table_assets.php', $output);
137
            
138
        } else {
139
            redirect('permissions');
140
        }
141
        
142
    }
143
    
144
    public function pending() {
145
        
146
        if (in_array('CN=Dashboard_New_Staff_Account,OU=Dashboard_Group,OU=Intranet_Group,OU=Groups,DC=cant-col,DC=ac,DC=uk', $_SESSION['ldap']['groups']) || 
147
            in_array('CN=DG06,OU=Distribution Groups,OU=Email Groups,OU=Accounts,DC=cant-col,DC=ac,DC=uk', $_SESSION['ldap']['groups']) ||
148
            in_array('CN=Intranet_Edit_HR,OU=Intranet_Group,OU=Groups,DC=cant-col,DC=ac,DC=uk', $_SESSION['ldap']['groups'])) {
149
        
150
            $crud = new grocery_CRUD();
151
            $crud->set_table('new_account_pending');
152
            $crud->set_subject('pending', 'Pending');
153
            $crud->set_primary_key('id');
154
            $crud->unset_edit_fields('id', 'password', 'username');
155
            $crud->unset_add();
156
            $crud->unset_read();
157
            $output = $crud->render();
158
159
            $this->load->view('templates/header.php');
160
            $this->load->view('computing-support/new-account/pending', $output);
161
            $this->load->view('templates/footer.php');
162
            $this->load->view('templates/table_assets.php', $output);
163
            
164
        } else {
165
            redirect('permissions');
166
        }
167
        
168
    }
169
    
170
    // If changed are made, duplicate in New_staff_export.php controller.
171
172 View Code Duplication
    public function complete() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
173
        
174
        if (in_array('CN=Dashboard_Admin,OU=Dashboard_Group,OU=Intranet_Group,OU=Groups,DC=cant-col,DC=ac,DC=uk', $_SESSION['ldap']['groups'])) {
175
            
176
            $function = 'complete_staff_accounts_manual';
177
            $this->user_model->function_log($function);
178
        
179
            $this->new_account_model->complete_account();
180
181
            $this->load->view('templates/header');
182
            $this->load->view('computing-support/new-account/complete');
183
            $this->load->view('templates/footer');
184
            
185
        } else {
186
            redirect('permissions');
187
        }
188
    }
189
    
190
    // If changed are made, duplicate in New_staff_export.php controller.
191
     
192 View Code Duplication
    function export() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
193
        
194
        if (in_array('CN=DG06,OU=Distribution Groups,OU=Email Groups,OU=Accounts,DC=cant-col,DC=ac,DC=uk', $_SESSION['ldap']['groups'])) {
195
            
196
            $function = 'export_staff_account_manual';
197
            $this->user_model->function_log($function);
198
        
199
            $this->load->dbutil();
200
            //MySQL View - only export incomplete
201
            $query = $this->db->query("SELECT * FROM new_account_export");
202
            $delimiter = ",";
203
            $newline = "\n";
204
            $output = $this->dbutil->csv_from_result($query, $delimiter, $newline);
205
206
            function clean_export($string) {
0 ignored issues
show
Best Practice introduced by
The function clean_export() has been defined more than once; this definition is ignored, only the first definition in dashboard/application/co...g-support/Disposals.php (L153-156) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
207
                $string = str_replace('"', '', $string); // Replaces all spaces with hyphens - Required by SAD02-46 script.
208
                return $string;
209
            }
210
211
            $output = clean_export($output);
212
            force_download("newstaff.csv", $output);
213
            
214
        } else {
215
            redirect('permissions');
216
        }
217
    }
218
    
219
    public function disable_account() {
220
        
221
        if (in_array('CN=Dashboard_New_Staff_Account,OU=Dashboard_Group,OU=Intranet_Group,OU=Groups,DC=cant-col,DC=ac,DC=uk', $_SESSION['ldap']['groups']) || 
222
            in_array('CN=DG06,OU=Distribution Groups,OU=Email Groups,OU=Accounts,DC=cant-col,DC=ac,DC=uk', $_SESSION['ldap']['groups']) ||
223
            in_array('CN=Intranet_Edit_HR,OU=Intranet_Group,OU=Groups,DC=cant-col,DC=ac,DC=uk', $_SESSION['ldap']['groups'])) {
224
        
225
            $this->load->helper('form');
226
            $this->load->library('form_validation');
227
228
            // validation rules
229
            $this->form_validation->set_rules('full_name', 'Full Name', 'trim|required');
230
            $this->form_validation->set_rules('last', 'Last Day', 'trim|required');
231
232
            if ($this->form_validation->run() === false) {
233
234
                $this->load->view('templates/header');
235
                $this->load->view('computing-support/new-account/disable');
236
                $this->load->view('templates/footer');
237
238
            } else {
239
240
                $full_name = $this->input->post('full_name');
241
                $last = $this->input->post('last');
242
                $full_name2 = $this->input->post('full_name2');
243
                $last2 = $this->input->post('last2');
244
                $full_name3 = $this->input->post('full_name3');
245
                $last3 = $this->input->post('last3');
246
                $full_name4 = $this->input->post('full_name4');
247
                $last4 = $this->input->post('last4');
248
                $full_name5 = $this->input->post('full_name5');
249
                $last5 = $this->input->post('last5');
250
                $full_name6 = $this->input->post('full_name6');
251
                $last6 = $this->input->post('last6');
252
                $full_name7 = $this->input->post('full_name7');
253
                $last7 = $this->input->post('last7');
254
                $full_name8 = $this->input->post('full_name8');
255
                $last8 = $this->input->post('last8');
256
                $full_name9 = $this->input->post('full_name9');
257
                $last9 = $this->input->post('last9');
258
                $full_name10 = $this->input->post('full_name10');
259
                $last10 = $this->input->post('last10');
260
261
                if ($this->new_account_model->disable($full_name, $last)) {
262
                    
263
                    $function = 'disable_staff_account_submittion';
264
                    $this->user_model->function_log($function);
265
                
266
                    if (isset($full_name2)) {
267
                        $full_name = $full_name2;
268
                        $last = $last2;
269
                        $this->new_account_model->disable($full_name, $last);
270
                    } if (isset($full_name3)) {
271
                        $full_name = $full_name3;
272
                        $last = $last3;
273
                        $this->new_account_model->disable($full_name, $last);
274
                    } if (isset($full_name4)) {
275
                        $full_name = $full_name4;
276
                        $last = $last4;
277
                        $this->new_account_model->disable($full_name, $last);
278
                    } if (isset($full_name5)) {
279
                        $full_name = $full_name5;
280
                        $last = $last5;
281
                        $this->new_account_model->disable($full_name, $last);
282
                    } if (isset($full_name6)) {
283
                        $full_name = $full_name6;
284
                        $last = $last6;
285
                        $this->new_account_model->disable($full_name, $last);
286
                    } if (isset($full_name7)) {
287
                        $full_name = $full_name7;
288
                        $last = $last7;
289
                        $this->new_account_model->disable($full_name, $last);
290
                    } if (isset($full_name8)) {
291
                        $full_name = $full_name8;
292
                        $last = $last8;
293
                        $this->new_account_model->disable($full_name, $last);
294
                    } if (isset($full_name9)) {
295
                        $full_name = $full_name9;
296
                        $last = $last9;
297
                        $this->new_account_model->disable($full_name, $last);
298
                    } if (isset($full_name10)) {
299
                        $full_name = $full_name10;
300
                        $last = $last10;
301
                        $this->new_account_model->disable($full_name, $last);
302
                    }
303
304
                    $this->email->from('[email protected]', 'Disable Staff Account');
305
                    $this->email->to('[email protected]');
306
                    $this->email->subject('Disable Staff Account');
307
                    $this->email->message('A staff account needs to be disabled and has been requested by '.$_SESSION['username'].'
308
                       Name: ' . $this->input->post('full_name') . '
309
                       Last Day: ' . $this->input->post('last') . '
310
                           
311
                       By: ' . $_SESSION['ldap']['full_name'].'
312
                           
313
                       https://intranet.cant-col.ac.uk/dashboard/computing-support/new-account/disable-account/pending');
314
                    $this->email->send();
315
316
                    $this->load->view('templates/header');
317
                    $this->load->view('computing-support/new-account/disabled');
318
                    $this->load->view('templates/footer');
319
320
                } else {
321
                    
322
                    $function = 'disable_staff_account_error';
323
                    $this->user_model->function_log($function);
324
325
                    $data = new stdClass();
326
                    $data->error = 'There was a problem making this request. Please try again.';
327
328
                    // failed to create user
329
                    $this->load->view('templates/header');
330
                    $this->load->view('computing-support/new-account/disable', $data);
331
                    $this->load->view('templates/footer');
332
333
                }	
334
            }    
335
        } else {
336
            redirect('permissions');
337
        }
338
    }
339
    
340 View Code Duplication
    public function history_disabled_account() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
341
        
342
        if (in_array('CN=Dashboard_New_Staff_Account,OU=Dashboard_Group,OU=Intranet_Group,OU=Groups,DC=cant-col,DC=ac,DC=uk', $_SESSION['ldap']['groups']) || 
343
            in_array('CN=DG06,OU=Distribution Groups,OU=Email Groups,OU=Accounts,DC=cant-col,DC=ac,DC=uk', $_SESSION['ldap']['groups']) ||
344
            in_array('CN=Intranet_Edit_HR,OU=Intranet_Group,OU=Groups,DC=cant-col,DC=ac,DC=uk', $_SESSION['ldap']['groups'])) {
345
        
346
            $crud = new grocery_CRUD();
347
            $crud->set_table('disable_account');
348
            $crud->set_subject('disable_account', 'Disable Account');
349
            $crud->unset_add();
350
            $crud->unset_read();
351
            $crud->unset_delete();
352
            $output = $crud->render();
353
354
            $this->load->view('templates/header.php');
355
            $this->load->view('computing-support/new-account/disable_history', $output);
356
            $this->load->view('templates/footer.php');
357
            $this->load->view('templates/table_assets.php', $output);
358
            
359
        } else {
360
            redirect('permissions');
361
        }
362
        
363
    }
364
    
365 View Code Duplication
    public function pending_disabled_account() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
366
        
367
        if (in_array('CN=Dashboard_New_Staff_Account,OU=Dashboard_Group,OU=Intranet_Group,OU=Groups,DC=cant-col,DC=ac,DC=uk', $_SESSION['ldap']['groups']) || 
368
            in_array('CN=DG06,OU=Distribution Groups,OU=Email Groups,OU=Accounts,DC=cant-col,DC=ac,DC=uk', $_SESSION['ldap']['groups']) ||
369
            in_array('CN=Intranet_Edit_HR,OU=Intranet_Group,OU=Groups,DC=cant-col,DC=ac,DC=uk', $_SESSION['ldap']['groups'])) {
370
        
371
            $crud = new grocery_CRUD();
372
            $crud->set_table('disable_account_pending');
373
            $crud->set_subject('pending', 'Pending');
374
            $crud->set_primary_key('id');
375
            $crud->unset_columns('id'); 
376
            $crud->unset_add();
377
            $crud->unset_read();
378
            $output = $crud->render();
379
380
            $this->load->view('templates/header.php');
381
            $this->load->view('computing-support/new-account/disable_pending', $output);
382
            $this->load->view('templates/footer.php');
383
            $this->load->view('templates/table_assets.php', $output);
384
            
385
        } else {
386
            redirect('permissions');
387
        }
388
        
389
    }
390
}
391