Inquirer.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace app\models;
  3. use app\models\InquirerAnswer;
  4. use app\models\InquirerLog;
  5. use Yii;
  6. /**
  7. * This is the model class for table "inquirer_question".
  8. *
  9. * @property int $id
  10. * @property string $text
  11. * @property string $active доступна
  12. * @property string $show показывать на главной
  13. * @property string $dt_cr
  14. * @property string $dt_pub
  15. * @property string $dt_end
  16. */
  17. class Inquirer extends \yii\db\ActiveRecord
  18. {
  19. /**
  20. * @var bin
  21. */
  22. protected $log = NULL;
  23. public $resetTime = 60*60;
  24. public $description = '';
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public static function tableName()
  29. {
  30. return 'inquirer_question';
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function rules()
  36. {
  37. return [
  38. [['text'], 'required'],
  39. [['text', 'active', 'show'], 'string'],
  40. [['dt_cr', 'dt_pub', 'dt_end'], 'safe'],
  41. ];
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function attributeLabels()
  47. {
  48. return [
  49. 'id' => 'ID',
  50. 'text' => 'Вопрос',
  51. 'active' => 'Доступна',
  52. 'show' => 'Активный опрос',
  53. 'dt_cr' => 'Дата создания',
  54. 'dt_pub' => 'Дата публикации',
  55. 'dt_end' => 'Дата окончания опроса',
  56. ];
  57. }
  58. public function getAnswers()
  59. {
  60. return InquirerAnswer::findAll(['idq'=>$this->id]);
  61. }
  62. public function getAnswer($idq, $ida)
  63. {
  64. return InquirerAnswer::find()->where(['idq'=>$idq, 'id'=>$ida])->One();;
  65. }
  66. public function getLog()
  67. {
  68. if( !$this->log ){
  69. $this->log = new InquirerLog();
  70. }
  71. return $this->log;
  72. }
  73. public function getPart() //проверка на участие
  74. {
  75. $session = Yii::$app->session;
  76. $ua = \Yii::$app->request->getUserAgent();
  77. $uip = \Yii::$app->request->getRemoteIP().",".\Yii::$app->request->getUserIP();
  78. $hash = md5( $ua.$uip ); //метка пользователя
  79. $do = $session->has('askq_'.$this->id.'_'.$hash);
  80. if( $do ){
  81. $do = ($session->get('askq_'.$this->id.'_'.$hash)+$this->resetTime< time())?false:true;
  82. }
  83. return $do;
  84. }
  85. public function Show($id, $state = -1)
  86. {
  87. if( $state < 0 ){
  88. $this->show = ($this->show == 'Y')?'N':'Y';
  89. }else{
  90. $this->show = $state;
  91. }
  92. return $this->save();
  93. }
  94. public function Active($id, $state = -1)
  95. {
  96. if( $state < 0 ){
  97. $this->active = ($this->active == 'Y')?'N':'Y';
  98. }else{
  99. $this->active = $state;
  100. }
  101. return $this->save();
  102. }
  103. }