public function execute() { global $wgUser; if (!$wgUser->isAllowed('surveysubmit') || $wgUser->isBlocked()) { $this->dieUsageMsg(array('badaccess-groups')); } $params = $this->extractRequestParams(); if (!(isset($params['id']) xor isset($params['name']))) { $this->dieUsage(wfMsg('survey-err-id-xor-name'), 'id-xor-name'); } if (isset($params['name'])) { $survey = Survey::newFromName($params['name'], null, false); if ($survey === false) { $this->dieUsage(wfMsgExt('survey-err-survey-name-unknown', 'parsemag', $params['name']), 'survey-name-unknown'); } } else { $survey = Survey::newFromId($params['id'], null, false); if ($survey === false) { $this->dieUsage(wfMsgExt('survey-err-survey-id-unknown', 'parsemag', $params['id']), 'survey-id-unknown'); } } $submission = new SurveySubmission(array('survey_id' => $survey->getId(), 'page_id' => 0, 'user_name' => $GLOBALS['wgUser']->getName(), 'time' => wfTimestampNow())); foreach (FormatJson::decode($params['answers']) as $answer) { $submission->addAnswer(SurveyAnswer::newFromArray((array) $answer)); } $submission->writeToDB(); }
/** * (non-PHPdoc) * @see includes/api/ApiBase#getParamDescription() */ public function getParamDescription() { $descs = array('props' => 'Survey data to query', 'continue' => 'Offset number from where to continue the query', 'limit' => 'Max amount of words to return'); return array_merge(SurveyAnswer::getFieldDescriptions(), $descs); }
public function Answers($memberID = 0) { $memberID = DB::Safe($memberID); $q = "SELECT ID FROM SurveyAnswers WHERE QuestionID='{$this->ID}'"; if ($memberID > 0) { $q .= " AND MemberID='{$memberID}'"; } $r = DB::Run($q); $ans = array(); while ($answerID = mysql_fetch_array($r)) { array_push($ans, SurveyAnswer::Load($answerID['ID'])); } // When getting a specific members' answers, only return the // single answer if there is just one. Much easier to work with. if (mysql_num_rows($r) == 1 && $memberID > 0) { return $ans[0]; } elseif (mysql_num_rows($r) == 0 && $memberID > 0) { return null; } else { return $ans; } }
public function answers($reload = false) { if ($reload or !$this->answers_cache) { $id = mysql_real_escape_string($this->id); $this->answers_cache = SurveyAnswer::find_all("survey_questions.id = '{$id}'"); } return $this->answers_cache; }
/** * @param string $from * @param SurveyQuestion $question * @param string $answerString */ private function persistAnswer($from, SurveyQuestion $question, $answerString) { $answer = new SurveyAnswer(); $answer->setAnswer($answerString); $answer->setAnsweredBy($from); //Persist the answer $this->surveyManager->addAnswer($question->getId(), $answer); }
public static function load_from_row($row, $recurse_limit = 1, $current_level = 0) { // When PHP 5.3.0 is in, we can shift this over too and just redefine it // if needed (eg. loading child objects) $class = __CLASS__; $object = new $class(); $fields = self::fields_array(); $fields[] = array("created_at", "datetime"); $fields[] = array("updated_at", "datetime"); $fields[] = array("deleted", "bool"); foreach ($fields as $field) { $property = $field[0]; if (isset($field[2])) { $property = $field[2]; } if (isset($row[self::table . ".{$field[0]}"])) { if ($field[1] == "datetime") { $object->{$property} = strtotime($row[self::table . ".{$field[0]}"]); } else { $object->{$property} = $row[self::table . ".{$field[0]}"]; } } else { $object->{$property} = null; } } $object->is_new = false; // Load child objects here if ($current_level < $recurse_limit) { $current_level++; $object->survey = Survey::load_from_row($row, $recurse_limit, $current_level); $object->user = User::load_from_row($row, $recurse_limit, $current_level); $object->question = SurveyQuestion::load_from_row($row, $recurse_limit, $current_level); $object->response = SurveyResponse::load_from_row($row, $recurse_limit, $current_level); $object->answer = SurveyAnswer::load_from_row($row, $recurse_limit, $current_level); $object->option = SurveyQuestionOption::load_from_row($row, $recurse_limit, $current_level); } return $object; }
public function testAnswerQuestion() { $survey = new Survey(); $survey->setSurveyName("hi"); $survey->setDescription('stuff'); $q = new SurveyQuestion(); $q->setType(QuestionType::StarRating()); $q->setQuestion("A question"); $survey->addQuestion($q); $this->manager->createSurvey($survey); $ss = $this->manager->getSurvey($survey->getId()); $q = $ss->getQuestions()[0]; $ans = new SurveyAnswer(); $ans->setAnswer("awesome!"); $ans->setAnsweredBy("+12064122496"); $this->manager->addAnswer($q->getId(), $ans); //SurveyEntityManager::testClear(); $answers = $this->manager->getAnswers($survey->getId()); $this->assertNotNull($answers); $this->assertEquals(1, count($answers)); $this->assertEquals($answers->getAnswers($q->getId())[0]->getAnswer(), "awesome!"); $this->assertEquals($answers->getAnswers($q->getId())[0]->getSurveyQuestionId(), $q->getId()); //add another question $q2 = new SurveyQuestion(); $q2->setType(QuestionType::StarRating()); $q2->setQuestion("Second question"); $survey->addQuestion($q2); $this->manager->updateSurvey($survey); $this->assertNotNull($q2->getId(), "Asserting the second question was saved"); //Create another survey and question to make sure we do not // have leaks. $survey2 = new Survey(); $survey2->setSurveyName("hi2"); $survey2->setDescription('stuff2'); $q3 = new SurveyQuestion(); $q3->setType(QuestionType::StarRating()); $q3->setQuestion("A question3"); $survey2->addQuestion($q3); $this->manager->createSurvey($survey2); //Now lets add a lot more answers! $max = rand(1, 10); for ($i = 0; $i < $max; ++$i) { $ans = new SurveyAnswer(); $ans->setAnswer("awesome{$i}"); $ans->setAnsweredBy("+12064122496"); $this->manager->addAnswer($q->getId(), $ans); $ans2 = new SurveyAnswer(); $ans2->setAnswer("2awesome{$i}"); $ans2->setAnsweredBy("+12064122496"); $this->manager->addAnswer($q2->getId(), $ans2); $ans3 = new SurveyAnswer(); $ans3->setAnswer("don't return me"); $ans3->setAnsweredBy("+12064122496"); $this->manager->addAnswer($q3->getId(), $ans3); } $answers = $this->manager->getAnswers($survey->getId()); $this->assertNotNull($answers); $this->assertEquals(1 + $max * 2, $answers->getTotalAnswers()); $answers = $this->manager->getAnswers($survey2->getId()); $this->assertNotNull($answers); $this->assertEquals($max, $answers->getTotalAnswers()); }
public function testTagCloud() { $survey = new Survey(); $survey->setSurveyName("hi"); $survey->setDescription('stuff'); $q = new SurveyQuestion(); $q->setType(QuestionType::Text()); $q->setQuestion("Your Suggestions"); $survey->addQuestion($q); $this->manager->createSurvey($survey); $max = rand(1, 20); for ($i = 0; $i < $max; ++$i) { $val = $this->getRandomString(); $ans = new SurveyAnswer(); $ans->setAnswer($val); $ans->setAnsweredBy("+12064122496"); $this->manager->addAnswer($q->getId(), $ans); } $answers = $this->manager->getAnswers($survey->getId()); $strArr = ReportChartFormatter::getChartData($answers->getAnswers($q->getId()), ChartFormats::TagCloud()); $this->assertNotNull($strArr); //TODO: how the heck do i test this? I guess that it just works? }
/** * Get a list of most provided answers for the question. * * @since 0.1 * * @param SurveyQuestion $question * * @return string */ protected function getAnswerList(SurveyQuestion $question) { if ($question->isRestrictiveType()) { $list = '<ul>'; $answers = array(); $answerTranslations = array(); if ($question->getField('type') == SurveyQuestion::$TYPE_CHECK) { $possibilities = array('0', '1'); $answerTranslations['0'] = wfMsg('surveys-surveystats-unchecked'); $answerTranslations['1'] = wfMsg('surveys-surveystats-checked'); } else { $possibilities = $question->getField('answers'); } foreach ($possibilities as $answer) { $answers[$answer] = SurveyAnswer::count(array('text' => $answer)); } asort($answers, SORT_NUMERIC); foreach (array_reverse($answers) as $answer => $answerCount) { if (array_key_exists($answer, $answerTranslations)) { $answer = $answerTranslations[$answer]; } $list .= Html::element('li', array(), wfMsgExt('surveys-surveystats-question-answer', 'parsemag', $answer, $this->getLanguage()->formatNum($answerCount))); } return $list . '</ul>'; } else { return ''; } }
/** * Exclude object from result * * @param SurveyAnswer $surveyAnswer Object to remove from the list of results * * @return SurveyAnswerQuery The current query, for fluid interface */ public function prune($surveyAnswer = null) { if ($surveyAnswer) { $this->addUsingAlias(SurveyAnswerPeer::ANSWER_ID, $surveyAnswer->getAnswerId(), Criteria::NOT_EQUAL); } return $this; }
/** * Adds an object to the instance pool. * * Propel keeps cached copies of objects in an instance pool when they are retrieved * from the database. In some cases -- especially when you override doSelect*() * methods in your stub classes -- you may need to explicitly add objects * to the cache in order to ensure that the same objects are always returned by doSelect*() * and retrieveByPK*() calls. * * @param SurveyAnswer $value A SurveyAnswer object. * @param string $key (optional) key to use for instance map (for performance boost if key was already calculated externally). */ public static function addInstanceToPool(SurveyAnswer $obj, $key = null) { if (Propel::isInstancePoolingEnabled()) { if ($key === null) { $key = (string) $obj->getAnswerId(); } // if key === null self::$instances[$key] = $obj; } }
public function show($permalink = null) { if (isset($_GET['permalink'])) { $permalink = $_GET['permalink']; } $survey = Survey::find_by_permalink($permalink); if (!$survey or !$survey->active && Site::CurrentUser()->isAdmin() == 0) { Error404(); } if ($survey->event->id) { $event_id = mysql_real_escape_string($survey->event_id); $user_id = mysql_real_escape_string(Site::CurrentUser()->id); $result = EventSignup::find("events.id = '{$event_id}' AND users.id = '{$user_id}' AND event_signups.paid"); if (!$result) { Site::Flash("error", "You must have attended {$survey->event->name} to take this survey"); RedirectBack(); } } $user_id = mysql_real_escape_string(Site::CurrentUser()->id); $survey_id = mysql_real_escape_string($survey->id); $response = SurveyResponse::find("surveys.id = '{$survey_id}' AND users.id = '{$user_id}'"); if ($response) { Site::Flash("error", "You have already completed this survey"); Redirect("surveys"); } $response = new SurveyResponse(); $response->survey = $survey; $response->survey_id = $survey->id; $response->user = Site::CurrentUser(); $response->user_id = Site::CurrentUser()->id; $answers = array(); $choices = array(); $valid = true; $errors = array(); if ($this->post) { foreach ($survey->questions() as $question) { $answer = new SurveyAnswer(); $answer->survey_question_id = $question->id; if (in_array($question->type, array("sqtTextbox", "sqtTextArea"))) { // Free-text input if (isset($_POST['question'][$question->id])) { $answer->value = $_POST['question'][$question->id]; } elseif (!$question->required) { continue; } } else { if (isset($_POST['question'][$question->id])) { $chosen = array(); if ($question->type == "sqtCheckbox") { // Checkboxes $chosen = $_POST['question'][$question->id]; } else { // Radio/Select $chosen = array($_POST['question'][$question->id]); } foreach ($chosen as $id) { $option = SurveyQuestionOption::find_by_id($id); if (!$option or $option->question->id != $question->id) { if (!$question->required) { continue; } $valid = false; $answer->add_error("Answer for question {$question->position} is invalid"); $errors[] = $question->id; break; } $choice = new SurveyAnswerChoice(); $choice->survey_question_option_id = $option->id; $choices[$question->id][$option->id] = $choice; } } elseif ($question->required) { $valid = false; $answer->add_error("You must enter an answer for question {$question->position}"); $errors[] = $question->id; } else { continue; } } $result = $answer->validate(); if (!$result) { $valid = false; } $answers[$question->id] = $answer; } if ($valid) { // This is valid, let's save everything! if ($response->save()) { foreach ($answers as $answer) { $answer->survey_response_id = $response->id; $answer->save(); $answers[$answer->survey_question_id] = $answer; } foreach ($choices as $question_id => $answer_choices) { foreach ($answer_choices as $choice) { $choice->survey_answer_id = $answers[$question_id]->id; $choice->save(); } } $account = TwitterAccount::find_by_code('site'); if ($account) { $message = "{$response->user->nickname} has completed the {$survey->name} Survey"; $account->add_tweet($message); } Redirect("surveys/{$survey->permalink}/complete"); } $valid = false; } } $this->assign("answers", $answers); $this->assign("choices", $choices); $this->assign("valid", $valid); $this->assign("survey", $survey); $this->assign("response", $response); $this->assign("errors", $errors); $this->title = $survey->name; $this->render("survey/show.tpl"); }