|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
require_once 'common.inc.php'; |
|
4
|
|
|
|
|
5
|
|
|
use smtech\StMarksColors as sm; |
|
6
|
|
|
use Battis\BootstrapSmarty\NotificationMessage; |
|
7
|
|
|
|
|
8
|
|
|
$toolbox->cache_pushKey(basename(__FILE__, '.php')); |
|
9
|
|
|
|
|
10
|
|
|
define('STEP_INSTRUCTIONS', 1); |
|
11
|
|
|
define('STEP_RESULT', 2); |
|
12
|
|
|
|
|
13
|
|
|
$step = (empty($_REQUEST['step']) ? STEP_INSTRUCTIONS : $_REQUEST['step']); |
|
14
|
|
|
|
|
15
|
|
|
switch ($step) { |
|
16
|
|
|
case STEP_RESULT: |
|
17
|
|
|
/* make a list of colors */ |
|
18
|
|
|
$colors = implode('|', sm::all()); |
|
19
|
|
|
|
|
20
|
|
|
$account = (empty($_REQUEST['account']) ? 1 : $_REQUEST['account']); |
|
21
|
|
|
if (empty($_REQUEST['account'])) { |
|
22
|
|
|
$toolbox->smarty_addMessage( |
|
23
|
|
|
'No Account', |
|
24
|
|
|
'Defaulting to the main account. (This may make things a bit slower.)', |
|
25
|
|
|
NotificationMessage::WARNING |
|
26
|
|
|
); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
if (empty($_REQUEST['term'])) { |
|
30
|
|
|
$toolbox->smarty_addMessage( |
|
31
|
|
|
'No Term', |
|
32
|
|
|
'Please select a term for which color blocks should be assigned.', |
|
33
|
|
|
NotificationMessage::ERROR |
|
|
|
|
|
|
34
|
|
|
); |
|
35
|
|
|
$step = STEP_INSTRUCTIONS; |
|
36
|
|
|
} else { |
|
37
|
|
|
$affected = array(); |
|
38
|
|
|
$unaffected = array(); |
|
39
|
|
|
|
|
40
|
|
|
$parentCourses = $toolbox->cache_get('parent courses'); |
|
41
|
|
|
if (!$parentCourses) { |
|
42
|
|
|
$parentCourses = []; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$colorAssignments = $toolbox->cache_get('color assignments'); |
|
46
|
|
|
if (!$colorAssignments) { |
|
47
|
|
|
$colorAssignments = []; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
try { |
|
51
|
|
|
$courses = $toolbox->api_get( |
|
52
|
|
|
"accounts/$account/courses", |
|
53
|
|
|
array( |
|
54
|
|
|
'enrollment_term_id' => $_REQUEST['term'] |
|
55
|
|
|
) |
|
56
|
|
|
); |
|
57
|
|
|
|
|
58
|
|
|
/* ...and get their sections... */ |
|
59
|
|
|
foreach ($courses as $course) { |
|
60
|
|
|
/* ...and figure out their _original_ course SIS ID... */ |
|
61
|
|
|
$sections = $toolbox->api_get("courses/{$course['id']}/sections"); |
|
62
|
|
|
foreach ($sections as $section) { |
|
63
|
|
|
$sis_course_id = (isset($parentCourses[$section['sis_section_id']]) ? |
|
64
|
|
|
$parentCourses[$section['sis_section_id']] : false |
|
65
|
|
|
); |
|
66
|
|
|
if ($sis_course_id === false) { |
|
67
|
|
|
$parentCourse = $course; |
|
68
|
|
|
if (!empty($section['nonxlist_course_id'])) { |
|
69
|
|
|
$parentCourse = $toolbox->api_get("courses/{$section['nonxlist_course_id']}"); |
|
70
|
|
|
} |
|
71
|
|
|
$sis_course_id = $parentCourse['sis_course_id']; |
|
72
|
|
|
$parentCourses[$section['sis_section_id']] = $sis_course_id; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/* ...figure out the proper block color... */ |
|
76
|
|
|
if (preg_match("/($colors)/i", $sis_course_id, $match)) { |
|
77
|
|
|
$color = sm::get(strtolower($match[1]))->dark()->value(); |
|
78
|
|
|
|
|
79
|
|
|
/* ...and set it for all enrolled users. */ |
|
80
|
|
|
$enrollments = $toolbox->api_get("sections/{$section['id']}/enrollments", array('state' => 'active')); |
|
81
|
|
|
$users = 0; |
|
82
|
|
|
foreach ($enrollments as $enrollment) { |
|
83
|
|
|
if ($enrollment['user']['name'] !== 'Test Student' && !isset($colorAssignments[$enrollment['user']['id']][$course['id']])) { |
|
84
|
|
|
$response = $toolbox->api_put( |
|
85
|
|
|
"users/{$enrollment['user']['id']}/colors/course_{$course['id']}", |
|
86
|
|
|
array( |
|
87
|
|
|
'hexcode' => $color |
|
88
|
|
|
) |
|
89
|
|
|
); |
|
90
|
|
|
$colorAssignments[$enrollment['user']['id']][$course['id']] = $response['hexcode']; |
|
91
|
|
|
$users++; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
$notification = "<a href=\"{$_SESSION[CANVAS_INSTANCE_URL]}/courses/{$course['id']}/sections/{$section['id']}\">{$section['name']}</a> <span style=\"color: #$color;\"><span class=\"glyphicon glyphicon-calendar\"></span></span> ($users / " . $enrollments->count() . " users)"; |
|
95
|
|
|
if ($users > 0) { |
|
96
|
|
|
$affected[] = $notification; |
|
97
|
|
|
} else { |
|
98
|
|
|
$unaffected[] = $notification; |
|
99
|
|
|
} |
|
100
|
|
|
} else { |
|
101
|
|
|
$unaffected[] = "<a href=\"{$_SESSION[CANVAS_INSTANCE_URL]}/courses/{$course['id']}/sections/{$section['id']}\">{$section['name']}</a> (Not assigned to a color block)"; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
} catch (Exception $e) { |
|
106
|
|
|
$toolbox->exceptionErrorMessage($e); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$toolbox->cache_set('parent courses', $parentCourses, \Battis\HierarchicalSimpleCache::IMMORTAL_LIFETIME); |
|
110
|
|
|
$toolbox->cache_set('color assignments', $colorAssignments, \Battis\HierarchicalSimpleCache::IMMORTAL_LIFETIME); |
|
111
|
|
|
|
|
112
|
|
|
$toolbox->smarty_addMessage( |
|
113
|
|
|
count($affected) . ' Color Blocks Assigned', |
|
114
|
|
|
'<dl>' . (count($unaffected) > 0 ? '<dt>No changes made to…</dt><dd><ol><li>' . implode('</li><li>', $unaffected) . '</li></ol></dd>' : '') . |
|
115
|
|
|
(count($affected) > 0 ? '<dt>Colors assigned for…</dt><dd><ol><li>' . implode('</li><li>', $affected) . '</li></ol></dd>' : '') . '</dl>', |
|
116
|
|
|
NotificationMessage::GOOD |
|
|
|
|
|
|
117
|
|
|
); |
|
118
|
|
|
} |
|
119
|
|
|
$toolbox->smarty_assign([ |
|
120
|
|
|
'account' => $_REQUEST['account'], |
|
121
|
|
|
'term' => $_REQUEST['term'] |
|
122
|
|
|
]); |
|
123
|
|
|
|
|
124
|
|
|
/* flows into STEP_INSTRUCTIONS */ |
|
125
|
|
|
|
|
126
|
|
|
case STEP_INSTRUCTIONS: |
|
127
|
|
|
default: |
|
128
|
|
|
$toolbox->smarty_assign('terms', $toolbox->getTermList()); |
|
129
|
|
|
$toolbox->smarty_assign('accounts', $toolbox->getAccountList()); |
|
130
|
|
|
$toolbox->smarty_assign('formHidden', array('step' => STEP_RESULT)); |
|
131
|
|
|
$toolbox->smarty_display(basename(__FILE__, '.php') . '/instructions.tpl'); |
|
132
|
|
|
} |
|
133
|
|
|
|
This class constant has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.