public function initOption($countList, $optionCount = null) { if (empty($this->optionDtoList)) { return array(); } foreach ($this->optionDtoList as $opt) { $this->optionIdList[] = $opt->id; $cmp = new QUESTIONS_CMP_Answer($opt, $this->uniqId); $this->optionList[$opt->id] = $cmp; $cmp->setDisbled(!$this->editable); $cmp->setIsMultiple(!$this->poll); } $checkedOptions = array(); $optionsState = array(); $totalAnswers = $this->answerCount; //$countList = $this->service->findAnswersCount($this->optionIdList); $answerDtoList = $this->service->findUserAnswerList($this->userId, $this->optionIdList); foreach ($answerDtoList as $item) { $checkedOptions[] = $item->optionId; $this->getOption($item->optionId)->setVoted(); } $optionCount = empty($optionCount) ? count($this->optionDtoList) : $optionCount; foreach ($this->optionDtoList as $optionDto) { $optionId = $optionDto->id; $checked = in_array($optionId, $checkedOptions); $voteCount = $countList[$optionId]; $users = $this->service->findAnsweredUserIdList($optionId, $this->usersContext, $checked ? 4 : 3); $optionsState[] = array('id' => $optionId, 'users' => $users, 'voteCount' => $voteCount, 'checked' => $checked); $this->getOption($optionId)->setVoteCount($voteCount); $this->getOption($optionId)->setUsers($users); $canEdit = $optionDto->userId == $this->userId && ($voteCount == 0 || $voteCount == 1 && $checked); $canEdit = $this->editMode || $canEdit; $canEdit = $this->poll ? $canEdit && $optionCount > 2 : $canEdit; $this->getOption($optionId)->setEditMode($canEdit); if ($totalAnswers) { $this->getOption($optionId)->setPercents($voteCount * 100 / $totalAnswers); } } return $optionsState; }
private function addAnswer($query, $data, $relation) { $userId = OW::getUser()->getId(); $questionId = $data['questionId']; $uniqId = $data['uniqId']; $text = strip_tags(trim($query['text'])); $option = $this->service->findOptionByText($questionId, $text); $new = false; if ($option === null) { $new = true; $option = $this->service->addOption($questionId, $userId, $text); } $cmp = new QUESTIONS_CMP_Answer($option, $uniqId); $cmp->setIsMultiple(!$data['poll']); $voteCount = 0; $checked = false; $users = array(); if ($new) { $cmp->setEditMode(); $data['optionTotal']++; } else { $canEdit = $option->userId == $userId; $answerCount = $data['poll'] ? $this->service->findTotalAnswersCount($questionId) : $this->service->findMaxAnswersCount($questionId); if ($answerCount) { $voteCount = $this->service->findAnswerCountByOptionId($option->id); if ($voteCount) { $checked = $this->service->findAnswer($userId, $option->id) !== null; $users = $this->service->findAnsweredUserIdList($option->id, $data['userContext'], $checked ? 4 : 3); $cmp->setVoteCount($voteCount); $cmp->setVoted($checked); $cmp->setUsers($users); $cmp->setPercents($voteCount * 100 / $answerCount); $canEdit = $canEdit && ($voteCount == 0 || $voteCount == 1 && $checked); } } $cmp->setEditMode($data['editMode'] || $canEdit); } $data['displayedCount']++; $options = array(); $options[] = array('markup' => $cmp->render(), 'data' => array('newOption' => $new, 'checked' => $checked, 'users' => $users, 'voteCount' => $voteCount, 'id' => $option->id)); if (!empty($relation)) { $relation = $this->reload(array(), $relation['data']); } return array('options' => $options, 'data' => $data, 'relation' => $relation); }