Drupal Doodle Like Content Type sample
Searching for a module with features like doodle, i build one using cck and a custom node.tpl ...
Maybe this can be usefull for someone.
It will allow the authentificated user to vote on multiple date options.
Doodle like content type
We add a news content type. I call it fix_a_date.
This content type has a title and body and a cck field type 'date'.
Widget: Date: Text Field with jquery pop-up calendar if installed
Label: date_choice
On data settings we allow unlimited multiple dates.
Choose your Granularity, i think you do not need Seconds.
On Time Zone choose User's time zone.
Now we can create a content type where the users can put multiple dates
fields used later as choices.
Create one ...
Drupal choice storage table
Now we need the database table for storing the users choices ..
!! hardcoded in the functions below ..
CREATE TABLE IF NOT EXISTS `fix_a_date` (
`nid` int(11) unsigned NOT NULL,
`uname` varchar(255) collate utf8_unicode_ci NOT NULL,
`submitted` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
`remote_addr` varchar(50) collate utf8_unicode_ci NOT NULL,
`fieldname` varchar(80) collate utf8_unicode_ci NOT NULL,
`value` varchar(255) collate utf8_unicode_ci NOT NULL,
`data` varchar(255) collate utf8_unicode_ci NOT NULL,
`uid` int(11) unsigned NOT NULL,
PRIMARY KEY (`nid`,`uid`,`fieldname`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;Doodle like output handling
Drupal now output the node fix_a_date with the given values, we should adapt
the node output to let the user choice dates ...
So we write a custom node template.
Save the following code in your actual theme folder as node-fix_a_date.tpl.php
Maybe adapted for your actual template ... main script are the functions within.
<?php phptemplate_comment_wrapper(NULL, $node->type); ?>
<div id="node-<?php print $node->nid; ?>" class="node<?php if ($sticky) { print ' sticky'; } ?><?php if (!$status) { print ' node-unpublished'; } ?>">
<?php print $picture ?>
<div class="meta">
<?php if ($taxonomy): ?>
<div class="terms"><?php print $terms ?></div>
<?php endif;?>
</div>
<?php if ($page == 0): ?>
<h2><a href="<?php print $node_url ?>" title="<?php print $title ?>"><?php print $title ?></a></h2>
<?php endif; ?>
<?php if ($submitted): ?>
<span class="submitted"><?php print t('!date — !username', array('!username' => theme('username', $node), '!date' => format_date($node->created))); ?></span>
<?php endif; ?>
<?php
global $user;
if(!function_exists('fixAdate_show_user_vote')){
function fixAdate_show_user_vote($nid,$checkfields) {
global $user;
$sql = "SELECT uid,uname,submitted,fieldname,value,data FROM fix_a_date WHERE nid='".$nid."'";
$result = db_query($sql);
while (($row = db_fetch_object($result))) {
$data[$row->uid.".".$row->fieldname] = $row;
$users[$row->uid] = $row->uname; # so they are unique ;=)
}
$out = '';
$out .= '<table cellpadding="4" celllspacing="0" border="0">';
$out .= '<tr>';
$out .= '<th>'.t('User').'</th>';
foreach($checkfields as $k=>$v) {
$out .= '<th>'.$v.'</th>';
}
$out .= '</tr>';
if (count($users)>0) {
foreach($users as $k=>$v) {
$out .= '<tr>';
$out .= '<td>'.$v.'</td>';
foreach($checkfields as $sk=>$sv) {
# foreach($data as $Dk=>$Dv) {
# if ( ($Dv->uid == $k) && ($Dv->data == $v)) {
if ($data[$k.".".$sk]->value == 1) {
$out .= '<td style="background-color:#99FF99" align="center">'.$data[$k.".".$sk]->value.'</td>';
} else {
$out .= '<td align="center">'.$data[$k.".".$sk]->value.'</td>';
}
# }
# }
}
$out .= '</tr>';
}
}
$out .= '</table>';
return $out;
}
}
if(!function_exists('fixAdate_check_user_vote')){
function fixAdate_check_user_vote($nid) {
global $user;
$sql = "SELECT submitted FROM fix_a_date WHERE uid='".$user->uid."' AND nid='".$nid."'";
$result = db_query($sql);
$notfound = 1;
while (($nid = db_fetch_object($result))) {
$notfound = 0;
}
return $notfound;
}
}
if(!function_exists('fixAdate_form')){
function fixAdate_form($checkfields,$nid) {
global $user;
$options = array('1' => t('Enabled'), '0' => t('Disabled'));
$form['#action'] = url('node/'. $nid);
$form['access'] = array(
'#type' => 'fieldset',
'#tree' => TRUE,
'#title' => t('Termin fixieren ...'),
'#prefix' => '<div class="foo">',
'#suffix' => '</div>',
);
$form['access']['user'] = array(
'#weight' => -19,
'#type' => 'textfield',
'#title' => t('User'),
'#value' => $user->name,
'#maxlength' => 64,
'#disabled' => TRUE,
'#size' => 15,
);
$form['access']['uid'] = array(
'#weight' => -19,
'#type' => 'hidden',
'#value' => $user->uid,
);
$form['access']['node'] = array(
'#weight' => -19,
'#type' => 'hidden',
'#value' => $nid,
);
foreach($checkfields as $k=>$cb) {
$form['access']['check'][$k."#".$cb] = array(
'#type' => 'checkbox',
'#title' => $cb,
# '#value' => 1,
);
}
$form['preview'] = array(
'#type' => 'button',
'#value' => t('Preview'),
'#weight' => 19,
);
# print_r($form);
$form['submit'] = array(
'#type' => 'submit',
'#weight' => 20,
'#name' => 'fixAdate_form_submit',
'#value' => t('Teilnehmen'));
return $form;
}
}
$output .= '<div class="description">';
// FIXME :: do not parse html .. can be buggy ..
# <span class="date-display-single">14.04.2008 - 21:12</span>
$regexp = "<span\s[^>]*class=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/span>";
$columns = 1;
if(preg_match_all("/$regexp/siU", $content, $matches, PREG_SET_ORDER)) {
foreach($matches as $match) {
$voteData[count($voteData)] = $match[3];# . strtotime($match[3]);
$columns++;
}
}
// if user has not voted jet ... build a form ...
if (fixAdate_check_user_vote($node->nid)) {
$output .= drupal_get_form('fixAdate_form', $voteData , $node->nid);
}
$output .= fixAdate_show_user_vote($nid,$voteData);
#$output .= $content;
$output .= '</div>';
echo $output;
?>
<div class="clear-block clear">
<div class="meta">
<?php if ($taxonomy): ?>
<div class="terms"><?php print $terms ?></div>
<?php endif;?>
</div>
<?php if ($links): ?>
<div class="links"><?php print $links; ?></div>
<?php endif; ?>
</div>
</div>Data storage handling
Now we adapt our template.php in the defined themes to let drupal
know how to store our fix_a_date users choices ...
function fixAdate_form_submit($form_id, $form_values) {
$uname = $form_values['access']['user'];
$uid = $form_values['access']['uid'];
$nid = $form_values['access']['node'];
$remote = (getenv(HTTP_X_FORWARDED_FOR)) ? getenv(HTTP_X_FORWARDED_FOR) : getenv(REMOTE_ADDR);
foreach($form_values['access']['check'] as $k=>$v) {
list($realKey,$dateStr) = split('#',$k);
$sql = "INSERT INTO fix_a_date (uname,nid,uid,remote_addr,fieldname,value,data) ".
"VALUES ('".$uname."','".$nid."','".$uid."','".$remote."','".$realKey."','".$v."','".$dateStr."')";
db_query($sql);
}
drupal_set_message(t('Your form has been saved.'));
}Possible Dependencies
| Attachment | Size |
|---|---|
| node-fix_a_date.tpl_.php_.txt | 4.9 KB |
| dok_fdennler_20090115_03_drupal.fix_a_date.pdf | 25.3 KB |

The idea is good, but..
The idea is really good to me, but I'm not really sure it should be implemented that way... At least, it would be a good idea to use the db_query() function in a nicer way...
But still.. nice idea... (I'll try to make a module from this starting point)
Edit: The module has just been released under the name "Dudel".
web is my playground
the way ..
the way i have implemented this idea is horrible, i know. it was my introduction to learn how drupal works.. and a startpoint for you ;) thank's for making a module ..
-- ease your mind ...
Awesome job to both of you
Awesome job to both of you guys.