Source for file PolarFile.class.php

Documentation is available at PolarFile.class.php

  1. <?php
  2. /**
  3.  * Common Class for all Polar files
  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: PolarFile.class.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.  * Interval training parse
  18.  */
  19. require_once ('include/int_training.php');
  20.  
  21. /**
  22.  * Common Class for all Polar files (ppd, pdd, hrm)
  23.  * 
  24.  * Contains common code for Polar files parsing
  25.  *
  26.  * @todo    Include common code to open / close file
  27.  */
  28. class PolarFile
  29. {
  30. /**
  31.  * File name of Polar file (full pathname)
  32.  * @var    string 
  33.  */
  34.   var    $fileName;
  35.  
  36. /**
  37.  * User ID (as in database)
  38.  * @var    integer 
  39.  */
  40.   var    $userId;
  41.  
  42. /**
  43.  * Polar file version
  44.  * @var    string 
  45.  */
  46.   var    $version;
  47.  
  48. /**
  49.  * Class constructor
  50.  *
  51.  * @param    string    $fileName Polar file name (full path name)
  52.  * @param    integer    $userId User ID (as in database)
  53.  * @return    void 
  54.  */
  55.   function PolarFile ($fileName$userId)
  56.   {
  57.     $this->fileName = $fileName;
  58.     $this->userId = $userId;
  59.   }
  60.  
  61. /**
  62.  * Parse a block from a Polar file (buffered)
  63.  *
  64.  * A Block starts with a line [block_name]
  65.  *
  66.  * @param    string    &$buffer Pointer to buffer
  67.  * @param    integer    &$line_no Pointer to line number where to start parsing.
  68.  *         It contains the line number of the end of the block on return.
  69.  * @return    string    Block name or null on error
  70.  */
  71.   function _parse_block_get(&$buffer&$line_no)
  72.   {
  73.     if (preg_match('/^\[([^\]]+)\][\s]*$/i'$buffer[$line_no]$match))
  74.     {
  75.       $block_name strtolower($match[1]);
  76.       if ($block_name == 'hrdata')
  77.       $line_no count($buffer)}
  78.       else
  79.       {
  80.     $line_no++;
  81.     while (($line_no count($buffer)) &&
  82.       (preg_match('/^\[([^\]]+)\][\s]*$/i'$buffer[$line_no])))
  83.     $line_no++}
  84.       }
  85.       return $block_name;
  86.     }
  87.     $line_no++;
  88.     return null;
  89.   }
  90.  
  91. /**
  92.  * Parse note to extract contest distance or interval training expression
  93.  *
  94.  * Contest distance / Internal expression is in { }
  95.  * - Contest distance is D=<distance>, where <distance> is number of meters or
  96.  *   <distance>m or <distance>k[m] or number of km if float number
  97.  *   (contains . or ,). <distance> can be also a keyword in
  98.  *   ('marathon','half','semi').
  99.  * - Interval training expression start with 'F='
  100.  *
  101.  * Options also in { }
  102.  * - Shoe used : S=<shoe-name>[:<distance>[k[m]]] where <distance> is initial
  103.  *   distance for shoe (in km if not specified)
  104.  *
  105.  * @param    string    $note Exercise note
  106.  * @param    string    &exercise_type exercise type : 'contest' for contest,
  107.  *             'int_training' for interval training, else 'training'.
  108.  * @param    integer    &$distance If 'contest' : official distance in meters
  109.  * @param    array    &$int_training If 'int_training' :
  110.  *             Array of interval training elements (index from 0) :
  111.  *             - repeat_nb : Nb of repetitions (integer >= 1)
  112.  *             - work_distance : Distance of effort in meters
  113.  *             - work_duration : Duration of effort in seconds
  114.  *             - reco_distance : Distance of recovery in meters
  115.  *             - reco_duration : Duration of recovery in seconds
  116.  * @param    array    &$options :
  117.  *             Associative array of options :
  118.  *             - shoe : Shoe name (ID)
  119.  *             - shoe_ini_dist : Shoe optional init distance (meters)
  120.  *             - footpod_id : Foot pod ID (1 digit, default : 1)
  121.  *             - footpod_batt : Battery initial use (sec x10)
  122.  * @return    string    Note with { } distance and options removed
  123.  */
  124.   function _parse_note($note&$exercise_type&$distance&$int_training,
  125.     &$options)
  126.   {
  127.     $exercise_type 'training';
  128.     $distance 0;
  129.     $int_training null;
  130.     $options array();
  131.  
  132.   // 1. Extract exercise type in { }
  133.     if (preg_match('/^(.*){\s*([df])\s*=?([^}]+)}(.*)$/i'$note$matches))
  134.     {
  135.       $note trim($matches[1]);
  136.       if ($matches[1&& $matches[4])
  137.       $note .= ' '}
  138.       $note .= trim($matches[4]);
  139.       switch (strtolower($matches[2]))
  140.       {
  141.       case 'd' :    // Contest => Find out exact distance
  142.     $dist_str trim($matches[3]);
  143.     if (preg_match('/^((([0-9]+)([\.,]([0-9]+))?\s*(m|k|km)?)|marathon|half([\-\s]*marathon)?|semi([\-\s]*marathon)?)$/i',
  144.       $dist_str$matches))
  145.     {
  146.       if ($matches[3== '')
  147.       {
  148.       // Keyword distance
  149.         switch (substr(strtolower($matches[1])04))
  150.         {
  151.         case 'mara' :
  152.           $distance 42195;
  153.           break;
  154.         case 'half' :
  155.         case 'semi' :
  156.           $distance 21098;
  157.           break;
  158.         }
  159.       }
  160.       else
  161.       {
  162.       // Numeric distance
  163.         $distance = (float)($matches[3'.' $matches[5]);
  164.         switch (strtolower($matches[6]))
  165.         {
  166.         case 'k' :    // kilometers
  167.         case 'km' :
  168.           $distance *= 1000;
  169.           break;
  170.         }
  171.         $distance round($distance);
  172.       }
  173.       if ($distance 0)
  174.       $exercise_type 'contest'}
  175.     }
  176.           break;
  177.       case 'f' :    // Interval training
  178.     $int_training int_training_parse(trim($matches[3]));
  179.     if (is_array($int_training))
  180.     $exercise_type 'int_training'}
  181.           break;
  182.       }
  183.     }
  184.  
  185.   // 2. Extract shoe + optional initial nb of km in last { }
  186.     if (preg_match('/^(.*){\s*s\s*=([^}:\s]+)(\s*:\s*(([0-9]+)([\.,]([0-9]+))?\s*(k|km)?))?}(.*)$/i'$note$matches))
  187.     {
  188.       $note trim($matches[1]);
  189.       if ($matches[1&& $matches[9])
  190.       $note .= ' '}
  191.       $note .= trim($matches[9]);
  192.     // Shoe => Get shoe name + optional initial nb of km
  193.       $shoe_str substr(strtolower(trim($matches[2]))020);
  194.       $options['shoe'$shoe_str;
  195.       if ($matches[3])
  196.       {
  197.       // Numeric distance
  198.     $shoe_dist = (float)($matches[5'.' $matches[7]);
  199.     $shoe_dist round(1000 $shoe_dist);
  200.     $options['shoe_ini_dist'$shoe_dist;
  201.       }
  202.     }
  203.  
  204.   // 3. Extract foot pod + optional initial nb of hours in last { }
  205.     $options['footpod_id'1;        // Default foot pod
  206.     if (preg_match('/^(.*){\s*p\s*(=\s*([1-9]))?(\s*:\s*(([0-9]+)([\.,]([0-9]+))?\s*(h)?))?}(.*)$/i'$note$matches))
  207.     {
  208.       $note trim($matches[1]);
  209.       if ($matches[1&& $matches[10])
  210.       $note .= ' '}
  211.       $note .= trim($matches[10]);
  212.     // Pod => Get optional pod ID + optional initial nb of hours
  213.       if ($matches[3])
  214.       $pod_id $matches[3]}
  215.       else
  216.       $pod_id 1}
  217.       $options['footpod_id'$pod_id;
  218.       if ($matches[4])
  219.       {
  220.       // Numeric number of hours
  221.     $pod_battery = (float)($matches[6'.' $matches[8]);
  222.     $pod_battery round(36000 $pod_battery);
  223.     $options['footpod_batt'$pod_battery;
  224.       }
  225.     }
  226.  
  227.     return $note;
  228.   }
  229. }
  230. ?>

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