Created
October 4, 2011 14:15
-
-
Save jaspertandy/1261742 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Competition_Validation extends Validation { | |
protected $competition; | |
protected $guess; | |
public function init(){ | |
$this->add_rule( 'name' , 'required' ); | |
$this->add_rule( 'email' , 'required' ); | |
$this->add_rule( 'email' , 'email' ); | |
$this->add_rule( 'answer' , array( $this , 'valid_competition' ) ); | |
$this->add_rule( 'email' , array( $this , 'ip_cant_enter' ) ); | |
} | |
public function valid_competition( Validation $v , $field ){ | |
$this->competition = Competitions::factory( $this->d() )->get_latest_live(); | |
if ( !$this->competition->loaded() ) return false; | |
foreach ( $this->competition->answers() as $answer ) { | |
if ( (int) $answer['id'] === (int) $v->$field ) $this->guess = $answer; | |
} | |
return $this->guess ? true : false; | |
} | |
public function ip_cant_enter( Validation $v , $field ){ | |
if ( !$this->competition ) return false; | |
$entries = $this->d() | |
->db | |
->query('select count(*) c | |
from competitionGuesses | |
where ip = :ip and competitionId = :competitionid' , array( | |
'ip' => $_SERVER['REMOTE_ADDR'], | |
'competitionid' => $this->competition->id | |
)) | |
->shift(); | |
return $entries['c'] > 0 ? false : true; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment