Created
November 3, 2010 16:15
-
-
Save rmasters/661285 to your computer and use it in GitHub Desktop.
Aim: confuse rich
This file contains hidden or 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 Model_Question extends WebApp_Model | |
{ | |
protected $id; | |
protected $question; | |
protected $choices = array(); | |
protected $asker; | |
protected $asked; | |
protected $visible = true; | |
public function setId($id) { | |
$this->id = (int) $id; | |
} | |
public function setQuestion($question) { | |
$question = trim($question); | |
if (empty($question)) { | |
throw new InvalidArgument("Question cannot be empty"); | |
} | |
if (strlen($question) > 140) { | |
throw new InvalidArgument("Question must be shorter than 140 characters"); | |
} | |
$this->question = htmlentities($question); | |
} | |
public function setChoices(array $choices) { | |
foreach ($choices as $choice) { | |
if (!$choice instanceof Model_Choice) { | |
throw new InvalidArgument("Choices must be instances of Model_Choice"); | |
} | |
} | |
} | |
public function addChoice($choice) { | |
if (!$choice instanceof Model_Choice) { | |
if (is_int($choice)) { | |
$choice = Model_Choice::fetch($choice); | |
if (!$choice) { | |
throw new InvalidArgument("Supply choice as an instance of Model_Choice, a choice id or a string"); | |
} | |
} else { | |
$choice = new Model_Choice(array( | |
"label" => $choice, | |
"post" => $this | |
)); | |
} | |
} | |
$this->choices[] = $choice; | |
} | |
public function setAsker($user) { | |
if (!$user instanceof Model_User) { | |
$user = Model_User::fetch($user); | |
if (!$user) { | |
throw new InvalidArgument("Supply asker as an instance of Model_User or a user id"); | |
} | |
} | |
$this->asker = $user; | |
} | |
public function setAsked($when) { | |
if (!is_int($when)) { | |
$when = strtotime($when); | |
if (!$when) { | |
throw new InvalidArgument("Supply asked as a timestamp or valid date string accepted by strtotime"); | |
} | |
} | |
$this->asked = $when; | |
} | |
public function getAsked() { | |
if ($this->asked == null) { | |
$this->asked = time(); | |
} | |
return $this->asked; | |
} | |
public function setVisible($visible) { | |
$this->visible = (bool) $visible; | |
} | |
public function save() { | |
$values = array( | |
":id" => $this->id, | |
":question" => $this->question, | |
":asker" => $this->asker->id, | |
":asked" => date("Y-m-d H:i:s", $this->asked), | |
":visible" => $this->visible | |
); | |
if ($this->id) { | |
$sql = "INSERT INTO question (question, asker, asked, visible) " . | |
"VALUES (question, :asker, :asked, :visible)"; | |
unset($values[":id"]); | |
} else { | |
$sql = "UPDATE question " . | |
"SET question = :question, asker = :asker, asked = :asked, visible = :visible " . | |
"WHERE id = :id LIMIT 1"; | |
} | |
// @todo Move to WebApp_Model::save() and call parent::save() | |
$stmt = parent::getDb()->prepare($sql); | |
$success = $stmt->execute($values); | |
$stmt->closeCursor(); | |
// Check for an error | |
// @todo Also move, base 'question' off the class name | |
if (!$success) { | |
$action = isset($id) ? "update" : "insert"; | |
throw new DataError("Could not $action the question", parent::getDb()->errorInfo()); | |
} | |
// Set the new id | |
$this->id = $id; | |
// Save any choices that have been added | |
foreach ($this->choices as $choice) { | |
$choice->save(); | |
} | |
// @todo Depends on whether askers can be anonymous, change later | |
//$this->asker->save(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment