Campustream 1.0
A social network MQP for WPI
|
00001 <? 00002 00006 class Response_Model extends ActiveRecord { 00007 public $table_name = 'responses'; 00008 public $columns = array('id', 'question_id', 'user_id', 'message', 'post_date', 'vote_count', 'is_public'); 00009 public $cache_keys = array( 'response/#' => 'id', 'responses/question:#' => 'question_id', 'responses/user:#' => 'user_id' ); 00010 public $has_one = array('question', 'user'); 00011 public $public_columns = array( 'id', 'question_id', 'user_id', 'message', 'post_date', 'vote_count', 'up_votes', 'down_votes' ); 00012 00016 public function load_user() { 00017 if (!$this->user) { 00018 $user = ActiveCache::find('User_Model', $this->user_id, 43200)->sql( 00019 "SELECT * FROM users WHERE id = {$this->user_id} LIMIT 1" 00020 ); 00021 00022 if ($user->is_loaded()) { 00023 $this->add_relationship('user', $user); 00024 } 00025 } 00026 } 00027 00031 public function load_question() { 00032 if (!$this->question) { 00033 $question = ActiveCache::find('Question_Model', $this->question_id, 43200)->sql( 00034 "SELECT * FROM questions WHERE id = {$this->question_id} LIMIT 1" 00035 ); 00036 00037 if ($question->is_loaded()) { 00038 $this->add_relationship('question', $question); 00039 } 00040 } 00041 } 00042 00048 public function vote($dir) { 00049 // First we need to check to make sure this user hasn't voted on this response before 00050 $r = RedisManager::connection(); 00051 $userID = sess::getUserID(); 00052 $dir = strtolower($dir); 00053 $has_voted = $this->has_voted(); 00054 00055 if ($has_voted === 'up') { 00056 $this->vote_count--; 00057 $r->srem("voting:response:up:{$this->id}", $userID); 00058 } elseif ($has_voted === 'down') { 00059 $this->vote_count++; 00060 $r->srem("voting:response:down:{$this->id}", $userID); 00061 } 00062 00063 $this->load_user(); 00064 00065 if ($has_voted === 'up' && $dir === 'up') { 00066 $this->save(false); 00067 $this->user->karma--; 00068 $this->user->save(); 00069 return; 00070 } 00071 00072 if ($has_voted === 'down' && $dir === 'down') { 00073 $this->save(false); 00074 $this->user->karma++; 00075 $this->user->save(); 00076 return; 00077 } 00078 00079 if ($dir === 'up') { 00080 $this->vote_count++; 00081 $this->user->karma++; 00082 } else { 00083 $this->vote_count--; 00084 $this->user->karma--; 00085 } 00086 00087 $this->user->save(); 00088 00089 // Add user ID to the voted set 00090 $r->sadd("voting:response:{$dir}:{$this->id}", $userID); 00091 $this->save(false); 00092 } 00093 00097 public function has_voted() { 00098 $r = RedisManager::connection(); 00099 $userID = sess::getUserID(); 00100 $has_voted = $r->sismember("voting:response:up:{$this->id}", $userID); 00101 if ($has_voted) { 00102 return 'up'; 00103 } 00104 00105 $has_voted = $r->sismember("voting:response:down:{$this->id}", $userID); 00106 if ($has_voted) { 00107 return 'down'; 00108 } 00109 00110 return false; 00111 } 00112 00113 private function individual_vote_count($dir) { 00114 $r = RedisManager::connection(); 00115 return $r->scard("voting:response:{$dir}:{$this->id}"); 00116 } 00117 00121 public function save($post_status = true) { 00122 // We need to save this first, because it will change 00123 // 2 => 1 when we save to MySQL 00124 $is_public = $this->is_public; 00125 if ($is_public === 2) { 00126 $this->is_public = 0; 00127 } 00128 00129 parent::save(); 00130 00131 if (!$post_status) { 00132 return true; 00133 } 00134 00135 $this->load_question(); 00136 if ($this->user_id != $this->question->user_id) { 00137 $n = notifications::load(); 00138 $n->send($this->question->user_id, 'collaborate', "has posted a reply to your question: {$this->question->title}", $this->question->short_id); 00139 } 00140 00141 $m = MemcacheManager::connection(); 00142 $m->delete("responses/question:{$this->question_id}"); 00143 00144 if ($is_public == 2) { 00145 return true; 00146 } 00147 00148 // then create the status message for it 00149 $status = new Status_Model(); 00150 $status->user_id = $this->user_id; 00151 $status->source = 'site'; 00152 $status->message = $this->message; 00153 $status->post_date = ActiveRecord::NOW(); 00154 $status->type = 'response'; 00155 $status->content_id = $this->question->id; 00156 $status->is_public = $this->is_public; 00157 $status->save(); 00158 00159 } 00160 00161 public function __get($key) { 00162 switch ($key) { 00163 case 'up_votes': return $this->individual_vote_count('up'); 00164 case 'down_votes': return $this->individual_vote_count('down'); 00165 } 00166 00167 return parent::__get($key); 00168 } 00169 }