Source for file hr_tools.php

Documentation is available at hr_tools.php

  1. <?php
  2. /**
  3.  * Tools for heart rate computing and display
  4.  *
  5.  * @author    Jean-Philippe Brunon <jp75018@free.fr>
  6.  * @copyright    2007-2009 Jean-Philippe Brunon
  7.  * @license    http://www.opensource.org/licenses/gpl-license.php GPL
  8.  * @package    php-endurance
  9.  * @version    $Id: hr_tools.php 22 2009-03-05 23:58:02Z jp75018 $
  10.  */
  11.  
  12. /**
  13.  * General configuration file
  14.  */
  15. require_once ('conf/config.php');
  16.  
  17. /**
  18.  * Heart rate colors for some particuliar HR pct values.
  19.  * Used to compute colors by interpolation.
  20.  * @global array $GLOBALS['hr_col'] 
  21.  */
  22. $GLOBALS['hr_col'array(
  23.   'rest' =>    array(0X000X000Xff),    // blue
  24.   'pct65' =>    array(0X000Xff0X00),    // green
  25.   'pct80' =>    array(0Xff0Xff0X00),    // yellow
  26.   'pct90' =>    array(0Xff0X800X00),    // orange
  27.   'max' =>    array(0Xff0X000X00),    // red
  28. );
  29.  
  30. /**
  31.  * Get user rest and maximum heart rate from database
  32.  *
  33.  * @param       integer $user_id User ID (as in database)
  34.  * @param       integer &$rest_hr Rest heart rate computed if input is empty.
  35.  * @param       integer &$max_hr Maximum heart rate computed if input is empty.
  36.  * @param       integer $exe_max_hr Maximum heart rate found in exercise.
  37.  *             (exe_max_hr cannot be > max_hr)
  38.  * @return      void 
  39.  */
  40. function get_user_hr_limits($user_id&$rest_hr&$max_hr$exe_max_hr)
  41. {
  42.   if ($rest_hr && $max_hr)
  43.   return}
  44.  
  45.   $request sprintf("SELECT gender, birthdate, rest_hr, max_hr FROM plw_user WHERE id = %d",
  46.     $user_id);
  47.   $result mysql_query($request);
  48.   $user_info mysql_fetch_assoc($result);
  49.   mysql_free_result($result);
  50.  
  51.   if ($rest_hr)
  52.   $rest_hr $user_info['rest_hr']}
  53.   if ($max_hr)
  54.   $max_hr $user_info['max_hr']}
  55.  
  56. // If yet no max HR => Use 220/226 - age formula (if birthdate not null)
  57.   if (($max_hr&& ($user_info['birthdate']))
  58.   {
  59.     if ($user_info['gender'== 'female')
  60.     $def_max_hr 226}
  61.     else
  62.     $def_max_hr 220}
  63.     $cur_ts mktime();
  64.     $birthdate $user_info['birthdate'];
  65.     $birth_year  substr($birthdate04);
  66.     $birth_month substr($birthdate52);
  67.     $birth_day   substr($birthdate82);
  68.     $user_birth_ts =  mktime(1200$birth_month$birth_day$birth_year);
  69.     $user_age ($cur_ts $user_birth_ts(365.25 86400);
  70.     $max_hr round($def_max_hr $user_age);
  71.   // If yet no max HR => Get default
  72.     if ($max_hr)
  73.     $max_hr DEF_MAX_HR}
  74.   // Check max HR is coherent with exercise data
  75.     if ($max_hr $exe_max_hr)
  76.     $max_hr $exe_max_hr}
  77.   }
  78.  
  79. // If yet no rest HR => Get default
  80.   if ($rest_hr)
  81.   $rest_hr DEF_REST_HR}
  82.   if ($max_hr 255)
  83.   $max_hr 255}
  84.  
  85.   return;
  86. }
  87.  
  88. /**
  89.  * Get day heart rate information within period
  90.  *
  91.  * Rest HR is minumum rest HR, max GR is maximum max HR within period.
  92.  *
  93.  * @param       integer $user_id User ID (as in database)
  94.  * @param       string  $min_day Minimum date considered (format: YYYY-MM-DD)
  95.  * @param       string  $max_day Maximum date considered (format: YYYY-MM-DD)
  96.  * @return      array    Rest and max HR :
  97.  *             array('rest_hr' => <value>, 'max_hr' => <value>)
  98.  */
  99. function get_days_hr_info($user_id$min_day$max_day)
  100. {
  101.   $request sprintf("SELECT MIN(rest_hr) AS rest_hr, MAX(max_hr) AS max_hr FROM plw_day_info WHERE user_id = %d AND rest_hr > 0 AND max_hr > 0 AND day >= '%s' AND day <= '%s'",
  102.     $user_id$min_day$max_day);
  103.   $result mysql_query($request);
  104.   $day_info mysql_fetch_assoc($result);
  105.   mysql_free_result($result);
  106.  
  107.   return $day_info;
  108. }
  109.  
  110. /**
  111.  * Get RGB color for a given heart rate value
  112.  *
  113.  * Rest HR is minumum rest HR, max HR is maximum max HR within period.
  114.  *
  115.  * @param       integer $hr Heart rate value
  116.  * @param       array   $hr_lim HR special values
  117.  *             ('rest', 'pct65', 'pct80', 'pct90', 'max')
  118.  * @param       float   $white_ratio Mix with white color (0: no, 1: white)
  119.  * @return      array    RGB color (array of 3 integers [0-255])
  120.  */
  121. function get_gd_hr_color($hr$hr_lim$white_ratio 0)
  122. {
  123.   for ($i 0$i 3$i++)
  124.   {
  125.     if ($hr <= $hr_lim['pct65'])
  126.     {
  127.       $rgb_col[$i$GLOBALS['hr_col']['rest'][$i+
  128.     round(($GLOBALS['hr_col']['pct65'][$i-
  129.       $GLOBALS['hr_col']['rest'][$i]*
  130.     ($hr $hr_lim['rest']($hr_lim['pct65'$hr_lim['rest']));
  131.     }
  132.     if (($hr $hr_lim['pct65']&& ($hr <= $hr_lim['pct80']))
  133.     {
  134.       $rgb_col[$i$GLOBALS['hr_col']['pct65'][$i+
  135.     round(($GLOBALS['hr_col']['pct80'][$i-
  136.       $GLOBALS['hr_col']['pct65'][$i]*
  137.     ($hr $hr_lim['pct65']($hr_lim['pct80'$hr_lim['pct65']));
  138.     }
  139.     if (($hr $hr_lim['pct80']&& ($hr <= $hr_lim['pct90']))
  140.     {
  141.       $rgb_col[$i$GLOBALS['hr_col']['pct80'][$i+
  142.     round(($GLOBALS['hr_col']['pct90'][$i-
  143.       $GLOBALS['hr_col']['pct80'][$i]*
  144.     ($hr $hr_lim['pct80']($hr_lim['pct90'$hr_lim['pct80']));
  145.     }
  146.     if ($hr $hr_lim['pct90'])
  147.     {
  148.       $rgb_col[$i$GLOBALS['hr_col']['pct90'][$i+
  149.     round(($GLOBALS['hr_col']['max'][$i-
  150.       $GLOBALS['hr_col']['pct90'][$i]*
  151.     ($hr $hr_lim['pct90']($hr_lim['max'$hr_lim['pct90']));
  152.     }
  153.     if ($rgb_col[$i0)
  154.     $rgb_col[$i0}
  155.     if ($rgb_col[$i255)
  156.     $rgb_col[$i255}
  157.     if ($white_ratio)
  158.     $rgb_col[$i+= (255 $rgb_col[$i]$white_ratio}
  159.   }
  160.  
  161.   return array($rgb_col[0]$rgb_col[1]$rgb_col[2]);
  162. }
  163. ?>

Documentation generated on Sat, 28 Mar 2009 23:15:19 +0000 by phpDocumentor 1.4.1