*/
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
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
#
# 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 */;
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
# ------------------------------------------------------------
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;
--- /dev/null
+<?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
}
/*
+ * 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
--- /dev/null
+<?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