/** * Create prediction action * @return string */ public function create() { $response = array('success' => false); if (Request::ajax()) { $postData = Input::all(); $tabletId = $postData['tabletId']; $predictionName = $postData['prediction']; $predictionValue = $postData['value']; $rules = array('prediction' => array('required', 'max:20'), 'value' => array('required', 'numeric', 'min:0')); $messages = array('prediction.required' => 'Please enter prediction name.', 'prediction.max' => 'Prediction name shouldn\'t be bigger than 20 chars.', 'prediction.unique' => 'You arleady have a prediction with this name.', 'value.required' => 'Please enter prediction value.', 'value.numeric' => 'Prediction value must be a numeric value. Ex: 90, 3.42', 'value.min' => 'Value must be a positive number.'); $validator = Validator::make($postData, $rules, $messages); if ($validator->fails()) { $messages = $validator->messages(); $response['predictionMsg'] = $messages->first('prediction'); $response['valueMsg'] = $messages->first('value'); return Response::json($response); } $prediction = new Prediction(); $prediction->tablet_id = $tabletId; $prediction->name = $predictionName; $prediction->predicted = $predictionValue; $prediction->value = $predictionValue; $prediction->save(); $response['predictionId'] = $prediction->id; $response['success'] = true; } return Response::json($response); }
protected function _populateTabletWithPreviousData($newTablet) { $lastInactiveTablet = $this->_getCurrentUser()->getLastInactiveTablet(); $oldPredictions = $lastInactiveTablet->predictions; if (count($oldPredictions)) { foreach ($oldPredictions as $oldPrediction) { $prediction = new Prediction(); $prediction->tablet_id = $newTablet->id; $prediction->name = $oldPrediction->name; $startingSum = $oldPrediction->getTotalExpenses() ? $oldPrediction->getTotalExpenses() : $oldPrediction->predicted; $prediction->predicted = $startingSum; $prediction->value = $startingSum; $prediction->save(); } } }