public function newAction() { $response = new ApiResponse(); if ($this->request->isPost()) { $answer = new Answers(); $answer->id = uniqid(); $answer->content = $this->request->getPost('content'); $answer->users_id = $this->request->getPost('users_id'); $answer->questions_id = $this->request->getPost('questions_id'); if ($this->request->hasFiles() == true) { $baseLocation = 'files/'; foreach ($this->request->getUploadedFiles() as $file) { $photos = new Photos(); $unique_filename = $answer->id; $photos->size = $file->getSize(); $photos->original_name = $file->getName(); $photos->file_name = $unique_filename; $photos->extension = $file->getExtension(); $location = $baseLocation . $unique_filename . "." . $file->getExtension(); $photos->public_link = $location; try { if (!$photos->save()) { $response->setResponseError($photos->getMessages()); } else { //Move the file into the application $file->moveTo($location); $answer->photo = $photos->public_link; } } catch (PDOException $e) { $response->setResponseError($e->getMessage()); } } } try { if ($answer->save() == false) { $response->setResponseError($answer->getMessages()); } else { $response->setResponse($answer->id); } } catch (PDOException $e) { $response->setResponseError($e->getMessage()); } } else { $response->setResponseError('Wrong HTTP Method'); } return $response; }
/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionCreate($id = '') { if ($id) { $model = $this->loadModel($id); } else { $model = new Answers(); } // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['Answers'])) { $model->attributes = $_POST['Answers']; if ($model->save()) { $this->redirect(array('view', 'id' => $model->id)); } } $this->render('create', array('model' => $model)); }
public static function getQuestion($userId = null) { if ($userId) { $db = Yii::app()->db; $_question = []; $all = []; $fields = (double) rand() / (double) getrandmax() > 0.5 ? ['answer' => 'nameRu as answer', 'question' => 'nameEn'] : ['answer' => 'nameEn as answer', 'question' => 'nameRu']; $currentAnswer = Answers::model()->current()->find('userId=?', [$userId]); if ($currentAnswer === null) { $_question = $db->createCommand('SELECT id,' . implode(',', $fields) . ' FROM `Dictonary` WHERE id NOT IN (SELECT dictonaryId From Answers WHERE userId = ' . (int) $userId . ') ORDER BY RAND() LIMIT 1;')->queryRow(); if (isset($_question['id'])) { $currentAnswer = new Answers(); $currentAnswer->userId = $userId; $currentAnswer->dictonaryId = $_question['id']; $currentAnswer->isCurrent = true; if ($currentAnswer->save() === false) { var_dump($currentAnswer->getErrors()); } } } else { $_question = $db->createCommand('SELECT id,' . implode(',', $fields) . ' FROM `Dictonary` WHERE id = ' . $currentAnswer->dictonaryId)->queryRow(); } if (count($_question) > 0) { $question = $_question[$fields['question']]; unset($_question[$fields['question']]); } $all = $db->createCommand()->select('id,' . $fields['answer'])->from('Dictonary')->where('id!=:userId', array(':userId' => $_question['id']))->order('RAND()')->limit(3)->queryAll(); $all[] = $_question; shuffle($all); shuffle($all); if (count($_question) > 0 && count($all) > 0) { return ['success' => true, 'question' => $question, 'answers' => $all, 'error' => ErrorAnswers::getCount(Yii::app()->user->id), 'ok' => Answers::getCount($userId, Answers::FLAG_OK)]; } else { return ['success' => true, 'question' => false, 'answers' => false, 'error' => ErrorAnswers::getCount(Yii::app()->user->id), 'ok' => Answers::getCount($userId, Answers::FLAG_OK)]; } } else { return ['success' => false]; } }
/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionCreate() { $model = new Pools(); $answers = new Answers(); if (isset($_POST['Pools'])) { $model->attributes = $_POST['Pools']; if ($model->save()) { if (isset($_POST['Answers_name'])) { foreach ($_POST['Answers_name'] as $answer) { $variant = new Answers(); $variant->name = $answer; $variant->pool_id = $model->id; $variant->save(); } Yii::app()->user->setFlash('success', Yii::t('main', 'Данные успешно сохранены!')); $this->redirect(array('update', 'id' => $model->id)); } } else { Yii::app()->user->setFlash('error', Yii::t('main', 'Ошибка!')); } } $this->render('create', array('answers' => $answers, 'model' => $model)); }
/** * This function imports a LimeSurvey .lsq question XML file * * @param mixed $sFullFilepath The full filepath of the uploaded file * @param mixed $iNewSID The new survey id * @param mixed $newgid The new question group id -the question will always be added after the last question in the group */ function XMLImportQuestion($sFullFilepath, $iNewSID, $newgid) { $clang = Yii::app()->lang; $aLanguagesSupported = array(); // this array will keep all the languages supported for the survey $sBaseLanguage = Survey::model()->findByPk($iNewSID)->language; $aLanguagesSupported[] = $sBaseLanguage; // adds the base language to the list of supported languages $aLanguagesSupported = array_merge($aLanguagesSupported, Survey::model()->findByPk($iNewSID)->additionalLanguages); $xml = simplexml_load_file($sFullFilepath); if ($xml->LimeSurveyDocType != 'Question') { safeDie('This is not a valid LimeSurvey question structure XML file.'); } $iDBVersion = (int) $xml->DBVersion; $aQIDReplacements = array(); $aSQIDReplacements = array(0 => 0); $results['defaultvalues'] = 0; $results['answers'] = 0; $results['question_attributes'] = 0; $results['subquestions'] = 0; $importlanguages = array(); foreach ($xml->languages->language as $language) { $importlanguages[] = (string) $language; } if (!in_array($sBaseLanguage, $importlanguages)) { $results['fatalerror'] = $clang->gT("The languages of the imported question file must at least include the base language of this survey."); return $results; } // First get an overview of fieldnames - it's not useful for the moment but might be with newer versions /* $fieldnames=array(); foreach ($xml->questions->fields->fieldname as $fieldname ) { $fieldnames[]=(string)$fieldname; };*/ // Import questions table =================================================================================== // We have to run the question table data two times - first to find all main questions // then for subquestions (because we need to determine the new qids for the main questions first) $query = "SELECT MAX(question_order) AS maxqo FROM {{questions}} WHERE sid={$iNewSID} AND gid={$newgid}"; $res = Yii::app()->db->createCommand($query)->query(); $resrow = $res->read(); $newquestionorder = $resrow['maxqo'] + 1; if (is_null($newquestionorder)) { $newquestionorder = 0; } else { $newquestionorder++; } foreach ($xml->questions->rows->row as $row) { $insertdata = array(); foreach ($row as $key => $value) { $insertdata[(string) $key] = (string) $value; } $iOldSID = $insertdata['sid']; $insertdata['sid'] = $iNewSID; $insertdata['gid'] = $newgid; $insertdata['question_order'] = $newquestionorder; $oldqid = $insertdata['qid']; unset($insertdata['qid']); // save the old qid // now translate any links $insertdata['title'] = translateLinks('survey', $iOldSID, $iNewSID, $insertdata['title']); $insertdata['question'] = translateLinks('survey', $iOldSID, $iNewSID, $insertdata['question']); $insertdata['help'] = translateLinks('survey', $iOldSID, $iNewSID, $insertdata['help']); // Insert the new question if (isset($aQIDReplacements[$oldqid])) { $insertdata['qid'] = $aQIDReplacements[$oldqid]; } $ques = new Questions(); if ($insertdata) { XSSFilterArray($insertdata); } foreach ($insertdata as $k => $v) { $ques->{$k} = $v; } $result = $ques->save(); if (!isset($aQIDReplacements[$oldqid])) { $newqid = Yii::app()->db->getCommandBuilder()->getLastInsertID($ques->tableName()); $aQIDReplacements[$oldqid] = $newqid; // add old and new qid to the mapping array } } // Import subquestions -------------------------------------------------------------- if (isset($xml->subquestions)) { foreach ($xml->subquestions->rows->row as $row) { $insertdata = array(); foreach ($row as $key => $value) { $insertdata[(string) $key] = (string) $value; } $insertdata['sid'] = $iNewSID; $insertdata['gid'] = $newgid; $oldsqid = (int) $insertdata['qid']; unset($insertdata['qid']); // save the old qid $insertdata['parent_qid'] = $aQIDReplacements[(int) $insertdata['parent_qid']]; // remap the parent_qid // now translate any links $insertdata['question'] = translateLinks('survey', $iOldSID, $iNewSID, $insertdata['question']); if (isset($insertdata['help'])) { $insertdata['help'] = translateLinks('survey', $iOldSID, $iNewSID, $insertdata['help']); } if (isset($aQIDReplacements[$oldsqid])) { $insertdata['qid'] = $aQIDReplacements[$oldsqid]; } if ($insertdata) { XSSFilterArray($insertdata); } $ques = new Questions(); foreach ($insertdata as $k => $v) { $ques->{$k} = $v; } $result = $ques->save(); $newsqid = Yii::app()->db->getCommandBuilder()->getLastInsertID($ques->tableName()); if (!isset($insertdata['qid'])) { $aQIDReplacements[$oldsqid] = $newsqid; // add old and new qid to the mapping array } $results['subquestions']++; } } // Import answers -------------------------------------------------------------- if (isset($xml->answers)) { foreach ($xml->answers->rows->row as $row) { $insertdata = array(); foreach ($row as $key => $value) { $insertdata[(string) $key] = (string) $value; } $insertdata['qid'] = $aQIDReplacements[(int) $insertdata['qid']]; // remap the parent_qid // now translate any links $answers = new Answers(); if ($insertdata) { XSSFilterArray($insertdata); } foreach ($insertdata as $k => $v) { $answers->{$k} = $v; } $result = $answers->save(); $results['answers']++; } } // Import questionattributes -------------------------------------------------------------- if (isset($xml->question_attributes)) { $aAllAttributes = questionAttributes(true); foreach ($xml->question_attributes->rows->row as $row) { $insertdata = array(); foreach ($row as $key => $value) { $insertdata[(string) $key] = (string) $value; } unset($insertdata['qaid']); $insertdata['qid'] = $aQIDReplacements[(int) $insertdata['qid']]; // remap the parent_qid if ($iDBVersion < 148 && isset($aAllAttributes[$insertdata['attribute']]['i18n']) && $aAllAttributes[$insertdata['attribute']]['i18n']) { foreach ($importlanguages as $sLanguage) { $insertdata['language'] = $sLanguage; $attributes = new Question_attributes(); if ($insertdata) { XSSFilterArray($insertdata); } foreach ($insertdata as $k => $v) { $attributes->{$k} = $v; } $result = $attributes->save(); } } else { $attributes = new Question_attributes(); if ($insertdata) { XSSFilterArray($insertdata); } foreach ($insertdata as $k => $v) { $attributes->{$k} = $v; } $result = $attributes->save(); } $results['question_attributes']++; } } // Import defaultvalues -------------------------------------------------------------- if (isset($xml->defaultvalues)) { $results['defaultvalues'] = 0; foreach ($xml->defaultvalues->rows->row as $row) { $insertdata = array(); foreach ($row as $key => $value) { $insertdata[(string) $key] = (string) $value; } $insertdata['qid'] = $aQIDReplacements[(int) $insertdata['qid']]; // remap the qid $insertdata['sqid'] = $aSQIDReplacements[(int) $insertdata['sqid']]; // remap the subquestion id // now translate any links $default = new Defaultvalues(); if ($insertdata) { XSSFilterArray($insertdata); } foreach ($insertdata as $k => $v) { $default->{$k} = $v; } $result = $default->save(); $results['defaultvalues']++; } } LimeExpressionManager::SetDirtyFlag(); // so refreshes syntax highlighting $results['newqid'] = $newqid; $results['questions'] = 1; $results['labelsets'] = 0; $results['labels'] = 0; return $results; }
/** * Load editing of answer options specific screen only. * * @access public * @param int $surveyid * @param int $gid * @param int $qid * @return void */ public function _editansweroptions($surveyid, $gid, $qid) { Yii::app()->loadHelper('database'); $surveyid = sanitize_int($surveyid); $qid = sanitize_int($qid); $gid = sanitize_int($gid); // Get languages select on survey. $anslangs = Survey::model()->findByPk($surveyid)->additionalLanguages; $baselang = Survey::model()->findByPk($surveyid)->language; $qrow = Questions::model()->findByAttributes(array('qid' => $qid, 'language' => $baselang)); $qtype = $qrow['type']; $qtypes = getQuestionTypeList('', 'array'); $scalecount = $qtypes[$qtype]['answerscales']; $clang = $this->getController()->lang; // Check if there is at least one answer for ($i = 0; $i < $scalecount; $i++) { $ans = new CDbCriteria(); $ans->addCondition("qid={$qid}")->addCondition("scale_id={$i}")->addCondition("language='{$baselang}'"); $qresult = Answers::model()->count($ans); if ((int) $qresult == 0) { $oAnswer = new Answers(); $oAnswer->qid = $qid; $oAnswer->code = 'A1'; $oAnswer->answer = $clang->gT('Some example answer option'); $oAnswer->language = $baselang; $oAnswer->sortorder = 0; $oAnswer->scale_id = $i; $oAnswer->save(); } } // Check that there are answers for every language supported by the survey for ($i = 0; $i < $scalecount; $i++) { foreach ($anslangs as $language) { $ans = new CDbCriteria(); $ans->addCondition("qid={$qid}")->addCondition("scale_id={$i}")->addCondition("language='{$language}'"); $iAnswerCount = Answers::model()->count($ans); // Means that no record for the language exists in the answers table if (empty($iAnswerCount)) { foreach (Answers::model()->findAllByAttributes(array('qid' => $qid, 'scale_id' => $i, 'language' => $baselang)) as $answer) { $oAnswer = new Answers(); } $oAnswer->qid = $answer->qid; $oAnswer->code = $answer->code; $oAnswer->answer = $answer->answer; $oAnswer->language = $language; $oAnswer->sortorder = $answer->sortorder; $oAnswer->scale_id = $i; $oAnswer->assessment_value = $answer->assessment_value; $oAnswer->save(); } } } // Makes an array with ALL the languages supported by the survey -> $anslangs array_unshift($anslangs, $baselang); // Delete the answers in languages not supported by the survey $criteria = new CDbCriteria(); $criteria->addColumnCondition(array('qid' => $qid)); $criteria->addNotInCondition('language', $anslangs); $languageresult = Answers::model()->deleteAll($criteria); if (!isset($_POST['ansaction'])) { // Check if any nulls exist. If they do, redo the sortorders $ans = new CDbCriteria(); $ans->addCondition("qid={$qid}")->addCondition("scale_id={$i}")->addCondition("language='{$baselang}'"); $cacount = Answers::model()->count($ans); if (!empty($cacount)) { Answers::model()->updateSortOrder($qid, Survey::model()->findByPk($surveyid)->language); } } Yii::app()->loadHelper('admin/htmleditor'); $row = Answers::model()->findByAttributes(array('qid' => $qid, 'language' => Survey::model()->findByPk($surveyid)->language), array('order' => 'sortorder desc')); if (!is_null($row)) { $maxsortorder = $row->sortorder + 1; } else { $maxsortorder = 1; } $aData['surveyid'] = $surveyid; $aData['gid'] = $gid; $aData['qid'] = $qid; $aData['anslangs'] = $anslangs; $aData['scalecount'] = $scalecount; // The following line decides if the assessment input fields are visible or not $sumresult1 = Survey::model()->with(array('languagesettings' => array('condition' => 'surveyls_language=language')))->together()->findByAttributes(array('sid' => $surveyid)); if (is_null($sumresult1)) { $this->getController()->error('Invalid survey ID'); } $surveyinfo = $sumresult1->attributes; $surveyinfo = array_merge($surveyinfo, $sumresult1->languagesettings[0]->attributes); $surveyinfo = array_map('flattenText', $surveyinfo); $assessmentvisible = $surveyinfo['assessments'] == 'Y' && $qtypes[$qtype]['assessable'] == 1; $aData['assessmentvisible'] = $assessmentvisible; $aViewUrls['answerOptions_view'][] = $aData; return $aViewUrls; }
public function uploadAction($uuid = null) { $this->view->disable(); if ($this->request->isPost()) { $uuid = $this->request->getPost('uuid', 'string'); $upload = $this->getDi()->getUpload($options = array('uuid' => $uuid)); // handle quesiton uploads if (isset($upload->response['files'][0]->url)) { if ($this->request->getPost('question')) { // handle question uploads $ques = $this->request->getPost('question', 'int'); $value = $upload->response['files'][0]; $check = Answers::findFirst(array('uuid = ?1 AND question = ?2 AND type = ?3', 'bind' => array(1 => $uuid, 2 => $ques, 3 => 'video'))); if ($check) { // save existing $check->value = json_encode($value); $check->save(); } else { // save new $answer = new Answers(); $answer->assign(array('uuid' => $uuid, 'question' => $ques, 'type' => 'video', 'value' => json_encode($value))); $answer->save(); } } else { // hadnle resume uploads $response = Response::findFirstByUuid($uuid); $response->resume = json_encode($upload->response['files'][0]); $response->save(); } } } }
/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionCreate($id) { // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (!empty($_POST['projid'])) { //$this->mailsend("*****@*****.**","Hi","Test"); foreach ($_FILES as $k => $p) { $model = new Answers(); $vals = explode("_", $k); if (!empty($p)) { $connection = Yii::app()->db; $command = $connection->createCommand("DELETE FROM answers where questionid='{$vals['1']}' and sectionid='{$vals['2']}' and projectid='{$vals['3']}'"); $command->execute(); $mostview = $command->query(); $pnames = ""; foreach ($p['name'] as $nk => $nm) { $pname = md5(rand(1000, 9999999)) . time() . "." . pathinfo($nm, PATHINFO_EXTENSION); $pnames = $pnames . "," . $pname; $file = Yii::app()->basePath . '/../uploads/images/' . $pname; move_uploaded_file($p['tmp_name'][$nk], $file); } $pnames = ltrim($pnames); $model->questionid = $vals[1]; $model->sectionid = $vals[2]; $model->projectid = $vals[3]; $model->Answer = $pnames; $model->userid = Yii::app()->user->id; $model->save(); } } foreach ($_POST as $k => $p) { if (!empty($p)) { $model = new Answers(); if (is_array($p)) { $p = implode(",", $p); } $vals = explode("_", $k); $connection = Yii::app()->db; $command = $connection->createCommand("DELETE FROM answers where questionid='{$vals['1']}' and sectionid='{$vals['2']}' and projectid='{$vals['3']}'"); $command->execute(); $mostview = $command->query(); $model->questionid = $vals[1]; $model->sectionid = $vals[2]; $model->projectid = $vals[3]; $model->Answer = $p; $model->userid = Yii::app()->user->id; $model->save(); } } if ($_POST['projid'] == 0) { ob_start(); $datax = $this->renderPartial('eview', array('pid' => $vals[3]), true); ob_end_clean(); $this->mailsend("*****@*****.**", "Hi", $datax); $this->redirect(array('project/index')); } else { $this->render('create', array('pid' => $_POST['projid'])); } } else { $this->render('create', array('pid' => $id)); } }
public function addanswer($post) { $role = Yii::app()->user->role; $userid = Yii::app()->user->getId(); $question_id = $post['question_id']; $a = new Answers(); $a->question_id = $question_id; $a->answer = $post['answer']; $a->owner_id = $userid; $a->owner_user_type = $role; if ($a->save()) { $status = true; $answer_id = Yii::app()->db->getLastInsertId(); $this->SendMailAfterAnswer($question_id, $answer_id); } else { $status = false; $return['message'] = $q->getErrors(); } $question = Questions::model()->findByPk($question_id); $return['status'] = $status; $return['answercount'] = $question->answerCount; $this->renderJSON($return, $status); }