0

I've searched on SE and google for 2 weeks, and mostly found JavaScript or jquery solutions. I have very little knowledge of coding either. I have altered a custom Drupal module form and added a user "wager" input. I have #element validate (it is a positive integer) working correctly for each row of the "wager" input. It is also saving correctly to MySQL table for each "wager" input.

Questions: How do I sum all of the users "wager" boxes and compare >= to the users total available points for form validate? Is there an alternative solution to the jquery or JS? If not, how the heck would I begin to implement for that form? I can post code or screenshots if needed.

Edit: added screenshot of form: enter image description here

 > //  * Menu callback; presents the list of all weeks and games.  *
> function mypicks_all_page($league) {   return theme('mypicks_all',
> array('league' => $league)); }
> 
> 
> /**  * Display the list of all weeks and games.  * The user would pick
> a week link from here to make their weekly picks.  */ function
> theme_mypicks_all($variables) {   global $_PICKEM_MY_LEAGUES;   global
> $user;  /*******************blakestart*/
> 
> /********blakeend*******************/   $league =
> $variables['league'];
> 
 >   // Param check: user must be a member of this league to continue.  
 > if ( !is_my_league($league) ) {
  >     return drupal_not_found();   }
  > 
   >   drupal_set_title(t('My Picks - %user_name', array('%user_name' =>
 > $user->name)), PASS_THROUGH);
   > 
       >   $breadcrumb = drupal_get_breadcrumb();   $breadcrumb[] =
       > l($league->name, 'pickem/' . $league->lid);  
        > drupal_set_breadcrumb($breadcrumb);
    > 
    >   $teams = get_teams(array('gameset_id' => $league->gsid));   $games =
      > get_games(array('league_id' => $league->lid), array('by_week' => 1,
  > 'order_by' => 'week_ended, g.gamestart, g.gid'));
    > 
  > 
      >   $header = array(t('Week'), t('Games'));   $rows = array();   foreach
  > ($games as $wid => $weeks_games) {
    >     $cols = array();
 >     $current_week = '';
 >     foreach ($weeks_games as $gid => $game) {
   >         
   >         
     >         
    > 
    > 
    >       // Figure out the matchup operator.
 >       if ($game->neutral_venue == 1) {
  >         $matchup = "vs ";
     >       }
     >       else {
 >         $matchup = "@";
>       }
> 
>       // The first column should be the week number link.
 >       if ( count($cols) == 0 ) {
>         $cols[] = array('data' => l(t('Week %wk_number', array
('%wk_number' =>     $game->wk_number)),
> "pickem/$league->lid/mypicks/" . $wid, array('html' => TRUE)), 'class'
> => 'week');
>       }
>       $current_week .= '<div class="picks-float">' . $teams[$game->v_id]->short_name .     '<br/>' . $matchup .
> $teams[$game->h_id]->short_name . '</div>';
>     }
>     $cols[] = $current_week;
>     $rows[] = $cols;   }   return theme('table', array('header' => $header, 'rows' =>        $rows, 'attributes' => array('class' =>
 > array('pickem-mypicks-all')))); }
> 
> 
> function mypicks_week_form($form, &$form_state, $league, $week) {  
> global $user;  global $wager;
> 
>   
> 
>   $form = build_picks_week_form($form, $form_state, $league, $week,
> $user, $wager);   return $form; }
> 
> 
> function theme_mypicks_week_form($variables) {   global $user;
 > 
 > 
 >      $form = $variables['form'];   $league =
 > league_load($form['league_id']['#value']);
> 
>   // Drupal sets the default timezone to be the user's   //if
> available, otherwise, the system's.   $timezone =
> date_default_timezone_get();
> 
>   $header = array();   $header[] = t('Matchup');   if
 > ($league->scoring_engine->features['with_confidence'] == 1) {
>     $header[] = array('data' => t('Confidence'), 'class' => 'confidence-pts');   }          $header[] = t('Status');   $header[] =
> array('data'=>t('Game time'), 'title'=> t('Timezone is @timezone',
 > array('@timezone' => $timezone)));  if
 > ($league->scoring_engine->features['with_spread'] == 1){   $header[] =
 > array('data'=>t('Wager'), 'title'=>'This is Wager column');  }   $rows
 > = array();   $last = count(element_children($form['games']));   $count = 0;   foreach      (element_children($form['games']) as $gid) {
 >     $count++;
>     $cols = array();
 > 
 >     if (isset($form['games'][$gid]['pick'])) {
 >       if ($form['games'][$gid]['pick']['#default_value'] == '') {
  >         $cols[] = array('data' => drupal_render($form['games'][$gid]['pick']),  'class' => 'no-pick');
  >       }
 >       else {
 >         $cols[] = array('data' => drupal_render($form['games'][$gid]['pick']));
  >       }
 >     }
 >     else {
  >       $cols[] = drupal_render($form['games'][$gid]['pick_v_score']) . drupal_render($form['games'][$gid]['pick_h_score']);
 >     }
 > 
 >     // Render confidence boxen
 >     if ($league->scoring_engine->features['with_confidence'] == 1) {
 >       if($form['games'][$gid]['confidence']['#default_value'] == '') {
 >         $cols[] = array('data' => drupal_render($form['games'][$gid]['confidence']),
 > 'class'=>'no-pick');
 >       } else {
 >         $cols[] = array('data' => drupal_render($form['games'][$gid]['confidence']));
 >       }
 >     }
 > 
 >     $cols[] = drupal_render($form['games'][$gid]['pick_status']);
 > 
 >     $cols[] = drupal_render($form['games'][$gid]['gamestart']);
 >     
 > 
 >     $cols[] = drupal_render($form['games'][$gid]['wager']);
 > 
 > 
 >   
 >     $rows[] = $cols;   }
 > 
 >   $output = $league->scoring_engine->description;   $output .= '<p>' .
 > t('Game time is represented in the @timezone timezone.',
 > array('@timezone' => $timezone)) . '</p>';   $output .= theme('table',
 > array('header' => $header, 'rows' => $rows, 'attributes' =>
 > array('class' => array('pickem-mypicks-week'))));   $output .=
  > drupal_render_children($form);   return $output; }
 > 
 > 
 > 
 > 
  >   
 > 
 > 
 > function mypicks_week_form_validate($form, &$form_state) {   $league =
 > league_load($form_state['values']['league_id']);   if
 > (($league->scoring_engine->features['with_confidence'] == 1)) {
 >     $conf_values = array();
>     $max_conf = count($form_state['values']['games']);
>     foreach ($form_state['values']['games'] as $gid => $pick) {
> 
>       // Skip empty confidence values.
>       if ($pick['confidence'] == '') {
>         continue;
>       }
> 
>       // Out of range confidence numbers
>       if (!is_numeric($pick['confidence']) || $pick['confidence'] < 1 || $pick['confidence'] > $max_conf) {
>         form_set_error('games][' . $gid . '][confidence', t('Confidence point must be a number from 1 to %max.', array('%max' =>
> $max_conf)));
>       }
> 
>       // Duplicate confidence numbers
>       if (isset($conf_values[$pick['confidence']])) {
>         form_set_error('games][' . $gid . '][confidence', t('Confidence point %pt already used.', array('%pt' =>
> $pick['confidence'])));
>       }
>       $conf_values[$pick['confidence']] = TRUE;
>     
>     
> 
> 
>    
>          
>       }}}    /**  * Implements hook_form_alter().  */ function webform_calc_form_alter(&$form, $form_state, $form_id) {   if
> ($form_id == 'mypicks-week-form') {
>     $form['#validate'][] = 'webform_calc_validate';   } } function webform_calc_validate($form, &$form_state) {  
> switch($form_state['input']['details']['page_num']) {
>     case '1':
>       $bet = $form_state['input']['submitted']['wager'];
>       
>       
>       $totalbet = $wager + $wager;
 >       form_set_value(
>         $form['submitted']['totalbet'], $totalbet, $form_state);
>       break;
>     case '2':
>       $totalbet = $form_state['storage']['submitted'][4];
>       $form_state['webform']['component_tree']['children']
>         [11]['value'] = 'Total bet: ' . $totalbet; 
>       break;   }   return $form; } 
> 
> function mypicks_week_form_submit($form, &$form_state) {   global
> $user;
> 
> 
>   $league = league_load($form_state['values']['league_id']);   $week =
> week_load($form_state['values']['week_id']);  
> $form_state['values']['wager'] = array();
>   
>   
> 
>   // You should not be able to save picks after lockup.   if
> ($league->pick_lock == PICKEM_PICK_LOCK_WEEK && $week->week_started ==
> 1) {
>     drupal_set_title(t('%week_name picks are locked.', array('%week_name' => $week-      >wk_name)), PASS_THROUGH);
>     return t("You cannot change your picks now.");   }
> 
>   
> 
>   
>  
>   
> 
>   // Check and see if the user wants to save to all their leagues that
> use the same gameset.   $save_for_these_leagues = array();   if (
> $form_state['values']['apply_to_all_leagues'] == 1 ) {
>     $save_for_these_leagues = get_my_leagues($league->gsid);   }   else {
>     $save_for_these_leagues[] = $league;   }
> 
  >   foreach ($save_for_these_leagues as $l) {
>     // Only save for league using the same scoring system.
>     $success = TRUE;
>     if ($l->scoring_type == $league->scoring_type) {
>       foreach ($form_state['values']['games'] as $gid => $pick) {
>         $pick['gid'] = $gid;
>         if (set_pick($user->uid, $l, $pick, 'user') != TRUE) {
>           $success = FALSE;
>         }
>       }
> 
>    
>  
>      if ($success) {
>         
>          
>   
>            
 >        drupal_set_message(t('The picks have been saved for league <i>%league</i>.', array('%league' => $l->name)));
>       
 >       }
 >     }   } } //
1

Where do you want to do that calculation? Before or after submitting?

Assuming you want to do it before submitting the form, you can use the hook_form_alter() to set a custom submit function where you will receive the values from the form and do whatever you need with them. Something like:

function YOUR_MODULE_form_alter(&$form, &$form_state, $form_id) {
    if ($form_id == "YOUR_FORM_ID") {
        $form['#submit'][] = "_YOUR_MODULE_do_calculations";
    }
}

function _YOUR_MODULE_do_calculations($form, &$form_state) {

     $total = 0;
     if (isset($form_state['values']['YOUR_FIELD_1'])
      && isset($form_state['values']['YOUR_FIELD_2'])
      && isset($form_state['values']['YOUR_FIELD_3'])) {

        // Sum up values, save in DB or anything you need

     }
}
  • awesome I haven't stumbled across anything like that . I will try it out and get back ASAP. thanks for your quick response. – Austin Oct 29 '13 at 14:11
  • It seems like im almost there now with that. I should have specified, I am trying to sum all the 'wager' fields and compare to the current users total points, * and spit out a drupal_set_message(t('you dont have enough points')) if the SUM_of_Wagers > total_points. So would this still be form_alter on #submit or #validate ? Thanks ! – Austin Oct 29 '13 at 19:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.