public function add_rule($hash_id, Request $request)
 {
     $enfermedad = Disease::findOrFail($this->decode($hash_id));
     $sintomas = Symptom::orderBy('name', 'asc')->lists('name', 'id')->toArray();
     $reglas = Rule::with('symptom')->where('disease_id', $enfermedad->id)->get()->groupBy('number');
     if ($request->isMethod('post')) {
         $this->validate($request, ['sintomas' => 'required']);
         if ($this->checkIfRuleExists($request->sintomas)) {
             alert('La regla ya existe, por favor ingrese otros síntomas', 'danger');
             return redirect()->back();
         }
         $lastRule = Rule::orderBy('id', 'desc')->first();
         if (is_null($lastRule)) {
             $numberRule = 1;
         } else {
             $numberRule = intval($lastRule->number) + 1;
         }
         $symptoms = Symptom::findOrFail($request->sintomas);
         foreach ($symptoms as $symptom) {
             $rule = new Rule();
             $rule->number = $numberRule;
             $rule->disease()->associate($enfermedad);
             $rule->symptom()->associate($symptom);
             $rule->save();
         }
         alert('Se registró la regla con éxito');
         return redirect()->back();
     }
     return view('admin.disease.add_rule')->with('enfermedad', $enfermedad)->with('reglas', $reglas)->with('sintomas', $sintomas);
 }
 public function index()
 {
     $countDiagnostic = count(auth()->user()->diagnostics);
     $countDiseases = Disease::count();
     $countSymptom = Symptom::count();
     $diagnostics = Diagnostic::with('disease')->where('user_id', auth()->user()->id)->orderBy('created_at', 'desc')->take(5)->get();
     return view('user.home')->with('countSymptom', $countSymptom)->with('countDiseases', $countDiseases)->with('countDiagnostic', $countDiagnostic)->with('diagnostics', $diagnostics);
 }
 public function home()
 {
     $countDiseases = Disease::count();
     $countSymptom = Symptom::count();
     $countDiagnostic = Diagnostic::count();
     $lastRegistered = User::orderBy('created_at', 'desc')->take(9)->get();
     return view('admin.home')->with('countDiagnostic', $countDiagnostic)->with('countSymptom', $countSymptom)->with('countDiseases', $countDiseases)->with('lastRegistered', $lastRegistered);
 }
 public function user_diseases()
 {
     $data = [];
     $diagnostics = Diagnostic::where('user_id', auth()->user()->id)->get();
     $groupDiagnostics = $diagnostics->groupBy('disease_id');
     $groupDiagnostics->each(function ($value, $key) use(&$data) {
         $disease = Disease::findOrFail($key);
         $data[] = ['label' => $disease->name, 'value' => count($value)];
     });
     return response()->json($data);
 }
 public function index()
 {
     $enfermedades = Disease::with('rules')->orderBy('name', 'asc')->paginate(10);
     return view('user.disease.index')->with('enfermedades', $enfermedades);
 }
 /**
  * Función que calcula la evolución de diagnósticos por enfermedad, anualmente
  * se usa -1 porque Morris toma valores desde un array con index 0
  */
 public function anual_state_diagnostics($state_id)
 {
     $names = $indexs = [];
     $diseases = Disease::all();
     $diagnostics = Diagnostic::whereHas('user', function ($query) use($state_id) {
         $query->where('state_id', $state_id);
     })->with('user')->get();
     // llenamos un arreglo de indices y otro de nombres para la grafica
     $diseases->each(function ($disease) use(&$names, &$indexs) {
         $indexs[] = $disease->id - 1;
         $names[$disease->id - 1] = $disease->name;
     });
     $months = array_months();
     $today = Carbon::create(null, null, 1);
     $newDate = $today->copy();
     // Por cada mes mostramos cuantos diagnosticos se realizaron
     foreach ($months as $keyMonth => $value) {
         if ($keyMonth != 1) {
             $newDate->subDay();
             $newDate->startOfMonth();
         }
         foreach ($diseases as $disease) {
             $aux[$disease->id - 1] = 0;
         }
         foreach ($diagnostics as $diagnostic) {
             if ($diagnostic->created_at->month == $newDate->month && $diagnostic->created_at->year == $newDate->year) {
                 $aux[$diagnostic->disease_id - 1]++;
             }
         }
         $arrayMonth = ['month' => $value];
         // aca no se utiliza merge, para que no reindexe la union de arrays
         $result[] = $arrayMonth + $aux;
     }
     return response()->json(['names' => $names, 'indexs' => $indexs, 'result' => $result]);
 }