Beispiel #1
0
 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 EQUESTIONS_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']);
     }
     $permissions = EQUESTIONS_CLASS_CreditsBridge::getInstance()->getAllPermissions(EQUESTIONS_CLASS_Credits::ACTION_ADD_ANSWER);
     return array('options' => $options, 'data' => $data, 'relation' => $relation, 'permissions' => $permissions);
 }
Beispiel #2
0
 public function addQuestion()
 {
     if (!OW::getRequest()->isAjax()) {
         throw new Redirect404Exception();
     }
     if (!OW::getUser()->isAuthenticated()) {
         echo json_encode(false);
         exit;
     }
     if (empty($_POST['question'])) {
         echo json_encode(false);
         exit;
     }
     $permissions = EQUESTIONS_CLASS_CreditsBridge::getInstance()->getAllPermissions(EQUESTIONS_CLASS_Credits::ACTION_ASK);
     if (!$permissions[EQUESTIONS_CLASS_Credits::ACTION_ASK]) {
         echo json_encode(array('reset' => false, 'warning' => EQUESTIONS_CLASS_CreditsBridge::getInstance()->credits->getErrorMessage(EQUESTIONS_CLASS_Credits::ACTION_ASK)));
         exit;
     }
     $question = empty($_POST['question']) ? '' : htmlspecialchars($_POST['question']);
     $answers = empty($_POST['answers']) ? array() : array_filter($_POST['answers'], 'trim');
     $allowAddOprions = !empty($_POST['allowAddOprions']);
     $attachment = empty($_POST['attachment']) ? array() : json_decode($_POST['attachment'], true);
     if (!empty($attachment)) {
         if ($attachment['type'] == 'file') {
             $attachment['url'] = OW::getEventManager()->call('base.attachment_save_image', array('genId' => $attachment['fileId']));
             $attachment['type'] = 'photo';
             $attachment['href'] = $attachment['url'];
         }
     }
     $userId = OW::getUser()->getId();
     $questionDto = $this->service->addQuestion($userId, $question, $attachment, array('allowAddOprions' => $allowAddOprions));
     foreach ($answers as $ans) {
         $this->service->addOption($questionDto->id, $userId, $ans);
     }
     $event = new OW_Event('feed.action', array('entityType' => EQUESTIONS_BOL_Service::ENTITY_TYPE, 'entityId' => $questionDto->id, 'pluginKey' => 'equestions', 'userId' => $userId, 'visibility' => 15));
     OW::getEventManager()->trigger($event);
     $activityList = EQUESTIONS_BOL_FeedService::getInstance()->findMainActivity(time(), array($questionDto->id), array(0, 6));
     $cmp = new EQUESTIONS_CMP_FeedItem($questionDto, reset($activityList[$questionDto->id]), $activityList[$questionDto->id]);
     $html = $cmp->render();
     $script = OW::getDocument()->getOnloadScript();
     echo json_encode(array('markup' => array('html' => $html, 'script' => $script, 'position' => 'prepend'), 'permissions' => $permissions));
     exit;
 }