<?php $habit = $app['controllers_factory']; $habit->get('/current_habits', function () use($app) { $count = 0; return $app['twig']->render('habit/current_habits.html.twig', array('habits' => Habit::getAll())); }); $habit->get('/habits/{id}', function ($id) use($app) { $habit = Habit::find($id); return $app['twig']->render('habit/habit_edit.html.twig', array('habit' => $habit)); }); $habit->patch('/habits/{id}', function ($id) use($app) { $habit = Habit::find($id); $name = $_POST['name']; $motivation = $_POST['motivation']; $interval_days = $_POST['interval_days']; $habit->updateName($name); $habit->updateMotivation($motivation); $habit->updateIntervalDays($interval_days); return $app['twig']->render('habit/habit_edit.html.twig', array('habit' => $habit)); }); $habit->delete('/habits/{id}', function ($id) use($app) { $habit = Habit::find($id); $habit->delete(); return $app['twig']->render('habit/current_habits.html.twig', array('habits' => Habit::getAll())); }); // Place all urls in this file at /habit/* $app->mount('/habit', $habit);
function testFind() { $name = "Meditate"; $motivation = "Clarity"; $interval_days = 3; $id = 4; $completed = False; $test_habit = new Habit($name, $motivation, $interval_days, $completed, $id); $test_habit->save(); $name2 = "Go running"; $motivation2 = "Fitness"; $interval_days2 = 35; $id2 = 5; $completed2 = False; $test_habit2 = new Habit($name2, $motivation2, $interval_days2, $completed2, $id2); $test_habit2->save(); $result = Habit::find($test_habit->getId()); $this->assertEquals($test_habit, $result); }
function getDaysCompleted($habit_id) { $this_habit = Habit::find($habit_id); $days = $GLOBALS['DB']->query("SELECT day_id FROM daily_completed WHERE complete_today = true AND habit_id = {$habit_id};"); $count = 0; foreach ($days as $day) { $count++; } return $count; }