Skip to content
Browse files

added server for teacher awards

  • Loading branch information...
1 parent 8bb8fa2 commit 1f7f83e33b017ca6ace8f47d5f980079b72c301d @DerWaldschrat committed Mar 27, 2013
Showing with 107 additions and 4 deletions.
  1. +76 −0 Award/Teacher/index.php
  2. +21 −0 Award/Teacher/list/index.php
  3. +2 −2 Award/createAward.php
  4. +2 −2 Award/fetchAll.php
  5. +3 −0 coreconfig.online.php
  6. +3 −0 coreconfig.php
View
76 Award/Teacher/index.php
@@ -0,0 +1,76 @@
+<?php
+define("IN", "../../");
+define("__EXEC", true);
+define("PHP_EX", ".php");
+
+require IN . "coreconfig" . PHP_EX;
+require IN . "mapper" . PHP_EX;
+
+function awardDefaultsTeacher(&$award) {
+ if (!property_exists($award, "firstid")) {
+ $award->firstid = -1;
+ }
+ if (!property_exists($award, "secondid")) {
+ $award->secondid = -1;
+ }
+ if (!property_exists($award, "thirdid")) {
+ $award->thirdid = -1;
+ }
+}
+
+if (isLoggedin(1)){
+ get(function () {
+ require IN . "Award/fetchAll" . PHP_EX;
+ $categories = fetchCategoriesAll(TEACHER_CATEGORY);
+ // There are no categories at all, we leave it at this
+ if ($categories === array()) {
+ hJSON(array());
+ } else {
+ $userid = userField("userid");
+ $db = db();
+ $awards = array();
+ $result = $db->query("SELECT awardid, firstid, secondid, thirdid, categoryid, userid FROM " . TEACHER_AWARD . " WHERE userid = " . $userid);
+ if($result instanceof mysqli_result) {
+ while($row = $result->fetch_object()) {
+ $awards[$row->categoryid] = $row;
+ }
+ }
+ $json = array();
+ foreach($categories as $categoryid => $category) {
+ $award = null;
+ if (!array_key_exists($categoryid, $awards)) {
+ require_once IN . "Award/createAward" . PHP_EX;
+ $award = new stdClass();
+ awardDefaultsTeacher($award);
+ $award->awardid = createAward($categoryid, $userid, TEACHER_AWARD);
+ $award->categoryid = $categoryid;
+ } else {
+ $award = $awards[$categoryid];
+ }
+ $award->title = $category->title;
+ $json[] = $award;
+ }
+ hJSON($json);
+ }
+ });
+
+ // Receive updates for the awards
+ put(function () {
+ require IN . "validator" . PHP_EX;
+ $award = bodyAsJSON();
+ if(hasAllSet($award, array("categoryid", "awardid"))) {
+ awardDefaultsTeacher($award);
+ $userid = userField("userid");
+ $db = db();
+ $st = $db->prepare("UPDATE " . TEACHER_AWARD . " SET firstid = ?, secondid = ?, thirdid = ? WHERE userid = ? AND categoryid = ?");
+ $st->bind_param("iiiii", $award->firstid, $award->secondid, $award->thirdid, $userid, $award->categoryid);
+ if(!exQuery($st)) {
+ fail("awardSaveFail");
+ }
+ } else {
+ fail("awardSaveFail");
+ }
+ });
+}
+
+?>
View
21 Award/Teacher/list/index.php
@@ -0,0 +1,21 @@
+<?php
+define("IN", "../../../");
+define("__EXEC", true);
+define("PHP_EX", ".php");
+
+require IN . "coreconfig" . PHP_EX;
+require IN . "mapper" . PHP_EX;
+
+if (isLoggedin(1)) {
+
+ get(function () {
+ $db = db();
+ $result = $db->query("SELECT teacherid, name FROM " . TEACHER);
+ $json = array();
+ while ($row = $result->fetch_object()) {
+ $json[] = $row;
+ }
+ hJSON($json);
+ });
+}
+?>
View
4 Award/createAward.php
@@ -6,9 +6,9 @@
* @param $user
* @return int The inserted id or -1 if it failed
*/
-function createAward($category, $user)
+function createAward($category, $user, $table = AWARD)
{
- $st = db()->prepare("INSERT INTO " . AWARD . " (categoryid, userid) VALUES (?, ?)");
+ $st = db()->prepare("INSERT INTO " . $table . " (categoryid, userid) VALUES (?, ?)");
$st->bind_param("ii", $category, $user);
if (exQuery($st)) {
return $st->insert_id;
View
4 Award/fetchAll.php
@@ -1,8 +1,8 @@
<?php
defined("__EXEC") or exit;
// Fetches all Categories
-function fetchCategoriesAll() {
- $result = db()->query("SELECT categoryid, title FROM ". CATEGORY);
+function fetchCategoriesAll($table = CATEGORY) {
+ $result = db()->query("SELECT categoryid, title FROM ". $table);
if ($result instanceof mysqli_result) {
$rows = array();
while($r = $result->fetch_object()) {
View
3 coreconfig.online.php
@@ -51,6 +51,9 @@ function db()
define("MARK", $pre . "images_mark");
define("DATA", $pre . "data");
define("RATING", $pre . "rating");
+define("TEACHER", $pre . "teacher");
+define("TEACHER_AWARD", $pre . "teacher_award");
+define("TEACHER_CATEGORY", $pre . "teacher_category");
/**
View
3 coreconfig.php
@@ -75,6 +75,9 @@ function db()
define("MARK", $pre . "images_mark");
define("DATA", $pre . "data");
define("RATING", $pre . "rating");
+define("TEACHER", $pre . "teacher");
+define("TEACHER_AWARD", $pre . "teacher_award");
+define("TEACHER_CATEGORY", $pre . "teacher_category");
/**

0 comments on commit 1f7f83e

Please sign in to comment.
Something went wrong with that request. Please try again.