InquirerController.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 beforeAction($action)
  13. {
  14. if (in_array($action->id, ['ajax'])) {
  15. $this->enableCsrfValidation = false;
  16. }
  17. return parent::beforeAction($action);
  18. }
  19. public function actionView($id){
  20. $model = Inquirer::findOne(['id'=>$id]);
  21. if( ( !$model instanceof Inquirer ) || $model->active == 'N' ){
  22. throw new NotFoundHttpException("Опросник не найден");
  23. }
  24. return $this->render("view",["model"=>$model]);
  25. }
  26. public function actionWidget($id){
  27. $model = Inquirer::findOne(['id'=>$id]);
  28. if( ( !$model instanceof Inquirer ) || $model->active == 'N' ){
  29. throw new NotFoundHttpException("Опросник не найден");
  30. }
  31. return $this->renderPartial("widget",["model"=>$model]);
  32. }
  33. public function actionAjax(){
  34. $this->enableCsrfValidation = false;
  35. if(\Yii::$app->request->isPost){
  36. if(\Yii::$app->request->post('idq') && \Yii::$app->request->post('ask') ){
  37. $ida = (int) \Yii::$app->request->post('ask'); //ответ
  38. $idq = (int) \Yii::$app->request->post('idq'); //вопрос
  39. $ua = \Yii::$app->request->getUserAgent();
  40. $uip = \Yii::$app->request->getRemoteIP().",".\Yii::$app->request->getUserIP();
  41. $model = Inquirer::findOne(['id'=>$idq]);
  42. if( $model ){
  43. $log=$model->getLog();
  44. $hash = md5( $ua.$uip ); //метка пользователя
  45. $ret = $log->testq($idq, $hash); //голосовал ли этот клиент
  46. $session = Yii::$app->session;
  47. $session->set('askq_'.$idq.'_'.$hash, time());
  48. if( !$ret ){
  49. $log->add($idq, $ida, $hash);
  50. $answer = $model->getAnswer($idq, $ida);
  51. $answer->count++;
  52. $answer->save();
  53. return $this->asJson(['status'=>'ok','ret'=>'14']);
  54. }
  55. return $this->asJson(['status'=>'ok','ret'=>'15']);
  56. }
  57. return $this->asJson(['status'=>'ok','ret'=>'5']);
  58. }
  59. }
  60. return $this->asJson(['status'=>'ok','ret'=>'10']);
  61. }
  62. }