1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
require 'common.inc.php'; |
4
|
|
|
|
5
|
|
|
use Battis\BootstrapSmarty\NotificationMessage; |
6
|
|
|
|
7
|
|
|
$STEP_INSTRUCTIONS = 1; |
8
|
|
|
$STEP_PRUNE = 2; |
9
|
|
|
|
10
|
|
|
$toolbox->cache_pushKey(basename(__FILE__, '.php')); |
11
|
|
|
|
12
|
|
|
$step = (empty($_REQUEST['step']) ? $STEP_INSTRUCTIONS : $_REQUEST['step']); |
13
|
|
|
|
14
|
|
|
switch ($step) { |
15
|
|
|
case $STEP_PRUNE: |
16
|
|
|
/* generate CSV for download */ |
17
|
|
|
try { |
18
|
|
|
$toolbox->cache_pushKey($_REQUEST['account']); |
19
|
|
|
$expired = []; |
20
|
|
|
foreach ($toolbox->getTermList() as $term) { |
21
|
|
|
if (strtotime($term['end_at']) < time()) { |
22
|
|
|
$expired[$term['id']] = true; |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
$prune = $toolbox->cache_get('prune'); |
26
|
|
|
if ($prune === false) { |
27
|
|
|
$prune[] = [ |
28
|
|
|
'user_id', |
29
|
|
|
'login_id', |
30
|
|
|
'sortable_name', |
31
|
|
|
'email', |
32
|
|
|
'status' |
33
|
|
|
]; |
34
|
|
|
$advisors = $toolbox->api_get("accounts/{$_REQUEST['account']}/users", [ |
35
|
|
|
'search_term' => '-advisor' |
36
|
|
|
]); |
37
|
|
|
|
38
|
|
|
foreach ($advisors as $advisor) { |
39
|
|
|
$observees = $toolbox->api_get("users/{$advisor['id']}/observees"); |
40
|
|
|
if ($observees->count() > 0) { |
41
|
|
|
$inactive = true; |
42
|
|
|
$courses = $toolbox->api_get("users/{$advisor['id']}/courses"); |
43
|
|
|
foreach ($courses as $course) { |
44
|
|
|
if (!isset($course['access_restricted_by_date']) && |
45
|
|
|
( |
46
|
|
|
isset($course['enrollment_term_id']) && |
47
|
|
|
!array_key_exists($course['enrollment_term_id'], $expired) |
48
|
|
|
) |
49
|
|
|
) { |
50
|
|
|
$inactive = false; |
51
|
|
|
break; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
if ($inactive) { |
55
|
|
|
$prune[] = [ |
56
|
|
|
$toolbox->blank($advisor, 'sis_user_id'), |
57
|
|
|
$toolbox->blank($advisor, 'login_id'), |
58
|
|
|
$toolbox->blank($advisor, 'sortable_name'), |
59
|
|
|
$toolbox->blank($advisor, 'email'), |
60
|
|
|
'deleted' |
61
|
|
|
]; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
$toolbox->getCache()->setLifetime(60); |
66
|
|
|
$toolbox->cache_set('prune', $prune); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$csv = urlencode($toolbox->getCache()->getHierarchicalKey('prune')); |
70
|
|
|
$filename = urlencode(date('Y-m-d_H-i-s') . "_prune-account-{$_REQUEST['account']}_observers"); |
71
|
|
|
$toolbox->smarty_assign([ |
72
|
|
|
'csv' => $csv, |
73
|
|
|
'filename' => $filename |
74
|
|
|
]); |
75
|
|
|
$toolbox->smarty_addMessage( |
76
|
|
|
'Ready for Download', |
77
|
|
|
"<a href=\"../generate-csv.php?data=$csv&" . |
78
|
|
|
"filename=$filename\">$filename</a> is ready and download " . |
79
|
|
|
'should start automatically in a few seconds. Click the link ' . |
80
|
|
|
'if the download does not start automatically.', |
81
|
|
|
NotificationMessage::SUCCESS |
82
|
|
|
); |
83
|
|
|
$toolbox->cache_popKey(); |
84
|
|
|
} catch (Exception $e) { |
85
|
|
|
$toolbox->smarty_addMessage('Error ' . $e->getCode(), $e->getMessage(), NotificationMessage::DANGER); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/* flows into $STEP_INSTRUCTIONS */ |
89
|
|
View Code Duplication |
case $STEP_INSTRUCTIONS: |
|
|
|
|
90
|
|
|
$toolbox->smarty_assign('formHidden', [ |
91
|
|
|
'step' => $STEP_PRUNE, |
92
|
|
|
'account' => $_SESSION[ACCOUNT_ID] |
93
|
|
|
]); |
94
|
|
|
$toolbox->smarty_display(basename(__FILE__, '.php') . '/instructions.tpl'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$toolbox->cache_popKey(); |
98
|
|
|
|
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.