| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
require_once 'lib/model/om/BaseQuestion.php'; |
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
class Question extends BaseQuestion |
|---|
| 18 |
{ |
|---|
| 19 |
public function setTitle($v) |
|---|
| 20 |
{ |
|---|
| 21 |
parent::setTitle($v); |
|---|
| 22 |
|
|---|
| 23 |
$this->setStrippedTitle(myTools::stripText($v)); |
|---|
| 24 |
} |
|---|
| 25 |
|
|---|
| 26 |
public function setBody($v) |
|---|
| 27 |
{ |
|---|
| 28 |
parent::setBody($v); |
|---|
| 29 |
|
|---|
| 30 |
require_once('markdown.php'); |
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
$v = htmlentities($v, ENT_QUOTES, 'UTF-8'); |
|---|
| 34 |
|
|---|
| 35 |
$this->setHtmlBody(markdown($v)); |
|---|
| 36 |
} |
|---|
| 37 |
|
|---|
| 38 |
public function getAllInterestedUsers() |
|---|
| 39 |
{ |
|---|
| 40 |
$c = new Criteria(); |
|---|
| 41 |
$c->addJoin(UserPeer::ID, InterestPeer::USER_ID, Criteria::LEFT_JOIN); |
|---|
| 42 |
$c->add(InterestPeer::QUESTION_ID, $this->getId()); |
|---|
| 43 |
|
|---|
| 44 |
return UserPeer::doSelect($c); |
|---|
| 45 |
} |
|---|
| 46 |
|
|---|
| 47 |
public function getInterestedUsersPager($page) |
|---|
| 48 |
{ |
|---|
| 49 |
$c = new Criteria(); |
|---|
| 50 |
$c->addJoin(UserPeer::ID, InterestPeer::USER_ID, Criteria::LEFT_JOIN); |
|---|
| 51 |
$c->add(InterestPeer::QUESTION_ID, $this->getId()); |
|---|
| 52 |
|
|---|
| 53 |
$pager = new sfPropelPager('User', sfConfig::get('app_pager_users_max')); |
|---|
| 54 |
$pager->setCriteria($c); |
|---|
| 55 |
$pager->setPage($page); |
|---|
| 56 |
$pager->init(); |
|---|
| 57 |
|
|---|
| 58 |
return $pager; |
|---|
| 59 |
} |
|---|
| 60 |
|
|---|
| 61 |
public function getTags() |
|---|
| 62 |
{ |
|---|
| 63 |
$c = new Criteria(); |
|---|
| 64 |
$c->add(QuestionTagPeer::QUESTION_ID, $this->getId()); |
|---|
| 65 |
$c->addGroupByColumn(QuestionTagPeer::NORMALIZED_TAG); |
|---|
| 66 |
$c->setDistinct(); |
|---|
| 67 |
$c->addAscendingOrderByColumn(QuestionTagPeer::NORMALIZED_TAG); |
|---|
| 68 |
|
|---|
| 69 |
$tags = array(); |
|---|
| 70 |
foreach (QuestionTagPeer::doSelect($c) as $tag) |
|---|
| 71 |
{ |
|---|
| 72 |
if (sfConfig::get('app_permanent_tag') == $tag) |
|---|
| 73 |
{ |
|---|
| 74 |
continue; |
|---|
| 75 |
} |
|---|
| 76 |
|
|---|
| 77 |
$tags[] = $tag->getNormalizedTag(); |
|---|
| 78 |
} |
|---|
| 79 |
|
|---|
| 80 |
return $tags; |
|---|
| 81 |
} |
|---|
| 82 |
|
|---|
| 83 |
public function getPopularTags($max = 5) |
|---|
| 84 |
{ |
|---|
| 85 |
$tags = array(); |
|---|
| 86 |
|
|---|
| 87 |
$con = Propel::getConnection(); |
|---|
| 88 |
$query = ' |
|---|
| 89 |
SELECT %s AS tag, COUNT(%s) AS count |
|---|
| 90 |
FROM %s |
|---|
| 91 |
WHERE %s = ? |
|---|
| 92 |
GROUP BY %s |
|---|
| 93 |
ORDER BY count DESC |
|---|
| 94 |
'; |
|---|
| 95 |
|
|---|
| 96 |
$query = sprintf($query, |
|---|
| 97 |
QuestionTagPeer::NORMALIZED_TAG, |
|---|
| 98 |
QuestionTagPeer::NORMALIZED_TAG, |
|---|
| 99 |
QuestionTagPeer::TABLE_NAME, |
|---|
| 100 |
QuestionTagPeer::QUESTION_ID, |
|---|
| 101 |
QuestionTagPeer::NORMALIZED_TAG |
|---|
| 102 |
); |
|---|
| 103 |
|
|---|
| 104 |
$stmt = $con->prepareStatement($query); |
|---|
| 105 |
$stmt->setInt(1, $this->getId()); |
|---|
| 106 |
$stmt->setLimit($max); |
|---|
| 107 |
$rs = $stmt->executeQuery(); |
|---|
| 108 |
while ($rs->next()) |
|---|
| 109 |
{ |
|---|
| 110 |
if (sfConfig::get('app_permanent_tag') == $rs->getString('tag')) |
|---|
| 111 |
{ |
|---|
| 112 |
continue; |
|---|
| 113 |
} |
|---|
| 114 |
|
|---|
| 115 |
$tags[$rs->getString('tag')] = $rs->getInt('count'); |
|---|
| 116 |
} |
|---|
| 117 |
|
|---|
| 118 |
return $tags; |
|---|
| 119 |
} |
|---|
| 120 |
|
|---|
| 121 |
public function addTagsForUser($phrase, $userId) |
|---|
| 122 |
{ |
|---|
| 123 |
|
|---|
| 124 |
$tags = Tag::splitPhrase($phrase.' '.sfConfig::get('app_permanent_tag')); |
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
foreach ($tags as $tag) |
|---|
| 128 |
{ |
|---|
| 129 |
$questionTag = new QuestionTag(); |
|---|
| 130 |
$questionTag->setQuestionId($this->getId()); |
|---|
| 131 |
$questionTag->setUserId($userId); |
|---|
| 132 |
$questionTag->setTag($tag); |
|---|
| 133 |
try |
|---|
| 134 |
{ |
|---|
| 135 |
$questionTag->save(); |
|---|
| 136 |
} |
|---|
| 137 |
catch (PropelException $e) |
|---|
| 138 |
{ |
|---|
| 139 |
|
|---|
| 140 |
} |
|---|
| 141 |
} |
|---|
| 142 |
} |
|---|
| 143 |
|
|---|
| 144 |
public function getPopularAnswers() |
|---|
| 145 |
{ |
|---|
| 146 |
$c = new Criteria(); |
|---|
| 147 |
$c->add(AnswerPeer::QUESTION_ID, $this->getId()); |
|---|
| 148 |
$c->addAsColumn('relevancy', AnswerPeer::RELEVANCY_UP.' / ('.AnswerPeer::RELEVANCY_UP.' + '.AnswerPeer::RELEVANCY_DOWN.')'); |
|---|
| 149 |
$c->addDescendingOrderByColumn('relevancy'); |
|---|
| 150 |
$c->addDescendingOrderByColumn(AnswerPeer::RELEVANCY_UP); |
|---|
| 151 |
|
|---|
| 152 |
return AnswerPeer::doSelect($c); |
|---|
| 153 |
} |
|---|
| 154 |
|
|---|
| 155 |
public function getWords() |
|---|
| 156 |
{ |
|---|
| 157 |
|
|---|
| 158 |
$raw_text = str_repeat(' '.strip_tags($this->getHtmlBody()), sfConfig::get('app_search_body_weight')); |
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 |
$raw_text .= str_repeat(' '.$this->getTitle(), sfConfig::get('app_search_title_weight')); |
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 |
$stemmed_words = myTools::stemPhrase($raw_text); |
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 |
$words = array_count_values($stemmed_words); |
|---|
| 168 |
|
|---|
| 169 |
|
|---|
| 170 |
$max = 0; |
|---|
| 171 |
foreach ($this->getPopularTags(20) as $tag => $count) |
|---|
| 172 |
{ |
|---|
| 173 |
if (!$max) |
|---|
| 174 |
{ |
|---|
| 175 |
$max = $count; |
|---|
| 176 |
} |
|---|
| 177 |
|
|---|
| 178 |
$stemmed_tag = PorterStemmer::stem($tag); |
|---|
| 179 |
|
|---|
| 180 |
if (!isset($words[$stemmed_tag])) |
|---|
| 181 |
{ |
|---|
| 182 |
$words[$stemmed_tag] = 0; |
|---|
| 183 |
} |
|---|
| 184 |
$words[$stemmed_tag] += ceil(($count / $max) * sfConfig::get('app_search_tag_weight')); |
|---|
| 185 |
} |
|---|
| 186 |
|
|---|
| 187 |
return $words; |
|---|
| 188 |
} |
|---|
| 189 |
|
|---|
| 190 |
public function save($con = null) |
|---|
| 191 |
{ |
|---|
| 192 |
$con = Propel::getConnection(); |
|---|
| 193 |
try |
|---|
| 194 |
{ |
|---|
| 195 |
$con->begin(); |
|---|
| 196 |
|
|---|
| 197 |
$ret = parent::save(); |
|---|
| 198 |
|
|---|
| 199 |
$this->updateSearchIndex(); |
|---|
| 200 |
|
|---|
| 201 |
$con->commit(); |
|---|
| 202 |
|
|---|
| 203 |
return $ret; |
|---|
| 204 |
} |
|---|
| 205 |
catch (Exception $e) |
|---|
| 206 |
{ |
|---|
| 207 |
$con->rollback(); |
|---|
| 208 |
throw $e; |
|---|
| 209 |
} |
|---|
| 210 |
} |
|---|
| 211 |
|
|---|
| 212 |
public function updateSearchIndex() |
|---|
| 213 |
{ |
|---|
| 214 |
|
|---|
| 215 |
$c = new Criteria(); |
|---|
| 216 |
$c->add(SearchIndexPeer::QUESTION_ID, $this->getId()); |
|---|
| 217 |
SearchIndexPeer::doDelete($c); |
|---|
| 218 |
|
|---|
| 219 |
foreach ($this->getWords() as $word => $weight) |
|---|
| 220 |
{ |
|---|
| 221 |
$index = new SearchIndex(); |
|---|
| 222 |
$index->setQuestionId($this->getId()); |
|---|
| 223 |
$index->setWord($word); |
|---|
| 224 |
$index->setWeight($weight); |
|---|
| 225 |
$index->save(); |
|---|
| 226 |
} |
|---|
| 227 |
} |
|---|
| 228 |
|
|---|
| 229 |
public function hasTag($tag) |
|---|
| 230 |
{ |
|---|
| 231 |
$c = new Criteria(); |
|---|
| 232 |
$c->add(QuestionTagPeer::QUESTION_ID, $this->getId()); |
|---|
| 233 |
$c->add(QuestionTagPeer::NORMALIZED_TAG, Tag::normalize($tag)); |
|---|
| 234 |
|
|---|
| 235 |
return QuestionTagPeer::doSelectOne($c) ? true : false; |
|---|
| 236 |
} |
|---|
| 237 |
|
|---|
| 238 |
public function deleteReports() |
|---|
| 239 |
{ |
|---|
| 240 |
$reports = $this->getReportQuestions(); |
|---|
| 241 |
foreach ($reports as $report) |
|---|
| 242 |
{ |
|---|
| 243 |
$report->delete(); |
|---|
| 244 |
} |
|---|
| 245 |
|
|---|
| 246 |
$this->setReports(0); |
|---|
| 247 |
} |
|---|
| 248 |
|
|---|
| 249 |
public function deleteSpam($moderator) |
|---|
| 250 |
{ |
|---|
| 251 |
$con = Propel::getConnection(); |
|---|
| 252 |
try |
|---|
| 253 |
{ |
|---|
| 254 |
$con->begin(); |
|---|
| 255 |
|
|---|
| 256 |
$user = $this->getUser(); |
|---|
| 257 |
$user->setDeletions($user->getDeletions() + 1); |
|---|
| 258 |
$user->save(); |
|---|
| 259 |
|
|---|
| 260 |
$this->delete(); |
|---|
| 261 |
|
|---|
| 262 |
$con->commit(); |
|---|
| 263 |
|
|---|
| 264 |
$log = 'moderator "%s" deleted question "%s"'; |
|---|
| 265 |
$log = sprintf($log, $moderator->getNickname(), $this->getTitle()); |
|---|
| 266 |
sfContext::getInstance()->getLogger()->warning($log); |
|---|
| 267 |
} |
|---|
| 268 |
catch (PropelException $e) |
|---|
| 269 |
{ |
|---|
| 270 |
$con->rollback(); |
|---|
| 271 |
throw $e; |
|---|
| 272 |
} |
|---|
| 273 |
} |
|---|
| 274 |
|
|---|
| 275 |
public function getFeedAuthorEmail() |
|---|
| 276 |
{ |
|---|
| 277 |
return ''; |
|---|
| 278 |
} |
|---|
| 279 |
} |
|---|
| 280 |
|
|---|
| 281 |
?> |
|---|