public function actionSave($id) { // Получаем куки для опроса /* define('COOKIE_NAME', 'poll_genplanmos_ru'); $isCookie = isset(Yii::app()->request->cookies[COOKIE_NAME]->value); $pollCookie = $isCookie ? Yii::app()->request->cookies[COOKIE_NAME] : new CHttpCookie(COOKIE_NAME, ''); $pollCookieValue = $isCookie? CJSON::decode($pollCookie->value) : array(); * */ // Авторизован ли пользователь if (Yii::app()->user->isAuthenticated() === false) { $this->redirect(Yii::app()->user->loginUrl); } if (($user = Yii::app()->user->getProfile()) === null) { Yii::app()->user->setFlash(yupe\widgets\YFlashMessages::ERROR_MESSAGE, Yii::t('UserModule.user', 'User not found.')); Yii::app()->user->logout(); $this->redirect((array) '/user/account/login'); } $poll = Poll::model()->active()->with(array('questions' => array('scopes' => 'active')))->findByPK((int) $id); if (!$poll) { throw new CHttpException(404, Yii::t('PollModule.poll', 'Страница не найдена')); } // Поиск результата $result = Result::model()->find('t.user_id = :user_id AND t.poll_id = :poll_id', array(':user_id' => $user->id, ':poll_id' => $poll->id)); if ($result !== null) { throw new CHttpException(403, Yii::t('PollModule.poll', 'Вы уже проходили данный опрос')); } $result = new Result(); $result->user_id = $user->id; $result->poll_id = $poll->id; // Обработка результата if (($data = Yii::app()->getRequest()->getPost('question')) !== null) { // Формирование списка ответов $answers = array(); foreach ($poll->questions as $question) { $formValue = isset($data[$question->id]) ? $data[$question->id] : null; switch ($question->type) { case Question::TYPE_VARIANT: $answer = new Answer(); $answer->question_id = $question->id; $answer->variant_id = $formValue !== null ? (int) $formValue : $formValue; $answers[] = $answer; break; case Question::TYPE_MULTIPLE: // Просматриваем полученные ответы if (!isset($formValue) || !is_array($formValue)) { break; } foreach ($formValue as $value) { $answer = new Answer(); $answer->question_id = $question->id; $answer->variant_id = (int) $value; $answers[] = $answer; } break; default: $answer = new Answer(); $answer->question_id = $question->id; $answer->value = $formValue !== null ? $formValue : ''; $answers[] = $answer; break; } $result->answers = $answers; } if ($result->validate()) { $result->save(); Yii::app()->ajax->success(array('html' => $this->widget('application.modules.poll.widgets.PollWidget', array('model_id' => $poll->id), true))); } else { Yii::app()->ajax->failure(array('message' => '', 'errors' => $result->getErrors())); } } }
/** * Generated from @assert ('0-1*') === false. * * @covers pgn\tags\Result::validate */ public function testValidate16() { $this->assertSame(false, $this->object->validate('0-1*')); }
public function importExcelTable() { if (isset($_FILES['Excel'])) { $model = new Result(); $model->attributes = $_FILES['Excel']; $model->excel = CUploadedFile::getInstanceByName('Excel'); if ($model->validate()) { $post = $_FILES['Excel']; //$post=(string)$post; $dossier = Yii::app()->params['dfs'] . "/temp/"; $dir = "temp/"; $rnd = rand(0, 99999); $fichier = "{$rnd}-"; if (move_uploaded_file($post['tmp_name'], $dossier . preg_replace("/[^a-zA-Z0-9\\/_|.-]/", "_", $fichier))) { return $dossier . preg_replace("/[^a-zA-Z0-9\\/_|.-]/", "_", $fichier); } else { return $dossier . $post['tmp_name']; } } else { throw new CHttpException(403, "File upload failure "); } } else { throw new CHttpException(403, "File upload failure"); } }