233 $connection->executeSqlBatch(<<<SQL
234UPDATE b_vote V SET V.COUNTER=(
237 WHERE VE.VOTE_ID=V.ID)
239UPDATE b_vote_question VQ SET VQ.COUNTER=(
241 FROM b_vote_event_question VEQ
242 WHERE VEQ.QUESTION_ID=VQ.ID)
243WHERE VQ.VOTE_ID={$voteId};
244UPDATE b_vote_answer VA, b_vote_question VQ SET VA.COUNTER=(
246 FROM b_vote_event_answer VEA
247 WHERE VEA.ANSWER_ID=VA.ID)
248WHERE VQ.ID = VA.QUESTION_ID AND VQ.VOTE_ID={$voteId};
249UPDATE b_vote_user VU, b_vote_event VE SET VU.COUNTER=(
252 WHERE VU.ID=VE.VOTE_USER_ID AND VE.VALID='Y')
253WHERE VU.ID IN (SELECT VOTE_USER_ID FROM b_vote_event WHERE VOTE_ID={$voteId});
257 else if ($connection instanceof MssqlConnection)
259 $connection->executeSqlBatch(<<<SQL
260UPDATE b_vote SET b_vote.COUNTER=E.COUNTER
262 SELECT COUNT(ID) COUNTER, VOTE_ID
264 WHERE VOTE_ID={$voteId}
266WHERE b_vote.ID=E.VOTE_ID AND b_vote.ID={$voteId}
268UPDATE b_vote_question SET COUNTER=E.COUNTER
270 SELECT COUNT(EQ.ID) COUNTER, EQ.QUESTION_ID
271 FROM b_vote_event_question EQ
272 JOIN b_vote_question Q ON (Q.ID = EQ.QUESTION_ID)
273 WHERE Q.VOTE_ID={$voteId}
274 GROUP BY EQ.QUESTION_ID) E
275WHERE b_vote_question.ID=E.QUESTION_ID AND b_vote_question.VOTE_ID={$voteId}
277UPDATE b_vote_answer SET b_vote_answer.COUNTER=E.COUNTER
279 SELECT COUNT(VEA.ID) COUNTER, VEA.ANSWER_ID
280 FROM b_vote_event_answer VEA
281 INNER JOIN b_vote_answer VA ON (VA.ID=VEA.ANSWER_ID)
282 INNER JOIN b_vote_question VQ ON (VQ.ID=VA.QUESTION_ID)
283 WHERE VQ.VOTE_ID={$voteId}
284 GROUP BY VEA.ANSWER_ID
286WHERE b_vote_answer.ID=E.ANSWER_ID
288UPDATE b_vote_user SET b_vote_user.COUNTER=E.COUNTER
290 SELECT COUNT(ID) COUNTER, VOTE_USER_ID
293 GROUP BY VOTE_USER_ID
295WHERE b_vote_user.ID=E.VOTE_USER_ID AND b_vote_user.ID IN (SELECT VOTE_USER_ID FROM b_vote_event WHERE VOTE_ID={$voteId})
300 elseif ($connection instanceof OracleConnection)
302 $connection->executeSqlBatch(<<<SQL
303UPDATE b_vote V SET V.COUNTER=(
306 WHERE VE.VOTE_ID=V.ID)
309UPDATE b_vote_question VQ SET VQ.COUNTER=(
311 FROM b_vote_event_question VEQ
312 WHERE VEQ.QUESTION_ID=VQ.ID)
313WHERE VQ.VOTE_ID={$voteId}
315UPDATE b_vote_answer VA SET VA.COUNTER=(
317 FROM b_vote_event_answer
318 WHERE ANSWER_ID=VA.ID)
319WHERE VA.QUESTION_ID IN (
322 WHERE VOTE_ID={$voteId}
325UPDATE b_vote_user VU SET VU.COUNTER=(
328 WHERE VU.ID=VOTE_USER_ID AND VALID=
'Y')
329WHERE VU.ID IN (SELECT VOTE_USER_ID FROM b_vote_event WHERE VOTE_ID={$voteId})
345 $connection->executeSqlBatch(<<<SQL
348 SELECT count(ID) as COUNTER, VOTE_USER_ID
350 WHERE VOTE_ID={$voteId}
351 GROUP BY VOTE_USER_ID
352 ) E ON (E.VOTE_USER_ID=U.ID)
353 SET U.COUNTER = (CASE WHEN U.COUNTER - E.COUNTER > 0 THEN U.COUNTER - E.COUNTER ELSE 0 END);
354UPDATE b_vote
set COUNTER=0 WHERE ID={$voteId};
355UPDATE b_vote_question SET COUNTER=0 WHERE VOTE_ID={$voteId};
356UPDATE b_vote_answer A
357 INNER JOIN b_vote_question Q ON (Q.ID=A.QUESTION_ID AND Q.VOTE_ID={$voteId})
359DELETE FROM b_vote_event WHERE VOTE_ID={$voteId};
360DELETE EQ FROM b_vote_event_question EQ
361 JOIN b_vote_question Q ON Q.ID = EQ.QUESTION_ID
362WHERE Q.VOTE_ID = {$voteId};
363DELETE EA FROM b_vote_event_answer EA
364 JOIN b_vote_answer A ON A.ID = EA.ANSWER_ID
365 JOIN b_vote_question Q ON Q.ID = A.QUESTION_ID
366WHERE Q.VOTE_ID = {$voteId};
373 $connection->executeSqlBatch(<<<SQL
374UPDATE b_vote_user SET b_vote_user.COUNTER = (CASE WHEN b_vote_user.COUNTER - E.COUNTER > 0 THEN b_vote_user.COUNTER - E.COUNTER ELSE 0 END)
376 SELECT count(ID) as COUNTER, VOTE_USER_ID
378 WHERE VOTE_ID={$voteId}
379 GROUP BY VOTE_USER_ID
381WHERE (E.VOTE_USER_ID=b_vote_user.ID)
383UPDATE b_vote
set COUNTER=0 WHERE ID={$voteId}
385UPDATE b_vote_question SET COUNTER=0 WHERE VOTE_ID={$voteId}
387UPDATE b_vote_answer SET COUNTER=0
389 SELECT ID FROM b_vote_question WHERE VOTE_ID={$voteId}
391WHERE b_vote_answer.QUESTION_ID=Q.ID
393DELETE FROM b_vote_event WHERE VOTE_ID={$voteId}
395DELETE EQ FROM b_vote_event_question EQ
396 JOIN b_vote_question Q ON Q.ID = EQ.QUESTION_ID
397WHERE Q.VOTE_ID = {$voteId}
399DELETE EA FROM b_vote_event_answer EA
400 JOIN b_vote_answer A ON A.ID = EA.ANSWER_ID
401 JOIN b_vote_question Q ON Q.ID = A.QUESTION_ID
402WHERE Q.VOTE_ID = {$voteId}
409 $connection->executeSqlBatch(<<<SQL
410UPDATE b_vote_user U SET U.COUNTER = (
411 SELECT (CASE WHEN U.COUNTER - E.COUNTER > 0 THEN U.COUNTER - E.COUNTER ELSE 0 END)
413 SELECT count(ID) as COUNTER, VOTE_USER_ID
415 WHERE VOTE_ID={$voteId}
416 GROUP BY VOTE_USER_ID
418 WHERE E.VOTE_USER_ID = U.ID
423 WHERE VOTE_ID={$voteId}
424 GROUP BY VOTE_USER_ID
427UPDATE b_vote
set COUNTER=0 WHERE ID={$voteId}
429UPDATE b_vote_question SET COUNTER=0 WHERE VOTE_ID={$voteId}
431UPDATE b_vote_answer SET COUNTER=0
432WHERE QUESTION_ID IN (
433 SELECT ID FROM b_vote_question WHERE VOTE_ID={$voteId}
436DELETE FROM b_vote_event WHERE VOTE_ID={$voteId}
438DELETE FROM b_vote_event_question
439WHERE QUESTION_ID IN (
440 SELECT ID from b_vote_question WHERE VOTE_ID = {$voteId}
443DELETE FROM b_vote_event_answer
447 JOIN b_vote_question Q ON (Q.ID = A.QUESTION_ID)
448 WHERE Q.VOTE_ID = {$voteId}
454 foreach (GetModuleEvents(
"vote",
"onVoteReset",
true) as $event)
455 ExecuteModuleEventEx($event, array($voteId));
485 $valid = ($valid ==
"Y" ?
"Y" :
"N");
486 $eventId = intval($eventId);
490 $dbRes = EventTable::getList(array(
493 'Q_' =>
'QUESTION.*',
494 'A_' =>
'QUESTION.ANSWER.*'),
497 '!=VALID' => $valid),
500 'QUESTION.ID' =>
'ASC',
501 'QUESTION.ANSWER.ID' =>
'ASC')));
502 if (($res = $dbRes->fetch()) && $res)
504 $questions = array();
506 EventTable::update($eventId, array(
"VALID" => $valid));
507 VoteTable::setCounter(array($res[
"V_VOTE_ID"]), ($valid ==
"Y"));
508 UserTable::setCounter(array($res[
"V_VOTE_USER_ID"]), ($valid ==
"Y"));
511 $questions[] = $res[
"Q_QUESTION_ID"];
512 $answers[] = $res[
"A_ANSWER_ID"];
513 }
while ($res = $dbRes->fetch());
515 QuestionTable::setCounter(array_unique($questions), ($valid ==
"Y"));
516 AnswerTable::setCounter($answers, ($valid ==
"Y"));
538 array_key_exists(self::EVENT_FIELD_NAME, $request) &&
539 is_array($request[self::EVENT_FIELD_NAME]) &&
540 array_key_exists($id, $request[self::EVENT_FIELD_NAME]) &&
541 is_array($request[self::EVENT_FIELD_NAME][$id])
545 if (array_key_exists(
"BALLOT", $request[self::EVENT_FIELD_NAME][$id]))
547 foreach ($request[self::EVENT_FIELD_NAME][$id][
"BALLOT"] as $qId => $answerIds)
549 $answerIds = is_array($answerIds) ? $answerIds : array($answerIds);
550 foreach ($answerIds as $answerId)
552 $data[
"BALLOT"] = is_array($data[
"BALLOT"]) ? $data[
"BALLOT"] : [];
553 $data[
"BALLOT"][$qId] = is_array($data[
"BALLOT"][$qId]) ? $data[
"BALLOT"][$qId] : [];
554 $data[
"BALLOT"][$qId][$answerId] =
true;
558 if (array_key_exists(
"MESSAGE", $request[self::EVENT_FIELD_NAME][$id]))
560 foreach ($request[self::EVENT_FIELD_NAME][$id][
"MESSAGE"] as $qId => $answerIds)
563 foreach ($answerIds as $answerId => $message)
565 $message = trim($message);
568 $data[
"MESSAGE"][$qId] = is_array($data[
"MESSAGE"][$qId]) ? $data[
"MESSAGE"][$qId] : [];
569 $data[
"MESSAGE"][$qId][$answerId] = $message;
574 if (array_key_exists(
"EXTRAS", $request[self::EVENT_FIELD_NAME][$id]))
576 $data[
"EXTRAS"] = $request[self::EVENT_FIELD_NAME][$id][
"EXTRAS"];
587 public function check(array $ballot)
589 $questions = $this->vote->getQuestions();
591 $data = (array_key_exists(
"BALLOT", $ballot) ? $ballot[
"BALLOT"] : []);
592 $message = (array_key_exists(
"MESSAGE", $ballot) ? $ballot[
"MESSAGE"] : []);
593 foreach ($questions as $questionId => $question)
595 if (array_key_exists($question[
"ID"], $data) && is_array($data[$question[
"ID"]]))
597 $answers = array_intersect_key($data[$question[
"ID"]], $question[
"ANSWERS"]);
598 if ($question[
"FIELD_TYPE"] === QuestionTypes::COMPATIBILITY && array_key_exists($question[
"ID"], $message))
600 foreach($message[$question[
"ID"]] as $id => $value)
602 $value = trim($value);
605 $answers[$id] =
true;
609 if (!empty($answers))
612 if ($question[
"FIELD_TYPE"] == QuestionTypes::COMPATIBILITY)
614 $singleVal = array(AnswerTypes::RADIO =>
false, AnswerTypes::DROPDOWN =>
false);
616 foreach ($answers as $id => $value)
618 $answer = $question[
"ANSWERS"][$id];
619 switch ($answer[
"FIELD_TYPE"])
621 case AnswerTypes::RADIO :
622 case AnswerTypes::DROPDOWN :
623 if (!$singleVal[$answer[
"FIELD_TYPE"]])
625 $singleVal[$answer[
"FIELD_TYPE"]] =
true;
636 $fields[$question[
"ID"]] = $res;
640 else if ($question[
"FIELD_TYPE"] == QuestionTypes::RADIO ||
641 $question[
"FIELD_TYPE"] == QuestionTypes::DROPDOWN)
643 $val = reset($answers);
644 $fields[$question[
"ID"]] = array(
645 key($answers) => $val
650 $fields[$question[
"ID"]] = $answers;
653 $res = $fields[$question[
"ID"]];
654 if (array_key_exists($question[
"ID"], $message))
656 $message[$question[
"ID"]] = is_array($message[$question[
"ID"]]) ? $message[$question[
"ID"]] : [];
657 foreach ($fields[$question[
"ID"]] as $id => $value)
659 if (array_key_exists($id, $message[$question[
"ID"]]))
660 $fields[$question[
"ID"]][$id] = trim($message[$question[
"ID"]][$id]);
663 if (empty($fields[$question[
"ID"]]))
665 unset($fields[$question[
"ID"]]);
670 if ($question[
'REQUIRED'] ===
'Y' && $question[
'ACTIVE'] ===
'Y' && !array_key_exists($question[
"ID"], $fields))
672 $this->errorCollection->add(array(
new Error(
Loc::getMessage(
"VOTE_REQUIRED_MISSING"),
"QUESTION_".$questionId)));
676 $this->errorCollection->add(array(
new Error(
Loc::getMessage(
"USER_VOTE_EMPTY"),
"VOTE_".$this->vote->getId())));
681 public function add(array $eventFields, array $ballot, $setCounter =
true): ?
EventResult
683 $this->errorCollection->clear();
684 $fields = $this->check($ballot);
685 if (!$this->errorCollection->isEmpty())
689 $eventFields = array(
690 "VOTE_ID" => $this->vote->getId(),
691 "VOTE_USER_ID" => $eventFields[
"VOTE_USER_ID"],
692 "DATE_VOTE" => (array_key_exists(
"DATE_VOTE", $eventFields) ? $eventFields[
"DATE_VOTE"] : new \Bitrix\Main\Type\DateTime()),
693 "STAT_SESSION_ID" => $eventFields[
"STAT_SESSION_ID"],
694 "IP" => $eventFields[
"IP"],
695 "VALID" => $eventFields[
"VALID"] ?:
"Y",
696 "VISIBLE" => ($eventFields[
"VISIBLE"] ?:
"Y")
698 if (array_key_exists(
"EXTRAS", $ballot) && is_array($ballot[
"EXTRAS"]) && array_key_exists(
"VISIBLE", $ballot[
"EXTRAS"]))
699 $eventFields[
"VISIBLE"] = ($ballot[
"EXTRAS"][
"VISIBLE"] ===
"N" ?
"N" :
"Y");
702 $sqlAnswers = array();
703 foreach ($fields as $questionId => $fieldsAnswer)
705 foreach ($fieldsAnswer as $answerId => $value)
707 $sqlAnswers[$questionId][$answerId] = array(
708 "ANSWER_ID" => $answerId,
709 "MESSAGE" => is_string($value)? mb_substr($value, 0, 2000) :
"");
714 foreach (GetModuleEvents(
"vote",
"onBeforeVoting",
true) as $event)
716 if (ExecuteModuleEventEx($event, array(&$eventFields, &$sqlAnswers)) ===
false)
718 $this->errorCollection->add(array(
new Error(
"onBeforeVoting error",
"VOTE_".$eventFields[
"VOTE_ID"])));
723 if (!empty($sqlAnswers) && ($eventId = EventTable::add($eventFields)->getId()) && $eventId > 0)
726 $answerIdsForCounter = array();
727 foreach ($sqlAnswers as $questionId => $fieldsAnswer)
729 if (($eventQId = EventQuestionTable::add(array(
"EVENT_ID" => $eventId,
"QUESTION_ID" => $questionId))->getId()) && $eventQId > 0)
731 $ids[$questionId] = [
732 "EVENT_ID" => $eventQId,
735 foreach ($fieldsAnswer as $answerId => $res)
737 if (($eventAId = EventAnswerTable::add(array(
738 "EVENT_QUESTION_ID" => $eventQId,
739 "ANSWER_ID" => $res[
"ANSWER_ID"],
740 "MESSAGE" => $res[
"MESSAGE"]))->getId()
743 $ids[$questionId][
"ANSWERS"][$answerId] = [
744 "EVENT_ID" => $eventAId,
745 "EVENT_QUESTION_ID" => $eventQId,
746 "ANSWER_ID" => $res[
"ANSWER_ID"],
747 "MESSAGE" => $res[
"MESSAGE"]
749 $answerIdsForCounter[] = $answerId;
752 if (empty($ids[$questionId]))
754 EventQuestionTable::delete($eventQId);
755 unset($ids[$questionId]);
764 VoteTable::setCounter(array($this->vote->getId()),
true);
765 QuestionTable::setCounter(array_keys($ids),
true);
766 AnswerTable::setCounter($answerIdsForCounter,
true);
770 "EVENT_ID" => $eventId,
771 "VOTE_ID" => $eventFields[
"VOTE_ID"],
772 "VOTE_USER_ID" => $eventFields[
"VOTE_USER_ID"],
773 "DATE_VOTE" => $eventFields[
"DATE_VOTE"],
774 "STAT_SESSION_ID" => $eventFields[
"SESS_SESSION_ID"],
775 "IP" => $eventFields[
"IP"],
776 "VISIBLE" => $eventFields[
"VISIBLE"],
777 "VALID" => $eventFields[
"VALID"],
781 EventTable::delete($eventId);