Version 4.1.19

This commit is contained in:
Neo committed 2025-12-19 05:38:56 -08:00
1 parent 28bd025b35
commit 2bd5025018
2674 files changed
+233518 -77766

No files matched your search

+88 -16
View File
@@ -2,9 +2,9 @@
/**
* @brief Poll Model
* @author <a href='http://www.invisionpower.com'>Invision Power Services, Inc.</a>
* @copyright (c) 2001 - SVN_YYYY Invision Power Services, Inc.
* @copyright (c) 2001 - 2016 Invision Power Services, Inc.
* @license http://www.invisionpower.com/legal/standards/
* @package IPS Social Suite
* @package IPS Community Suite
* @since 10 Jan 2014
* @version SVN_VERSION_NUMBER
*/
@@ -155,8 +155,11 @@ class _Poll extends \IPS\Patterns\ActiveRecord implements \SplSubject
$config->set( 'HTML.AllowedElements', 'a,img' );
} ), '<a><img>' );
/* AllowedElements strips <___base_url___> */
$answerData['value'] = preg_replace( '#(<img\s+?src=[\'"])___base_url___/#', '\1<___base_url___>/', $answerData['value'] );
$count = isset( $existing[ $k ]['votes'][ $answerId ] ) ? $existing[ $k ]['votes'][ $answerId ] : 0;
if ( trim( $answerData['value'] ) )
if ( trim( $answerData['value'] ) !== "" )
{
$choices[ $k ]['choice'][ $answerId ] = $answerData['value'];
@@ -165,13 +168,15 @@ class _Poll extends \IPS\Patterns\ActiveRecord implements \SplSubject
$count = ( $answerData['count'] > 0 ? intval( $answerData['count'] ) : 0 );
}
}
$this->votes += $count;
$choices[ $k ]['votes'][ $answerId ] = $count;
}
}
}
$this->choices = $choices;
/* Set the number of voters */
$this->recountVotes();
}
/**
@@ -204,6 +209,11 @@ class _Poll extends \IPS\Patterns\ActiveRecord implements \SplSubject
return FALSE;
}
if ( $this->poll_closed )
{
return FALSE;
}
return TRUE;
}
@@ -239,9 +249,10 @@ class _Poll extends \IPS\Patterns\ActiveRecord implements \SplSubject
$this->notify();
/* Log */
$this->votes = 0;
if ( $vote->member_choices !== NULL )
{
$this->votes = 0;
$pollChoices = $this->choices;
foreach ( $vote->member_choices as $key => $value )
{
@@ -259,16 +270,26 @@ class _Poll extends \IPS\Patterns\ActiveRecord implements \SplSubject
$pollChoices[ $key ]['votes'][$value]++;
}
}
$numberOfVotes = array_sum( $pollChoices[ $key ]['votes'] );
if ( $numberOfVotes > $this->votes )
{
$this->votes = $numberOfVotes;
}
}
$this->choices = $pollChoices;
$this->recountVotes();
$this->save();
}
}
/**
* Recount the total votes for the poll.
* A vote is a single "submit" regardless of how many questions there are.
*
* @note This requires that $this->choices have the votes stored in it
* @note This method does not call save()...be sure you do that yourself
* @return void
*/
public function recountVotes()
{
/* Reset the cached count */
$this->votes = \IPS\Db::i()->select( 'COUNT(*)', 'core_voters', array( 'poll=?', $this->pid ) )->first();
}
/**
* Get Votes
@@ -290,6 +311,11 @@ class _Poll extends \IPS\Patterns\ActiveRecord implements \SplSubject
return $iterator;
}
/**
* @brief Vote Cache
*/
protected $_voteCache = array();
/**
* Get Vote
*
@@ -301,10 +327,21 @@ class _Poll extends \IPS\Patterns\ActiveRecord implements \SplSubject
$member = $member ?: \IPS\Member::loggedIn();
try
{
return \IPS\Poll\Vote::load( $member->member_id, 'member_id', array( 'poll=?', $this->pid ) );
if ( !isset( $this->_voteCache[ $member->member_id ] ) )
{
$this->_voteCache[ $member->member_id ] = \IPS\Poll\Vote::load( $member->member_id, 'member_id', array( 'poll=?', $this->pid ) );
}
if ( $this->_voteCache[ $member->member_id ] === FALSE )
{
return NULL;
}
return $this->_voteCache[ $member->member_id ];
}
catch ( \OutOfRangeException $e )
{
$this->_voteCache[ $member->member_id ] = FALSE;
return NULL;
}
}
@@ -319,8 +356,8 @@ class _Poll extends \IPS\Patterns\ActiveRecord implements \SplSubject
try
{
/* Pre IPS4 data can be bad */
if ( !count( $this->choices ) )
{
if ( !is_array( $this->choices ) || !count( $this->choices ) )
{
return '';
}
@@ -343,7 +380,7 @@ class _Poll extends \IPS\Patterns\ActiveRecord implements \SplSubject
{
/* If a vote was submitted but we're returning HTML, that means there was an error (probably a choice not selected for
a question) so we return a 500 error code to make the form submit properly rather than showing "Your vote has been saved" */
\IPS\Output::i()->sendOutput( $output, ( $this->buildForm() and ! isset( \IPS\Request::i()->viewResults ) ) ? 500 : 200, 'text/html' );
\IPS\Output::i()->sendOutput( $output, ( $this->buildForm() and $this->formSaved === FALSE and ! isset( \IPS\Request::i()->viewResults ) ) ? 500 : 200, 'text/html' );
}
return $output;
@@ -352,6 +389,10 @@ class _Poll extends \IPS\Patterns\ActiveRecord implements \SplSubject
{
\IPS\IPS::exceptionHandler( $e );
}
catch ( \Throwable $e )
{
\IPS\IPS::exceptionHandler( $e );
}
}
/**
@@ -377,6 +418,11 @@ class _Poll extends \IPS\Patterns\ActiveRecord implements \SplSubject
return FALSE;
}
/**
* @brief Flag if the form has been saved so we don't resave votes
*/
protected $formSaved = FALSE;
/**
* Build Form
@@ -400,8 +446,9 @@ class _Poll extends \IPS\Patterns\ActiveRecord implements \SplSubject
$form->add( $input );
}
if ( $values = $form->values() )
if ( $values = $form->values() AND !$this->formSaved )
{
$this->formSaved = TRUE;
$this->addVote( \IPS\Poll\Vote::fromForm( $values ) );
return '';
}
@@ -457,4 +504,29 @@ class _Poll extends \IPS\Patterns\ActiveRecord implements \SplSubject
$v->update( $this );
}
}
/**
* Get output for API
*
* @return array
* @apiresponse int id ID number
* @apiresponse string title Title
* @apiresponse int votes Number of votes
* @apiresponse [\IPS\Poll\Question] questions The questions
*/
public function apiOutput()
{
$questions = array();
foreach ( $this->choices as $choice )
{
$questions[] = ( new \IPS\Poll\Question( $choice ) )->apiOutput();
}
return array(
'id' => $this->pid,
'title' => $this->poll_question,
'votes' => $this->votes,
'questions' => $questions
);
}
}