/**
  * Get survey(s)
  * 
  * Call examples :
  *  /survey : get all survey the user can view TODO check heaviness
  *  /survey/<id> : get survey from its id
  * 
  * @param int $id survey id
  * 
  * @return mixed
  * 
  * @throws RestBadParameterException
  * @throws RestOwnershipRequiredException
  * @throws RestSurveyNotFoundException
  * @throws RestNotAllowedException
  */
 public static function get($id = null)
 {
     if ($id) {
         // Trying to get a single survey
         try {
             $survey = Survey::fromId($id);
         } catch (NotFoundException $e) {
             throw new RestSurveyNotFoundException($id);
         }
         // Check permission
         if (!$survey->can->view) {
             throw new RestNotAllowedException('view survey ' . $survey->id);
         }
         return self::cast($survey);
     }
     $surveys = array();
     foreach (Survey::all() as $survey) {
         if (!Auth::isAuthenticated()) {
             continue;
         }
         if (!Auth::isAdmin() && !Auth::user()->is($survey->owner)) {
             continue;
         }
         if (!$survey->can->view) {
             continue;
         }
         $surveys[] = self::cast($survey);
     }
     return $surveys;
 }
 public static function sandbox()
 {
     // Testaa koodiasi täällä
     $eka = Survey::find(1);
     $kaikki = Survey::all();
     Kint::dump($eka);
     Kint::dump($kaikki);
 }
 public static function index()
 {
     if (!self::check_logged_in()) {
         Redirect::to('/login', array('error' => 'You must log in to access this resource.'));
         return;
     }
     $surveys = Survey::all();
     $results = Result::all();
     View::make('survey/index.html', array('surveys' => $surveys, 'results' => $results));
 }
 /**
  * This will handle both updating and deleting group
  */
 public function updateGroup()
 {
     if ($_POST) {
         if (Input::has('delete')) {
             $group = Group::find(Input::get('id'));
             if (is_null($group)) {
                 return Redirect::to('groups/manage');
             }
             $group->delete();
             return Redirect::to('groups/manage');
         } elseif (Input::has('edit')) {
             $group = Group::find(Input::get('id'));
             if (is_null($group)) {
                 return Redirect::to('groups/manage');
             }
             return Redirect::to('group/edit')->with('update_group', $group);
         } elseif (Input::has('addremusers')) {
             $group = Group::find(Input::get('id'));
             if (is_null($group)) {
                 return Redirect::to('groups/manage');
             }
             return Redirect::action('GroupController@manageUsers', array('id' => $group->id));
         } elseif (Input::has('update')) {
             $group = Group::find(Input::get('id'));
             if (is_null($group)) {
                 return Redirect::to('groups/manage');
             }
             $group->name = Input::get('name');
             $group->description = Input::get('description');
             $group->timestart = strtotime(Input::get('timestart'));
             $group->outcome = Input::get('outcome');
             $group->survey = Input::get('survey');
             $group->postsurvey = Input::get('postsurvey');
             if (Input::hasFile('thumbnail')) {
                 $file = Input::file('thumbnail');
                 $pixpath = '/uploads/pix/group/';
                 $destinationPath = public_path() . $pixpath;
                 $filename = str_replace(" ", "_", $group->name) . '.' . $file->getClientOriginalExtension();
                 $file->move($destinationPath, $filename);
                 $group->thumbnail = base64_encode($pixpath . $filename);
             }
             $group->save();
             return Redirect::to('groups/manage');
         }
     }
     $group = Session::get('update_group');
     if (is_null($group)) {
         return Redirect::to('groups/manage');
     }
     //Outcome menu
     $outcomes = Outcome::all();
     $outcomes_menu = array();
     $outcomes_menu[0] = trans('master.choose');
     foreach ($outcomes as $outcome) {
         $outcomes_menu[$outcome->id] = trans($outcome->name);
     }
     //Survey menu
     $surveys = Survey::all();
     $surveys_menu = array();
     $surveys_menu[0] = trans('master.choose');
     foreach ($surveys as $survey) {
         $surveys_menu[$survey->id] = trans($survey->name);
     }
     return $this->layout->content = View::make('group.edit')->with('outcomes', $outcomes_menu)->with('surveys', $surveys_menu)->with('group', $group);
 }
 public function manageSurveys()
 {
     // TODO: this function will list the surveys
     $surveys = Survey::all();
     $this->layout->content = View::make('survey.manage')->with('surveys', $surveys);
 }