Index: trunk/extensions/EducationProgram/includes/EPDBObject.php |
— | — | @@ -459,34 +459,10 @@ |
460 | 460 | protected function log( $subType ) { |
461 | 461 | if ( $this->log ) { |
462 | 462 | $info = $this->getLogInfo( $subType ); |
463 | | - $user = array_key_exists( 'user', $info ) ? $info['user'] : $GLOBALS['wgUser']; |
464 | 463 | |
465 | 464 | 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 ); |
491 | 467 | } |
492 | 468 | } |
493 | 469 | } |
Index: trunk/extensions/EducationProgram/includes/EPInstructor.php |
— | — | @@ -90,6 +90,17 @@ |
91 | 91 | } |
92 | 92 | |
93 | 93 | /** |
| 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 | + /** |
94 | 105 | * Retruns the user link for this instructor, using their real name when available. |
95 | 106 | * |
96 | 107 | * @since 0.1 |
— | — | @@ -99,7 +110,7 @@ |
100 | 111 | public function getUserLink() { |
101 | 112 | return Linker::userLink( |
102 | 113 | $this->getUser()->getId(), |
103 | | - $this->getUser()->getRealName() === '' ? $this->getUser()->getName() : $this->getUser()->getRealName() |
| 114 | + $this->getName() |
104 | 115 | ); |
105 | 116 | } |
106 | 117 | |
Index: trunk/extensions/EducationProgram/includes/EPCourse.php |
— | — | @@ -333,12 +333,23 @@ |
334 | 334 | */ |
335 | 335 | public function getLink() { |
336 | 336 | return Linker::linkKnown( |
337 | | - SpecialPage::getTitleFor( 'Course', $this->getField( 'name' ) ), |
| 337 | + $this->getTitle(), |
338 | 338 | htmlspecialchars( $this->getField( 'name' ) ) |
339 | 339 | ); |
340 | 340 | } |
341 | 341 | |
342 | 342 | /** |
| 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 | + /** |
343 | 354 | * Returns the instructors as a list of EPInstructor objects. |
344 | 355 | * |
345 | 356 | * @since 0.1 |
— | — | @@ -358,5 +369,91 @@ |
359 | 370 | |
360 | 371 | return $this->instructors; |
361 | 372 | } |
| 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 | + } |
362 | 459 | |
363 | 460 | } |
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 |
1 | 54 | + native |
Index: trunk/extensions/EducationProgram/EducationProgram.i18n.php |
— | — | @@ -56,6 +56,11 @@ |
57 | 57 | 'logentry-term-remove' => '$1 {{GENDER:$2|removed term}} $3', |
58 | 58 | 'logentry-term-update' => '$1 {{GENDER:$2|updated term}} $3', |
59 | 59 | |
| 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 | + |
60 | 65 | // Preferences |
61 | 66 | 'prefs-education' => 'Education', |
62 | 67 | 'ep-prefs-showtoplink' => 'Show a link to [[Special:MyCourses|your courses]] at the top of every page.', |
— | — | @@ -372,6 +377,11 @@ |
373 | 378 | |
374 | 379 | // EPInstrucor |
375 | 380 | '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', |
376 | 386 | ); |
377 | 387 | |
378 | 388 | /** Message documentation (Message documentation) |
Index: trunk/extensions/EducationProgram/EducationProgram.php |
— | — | @@ -76,6 +76,7 @@ |
77 | 77 | $wgAutoloadClasses['EPStudentPager'] = dirname( __FILE__ ) . '/includes/EPStudentPager.php'; |
78 | 78 | $wgAutoloadClasses['EPTerm'] = dirname( __FILE__ ) . '/includes/EPTerm.php'; |
79 | 79 | $wgAutoloadClasses['EPTermPager'] = dirname( __FILE__ ) . '/includes/EPTermPager.php'; |
| 80 | +$wgAutoloadClasses['EPUtils'] = dirname( __FILE__ ) . '/includes/EPUtils.php'; |
80 | 81 | |
81 | 82 | $wgAutoloadClasses['SpecialCourse'] = dirname( __FILE__ ) . '/specials/SpecialCourse.php'; |
82 | 83 | $wgAutoloadClasses['SpecialCourses'] = dirname( __FILE__ ) . '/specials/SpecialCourses.php'; |
— | — | @@ -169,12 +170,21 @@ |
170 | 171 | } |
171 | 172 | else { |
172 | 173 | // Compatibility with MediaWiki 1.18. |
173 | | - foreach ( array( 'institution', 'course', 'term', 'student', 'ambassador', 'instructor' ) as $type ) { |
| 174 | + foreach ( array( 'institution', 'course', 'term' ) as $type ) { |
174 | 175 | foreach ( array( 'add', 'remove', 'update' ) as $action ) { |
175 | 176 | $wgLogActionsHandlers[$type . '/' . $action] = 'EPHooks::formatLogEntry'; |
176 | 177 | } |
177 | 178 | } |
178 | 179 | |
| 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 | + |
179 | 189 | // Compatibility with MediaWiki 1.18. |
180 | 190 | $wgLogNames['institution'] = 'log-name-institution'; |
181 | 191 | $wgLogNames['course'] = 'log-name-course'; |
Index: trunk/extensions/EducationProgram/api/ApiAddInstructor.php |
— | — | @@ -18,17 +18,31 @@ |
19 | 19 | $params = $this->extractRequestParams(); |
20 | 20 | |
21 | 21 | 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' ); |
23 | 23 | } |
24 | 24 | |
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'] ) ); |
26 | 38 | |
27 | | - // TODO |
28 | | - |
| 39 | + if ( $course === false ) { |
| 40 | + $this->dieUsage( wfMsgExt( 'ep-addinstructor-invalid-course' ), 'invalid-course' ); |
| 41 | + } |
| 42 | + |
29 | 43 | $this->getResult()->addValue( |
30 | 44 | null, |
31 | 45 | 'success', |
32 | | - $everythingOk |
| 46 | + $course->addInstructors( array( $userId ) ) |
33 | 47 | ); |
34 | 48 | } |
35 | 49 | |
— | — | @@ -44,14 +58,14 @@ |
45 | 59 | return method_exists( 'ApiBase', 'getUser' ) ? parent::getUser() : $GLOBALS['wgUser']; |
46 | 60 | } |
47 | 61 | |
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 | +// } |
51 | 69 | |
52 | | - public function mustBePosted() { |
53 | | - return true; |
54 | | - } |
55 | | - |
56 | 70 | public function getAllowedParams() { |
57 | 71 | return array( |
58 | 72 | 'username' => array( |
— | — | @@ -88,6 +102,8 @@ |
89 | 103 | public function getPossibleErrors() { |
90 | 104 | return array_merge( parent::getPossibleErrors(), array( |
91 | 105 | 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' ), |
92 | 108 | ) ); |
93 | 109 | } |
94 | 110 | |