本文整理汇总了PHP中Answer::save方法的典型用法代码示例。如果您正苦于以下问题:PHP Answer::save方法的具体用法?PHP Answer::save怎么用?PHP Answer::save使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Answer
的用法示例。
在下文中一共展示了Answer::save方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: postAnswer
/**
* Store a newly created post in storage.
*
* @return Response
*/
public function postAnswer($question_id)
{
$validate = Validator::make(Input::all(), Answer::$rules);
if ($validate->passes()) {
//get file from input
$audio = Input::file('audio');
//get file's temporary path in server
$file_temporary_path = $audio->getPathname();
//create MP3 Object
$audio_file = new MP3($file_temporary_path);
$duration = $audio_file->getDuration();
#Do same thing in 1 line:
#$duration = with(new MP3($audio->getPathname()))->getDuration();
//check if audio is less than/equal to 120 Seconds, then save it!
if ($duration <= 120) {
//seconds
$name = time() . '-' . $audio->getClientOriginalName();
//Move file from temporary folder to PUBLIC folder.
//PUBLIC folder because we want user have access to this file later.
$avatar = $audio->move(public_path() . '/answers/', $name);
$answer = new Answer();
$answer->title = Input::get('title');
$answer->info = Input::get('info');
$answer->audio = $name;
if (Auth::check()) {
$answer->user_id = Auth::id();
} else {
$answer->user_id = 0;
}
$answer->save();
}
return Redirect::action('AnswerController@index');
}
return Redirect::back()->withErrors($validate)->withInput();
}
示例2: actionView
/**
* Controller action for viewing a questions.
* Also provides functionality for creating an answer,
* adding a comment and voting.
*/
public function actionView()
{
error_reporting(E_ALL);
ini_set("display_errors", 1);
$question = Question::model()->findByPk(Yii::app()->request->getParam('id'));
if (isset($_POST['Answer'])) {
$answerModel = new Answer();
$answerModel->attributes = $_POST['Answer'];
$answerModel->created_by = Yii::app()->user->id;
$answerModel->post_type = "answer";
$answerModel->question_id = $question->id;
if ($answerModel->validate()) {
$answerModel->save();
$this->redirect($this->createUrl('//questionanswer/main/view', array('id' => $question->id)));
}
}
if (isset($_POST['Comment'])) {
$commentModel = new Comment();
$commentModel->attributes = $_POST['Comment'];
$commentModel->created_by = Yii::app()->user->id;
$commentModel->post_type = "comment";
$commentModel->question_id = $question->id;
if ($commentModel->validate()) {
$commentModel->save();
$this->redirect($this->createUrl('//questionanswer/main/view', array('id' => $question->id)));
}
}
// User has just voted on a question
if (isset($_POST['QuestionVotes'])) {
$questionVotesModel = new QuestionVotes();
$questionVotesModel->attributes = $_POST['QuestionVotes'];
QuestionVotes::model()->castVote($questionVotesModel, $question->id);
}
$this->render('view', array('author' => $question->user->id, 'question' => $question, 'answers' => Answer::model()->overview($question->id), 'related' => Question::model()->related($question->id)));
}
示例3: actionCheckAnswer
/**
* 验证回答
*/
public function actionCheckAnswer()
{
//登入判断
$this->checkAuth();
$model = new AnswerForm();
if (isset($_POST['ajax']) && $_POST['ajax'] === 'answer-form') {
echo CActiveForm::validate($model);
Yii::app()->end();
}
//var_dump($_POST['AnswerForm']);
//启用事物处理 因为需要插入 Answer表及Question字段中的answer_count 字段
$transaction = Yii::app()->db->beginTransaction();
try {
$answer_model = new Answer();
$answer_model->answer_content = $_POST['AnswerForm']['answer_content'];
$answer_model->question_id = $_POST['AnswerForm']['question_id'];
$answer_model->uid = Yii::app()->user->id;
$answer_model->add_time = time();
$answer_model->ip = Yii::app()->request->userHostAddress;
if (!$answer_model->save()) {
throw new ErrorException('回答失败1');
}
//更改question表中的answer_count 信息
if (!Question::model()->updateByPk($answer_model->question_id, array('answer_count' => new CDbExpression('answer_count+1')))) {
throw new ErrorException('回答失败2');
}
$transaction->commit();
$this->redirect(Yii::app()->request->urlReferrer);
//$this->success('回答成功');
} catch (Exception $e) {
$transaction->rollBack();
//exit($e->getMessage());
$this->error($e->getMessage());
}
}
示例4: actionCreate
/**
* Creates a new model.
* If creation is successful, the browser will be redirected to the 'view' page.
*/
public function actionCreate()
{
$answer = new Answer();
if (isset($_POST['Answer'])) {
$this->forcePostRequest();
$_POST = Yii::app()->input->stripClean($_POST);
$answer->attributes = $_POST['Answer'];
$answer->content->populateByForm();
$answer->post_type = "answer";
if ($answer->validate()) {
$answer->save();
$this->redirect(array('//questionanswer/question/view', 'id' => $answer->question_id));
}
}
/*$model=new Answer;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Answer']))
{
$model->attributes=$_POST['Answer'];
$model->created_by = Yii::app()->user->id;
$model->post_type = "answer";
if($model->save())
$this->redirect(array('//questionanswer/question/view','id'=>$model->question_id));
}
$this->render('create',array(
'model'=>$model,
));*/
}
示例5: actionCreate
/**
* Creates a new model.
* If creation is successful, the browser will be redirected to the 'view' page.
*/
public function actionCreate()
{
$model = new Answer();
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if (isset($_POST['Answer'])) {
$model->attributes = $_POST['Answer'];
if ($model->save()) {
$this->redirect(array('view', 'id' => $model->id));
}
}
$this->render('create', array('model' => $model));
}
示例6: postGuardar
public function postGuardar()
{
$bandera = false;
$id = Input::get('idQues');
$ans = Input::get('res');
$answers = Answer::all();
$question = Question::find($id);
$cantidad = Question::count();
foreach ($answers as $a) {
if ($a->id_question == $id) {
$bandera = true;
$answer = $a;
break;
}
}
if (!$bandera) {
$answer = new Answer();
$answer->id_question = $id;
$ids = Auth::id();
$student = Student::where('id_user', '=', $ids)->get()->first();
$answer->id_student = $student->id;
$answer->answer = $ans;
$answer->result = QuestionsController::verifyAnswer($question, $ans);
$answer->save();
} else {
$answer->result = QuestionsController::verifyAnswer($question, $ans);
$answer->answer = $ans;
$answer->save();
}
if ($cantidad == $id) {
return Redirect::to('questions/exam?endTest=yes');
//"holi ".$id." ".$ans;
} else {
if ($cantidad > $id) {
return Redirect::to('questions/exam?numberQIn=' . ++$id);
}
}
}
示例7: answerQuestion
public function answerQuestion()
{
$validator = Validator::make(Input::all(), Answer::$rules);
$question_id = Input::get('question_id');
if ($validator->fails()) {
return Redirect::route('single-question', $question_id)->withErrors($validator)->withInput();
} else {
$insertAnswer = new Answer();
$insertAnswer->user_id = Auth::user()->user_id;
$insertAnswer->question_id = $question_id;
$insertAnswer->answer = Input::get('answer');
$insertAnswer->save();
$insertPoint = User::find(Auth::user()->user_id);
$insertPoint->points = $insertPoint->points + 3;
$insertPoint->save();
return Redirect::route('single-question', $question_id)->with('global', 'Answer is successfully posted');
}
}
示例8: actionAnswer
public function actionAnswer($id)
{
// get chosen variant IDs
$chosenAnswers = array_values($_POST);
// add answers for all answer variants for the given poll
$poll = Poll::findById($id);
foreach ($poll->questions as $question) {
foreach ($question->variants as $variant) {
$answer = new Answer();
$answer->variant = $variant;
$answer->answer = in_array($variant->id, $chosenAnswers);
$answer->save(false);
// but do not commit
}
}
Model::flush();
// commit
$this->redirect($this->url('poll.stat', array('id' => $id)));
}
示例9: actionView
public function actionView($qid)
{
Yii::app()->user->setReturnUrl(Yii::app()->request->urlReferrer);
if (Yii::app()->user->isGuest) {
Yii::app()->user->loginRequired();
} else {
$questionInfo = Question::model()->with('answers', 'user', 'tags')->findByPk($qid, array('order' => 'answers.likeCount DESC, answers.time ASC'));
$bestAns = Answer::model()->findByPk($questionInfo->bestAnsId);
$criteria = new CDbCriteria();
$criteria->addCondition("userId=" . Yii::app()->user->id);
$criteria->addCondition("questionId=" . $qid);
if (LikeQue::model()->find($criteria) != NULL) {
$ifLike = 1;
} else {
$ifLike = 0;
}
$myLike = LikeAns::model()->findAll('userId=:uid', array(':uid' => Yii::app()->user->id));
$answerModel = new Answer();
$data = array('question' => $questionInfo, 'bestAns' => $bestAns, 'answerModel' => $answerModel, 'ifLike' => $ifLike, 'myLike' => $myLike);
if (isset($_POST['Answer'])) {
$success = '回答成功~';
$answerModel->attributes = $_POST['Answer'];
$answerModel->questionId = $qid;
$answerModel->userId = Yii::app()->user->id;
$answerModel->time = date("Y-m-d H:i:s");
$questionInfo->answerCount++;
$answerModel->save();
$questionInfo->save();
if ($questionInfo->userId != Yii::app()->user->id) {
$userInfo = User::model()->findByPk(Yii::app()->user->id);
$userInfo->score += 2;
$userInfo->lv = $userInfo->getLevel();
$userInfo->save();
$success = $success . '积分+2~';
}
Yii::app()->user->setFlash('success', $success);
$this->redirect(array('view', 'qid' => $qid));
}
$this->render('view', $data);
}
}
示例10: actionReply
public function actionReply($feedbackid)
{
if (Yii::app()->user->checkAccess('createAnswer') == false) {
throw new CHttpException(403);
}
$feedback = Feedback::model()->findByPk($feedbackid);
$answer = new Answer();
if (isset($_POST['Answer'])) {
$answer->attributes = Yii::app()->request->getPost('Answer');
$answer->reply_time = date('Y-m-d');
$answer->feedback_id = $feedbackid;
if ($answer->save()) {
$this->setFlashMessage(strtr('<strong>{link}</strong> 问题咨询回复成功', array('{link}' => CHtml::link(CHtml::encode($feedback->content), array('view', 'id' => $answer->primaryKey)))));
$feedback->is_reply = 1;
$feedback->save();
$this->redirect(array('answer/index'));
}
}
$this->breadcrumbs = array('问题咨询' => array('feedback/index'), '回复');
$this->render('reply', array('answer' => $answer, 'feedback' => $feedback));
}
示例11: _save
protected function _save($data)
{
$obj = new Answer();
$condition = array('user_id' => $this->_user->id, 'topic_id' => $data->topic_id);
$obj->where($condition)->get();
if (isset($data->selected) and $data->selected) {
foreach ($data->selected as $tid => $choose) {
$newData = new StdClass();
$newData->topic_id = $tid;
$newData->aChoose = $choose;
$this->_save($newData);
}
}
$obj->aChoose = implode(',', $data->aChoose);
$obj->topic_id = $data->topic_id;
$obj->user_id = $this->_user->id;
if ($obj->save()) {
return $obj->to_array();
} else {
return array('error' => $obj->error->string);
}
}
示例12: store
/**
* Store a newly created post in storage.
*
* @return Response
*/
public function store()
{
$validate = Validator::make(Input::all(), Answer::$rules);
if ($validate->passes()) {
$audio = Input::file('audio');
$name = time() . "-" . $audio->getClientOriginalName();
$avatar = $audio->move("./answers/", $name);
$audio = Input::file('audio');
$name = time() . "-" . $audio->getClientOriginalName();
$avatar = $audio->move("./answers/", $name);
$answer = new Answer();
$answer->title = Input::get('title');
$answer->info = Input::get('info');
$answer->audio = $name;
if (Auth::check()) {
$answer->user_id = Auth::id();
} else {
$answer->user_id = 0;
}
$answer->save();
return Redirect::route('answer.admin..index');
}
return Redirect::back()->withErrors($validate)->withInput();
}
示例13: actionAnswer
public function actionAnswer()
{
$answer = new Answer();
if (isset($_POST['Answer'])) {
$answer->attributes = $_POST['Answer'];
$answer->id_account = Yii::app()->user->getId();
if ($answer->save()) {
$question = $answer->question;
$lesson = $question->lesson;
$criteria = new CDbCriteria();
$criteria->addCondition(array('id_lesson' => $lesson->getPrimaryKey()));
$criteria->order = 'id DESC';
$count = Question::model()->count($criteria);
$questionPages = new CPagination($count);
// results per page
$questionPages->pageSize = Yii::app()->params['nQuestionsInLessonPage'];
$questionPages->applyLimit($criteria);
$questions = Question::model()->findAll($criteria);
$this->renderPartial('/_questions_answers', array('lesson' => $lesson, 'questions' => $questions, 'questionPages' => $questionPages));
} else {
echo '-1';
}
}
}
示例14: processAnswersCreation
/**
* abstract function which creates the form to create / edit the answers of the question
* @param FormValidator $form
*/
function processAnswersCreation($form)
{
$questionWeighting = $nbrGoodAnswers = 0;
$objAnswer = new Answer($this->id);
$nb_answers = $form->getSubmitValue('nb_answers');
// Score total
$answer_score = trim($form->getSubmitValue('weighting[1]'));
// Reponses correctes
$nbr_corrects = 0;
for ($i = 1; $i <= $nb_answers; $i++) {
$goodAnswer = trim($form->getSubmitValue('correct[' . $i . ']'));
if ($goodAnswer) {
$nbr_corrects++;
}
}
// Set question weighting (score total)
$questionWeighting = $answer_score;
// Set score per answer
$nbr_corrects = $nbr_corrects == 0 ? 1 : $nbr_corrects;
$answer_score = $nbr_corrects == 0 ? 0 : $answer_score;
$answer_score = $answer_score / $nbr_corrects;
//$answer_score �quivaut � la valeur d'une bonne r�ponse
// cr�ation variable pour r�cuperer la valeur de la coche pour la prise en compte des n�gatifs
$test = $form->getSubmitValue('pts');
for ($i = 1; $i <= $nb_answers; $i++) {
$answer = trim($form->getSubmitValue('answer[' . $i . ']'));
$comment = trim($form->getSubmitValue('comment[' . $i . ']'));
$goodAnswer = trim($form->getSubmitValue('correct[' . $i . ']'));
if ($goodAnswer) {
$weighting = abs($answer_score);
} else {
if ($test == 1) {
$weighting = 0;
} else {
$weighting = -abs($answer_score);
}
}
$objAnswer->createAnswer($answer, $goodAnswer, $comment, $weighting, $i);
}
// saves the answers into the data base
$objAnswer->save();
// sets the total weighting of the question --> sert � donner le score total pendant l'examen
$this->updateWeighting($questionWeighting);
$this->save();
}
示例15: processAnswersCreation
/**
* Function which creates the form to create/edit the answers of the question
* @param FormValidator $form
*/
public function processAnswersCreation($form)
{
$answer = $form->getSubmitValue('answer');
// Due the ckeditor transform the elements to their HTML value
//$answer = api_html_entity_decode($answer, ENT_QUOTES, $charset);
//$answer = htmlentities(api_utf8_encode($answer));
// remove the :: eventually written by the user
$answer = str_replace('::', '', $answer);
// remove starting and ending space and
$answer = api_preg_replace("/ /", " ", $answer);
// start and end separator
$blankStartSeparator = self::getStartSeparator($form->getSubmitValue('select_separator'));
$blankEndSeparator = self::getEndSeparator($form->getSubmitValue('select_separator'));
$blankStartSeparatorRegexp = self::escapeForRegexp($blankStartSeparator);
$blankEndSeparatorRegexp = self::escapeForRegexp($blankEndSeparator);
// remove spaces at the beginning and the end of text in square brackets
$answer = preg_replace_callback("/" . $blankStartSeparatorRegexp . "[^]]+" . $blankEndSeparatorRegexp . "/", function ($matches) use($blankStartSeparator, $blankEndSeparator) {
$matchingResult = $matches[0];
$matchingResult = trim($matchingResult, $blankStartSeparator);
$matchingResult = trim($matchingResult, $blankEndSeparator);
$matchingResult = trim($matchingResult);
// remove forbidden chars
$matchingResult = str_replace("/\\/", "", $matchingResult);
$matchingResult = str_replace('/"/', "", $matchingResult);
return $blankStartSeparator . $matchingResult . $blankEndSeparator;
}, $answer);
// get the blanks weightings
$nb = preg_match_all('/' . $blankStartSeparatorRegexp . '[^' . $blankStartSeparatorRegexp . ']*' . $blankEndSeparatorRegexp . '/', $answer, $blanks);
if (isset($_GET['editQuestion'])) {
$this->weighting = 0;
}
/* if we have some [tobefound] in the text
build the string to save the following in the answers table
<p>I use a [computer] and a [pen].</p>
becomes
<p>I use a [computer] and a [pen].</p>::100,50:100,50@1
++++++++-------**
--- -- --- -- -
A B (C) (D)(E)
+++++++ : required, weighting of each words
------- : optional, input width to display, 200 if not present
** : equal @1 if "Allow answers order switches" has been checked, @ otherwise
A : weighting for the word [computer]
B : weighting for the word [pen]
C : input width for the word [computer]
D : input width for the word [pen]
E : equal @1 if "Allow answers order switches" has been checked, @ otherwise
*/
if ($nb > 0) {
$answer .= '::';
// weighting
for ($i = 0; $i < $nb; ++$i) {
// enter the weighting of word $i
$answer .= $form->getSubmitValue('weighting[' . $i . ']');
// not the last word, add ","
if ($i != $nb - 1) {
$answer .= ",";
}
// calculate the global weighting for the question
$this->weighting += $form->getSubmitValue('weighting[' . $i . ']');
}
// input width
$answer .= ":";
for ($i = 0; $i < $nb; ++$i) {
// enter the width of input for word $i
$answer .= $form->getSubmitValue('sizeofinput[' . $i . ']');
// not the last word, add ","
if ($i != $nb - 1) {
$answer .= ",";
}
}
}
// write the blank separator code number
// see function getAllowedSeparator
/*
0 [...]
1 {...}
2 (...)
3 *...*
4 #...#
5 %...%
6 $...$
*/
$answer .= ":" . $form->getSubmitValue('select_separator');
// Allow answers order switches
$is_multiple = $form->getSubmitValue('multiple_answer');
$answer .= '@' . $is_multiple;
$this->save();
$objAnswer = new Answer($this->id);
$objAnswer->createAnswer($answer, 0, '', 0, 1);
$objAnswer->save();
}