/**
  * Saves a score to the database
  * Or updates an existing score
  *
  * @url POST /
  * @url PUT /$id
  */
 public function SaveScore($id = null, $data)
 {
     // Only process valid data
     if ($this->ValidateScore($data)) {
         // Format for update/insert query
         $dbData = array('sectionId' => $data->Section->Id, 'userId' => $data->User->Id, 'score' => $data->Score);
         // Update existing user?
         if ($id) {
             $this->Db()->where('id', $id);
             if ($this->Db()->update('Scores', $dbData)) {
                 $score = new ScoreModel($id, $data->Section, $data->User, $data->Score);
             } else {
                 throw new RestException(500, 'Update mislukt: ' . $this->Db()->getLastError());
             }
             // Add new user
         } else {
             $id = $this->Db()->insert('Scores', $dbData);
             if ($id) {
                 $score = new ScoreModel($id, $data->Section, $data->User, $data->Score);
             } else {
                 throw new RestException(500, 'Toevoegen mislukt: ' . $this->Db()->getLastError());
             }
         }
         return $score->toArray();
     }
 }
Example #2
0
    public function Execute()
    {
        $results = $this->Db()->join('Sections s', 's.id = r.sectionId', 'INNER')->join('Users u', 'u.id = r.userId', 'INNER')->join('Categories c', 'c.id = u.categoryId', 'LEFT')->orderBy('u.firstName', 'ASC')->orderBy('u.lastName', 'ASC')->get('Scores r', null, 'r.id AS id,
			r.score AS score,
			s.id AS sectionId,
			s.name AS sectionName,
            u.id AS userId,
            u.firstName AS firstName,
            u.lastName AS lastName,
            u.birthday AS birthday,
            u.gender as gender,
            c.id AS categoryId,
            c.name AS categoryName');
        $scores = array();
        // Add all user object to array
        foreach ($results as $i => $result) {
            $s = new SectionModel($result['sectionId'], $result['sectionName']);
            $c = new CategoryModel($result['categoryId'], $result['categoryName']);
            $u = new UserModel($result['userId'], $result['firstName'], $result['lastName'], $result['birthday'], $result['gender'], $c);
            $r = new ScoreModel($result['id'], $s, $u, $result['score']);
            $scores[] = $r->toArray();
        }
        return $scores;
    }