Index: trunk/extensions/EducationProgram/includes/EPDBObject.php |
— | — | @@ -1253,7 +1253,7 @@ |
1254 | 1254 | } |
1255 | 1255 | |
1256 | 1256 | /** |
1257 | | - * Sets the value for the @see $updateSummaries field. |
| 1257 | + * Sets the value for the @see $inSummaryMode field. |
1258 | 1258 | * |
1259 | 1259 | * @since 0.1 |
1260 | 1260 | * |
Index: trunk/extensions/EducationProgram/includes/EPRoleObject.php |
— | — | @@ -121,41 +121,27 @@ |
122 | 122 | * |
123 | 123 | * @param array $courses |
124 | 124 | * |
125 | | - * @return bool |
| 125 | + * @return bool Success indicator |
126 | 126 | */ |
127 | 127 | public function associateWithCourses( array /* of EPCourse */ $courses ) { |
128 | 128 | $success = true; |
129 | 129 | |
130 | | - if ( count( $courses ) > 0 ) { |
131 | | - $courseIds = array(); |
132 | | - $orgIds = array(); |
| 130 | + foreach ( $courses as /* EPCourse */ $course ) { |
| 131 | + $courseIds[] = $course->getId(); |
| 132 | + $course->setUpdateSummaries( false ); |
| 133 | + $course->enlistUsers( $this->getField( 'user_id' ), $this->getRoleName(), '' ); |
| 134 | + $course->setUpdateSummaries( true ); |
| 135 | + } |
133 | 136 | |
134 | | - $dbw = wfGetDB( DB_MASTER ); |
135 | | - $dbw->begin(); |
| 137 | + $fieldMap = array( |
| 138 | + 'student' => 'student_count', |
| 139 | + 'online' => 'oa_count', |
| 140 | + 'campus' => 'ca_count', |
| 141 | + ); |
136 | 142 | |
137 | | - foreach ( $courses as /* EPCourse */ $course ) { |
138 | | - $success = $dbw->insert( |
139 | | - 'ep_users_per_course', |
140 | | - array( |
141 | | - 'upc_user_id' => $this->getId(), |
142 | | - 'upc_course_id' => $course->getId(), |
143 | | - 'upc_role' => EPUtils::getRoleId( $this->getRoleName() ), |
144 | | - ) |
145 | | - ) && $success; |
| 143 | + $field = $fieldMap[$this->getRoleName()]; |
146 | 144 | |
147 | | - $courseIds[] = $course->getId(); |
148 | | - } |
149 | | - |
150 | | - $dbw->commit(); |
151 | | - |
152 | | - $fieldMap = array( |
153 | | - 'student' => 'student_count', |
154 | | - 'online' => 'oa_count', |
155 | | - 'campus' => 'ca_count', |
156 | | - ); |
157 | | - |
158 | | - $field = $fieldMap[$this->getRoleName()]; |
159 | | - |
| 145 | + if ( count( $courseIds ) > 0 ) { |
160 | 146 | EPOrg::updateSummaryFields( $field, array( 'id' => array_unique( $courseIds ) ) ); |
161 | 147 | EPCourse::updateSummaryFields( $field, array( 'id' => $courseIds ) ); |
162 | 148 | } |
Index: trunk/extensions/EducationProgram/includes/EPCourse.php |
— | — | @@ -224,58 +224,58 @@ |
225 | 225 | * @see EPRevisionedObject::onUpdated() |
226 | 226 | */ |
227 | 227 | protected function onUpdated( EPRevisionedObject $originalCourse ) { |
228 | | - if ( $this->updateSummaries ) { |
229 | | - $newUsers = array(); |
230 | | - $changedSummaries = array(); |
| 228 | + $newUsers = array(); |
| 229 | + $changedSummaries = array(); |
231 | 230 | |
232 | | - $roleMap = array( |
233 | | - 'online_ambs' => EP_OA, |
234 | | - 'campus_ambs' => EP_CA, |
235 | | - 'students' => EP_STUDENT, |
236 | | - 'instructors' => EP_INSTRUCTOR, |
237 | | - ); |
| 231 | + $roleMap = array( |
| 232 | + 'online_ambs' => EP_OA, |
| 233 | + 'campus_ambs' => EP_CA, |
| 234 | + 'students' => EP_STUDENT, |
| 235 | + 'instructors' => EP_INSTRUCTOR, |
| 236 | + ); |
238 | 237 | |
239 | | - $countMap = array_flip( self::$countMap ); |
| 238 | + $countMap = array_flip( self::$countMap ); |
240 | 239 | |
241 | | - $dbw = wfGetDB( DB_MASTER ); |
| 240 | + $dbw = wfGetDB( DB_MASTER ); |
242 | 241 | |
243 | | - foreach ( array( 'online_ambs', 'campus_ambs', 'students', 'instructors' ) as $usersField ) { |
244 | | - if ( $this->hasField( $usersField ) && $originalCourse->getField( $usersField ) !== $this->getField( $usersField ) ) { |
245 | | - $removedIds = array_diff( $originalCourse->getField( $usersField ), $this->getField( $usersField ) ); |
246 | | - $addedIds = array_diff( $this->getField( $usersField ), $originalCourse->getField( $usersField ) ); |
| 242 | + foreach ( array( 'online_ambs', 'campus_ambs', 'students', 'instructors' ) as $usersField ) { |
| 243 | + if ( $this->hasField( $usersField ) && $originalCourse->getField( $usersField ) !== $this->getField( $usersField ) ) { |
| 244 | + $removedIds = array_diff( $originalCourse->getField( $usersField ), $this->getField( $usersField ) ); |
| 245 | + $addedIds = array_diff( $this->getField( $usersField ), $originalCourse->getField( $usersField ) ); |
247 | 246 | |
248 | | - foreach ( $addedIds as $addedId ) { |
249 | | - $newUsers[] = array( |
250 | | - 'upc_course_id' => $this->getId(), |
251 | | - 'upc_user_id' => $addedId, |
252 | | - 'upc_role' => $roleMap[$usersField], |
253 | | - ); |
254 | | - } |
| 247 | + foreach ( $addedIds as $addedId ) { |
| 248 | + $newUsers[] = array( |
| 249 | + 'upc_course_id' => $this->getId(), |
| 250 | + 'upc_user_id' => $addedId, |
| 251 | + 'upc_role' => $roleMap[$usersField], |
| 252 | + ); |
| 253 | + } |
255 | 254 | |
256 | | - if ( !empty( $removedIds ) || !empty( $addedIds ) ) { |
257 | | - $changedSummaries[] = $countMap[$usersField]; |
258 | | - } |
| 255 | + if ( !empty( $removedIds ) || !empty( $addedIds ) ) { |
| 256 | + $changedSummaries[] = $countMap[$usersField]; |
| 257 | + } |
259 | 258 | |
260 | | - if ( count( $removedIds ) > 0 ) { |
261 | | - $dbw->delete( 'ep_users_per_course', array( |
262 | | - 'upc_course_id' => $this->getId(), |
263 | | - 'upc_user_id' => $removedIds, |
264 | | - 'upc_role' => $roleMap[$usersField], |
265 | | - ) ); |
266 | | - } |
| 259 | + if ( count( $removedIds ) > 0 ) { |
| 260 | + $dbw->delete( 'ep_users_per_course', array( |
| 261 | + 'upc_course_id' => $this->getId(), |
| 262 | + 'upc_user_id' => $removedIds, |
| 263 | + 'upc_role' => $roleMap[$usersField], |
| 264 | + ) ); |
267 | 265 | } |
268 | 266 | } |
| 267 | + } |
269 | 268 | |
270 | | - if ( count( $newUsers ) > 0 ) { |
271 | | - $dbw->begin(); |
| 269 | + if ( count( $newUsers ) > 0 ) { |
| 270 | + $dbw->begin(); |
272 | 271 | |
273 | | - foreach ( $newUsers as $userLink ) { |
274 | | - $dbw->insert( 'ep_users_per_course', $userLink ); |
275 | | - } |
276 | | - |
277 | | - $dbw->commit(); |
| 272 | + foreach ( $newUsers as $userLink ) { |
| 273 | + $dbw->insert( 'ep_users_per_course', $userLink ); |
278 | 274 | } |
279 | 275 | |
| 276 | + $dbw->commit(); |
| 277 | + } |
| 278 | + |
| 279 | + if ( $this->updateSummaries ) { |
280 | 280 | if ( $this->hasField( 'org_id' ) && $originalCourse->getField( 'org_id' ) !== $this->getField( 'org_id' ) ) { |
281 | 281 | $conds = array( 'id' => array( $originalCourse->getField( 'org_id' ), $this->getField( 'org_id' ) ) ); |
282 | 282 | EPOrg::updateSummaryFields( null, $conds ); |
— | — | @@ -630,7 +630,14 @@ |
631 | 631 | * @return boolean Success indicator |
632 | 632 | */ |
633 | 633 | public function enlistUsers( $newUsers, $role, $message = '', $save = true, $log = true ) { |
634 | | - $field = $role === 'instructor' ? 'instructors' : $role . '_ambs'; |
| 634 | + $roleMap = array( |
| 635 | + 'student' => 'students', |
| 636 | + 'campus' => 'campus_ambs', |
| 637 | + 'online' => 'online_ambs', |
| 638 | + 'instructor' => 'instructors', |
| 639 | + ); |
| 640 | + |
| 641 | + $field = $roleMap[$role]; |
635 | 642 | $users = $this->getField( $field ); |
636 | 643 | $addedUsers = array(); |
637 | 644 | |
— | — | @@ -687,7 +694,14 @@ |
688 | 695 | $remaimingUsers = array(); |
689 | 696 | $sadUsers = (array)$sadUsers; |
690 | 697 | |
691 | | - $field = $role === 'instructor' ? 'instructors' : $role . '_ambs'; |
| 698 | + $roleMap = array( |
| 699 | + 'student' => 'students', |
| 700 | + 'campus' => 'campus_ambs', |
| 701 | + 'online' => 'online_ambs', |
| 702 | + 'instructor' => 'instructors', |
| 703 | + ); |
| 704 | + |
| 705 | + $field = $roleMap[$role]; |
692 | 706 | |
693 | 707 | foreach ( $this->getField( $field ) as $userId ) { |
694 | 708 | if ( in_array( $userId, $sadUsers ) ) { |
— | — | @@ -737,6 +751,7 @@ |
738 | 752 | 'instructor' => 'EPInstructor', |
739 | 753 | 'campus' => 'EPCA', |
740 | 754 | 'online' => 'EPOA', |
| 755 | + 'student' => 'EPStudent', |
741 | 756 | ); |
742 | 757 | |
743 | 758 | $class = $classes[$role]; |
Index: trunk/extensions/EducationProgram/EducationProgram.i18n.php |
— | — | @@ -105,7 +105,7 @@ |
106 | 106 | 'logentry-campus-selfadd' => '$1 added {{GENDER:$2|himself|herself}} as {{GENDER:$2|Campus Ambassador}} to course $3', |
107 | 107 | 'logentry-campus-selfremove' => '$1 removed {{GENDER:$2|himself|herself}} as {{GENDER:$2|Campus Ambassador}} from course $3', |
108 | 108 | |
109 | | - 'logentry-student-enroll' => '$1 enrolled in course $3', |
| 109 | + 'logentry-student-add' => '$1 enrolled in course $3', |
110 | 110 | 'logentry-student-remove' => '$1 removed $4 as student from course $3', |
111 | 111 | 'logentry-student-selfremove' => '$1 removed {{GENDER:$2|his|her}} enrollment from course $3', |
112 | 112 | |
Index: trunk/extensions/EducationProgram/api/ApiEnlist.php |
— | — | @@ -33,11 +33,19 @@ |
34 | 34 | $this->dieUsage( wfMsg( 'ep-enlist-invalid-user' ), 'invalid-user' ); |
35 | 35 | } |
36 | 36 | |
37 | | - if ( !$this->userIsAllowed( $userId ) ) { |
| 37 | + if ( !$this->userIsAllowed( $userId, $params['role'] ) ) { |
38 | 38 | $this->dieUsageMsg( array( 'badaccess-groups' ) ); |
39 | 39 | } |
40 | | - |
41 | | - $field = $params['role'] === 'instructor' ? 'instructors' : $params['role'] . '_ambs'; |
| 40 | + |
| 41 | + $roleMap = array( |
| 42 | + 'student' => 'students', |
| 43 | + 'campus' => 'campus_ambs', |
| 44 | + 'online' => 'online_ambs', |
| 45 | + 'instructor' => 'instructors', |
| 46 | + ); |
| 47 | + |
| 48 | + $field = $roleMap[$params['role']]; |
| 49 | + |
42 | 50 | $course = EPCourse::selectRow( array( 'id', 'name', $field ), array( 'id' => $params['courseid'] ) ); |
43 | 51 | |
44 | 52 | if ( $course === false ) { |
— | — | @@ -63,35 +71,37 @@ |
64 | 72 | } |
65 | 73 | |
66 | 74 | /** |
67 | | - * Get the User being used for this instance. |
68 | | - * ApiBase extends ContextSource as of 1.19. |
69 | | - * |
70 | | - * @since 0.1 |
71 | | - * |
72 | | - * @return User |
73 | | - */ |
74 | | - public function getUser() { |
75 | | - return method_exists( 'ApiBase', 'getUser' ) ? parent::getUser() : $GLOBALS['wgUser']; |
76 | | - } |
77 | | - |
78 | | - /** |
79 | 75 | * Returns if the user is allowed to do the requested action. |
80 | 76 | * |
81 | 77 | * @since 0.1 |
82 | 78 | * |
83 | 79 | * @param integer $userId User id of the mentor affected |
| 80 | + * @param string $role |
| 81 | + * |
| 82 | + * @return boolean |
84 | 83 | */ |
85 | | - protected function userIsAllowed( $userId ) { |
| 84 | + protected function userIsAllowed( $userId, $role ) { |
86 | 85 | $user = $this->getUser(); |
87 | | - |
88 | | - if ( $user->isAllowed( 'ep-instructor' ) ) { |
89 | | - return true; |
| 86 | + $isSelf = $user->getId() === $userId; |
| 87 | + |
| 88 | + switch ( $role ) { |
| 89 | + case 'student': |
| 90 | + return $user->isAllowed( 'ep-enroll' ) && $isSelf; |
| 91 | + break; |
| 92 | + case 'instructor': |
| 93 | + return $user->isAllowed( 'ep-instructor' ) |
| 94 | + || ( $user->isAllowed( 'ep-beinstructor' ) && $isSelf ); |
| 95 | + break; |
| 96 | + case 'online': |
| 97 | + return $user->isAllowed( 'ep-online' ) |
| 98 | + || ( $user->isAllowed( 'ep-beonline' ) && $isSelf ); |
| 99 | + break; |
| 100 | + case 'campus': |
| 101 | + return $user->isAllowed( 'ep-campus' ) |
| 102 | + || ( $user->isAllowed( 'ep-becampus' ) && $isSelf ); |
| 103 | + break; |
90 | 104 | } |
91 | | - |
92 | | - if ( $user->isAllowed( 'ep-beinstructor' ) && $user->getId() === $userId ) { |
93 | | - return true; |
94 | | - } |
95 | | - |
| 105 | + |
96 | 106 | return false; |
97 | 107 | } |
98 | 108 | |
— | — | @@ -110,7 +120,7 @@ |
111 | 121 | ApiBase::PARAM_REQUIRED => true, |
112 | 122 | ), |
113 | 123 | 'role' => array( |
114 | | - ApiBase::PARAM_TYPE => array( 'instructor', 'online', 'campus' ), |
| 124 | + ApiBase::PARAM_TYPE => array( 'instructor', 'online', 'campus', 'student' ), |
115 | 125 | ApiBase::PARAM_REQUIRED => true, |
116 | 126 | ), |
117 | 127 | 'username' => array( |