Source for file PolarSport.class.php

Documentation is available at PolarSport.class.php

  1. <?php
  2. /**
  3.  * Class to get sports of a user
  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: PolarSport.class.php 33 2009-03-06 22:47:22Z jp75018 $
  10.  */
  11.  
  12. /**
  13.  * General configuration file
  14.  */
  15. require_once ('conf/config.php');
  16. /**
  17.  * Default sport names, abbrev, and name parser are language dependant
  18.  */
  19. require_once ('lang/strings_' $GLOBALS['lang''.inc.php');
  20.  
  21. /**
  22.  * Standard Polar sport ID for running
  23.  */
  24. define('SPORT_ID_RUNNING'1);
  25. /**
  26.  * Standard Polar sport ID for cycling
  27.  */
  28. define('SPORT_ID_CYCLING'2);
  29. /**
  30.  * Standard Polar sport ID for swimming
  31.  */
  32. define('SPORT_ID_SWIMMING'3);
  33.  
  34. /**
  35.  * Class to get sports of a user
  36.  */
  37. class PolarSport
  38. {
  39. /**
  40.  * Class constructor
  41.  *
  42.  * @return      void 
  43.  */
  44.   function PolarSport()
  45.   {
  46.   }
  47.  
  48. /*
  49.  * Complete user's sports with default sports (running, cycling, swimming)
  50.  *
  51.  * Call this method in personal file (.PPD) load
  52.  *
  53.  * It completes sport records with standard 'index' string before inserting
  54.  * into database.
  55.  * 'index' can be: 'running', 'cycling', 'swimming',
  56.  * 'ccskiing' (cross country skiing), 'walking', 'snowshoe', 'resting'.
  57.  * (running, cycling, swimming are standard Polar sports).
  58.  *
  59.  * @param    integer    $user_id User ID (in database)
  60.  * @param    array    $sport_tab List of sports indexed by sport ID.
  61.  *            For each sport :
  62.  *            - 'name' : Full name of sport as defined by user
  63.  *            - 'abbrev' : Abbreviation of sport as defined by user
  64.  * @return    boolean true on success, else false (no user)
  65.  */
  66.   function insert_sports($user_id$sport_tab)
  67.   {
  68.     if (is_array($sport_tab))
  69.     return false}
  70.  
  71.   // 1. Check user exists
  72.     $ret_val false;
  73.     $request sprintf("SELECT id FROM plw_user WHERE id = %d"$user_id);
  74.     $result mysql_query($request);
  75.  
  76.     if (mysql_num_rows($result0)
  77.     {
  78.     // 2. Compute standard 'index' string
  79.       $sports array();
  80.       reset($sport_tab);
  81.       while (list($sport_id$sporteach($sport_tab))
  82.       {
  83.     $sports[$sport_id$sport;
  84.     $sports[$sport_id]['index'=
  85.       $this->_sport_name_to_index($sport['name']);
  86.     if ($sports[$sport_id]['index'== 'unksport')
  87.     {
  88.       switch ($sport_id)
  89.       {
  90.       case SPORT_ID_RUNNING :
  91.         $sports[$sport_id]['index''running';
  92.         break;
  93.       case SPORT_ID_CYCLING :
  94.         $sports[$sport_id]['index''cycling';
  95.         break;
  96.       case SPORT_ID_SWIMMING :
  97.         $sports[$sport_id]['index''swimming';
  98.         break;
  99.       }
  100.     }
  101.     unset($sports[$sport_id]['id']);
  102.       }
  103.  
  104.     // 3. Insert sports into database
  105.       reset($sports);
  106.       while (list($sport_id$sporteach($sports))
  107.       {
  108.     $request sprintf("INSERT INTO plw_sport(user_id, id, name, abbrev, sport_index) VALUES(%d, %d, '%s', %s, %s)",
  109.       $user_id$sport_id$sport['name'],
  110.       $sport['abbrev'!= '' '\'' $sport['abbrev''\'' 'NULL',
  111.       $sport['index'!= '' '\'' $sport['index''\'' 'NULL');
  112.     mysql_query($request);
  113.       }
  114.  
  115.       $ret_val true;
  116.     }
  117.  
  118.     mysql_free_result($result);
  119.  
  120.     return $ret_val;
  121.   }
  122.  
  123. /*
  124.  * Complete user's sports with default sports (running, cycling, swimming)
  125.  *
  126.  * Call this method :
  127.  * - After creating a new user
  128.  * - In personal file (.PPD) load in case a standard sport is missing
  129.  *
  130.  * @param    integer    $user_id User ID (in database)
  131.  * @return    boolean true on success, else false (no user)
  132.  */
  133.   function set_default_sports($user_id)
  134.   {
  135.   // 1. Check user exists
  136.     $ret_val false;
  137.     $request sprintf("SELECT id FROM plw_user WHERE id = %d"$user_id);
  138.     $result mysql_query($request);
  139.  
  140.     if (mysql_num_rows($result0)
  141.     {
  142.     // 2. Get user's sports
  143.       $sports $this->get_sports($user_id);
  144.  
  145.     // 3. Insert missing default sports into database
  146.       $std_sports array (
  147.     SPORT_ID_RUNNING => 'running',
  148.     SPORT_ID_CYCLING => 'cycling',
  149.     SPORT_ID_SWIMMING => 'swimming'
  150.       );
  151.       reset($std_sports);
  152.       while (list($i$std_sporteach($std_sports))
  153.       {
  154.     if (is_array($sports[$i]))
  155.     {
  156.       $found false;
  157.       reset($sports);
  158.       while (list($sport_id$sporteach($sports))
  159.       {
  160.         if ($sport['index'== $std_sport)
  161.         {
  162.           $found true;
  163.           break;
  164.         }
  165.       }
  166.       if ($found)
  167.       {
  168.         $request sprintf("INSERT INTO plw_sport(user_id, id, name, abbrev, sport_index) VALUES(%d, %d, '%s', '%s', '%s')",
  169.           $user_id$iucfirst($GLOBALS['val_sport'][$std_sport]),
  170.           ucfirst($GLOBALS['val_abbr_sport'][$std_sport]),
  171.           $std_sport);
  172.         mysql_query($request);
  173.       }
  174.     }
  175.       }
  176.  
  177.       $ret_val true;
  178.     }
  179.  
  180.     mysql_free_result($result);
  181.  
  182.     return $ret_val;
  183.   }
  184.  
  185. /*
  186.  * Get list of sports for one user
  187.  *
  188.  * @param    integer    $user_id User ID (in database)
  189.  * @return    array    List of sports indexed by sport ID.
  190.  *            For each sport :
  191.  *            - 'name' : Full name of sport as defined by user
  192.  *            - 'abbrev' : Abbreviation of sport as defined by user
  193.  *            - 'index' : Sport standard index common to all users
  194.  */
  195.   function get_sports($user_id)
  196.   {
  197.     $sports array();
  198.  
  199.   // Get sports
  200.     $request sprintf("SELECT id, name, abbrev, sport_index FROM plw_sport WHERE user_id = %d ORDER BY id",
  201.       $user_id);
  202.     $result mysql_query($request);
  203.     while ($row mysql_fetch_assoc($result))
  204.     {
  205.       $sports[$row['id']] $row;
  206.       $sports[$row['id']]['index'$row['sport_index'];
  207.       unset($sports[$row['id']]['id']);
  208.       unset($sports[$row['id']]['sport_index']);
  209.     }
  210.     mysql_free_result($result);
  211.  
  212.     return $sports;
  213.   }
  214.  
  215. /*
  216.  * Parse sport name to get sport index
  217.  *
  218.  * Use $GLOBALS['val_regexp_sport'] array (language dependant) and use
  219.  * first matching regexp.
  220.  *
  221.  * @param    string    $name Sport name to parse
  222.  * @return    string    Sport index ('unksport' if no regexp match)
  223.  */
  224.   function _sport_name_to_index($name)
  225.   {
  226.     $sport_index 'unksport';
  227.  
  228.   // 1. Normalize sport name
  229.     $name strtolower(trim($name));
  230.   // Remove accents
  231.     $name strtr($name,
  232.       'áàâäãåçéèêëíìîïóòôöõñúùûüýÿ''aaaaaaceeeeiiiiooooonuuuuyy');
  233.   // All but digits and letters are seprators => Convert to single ' '
  234.     $name implode(' 'preg_split('/[^a-z0-9]+/'$name));
  235.  
  236.   // 2. Loop on known sports until match
  237.     reset($GLOBALS['val_regexp_sport']);
  238.     while (list($index$reg_expeach($GLOBALS['val_regexp_sport']))
  239.     {
  240.       if (preg_match($reg_exp$name))
  241.       {
  242.           $sport_index $index;
  243.     break;
  244.       }
  245.     }
  246.  
  247.     return $sport_index;
  248.   }
  249. }

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