-
Notifications
You must be signed in to change notification settings - Fork 1
/
Question.php
239 lines (217 loc) · 6.57 KB
/
Question.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
class Wenda_Model_Question extends RFLib_Model_Abstract
{
/**
* 取问题的toke和subject资料
* @param int $id
* @return array | null
*/
public function getShortDataById($id)
{
return $this->getTable('Question')->getShortDataById($id);
}
/**
* 取问题的详细资料
*
* @param string $token
* @return array | null
*/
public function getDetailByToken($token)
{
if ($row = $this->getTable('Question')->findByToken($token)) {
return $row;
} else {
return null;
}
}
/**
* 通过用户输入的查询条件查找所有问题
*
* @param string $query
* @param int $page
* @param int $limit
* @return array | null
*/
public function getBySearchQuery($query,$page,$limit=10)
{
return $this->getTable('Question')->getBySearchQuery($query,$page,$limit);
}
/**
* 取用户最近的发表的问题
*
* @param int $userId
* @param string $ignore need ignore question
* @param int $limit
* @return array | null
*/
public function getByUserId($userId, $ignore=null, $limit=20)
{
return $this->getTable('Question')->getByUserId($userId, $ignore, $limit);
}
/**
* 取相关关健字的问题
*
* @param int $userId
* @param string $ignore need ignore question
* @param int $limit
* @return array | null
*/
public function getByKeywords($keywords,$ignore=null,$limit=10)
{
return $this->getTable('Question')->getByKeywords($keywords,$ignore,$limit);
}
/**
* 取相关关健字的问题带分页
*
* @param int $userId
* @param string $ignore need ignore question
* @param int $limit
* @return array | null
*/
public function getAllByKeywords($keywords, $page=1,$limit=50)
{
return $this->getTable('Question')->getAllByKeywords($keywords, $page, $limit);
}
/**
* Get unsolved question data
* @param int $categoryId
* @return array | null
*/
public function unsolves($categoryId=null,$paged=0,$limit=30)
{
return $this->getTable('Question')->unsolves($categoryId,$paged,$limit);
}
/**
* Get solved question data
* @param int $categoryId
* @return array | null
*/
public function solves($categoryId=null,$paged=0,$limit=30)
{
return $this->getTable('Question')->solves($categoryId,$paged,$limit);
}
/**
* Get zero answer of question data
* @param int $categoryId
* @return array | null
*/
public function zeros($categoryId=null,$paged=0,$limit=30)
{
return $this->getTable('Question')->zeros($categoryId,$paged,$limit);
}
/**
* 设置问题总共有多少回复
* @param int $questionId
* @return boolean
*/
public function setAnswers($questionId)
{
$auth = RFLib_Core::getService('authentication')->getAuth();
if (!$auth->hasIdentity()) {
return false;
}
$row = $this->getTable('Question')->findById($questionId);
if (!empty($row)) {
$date = new Zend_Date();
$user = $auth->getStorage()->read();
$row->last_answer_user_id = $user['id'];
$row->last_answer_at = $date->getTimestamp();
$row->answers = $row->answers + 1;
return $this->getTable('Question')->saveRow($row->toArray(), $row);
} else {
return false;
}
}
/*
* Set pageviews of question when user click this question
*
* @param int $questionId
* @return boolean
*/
public function setPageviews($questionId)
{
$auth = RFLib_Core::getService('authentication')->getAuth();
if (!$auth->hasIdentity()) {
return false;
}
$row = $this->getTable('Question')->findById($questionId);
if (!empty($row)) {
$row->pageviews = $row->pageviews + 1;
return $this->getTable('Question')->saveRow($row->toArray(), $row);
}
}
/**
* Create quesiton data
* @param array $data
* @return boolean;
*/
public function create($data)
{
//get keywords
$keywords = array();
foreach ($data as $key => $value) {
if (false !== strpos($key, 'tag') && !empty($value)) {
$keywords[] = $value;
}
}
$keywords = RFLib_Core::getModel('Keyword')->optimize($keywords);
//set default value
$data['finish'] = 'N';
$data['status'] = 'A';
if ($questionId = $this->_save($data)) {
$this->getTable('Question')->setToken($questionId);
RFLib_Core::getModel('Keyword')->create($data['category_id'],$keywords);
$data['question_id'] = $questionId;
$data['keywords'] = implode(' ', $keywords);
if ($this->_saveContent($data)) {
return $questionId;
}
}
return false;
}
/**
* Save question data to question_content table
* @param array $data
* @return boolean
*/
protected function _saveContent($data)
{
$row = $this->getTable('Questioncontent')->findByQuestionId($data['question_id']);
if (!$row) {
$row = null;
$date = new Zend_Date();
$data['created_at'] = $date->getTimestamp();
$date = null;
}
return $this->getTable('Questioncontent')->saveRow($data, $row);
}
/**
* Save question data
* @param array|Wenda_Model_DbTable_QuestionTable $data
* @return boolean
*/
protected function _save($data)
{
$auth = RFLib_Core::getService('authentication')->getAuth();
if (!$auth->hasIdentity()) {
return false;
}
$user = $auth->getStorage()->read();
$date = new Zend_Date();
if (isset($data['id'])) {
$row = $this->getTable('Question')->findById($data['id']);
} else {
$row = null;
}
if (null === $row) {
$data['user_id'] = $user['id'];
$data['created_at'] = $date->getTimestamp();
$data['expired_at'] = $date->addWeek(1)->getTimestamp();
$data['ip_address'] = ip2long(Zend_Controller_Front::getInstance()->getRequest()->getClientIp());
} else {
$data['updated_at'] = $date->getTimestamp();
}
$date = null;
return $this->getTable('Question')->saveRow($data, $row);
}
}