r109070 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r109069‎ | r109070 | r109071 >
Date:22:27, 16 January 2012
Author:jeroendedauw
Status:deferred
Tags:
Comment:
work on instructor management
Modified paths:
  • /trunk/extensions/EducationProgram/EducationProgram.i18n.php (modified) (history)
  • /trunk/extensions/EducationProgram/api/ApiInstructor.php (modified) (history)
  • /trunk/extensions/EducationProgram/includes/EPCourse.php (modified) (history)
  • /trunk/extensions/EducationProgram/resources/ep.api.js (modified) (history)
  • /trunk/extensions/EducationProgram/resources/ep.instructor.js (modified) (history)
  • /trunk/extensions/EducationProgram/specials/SpecialCourse.php (modified) (history)

Diff [purge]

Index: trunk/extensions/EducationProgram/specials/SpecialCourse.php
@@ -121,28 +121,81 @@
122122 );
123123 }
124124
 125+ $stats['instructors'] = $this->getInstructorsList( $course ) . $this->getInstructorControls( $course );
 126+
 127+ return $stats;
 128+ }
 129+
 130+ /**
 131+ * Returns a list with the instructors for the provided course
 132+ * or a message indicating there are none.
 133+ *
 134+ * @since 0.1
 135+ *
 136+ * @param EPCourse $course
 137+ *
 138+ * @return string
 139+ */
 140+ protected function getInstructorsList( EPCourse $course ) {
125141 $instructors = $course->getInstructors();
126142
127143 if ( count( $instructors ) > 0 ) {
128 - $stats['instructors'] = array();
 144+ $instList = array();
129145
130146 foreach ( $instructors as /* EPInstructor */ $instructor ) {
131 - $stats['instructors'][] = $instructor->getUserLink() . $instructor->getToolLinks( $this->getContext(), $course );
 147+ $instList[] = $instructor->getUserLink() . $instructor->getToolLinks( $this->getContext(), $course );
132148 }
133149
134150 if ( count( $instructors ) == 1 ) {
135 - $stats['instructors'] = $stats['instructors'][0];
 151+ return $instList[0];
136152 }
137153 else {
138 - $stats['instructors'] = '<ul><li>' . implode( '</li><li>', $stats['instructors'] ) . '</li></ul>';
 154+ return '<ul><li>' . implode( '</li><li>', $instList ) . '</li></ul>';
139155 }
140156 }
141157 else {
142 - $stats['instructors'] = wfMsgHtml( 'ep-course-no-instructors' );
 158+ return wfMsgHtml( 'ep-course-no-instructors' );
143159 }
 160+ }
 161+
 162+ protected function getInstructorControls( EPCourse $course ) {
 163+ $user = $this->getUser();
 164+ $links = array();
144165
145 -
146 - return $stats;
 166+ if ( ( $user->isAllowed( 'ep-instructor' ) || $user->isAllowed( 'ep-beinstructor' ) )
 167+ && !in_array( $user->getId(), $course->getField( 'instructors' ) )
 168+ ) {
 169+ $links[] = Html::element(
 170+ 'a',
 171+ array(
 172+ 'href' => '#',
 173+ 'class' => 'ep-become-instructor',
 174+ 'data-courseid' => $course->getId(),
 175+ 'data-coursename' => $course->getField( 'name' ),
 176+ ),
 177+ wfMsg( 'ep-course-become-instructor' )
 178+ );
 179+ }
 180+
 181+ if ( $user->isAllowed( 'ep-instructor' ) ) {
 182+ $links[] = Html::element(
 183+ 'a',
 184+ array(
 185+ 'href' => '#',
 186+ 'class' => 'ep-add-instructor',
 187+ 'data-courseid' => $course->getId(),
 188+ 'data-coursename' => $course->getField( 'name' ),
 189+ ),
 190+ wfMsg( 'ep-course-add-instructor' )
 191+ );
 192+ }
 193+
 194+ if ( count( $links ) > 0 ) {
 195+ return '<br />' . $this->getLanguage()->pipeList( $links );
 196+ }
 197+ else {
 198+ return '';
 199+ }
147200 }
148201
149202 }
Index: trunk/extensions/EducationProgram/includes/EPCourse.php
@@ -412,9 +412,9 @@
413413 }
414414 }
415415
416 - $this->setField( 'instructors', $instructors );
417 -
418416 if ( count( $addedInstructors ) > 0 ) {
 417+ $this->setField( 'instructors', $instructors );
 418+
419419 $success = true;
420420
421421 if ( $save ) {
@@ -424,27 +424,7 @@
425425 }
426426
427427 if ( $success && $log ) {
428 - $names = array();
429 -
430 - foreach ( $addedInstructors as $userId ) {
431 - $names[] = EPInstructor::newFromId( $userId )->getName();
432 - }
433 -
434 - $info = array(
435 - 'type' => 'instructor',
436 - 'subtype' => 'add',
437 - 'title' => $this->getTitle(),
438 - 'parameters' => array(
439 - '4::instructorcount' => count( $names ),
440 - '5::instructors' => $GLOBALS['wgLang']->listToText( $names )
441 - ),
442 - );
443 -
444 - if ( $message !== '' ) {
445 - $info['comment'] = $message;
446 - }
447 -
448 - EPUtils::log( $info );
 428+ $this->logInstructorChange( 'add', $addedInstructors, $message );
449429 }
450430
451431 return $success;
@@ -469,7 +449,72 @@
470450 * @return boolean Success indicator
471451 */
472452 public function removeInstructors( $sadInstructors, $message = '', $save = true, $log = true ) {
473 - // TODO
 453+ $removedInstructors = array();
 454+ $remaimingInstructors = array();
 455+ $sadInstructors = (array)$sadInstructors;
 456+
 457+ foreach ( $this->getField( 'instructors' ) as $userId ) {
 458+ if ( in_array( $userId, $sadInstructors ) ) {
 459+ $removedInstructors[] = $userId;
 460+ }
 461+ else {
 462+ $remaimingInstructors[] = $userId;
 463+ }
 464+ }
 465+
 466+ if ( count( $removedInstructors ) > 0 ) {
 467+ $this->setField( 'instructors', $remaimingInstructors );
 468+
 469+ $success = true;
 470+
 471+ if ( $save ) {
 472+ $this->disableLogging();
 473+ $success = $this->writeToDB();
 474+ $this->enableLogging();
 475+ }
 476+
 477+ if ( $success && $log ) {
 478+ $this->logInstructorChange( 'remove', $removedInstructors, $message );
 479+ }
 480+
 481+ return $success;
 482+ }
 483+ else {
 484+ return true;
 485+ }
474486 }
475487
 488+ /**
 489+ * Log a change of the instructors of the course.
 490+ *
 491+ * @since 0.1
 492+ *
 493+ * @param string $action
 494+ * @param array $instructors
 495+ * @param string $message
 496+ */
 497+ protected function logInstructorChange( $action, array $instructors, $message ) {
 498+ $names = array();
 499+
 500+ foreach ( $instructors as $userId ) {
 501+ $names[] = EPInstructor::newFromId( $userId )->getName();
 502+ }
 503+
 504+ $info = array(
 505+ 'type' => 'instructor',
 506+ 'subtype' => $action,
 507+ 'title' => $this->getTitle(),
 508+ 'parameters' => array(
 509+ '4::instructorcount' => count( $names ),
 510+ '5::instructors' => $GLOBALS['wgLang']->listToText( $names )
 511+ ),
 512+ );
 513+
 514+ if ( $message !== '' ) {
 515+ $info['comment'] = $message;
 516+ }
 517+
 518+ EPUtils::log( $info );
 519+ }
 520+
476521 }
Index: trunk/extensions/EducationProgram/EducationProgram.i18n.php
@@ -308,6 +308,8 @@
309309 'specialcourse-summary-terms' => 'Term count',
310310 'specialcourse-summary-instructors' => 'Instructors',
311311 'ep-course-no-instructors' => 'There are no instructors for this course.',
 312+ 'ep-course-become-instructor' => 'Become instructor',
 313+ 'ep-course-add-instructor' => 'Add an instructor',
312314
313315 // Special:Term
314316 'ep-term-title' => 'Term: $1',
Index: trunk/extensions/EducationProgram/api/ApiInstructor.php
@@ -47,10 +47,10 @@
4848
4949 switch ( $params['subaction'] ) {
5050 case 'add':
51 - $success = $course->addInstructors( array( $userId ) );
 51+ $success = $course->addInstructors( array( $userId ), $params['reason'] );
5252 break;
5353 case 'remove':
54 - $success = $course->removeInstructors( array( $userId ) );
 54+ $success = $course->removeInstructors( array( $userId ), $params['reason'] );
5555 break;
5656 }
5757
@@ -123,6 +123,7 @@
124124 'reason' => array(
125125 ApiBase::PARAM_TYPE => 'string',
126126 ApiBase::PARAM_REQUIRED => false,
 127+ ApiBase::PARAM_DFLT => '',
127128 ),
128129 'token' => null,
129130 );
Index: trunk/extensions/EducationProgram/resources/ep.api.js
@@ -32,10 +32,9 @@
3333 );
3434 };
3535
36 - this.addInstructor = function( args ) {
 36+ this.instructor = function( args ) {
3737 var requestArgs = $.extend( {
3838 'action': 'instructor',
39 - 'subaction': 'add',
4039 'format': 'json',
4140 'token': window.mw.user.tokens.get( 'editToken' )
4241 }, args );
@@ -57,6 +56,16 @@
5857
5958 return deferred.promise();
6059 };
 60+
 61+ this.addInstructor = function( args ) {
 62+ args.subaction = 'add';
 63+ return this.instructor( args );
 64+ };
 65+
 66+ this.removeInstructor = function( args ) {
 67+ args.subaction = 'remove';
 68+ return this.instructor( args );
 69+ };
6170
6271 } );
6372
Index: trunk/extensions/EducationProgram/resources/ep.instructor.js
@@ -26,7 +26,7 @@
2727 $remove.button( 'option', 'disabled', true );
2828 $remove.button( 'option', 'label', mw.msg( 'ep-instructor-removing' ) );
2929
30 - ep.api.addInstructor( {
 30+ ep.api.removeInstructor( {
3131 'courseid': courseId,
3232 'userid': userId,
3333 'reason': summaryInput.val()
@@ -67,7 +67,12 @@
6868 ]
6969 } );
7070
71 - $dialog.append( $( '<p>' ).text( gM( 'ep-instructor-remove-text', userName, bestName, courseName ) ) );
 71+ $dialog.append( $( '<p>' ).msg(
 72+ 'ep-instructor-remove-text',
 73+ mw.html.escape( userName ),
 74+ '<b>' + mw.html.escape( bestName ) + '</b>',
 75+ '<b>' + mw.html.escape( courseName ) + '</b>'
 76+ ) );
7277
7378 $dialog.append( summaryInput );
7479

Status & tagging log