Exemplo n.º 1
0
 /**
  * Getter
  * 
  * @param string $property property to get
  * 
  * @throws PropertyAccessException
  * 
  * @return property value
  */
 public function __get($property)
 {
     if (in_array($property, array('id', 'survey_id', 'user_id', 'created', 'updated', 'answers'))) {
         return $this->{$property};
     }
     if ($property == 'survey') {
         return Survey::fromId($this->survey_id);
     }
     if (in_array($property, array('user', 'owner', 'author'))) {
         return User::fromId($this->user_id);
     }
     throw new PropertyAccessException($this, $property);
 }
Exemplo n.º 2
0
<?php

$id = array_shift($path);
if (!$id) {
    throw new NotFoundException('survey', $id);
}
$survey = Survey::fromId($id);
if (!$survey->can->view) {
    throw new NotFoundException('survey access', $id);
}
?>

<pre class="row"><?php 
print_r($survey);
?>
</pre>

<pre class="row"><?php 
print_r($survey->votes);
?>
</pre>

<script type="text/javascript">
function foo(data) {
    console.log(data);
}
</script>

<script type="text/javascript" src="{url:rest.php/user/@me?callback=foo}"></script>

<?php 
Exemplo n.º 3
0
 /**
  * Delete a survey
  * 
  * @param int $id survey id
  * @param string $property
  * @param string $key id of sub-resource
  * 
  * @return bool
  * 
  * @throws RestMissingParameterException
  * @throws RestSurveyNotFoundException
  * @throws RestNotAllowedException
  */
 public static function delete($id, $property = null, $key = null)
 {
     if (!$id) {
         throw new RestMissingParameterException('id');
     }
     // Get survey
     $survey = Survey::fromId($id);
     if ($property) {
         if (!$key) {
             throw new RestBadParameterException('key');
         }
         // Check permissions
         if (!Auth::isAdmin() || !$survey->owner->is(Auth::user())) {
             throw new RestNotAllowedException('update survey ' . $survey->id);
         }
         if ($property == 'choice') {
             $survey->deleteChoice($key);
         }
         if ($property == 'guest') {
             $survey->deleteGuest($key);
         }
         $survey->save();
         return true;
     }
     // Check permissions
     if (!Auth::isAdmin() || !$survey->owner->is(Auth::user())) {
         throw new RestNotAllowedException('delete survey ' . $survey->id);
     }
     $survey->delete();
     return true;
 }
Exemplo n.º 4
0
 /**
  * Vote to a survey
  * 
  * Request body must be an object with properties :
  *   * survey_id : identifier of the survey the vote is to be added to
  *   * answers : array of objects with properties
  *     * key : choice key
  *     * value : answer value for choice
  * 
  * @return array
  * 
  * @throws RestBadParameterException
  * @throws RestMissingParameterException
  * @throws NotFoundException
  * @throws RestNotAllowedException
  */
 public static function post()
 {
     // Get creation data
     $data = RestServer::getRequest()->input;
     if (!is_object($data)) {
         throw new RestBadParameterException('vote');
     }
     // Get related survey
     if (!property_exists($data, 'survey_id')) {
         throw new RestMissingParameterException('vote.survey_id');
     }
     $survey = Survey::fromId($data->survey_id);
     // Check permissions
     if (!$survey->can->vote) {
         throw new RestNotAllowedException('add vote to survey ' . $survey->id);
     }
     // Check if vote did not exists already
     foreach ($survey->votes as $vote) {
         if ($vote->author->is(Auth::user())) {
             throw new RestVoteAlreadyExistsException($survey);
         }
     }
     // Check if there is answers
     if (!property_exists($data, 'answers')) {
         throw new RestMissingParameterException('vote.answers');
     }
     if (!is_array($data->answers)) {
         throw new RestBadParameterException('vote.answers');
     }
     // Create the vote and add answers
     $vote = Vote::create($survey);
     foreach ($data->answers as $answer) {
         if (!is_object($answer)) {
             throw new RestBadParameterException('vote.answers[]');
         }
         if (!property_exists($answer, 'key')) {
             throw new RestMissingParameterException('vote.answers[].key');
         }
         if (!property_exists($answer, 'value')) {
             throw new RestMissingParameterException('vote.answers[].value');
         }
         $vote->addAnswer($answer->key, $answer->value);
         // throws if anything wrong
     }
     $vote->save();
     return array('path' => '/vote/' . $vote->id, 'data' => self::cast($vote));
 }