Source for file PolarPpd.class.php

Documentation is available at PolarPpd.class.php

  1. <?php
  2. /**
  3.  * Class to parse Polar PPD file and insert / update data to database
  4.  *
  5.  * PPD file contains data related to 1 person (the user).
  6.  * This data comes from Polar Software.
  7.  *
  8.  * It includes list of sports.
  9.  *
  10.  * @author    Jean-Philippe Brunon <jp75018@free.fr>
  11.  * @copyright    2007-2009 Jean-Philippe Brunon
  12.  * @license    http://www.opensource.org/licenses/gpl-license.php GPL
  13.  * @package    php-endurance
  14.  * @version    $Id: PolarPpd.class.php 52 2009-03-17 15:15:30Z jp75018 $
  15.  */
  16.  
  17. /**
  18.  * Class common to all Polar files
  19.  */
  20. require_once ('PolarFile.class.php');
  21.  
  22. /**
  23.  * To insert user related sports
  24.  */
  25. require_once ('PolarSport.class.php');
  26.  
  27. /**
  28.  * Class to parse Polar PPD file and insert / update data to database
  29.  *
  30.  * PPD file contains data related to 1 person (the user), including sports.
  31.  *
  32.  * The following fields are handled when present in PPD file :
  33.  *
  34.  * Person info :
  35.  * - birthdate : Birthdate (format : YYYY-MM-DD)
  36.  * - gender : 'male' or 'female'
  37.  * - height : Height in cm x 10
  38.  * - max_hr : Maximum heart rate (bpm)
  39.  * - rest_hr : Rest heart rate (bpm)
  40.  * - vo2max : VO2max x 10
  41.  * - firstname : First name
  42.  * - lastname : Last name
  43.  *
  44.  * Person sports (for each sport) :
  45.  * - sport_id : Sport ID as in PDD files
  46.  * - name : Sport name
  47.  * - abbreviation : Sport abbreviation
  48.  * - index : Standard sport index in ('running','cycling','swimming',
  49.  *   'ccskiing','walking','resting')
  50.  */
  51. class PolarPpd extends PolarFile
  52. {
  53. /**
  54.  * Class constructor
  55.  *
  56.  * @param    string    $fileName Polar PPD file name (full path name)
  57.  * @param    integer    $userId User ID (as in database)
  58.  * @return    void 
  59.  */
  60.   function PolarPpd ($fileName$userId)
  61.   {
  62.     parent::PolarFile($fileName$userId);
  63.   }
  64.  
  65. /**
  66.  * Insert person information into database
  67.  *
  68.  * SQL tables :
  69.  * - plw_user (update)
  70.  * - plw_sport (delete / insert)
  71.  *
  72.  * @param    array    $person Structure for person and sports :
  73.  *             - 'personinfo' : Person fields
  74.  *             - 'personsports' : Array of sport fields, indexed by
  75.  *             sport ID
  76.  * @return    void 
  77.  */
  78.   function db_insert($person)
  79.   {
  80.     $info $person['personinfo'];
  81.     $spos $person['personsports'];
  82.  
  83.     if (is_array($spos))
  84.     return}
  85.  
  86.   // 1. Update person part
  87.     $request sprintf("UPDATE plw_user SET cre_date=cre_date, mod_date=NOW(), lastname=%s, firstname=%s, birthdate=%s, gender=%s, height=%s, rest_hr=%s, max_hr=%s, vo2max=%s WHERE id = %d",
  88.       $info['lastname'!= '' ?
  89.     '\'' mysql_escape_string($info['lastname']'\'' 'NULL',
  90.       $info['firstname'!= '' ?
  91.     '\'' mysql_escape_string($info['firstname']'\'' 'NULL',
  92.       $info['birthdate'!= '' ?  $info['birthdate''NULL',
  93.       $info['gender'!= '' '\'' $info['gender''\'' 'NULL',
  94.       $info['height'$info['height''NULL',
  95.       $info['rest_hr'$info['rest_hr''NULL',
  96.       $info['max_hr'$info['max_hr''NULL',
  97.       $info['vo2max'$info['vo2max''NULL',
  98.       $this->userId
  99.     );
  100.     mysql_query($request);
  101.  
  102.   // 2. Insert sports (delete first)
  103.     $request sprintf("DELETE FROM plw_sport WHERE user_id = %d",
  104.       $this->userId);
  105.     mysql_query($request);
  106.     $sport new PolarSport();
  107.     $sport->insert_sports($this->userId$spos);
  108.     $sport->set_default_sports($this->userId);
  109.     unset($sport);
  110.   }
  111.  
  112. /**
  113.  * Parse Polar PPD file (person information, including sports)
  114.  *
  115.  * Note : Not all information in file is used, only <personinfo> and
  116.  * <personsports> blocks are parsed.
  117.  *
  118.  * @return    array    Structure for person and sports :
  119.  *             - 'personinfo' : Person fields
  120.  *             - 'personsports' : Array of sport fields, indexed by
  121.  *             sport ID
  122.  */
  123.   function parse()
  124.   {
  125.     $buffer @file($this->fileName);
  126.     if (is_array($buffer))
  127.     return null}
  128.  
  129.     $line_no 0;
  130.     while ($line_no count($buffer))
  131.     {
  132.       $begin_block $line_no 1;
  133.       $block_name $this->_parse_block_get(&$buffer&$line_no);
  134.       $lg_block $line_no $begin_block 1;
  135.       if (trim($buffer[$end_block]))
  136.       $end_block--}
  137.       $parse_func '_parse_block_' $block_name '_get';
  138.       if (method_exists($this$parse_func))
  139.       {
  140.     $person[$block_name=
  141.       $this->$parse_func(&$buffer$begin_block$lg_block);
  142.       }
  143.     }
  144.     return $person;
  145.   }
  146.  
  147. /**
  148.  * Parse <personinfo> block in PPD file
  149.  *
  150.  * @param    string    $buffer Buffer with PPD file content
  151.  * @param    integer    $start Line number of block in buffer
  152.  * @param    integer    $nb Number of lines in block
  153.  * @return    array    Fields of person
  154.  */
  155.   function _parse_block_personinfo_get(&$buffer$start$nb)
  156.   {
  157.     $person null;
  158.     if ($nb 2)
  159.     return $person}
  160.     $row explode("\t"trim($buffer[$start 1]));
  161.     $person['birthdate'$row[0];
  162.     switch($row[1])
  163.     {
  164.     case :
  165.       $person['gender''female';
  166.       break;
  167.     case :
  168.       $person['gender''male';
  169.       break;
  170.     }
  171.     $person['height'10 $row[2];
  172.     if ($nb 5)
  173.     return $person}
  174.     $row explode("\t"trim($buffer[$start 4]));
  175.     $person['max_hr'$row[0];
  176.     $person['rest_hr'$row[3];
  177.     $person['vo2max'$row[4];
  178.     if ($nb 8)
  179.     return $person}
  180.     $person['firstname'trim($buffer[$start 7]);
  181.     if ($nb 9)
  182.     return $person}
  183.     $person['lastname'trim($buffer[$start 8]);
  184.  
  185.     return $person;
  186.   }
  187.  
  188. /**
  189.  * Parse <personsports> block in PPD file
  190.  *
  191.  * @param    string    $buffer Buffer with PPD file content
  192.  * @param    integer    $start Line number of block in buffer
  193.  * @param    integer    $nb Number of lines in block
  194.  * @return    array    Array of sports, indexed by sport ID
  195.  */
  196.   function _parse_block_personsports_get(&$buffer$start$nb)
  197.   {
  198.     $sports array();
  199.     $j 3;
  200.     while ($j ($nb 3))
  201.     {
  202.       $row explode("\t"trim($buffer[$start $j]));
  203.       $sport_id $row[0];
  204.       $sports[$sport_id]['name'trim($buffer[$start $j 2]);
  205.       $sports[$sport_id]['abbrev'trim($buffer[$start $j 3]);
  206.       $j += 6;
  207.     }
  208.  
  209.     return $sports;
  210.   }
  211. }
  212. ?>

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