Sending and fetching results feature complete
authorArtem Daniliants <artem@daniliants.com>
Fri, 19 Mar 2010 09:00:01 +0000 (11:00 +0200)
committerArtem Daniliants <artem@daniliants.com>
Fri, 19 Mar 2010 09:25:43 +0000 (11:25 +0200)
Server/application/controllers/api.php
Server/application/models/category.php
Server/application/models/database_dump.sql
Server/application/models/result.php [new file with mode: 0644]
Server/application/models/user.php
Server/application/views/api/results.php [new file with mode: 0644]

index e40b263..4d48e13 100644 (file)
@@ -95,12 +95,55 @@ class Api_Controller extends Controller{
      */
     public function categories(){
        if ($this->is_authorized()){
-               $xml = new View('api/categories');
+               $view = new View('api/categories');
                $cat = new Category_Model();
-               $xml->categories=$cat->get_all();
-               $xml->render(true);
+               $view->categories=$cat->get_all();
+               $view->render(true);
        }
        else
           $this->not_authorized();
     }
+
+    /*
+     * Get results
+     *
+     */
+    public function results($category, $limit){
+       $results = New Result_Model();
+       $cat = New Category_Model();
+        if ($cat->category_exists($category) AND $this->is_authorized()){
+               $view = new View('api/results');
+               $view->results = $results->get_results($category, $limit);
+               $view->render(true);
+           }
+        else
+            $this->not_authorized();
+    }
+
+    /*
+     * Submit results to selected category
+     *
+     * @param string $category Category to which results are submitted
+     */
+    public function update($category){
+       $cat = New Category_Model();
+       if ($cat->category_exists($category) AND $this->is_authorized()){
+               $xml = $this->get_xml();
+               $result = New Result_Model();
+               if ($result->insert($category,$_SERVER['PHP_AUTH_USER'], $xml['value'])){
+                       print "OK";
+                       die;
+               }
+               else {
+                       header("HTTP/1.1 400 Bad Request");
+                   echo "Invalid request";
+                   die;
+               }
+       }
+       else {
+            header("HTTP/1.0 404 Not Found");
+            die('Category not found');
+       }
+
+    }
 }
\ No newline at end of file
index f47f0e4..a5c0c20 100644 (file)
@@ -28,4 +28,32 @@ class Category_Model extends Model {
             return false;
     }
 
+    /*
+     * Check if category exists
+     *
+     * @param string $category Category name (slug)
+     * @return bool True if exists and False otherwise
+     */
+    public function category_exists($category){
+       $results = $this->db->query("SELECT id FROM categories where slug = ?", $category);
+    if ($results->count()>0)
+            return true;
+        else
+            return false;
+    }
+
+    /*
+     * Get category id
+     *
+     * @param string $category Category name (slug)
+     * @return integer|bool Category id if successful or false
+     */
+    public function get_id($category){
+       $results = $this->db->query("SELECT id FROM categories where slug = ?", $category);
+    if ($results->count()>0)
+            return $results[0]->id;
+        else
+            return false;
+    }
+
 }
\ No newline at end of file
index 4232936..a1c18e3 100644 (file)
@@ -4,7 +4,7 @@
 #
 # Host: localhost (MySQL 5.1.37)
 # Database: speedfreak
-# Generation Time: 2010-03-17 14:16:47 +0200
+# Generation Time: 2010-03-19 09:49:37 +0200
 # ************************************************************
 
 /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
@@ -43,6 +43,23 @@ VALUES
 UNLOCK TABLES;
 
 
+# Dump of table results
+# ------------------------------------------------------------
+
+DROP TABLE IF EXISTS `results`;
+
+CREATE TABLE `results` (
+  `cat_id` int(11) NOT NULL,
+  `user_id` int(11) NOT NULL,
+  `value` text NOT NULL,
+  KEY `cat_id` (`cat_id`),
+  KEY `user_id` (`user_id`),
+  CONSTRAINT `results_ibfk_1` FOREIGN KEY (`cat_id`) REFERENCES `categories` (`id`),
+  CONSTRAINT `results_ibfk_2` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8;
+
+
+
 # Dump of table users
 # ------------------------------------------------------------
 
@@ -56,7 +73,7 @@ CREATE TABLE `users` (
   PRIMARY KEY (`id`),
   UNIQUE KEY `login_unique` (`username`),
   UNIQUE KEY `email_unique` (`email`)
-) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
+) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
 
 
 
diff --git a/Server/application/models/result.php b/Server/application/models/result.php
new file mode 100644 (file)
index 0000000..b7fd283
--- /dev/null
@@ -0,0 +1,51 @@
+<?php defined('SYSPATH') or die('No direct script access.');
+/*
+ * Results model for creating and manipulating results
+ *
+ * @author      Artem Daniliants <artem@daniliants.com>
+ * @copyright   (c) 2010 Speed Freak team
+ * @license     http://opensource.org/licenses/gpl-license.php GNU Public License
+ */
+
+class Result_Model extends Model {
+
+    public function __construct(){
+
+        // load database library into $this->db
+        parent::__construct();
+    }
+
+    /*
+     * Fetch results
+     *
+     * @param string $category From which category should we get results
+     * @param integer $limit How many results to return
+     * @return object|bool Returns object containing results if everything is ok and false otherwise
+     */
+    public function get_results($category, $limit=10){
+        $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 value DESC LIMIT ".(int)$limit, $category);
+    if ($results->count()>0)
+            return $results;
+        else
+            return false;
+    }
+
+    /*
+     * Insert new result
+     *
+     * @param string $category Category name (slug)
+     * @return bool True if exists and False otherwise
+     */
+    public function insert($category, $username, $value){
+       $cat = New Category_Model();
+       $category = $cat->get_id($category);
+       $user = New User_Model();
+       $username = $user->get_id($username);
+        $results = $this->db->query("INSERT INTO results SET cat_id = ?, user_id = ?, value = ?, result_date = NOW()", $category, $username, $value);
+        if ($results)
+            return true;
+        else
+            return false;
+    }
+
+}
\ No newline at end of file
index 855ffaf..78ef8ca 100644 (file)
@@ -90,6 +90,20 @@ class User_Model extends Model {
     }
     
     /*
+     * Get user id
+     *
+     * @param string $username Username
+     * @return integer|bool User id if successful or false
+     */
+    public function get_id($username){
+        $result = $this->db->query("SELECT id FROM users WHERE username = ?", $username);
+       if ($result->count()>0)
+           return $result[0]->id;
+        else
+           return false;
+    }
+
+    /*
      * Check if supplied credentials are valid
      * 
      * @param string $username Username
diff --git a/Server/application/views/api/results.php b/Server/application/views/api/results.php
new file mode 100644 (file)
index 0000000..d823be0
--- /dev/null
@@ -0,0 +1,5 @@
+<?php echo "<?"; ?>xml version="1.0" encoding="utf-8" <?php echo "?>"; ?>
+
+<results>
+    <?php $i=1; foreach ($results as $r){ ?>   <result username="<?php echo $r->username; ?>" position="<?php echo $i; ?>" date="<?php echo $r->result_date; ?>" unit="<?php echo $r->unit; ?>" value="<?php echo $r->value; ?>" />
+<?php $i++; } ?></results>
\ No newline at end of file