<?php // -*-php-*-
/*
* Original module by Alaa Abd El Fatah.
* Mohammed Sameer: My Modifications Copyright (c) 2004 Mohammed Sameer,
* under the GNU GPL v2 or later.
* Mohammed Sameer: 2004 10 30
* * Ported to drupal 4.5 API.
*/
setlocale(LC_ALL, "ar_EG.UTF-8");
function bidi_help($section) {
$output = "";
switch ($section) {
case 'admin/modules#description':
$output = t("Automaticaly sets line direction");
break;
}
return $output;
}
function bidi_filter($op, $delta = 0, $format = -1, $text = "") {
switch ($op) {
case 'list':
return array(0 => t('BiDi'));
// case "name":
// return t("Bidi filter");
case 'description':
return t("Bidi filter");
case "process":
return _bidi_filter_process($text);
case "settings":
return _bidi_filter_settings($text);
default:
return $text;
}
}
//get dir needs to be split into a single include file so it can be used on theme and other places
function _get_dir($text) {
$text = strtolower($text);
$len = strlen($text);
$arabic = 0;
$english = 0;
//constants
$a = ord('a');
$z = ord('z');
//arabic UTF-8 letters have one of these values in the 1st byte
$ar1 = 0xd8;
$ar2 = 0xd9;
// this calculates the dominant language in the text, most bidi implementations just use the first letter (which may be more efficient)
for ($i = 0; $i < $len; ++$i) {
$bin = ord($text[$i]);
if ($bin == $ar1 || $bin == $ar2) {
++$arabic;
++$i;
} else if ($bin >= $a && $bin <= $z) {
++$english;
}
}
//should this return a bool instead?
if ($english || $arabic) {
if ($english > $arabic)
return ' class="left2right"';
else
return ' class="right2left"';
} else {
return "";
}
}
function _bidi_filter_process($text) {
$output = "";
$len = strlen($text);
$i = 0;
while ($i < $len) {
//find next tag
$e = strpos(substr($text, $i), "<");
if ($e === FALSE) break;
$output.=substr($text, $i, $e);
$i += $e;
//find end of next tag
$e = strcspn(substr($text, $i), "/> ");
$tag = substr($text, $i+1, $e-1);
$output.=substr($text, $i, $e);
$i += $e;
switch ($tag) {
//single tag entity
case "img":
case "br":
case "hr":
continue;
break;
//nested cases
case "ul":
case "ol":
case "dl":
case "div":
$e = 0;
$j = 1; //stack counter
// if $j = 0 then stack is empty
while ($j > 0) {
$e += strpos(substr($text, $i+$e) , $tag);
//yeah I know I'm assuming certain sizes for tags, sue me
if ($text[$i+$e-1] == '<') { //new nested block
++$j;
} else if ($text[$i+$e-1] == '/') { //end of block
--$j;
}
$e += 2; //skip current tag
}
$content = substr($text, $i+1, $e);
$output.=_get_dir(strip_tags($content));
$e += strlen($tag);
$output.= substr($text, $i, $e);
$i += $e;
break;
//block elements these get out attention
case "p":
case "blockquote":
case "pre":
case "h1":
case "h2":
case "h3":
case "h4":
case "h5":
case "h6":
//nested lists are proken better think of a fix
case "li":
$close_tag = "</".$tag.">";
//breaks on extra whitespaces, maybe I should use regexps instead
$e = strpos(substr($text, $i), $close_tag);
$content = substr($text, $i+1, $e-1);
//calculate directions
$output.= _get_dir(strip_tags($content));
$e += strlen($close_tag);
$output.= substr($text, $i, $e);
$i += $e;
break;
//non block tags we are not handeling
default:
$close_tag = "</".$tag.">";
$e = strpos(substr($text, $i), $close_tag) + strlen($close_tag);
$output.= substr($text, $i, $e);
$i += $e;
}
}
//any missing content
$output .= substr($text, $i);
return $output;
}
function _bidi_filter_settings() {
return form_group(t("Bidi filter"), t("automatic bidi"));
}
function bidi_filter_tips() {
if ($long)
{
}
else {
return t("You may write mixed Arabic and English freely, line direction will be computed automaticaly");
}
}
?>
This is a patch I made for drupal's captcha module.
Since the captcha can only be read using graphical browsers, I thought of this idea to enable users with textish browsers to read the captcha code too.
It's not secure though. Since the captcha code gets printed in a HTML tag (that's how textish browsers can read it), a script can read the code and bypass your captcha protection. I don't recommend applying the patch if you are going to use captcha in anything other than fighting spam comments :).
captcha.module.patch
--- captcha.module.orig 2005-01-19 17:00:08.000000000 +0200
+++ captcha.module 2005-03-05 00:12:03.921875000 +0200
@@ -83,9 +83,10 @@
if (_captcha_istrue("captcha_user_register") && !$newuser->uid && !$user->uid)
switch ($type) {
case t("register"):
- // Add two items to the resigtration form.
+ $string = _captcha_code();
- $output .= form_item("", '<img src="'.url('captcha/image/'.time()).'" alt="Captcha Image: you will need to recognize the text in it."/>');
+ // Add two items to the resigtration form.
+ $output .= form_item("", '<img src="'.url('captcha/image/'.time()).'" alt="Captcha: '.$string.'"/>');
$output .= form_textfield(t('Word'), 'captchaword', NULL, 15, 15, 'Please type in the letters/numbers that are shown in the image above.', NULL, TRUE);
return array(array('title' => t('Verify Registration'), 'data'=>$output));
@@ -137,7 +138,8 @@
case 'form':
if (sess_read('captcha_comment_correct')!='ok') {
- $output .= form_item("", '<img src="'.url('captcha/image/'.time()).'" alt="Captcha Image: you will need to recognize the text in it."/>');
+ $string = _captcha_code();
+ $output .= form_item("", '<img src="'.url('captcha/image/'.time()).'" alt="Captcha: '.$string.'"/>');
$output .= form_textfield(t('Word'), 'captchaword', NULL, 15, 15, 'Please type in the letters/numbers that are shown in the image above.', NULL, TRUE);
return form_group(t('Verify comment authorship'), $output);
} else return NULL;
@@ -206,7 +208,7 @@
header('Content-type: image/png');
- $string = _captcha_code();
+ $string = sess_read('captcha');
//set up image, the first number is the width and the second is the height
$im = imagecreatetruecolor(120, 20);
<?php /* -*-php-*- */
/*
* Diff module for drupal.
* Copyright (c) 2005 Mohammed Sameer.
* This module is distributed under the GPL.
*/
function diff_help($section) {
switch ($section) {
case 'admin/help#diff':
case 'admin/modules#description':
case 'admin/diff':
return "Show difference between a revision and the 1st revision.";
}
}
/**
* Implementation of hook_menu().
*/
function diff_menu($may_cache) {
$items = array();
/* if ($may_cache) {
}
*/
if (arg(0) == 'node' && is_numeric(arg(1)))
{
$node = node_load(array('nid' => arg(1)));
if ($node->nid)
{
if ($node->revisions)
{
$items[] = array('path' => 'node/'. arg(1) .'/diff', 'title' => t('diff'),
'callback' => 'diff_page',
'access' => user_access('access content'),
'type' => MENU_LOCAL_TASK);
}
}
}
return $items;
}
/**
* Show a difference between revisions.
*/
function diff_page($rid1=false, $rid2=false)
{
if ($rid1 !== false && $rid2 !== false)
{
_diff_show_revision(arg(1), $rid1, $rid2);
}
else
{
_diff_show_page(arg(1));
}
}
function _diff_show_revision($nid, $rid1, $rid2 )
{
$node = node_load(array('nid' => $nid));
$r1 = node_revision_load($node, $rid1);
if ($rid2="current") {
$r2 = & $node;
} else {
$r2 = node_revision_load($node, $rid2);
}
include_once 'Text/Diff.php';
include_once 'Text/Diff/Renderer.php';
include_once 'Text/Diff/Renderer/inline.php';
$diff = &new Text_Diff(explode("\n", $r1->body), explode("\n",$r2->body));
$renderer = &new Text_Diff_Renderer_inline();
$node = $r1;
$r1->body = $renderer->render($diff);
$r1 = node_prepare($r1);
print theme ('page', theme('node', $r1));
}
function _diff_show_page($nid)
{
$node = node_load(array('nid' => $nid));
drupal_set_title($node->title);
$header = array(t('Older revisions'), t('Operations'));
$last_key = count($node->revisions) - 1;
foreach ($node->revisions as $key => $revision)
{
if ($key != 0) {
$prev = l(t('previous'), "node/$node->nid/diff/$key/".($key-1));
$first = l(t('first'), "node/$node->nid/diff/$key/0");
}
$last = l(t('last'), "node/$node->nid/diff/$key/current");
$rows[] = array(t('revision #%r revised by %u on %d', array('%r' => $key, '%u' => format_name(user_load(array('uid' => $revision['uid']))), '%d' => format_date($revision['timestamp'], 'small'))) . ($revision['history'] ? '<br /><small>'. $revision['history'] .'</small>' : ''), $prev, $first, $last);
}
$output .= theme('table', $header, $rows);
print theme('page', $output);
}
?>
Hello guys,
I've ported our eglug.org's lincount and rankvote modules to drupal 4.6 API. So we can move to drupal 4.6 once it's out. I've attached them. You will also find a "comments" file where I wrote some comments I thought they should be reported.
I will also port BiDi module to drupal 4.6 and hopefully write a better version of it.
The ported modules should also be available in alaa's sandbox soon.
- Amr
<?php // -*-php-*-
/*
* Original module by Alaa Abd El Fatah.
* Modifications to enclude the admin configuration by Mohammed Sameer.
* Copyright (c) 2004 Mohammed Sameer, All rights reserved.
* Mohammed Sameer: My Modifications are under the GNU GPL v2 or later.
* Mohammed Sameer: 2004 09 10:
* * Output is themed by drupal.
* * Don't modify the variables if you can't open the file.
* * The "more..." link should include the country ISO code.
* * s/||/|/ between the 2 links at the bottom per alaa's request.
* 2004 10 29L
* * Ported to drupal 4.5 API.
*/
function lincount_help($section) {
switch ($section) {
case 'admin/modules#description':
$output = "<p>displays a block with the current Egypt statistics from Linux Counter Project</p>";
break;
}
return $output;
}
function lincount_cron() {
$file = 'http://counter.li.org/reports/short.txt';
$data = file_get_contents($file);
if ($data == FALSE)
{
return;
}
$country = variable_get("lincount_country", "EG");
$matches = array();
preg_match("|([0-9]+)\s*$country\s*([0-9a-zA-Z]+)\s*([0-9]+)\s*([0-9]+)\s*([0-9]+)\s*([0-9]+\.[0-9]+)\s*([0-9]+.[0-9]+)|", $data, $matches);
$rank = $matches[1];
$country = $matches[2];
$users = $matches[3] + $matches[4];
$machines = $matches[5];
$userdensity = $matches[6];
variable_set("lincount_rank", $rank);
variable_set("lincount_users", $users);
variable_set("lincount_machines", $machines);
variable_set("lincount_userdensity", $userdensity);
variable_set("lincount_country_long", $country);
}
function lincount_block($op = "list", $delta = 0) {
if ($op == "list") {
$blocks[0]["info"] = t("Linux Counter Statistics");
return $blocks;
}
else {
$country = variable_get("lincount_country_long", "Egypt");
$block["subject"] = t("Linux Counter %s Statistics",array("%s" => $country));
$block["content"] = lincount_display_block();
return $block;
}
}
function lincount_display_block() {
$rank = variable_get("lincount_rank",0);
$users = variable_get("lincount_users",0);
$machines = variable_get("lincount_machines",0);
$userdensity = variable_get("lincount_userdensity",0);
$country = variable_get("lincount_country","EGY");
$lincount_array[] = "Country Rank: $rank";
$lincount_array[] = "Number Of Users: $users";
$lincount_array[] = "Number Of Machines: $machines";
$lincount_array[] = "User Density: $userdensity";
$output = theme_node_list($lincount_array);
$output .= "<br />";
$output_array[] = l("Get Counted","http://counter.li.org/enter-person.php");
$output_array[] = l("more...","http://counter.li.org/reports/arearank.php?area=$country");
$output .= "<div class=\"more-link\">";
$output .= theme_links($output_array, ' | ');
$output .= "</div>";
return $output;
}
function lincount_settings()
{
$country = variable_get("lincount_country", "EG");
$output .= form_textfield(t("Enter the ISO code for the country:"), "lincount_country", $country, 55, 100);
return $output;
}
?>
<?php
/**
* @file
* Enables your site members to vote on decisions using a simple ranking voting system
*/
/**
* Implementation of hook_help().
*/
function rankvote_help($section) {
switch($section) {
case 'admin/help#rankvote':
return t("<p>some text to help admins</p>");
case 'admin/modules#description':
return t("Enables your site members to vote on decisions using a simple ranking vote system");
case 'node/add#rankvote':
return t("A Ranking Vote is a form where visitors can rank each option");
}
}
/**
* Implementation of hook_access().
*/
function rankvote_access($op, $node) {
if ($op == 'create') {
return user_access('create ranking votes');
}
}
/**
* Implementation of hook_block().
*
* Generates a block containing the latest rankvote.
*/
function rankvote_block($op = 'list', $delta = 0) {
if (user_access('access content')) {
if ($op == 'list') {
$blocks[0]['info'] = t('Most recent Ranking Vote');
return $blocks;
}
else {
// Retrieve the latest rankvote.
$timestamp = db_result(db_query('SELECT MAX(n.created) FROM {node} n '. node_access_join_sql() ." WHERE n.type = 'rankvote' AND n.status = 1 AND ". node_access_where_sql() .' AND n.moderate = 0'));
if ($timestamp) {
$rankvote = node_load(array('type' => 'rankvote', 'created' => $timestamp, 'moderate' => 0, 'status' => 1));
if ($rankvote->nid) {
// rankvote_view() dumps the output into $rankvote->body.
rankvote_view($rankvote, 1, 0, 1);
}
}
$block['subject'] = t('Ranking Vote');
$block['content'] = $rankvote->body;
return $block;
}
}
}
/**
* Implementation of hook_cron().
*
* Closes rankvotes that ave exceeded their allowed runtime.
*/
function rankvote_cron() {
$result = db_query("SELECT p.nid FROM {rankvote} p INNER JOIN {node} n ON p.nid=n.nid WHERE (n.created + p.runtime) < '". time() ."' AND p.active = '1' AND p.runtime != '0'");
while ($rankvote = db_fetch_object($result)) {
db_query("UPDATE {rankvote} SET active='0' WHERE nid = %d", $rankvote->nid);
}
}
/**
* Implementation of hook_delete().
*/
function rankvote_delete($node) {
db_query("DELETE FROM {rankvote} WHERE nid=%d", $node->nid);
db_query("DELETE FROM {rankvote_choices} WHERE nid = %d", $node->nid);
}
/**
* Implementation of hook_validate().
*/
function rankvote_validate(&$node) {
if (isset($node->title)) {
// Check for at least two options and validate amount of votes:
$realchoices = 0;
foreach($node->choice as $i => $choice) {
if ($choice['chtext'] != '') {
$realchoices++;
}
if ($choice['chvotes'] < 0) {
form_set_error("choice][$i][chvotes", t('Negative values are not allowed.'));
}
}
if ($realchoices < 2) {
form_set_error("choice][$realchoices][chtext", t('You must fill in at least two choices.'));
}
}
$node->teaser = rankvote_teaser($node);
}
/**
* Implementation of hook_form().
*/
function rankvote_form(&$node) {
$admin = user_access('administer nodes');
if (function_exists('taxonomy_node_form')) {
$output = implode('', taxonomy_node_form('rankvote', $node));
}
if (!isset($node->choices)) {
$node->choices = max(2, count($node->choice) ? count($node->choice) : 5);
}
// User ticked 'need more choices'.
if ($node->morechoices) {
$node->choices *= 2;
}
$output .= '<div class="rankvote-form">';
// RankVote choices
//$opts = drupal_map_assoc(range(2, $node->choices * 2 + 5));
for ($a = 0; $a < $node->choices; $a++) {
$group1 .= form_textfield(t('Choice %n', array('%n' => ($a + 1))), "choice][$a][chtext", $node->choice[$a]['chtext'], 50, 127);
if ($admin) {
$group1 .= form_textfield(t('Votes for choice %n', array('%n' => ($a + 1))), "choice][$a][chvotes", (int)$node->choice[$a]['chvotes'], 7, 7);
}
}
$group1 .= form_hidden('choices', $node->choices);
$group1 .= form_checkbox(t('Need more choices'), 'morechoices', 1, 0, t("If the amount of boxes above isn't enough, check this box and click the Preview button below to add some more."));
$output .= form_group(t('Choices'), $group1);
// RankVote attributes
$_duration = array(0 => t('Unlimited')) + drupal_map_assoc(array(86400, 172800, 345600, 604800, 1209600, 2419200, 4838400, 9767800, 31536000), "format_interval");
$_active = array(0 => t('Closed'), 1=> t('Active'));
if ($admin) {
$group2 .= form_radios(t('Ranking Vote status'), 'active', isset($node->active) ? $node->active : 1, $_active, t('When a Ranking Vote is closed, visitors can no longer vote for it.'));
}
$group2 .= form_select(t('Ranking Vote duration'), 'runtime', $node->runtime ? $node->runtime : 0, $_duration, t('After this period, the Ranking Vote will be closed automatically.'));
$output .= form_group(t('Settings'), $group2);
$output .= '</div>';
return $output;
}
/**
* Implementation of hook_insert().
*/
function rankvote_insert($node) {
if (!user_access('administer nodes')) {
// Make sure all votes are 0 initially
foreach ($node->choice as $i => $choice) {
$node->choice[$i]['chvotes'] = 0;
}
$node->active = 1;
}
db_query("INSERT INTO {rankvote} (nid, runtime, voters, active) VALUES (%d, %d, '', %d)", $node->nid, $node->runtime, $node->active);
$i=0;
foreach ($node->choice as $choice) {
if ($choice['chtext'] != '') {
db_query("INSERT INTO {rankvote_choices} (nid, chtext, chvotes, chorder) VALUES (%d, '%s', %d, %d)", $node->nid, $choice['chtext'], $choice['chvotes'], $i++);
}
}
}
/**
* Implementation of hook_link().
*/
function rankvote_link($type, $node = 0, $main) {
$links = array();
if ($type == 'page' && user_access('access content')) {
$links[] = l(t('votes'), 'rankvote', array('title' => t('View the list of Ranking Votes on this site.')));
}
return $links;
}
/**
* Implementation of hook_menu().
*/
function rankvote_menu($may_cache) {
$items = array();
if($may_cache) {
$items[] = array('path' => 'node/add/rankvote', 'title' => t('ranking vote'),
'access' => user_access('create ranking votes'));
$items[] = array('path' => 'rankvote', 'title' => t('ranking votes'),
'callback' => 'rankvote_page',
'access' => user_access('access content'),
'type' => MENU_SUGGESTED_ITEM);
$items[] = array('path' => 'rankvote/vote',
'title' => t('vote'),
'callback' => 'rankvote_vote',
'access' => user_access('vote on ranking votes'),
'type' => MENU_CALLBACK);
}
else {
if (arg(0) == 'node' && is_numeric(arg(1))) {
$node = node_load(array('nid' => arg(1)));
if ($node->type == 'rankvote' && $node->allowvotes) {
$items[] = array('path' => 'node/'. arg(1) .'/results',
'title' => t('results'),
'callback' => 'rankvote_results',
'access' => user_access('access content'),
'weight' => 3,
'type' => MENU_LOCAL_TASK);
}
}
}
return $items;
}
/**
* Determine an adjusted user id, to allow for basic tracking of anonymous
* users (IP-based).
*/
function rankvote_uid() {
global $user;
if ($user->uid) {
// Pad the UID with underscores to allow a simple strstr() search
$id = '_'. $user->uid .'_';
}
else {
$id = $_SERVER['REMOTE_ADDR'];
}
return $id;
}
/**
* Implementation of hook_load().
*/
function rankvote_load($node) {
// Load the appropriate choices into the $node object
$rankvote = db_fetch_object(db_query("SELECT runtime, voters, active FROM {rankvote} WHERE nid = %d", $node->nid));
$result = db_query("SELECT chtext, chvotes, chorder FROM {rankvote_choices} WHERE nid=%d ORDER BY RAND()", $node->nid);
while ($choice = db_fetch_array($result)) {
$rankvote->choice[$choice['chorder']] = $choice;
}
// Determine whether or not this user is allowed to vote
$rankvote->allowvotes = false;
if (user_access('vote on ranking votes')) {
if(!strstr($rankvote->voters, rankvote_uid())) {
$rankvote->allowvotes = $rankvote->active;
}
}
return $rankvote;
}
/**
* Implementation of hook_node_name().
*/
function rankvote_node_name($node) {
return t("Ranking Vote");
}
function rankvote_page() {
// List all Ranking Votes
$result = pager_query("SELECT DISTINCT(n.nid), n.title, p.active, n.created, SUM(c.chvotes) AS votes FROM {node} n ". node_access_join_sql() ." INNER JOIN {rankvote} p ON n.nid=p.nid INNER JOIN {rankvote_choices} c ON n.nid=c.nid WHERE type = 'rankvote' AND status = 1 AND ". node_access_where_sql() ." AND moderate = 0 GROUP BY n.nid, n.title, p.active, n.created ORDER BY n.created DESC", 15);
$output = '<ul>';
while ($node = db_fetch_object($result)) {
$output .= '<li>'. l($node->title, "node/$node->nid") .' - '. format_plural($node->votes, '1 vote', '%count votes') .' - '. ($node->active ? t('open') : t('closed')) . '</li>';
}
$output .= '</ul>';
$output .= theme("pager", NULL, 15);
print theme('page', $output);
}
/**
* Implementation of hook_perm().
*/
function rankvote_perm() {
return array('create ranking votes', 'vote on ranking votes');
}
/**
* Creates a simple teaser that lists all the choices.
*/
function rankvote_teaser($node) {
if (is_array($node->choice)) {
foreach ($node->choice as $k => $choice) {
$teaser .= '* '. $choice['chtext'] .'\n';
}
}
return $teaser;
}
/**
* Generates the voting form for a ranking vote.
*/
function rankvote_view_voting(&$node, $main, $page, $block) {
$output = '<div class="rankvote">';
$form = '<div class="vote-form">';
$form .= '<div class="choices">';
if ($node->choice) {
$options = drupal_map_assoc(range(1, count($node->choice)));
foreach ($node->choice as $i => $choice) {
$form .= form_select(drupal_specialchars($choice['chtext']), "choice][". $choice['chorder'], -1, $options, '', 0, false, true);
}
}
$form .= '</div>';
$form .= form_hidden('nid', $node->nid);
$form .= form_submit(t('Vote'), 'vote') .'</div>';
$output .= form($form, 'post', url('rankvote/vote/'. $node->nid));
$output .= '</div>';
return $output;
}
/**
* Generates a graphical representation of the results of a ranking vote.
*/
function rankvote_view_results(&$node, $main, $page, $block) {
// Display the results
// Count the votes and find the maximum
$temp = array();
foreach($node->choice as $i => $choice) {
$temp[$i] = $choice['chvotes'];
}
asort($temp);
// Output the divs for the text, bars and percentages
$output .= '<div class="rankvote">';
if ($block) {
$output .= '<div class="title">'. $node->title .'</div>';
}
$output .= '<ul>';
foreach($temp as $i => $score) {
$output .='<li>'. drupal_specialchars($node->choice[$i]['chtext']) .' <small>score = '. $node->choice[$i]['chvotes'] . '</small></li>';
}
$output .= '</ul></div>';
return $output;
}
/**
* Callback for the 'results' tab for ranking votes you can vote on
*/
function rankvote_results() {
if ($node = node_load(array('nid' => arg(1)))) {
print theme('page', node_show($node, 0), $node->title);
}
else {
drupal_not_found();
}
}
function _valid_rankvote($choice) {
$temp = array();
foreach($choice as $rank) {
$temp[] = $rank;
}
sort($temp);
for ($i=0; $i < count($temp); ++$i) {
if ($i != $temp[$i]-1) return false;
}
return true;
}
/**
* Callback for processing a vote
*/
function rankvote_vote(&$node) {
$nid = arg(2);
if ($node = node_load(array('nid' => $nid))) {
$edit = $_POST['edit'];
$choice = $edit['choice'];
$vote = $_POST['vote'];
if (isset($choice) && count($choice) == count($node->choice)) { //need to check valid vote data here
if (_valid_rankvote($choice)) {
if ($node->allowvotes) {
$id = rankvote_uid();
$node->voters = $node->voters ? ($node->voters .' '. $id) : $id;
db_query("UPDATE {rankvote} SET voters='%s' WHERE nid = %d", $node->voters, $node->nid);
foreach($choice as $i => $rank) {
db_query("UPDATE {rankvote_choices} SET chvotes = chvotes + %d WHERE nid = %d AND chorder = %d", $rank, $node->nid, $i);
$node->choice[$i]['chvotes'] += $rank;
}
$node->allowvotes = false;
drupal_set_message(t('Your vote was recorded.'));
}
else {
drupal_set_message(t("You're not allowed to vote on this ranking vote."), 'error');
}
} else {
drupal_set_message(t("Each choice should get a unique rank for the vote to be valid."), 'error');
}
} else {
drupal_set_message(t("All choices must be ranked for the vote to be valid."), 'error');
}
drupal_goto('node/'. $nid);
}
else {
drupal_not_found();
}
}
/**
* Implementation of hook_view().
*
* @param $block
* An extra parameter that adapts the hook to display a block-ready
* rendering of the ranking vote.
*/
function rankvote_view(&$node, $teaser = FALSE, $page = FALSE, $block = FALSE) {
global $user;
$output = '';
if ($node->allowvotes && ($block || arg(2) != 'results')) {
$output .= rankvote_view_voting($node, $main, $page, $block);
}
else {
$output .= rankvote_view_results($node, $main, $page, $block);
}
// Special display for side-block
if ($block) {
// No 'read more' link
$node->body = $node->teaser = '';
$links = link_node($node, $main);
$links[] = l(t('older ranking votes'), 'rankvote', array('title' => t('View the list of ranking votes on the site.')));
if ($node->allowvotes && $block) {
$link[] = l(t('results'), 'node/'. $node->nid .'/results', array('title' => t('View the current ranking vote results.')));
}
$output .= '<div class="links">'. theme("links", $links) .'</div>';
}
$node->body = $node->teaser = $output;
}
/**
* Implementation of hook_update().
*/
function rankvote_update($node) {
db_query('UPDATE {rankvote} SET runtime = %d, active = %d WHERE nid = %d', $node->runtime, $node->active, $node->nid);
db_query('DELETE FROM {rankvote_choices} WHERE nid = %d', $node->nid);
$i=1;
foreach ($node->choice as $choice) {
$chvotes = (int)$choice['chvotes'];
$chtext = $choice['chtext'];
if ($chtext != '') {
db_query("INSERT INTO {rankvote_choices} (nid, chtext, chvotes, chorder) VALUES (%d, '%s', %d, %d)", $node->nid, $chtext, $chvotes, $i++);
}
}
}