1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
require_once('common.inc.php'); |
4
|
|
|
|
5
|
|
|
use Battis\DataUtilities; |
6
|
|
|
use Battis\BootstrapSmarty\NotificationMessage; |
7
|
|
|
|
8
|
|
|
define('STEP_INSTRUCTIONS', 1); |
9
|
|
|
define('STEP_NORMALIZE', 2); |
10
|
|
|
|
11
|
|
|
$step = (empty($_REQUEST['step']) ? STEP_INSTRUCTIONS : $_REQUEST['step']); |
12
|
|
|
|
13
|
|
|
switch ($step) { |
14
|
|
|
case STEP_NORMALIZE: |
15
|
|
|
$sections = DataUtilities::loadCsvToArray('csv'); |
16
|
|
|
$account = (empty($_REQUEST['account']) ? false : $_REQUEST['account']); |
17
|
|
|
$term = (empty($_REQUEST['term']) ? false : $_REQUEST['term']); |
18
|
|
|
|
19
|
|
|
if ($sections || $account || $term) { |
20
|
|
|
if ($sections) { |
21
|
|
|
$links = ""; |
22
|
|
|
foreach ($sections as $section) { |
23
|
|
|
try { |
24
|
|
|
$course = $toolbox->api_get("/courses/sis_course_id:{$section['course_id']}"); |
25
|
|
|
$courseSections = $toolbox->api_get("/courses/sis_course_id:{$section['course_id']}/sections"); |
26
|
|
|
|
27
|
|
|
/* do we have a singleton to rename? */ |
28
|
|
|
if ($courseSections->count() <= 1) { |
29
|
|
|
$params = array(); |
30
|
|
|
|
31
|
|
|
$_section = false; |
32
|
|
|
if ($courseSections->count() == 1) { |
33
|
|
|
$_section = $courseSections[0]; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if ($_section && $section['section_id'] != $_section['sis_section_id']) { |
37
|
|
|
$params['sis_section_id'] = $section['section_id']; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if ($_section && $course['name'] != $_section['name']) { |
41
|
|
|
$params['name'] = $course['name']; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if ($_section) { |
45
|
|
|
$response = $toolbox->api_put( |
46
|
|
|
"sections/{$_section['id']}", |
47
|
|
|
array( |
48
|
|
|
'course_section' => $params |
49
|
|
|
) |
50
|
|
|
); |
51
|
|
|
} else { |
52
|
|
|
$response = $toolbox->api_post( |
53
|
|
|
"courses/{$course['id']}/sections", |
54
|
|
|
array( |
55
|
|
|
'course_section[name]' => $course['name'], |
56
|
|
|
'course_section[sis_section_id]' => $section['section_id'] |
57
|
|
|
) |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (!empty($links)) { |
62
|
|
|
$links .= ', '; |
63
|
|
|
} |
64
|
|
|
$links .= "<a target=\"_parent\" href=\"{$_SESSION[CANVAS_INSTANCE_URL]}/courses/{$course['id']}/sections/{$response['id']}\">{$response['name']}</a>"; |
65
|
|
|
|
66
|
|
|
/* too many sections to (easily) normalize */ |
67
|
|
View Code Duplication |
} else { |
|
|
|
|
68
|
|
|
$toolbox->smarty_addMessage( |
69
|
|
|
'Multiple Sections', |
70
|
|
|
"<a target=\"_parent\" href=\"{$_SESSION[CANVAS_INSTANCE_URL]}/courses/{$course['id']}/settings\">{$course['name']}</a> has more than one section, which means that standard singleton-section normalization does not apply.", |
71
|
|
|
NotificationMessage::WARNING |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
} catch (Exception $e) { |
75
|
|
|
$toolbox->exceptionErrorMessage($e); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
$toolbox->smarty_addMessage( |
79
|
|
|
'Sections Normalized', |
80
|
|
|
'The following singleton course sections have been normalized to match their parent course title: ' . $links, |
81
|
|
|
NotificationMessage::GOOD |
|
|
|
|
82
|
|
|
); |
83
|
|
|
} else { |
84
|
|
|
$toolbox->smarty_addMessage( |
85
|
|
|
'Missing Constraint', |
86
|
|
|
'You must either upload a list of courses and sections, or select an account and/or term within which to normalize sections.', |
87
|
|
|
NotificationMessage::ERROR |
|
|
|
|
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/* flow into STEP_INSTRUCTIONS */ |
93
|
|
|
|
94
|
|
|
case STEP_INSTRUCTIONS: |
95
|
|
|
default: |
96
|
|
|
$toolbox->smarty_assign('accounts', $toolbox->getAccountList()); |
97
|
|
|
$toolbox->smarty_assign('terms', $toolbox->getTermList()); |
98
|
|
|
|
99
|
|
|
$toolbox->smarty_assign('formHidden', array('step' => STEP_NORMALIZE)); |
100
|
|
|
$toolbox->smarty_display(basename(__FILE__, '.php') . '/instructions.tpl'); |
101
|
|
|
} |
102
|
|
|
|
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.