Campustream 1.0
A social network MQP for WPI
application/models/question.php
Go to the documentation of this file.
00001 <?
00002 
00006 class Question_Model extends ActiveRecord {
00007         public $table_name = 'questions';
00008         public $columns = array('id', 'user_id', 'category', 'title', 'message', 'post_date', 'last_update', 'vote_count', 'is_public', 'deleted');
00009         public $cache_keys = array( 'question/#' => 'id', 'questions/user:#' => 'user_id' );
00010         public $has_many = array('responses', 'tags');
00011         public $has_one = array('user');
00012         public $public_columns = array( 'id', 'short_id', 'user_id', 'category', 'title', 'message', 'post_date', 'last_update', 'vote_count', 'user', 'num_responses', 'responses', 'tags' );
00013         
00014         public static $categories = array('academic', 'social', 'misc', 'happenings');
00015         
00019         public function load_user() {
00020                 if (!$this->user) {
00021                         $user = ActiveCache::find('User_Model', $this->user_id, 43200)->sql(
00022                                 "SELECT * FROM users WHERE id = {$this->user_id} LIMIT 1"
00023                         );
00024                         
00025                         if ($user->is_loaded()) {
00026                                 $this->add_relationship('user', $user);
00027                         }
00028                 }
00029         }
00030         
00034         public function load_responses() {
00035                 if (!$this->responses) {
00036                         $responses = ActiveCache::find_all('Response_Model', "question:{$this->id}", 43200)->sql(
00037                                 "SELECT * FROM responses WHERE question_id = {$this->id} ORDER BY vote_count DESC"
00038                         );
00039                         
00040                         $this->add_relationship('responses', $responses);
00041                 }
00042         }
00043         
00047         public function num_responses() {
00048                 $this->load_responses();
00049                 return count($this->responses);
00050         }
00051         
00055         public function load_tags() {
00056                 if (!$this->tags) {
00057                         $tags = ActiveCache::find_all('Questiontag_Model', "question:{$this->id}", 43200)->sql(
00058                                 "SELECT * FROM questiontags WHERE question_id = {$this->id}"
00059                         );
00060                         
00061                         if (count($tags) > 0) {
00062                                 $this->add_relationship('tags', $tags);
00063                         }
00064                 }
00065         }
00066         
00070         public function save($is_new = true) {
00071                 // Save to MySQL first
00072                 parent::save();
00073                 
00074                 if (!$is_new) {
00075                         return true;
00076                 }
00077                 
00078                 // and save it to Redis
00079                 $r = RedisManager::connection();                
00080                 $num = $r->lpush("questions:stream:{$this->category}:newest", $this->id);
00081                 if ($num > 200) {
00082                         $r->ltrim("questions:stream:{$this->category}:newest", 0, 200);
00083                 }
00084                 
00085                 // then create the status message for it
00086                 $status = new Status_Model();
00087                 $status->user_id = $this->user_id;
00088                 $status->source = 'site';
00089                 $status->message = $this->title;
00090                 $status->post_date = ActiveRecord::NOW();
00091                 $status->type = 'question';
00092                 $status->content_id = $this->id;
00093                 $status->is_public = $this->is_public;
00094                 $status->save();
00095         }
00096         
00100         public function delete() {
00101                 $this->deleted = 1;
00102                 $this->save(false);
00103                 
00104                 $r = RedisManager::connection();
00105                 $r->lrem("questions:stream:{$this->category}:newest", 0, $this->id);
00106                 
00107                 return true;
00108         }
00109         
00110         public function __get($key) {
00111                 switch ($key) {
00112                         case 'short_id': return base_convert($this->id, 10, 36);
00113                         case 'num_responses': return $this->num_responses();
00114                 }
00115                 
00116                 return parent::__get($key);
00117         }
00118 }