<?
##############################################
# Bitrix Site Manager Forum                  #
# Copyright (c) 2002-2009 Bitrix             #
# http://www.bitrixsoft.com                  #
# mailto:admin@bitrixsoft.com                #
##############################################
require_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/vote/classes/general/event.php");

class CVoteEvent extends CAllVoteEvent
{
	function err_mess()
	{
		$module_id = "vote";
		return "<br>Module: ".$module_id."<br>Class: CVoteEvent<br>File: ".__FILE__;
	}

	function GetUserAnswerStat($VOTE_ID, $arParams = array())
	{
		global $DB;
		$err_mess = (CAllVoteEvent::err_mess())."<br>Function: GetUserAnswerStat<br>Line: ";
		$VOTE_ID = intval($VOTE_ID);
		$arParams = (is_array($arParams) ? $arParams : array());
		if ($arParams["bGetUsersStat"] == "Y") {
			$strSql = "SELECT VEQ.QUESTION_ID, VEA.ANSWER_ID, ".($arParams["bGetMemoStat"] == "Y" ? "VEA.MESSAGE, " : "")."
			COUNT(VEA.ID) as COUNTER,
			GROUP_CONCAT(".($arParams["bGetUsersStatDistinct"] == "Y" ? "DISTINCT " : "")."VU.AUTH_USER_ID SEPARATOR ',') as USERS,
			MIN(TIMESTAMPDIFF(SECOND, VE.DATE_VOTE, NOW())) AS LAST_VOTE
			FROM b_vote_event VE
				INNER JOIN b_vote_event_question VEQ ON (VEQ.EVENT_ID = VE.ID)
				INNER JOIN b_vote_event_answer VEA ON (VEA.EVENT_QUESTION_ID = VEQ.ID)
				LEFT JOIN b_vote_user VU ON (VU.ID = VE.VOTE_USER_ID)
			WHERE
				VE.VOTE_ID = ".$VOTE_ID." AND VE.VALID = 'Y' ".
					($arParams["bGetMemoStat"] == "Y" ? " AND VEA.MESSAGE != ' '" : "")."
			GROUP BY
				VEQ.QUESTION_ID, VEA.ANSWER_ID".
					($arParams["bGetMemoStat"] == "Y" ? ", VEA.MESSAGE" : "")."
			ORDER BY COUNTER DESC ";
		} else {
			$strSql =
				"SELECT A.ANSWER_ID, A.MESSAGE, COUNT(A.ID) as COUNTER
			FROM b_vote_event E
				INNER JOIN b_vote_event_question Q ON (Q.EVENT_ID = E.ID)
				INNER JOIN b_vote_event_answer A ON (A.EVENT_QUESTION_ID = Q.ID)
			WHERE
				E.VOTE_ID = ".$VOTE_ID." and E.VALID = 'Y' and A.MESSAGE != ' '
			GROUP BY
				A.ANSWER_ID, A.MESSAGE
			ORDER BY
				COUNTER DESC";
		}
		return $DB->Query($strSql, false, $err_mess.__LINE__);
	}
}
?>