@@ -32,170 +32,170 @@ |
||
| 32 | 32 | } |
| 33 | 33 | |
| 34 | 34 | switch ($step) { |
| 35 | - case STEP_CONFIRM: |
|
| 36 | - try { |
|
| 37 | - $users = $toolbox->explodeCommaAndNewlines($_REQUEST['users']); |
|
| 38 | - |
|
| 39 | - if (empty($_REQUEST['course'])) { |
|
| 40 | - $toolbox->smarty_addMessage( |
|
| 41 | - 'Course', |
|
| 42 | - 'was not selected, so no enrollments can happen', |
|
| 43 | - NotificationMessage::ERROR |
|
| 44 | - ); |
|
| 45 | - $step = STEP_INSTRUCTIONS; |
|
| 46 | - } else { |
|
| 47 | - $sections = $toolbox->cache_get("courses/{$_REQUEST['course']}"); |
|
| 48 | - if (empty($sections)) { |
|
| 49 | - $section = array(); |
|
| 50 | - $courses = $toolbox->api_get( |
|
| 51 | - 'accounts/1/courses', |
|
| 52 | - array( |
|
| 53 | - 'search_term' => $_REQUEST['course'] |
|
| 54 | - ) |
|
| 35 | + case STEP_CONFIRM: |
|
| 36 | + try { |
|
| 37 | + $users = $toolbox->explodeCommaAndNewlines($_REQUEST['users']); |
|
| 38 | + |
|
| 39 | + if (empty($_REQUEST['course'])) { |
|
| 40 | + $toolbox->smarty_addMessage( |
|
| 41 | + 'Course', |
|
| 42 | + 'was not selected, so no enrollments can happen', |
|
| 43 | + NotificationMessage::ERROR |
|
| 55 | 44 | ); |
| 56 | - foreach ($courses as $course) { |
|
| 57 | - $courseSections = $toolbox->api_get("courses/{$course['id']}/sections"); |
|
| 58 | - if ($courseSections->count() == 0) { |
|
| 59 | - /* we have only the "magic" default section */ |
|
| 60 | - $sections[] = array('course' => $course); |
|
| 61 | - } else { |
|
| 62 | - foreach ($courseSections as $section) { |
|
| 63 | - $sections[] = array( |
|
| 64 | - 'course' => $course, |
|
| 65 | - 'section' => $section |
|
| 66 | - ); |
|
| 45 | + $step = STEP_INSTRUCTIONS; |
|
| 46 | + } else { |
|
| 47 | + $sections = $toolbox->cache_get("courses/{$_REQUEST['course']}"); |
|
| 48 | + if (empty($sections)) { |
|
| 49 | + $section = array(); |
|
| 50 | + $courses = $toolbox->api_get( |
|
| 51 | + 'accounts/1/courses', |
|
| 52 | + array( |
|
| 53 | + 'search_term' => $_REQUEST['course'] |
|
| 54 | + ) |
|
| 55 | + ); |
|
| 56 | + foreach ($courses as $course) { |
|
| 57 | + $courseSections = $toolbox->api_get("courses/{$course['id']}/sections"); |
|
| 58 | + if ($courseSections->count() == 0) { |
|
| 59 | + /* we have only the "magic" default section */ |
|
| 60 | + $sections[] = array('course' => $course); |
|
| 61 | + } else { |
|
| 62 | + foreach ($courseSections as $section) { |
|
| 63 | + $sections[] = array( |
|
| 64 | + 'course' => $course, |
|
| 65 | + 'section' => $section |
|
| 66 | + ); |
|
| 67 | + } |
|
| 67 | 68 | } |
| 68 | 69 | } |
| 70 | + $toolbox->cache_set("courses/{$_REQUEST['course']}", $sections, CACHE_LIFETIME); |
|
| 69 | 71 | } |
| 70 | - $toolbox->cache_set("courses/{$_REQUEST['course']}", $sections, CACHE_LIFETIME); |
|
| 71 | - } |
|
| 72 | 72 | |
| 73 | - if (empty($sections)) { |
|
| 74 | - $toolbox->smarty_addMessage( |
|
| 75 | - 'No Courses', |
|
| 76 | - "matched your search term '{$_REQUEST['course']}'.", |
|
| 77 | - NotificationMessage::WARNING |
|
| 78 | - ); |
|
| 79 | - $step = STEP_INSTRUCTIONS; |
|
| 73 | + if (empty($sections)) { |
|
| 74 | + $toolbox->smarty_addMessage( |
|
| 75 | + 'No Courses', |
|
| 76 | + "matched your search term '{$_REQUEST['course']}'.", |
|
| 77 | + NotificationMessage::WARNING |
|
| 78 | + ); |
|
| 79 | + $step = STEP_INSTRUCTIONS; |
|
| 80 | + } |
|
| 80 | 81 | } |
| 81 | - } |
|
| 82 | 82 | |
| 83 | - if ($step == STEP_CONFIRM) { |
|
| 84 | - if (!empty($users)) { |
|
| 85 | - $confirm = array(); |
|
| 86 | - foreach ($users as $term) { |
|
| 87 | - $confirm[$term] = $toolbox->cache_get("users/$term"); |
|
| 88 | - if ($confirm[$term] === false) { |
|
| 89 | - $confirm[$term] = $toolbox->api_get( |
|
| 90 | - 'accounts/1/users', |
|
| 91 | - array( |
|
| 92 | - 'search_term' => $term, |
|
| 93 | - 'include[]' => 'term' |
|
| 94 | - ) |
|
| 95 | - ); |
|
| 96 | - $toolbox->cache_set("users/$term", $confirm[$term], CACHE_LIFETIME); |
|
| 83 | + if ($step == STEP_CONFIRM) { |
|
| 84 | + if (!empty($users)) { |
|
| 85 | + $confirm = array(); |
|
| 86 | + foreach ($users as $term) { |
|
| 87 | + $confirm[$term] = $toolbox->cache_get("users/$term"); |
|
| 88 | + if ($confirm[$term] === false) { |
|
| 89 | + $confirm[$term] = $toolbox->api_get( |
|
| 90 | + 'accounts/1/users', |
|
| 91 | + array( |
|
| 92 | + 'search_term' => $term, |
|
| 93 | + 'include[]' => 'term' |
|
| 94 | + ) |
|
| 95 | + ); |
|
| 96 | + $toolbox->cache_set("users/$term", $confirm[$term], CACHE_LIFETIME); |
|
| 97 | + } |
|
| 97 | 98 | } |
| 98 | - } |
|
| 99 | 99 | |
| 100 | - $toolbox->smarty_assign('sections', $sections); |
|
| 101 | - $toolbox->smarty_assign('terms', $toolbox->getTermList()); |
|
| 102 | - $toolbox->smarty_assign('confirm', $confirm); |
|
| 103 | - $toolbox->smarty_assign('roles', $toolbox->api_get('accounts/1/roles')); // TODO make this account-specific |
|
| 104 | - $toolbox->smarty_assign('formHidden', array('step' => STEP_ENROLL)); |
|
| 105 | - $toolbox->smarty_display(basename(__FILE__, '.php') . '/confirm.tpl'); |
|
| 106 | - break; |
|
| 107 | - } else { |
|
| 108 | - $toolbox->smarty_addMessage( |
|
| 109 | - 'Users', |
|
| 110 | - 'were not selected, so no enrollments can happen.', |
|
| 111 | - NotificationMessage::ERROR |
|
| 112 | - ); |
|
| 113 | - $step = STEP_INSTRUCTIONS; |
|
| 114 | - } |
|
| 115 | - } |
|
| 116 | - } catch (Pest_Exception $e) { |
|
| 117 | - $toolbox->exceptionErrorMessage($e); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /* flow into STEP_ENROLL (and STEP_INSTRUCTIONS) */ |
|
| 121 | - |
|
| 122 | - case STEP_ENROLL: |
|
| 123 | - try { |
|
| 124 | - if ($step == STEP_ENROLL) { |
|
| 125 | - $courseEnrollment = false; |
|
| 126 | - if (empty($_REQUEST['section'])) { |
|
| 127 | - if (!empty($_REQUEST['course'])) { |
|
| 128 | - $courseEnrollment = true; |
|
| 100 | + $toolbox->smarty_assign('sections', $sections); |
|
| 101 | + $toolbox->smarty_assign('terms', $toolbox->getTermList()); |
|
| 102 | + $toolbox->smarty_assign('confirm', $confirm); |
|
| 103 | + $toolbox->smarty_assign('roles', $toolbox->api_get('accounts/1/roles')); // TODO make this account-specific |
|
| 104 | + $toolbox->smarty_assign('formHidden', array('step' => STEP_ENROLL)); |
|
| 105 | + $toolbox->smarty_display(basename(__FILE__, '.php') . '/confirm.tpl'); |
|
| 106 | + break; |
|
| 129 | 107 | } else { |
| 130 | 108 | $toolbox->smarty_addMessage( |
| 131 | - 'Course or Section', |
|
| 132 | - 'Missing from enrollment request.', |
|
| 109 | + 'Users', |
|
| 110 | + 'were not selected, so no enrollments can happen.', |
|
| 133 | 111 | NotificationMessage::ERROR |
| 134 | 112 | ); |
| 135 | 113 | $step = STEP_INSTRUCTIONS; |
| 136 | 114 | } |
| 137 | 115 | } |
| 116 | + } catch (Pest_Exception $e) { |
|
| 117 | + $toolbox->exceptionErrorMessage($e); |
|
| 118 | + } |
|
| 138 | 119 | |
| 139 | - if (empty($_REQUEST['users'])) { |
|
| 140 | - $toolbox->smarty_addMessage( |
|
| 141 | - 'Users', |
|
| 142 | - 'missing from enrollment request.', |
|
| 143 | - NotificationMessage::ERROR |
|
| 144 | - ); |
|
| 145 | - } elseif ($step == STEP_ENROLL) { |
|
| 146 | - $count = 0; |
|
| 147 | - foreach ($_REQUEST['users'] as $user) { |
|
| 148 | - $enrollment = $toolbox->api_post( |
|
| 149 | - ( |
|
| 150 | - $courseEnrollment ? |
|
| 151 | - "/courses/{$_REQUEST['course']}/enrollments" : |
|
| 152 | - "/sections/{$_REQUEST['section']}/enrollments" |
|
| 153 | - ), |
|
| 154 | - array( |
|
| 155 | - 'enrollment[user_id]' => $user['id'], |
|
| 156 | - 'enrollment[role_id]' => $user['role'], |
|
| 157 | - 'enrollment[enrollment_state]' => 'active', |
|
| 158 | - 'enrollment[notify]' => (empty($user['notify']) ? 'false' : $user['notify']) |
|
| 159 | - ) |
|
| 160 | - ); |
|
| 161 | - if (!empty($enrollment['id'])) { |
|
| 162 | - $count++; |
|
| 163 | - } // FIXME should really list errors, no? |
|
| 164 | - } |
|
| 120 | + /* flow into STEP_ENROLL (and STEP_INSTRUCTIONS) */ |
|
| 165 | 121 | |
| 166 | - if ($courseEnrollment) { |
|
| 167 | - $course = $_REQUEST['course']; |
|
| 168 | - } else { |
|
| 169 | - $section = $toolbox->api_get("sections/{$_REQUEST['section']}"); |
|
| 170 | - $course = $section['course_id']; |
|
| 122 | + case STEP_ENROLL: |
|
| 123 | + try { |
|
| 124 | + if ($step == STEP_ENROLL) { |
|
| 125 | + $courseEnrollment = false; |
|
| 126 | + if (empty($_REQUEST['section'])) { |
|
| 127 | + if (!empty($_REQUEST['course'])) { |
|
| 128 | + $courseEnrollment = true; |
|
| 129 | + } else { |
|
| 130 | + $toolbox->smarty_addMessage( |
|
| 131 | + 'Course or Section', |
|
| 132 | + 'Missing from enrollment request.', |
|
| 133 | + NotificationMessage::ERROR |
|
| 134 | + ); |
|
| 135 | + $step = STEP_INSTRUCTIONS; |
|
| 136 | + } |
|
| 171 | 137 | } |
| 172 | 138 | |
| 173 | - // FIXME no longer have the course ID… link is broken |
|
| 174 | - $toolbox->smarty_addMessage( |
|
| 175 | - 'Success', |
|
| 176 | - "<a target=\"_top\" href=\"{$_SESSION[CANVAS_INSTANCE_URL]}/courses/$course/users\">$count users enrolled</a>", |
|
| 177 | - NotificationMessage::GOOD |
|
| 178 | - ); |
|
| 139 | + if (empty($_REQUEST['users'])) { |
|
| 140 | + $toolbox->smarty_addMessage( |
|
| 141 | + 'Users', |
|
| 142 | + 'missing from enrollment request.', |
|
| 143 | + NotificationMessage::ERROR |
|
| 144 | + ); |
|
| 145 | + } elseif ($step == STEP_ENROLL) { |
|
| 146 | + $count = 0; |
|
| 147 | + foreach ($_REQUEST['users'] as $user) { |
|
| 148 | + $enrollment = $toolbox->api_post( |
|
| 149 | + ( |
|
| 150 | + $courseEnrollment ? |
|
| 151 | + "/courses/{$_REQUEST['course']}/enrollments" : |
|
| 152 | + "/sections/{$_REQUEST['section']}/enrollments" |
|
| 153 | + ), |
|
| 154 | + array( |
|
| 155 | + 'enrollment[user_id]' => $user['id'], |
|
| 156 | + 'enrollment[role_id]' => $user['role'], |
|
| 157 | + 'enrollment[enrollment_state]' => 'active', |
|
| 158 | + 'enrollment[notify]' => (empty($user['notify']) ? 'false' : $user['notify']) |
|
| 159 | + ) |
|
| 160 | + ); |
|
| 161 | + if (!empty($enrollment['id'])) { |
|
| 162 | + $count++; |
|
| 163 | + } // FIXME should really list errors, no? |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + if ($courseEnrollment) { |
|
| 167 | + $course = $_REQUEST['course']; |
|
| 168 | + } else { |
|
| 169 | + $section = $toolbox->api_get("sections/{$_REQUEST['section']}"); |
|
| 170 | + $course = $section['course_id']; |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + // FIXME no longer have the course ID… link is broken |
|
| 174 | + $toolbox->smarty_addMessage( |
|
| 175 | + 'Success', |
|
| 176 | + "<a target=\"_top\" href=\"{$_SESSION[CANVAS_INSTANCE_URL]}/courses/$course/users\">$count users enrolled</a>", |
|
| 177 | + NotificationMessage::GOOD |
|
| 178 | + ); |
|
| 179 | 179 | |
| 180 | - $_REQUEST = array(); |
|
| 180 | + $_REQUEST = array(); |
|
| 181 | + } |
|
| 181 | 182 | } |
| 183 | + } catch (Pest_Exception $e) { |
|
| 184 | + $toolbox->exceptionErrorMessage($e); |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + /* fall through to STEP_INSTRUCTION */ |
|
| 188 | + |
|
| 189 | + case STEP_INSTRUCTIONS: |
|
| 190 | + default: |
|
| 191 | + if (!empty($_REQUEST['users'])) { |
|
| 192 | + $toolbox->smarty_assign('users', $_REQUEST['users']); |
|
| 182 | 193 | } |
| 183 | - } catch (Pest_Exception $e) { |
|
| 184 | - $toolbox->exceptionErrorMessage($e); |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - /* fall through to STEP_INSTRUCTION */ |
|
| 188 | - |
|
| 189 | - case STEP_INSTRUCTIONS: |
|
| 190 | - default: |
|
| 191 | - if (!empty($_REQUEST['users'])) { |
|
| 192 | - $toolbox->smarty_assign('users', $_REQUEST['users']); |
|
| 193 | - } |
|
| 194 | - if (!empty($_REQUEST['course'])) { |
|
| 195 | - $toolbox->smarty_assign('course', $_REQUEST['course']); |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - $toolbox->smarty_assign('roles', $roles); |
|
| 199 | - $toolbox->smarty_assign('formHidden', array('step' => STEP_CONFIRM)); |
|
| 200 | - $toolbox->smarty_display(basename(__FILE__, '.php') . '/instructions.tpl'); |
|
| 194 | + if (!empty($_REQUEST['course'])) { |
|
| 195 | + $toolbox->smarty_assign('course', $_REQUEST['course']); |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + $toolbox->smarty_assign('roles', $roles); |
|
| 199 | + $toolbox->smarty_assign('formHidden', array('step' => STEP_CONFIRM)); |
|
| 200 | + $toolbox->smarty_display(basename(__FILE__, '.php') . '/instructions.tpl'); |
|
| 201 | 201 | } |