Fixed minor bugs
[speedfreak] / Server / application / models / result.php
1 <?php defined('SYSPATH') or die('No direct script access.');
2 /*
3  * Results model for creating and manipulating results
4  *
5  * @author      Artem Daniliants <artem@daniliants.com>
6  * @copyright   (c) 2010 Speed Freak team
7  * @license     http://opensource.org/licenses/gpl-license.php GNU Public License
8  */
9
10 class Result_Model extends Model {
11
12     public function __construct(){
13
14         // load database library into $this->db
15         parent::__construct();
16     }
17
18     /*
19      * Fetch results
20      *
21      * @param string $category From which category should we get results
22      * @param integer $limit How many results to return
23      * @return object|bool Returns object containing results if everything is ok and false otherwise
24      */
25     public function get_results($category, $limit=10){
26         $results = $this->db->query("SELECT u.username as username, c.unit as unit, r.value as value, r.result_date as result_date, c.slug as slug FROM users u, results r, categories c WHERE r.user_id=u.id AND c.slug = ? AND r.cat_id=c.id ORDER BY r.value ASC LIMIT ".(int)$limit, $category);
27     if ($results->count()>0)
28             return $results;
29         else
30             return false;
31     }
32
33     /*
34      * Insert new result
35      *
36      * @param string $category Category name (slug)
37      * @return bool True if exists and False otherwise
38      */
39     public function insert($category, $username, $value){
40         $cat = New Category_Model();
41         $category = $cat->get_id($category);
42         $user = New User_Model();
43         $username = $user->get_id($username);
44         $results = $this->db->query("INSERT INTO results SET cat_id = ?, user_id = ?, value = ?, result_date = NOW()", $category, $username, $value);
45         if ($results)
46             return true;
47         else
48             return false;
49     }
50
51 }