1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace smtech\CanvasManagement; |
4
|
|
|
|
5
|
|
|
use Battis\BootstrapSmarty\NotificationMessage; |
6
|
|
|
use Battis\DataUtilities; |
7
|
|
|
use Battis\HierarchicalSimpleCache; |
8
|
|
|
use smtech\LTI\Configuration\Option; |
9
|
|
|
|
10
|
|
|
class Toolbox extends \smtech\StMarksReflexiveCanvasLTI\Toolbox |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Configure course and account navigation placements |
14
|
|
|
* |
15
|
|
|
* @return \smtech\LTI\Configuration\Generator |
16
|
|
|
*/ |
17
|
|
|
public function getGenerator() |
18
|
|
|
{ |
19
|
|
|
parent::getGenerator(); |
20
|
|
|
|
21
|
|
|
$this->generator->setOptionProperty( |
22
|
|
|
Option::ACCOUNT_NAVIGATION(), |
23
|
|
|
'visibility', |
24
|
|
|
'admins' |
25
|
|
|
); |
26
|
|
|
|
27
|
|
|
return $this->generator; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Explode a string |
32
|
|
|
* |
33
|
|
|
* Explode into comma- and newline-delineated parts, and trim those parts. |
34
|
|
|
* |
35
|
|
|
* @param string $str |
36
|
|
|
* |
37
|
|
|
* @return string[] |
38
|
|
|
**/ |
39
|
|
|
public function explodeCommaAndNewlines($str) |
40
|
|
|
{ |
41
|
|
|
$list = array(); |
42
|
|
|
$lines = explode("\n", $str); |
43
|
|
|
foreach ($lines as $line) { |
44
|
|
|
$items = explode(',', $line); |
45
|
|
|
foreach ($items as $item) { |
46
|
|
|
$trimmed = trim($item); |
47
|
|
|
if (!empty($trimmed)) { |
48
|
|
|
$list[] = $trimmed; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
return $list; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Explode a string |
57
|
|
|
* |
58
|
|
|
* Explode into trimmed lines |
59
|
|
|
* |
60
|
|
|
* @param string $str |
61
|
|
|
* |
62
|
|
|
* @return string[] |
63
|
|
|
**/ |
64
|
|
|
public function explodeNewLines($str) |
65
|
|
|
{ |
66
|
|
|
$list = array(); |
67
|
|
|
$lines = explode("\n", $str); |
68
|
|
|
foreach ($lines as $line) { |
69
|
|
|
$trimmed = trim($line); |
70
|
|
|
if (!empty($trimmed)) { |
71
|
|
|
$list[] = $trimmed; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
return $list; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get a listing of all accounts organized for presentation in a select picker |
79
|
|
|
* |
80
|
|
|
* @return array |
81
|
|
|
**/ |
82
|
|
View Code Duplication |
public function getAccountList() |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
$cache = new HierarchicalSimpleCache($this->getMySQL(), __CLASS__); |
85
|
|
|
|
86
|
|
|
$accounts = $cache->getCache('accounts'); |
87
|
|
|
if ($accounts === false) { |
88
|
|
|
$accountsResponse = $this->api_get('accounts/1/sub_accounts', [ |
89
|
|
|
'recursive' => 'true' |
90
|
|
|
]); |
91
|
|
|
$accounts = array(); |
92
|
|
|
foreach ($accountsResponse as $account) { |
|
|
|
|
93
|
|
|
$accounts[$account['id']] = $account; |
94
|
|
|
} |
95
|
|
|
$cache->setCache('accounts', $accounts, 7 * 24 * 60 * 60); |
96
|
|
|
} |
97
|
|
|
return $accounts; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get a listing of all terms organized for presentation in a select picker |
102
|
|
|
* |
103
|
|
|
* @return array |
104
|
|
|
**/ |
105
|
|
View Code Duplication |
public function getTermList() |
|
|
|
|
106
|
|
|
{ |
107
|
|
|
$cache = new HierarchicalSimpleCache($this->getMySQL(), __CLASS__); |
108
|
|
|
|
109
|
|
|
$terms = $cache->getCache('terms'); |
110
|
|
|
if ($terms === false) { |
111
|
|
|
$_terms = $this->api_get('accounts/1/terms', [ |
112
|
|
|
'workflow_state' => 'active' |
113
|
|
|
]); |
114
|
|
|
$termsResponse = $_terms['enrollment_terms']; |
115
|
|
|
$terms = array(); |
116
|
|
|
foreach ($termsResponse as $term) { |
117
|
|
|
$terms[$term['id']] = $term; |
118
|
|
|
} |
119
|
|
|
$cache->setCache('terms', $terms, 7 * 24 * 60 * 60); |
120
|
|
|
} |
121
|
|
|
return $terms; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* A standard format for an error message due to an exception |
126
|
|
|
* |
127
|
|
|
* @param \Exception $e |
128
|
|
|
* |
129
|
|
|
* @return void |
130
|
|
|
**/ |
131
|
|
|
public function exceptionErrorMessage($e) |
132
|
|
|
{ |
133
|
|
|
$this->smarty_addMessage( |
134
|
|
|
'Error ' . $e->getCode(), |
135
|
|
|
'<p>Last API Request</p><pre>' . |
136
|
|
|
print_r($this->getAPI()->last_request, true) . |
137
|
|
|
'</pre><p>Last Headers</p><pre>' . |
138
|
|
|
print_r($this->getAPI()->last_headers, true) . |
139
|
|
|
'</pre><p>Error Message</p><pre>' . $e->getMessage() . '</pre>', |
140
|
|
|
NotificationMessage::ERROR |
|
|
|
|
141
|
|
|
); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function buildMenu($path, $ignore, $ignoreFiles = true) |
145
|
|
|
{ |
146
|
|
|
$menuItems = []; |
147
|
|
|
if (is_dir($path)) { |
148
|
|
|
$dir = opendir($path); |
149
|
|
|
while ($file = readdir($dir)) { |
150
|
|
|
if (substr($file, 0, 1) != '.') { |
151
|
|
|
if (is_dir("$path/$file") && array_search($file, $ignore) === false) { |
152
|
|
|
$menuItems[$file]['submenu'] = $this->buildMenu("$path/$file", $ignore, false); |
153
|
|
|
} elseif (!$ignoreFiles && is_file("$path/$file") && preg_match('/^[^.]+\.php$/i', $file)) { |
154
|
|
|
$menuItems[$file]['url'] = DataUtilities::URLfromPath("$path/$file"); |
155
|
|
|
} |
156
|
|
|
if (!empty($menuItems[$file])) { |
157
|
|
|
preg_match('/^(-?\d+)[-_](.*)$/', $file, $match); |
158
|
|
|
$menuItems[$file]['title'] = DataUtilities::titleCase( |
159
|
|
|
str_replace('-', ' ', basename((empty($match[2]) ? $file : $match[2]), '.php')) |
160
|
|
|
); |
161
|
|
|
if (!empty($match[1])) { |
162
|
|
|
$menuItems[$file]['order'] = (int) $match[1]; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
closedir($dir); |
168
|
|
|
} |
169
|
|
|
uasort($menuItems, function ($left, $right) { |
170
|
|
|
if (!empty($left['order'])) { |
171
|
|
|
if (!empty($right['order'])) { |
172
|
|
|
return $left['order'] - $right['order']; |
173
|
|
|
} else { |
174
|
|
|
return -1; |
175
|
|
|
} |
176
|
|
|
} elseif (!empty($right['order'])) { |
177
|
|
|
return 1; |
178
|
|
|
} else { |
179
|
|
|
return 0; |
180
|
|
|
} |
181
|
|
|
}); |
182
|
|
|
return $menuItems; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
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.