r111216 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r111215‎ | r111216 | r111217 >
Date:23:23, 10 February 2012
Author:jeroendedauw
Status:deferred
Tags:
Comment:
fix auth check in enlist module, added support for student role in enlist module and fixed issue with student enrollment
Modified paths:
  • /trunk/extensions/EducationProgram/EducationProgram.i18n.php (modified) (history)
  • /trunk/extensions/EducationProgram/api/ApiEnlist.php (modified) (history)
  • /trunk/extensions/EducationProgram/includes/EPCourse.php (modified) (history)
  • /trunk/extensions/EducationProgram/includes/EPDBObject.php (modified) (history)
  • /trunk/extensions/EducationProgram/includes/EPRoleObject.php (modified) (history)

Diff [purge]

Index: trunk/extensions/EducationProgram/includes/EPDBObject.php
@@ -1253,7 +1253,7 @@
12541254 }
12551255
12561256 /**
1257 - * Sets the value for the @see $updateSummaries field.
 1257+ * Sets the value for the @see $inSummaryMode field.
12581258 *
12591259 * @since 0.1
12601260 *
Index: trunk/extensions/EducationProgram/includes/EPRoleObject.php
@@ -121,41 +121,27 @@
122122 *
123123 * @param array $courses
124124 *
125 - * @return bool
 125+ * @return bool Success indicator
126126 */
127127 public function associateWithCourses( array /* of EPCourse */ $courses ) {
128128 $success = true;
129129
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+ }
133136
134 - $dbw = wfGetDB( DB_MASTER );
135 - $dbw->begin();
 137+ $fieldMap = array(
 138+ 'student' => 'student_count',
 139+ 'online' => 'oa_count',
 140+ 'campus' => 'ca_count',
 141+ );
136142
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()];
146144
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 ) {
160146 EPOrg::updateSummaryFields( $field, array( 'id' => array_unique( $courseIds ) ) );
161147 EPCourse::updateSummaryFields( $field, array( 'id' => $courseIds ) );
162148 }
Index: trunk/extensions/EducationProgram/includes/EPCourse.php
@@ -224,58 +224,58 @@
225225 * @see EPRevisionedObject::onUpdated()
226226 */
227227 protected function onUpdated( EPRevisionedObject $originalCourse ) {
228 - if ( $this->updateSummaries ) {
229 - $newUsers = array();
230 - $changedSummaries = array();
 228+ $newUsers = array();
 229+ $changedSummaries = array();
231230
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+ );
238237
239 - $countMap = array_flip( self::$countMap );
 238+ $countMap = array_flip( self::$countMap );
240239
241 - $dbw = wfGetDB( DB_MASTER );
 240+ $dbw = wfGetDB( DB_MASTER );
242241
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 ) );
247246
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+ }
255254
256 - if ( !empty( $removedIds ) || !empty( $addedIds ) ) {
257 - $changedSummaries[] = $countMap[$usersField];
258 - }
 255+ if ( !empty( $removedIds ) || !empty( $addedIds ) ) {
 256+ $changedSummaries[] = $countMap[$usersField];
 257+ }
259258
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+ ) );
267265 }
268266 }
 267+ }
269268
270 - if ( count( $newUsers ) > 0 ) {
271 - $dbw->begin();
 269+ if ( count( $newUsers ) > 0 ) {
 270+ $dbw->begin();
272271
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 );
278274 }
279275
 276+ $dbw->commit();
 277+ }
 278+
 279+ if ( $this->updateSummaries ) {
280280 if ( $this->hasField( 'org_id' ) && $originalCourse->getField( 'org_id' ) !== $this->getField( 'org_id' ) ) {
281281 $conds = array( 'id' => array( $originalCourse->getField( 'org_id' ), $this->getField( 'org_id' ) ) );
282282 EPOrg::updateSummaryFields( null, $conds );
@@ -630,7 +630,14 @@
631631 * @return boolean Success indicator
632632 */
633633 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];
635642 $users = $this->getField( $field );
636643 $addedUsers = array();
637644
@@ -687,7 +694,14 @@
688695 $remaimingUsers = array();
689696 $sadUsers = (array)$sadUsers;
690697
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];
692706
693707 foreach ( $this->getField( $field ) as $userId ) {
694708 if ( in_array( $userId, $sadUsers ) ) {
@@ -737,6 +751,7 @@
738752 'instructor' => 'EPInstructor',
739753 'campus' => 'EPCA',
740754 'online' => 'EPOA',
 755+ 'student' => 'EPStudent',
741756 );
742757
743758 $class = $classes[$role];
Index: trunk/extensions/EducationProgram/EducationProgram.i18n.php
@@ -105,7 +105,7 @@
106106 'logentry-campus-selfadd' => '$1 added {{GENDER:$2|himself|herself}} as {{GENDER:$2|Campus Ambassador}} to course $3',
107107 'logentry-campus-selfremove' => '$1 removed {{GENDER:$2|himself|herself}} as {{GENDER:$2|Campus Ambassador}} from course $3',
108108
109 - 'logentry-student-enroll' => '$1 enrolled in course $3',
 109+ 'logentry-student-add' => '$1 enrolled in course $3',
110110 'logentry-student-remove' => '$1 removed $4 as student from course $3',
111111 'logentry-student-selfremove' => '$1 removed {{GENDER:$2|his|her}} enrollment from course $3',
112112
Index: trunk/extensions/EducationProgram/api/ApiEnlist.php
@@ -33,11 +33,19 @@
3434 $this->dieUsage( wfMsg( 'ep-enlist-invalid-user' ), 'invalid-user' );
3535 }
3636
37 - if ( !$this->userIsAllowed( $userId ) ) {
 37+ if ( !$this->userIsAllowed( $userId, $params['role'] ) ) {
3838 $this->dieUsageMsg( array( 'badaccess-groups' ) );
3939 }
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+
4250 $course = EPCourse::selectRow( array( 'id', 'name', $field ), array( 'id' => $params['courseid'] ) );
4351
4452 if ( $course === false ) {
@@ -63,35 +71,37 @@
6472 }
6573
6674 /**
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 - /**
7975 * Returns if the user is allowed to do the requested action.
8076 *
8177 * @since 0.1
8278 *
8379 * @param integer $userId User id of the mentor affected
 80+ * @param string $role
 81+ *
 82+ * @return boolean
8483 */
85 - protected function userIsAllowed( $userId ) {
 84+ protected function userIsAllowed( $userId, $role ) {
8685 $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;
90104 }
91 -
92 - if ( $user->isAllowed( 'ep-beinstructor' ) && $user->getId() === $userId ) {
93 - return true;
94 - }
95 -
 105+
96106 return false;
97107 }
98108
@@ -110,7 +120,7 @@
111121 ApiBase::PARAM_REQUIRED => true,
112122 ),
113123 'role' => array(
114 - ApiBase::PARAM_TYPE => array( 'instructor', 'online', 'campus' ),
 124+ ApiBase::PARAM_TYPE => array( 'instructor', 'online', 'campus', 'student' ),
115125 ApiBase::PARAM_REQUIRED => true,
116126 ),
117127 'username' => array(

Status & tagging log