/**
  * Display the specified resource.
  *
  * @param  int  $id
  * @return Response
  */
 public function show($schoolName, $surveyName)
 {
     $school = School::where('name', '=', $schoolName)->first();
     if (!$school) {
         return Redirect::to('/schools/');
     }
     $survey = Survey::where('name', '=', $surveyName)->first();
     if (!$survey) {
         return Redirect::to("/schools/{$schoolName}");
     }
     $groupStats = array();
     foreach (Group::all() as $group) {
         $possibleLink = $group->surveys()->where('survey_id', $survey->id)->first();
         $active = !is_null($possibleLink);
         $groupInfo = array('group' => $group, 'open_time' => 'no time set', 'close_time' => 'no time set');
         if ($active) {
             $active = strtotime($possibleLink->pivot->open_time) < time() && strtotime($possibleLink->pivot->close_time) > time();
             if (strtotime($possibleLink->pivot->open_time)) {
                 $groupInfo['open_time'] = $possibleLink->pivot->open_time;
             }
             if (strtotime($possibleLink->pivot->close_time)) {
                 $groupInfo['close_time'] = $possibleLink->pivot->close_time;
             }
         }
         $groupInfo['active'] = $active;
         array_push($groupStats, $groupInfo);
     }
     return View::make('content.surveyShow')->with('school', $school)->with('survey', $survey)->with('groupStats', $groupStats);
 }
 /**
  * Display the specified resource.
  *
  * @param  int  $id
  * @return Response
  */
 public function show($schoolName, $surveyName, $groupName)
 {
     if (Input::has('password')) {
         return Redirect::to(Request::path())->with('password', Input::get('password'));
     } else {
         if (Input::has('passwordSubmit')) {
             return Redirect::to(Request::path())->with('emptyError', 1);
         } else {
             if (Session::has('emptyError')) {
                 return View::make('content.surveyGroupShow')->with('error', 'You must enter a password.');
             }
         }
     }
     //Loading all questions for a particular survey and group.
     $school = School::where('name', '=', $schoolName)->first();
     if (!$school) {
         return Redirect::to('/schools');
     }
     $survey = Survey::where('name', '=', $surveyName)->where('school_id', $school->id)->first();
     if (!$survey) {
         return Redirect::to("/schools/{$schoolName}");
     }
     $group = $survey->groups()->where('name', $groupName)->first();
     if (!$group || strtotime($group->pivot->open_time) > time() || strtotime($group->pivot->close_time) < time()) {
         return Redirect::to("schools/{$schoolName}/{$surveyName}");
     }
     $loggedIn = Auth::check();
     if ($loggedIn) {
         $user = Auth::user();
         $authName = $user->group->name;
         if ($authName != $groupName) {
             $firstAn = "a";
             $secondAn = "a";
             if (strstr("aeiou", strtolower($authName[0]))) {
                 $firstAn = "an";
             }
             if (strstr("aeiou", strtolower($groupName[0]))) {
                 $secondAn = "an";
             }
             return View::make('content.surveyGroupShow')->with('state', 'error')->with('error', "You are currently logged into {$firstAn} {$authName} account, and not {$secondAn} {$groupName} account.");
         }
         if ($user->name == "Anonymous" && !Session::has('remainAnon')) {
             return View::make('content.surveyGroupShow')->with('state', 'options');
         }
         $questionStore = Survey::parseQuestionStore(File::get(app_path() . "/questions/{$survey->id}.qs"), $school->id, true);
         if (!$questionStore[0]) {
             throw new Exception("Problem getting question file parsed.");
         }
         $path = app_path() . "/answers/{$user->id}.ans";
         $answers = array();
         if (File::exists($path)) {
             $answers = json_decode(File::get($path), true)[$user->id];
         }
         //dd($answers);
         return View::make('content.surveyGroupShow')->with('answers', $answers)->with('state', 'survey')->with('message', "This is the {$surveyName} survey for all members of the {$groupName} group.")->with('questionStore', $questionStore[1])->with('groupName', $group->name)->with('closeTime', $group->pivot->close_time);
     } else {
         if ($groupName == "Admin") {
             return View::make('content.surveyGroupShow')->with('state', 'error')->with('error', "You must be logged in as an Admin to take the Admin survey.");
         } else {
             if (Session::has('password')) {
                 if (Hash::check(Session::get('password'), $group->password)) {
                     return View::make('content.surveyGroupShow')->with('state', 'options');
                 } else {
                     return View::make('content.surveyGroupShow')->with('error', 'You entered the wrong password. Try again.');
                 }
             } else {
                 return View::make('content.surveyGroupShow');
             }
         }
     }
 }
 public function getSurveyList()
 {
     $input = Input::all();
     if ($input['type'] == 'pre') {
         $group = Group::where('id', $input['groupid'])->pluck('survey');
         //print_r($group);
         $survey = Survey::where('id', $group)->get();
     } else {
         $group = Group::where('id', $input['groupid'])->pluck('postsurvey');
         $survey = Survey::where('id', $group)->get();
     }
     $ret = str_replace('{"fields":', "", rtrim(unserialize($survey[0]->data)[0]));
     $ret = rtrim($ret, "}");
     return $ret;
 }