[scry] [ENHANCEMENT] New block to display latest poll

Adrian Silaghi adi at tvr.ro
Sat Aug 28 09:12:17 PDT 2004


Hello!

I am running a horde-based intranet on my company and routinely post a poll on
it. So I've hacked a new block (from lib/Block/poll.php) that display's
the latest poll. Just rename the attached file "latest.php" and put it in
lib/Block and you'l have a new
block named Polls:Latest Poll to choose from.

----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.
-------------- next part --------------
<?php

$block_name = _("Latest Poll");

/**
 * Scry LatestPoll View Block Class
 *
 * This file provides an api to include latest Scry poll in any other Horde app
 * through the Horde_Blocks, by extending the Horde_Blocks class.
 *
 * $Horde: scry/lib/Block/latest.php,v 1.0 2004/08/28 15:47:29 sadig Exp $
 *
 * hacked version of poll.php by Marko Djukic <marko at oblo.com>
 *
 * See the enclosed file COPYING for license information (GPL).  If you
 * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
 *
 * @author  Adrian Silaghi <adi at tvr.ro@tvr.ro>
 * @version $Revision: 1.0 $
 * @package Horde_Block
 */
class Horde_Block_scry_latest extends Horde_Block {

    var $_app = 'scry';

    function Horde_Block_scry_latest($params)
    {
        parent::Horde_Block($params);
    }

    function _title()
    {
        @define('SCRY_BASE', dirname(__FILE__) . '/../..');
        require_once SCRY_BASE . '/lib/base.php';
        global $scry;

        $latestpoll = end($scry->getPolls(null, 'open'));
        if (empty($latestpoll)) {
            return _("Poll not found");
        }

        $url = Util::addParameter(Horde::applicationUrl('vote.php', true), 'poll_id', $latestpoll['poll_id']);
        return Horde::link($url, $latestpoll['poll_title'], 'header') . $latestpoll['poll_title'] . '</a>';
    }

    function _content()
    {
        @define('SCRY_BASE', dirname(__FILE__) . '/../..');
        require_once SCRY_BASE . '/lib/base.php';
        require_once SCRY_BASE . '/lib/Vote.php';
        global $prefs, $notification, $scry;

        /* Fetch the last poll and check if available. */
        $latestpoll = end($scry->getPolls(null, 'open'));
        $poll_id = $latestpoll['poll_id'];
        if (!$latestpoll) {
            return _("This poll is not available.");
        }

        /* Set up the form variables. */
        require_once 'Horde/Variables.php';
        $vars = &Variables::getDefaultVariables();
        $form_submitted = ($vars->get('formname') == 'voteform');

        /* Check if form has been submitted for a voteable poll. */
        $is_votable = $scry->isVotable($latestpoll);
        if ($form_submitted && $is_votable) {
            $formvote = &Horde_Form::singleton('VoteForm', $vars);
            if ($formvote->validate($vars)) {
                $formvote->getInfo($vars, $info);
                $results = $scry->logVote($info);
                if (is_a($results, 'PEAR_Error')) {
                    $notification->push(sprintf(_("Voting failed. %s"), $results->getMessage()), 'horde.error');
                } else {
                    $notification->push(_("Thank you for your vote."), 'horde.success');
                    $url = Horde::selfURL(true, false, true);
                    header('Location: ' . $url);
                    exit;
                }
            }
        }

        /* Set up the renderer. */
        $vars = &new Variables($latestpoll);
        $formvote = &Horde_Form::singleton('VoteForm', $vars, '');
        $renderer = &$formvote->getRenderer();

        if (!$is_votable) {
            $formvote->addVariable('', 'results', 'html', false);

            /* Get the current voting results */
            $votes = $scry->getVotes($poll_id);
            $vars->set('results', Scry::getResults($votes));
            $main = Util::bufferOutput(array($formvote, 'renderInactive'), $renderer, $vars);
        } else {
            $main = Util::bufferOutput(array($formvote, 'renderActive'), $renderer, $vars, Horde::selfURL(true), 'post');
        }

        /* Set the main body to the template. */
        $template = &new Horde_Template();
        $template->set('main', $main);

        $html = $template->fetch(SCRY_TEMPLATES . '/block/poll.html');

        return $html;
    }

}


More information about the scry mailing list