Changeset 69


Ignore:
Timestamp:
17/11/06 14:51:41 (6 years ago)
Author:
nigel
Message:

Cleaned up unused code.
Added CSS grapher

File:
1 edited

Legend:

Unmodified
Added
Removed
  • meter/meter.module

    r68 r69  
    1212`nid` INT( 10 ) NOT NULL , 
    1313`time` DATETIME NOT NULL , 
    14 `value` DECIMAL NOT NULL 
     14`value` DECIMAL NOT NULL, 
     15`comment` VARCHAR( 255 ) 
    1516) ENGINE = MYISAM  
    1617*/ 
     
    249250 
    250251/** 
    251  * Themes the voting form for a poll. 
    252  */ 
    253 function theme_meter_view_voting($form) { 
    254   $output .= '<div class="poll">'; 
    255   $output .= '  <div class="vote-form">'; 
    256   $output .= '    <div class="choices">'; 
    257   $output .= form_render($form['choice']); 
    258   $output .= '    </div>'; 
    259   $output .= form_render($form['nid']); 
    260   $output .= form_render($form['vote']); 
    261   $output .= '  </div>'; 
    262   $output .= form_render($form); 
    263   $output .= '</div>'; 
    264   return $output; 
    265 } 
    266  
    267 /** 
    268  * Generates a graphical representation of the results of a poll. 
    269  */ 
    270 function meter_view_results(&$node, $teaser, $page, $block) { 
    271   // Count the votes and find the maximum 
    272   foreach ($node->choice as $choice) { 
    273     $total_votes += $choice['chvotes']; 
    274     $max_votes = max($max_votes, $choice['chvotes']); 
    275   } 
    276  
    277   foreach ($node->choice as $i => $choice) { 
    278     if ($choice['chtext'] != '') { 
    279       $meter_results .= theme('meter_bar', check_plain($choice['chtext']), round($choice['chvotes'] * 100 / max($total_votes, 1)), format_plural($choice['chvotes'], '1 vote', '%count votes'), $block); 
    280     } 
    281   } 
    282  
    283   $output .= theme('meter_results', check_plain($node->title), $meter_results, $total_votes, $node->links, $block); 
    284  
    285   return $output; 
    286 } 
    287  
    288 function theme_meter_results($title, $results, $votes, $links, $block) { 
    289   if ($block) { 
    290     $output .= '<div class="poll">'; 
    291     $output .= '<div class="title">'. $title .'</div>'; 
    292     $output .= $results; 
    293     $output .= '<div class="total">'. t('Total votes: %votes', array('%votes' => $votes)) .'</div>'; 
    294     $output .= '</div>'; 
    295     $output .= '<div class="links">'. theme('links', $links) .'</div>'; 
    296   } 
    297   else { 
    298     $output .= '<div class="poll">'; 
    299     $output .= $results; 
    300     $output .= '<div class="total">'. t('Total votes: %votes', array('%votes' => $votes)) .'</div>'; 
    301     $output .= '</div>'; 
    302   } 
    303  
    304   return $output; 
    305 } 
    306  
    307 function theme_meter_bar($title, $percentage, $votes, $block) { 
    308   if ($block) { 
    309     $output  = '<div class="text">'. $title .'</div>'; 
    310     $output .= '<div class="bar"><div style="width: '. $percentage .'%;" class="foreground"></div></div>'; 
    311     $output .= '<div class="percent">'. $percentage .'%</div>'; 
    312   } 
    313   else { 
    314     $output  = '<div class="text">'. $title .'</div>'; 
    315     $output .= '<div class="bar"><div style="width: '. $percentage .'%;" class="foreground"></div></div>'; 
    316     $output .= '<div class="percent">'. $percentage .'% ('. $votes .')</div>'; 
    317   } 
    318  
    319   return $output; 
    320 } 
    321  
    322 /** 
    323252 * Callback for processing a vote 
    324253 */ 
     
    332261  $date = $edit['date']; 
    333262  $value = $edit['value']; 
     263  $comment = $edit['comment']; 
    334264 
    335265 
    336266  if ($node->uid == $user->uid) { 
    337     db_query('INSERT INTO {meter_readings} (nid, time, value) VALUES (%d, "%s", %d)', $node->nid, $date, $value); 
     267    db_query('INSERT INTO {meter_readings} (nid, time, value, comment) VALUES (%d, "%s", %d, "%s")', $node->nid, $date, $value, $comment); 
    338268    drupal_set_message(t('Your reading was recorded.')); 
    339269  } 
     
    380310        '#type' => 'textfield'); 
    381311 
     312  $form['comment'] = array( 
     313        '#title' => t('Comment'), 
     314        '#type' => 'textfield'); 
     315 
    382316  $form['add_reading'] = array('#type' => 'submit', '#value' => t('Add reading')); 
    383317  $form['#action'] = url('node/'. $node->nid.'/add_reading'); 
    384318 
    385319  $res = meter_extrapolate(meter_get_values($node), 1,30); 
    386   foreach ($res as $val){ 
     320  /*foreach ($res as $val){ 
    387321    $output .= "<p>$val->to $val->value </p>"; 
    388   } 
    389  
     322  }*/ 
     323 
     324  $output .= meter_graph($res); 
    390325  $output .= drupal_get_form('meter_add_reading', $form); 
    391326 
     
    414349function meter_get_values($node){ 
    415350  $res = array(); 
    416   $results = db_query("SELECT time, value FROM {meter_readings} WHERE nid = %d ORDER BY time DESC", $node->nid); 
     351  $results = db_query("SELECT time, value, comment FROM {meter_readings} WHERE nid = %d ORDER BY time DESC", $node->nid); 
    417352 
    418353  while ($row = db_fetch_object($results)){ 
     
    482417    } 
    483418  } 
     419 
    484420  return $return; 
    485421} 
     422 
     423function meter_graph($entries){ 
     424  if (count($entries) == 0) 
     425    return "No entries"; 
     426  $height = 200; 
     427  $width = 500; 
     428  $output = '<div style="background: white; border: 1px solid; position: relative; width: '.$width.'px; height:'.$height.'px">'; 
     429 
     430  $w = floor($width/count($entries)); 
     431  $offset = ($width-($w*count($entries)))/2; 
     432  $i = 0; 
     433  $max = 20; 
     434  foreach ($entries as $e){ 
     435    $output .= "<div title='".$e->from." to ".$e->to." value: ".sprintf("%1.2f",$e->value)."' style='width:${w}px; height:".floor($e->value/20*$height)."px; bottom: 0px; position: absolute; right:".($i*$w+$offset)."px; background: grey; border: solid 1px;'>".round($e->value)."</div>\n"; 
     436    $i++; 
     437  } 
     438  $output .= '</div>'; 
     439  return $output; 
     440} 
Note: See TracChangeset for help on using the changeset viewer.