Пример #1
0
 public function create()
 {
     $question = new Question();
     $question->setAttributes(['subject' => $this->subject, 'content' => $this->content, 'author_id' => $this->author_id]);
     if (($result = $question->save()) && ($result = $question->setActive())) {
         $tags = Tag::findAll(['name' => is_array($this->tags) ? $this->tags : explode(',', $this->tags)]);
         foreach ($tags as $tag) {
             $tagItem = new TagItem();
             $tagItem->setAttributes(['target_id' => $question->id, 'target_type' => $question::TYPE]);
             $tag->addItem($tagItem);
         }
     }
     return $result ? $question : false;
 }
Пример #2
0
 /**
  * 收藏问题
  * @param User $user
  * @param Question $question
  */
 public static function question(User $user, Question $model)
 {
     $data = ['target_id' => $model->id, 'target_type' => $model::TYPE, 'author_id' => $user->id, 'status' => static::STATUS_ACTIVE];
     if (!static::deleteAll($data + ['type' => static::TYPE])) {
         // 删除数据有行数则代表有数据,无行数则添加数据
         $favorite = new static();
         $favorite->setAttributes($data);
         $result = $favorite->save();
         if ($result) {
             $model->updateCounters(['favorite_count' => 1]);
         }
         return [$result, $favorite];
     }
     $model->updateCounters(['favorite_count' => -1]);
     return [true, null];
 }
Пример #3
0
 public function run()
 {
     // Get list category of module question
     $model = \app\modules\category\models\Category::getCategory('question', '- ');
     // Get question quan tam nhieu nhat
     $concernedQuestion = Question::find()->where(['status' => 1])->andWhere(['stats.comment_count' => ['$gt' => 0]])->orderBy(['stats.comment_count' => -1])->limit(5)->all();
     return $this->render('sidebar', ['model' => $model, 'concernedQuestion' => $concernedQuestion]);
 }
Пример #4
0
 public function actionDoctor($id)
 {
     $query = Question::find();
     $query->orderBy('_id ASC');
     $query->where(['category' => $id, 'need_answer' => 1]);
     $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 10]]);
     return $this->render('doctor', ['model' => $dataProvider]);
 }
Пример #5
0
 /**
  * 通过ID获取指定问题
  */
 protected function findQuestion($id, \Closure $callback = null)
 {
     $query = Question::find();
     $callback !== null && $callback($query);
     if (($model = $query->andWhere(['id' => $id])->one()) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
Пример #6
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Question::find();
     $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['id' => SORT_DESC]]]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'pid' => $this->pid, 'author_id' => $this->author_id, 'view_count' => $this->view_count, 'comment_count' => $this->comment_count, 'favorite_count' => $this->favorite_count, 'like_count' => $this->like_count, 'hate_count' => $this->hate_count, 'status' => $this->status, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]);
     $query->andFilterWhere(['like', 'subject', $this->subject])->andFilterWhere(['like', 'content', $this->content])->andFilterWhere(['like', 'type', $this->type]);
     return $dataProvider;
 }
Пример #7
0
 public function run()
 {
     switch ($this->type) {
         case '':
             $questions = Question::find()->where(['category' => $this->cat_id])->limit(10)->orderBy('rand()')->all();
             break;
         case 'comment':
             $questions = Question::find()->where(['count_comment' => ['$gt' => 0]])->limit(10)->orderBy('rand()')->all();
             break;
         case 'cat':
             $questions = Category::find()->where(['lft' => ['$gt' => 1], 'module' => 'question'])->limit(4)->orderBy('rand()')->all();
         default:
             break;
     }
     $assign = ['questions' => $questions, 'name' => $this->name];
     return $this->render($this->view, $assign);
 }
Пример #8
0
 /**
  * 获取问题
  */
 public function getQuestion()
 {
     return $this->hasOne(Question::className(), ['id' => 'pid']);
 }
Пример #9
0
 /**
  * 创建新回答
  * @param $question
  * @return Answer
  */
 protected function newAnswer(Question $question)
 {
     $model = new Answer();
     if ($model->load(Yii::$app->request->post())) {
         $model->author_id = Yii::$app->user->id;
         if ($question->addAnswer($model)) {
             return $this->message('回答发表成功!', 'success', $this->refresh(), 'flash');
         }
     }
     return $model;
 }
Пример #10
0
 /**
  * 获取用户发表的问题
  * @return \yii\db\ActiveQuery
  */
 public function getQuestion($id)
 {
     return $this->hasOne(Question::className(), ['author_id' => 'id'])->andWhere(['id' => $id]);
 }