Source for file int_training.php

Documentation is available at int_training.php

  1. <?php
  2. /**
  3.  * Interval training parse and display functions
  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: int_training.php 22 2009-03-05 23:58:02Z jp75018 $
  10.  */
  11.  
  12. /**
  13.  * Parse interval training expression
  14.  *
  15.  * @param    string    $expr Interval training expression as in {F=<expr>}
  16.  * @return    array    Array of interval training elements (index from 0) :
  17.  *             - repeat_nb : Nb of repetitions (integer >= 1)
  18.  *             - work_distance : Distance of effort in meters
  19.  *             - work_duration : Duration of effort in seconds
  20.  *             - reco_distance : Distance of recovery in meters
  21.  *             - reco_duration : Duration of recovery in seconds
  22.  *             Work and recovery are defined either by distance
  23.  *             (usual for work) or duration.
  24.  *             If recovery is empty and repeat_nb > 1 => recovery
  25.  *             duration unknown
  26.  *             If work is empty and repeat_nb = 1 => recovery only
  27.  *             Return null if expression is not valid.
  28.  */
  29. function int_training_parse($expr)
  30. {
  31.   $train_tab array();
  32.  
  33. // 1. Split into components separated by '+'
  34.   $expr_tab explode('+'$expr);
  35.   for ($i 0$i count($expr_tab)$i++)
  36.   {
  37.     $expr trim($expr_tab[$i]);
  38.   // 2. Extract nb of repetitions
  39.     $nb_rep 1;
  40.     if (preg_match('/^([0-9]+)\s*[x\*]\s*[\(\[]?([^\)\]]+)[\)\]]?$/i',
  41.       $expr$matches))
  42.     {
  43.       $nb_rep $matches[1];
  44.       if ($nb_rep)
  45.       break}
  46.       $expr trim($matches[2]);
  47.     }
  48.     $expr_work null;
  49.     $expr_reco null;
  50.   // 3. Recovery alone?
  51.     if (preg_match('/^R\s*=(.*)$/i'$expr$matches))
  52.     $expr_reco trim($matches[1])}
  53.     else
  54.     {
  55.     // 4. Work / Optional recovery ?
  56.       if (preg_match('/^([^\/]+)(\/(\s*R\s*=)?(.*))?$/i'$expr$matches))
  57.       {
  58.     $expr_work trim($matches[1]);
  59.     if ($matches[4])
  60.     $expr_reco trim($matches[4])}
  61.       }
  62.       else
  63.       return null}
  64.     }
  65.     $training array();
  66.     $training['repeat_nb'$nb_rep;
  67.     if ($expr_work)
  68.     {
  69.       if (work_recovery_parse($expr_work&$distance&$duration))
  70.       return null}
  71.       if ($distance)
  72.       $training['work_distance'$distance}
  73.       else
  74.       $training['work_duration'$duration}
  75.     }
  76.     if ($expr_reco)
  77.     {
  78.       if (work_recovery_parse($expr_reco&$distance&$duration))
  79.       return null}
  80.       if ($distance)
  81.       $training['reco_distance'$distance}
  82.       else
  83.       $training['reco_duration'$duration}
  84.     }
  85.     array_push($train_tab$training);
  86.   }
  87.  
  88.   return $train_tab;
  89. }
  90.  
  91. /**
  92. * Parse interval training work or recovery element (separated by '+' in
  93. * interval training expression
  94. *
  95. @param    string    $expr Interval training element
  96. @param    integer    &$distance Distance of work or recovery in meters if
  97. *             element is a distance, else null
  98. @param    integer    &$duration Duration of work or recovery in seconds if
  99. *             element is a duration, else null
  100. @return    boolean    true if element is valid, else false
  101. */
  102. function work_recovery_parse($expr&$distance&$duration)
  103. {
  104.   if (preg_match('/^(([0-9]+)([\.,]([0-9]+))?\s*(m|k|km)?|([0-9]+)\s*[\'m]|([0-9]+)\s*[\"s]|([0-9]+)\s*[\'m:]\s*([0-9]+)\s*[\"s]?)$/i',
  105.     $expr$matches))
  106.   {
  107.     $distance null;
  108.     $duration null;
  109.     if ($matches[2!= '')
  110.     {
  111.     // Distance case
  112.       $distance = (float)($matches[2'.' $matches[4]);
  113.       switch (strtolower($matches[5]))
  114.       {
  115.       case 'k' :  // kilometers
  116.       case 'km' :
  117.     $distance *= 1000;
  118.     break;
  119.       }
  120.       $distance round($distance);
  121.       return $distance true false;
  122.     }
  123.     else
  124.     {
  125.     // Duration case
  126.       $duration 0;
  127.       if ($matches[6!= '')
  128.       $duration 60 $matches[6]}            // Minutes only
  129.       if ($matches[7!= '')
  130.       $duration $matches[7]}            // Seconds only
  131.       if (($matches[8!= ''&& ($matches[9!= ''))
  132.       $duration 60 $matches[8$matches[9]}    // Minutes and seconds
  133.       return $duration true false;
  134.     }
  135.   }
  136. }
  137.  
  138. /**
  139. * Format interval training expression for nice output
  140. *
  141. @param    array    $train_tab Array of interval training components
  142. @return    string    Formatted interval training
  143. */
  144. function int_training_format($train_tab)
  145. {
  146.   $train_str '';
  147.   for ($i 0$i count($train_tab)$i++)
  148.   {
  149.     if ($train_str)
  150.     $train_str .= '&nbsp;+ '}
  151.     if ($train_tab[$i]['repeat_nb'1)
  152.     $train_str .= $train_tab[$i]['repeat_nb''&nbsp;x&nbsp;['}
  153.     if ($train_tab[$i]['work_distance'|| $train_tab[$i]['work_duration'])
  154.     {
  155.       if ($train_tab[$i]['work_distance'])
  156.       $train_str .= round($train_tab[$i]['work_distance']'m'}
  157.       if ($train_tab[$i]['work_duration'])
  158.       {
  159.     $sec $train_tab[$i]['work_duration'];
  160.     if ($sec >= 60)
  161.     {
  162.       if ($sec 60)
  163.       $str_elapsed sprintf("%d'%02d\""floor($sec 60)$sec 60)}
  164.       else
  165.       $str_elapsed floor($sec/60'\''}
  166.     }
  167.     else
  168.     $str_elapsed sprintf("%02d\""$sec)}
  169.     $train_str .= $str_elapsed;
  170.       }
  171.       if ($train_tab[$i]['reco_distance'|| $train_tab[$i]['reco_duration'])
  172.       $train_str .= '/'}
  173.     }
  174.     if ($train_tab[$i]['reco_distance'|| $train_tab[$i]['reco_duration'])
  175.     {
  176.       $train_str .= $GLOBALS['str_abbr_recovery''=';
  177.       if ($train_tab[$i]['reco_distance'])
  178.       $train_str .= round($train_tab[$i]['reco_distance']'m'}
  179.       else
  180.       {
  181.     $sec $train_tab[$i]['reco_duration'];
  182.     if ($sec >= 60)
  183.     {
  184.       if ($sec 60)
  185.       $str_elapsed sprintf("%d'%02d\""floor($sec 60)$sec 60)}
  186.       else
  187.       $str_elapsed floor($sec/60'\''}
  188.     }
  189.     else
  190.     $str_elapsed sprintf("%02d\""$sec)}
  191.     $train_str .= $str_elapsed;
  192.       }
  193.     }
  194.     else
  195.     {
  196.       if ($train_tab[$i]['repeat_nb'1)
  197.       $train_str .= '/' $GLOBALS['str_abbr_recovery''=?'}
  198.     }
  199.     if ($train_tab[$i]['repeat_nb'1)
  200.     $train_str .= ']'}
  201.   }
  202.  
  203.   return $train_str;
  204. }

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