Permalink
Browse files

first version of getUser and addUser

  • Loading branch information...
1 parent cf433c3 commit 6c4b9cb9abd6d3213c156fcb2b23d59922ddb53b @jasonriley jasonriley committed Jan 22, 2012
Showing with 123 additions and 2 deletions.
  1. +28 −0 webroot/api/user.php
  2. +86 −2 webroot/api/wattquiz.php
  3. +9 −0 webroot/test.php
View
28 webroot/api/user.php
@@ -0,0 +1,28 @@
+<?php
+
+if ($_POST['userId']) {
+ $USER_ID = $_POST['userId'];
+}
+else
+{
+ header("Location: http://local.wattquiz.com/");
+ exit;
+}
+
+/** include the watt quiz services */
+require_once('wattquiz.php');
+
+// set your app id and app key
+$wq = new wattquiz(array(
+ 'debug' => false, // Debug mode echos API Url & POST data if set to true (Optional)
+));
+
+
+// make the getQuestion call
+$user = $wq->getUser( $USER_ID ); // ID of the user (Required)
+
+
+// Send the JSON back
+echo json_encode($user);
+
+?>
View
88 webroot/api/wattquiz.php
@@ -1,7 +1,7 @@
<?php
class wattquiz {
-
+
/**
* Creates a new wattquiz object
@@ -115,9 +115,18 @@ function answerQuestion($params) {
$questionId = $params['questionId'];
$userId = $params['userId'];
+ //
+ // Save the answer with the user.
+ //
+ //TODO
+
+ // add another record, with a different "shape"
+ $obj = array( "title" => "XKCD", "online" => true );
+ $collection->insert($obj);
+
// get the appropriate question
$question = $this->getQuestion(array(
- 'answeredCorrectly' => $correct, // whether they got it right or not
+ 'answeredCorrectly' => $correct, // whether they got it right or not
'previousQuestionId'=> $questionId, // Unique ID for the previous question (Optional)
'userId' => $userId // ID of the user answering the question (Required)
));
@@ -129,6 +138,81 @@ function answerQuestion($params) {
} // end of answerQuestion method
+ /**
+ * Get the user object with their information.
+ */
+ function getUser($userId) {
+
+ // connect
+ $m = new Mongo();
+
+ // select a database
+ $db = $m->wattquiz;
+
+ // find the user by their id
+ $collection = $db->wattUser;
+ $user = $collection->findOne(array('userId' => $userId));
+
+ if($user == '') {
+ $user = $this->_addUser($userId);
+ }
+
+ return $user;
+
+ } // end of getUser
+
+ function _addUser($userId) {
+
+ // connect
+ $m = new Mongo();
+
+ // select a database
+ $db = $m->wattquiz;
+
+ $gravatarHash = md5( strtolower( trim( $userId ) ) );
+ // then the URL is...
+ //http://www.gravatar.com/avatar/205e460b479e2e5b48aec07710c08d50?f=y
+
+ $user = array(
+// '_id' => $userId,
+ 'userId' => $userId,
+ 'gravatarHash' => $gravatarHash,
+ 'questionsAnswered' => 0,
+ 'greenButtonCount' => 0,
+ 'totalWatts' => 0,
+ 'rank' => 0,
+ 'lastCorrectAnswerId' => 0
+ );
+
+ // select the user collection
+ $collection = $db->wattUser;
+ $collection->insert($user);
+
+ return $user;
+
+ } // end of addUser
+
+
+ /**
+ * Get the leader board.
+ */
+ function getLeaderboard() {
+
+ $leaderboard = array(
+ '_id' => 'obama',
+ 'greenButtonCount' => 2,
+ 'gravatarHash' => '',
+ 'questionsAnswered' => 10,
+ 'totalWatts' => 22,
+ 'rank' => 1,
+ 'lastCorrectAnswerId' => 1
+ );
+
+ return $leaderboard;
+
+ }
+
+
}
?>
View
9 webroot/test.php
@@ -24,5 +24,14 @@
</form>
+ <form method="post" action="api/user.php">
+
+ userId: <input type="text" name="userId">
+
+ <input type="submit" value="Get or Add">
+
+ </form>
+
+
</body>
</html>

0 comments on commit 6c4b9cb

Please sign in to comment.