r109037 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r109036‎ | r109037 | r109038 >
Date:14:40, 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/EducationProgram.php (modified) (history)
  • /trunk/extensions/EducationProgram/api/ApiAddInstructor.php (modified) (history)
  • /trunk/extensions/EducationProgram/includes/EPCourse.php (modified) (history)
  • /trunk/extensions/EducationProgram/includes/EPDBObject.php (modified) (history)
  • /trunk/extensions/EducationProgram/includes/EPInstructor.php (modified) (history)
  • /trunk/extensions/EducationProgram/includes/EPUtils.php (added) (history)

Diff [purge]

Index: trunk/extensions/EducationProgram/includes/EPDBObject.php
@@ -459,34 +459,10 @@
460460 protected function log( $subType ) {
461461 if ( $this->log ) {
462462 $info = $this->getLogInfo( $subType );
463 - $user = array_key_exists( 'user', $info ) ? $info['user'] : $GLOBALS['wgUser'];
464463
465464 if ( $info !== false ) {
466 - if ( class_exists( 'ManualLogEntry' ) ) {
467 - $logEntry = new ManualLogEntry( $info['type'], $subType );
468 -
469 - $logEntry->setPerformer( $user );
470 - $logEntry->setTarget( $info['title'] );
471 -
472 - if ( array_key_exists( 'comment', $info ) ) {
473 - $logEntry->setComment( $info['comment'] );
474 - }
475 -
476 - $logid = $logEntry->insert();
477 - $logEntry->publish( $logid );
478 - }
479 - else {
480 - // Compatibility with MediaWiki 1.18.
481 - $log = new LogPage( $info['type'] );
482 -
483 - $log->addEntry(
484 - $subType,
485 - $info['title'],
486 - array_key_exists( 'comment', $info ) ? $info['comment'] : '',
487 - array(),
488 - $user
489 - );
490 - }
 465+ $info['subtype'] = $subType;
 466+ EPUtils::log( $info );
491467 }
492468 }
493469 }
Index: trunk/extensions/EducationProgram/includes/EPInstructor.php
@@ -90,6 +90,17 @@
9191 }
9292
9393 /**
 94+ * Returns the name of the instroctor, using their real name when available.
 95+ *
 96+ * @since 0.1
 97+ *
 98+ * @return string
 99+ */
 100+ public function getName() {
 101+ return $this->getUser()->getRealName() === '' ? $this->getUser()->getName() : $this->getUser()->getRealName();
 102+ }
 103+
 104+ /**
94105 * Retruns the user link for this instructor, using their real name when available.
95106 *
96107 * @since 0.1
@@ -99,7 +110,7 @@
100111 public function getUserLink() {
101112 return Linker::userLink(
102113 $this->getUser()->getId(),
103 - $this->getUser()->getRealName() === '' ? $this->getUser()->getName() : $this->getUser()->getRealName()
 114+ $this->getName()
104115 );
105116 }
106117
Index: trunk/extensions/EducationProgram/includes/EPCourse.php
@@ -333,12 +333,23 @@
334334 */
335335 public function getLink() {
336336 return Linker::linkKnown(
337 - SpecialPage::getTitleFor( 'Course', $this->getField( 'name' ) ),
 337+ $this->getTitle(),
338338 htmlspecialchars( $this->getField( 'name' ) )
339339 );
340340 }
341341
342342 /**
 343+ * Get the title of Special:Course/name.
 344+ *
 345+ * @since 0.1
 346+ *
 347+ * @return Title
 348+ */
 349+ public function getTitle() {
 350+ return SpecialPage::getTitleFor( 'Course', $this->getField( 'name' ) );
 351+ }
 352+
 353+ /**
343354 * Returns the instructors as a list of EPInstructor objects.
344355 *
345356 * @since 0.1
@@ -358,5 +369,91 @@
359370
360371 return $this->instructors;
361372 }
 373+
 374+ /**
 375+ * (non-PHPdoc)
 376+ * @see EPDBObject::setField()
 377+ */
 378+ public function setField( $name, $value ) {
 379+ if ( $name === 'instructors' ) {
 380+ $this->instructors = false;
 381+ }
 382+ elseif ( $name === 'org_id' ) {
 383+ $this->org = false;
 384+ }
 385+
 386+ parent::setField( $name, $value );
 387+ }
 388+
 389+ /**
 390+ * Adds a number of instructors to this course,
 391+ * by default also saving the course and only
 392+ * logging the adittion of the instructors.
 393+ *
 394+ * @since 0.1
 395+ *
 396+ * @param array|integer $newInstructors
 397+ * @param boolean $save
 398+ * @param boolean $log
 399+ * @param string $message
 400+ *
 401+ * @return boolean Success indicator
 402+ */
 403+ public function addInstructors( $newInstructors, $save = true, $log = true, $message = '' ) {
 404+ $instructors = $this->getField( 'instructors' );
 405+ $addedInstructors = array();
 406+
 407+ foreach ( (array)$newInstructors as $userId ) {
 408+ if ( !is_integer( $userId ) ) {
 409+ throw new MWException( 'Provided user id is not an integer' );
 410+ }
 411+
 412+ if ( !in_array( $userId, $instructors ) ) {
 413+ $instructors[] = $userId;
 414+ $addedInstructors[] = $userId;
 415+ }
 416+ }
 417+
 418+ $this->setField( 'instructors', $instructors );
 419+ $addedInstructors = array( 1 );
 420+ if ( count( $addedInstructors ) > 0 ) {
 421+ $success = true;
 422+
 423+ if ( $save ) {
 424+ $this->disableLogging();
 425+ $success = $this->writeToDB();
 426+ $this->enableLogging();
 427+ }
 428+
 429+ if ( $success && $log ) {
 430+ $names = array();
 431+
 432+ foreach ( $addedInstructors as $userId ) {
 433+ $names[] = EPInstructor::newFromId( $userId )->getName();
 434+ }
 435+
 436+ $info = array(
 437+ 'type' => 'instructor',
 438+ 'subtype' => 'add',
 439+ 'title' => $this->getTitle(),
 440+ 'parameters' => array(
 441+ '4::instructorcount' => count( $names ),
 442+ '5::instructors' => $GLOBALS['wgLang']->listToText( $names )
 443+ ),
 444+ );
 445+
 446+ if ( $message !== '' ) {
 447+ $info['comment'] = $message;
 448+ }
 449+
 450+ EPUtils::log( $info );
 451+ }
 452+
 453+ return $success;
 454+ }
 455+ else {
 456+ return true;
 457+ }
 458+ }
362459
363460 }
Index: trunk/extensions/EducationProgram/includes/EPUtils.php
@@ -0,0 +1,52 @@
 2+<?php
 3+
 4+/**
 5+ * Static class with utility functions for the Education Program extension.
 6+ *
 7+ * @since 0.1
 8+ *
 9+ * @file EPUtils.php
 10+ * @ingroup EducationProgram
 11+ *
 12+ * @licence GNU GPL v3 or later
 13+ * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 14+ */
 15+class EPUtils {
 16+
 17+ public static function log( array $info ) {
 18+ $user = array_key_exists( 'user', $info ) ? $info['user'] : $GLOBALS['wgUser'];
 19+
 20+ if ( $info !== false ) {
 21+ if ( class_exists( 'ManualLogEntry' ) ) {
 22+ $logEntry = new ManualLogEntry( $info['type'], $info['subtype'] );
 23+
 24+ $logEntry->setPerformer( $user );
 25+ $logEntry->setTarget( $info['title'] );
 26+
 27+ if ( array_key_exists( 'comment', $info ) ) {
 28+ $logEntry->setComment( $info['comment'] );
 29+ }
 30+
 31+ if ( array_key_exists( 'parameters', $info ) ) {
 32+ $logEntry->setParameters( $info['parameters'] );
 33+ }
 34+
 35+ $logid = $logEntry->insert();
 36+ $logEntry->publish( $logid );
 37+ }
 38+ else {
 39+ // Compatibility with MediaWiki 1.18.
 40+ $log = new LogPage( $info['type'] );
 41+
 42+ $log->addEntry(
 43+ $info['subtype'],
 44+ $info['title'],
 45+ array_key_exists( 'comment', $info ) ? $info['comment'] : '',
 46+ array_key_exists( 'parameters', $info ) ? $info['parameters'] : array(),
 47+ $user
 48+ );
 49+ }
 50+ }
 51+ }
 52+
 53+}
Property changes on: trunk/extensions/EducationProgram/includes/EPUtils.php
___________________________________________________________________
Added: svn:eol-style
154 + native
Index: trunk/extensions/EducationProgram/EducationProgram.i18n.php
@@ -56,6 +56,11 @@
5757 'logentry-term-remove' => '$1 {{GENDER:$2|removed term}} $3',
5858 'logentry-term-update' => '$1 {{GENDER:$2|updated term}} $3',
5959
 60+ 'logentry-instructor-add' => '$1 {{GENDER:$2|added}} {{PLURAL:$4|instructor|instructors}} $5 to course $3',
 61+ 'logentry-instructor-remove' => '$1 {{GENDER:$2|removed}} {{PLURAL:$4|instructor|instructors}} $5 from course $3',
 62+ 'logentry-instructor-selfadd' => '$1 {{GENDER:$2|added himself|added herself}} to course $3',
 63+ 'logentry-instructor-selfremove' => '$1 {{GENDER:$2|removed himself|removed herself}} from course $3',
 64+
6065 // Preferences
6166 'prefs-education' => 'Education',
6267 'ep-prefs-showtoplink' => 'Show a link to [[Special:MyCourses|your courses]] at the top of every page.',
@@ -372,6 +377,11 @@
373378
374379 // EPInstrucor
375380 'ep-instructor-remove' => 'remove from course',
 381+
 382+ // API addinstructor
 383+ 'ep-addinstructor-invalid-user-args' => 'You need to either provide the username or the userid parameter',
 384+ 'ep-addinstructor-invalid-user' => 'The provided user id or name is not valid and can therefore not be associated as instrucor with the specified course',
 385+ 'ep-addinstructor-invalid-course' => 'There is no course with the provided ID',
376386 );
377387
378388 /** Message documentation (Message documentation)
Index: trunk/extensions/EducationProgram/EducationProgram.php
@@ -76,6 +76,7 @@
7777 $wgAutoloadClasses['EPStudentPager'] = dirname( __FILE__ ) . '/includes/EPStudentPager.php';
7878 $wgAutoloadClasses['EPTerm'] = dirname( __FILE__ ) . '/includes/EPTerm.php';
7979 $wgAutoloadClasses['EPTermPager'] = dirname( __FILE__ ) . '/includes/EPTermPager.php';
 80+$wgAutoloadClasses['EPUtils'] = dirname( __FILE__ ) . '/includes/EPUtils.php';
8081
8182 $wgAutoloadClasses['SpecialCourse'] = dirname( __FILE__ ) . '/specials/SpecialCourse.php';
8283 $wgAutoloadClasses['SpecialCourses'] = dirname( __FILE__ ) . '/specials/SpecialCourses.php';
@@ -169,12 +170,21 @@
170171 }
171172 else {
172173 // Compatibility with MediaWiki 1.18.
173 - foreach ( array( 'institution', 'course', 'term', 'student', 'ambassador', 'instructor' ) as $type ) {
 174+ foreach ( array( 'institution', 'course', 'term' ) as $type ) {
174175 foreach ( array( 'add', 'remove', 'update' ) as $action ) {
175176 $wgLogActionsHandlers[$type . '/' . $action] = 'EPHooks::formatLogEntry';
176177 }
177178 }
178179
 180+ foreach ( array( 'instructor', 'ambassador' ) as $type ) {
 181+ foreach ( array( 'add', 'remove', 'selfadd', 'selfremove' ) as $action ) {
 182+ $wgLogActionsHandlers[$type . '/' . $action] = 'EPHooks::formatLogEntry';
 183+ }
 184+ }
 185+
 186+ $wgLogActionsHandlers['student/enroll'] = 'EPHooks::formatLogEntry';
 187+ $wgLogActionsHandlers['student/remove'] = 'EPHooks::formatLogEntry';
 188+
179189 // Compatibility with MediaWiki 1.18.
180190 $wgLogNames['institution'] = 'log-name-institution';
181191 $wgLogNames['course'] = 'log-name-course';
Index: trunk/extensions/EducationProgram/api/ApiAddInstructor.php
@@ -18,17 +18,31 @@
1919 $params = $this->extractRequestParams();
2020
2121 if ( !( isset( $params['username'] ) XOR isset( $params['userid'] ) ) ) {
22 - $this->dieUsage( wfMsgExt( 'ep-addinstructor-invalid-user-arg' ), 'username-xor-userid' );
 22+ $this->dieUsage( wfMsgExt( 'ep-addinstructor-invalid-user-args' ), 'username-xor-userid' );
2323 }
2424
25 - $everythingOk = true;
 25+ if ( isset( $params['username'] ) ) {
 26+ $user = User::newFromName( $params['username'] );
 27+ $userId = $user->getId();
 28+ }
 29+ else {
 30+ $userId = $params['userid'];
 31+ }
 32+
 33+ if ( $userId < 1 ) {
 34+ $this->dieUsage( wfMsgExt( 'ep-addinstructor-invalid-user' ), 'invalid-user' );
 35+ }
 36+
 37+ $course = EPCourse::selectRow( array( 'id', 'name', 'instructors' ), array( 'id' => $params['courseid'] ) );
2638
27 - // TODO
28 -
 39+ if ( $course === false ) {
 40+ $this->dieUsage( wfMsgExt( 'ep-addinstructor-invalid-course' ), 'invalid-course' );
 41+ }
 42+
2943 $this->getResult()->addValue(
3044 null,
3145 'success',
32 - $everythingOk
 46+ $course->addInstructors( array( $userId ) )
3347 );
3448 }
3549
@@ -44,14 +58,14 @@
4559 return method_exists( 'ApiBase', 'getUser' ) ? parent::getUser() : $GLOBALS['wgUser'];
4660 }
4761
48 - public function needsToken() {
49 - return true;
50 - }
 62+// public function needsToken() {
 63+// return true;
 64+// }
 65+//
 66+// public function mustBePosted() {
 67+// return true;
 68+// }
5169
52 - public function mustBePosted() {
53 - return true;
54 - }
55 -
5670 public function getAllowedParams() {
5771 return array(
5872 'username' => array(
@@ -88,6 +102,8 @@
89103 public function getPossibleErrors() {
90104 return array_merge( parent::getPossibleErrors(), array(
91105 array( 'code' => 'username-xor-userid', 'info' => 'You need to either provide the username or the userid parameter' ),
 106+ array( 'code' => 'invalid-user', 'info' => 'An invalid user name or id was provided' ),
 107+ array( 'code' => 'invalid-course', 'info' => 'There is no course with the provided ID' ),
92108 ) );
93109 }
94110

Status & tagging log