Пример #1
0
 /**
  * Make sure the user's trials are up to date with any newly
  * created experiments
  *
  * @param $user
  */
 public static function refreshTrials($user)
 {
     // add in new experiments
     $trial_ids = array_keys($user->trials->keyBy('experiment_id')->toArray());
     $now = Carbon::now();
     $yesterday = $now->copy();
     $yesterday->subHour(24);
     $newExperiments = Experiment::where('start_date', '<=', $now)->where('start_date', '>', $yesterday)->whereNotIn('id', $trial_ids)->get();
     foreach ($newExperiments as $experiment) {
         $t = new Trial();
         $t->experiment()->associate($experiment);
         $user->trials()->save($t);
     }
     // make sure one trial is unlocked and not complete
     $active = $user->trials()->whereLocked(false)->whereComplete(false)->get();
     if ($active->count() > 1) {
         throw new \Exception('More than 1 active trial');
     }
     if ($active->count() == 0 && $user->trials()->whereLocked(true)->whereComplete(false)->count() > 0) {
         // user has trials available, but all are locked, so unlock the first
         $active = $user->trials()->whereLocked(true)->whereComplete(false)->first();
         $active->locked = false;
         $active->save();
     }
 }
Пример #2
0
 /**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     for ($i = 0; $i < 50; $i++) {
         $t = new Trial();
         $t->researcher_id = rand(1, 4);
         $t->name = 'a test ' . rand();
         $t->save();
     }
     //
 }
Пример #3
0
 /**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     $maxParticipantID = Participant::count();
     $maxTrialID = Trial::count();
     for ($i = 0; $i < 1000; $i++) {
         $p = new Participation();
         $p->participant_id = rand(1, $maxParticipantID);
         $p->trial_id = rand(1, $maxTrialID);
         $p->save();
     }
     //
 }
Пример #4
0
 public function statistics()
 {
     $users = User::whereHas('trials', function ($query) {
         $query->where('complete', true);
     })->get();
     $totalUsers = $users->count();
     $totalTrials = Trial::whereComplete(true)->count();
     $totalHits = Trial::whereComplete(true)->has('hits', '>', 0)->count();
     $totalSelections = Selection::whereHas('trial', function ($query) {
         $query->where('complete', true);
     })->count();
     $totalChoices = $totalTrials * 5;
     $targets = Target::has('expiredExperiment')->get();
     //        $targets = Target
     return view('pages.statistics')->with(compact('totalUsers', 'totalTrials', 'totalHits', 'totalSelections', 'totalChoices', 'targets'));
 }
Пример #5
0
 public function walkthrough(Request $request)
 {
     $trial = Trial::findOrFail($request->trialId);
     if ($trial->expired()) {
         $trial->delete();
         Trial::refreshTrials($request->user());
         Session::flash('message', 'The last trial has expired. Please try the next one.');
         return redirect('/home');
     }
     switch ($request->stage) {
         case 'start':
             $trial->stage = 'view';
             $trial->save();
             return redirect('/home');
         case 'view':
             $trial->stage = 'evaluate';
             // skipping feedback for now
             $trial->save();
             return redirect('/home');
         case 'feedback':
             $trial->notes = $request->notes;
             $trial->stage = 'evaluate';
             $trial->save();
             return redirect('/home');
         case 'evaluate':
             $this->validate($request, ['targets' => 'required']);
             $this->saveChoices($trial, $request->targets);
             $trial->stage = 'confirm';
             $trial->save();
             return redirect('/home');
         case 'confirm':
             if ($request->Edit) {
                 $trial->stage = 'evaluate';
                 $trial->save();
             } else {
                 if ($request->Confirm) {
                     $trial->complete = true;
                     $trial->save();
                 }
             }
             return redirect('/home');
     }
 }
Пример #6
0
 public function trial($trialid)
 {
     $data['trial'] = Trial::find($trialid);
     $data['researcher'] = Researcher::find(2);
     $data['myTrials'] = Trial::where('researcher_id', '=', $data['researcher']->id)->get();
     $data['pageTitle'] = 'Viewing a trial';
     $data['participants'] = $data['trial']->participants()->get();
     $male = $female = 0;
     foreach ($data['participants'] as $p) {
         $p->sex == 'm' ? $male++ : $female++;
     }
     $chartData = ['labels' => ['Total', 'Male', 'Female'], 'datasets' => [['data' => [$male + $female, $male, $female]]]];
     $data['chartData'] = json_encode($chartData);
     //        var data = {
     //            labels: ["January", "February", "March", "April", "May", "June", "July"],
     //    datasets: [
     //        {
     //            label: "My First dataset",
     //            fillColor: "rgba(220,220,220,0.5)",
     //            strokeColor: "rgba(220,220,220,0.8)",
     //            highlightFill: "rgba(220,220,220,0.75)",
     //            highlightStroke: "rgba(220,220,220,1)",
     //            data: [65, 59, 80, 81, 56, 55, 40]
     //        },
     //        {
     //            label: "My Second dataset",
     //            fillColor: "rgba(151,187,205,0.5)",
     //            strokeColor: "rgba(151,187,205,0.8)",
     //            highlightFill: "rgba(151,187,205,0.75)",
     //            highlightStroke: "rgba(151,187,205,1)",
     //            data: [28, 48, 40, 19, 86, 27, 90]
     //        }
     //    ]
     //};
     return view('adminlte\\sections\\trial', $data);
 }
 /**
  * Bootstrap the application services.
  *
  * @return void
  */
 public function boot()
 {
     // compose the home page view showing user's trials
     view()->composer('pages.home', function ($view) {
         $user = Auth::user();
         \App\Trial::refreshTrials($user);
         // get the active trial
         $active = $user->trials()->whereLocked(false)->whereComplete(false)->first();
         // get the target and expiration for the active trial
         $target = null;
         $expiration = null;
         if ($active) {
             $target = $active->experiment->target->first();
             $expiration = $active->experiment->start_date->copy();
             $expiration->addDays(1);
         }
         // get the next available experiment
         $next = Experiment::where('start_date', '>', Carbon::now())->orderBy('start_date', 'asc')->first();
         // attach the data to the view
         $view->with(compact('active', 'next', 'target', 'expiration'));
     });
     // compose the history page view
     view()->composer('trials.history', function ($view) {
         $user = Auth::user();
         $history = $user->trials()->whereComplete(true)->get();
         $chance = 0;
         $actual = 0;
         if ($history->count() > 0) {
             // stats
             $totalTrials = 0;
             $totalSelections = 0;
             $totalTargets = 0;
             $totalHits = 0;
             foreach ($history as $trial) {
                 if ($trial->expired()) {
                     // only do stats on trials that have expired and
                     // thus have had the target and correct answers revealed
                     $totalTrials++;
                     $totalTargets += $trial->experiment->targets()->count();
                     $totalSelections += $trial->selections()->count();
                     if ($trial->success()) {
                         $totalHits++;
                     }
                 }
             }
             //
             //                $totalTrials = $user->trials()
             //                    ->whereComplete(true)
             //                    ->count();
             //                $totalSelections = $user->selections()->count();
             //                $totalTargets = $totalTrials * 5;
             //                $totalHits = $user->trials()->has('hits', 1)->count();
             if ($totalTrials && $totalTargets) {
                 $chance = $totalSelections / $totalTargets * 100;
                 // as a percentage
                 $actual = $totalHits / $totalTrials * 100;
                 // as a percentage
             }
         }
         $view->with(compact('history', 'chance', 'actual'));
     });
 }