/** * 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(); } }
/** * Display the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function show($id) { $experiment = Experiment::where("key", $id)->first(); if ($experiment == null) { return redirect('/'); } $element = $experiment->element()->first(); $allow[] = $experiment->element_id; $e = $experiment->element; while (Element::find($e->element_id) != null) { $allow[] = $e->element_id; if ($e->type == 5) { $fieldnr = session("field"); $field_id = substr($fieldnr, 0, -2); $field_type = substr($fieldnr, -2); $field = Field::where('id', $field_id)->where('type', $field_type)->first(); if ($field == null) { return redirect('/'); } } $e = Element::find($e->element_id); } session(['fields' => $allow]); return view('experiment.show', compact('experiment', 'element')); }
public function saveSvg($e_id, $f_id) { $experiment = Experiment::where("key", $e_id)->first(); if (!file_exists("svg/" . $experiment->id . "/")) { mkdir("svg/" . $experiment->id . "/", 0777); } $f = fopen("svg/" . $experiment->id . "/" . $f_id . "_" . session("user") . ".svg", "w"); fwrite($f, $_POST['draw']); //file_put_contents("svg/".$experiment->id."/".$f_id."_".session("user").".svg", $_POST['draw']); fclose($f); exec("/opt/local/bin/convert -background none svg/" . $experiment->id . "/" . $f_id . "_" . session("user") . ".svg svg/" . $experiment->id . "/" . $f_id . "_" . session("user") . ".png"); return "ok"; }
/** * 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')); }); }