public function save($experiment, Request $request) { $experiment = Experiment::findOrFail($experiment); $experiment->group = json_encode($request->recht); $experiment->save(); return redirect("/freigabe/" . $experiment->id); }
/** * 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(); } }
public static function fromTargetAndDecoys($target, $decoys) { $experiment = Experiment::create(); $targets = array_merge($decoys, []); $targets[] = $target; shuffle($targets); $experiment->targets()->saveMany($targets); // $experiment->target()->save($target); // $experiment->decoys()->saveMany($decoys); $experiment->save(); return $experiment; }
public function order(Request $request) { if (!Auth::check()) { return redirect('/auth/login'); } $order = explode(",", $_POST['order']); $experiment = Experiment::find($_POST['experiment']); $experiment->element_id = $order[0]; $experiment->save(); for ($i = 0; $i < count($order) - 1; $i++) { $element = Element::find($order[$i]); $element->element_id = $order[$i + 1]; $element->save(); } return "ok"; }
public function show($id) { $experiment = Experiment::findOrFail($id); $elements = array(); $element = Element::find($experiment->element_id); $xor2 = array(); $xorElement = null; $antworten = array(); while ($element != null) { if ($element->type == 5) { $xorElement = $element; $xor = json_decode($element->ref, true); $xor2 = array(); if (is_array($xor)) { foreach ($xor as $pkey => $pfad) { foreach ($pfad as $ekey => $el) { $ele = Element::find($el); if ($ele->type == 2 or $ele->type == 3 or $ele->type == 4) { $xor2[$pkey][$ekey] = $ele; $antwortenObjects = Answer::where("element", $ele->id)->where("experiment", $id)->get(); foreach ($antwortenObjects as $antwortObject) { $antworten[$antwortObject->student][$ele->id] = $antwortObject; } } } } } } elseif ($element->type == 2 or $element->type == 4 or $element->type == 3) { $elements[] = Element::findOrFail($element->id); $antwortenObjects = Answer::where("element", $element->id)->where("experiment", $id)->get(); foreach ($antwortenObjects as $antwortObject) { $antworten[$antwortObject->student][$element->id] = $antwortObject; } } $element = $element->next(); } $elements = array_merge($elements, [$xor2]); $fields = array(); $fields2 = Field::all(); foreach ($fields2 as $field) { $fields[$field->id] = $field; } return view('auswertung.show', compact('experiment', 'elements', 'antworten', 'xorElement', 'fields')); }
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"; }
/** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { if (!Auth::check()) { return redirect('/auth/login'); } $experiment = Experiment::findOrFail($id); $experiment->delete(); return redirect('experiment'); }
/** * 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')); }); }
/** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { $experiment = Experiment::findOrFail($id); if ($experiment->trials()->count() > 0) { Session::flash('message', "Experiment {$id} Could Not Be Deleted. It is part of a Trial"); } else { $experiment->delete(); Session::flash('message', "Experiment {$id} Deleted"); } return redirect('experiments'); }