Index: trunk/extensions/Contest/includes/ContestDBObject.php |
— | — | @@ -327,6 +327,9 @@ |
328 | 328 | case 'int': |
329 | 329 | $value = (int)$value; |
330 | 330 | break; |
| 331 | + case 'float': |
| 332 | + $value = (float)$value; |
| 333 | + break; |
331 | 334 | case 'bool': |
332 | 335 | if ( is_string( $value ) ) { |
333 | 336 | $value = $value !== '0'; |
— | — | @@ -362,6 +365,7 @@ |
363 | 366 | * * id |
364 | 367 | * * str |
365 | 368 | * * int |
| 369 | + * * float |
366 | 370 | * * bool |
367 | 371 | * * array |
368 | 372 | * |
— | — | @@ -492,6 +496,45 @@ |
493 | 497 | } |
494 | 498 | |
495 | 499 | /** |
| 500 | + * Add an amount (can be negative) to the specified field (needs to be numeric). |
| 501 | + * |
| 502 | + * @since 0.1 |
| 503 | + * |
| 504 | + * @param string $field |
| 505 | + * @param integer $amount |
| 506 | + * |
| 507 | + * @return boolean Success indicator |
| 508 | + */ |
| 509 | + public function addToField( $field, $amount ) { |
| 510 | + if ( $amount == 0 ) { |
| 511 | + return true; |
| 512 | + } |
| 513 | + |
| 514 | + if ( !$this->hasIdField() ) { |
| 515 | + return false; |
| 516 | + } |
| 517 | + |
| 518 | + $absoluteAmount = abs( $amount ); |
| 519 | + $isNegative = $amount < 0; |
| 520 | + |
| 521 | + $dbw = wfGetDB( DB_MASTER ); |
| 522 | + |
| 523 | + $fullField = $this->getPrefixedField( $field ); |
| 524 | + |
| 525 | + $success = $dbw->update( |
| 526 | + $this->getDBTable(), |
| 527 | + array( "$fullField=$fullField" . ( $isNegative ? '-' : '+' ) . $absoluteAmount ), |
| 528 | + array( $this->getPrefixedField( 'id' ) => $this->getId() ) |
| 529 | + ); |
| 530 | + |
| 531 | + if ( $success && $this->hasField( $field ) ) { |
| 532 | + $this->setField( $field, $this->getField( $field ) + $amount ); |
| 533 | + } |
| 534 | + |
| 535 | + return $success; |
| 536 | + } |
| 537 | + |
| 538 | + /** |
496 | 539 | * Selects the the specified fields of the records matching the provided |
497 | 540 | * conditions. Field names get prefixed. |
498 | 541 | * |
Index: trunk/extensions/Contest/includes/ContestVote.php |
— | — | @@ -0,0 +1,120 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * Class representing a single contest vote. |
| 6 | + * Votes can be made by judges on (submissions of) contestants. |
| 7 | + * |
| 8 | + * @since 0.1 |
| 9 | + * |
| 10 | + * @file ContestComment.php |
| 11 | + * @ingroup Contest |
| 12 | + * |
| 13 | + * @licence GNU GPL v3 or later |
| 14 | + * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
| 15 | + */ |
| 16 | +class ContestVote extends ContestDBObject { |
| 17 | + |
| 18 | + /** |
| 19 | + * Method to get an instance so methods that ought to be static, |
| 20 | + * but can't be due to PHP 5.2 not having LSB, can be called on |
| 21 | + * it. This also allows easy identifying of code that needs to |
| 22 | + * be changed once PHP 5.3 becomes an acceptable requirement. |
| 23 | + * |
| 24 | + * @since 0.1 |
| 25 | + * |
| 26 | + * @return ContestDBObject |
| 27 | + */ |
| 28 | + public static function s() { |
| 29 | + static $instance = false; |
| 30 | + |
| 31 | + if ( $instance === false ) { |
| 32 | + $instance = new self( array() ); |
| 33 | + } |
| 34 | + |
| 35 | + return $instance; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Get a new instance of the class from an array. |
| 40 | + * This method ought to be in the basic class and |
| 41 | + * return a new static(), but this requires LSB/PHP>=5.3. |
| 42 | + * |
| 43 | + * @since 0.1 |
| 44 | + * |
| 45 | + * @param array $data |
| 46 | + * @param boolean $loadDefaults |
| 47 | + * |
| 48 | + * @return ContestDBObject |
| 49 | + */ |
| 50 | + public function newFromArray( array $data, $loadDefaults = false ) { |
| 51 | + return new self( $data, $loadDefaults ); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @see parent::getFieldTypes |
| 56 | + * |
| 57 | + * @since 0.1 |
| 58 | + * |
| 59 | + * @return string |
| 60 | + */ |
| 61 | + public function getDBTable() { |
| 62 | + return 'contest_votes'; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @see parent::getFieldTypes |
| 67 | + * |
| 68 | + * @since 0.1 |
| 69 | + * |
| 70 | + * @return string |
| 71 | + */ |
| 72 | + protected function getFieldPrefix() { |
| 73 | + return 'vote_'; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @see parent::getFieldTypes |
| 78 | + * |
| 79 | + * @since 0.1 |
| 80 | + * |
| 81 | + * @return array |
| 82 | + */ |
| 83 | + protected function getFieldTypes() { |
| 84 | + return array( |
| 85 | + 'id' => 'id', |
| 86 | + 'contestant_id' => 'id', |
| 87 | + 'user_id' => 'id', |
| 88 | + |
| 89 | + 'value' => 'int', |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @see parent::getDefaults |
| 95 | + * |
| 96 | + * @since 0.1 |
| 97 | + * |
| 98 | + * @return array |
| 99 | + */ |
| 100 | + public function getDefaults() { |
| 101 | + return array( |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * (non-PHPdoc) |
| 107 | + * @see ContestDBObject::writeToDB() |
| 108 | + */ |
| 109 | + public function writeToDB() { |
| 110 | + $success = parent::writeToDB(); |
| 111 | + |
| 112 | + if ( $success ) { |
| 113 | + return ContestContestant::s() |
| 114 | + ->select( null, array( 'id' => $this->getField( 'contestant_id' ) ) ) |
| 115 | + ->updateVotes(); |
| 116 | + } |
| 117 | + |
| 118 | + return $success; |
| 119 | + } |
| 120 | + |
| 121 | +} |
Property changes on: trunk/extensions/Contest/includes/ContestVote.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 122 | + native |
Index: trunk/extensions/Contest/includes/ContestComment.php |
— | — | @@ -2,7 +2,7 @@ |
3 | 3 | |
4 | 4 | /** |
5 | 5 | * Class representing a single contest comment. |
6 | | - * Comments can be made by judges on submissions of contestants. |
| 6 | + * Comments can be made by judges on (submissions of) contestants. |
7 | 7 | * |
8 | 8 | * @since 0.1 |
9 | 9 | * |
— | — | @@ -83,7 +83,10 @@ |
84 | 84 | return array( |
85 | 85 | 'id' => 'id', |
86 | 86 | 'contestant_id' => 'id', |
| 87 | + 'user_id' => 'id', |
| 88 | + |
87 | 89 | 'text' => 'str', |
| 90 | + 'time' => 'int', |
88 | 91 | ); |
89 | 92 | } |
90 | 93 | |
— | — | @@ -97,7 +100,24 @@ |
98 | 101 | public function getDefaults() { |
99 | 102 | return array( |
100 | 103 | 'text' => '', |
| 104 | + 'time' => 0, |
101 | 105 | ); |
102 | 106 | } |
103 | 107 | |
| 108 | + /** |
| 109 | + * (non-PHPdoc) |
| 110 | + * @see ContestDBObject::insertIntoDB() |
| 111 | + */ |
| 112 | + protected function insertIntoDB() { |
| 113 | + $success = parent::insertIntoDB(); |
| 114 | + |
| 115 | + if ( $success ) { |
| 116 | + ContestContestant::s() |
| 117 | + ->select( 'id', array( 'id' => $this->getField( 'contestant_id' ) ) ) |
| 118 | + ->addToField( 'comments', 1 ); |
| 119 | + } |
| 120 | + |
| 121 | + return $success; |
| 122 | + } |
| 123 | + |
104 | 124 | } |
Index: trunk/extensions/Contest/includes/ContestContestant.php |
— | — | @@ -98,7 +98,7 @@ |
99 | 99 | |
100 | 100 | 'submission' => 'str', |
101 | 101 | |
102 | | - 'rating' => 'int', |
| 102 | + 'rating' => 'float', |
103 | 103 | 'rating_count' => 'int', |
104 | 104 | 'comments' => 'int', |
105 | 105 | ); |
— | — | @@ -452,4 +452,47 @@ |
453 | 453 | return $success; |
454 | 454 | } |
455 | 455 | |
| 456 | + /** |
| 457 | + * Update the vote count and avarage vote fields. |
| 458 | + * This does not write the changes to the database, |
| 459 | + * if this is required, call writeToDB. |
| 460 | + * |
| 461 | + * @since 0.1 |
| 462 | + */ |
| 463 | + public function updateVotes() { |
| 464 | + $votes = $this->getVotes(); |
| 465 | + |
| 466 | + $amount = count( $votes ); |
| 467 | + $total = 0; |
| 468 | + |
| 469 | + foreach ( $votes as /* ContestVote */ $vote ) { |
| 470 | + $total += $vote->getField( 'value' ); |
| 471 | + } |
| 472 | + |
| 473 | + $this->setField( 'rating_count', $amount ); |
| 474 | + $this->setField( 'rating', $amount > 0 ? $total / $amount : 0 ); |
| 475 | + } |
| 476 | + |
| 477 | + /** |
| 478 | + * Get the votes for this contestant. |
| 479 | + * |
| 480 | + * @since 0.1 |
| 481 | + * |
| 482 | + * @return array of ContestVote |
| 483 | + */ |
| 484 | + public function getVotes() { |
| 485 | + return ContestVote::s()->select( null, array( 'contestant_id' => $this->getId() ) ); |
| 486 | + } |
| 487 | + |
| 488 | + /** |
| 489 | + * Get the comments for this contestant. |
| 490 | + * |
| 491 | + * @since 0.1 |
| 492 | + * |
| 493 | + * @return array of ContestComment |
| 494 | + */ |
| 495 | + public function getComments() { |
| 496 | + return ContestComment::s()->select( null, array( 'contestant_id' => $this->getId() ) ); |
| 497 | + } |
| 498 | + |
456 | 499 | } |
Index: trunk/extensions/Contest/includes/Contest.class.php |
— | — | @@ -370,28 +370,7 @@ |
371 | 371 | * @return boolean Success indicator |
372 | 372 | */ |
373 | 373 | public function addToSubmissionCount( $amount ) { |
374 | | - if ( $amount == 0 ) { |
375 | | - return true; |
376 | | - } |
377 | | - |
378 | | - $absoluteAmount = abs( $amount ); |
379 | | - $isNegative = $amount < 0; |
380 | | - |
381 | | - $dbw = wfGetDB( DB_MASTER ); |
382 | | - |
383 | | - $countField = $this->getPrefixedField( 'submission_count' ); |
384 | | - |
385 | | - $success = $dbw->update( |
386 | | - $this->getDBTable(), |
387 | | - array( "$countField=$countField" . ( $isNegative ? '-' : '+' ) . $absoluteAmount ), |
388 | | - array( $this->getPrefixedField( 'id' ) => $this->getId() ) |
389 | | - ); |
390 | | - |
391 | | - if ( $success && $this->hasField( 'submission_count' ) ) { |
392 | | - $this->setField( 'submission_count', $this->getField( 'submission_count' ) + $amount ); |
393 | | - } |
394 | | - |
395 | | - return $success; |
| 374 | + return parent::addToField( 'submission_count', $amount ); |
396 | 375 | } |
397 | 376 | |
398 | 377 | /** |