Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
event.php
1<?php
8namespace Bitrix\Vote;
12use \Bitrix\Main\Entity;
15use \Bitrix\Main\Localization\Loc;
29use Bitrix\Main\Type\Dictionary as EventResult;
31
32Loc::loadMessages(__FILE__);
33
48class EventTable extends Entity\DataManager
49{
55 public static function getTableName()
56 {
57 return 'b_vote_event';
58 }
59
65 public static function getMap()
66 {
67 return array(
68 (new IntegerField('ID', ['primary' => true, 'autocomplete' => true])),
69 (new IntegerField('VOTE_ID')),
70 (new IntegerField('VOTE_USER_ID', ["required" => true])),
71 (new DatetimeField('DATE_VOTE')),
72 (new IntegerField('STAT_SESSION_ID')),
73 (new StringField('IP', ['size' => 15])),
74 (new BooleanField('VALID', ['values' => ['N', 'Y'], 'default_value' => 'Y'])),
75 (new BooleanField('VISIBLE', ['values' => ['N', 'Y'], 'default_value' => 'Y'])),
76 (new Reference('QUESTION', \Bitrix\Vote\EventQuestionTable::class, Join::on('this.ID', 'ref.EVENT_ID'))),
77 (new Reference('USER', \Bitrix\Vote\UserTable::class, Join::on('this.VOTE_USER_ID', 'ref.ID'))),
78 );
79 }
80}
91class EventQuestionTable extends Entity\DataManager
92{
98 public static function getTableName()
99 {
100 return 'b_vote_event_question';
101 }
102
108 public static function getMap()
109 {
110 return array(
111 'ID' => array(
112 'data_type' => 'integer',
113 'primary' => true,
114 'autocomplete' => true,
115 ),
116 'EVENT_ID' => array(
117 'data_type' => 'integer',
118 ),
119 'QUESTION_ID' => array(
120 'data_type' => 'integer',
121 ),
122 'VOTE' => array(
123 'data_type' => '\Bitrix\Vote\EventTable',
124 'reference' => array(
125 '=this.EVENT_ID' => 'ref.ID',
126 ),
127 'join_type' => 'RIGHT',
128 ),
129 'ANSWER' => array(
130 'data_type' => '\Bitrix\Vote\EventAnswerTable',
131 'reference' => array(
132 '=this.ID' => 'ref.EVENT_QUESTION_ID',
133 ),
134 'join_type' => 'LEFT',
135 )
136 );
137 }
138}
162class EventAnswerTable extends Entity\DataManager
163{
169 public static function getTableName()
170 {
171 return 'b_vote_event_answer';
172 }
173
179 public static function getMap()
180 {
181 return array(
182 'ID' => array(
183 'data_type' => 'integer',
184 'primary' => true,
185 'autocomplete' => true,
186 ),
187 'EVENT_QUESTION_ID' => array(
188 'data_type' => 'integer',
189 ),
190 'ANSWER_ID' => array(
191 'data_type' => 'integer',
192 ),
193 'MESSAGE' => array(
194 'data_type' => 'text',
195 )
196 );
197 }
198}
199
200class Event extends BaseObject
201{
202 private $vote;
207 const EVENT_FIELD_NAME = "bx_vote_event"; //
208 const EVENT_FIELD_BALLOT_TEMPLATE = self::EVENT_FIELD_NAME."[#ID#][BALLOT][#QUESTION_ID#]"; // this is template for voting
209 const EVENT_FIELD_MESSAGE_TEMPLATE = self::EVENT_FIELD_NAME."[#ID#][MESSAGE][#QUESTION_ID#][#ANSWER_ID#]"; // this is template for voting
210 const EVENT_FIELD_EXTRAS_TEMPLATE = self::EVENT_FIELD_NAME."[#ID#][EXTRAS][#ENTITY_ID#]";
211
218 function __construct(\Bitrix\Vote\Vote $vote)
219 {
220 $this->vote = $vote;
221 $this->errorCollection = new ErrorCollection;
222 }
223
228 public static function calculateStatistic($voteId)
229 {
230 $connection = Application::getInstance()->getConnection();
231 if ($connection instanceof MysqlCommonConnection)
232 {
233 $connection->executeSqlBatch(<<<SQL
234UPDATE b_vote V SET V.COUNTER=(
235 SELECT COUNT(VE.ID)
236 FROM b_vote_event VE
237 WHERE VE.VOTE_ID=V.ID)
238WHERE V.ID={$voteId};
239UPDATE b_vote_question VQ SET VQ.COUNTER=(
240 SELECT COUNT(VEQ.ID)
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=(
245 SELECT COUNT(VEA.ID)
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=(
250 SELECT COUNT(VE.ID)
251 FROM b_vote_event VE
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});
254SQL
255 );
256 }
257 else if ($connection instanceof MssqlConnection)
258 {
259 $connection->executeSqlBatch(<<<SQL
260UPDATE b_vote SET b_vote.COUNTER=E.COUNTER
261FROM (
262 SELECT COUNT(ID) COUNTER, VOTE_ID
263 FROM b_vote_event
264 WHERE VOTE_ID={$voteId}
265 GROUP BY VOTE_ID) E
266WHERE b_vote.ID=E.VOTE_ID AND b_vote.ID={$voteId}
267GO
268UPDATE b_vote_question SET COUNTER=E.COUNTER
269FROM (
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}
276GO
277UPDATE b_vote_answer SET b_vote_answer.COUNTER=E.COUNTER
278FROM (
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
285) E
286WHERE b_vote_answer.ID=E.ANSWER_ID
287GO
288UPDATE b_vote_user SET b_vote_user.COUNTER=E.COUNTER
289FROM (
290 SELECT COUNT(ID) COUNTER, VOTE_USER_ID
291 FROM b_vote_event
292 WHERE VALID='Y'
293 GROUP BY VOTE_USER_ID
294) E
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})
296GO
297SQL
298 );
299 }
300 elseif ($connection instanceof OracleConnection)
301 {
302 $connection->executeSqlBatch(<<<SQL
303UPDATE b_vote V SET V.COUNTER=(
304 SELECT COUNT(VE.ID)
305 FROM b_vote_event VE
306 WHERE VE.VOTE_ID=V.ID)
307WHERE V.ID={$voteId}
308/
309UPDATE b_vote_question VQ SET VQ.COUNTER=(
310 SELECT COUNT(VEQ.ID)
311 FROM b_vote_event_question VEQ
312 WHERE VEQ.QUESTION_ID=VQ.ID)
313WHERE VQ.VOTE_ID={$voteId}
314/
315UPDATE b_vote_answer VA SET VA.COUNTER=(
316 SELECT COUNT(ID)
317 FROM b_vote_event_answer
318 WHERE ANSWER_ID=VA.ID)
319WHERE VA.QUESTION_ID IN (
320 SELECT ID
321 FROM b_vote_question
322 WHERE VOTE_ID={$voteId}
323)
324/
325UPDATE b_vote_user VU SET VU.COUNTER=(
326 SELECT COUNT(ID)
327 FROM b_vote_event
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})
330/
331SQL
332 );
333 }
334 }
335
340 public static function resetStatistic($voteId)
341 {
342 $connection = Application::getInstance()->getConnection();
343 if ($connection instanceof MysqlCommonConnection)
344 {
345 $connection->executeSqlBatch(<<<SQL
346UPDATE b_vote_user U
347 INNER JOIN (
348 SELECT count(ID) as COUNTER, VOTE_USER_ID
349 FROM b_vote_event
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})
358 SET A.COUNTER = 0;
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};
367
368SQL
369 );
370 }
371 else if ($connection instanceof MssqlConnection)
372 {
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)
375FROM (
376 SELECT count(ID) as COUNTER, VOTE_USER_ID
377 FROM b_vote_event
378 WHERE VOTE_ID={$voteId}
379 GROUP BY VOTE_USER_ID
380) E
381WHERE (E.VOTE_USER_ID=b_vote_user.ID)
382GO
383UPDATE b_vote set COUNTER=0 WHERE ID={$voteId}
384GO
385UPDATE b_vote_question SET COUNTER=0 WHERE VOTE_ID={$voteId}
386GO
387UPDATE b_vote_answer SET COUNTER=0
388FROM (
389 SELECT ID FROM b_vote_question WHERE VOTE_ID={$voteId}
390) Q
391WHERE b_vote_answer.QUESTION_ID=Q.ID
392GO
393DELETE FROM b_vote_event WHERE VOTE_ID={$voteId}
394GO
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}
398GO
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}
403GO
404SQL
405 );
406 }
407 elseif ($connection instanceof OracleConnection)
408 {
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)
412 FROM (
413 SELECT count(ID) as COUNTER, VOTE_USER_ID
414 FROM b_vote_event
415 WHERE VOTE_ID={$voteId}
416 GROUP BY VOTE_USER_ID
417 ) E
418 WHERE E.VOTE_USER_ID = U.ID
419)
420WHERE U.ID IN (
421 SELECT VOTE_USER_ID
422 FROM b_vote_event
423 WHERE VOTE_ID={$voteId}
424 GROUP BY VOTE_USER_ID
425)
426/
427UPDATE b_vote set COUNTER=0 WHERE ID={$voteId}
428/
429UPDATE b_vote_question SET COUNTER=0 WHERE VOTE_ID={$voteId}
430/
431UPDATE b_vote_answer SET COUNTER=0
432WHERE QUESTION_ID IN (
433 SELECT ID FROM b_vote_question WHERE VOTE_ID={$voteId}
434)
435/
436DELETE FROM b_vote_event WHERE VOTE_ID={$voteId}
437/
438DELETE FROM b_vote_event_question
439WHERE QUESTION_ID IN (
440 SELECT ID from b_vote_question WHERE VOTE_ID = {$voteId}
441)
442/
443DELETE FROM b_vote_event_answer
444WHERE ANSWER_ID IN (
445 SELECT A.ID
446 FROM b_vote_answer A
447 JOIN b_vote_question Q ON (Q.ID = A.QUESTION_ID)
448 WHERE Q.VOTE_ID = {$voteId}
449)
450/
451SQL
452 );
453 /***************** Event OnVoteReset *******************************/
454 foreach (GetModuleEvents("vote", "onVoteReset", true) as $event)
455 ExecuteModuleEventEx($event, array($voteId));
456 /***************** /Event ******************************************/
457 }
458 }
459
464 public static function deleteEvent($eventId)
465 {
466 if (!is_integer($eventId))
467 throw new ArgumentTypeException("event ID");
468 else if ($eventId <= 0)
469 throw new ArgumentNullException("event ID");
470
471 self::setValid($eventId, "N");
472 $connection = Application::getInstance()->getConnection();
473 $connection->queryExecute("DELETE FROM b_vote_event_answer WHERE EVENT_QUESTION_ID IN (SELECT VEQ.ID FROM b_vote_event_question VEQ WHERE VEQ.EVENT_ID={$eventId})");
474 $connection->queryExecute("DELETE FROM b_vote_event_question WHERE EVENT_ID={$eventId}");
475 $connection->queryExecute("DELETE FROM b_vote_event WHERE ID={$eventId}");
476 return $connection->getAffectedRowsCount() > 0;
477 }
483 public static function setValid($eventId, $valid)
484 {
485 $valid = ($valid == "Y" ? "Y" : "N");
486 $eventId = intval($eventId);
487 if ($eventId <= 0)
488 return false;
489
490 $dbRes = EventTable::getList(array(
491 'select' => array(
492 'V_' => '*',
493 'Q_' => 'QUESTION.*',
494 'A_' => 'QUESTION.ANSWER.*'),
495 'filter' => array(
496 'ID' => $eventId,
497 '!=VALID' => $valid),
498 'order' => array(
499 'ID' => 'ASC',
500 'QUESTION.ID' => 'ASC',
501 'QUESTION.ANSWER.ID' => 'ASC')));
502 if (($res = $dbRes->fetch()) && $res)
503 {
504 $questions = array();
505 $answers = 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"));
509 do
510 {
511 $questions[] = $res["Q_QUESTION_ID"];
512 $answers[] = $res["A_ANSWER_ID"];
513 } while ($res = $dbRes->fetch());
514
515 QuestionTable::setCounter(array_unique($questions), ($valid == "Y"));
516 AnswerTable::setCounter($answers, ($valid == "Y"));
517 return true;
518 }
519 return false;
520 }
521
522 public static function getFieldName($id, $questionId)
523 {
524 return str_replace(array("#ID#", "#QUESTION_ID#"), array($id, $questionId), self::EVENT_FIELD_BALLOT_TEMPLATE);
525 }
526 public static function getMessageFieldName($id, $questionId, $answerId)
527 {
528 return str_replace(array("#ID#", "#QUESTION_ID#", "#ANSWER_ID#"), array($id, $questionId, $answerId), self::EVENT_FIELD_MESSAGE_TEMPLATE);
529 }
530 public static function getExtrasFieldName($id, $name)
531 {
532 return str_replace(array("#ID#", "#ENTITY_ID#"), array($id, $name), self::EVENT_FIELD_EXTRAS_TEMPLATE);
533 }
534
535 public static function getDataFromRequest($id, array $request)
536 {
537 if (
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])
542 )
543 {
544 $data = [];
545 if (array_key_exists("BALLOT", $request[self::EVENT_FIELD_NAME][$id]))
546 {
547 foreach ($request[self::EVENT_FIELD_NAME][$id]["BALLOT"] as $qId => $answerIds)
548 {
549 $answerIds = is_array($answerIds) ? $answerIds : array($answerIds);
550 foreach ($answerIds as $answerId)
551 {
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;
555 }
556 }
557 }
558 if (array_key_exists("MESSAGE", $request[self::EVENT_FIELD_NAME][$id]))
559 {
560 foreach ($request[self::EVENT_FIELD_NAME][$id]["MESSAGE"] as $qId => $answerIds)
561 {
562
563 foreach ($answerIds as $answerId => $message)
564 {
565 $message = trim($message);
566 if ($message <> '')
567 {
568 $data["MESSAGE"][$qId] = is_array($data["MESSAGE"][$qId]) ? $data["MESSAGE"][$qId] : [];
569 $data["MESSAGE"][$qId][$answerId] = $message;
570 }
571 }
572 }
573 }
574 if (array_key_exists("EXTRAS", $request[self::EVENT_FIELD_NAME][$id]))
575 {
576 $data["EXTRAS"] = $request[self::EVENT_FIELD_NAME][$id]["EXTRAS"];
577 }
578 if (!empty($data))
579 return $data;
580 }
581 return null;
582 }
587 public function check(array $ballot)
588 {
589 $questions = $this->vote->getQuestions();
590 $fields = array();
591 $data = (array_key_exists("BALLOT", $ballot) ? $ballot["BALLOT"] : []);
592 $message = (array_key_exists("MESSAGE", $ballot) ? $ballot["MESSAGE"] : []);
593 foreach ($questions as $questionId => $question)
594 {
595 if (array_key_exists($question["ID"], $data) && is_array($data[$question["ID"]]))
596 {
597 $answers = array_intersect_key($data[$question["ID"]], $question["ANSWERS"]);
598 if ($question["FIELD_TYPE"] === QuestionTypes::COMPATIBILITY && array_key_exists($question["ID"], $message))
599 {
600 foreach($message[$question["ID"]] as $id => $value)
601 {
602 $value = trim($value);
603 if ($value <> '')
604 {
605 $answers[$id] = true;
606 }
607 }
608 }
609 if (!empty($answers))
610 {
611 //region this code should not exists
612 if ($question["FIELD_TYPE"] == QuestionTypes::COMPATIBILITY)
613 {
614 $singleVal = array(AnswerTypes::RADIO => false, AnswerTypes::DROPDOWN => false);
615 $res = [];
616 foreach ($answers as $id => $value)
617 {
618 $answer = $question["ANSWERS"][$id];
619 switch ($answer["FIELD_TYPE"])
620 {
621 case AnswerTypes::RADIO :
622 case AnswerTypes::DROPDOWN :
623 if (!$singleVal[$answer["FIELD_TYPE"]])
624 {
625 $singleVal[$answer["FIELD_TYPE"]] = true;
626 $res[$id] = $value;
627 }
628 break;
629 default :
630 $res[$id] = $value;
631 break;
632 }
633 }
634 if (!empty($res))
635 {
636 $fields[$question["ID"]] = $res;
637 }
638 }
639 //endregion
640 else if ($question["FIELD_TYPE"] == QuestionTypes::RADIO ||
641 $question["FIELD_TYPE"] == QuestionTypes::DROPDOWN)
642 {
643 $val = reset($answers);
644 $fields[$question["ID"]] = array(
645 key($answers) => $val
646 );
647 }
648 else
649 {
650 $fields[$question["ID"]] = $answers;
651 }
652 //region Check for message text from form
653 $res = $fields[$question["ID"]];
654 if (array_key_exists($question["ID"], $message))
655 {
656 $message[$question["ID"]] = is_array($message[$question["ID"]]) ? $message[$question["ID"]] : [];
657 foreach ($fields[$question["ID"]] as $id => $value)
658 {
659 if (array_key_exists($id, $message[$question["ID"]]))
660 $fields[$question["ID"]][$id] = trim($message[$question["ID"]][$id]);
661 }
662 }
663 if (empty($fields[$question["ID"]]))
664 {
665 unset($fields[$question["ID"]]);
666 }
667 //endregion
668 }
669 }
670 if ($question['REQUIRED'] === 'Y' && $question['ACTIVE'] === 'Y' && !array_key_exists($question["ID"], $fields))
671 {
672 $this->errorCollection->add(array(new Error(Loc::getMessage("VOTE_REQUIRED_MISSING"), "QUESTION_".$questionId)));
673 }
674 }
675 if (empty($fields))
676 $this->errorCollection->add(array(new Error(Loc::getMessage("USER_VOTE_EMPTY"), "VOTE_".$this->vote->getId())));
677
678 return $fields;
679 }
680
681 public function add(array $eventFields, array $ballot, $setCounter = true): ?EventResult
682 {
683 $this->errorCollection->clear();
684 $fields = $this->check($ballot);
685 if (!$this->errorCollection->isEmpty())
686 {
687 return null;
688 }
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")
697 );
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");
700
701 // Compatibility
702 $sqlAnswers = array();
703 foreach ($fields as $questionId => $fieldsAnswer)
704 {
705 foreach ($fieldsAnswer as $answerId => $value)
706 {
707 $sqlAnswers[$questionId][$answerId] = array(
708 "ANSWER_ID" => $answerId,
709 "MESSAGE" => is_string($value)? mb_substr($value, 0, 2000) : "");
710 }
711 }
712
713 /***************** Event onBeforeVoting ****************************/
714 foreach (GetModuleEvents("vote", "onBeforeVoting", true) as $event)
715 {
716 if (ExecuteModuleEventEx($event, array(&$eventFields, &$sqlAnswers)) === false)
717 {
718 $this->errorCollection->add(array(new Error("onBeforeVoting error", "VOTE_".$eventFields["VOTE_ID"])));
719 return null;
720 }
721 }
722 /***************** /Event ******************************************/
723 if (!empty($sqlAnswers) && ($eventId = EventTable::add($eventFields)->getId()) && $eventId > 0)
724 {
725 $ids = array();
726 $answerIdsForCounter = array();
727 foreach ($sqlAnswers as $questionId => $fieldsAnswer)
728 {
729 if (($eventQId = EventQuestionTable::add(array("EVENT_ID" => $eventId, "QUESTION_ID" => $questionId))->getId()) && $eventQId > 0)
730 {
731 $ids[$questionId] = [
732 "EVENT_ID" => $eventQId,
733 "ANSWERS" => []
734 ];
735 foreach ($fieldsAnswer as $answerId => $res)
736 {
737 if (($eventAId = EventAnswerTable::add(array(
738 "EVENT_QUESTION_ID" => $eventQId,
739 "ANSWER_ID" => $res["ANSWER_ID"],
740 "MESSAGE" => $res["MESSAGE"]))->getId()
741 ) && $eventAId > 0)
742 {
743 $ids[$questionId]["ANSWERS"][$answerId] = [
744 "EVENT_ID" => $eventAId,
745 "EVENT_QUESTION_ID" => $eventQId,
746 "ANSWER_ID" => $res["ANSWER_ID"],
747 "MESSAGE" => $res["MESSAGE"]
748 ];
749 $answerIdsForCounter[] = $answerId;
750 }
751 }
752 if (empty($ids[$questionId]))
753 {
754 EventQuestionTable::delete($eventQId);
755 unset($ids[$questionId]);
756 }
757 }
758 }
759
760 if (!empty($ids))
761 {
762 if ($setCounter)
763 {
764 VoteTable::setCounter(array($this->vote->getId()), true);
765 QuestionTable::setCounter(array_keys($ids), true);
766 AnswerTable::setCounter($answerIdsForCounter, true);
767 }
768
769 return new EventResult(array(
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"],
778 "BALLOT" => $ids
779 ));
780 }
781 EventTable::delete($eventId);
782 }
783 return null;
784 }
785}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static deleteEvent($eventId)
Definition event.php:464
static getExtrasFieldName($id, $name)
Definition event.php:530
static getMessageFieldName($id, $questionId, $answerId)
Definition event.php:526
const EVENT_FIELD_EXTRAS_TEMPLATE
Definition event.php:210
const EVENT_FIELD_MESSAGE_TEMPLATE
Definition event.php:209
static getDataFromRequest($id, array $request)
Definition event.php:535
static calculateStatistic($voteId)
Definition event.php:228
add(array $eventFields, array $ballot, $setCounter=true)
Definition event.php:681
static resetStatistic($voteId)
Definition event.php:340
const EVENT_FIELD_BALLOT_TEMPLATE
Definition event.php:208
static getFieldName($id, $questionId)
Definition event.php:522
__construct(\Bitrix\Vote\Vote $vote)
Definition event.php:218
check(array $ballot)
Definition event.php:587
static setValid($eventId, $valid)
Definition event.php:483
const EVENT_FIELD_NAME
Definition event.php:207
static getTableName()
Definition event.php:55