InquirerController.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace app\controllers;
  3. use app\models\Inquirer;
  4. use yii\web\NotFoundHttpException;
  5. use Yii;
  6. class InquirerController extends \yii\web\Controller
  7. {
  8. public function actionIndex()
  9. {
  10. return $this->render('index');
  11. }
  12. public function actionView($id){
  13. $model = Inquirer::findOne(['id'=>$id]);
  14. if( ( !$model instanceof Inquirer ) || $model->active == 'N' ){
  15. throw new NotFoundHttpException("Опросник не найден");
  16. }
  17. return $this->render("view",["model"=>$model]);
  18. }
  19. public function actionWidget($id){
  20. $model = Inquirer::findOne(['id'=>$id]);
  21. if( ( !$model instanceof Inquirer ) || $model->active == 'N' ){
  22. throw new NotFoundHttpException("Опросник не найден");
  23. }
  24. return $this->renderPartial("widget",["model"=>$model]);
  25. }
  26. public function actionAjax(){
  27. if(\Yii::$app->request->isPost){
  28. if(\Yii::$app->request->post('idq') && \Yii::$app->request->post('ask') ){
  29. $ida = (int) \Yii::$app->request->post('ask'); //ответ
  30. $idq = (int) \Yii::$app->request->post('idq'); //вопрос
  31. $ua = \Yii::$app->request->getUserAgent();
  32. $uip = \Yii::$app->request->getRemoteIP().",".\Yii::$app->request->getUserIP();
  33. $model = Inquirer::findOne(['id'=>$idq]);
  34. if( $model ){
  35. $log=$model->getLog();
  36. $hash = md5( $ua.$uip ); //метка пользователя
  37. $ret = $log->testq($idq, $hash); //голосовал ли этот клиент
  38. $session = Yii::$app->session;
  39. $session->set('askq_'.$idq.'_'.$hash, time());
  40. if( !$ret ){
  41. $log->add($idq, $ida, $hash);
  42. $answer = $model->getAnswer($idq, $ida);
  43. $answer->count++;
  44. $answer->save();
  45. return $this->asJson(['status'=>'ok','ret'=>'14']);
  46. }
  47. return $this->asJson(['status'=>'ok','ret'=>'15']);
  48. }
  49. return $this->asJson(['status'=>'ok','ret'=>'5']);
  50. }
  51. }
  52. return $this->asJson(['status'=>'ok','ret'=>'10']);
  53. }
  54. }