ActivityPoll module

Module name: activitypoll
You have to enable this module in site/_modules.php file to use it.

This module gives you the possibility to register various polls to enable users to vote on objects wihch has an unique identifier.

Before the vote
pollonuserbefore.png
After the vote
pollonuser.png

Note: This poll above is generated by the sample code below.

ActivityPoll API

register_poll_container($containername)
Registers a poll container in the system which is a simple string without spaces and any special characters. (Defines a separate sql table for the polls located in this container) You have to call this function to use poll system, so it's practical to put this in an init hook. (See example below)

Note: Registering more poll containers makes the possibility to spread the data into more sql tables.

Note2: After enable the poll container you must visit the sql scheme editor page to satisfy the schema requirements of the module.

register_poll($pollname,$container,$title,$choices,$default = '',$date_start = '',$date_end = '')
Registers a new poll with name $pollname in the $container poll container. It have to call one time only when the poll created. After the call, the poll is stored in the system. The $title parameter contains the text put as description shown together with the poll. The $choices is an associative array contains the possible votes.

unregister_poll($pollname)
Unregisters and completely delete the specified poll from the system.

get_poll_list($container = '',array $search_options = [])
Search between the polls

    /* $search_options = [
                'activeonly' => true | false
                'fromdate' => '2019-01-20'
                'todate' => '2019-02-22'
                'add_voteactive' => true
                'add_votecount' => true //only works when container not empty
                'filled_by_uid' => UID   // need for filled_bysb_order
                'filled_to_ref' => REFID // need for filled_bysb_order
                'sort' => 'titletext'  '-name' or ['-voteactive','votecount','titletext']
            ]
    */

get_poll_choices_array_by_pollname($pollname)
It returns the possible choices of the specified poll.

poll_is_user_voted($pollname,$container,$id,$uid)
Checks whether the specified user take a vote or not in the specified poll. It returns TRUE or FALSE.

get_poll_results($container,$pollname,$id)
It returns the results of the specified poll in an associative array.

get_poll_block($pollname,$id,$maincssclass = '')
Generates a poll block for an object which has unique id with the specified poll. It returns the html codes of the poll block It generates ajax based controls.

Permissions of poll vote

You have to define the poll_access hook to controls who can vote in a specified poll in the system.

The hook receives the following parameters:

You have to return one of the following values from your hook:

Sample code

Enable polls on every user object for every authenticated users:

function hook_mymodule_init()
{
    register_poll_container('userrate');
}

//Set who can vote in a specified poll
function hook_mymodule_poll_access($pollname,$refid,$op,$account)
{
    if($pollname == 'howniceuser')
    {
      if($op == 'view')
          return ACTIVITYPOLL_ACCESS_ALLOW;
      if($account->auth)
          return ACTIVITYPOLL_ACCESS_ALLOW;
      return ACTIVITYPOLL_ACCESS_DENY;
    }
    return ACTIVITYPOLL_ACCESS_IGNORE;
}

//Makes the poll block visible after user page
function hook_mymodule_node_form_after($node,$op)
{
    ob_start();
    if($node->node_type == 'user' && $op != 'add')
        print get_poll_block('howniceuser',$node->node_nid);
    return ob_get_clean();
}

//Have to run once only (When you register the poll)
function register_howniceuser_poll()
{
    register_poll('howniceuser','userrate','How nice this user?',[
        '0' => 'Not nice',
        '1' => 'Average',
        '2' => 'Nice',
        '3' => 'Excellent'],
      '1');
}

Settings

The ActivityPoll module have some settings which can set in site settings.

namedefaultdescription
$site_config->activity_poll_main_css_class "ckpoll_default_style" It specifies the top level css class of the poll block. All default css rules depends on this class name so you can write completely different styles by redefine this string.
$site_config->activity_poll_showpoll_callback "pollresult_generator_default" The callback name of the poll renderer function. You can use your own renderer by change this variable.