/** * Returns the data model based on the primary key given in the GET variable. * If the data model is not found, an HTTP exception will be raised. * @param integer $id the ID of the model to be loaded * @return LiikuntaPaivakirja the loaded model * @throws CHttpException */ public function loadModel($id) { $model = LiikuntaPaivakirja::model()->findByPk($id); if ($model === null) { throw new CHttpException(404, 'The requested page does not exist.'); } return $model; }
public function actionLiikunta_paivakirja() { if (isset($_POST['kestoMuutos'])) { $li = LiikuntaPaivakirja::model()->findbypk($_POST['liikunta_id']); if (isset($li->id)) { $profiili = Profile::model()->find(" user_id = '" . Yii::app()->user->id . "' "); $ika = 0; $paino = 0; $pituus = 0; $sukupuoli = 0; if (!empty($profiili->ppkkvvvv)) { $ika = date("Y") - date("Y", strtotime($profiili->ppkkvvvv)); } if (!empty($profiili->paino)) { $paino = $profiili->paino; } if (!empty($profiili->pituus)) { $pituus = $profiili->pituus; } if (!empty($profiili->sukupuoli)) { $sukupuoli = $profiili->sukupuoli; } if ($sukupuoli == 1) { $spuoliValue = -161; } elseif ($sukupuoli == 2) { $spuoliValue = 5; } $la = $li->value; $s = 10 * $paino / 1 + 6.25 * $pituus / 1 - 5 * $ika / 1 + $spuoliValue; $tulos = $s / 24 * $la * 1; $tulos = round($tulos, 2); $energiankulutus = $tulos / 60 * $_POST['uusiValue']; $energiankulutus = round($energiankulutus); $lu = LiikuntaUser::model()->updatebypk($_POST['id'], array('kesto' => $_POST['uusiValue'], 'energiankulutus' => $energiankulutus)); if ($lu) { echo 'OK'; } exit; } } if (isset($_POST['pvm'])) { $criteria = new CDbCriteria(); $criteria->condition = "\n\t\t\tuser_id='" . Yii::app()->user->id . "'\n\t\t\tAND pvm='" . date("Y-m-d", strtotime($_POST['pvm'])) . "'\n\t\t"; $data = LiikuntaUser::model()->findAll($criteria); $energiankulutus = 0; $kesto = 0; $bod = ''; $bod .= ' <div class="row"> <div class="col-sm-5"> <div class="panel panel-primary"> <div class="panel-heading">Urheilu</div> <div class="panel-body"> <div class="tauluYhteensa"> <table class="table table-striped"> <tr> <th><input type="checkbox" class="selAll"></th> <th>Laji</th> <th>Energia (kcal)</th> <th>Kesto (min)</th> </tr>'; if (isset($data[0])) { foreach ($data as $m) { $energiankulutus += (double) $m->energiankulutus; $kesto += (double) $m->kesto; $bod .= ' <tr> <td><input type="checkbox" value="' . $m->id . '"></td> <td>' . $m->laji . '</td> <td>' . $m->energiankulutus . '</td> <td><a href="#" class="link kestoMuutos" for="' . $m->id . '" liikunta_id="' . $m->liikunta_id . '" tnimi="' . $m->laji . '"><b>' . $m->kesto . '</b></a></td> </tr> '; } } else { $bod .= ' <tr> <td></td> <td></td> <td>0</td> <td>0</td> </tr> '; } $bod .= ' <tfoot> <tr> <th></th> <th>Yhteensä</th> <th>' . $energiankulutus . '</th> <th>' . $kesto . '</th> </tr> </tfoot> '; $bod .= ' </table> <div class="valikkoAndPvm form-inline" style="display:none"> <div class="form-group">'; if ($this->user_agent() == 'ios' or $this->user_agent() == 'android') { $bod .= '<input type="date" class="form-control input-sm valPvm" placeholder="Valitse pvm..">'; } elseif ($this->user_agent() == 'windowsPhone') { $bod .= '<input type="text" class="form-control input-sm valPvm datepicker" placeholder="Valitse pvm..">'; } else { $bod .= '<input type="text" class="form-control input-sm valPvm datepicker" placeholder="Valitse pvm..">'; } $bod .= ' </div> <button class="btn btn-sm btn-primary ok" title="Valmis">Suorita</button> <br><br> </div> <button class="btn btn-sm btn-danger poistaVal" title="Poista">Poista</button> <button class="btn btn-sm btn-info copyVal" title="Kopioi toiseen pvm:n">Kopioi</button> <button class="btn btn-sm btn-primary sirraVal" title="Siirrä toiseen pvm:n">Siirrä</button> </div> </div> </div> '; $criteria = new CDbCriteria(); $criteria->order = " id DESC"; $criteria->condition = "\n\t\t\tuser_id='" . Yii::app()->user->id . "'\n\t\t\tAND YEARWEEK(pvm, 1)='" . date("YW", strtotime($_POST['pvm'])) . "'\n\t\t\tAND kvko!='' AND min!=''\n\t\t"; $mk = LiikuntaUser::model()->find($criteria); $min = 0; $kvko = 0; if (isset($mk->id)) { $kvko = $mk->kvko; $min = $mk->min; } else { $profiili = Profile::model()->findbypk(Yii::app()->user->id); if ($profiili->liikunta != 0 and $profiili->maarita_itse == '') { if ($profiili->liikunnan_kesto_viikossa == 0) { $min = 50; } if ($profiili->liikunnan_kesto_viikossa == 1) { $min = 70; } if ($profiili->liikunnan_kesto_viikossa == 2) { $min = 140; } if ($profiili->liikunnan_kesto_viikossa == 3) { $min = 300; } if ($profiili->liikunnan_kesto_viikossa == 4) { $min = 430; } $kvko = $profiili->liikunta_kertojen_maara_viikossa; } elseif ($profiili->liikunta != 0 and $profiili->liikunta_kertojen_maara_viikossa != 0 and $profiili->maarita_itse != '') { $min = $profiili->maarita_itse; $kvko = $profiili->liikunta_kertojen_maara_viikossa; } elseif ($profiili->liikunta == 0) { $min = 315; $kvko = 7; } } $criteria = new CDbCriteria(); $criteria->order = " id DESC"; $criteria->condition = "\n\t\t\tuser_id='" . Yii::app()->user->id . "'\n\t\t\tAND pvm='" . $_POST['pvm'] . "'\n\t\t"; $m = LiikuntaUser::model()->find($criteria); $p = ''; $vkTavoite = 0; $tavYht = 0; $week = date("W", strtotime($_POST['pvm'])); $year = date("Y", strtotime($_POST['pvm'])); $days = array(1 => 'Ma', 2 => 'Ti', 3 => 'Ke', 4 => 'To', 5 => 'Pe', 6 => 'La', 7 => 'Su'); $bod .= ' </div><div class="col-sm-7"> <div class="panel panel-primary"> <div class="panel-heading">Viikkotavoite</div> <div class="panel-body"> <div class="table-responsive"> <table class="table table-striped small">'; $bod .= '<tr>'; foreach ($days as $key => $val) { $p = date("d.m", strtotime($year . "-W" . $week . "-" . $key)); $bod .= '<th>' . $val . '<br>' . $p . '</th>'; } $bod .= '<th>Viikkotavoite</th>'; $bod .= '</tr><tr>'; function tav($kestoYht, $min, $kvko) { $lasketaan = $kestoYht / round($min / $kvko, 1) * 100; $bod = '<td>' . round($min / $kvko, 1) . 'min</td>'; return $bod; } foreach ($days as $key => $val) { $p = date("Y-m-d", strtotime($year . "-W" . $week . "-" . $key)); $criteria = new CDbCriteria(); $criteria->condition = "\n\t\t\tuser_id='" . Yii::app()->user->id . "'\n\t\t\tAND pvm='" . $p . "'\n\t\t"; $data = LiikuntaUser::model()->findAll($criteria); $kestoYht = 0; $lasketaan = 0; $mpvm = array(); foreach ($data as $m) { $kestoYht += $m->kesto; $tavYht += $m->kesto; } if ($min > 0 and $kvko > 0 and $kvko == 1 and $key == 3) { // yksi kerta vkossa $bod .= tav($kestoYht, $min, $kvko); } elseif ($min > 0 and $kvko > 0 and $kvko == 2 and ($key == 1 or $key == 5)) { // kaksi kerta vkossa $bod .= tav($kestoYht, $min, $kvko); } elseif ($min > 0 and $kvko > 0 and $kvko == 3 and ($key == 1 or $key == 3 or $key == 5)) { // kolme kerta vkossa $bod .= tav($kestoYht, $min, $kvko); } elseif ($min > 0 and $kvko > 0 and $kvko == 4 and ($key == 1 or $key == 3 or $key == 5 or $key == 7)) { // nelja kerta vkossa $bod .= tav($kestoYht, $min, $kvko); } elseif ($min > 0 and $kvko > 0 and $kvko == 5 and ($key == 1 or $key == 2 or $key == 3 or $key == 4 or $key == 5)) { // viisi kerta vkossa $bod .= tav($kestoYht, $min, $kvko); } elseif ($min > 0 and $kvko > 0 and $kvko == 6 and ($key == 1 or $key == 2 or $key == 3 or $key == 4 or $key == 5 or $key == 6)) { // kuusi kerta vkossa $bod .= tav($kestoYht, $min, $kvko); } elseif ($min > 0 and $kvko > 0 and $kvko == 7 and ($key == 1 or $key == 2 or $key == 3 or $key == 4 or $key == 5 or $key == 6 or $key == 7)) { // setseman kerta vkossa $bod .= tav($kestoYht, $min, $kvko); } else { $bod .= '<td>0min</td>'; } } $vkTavoite = 0; if ($tavYht > 0 and $min > 0) { $vkTavoite = $tavYht / $min * 100; } $bod .= '<td>' . $min . 'min</td> </tr> </table> </div> </div> </div>'; $bod .= ' <div class="panel panel-primary"> <div class="panel-heading">Suoritettu</div> <div class="panel-body"> <div class="table-responsive">'; $bod .= '<table class="table table-striped small"> <tr>'; foreach ($days as $key => $val) { $p = date("d.m", strtotime($year . "-W" . $week . "-" . $key)); $bod .= '<th>' . $val . '<br>' . $p . '</th>'; } $bod .= '<th>Yhteensä</th>'; $bod .= '</tr> '; $bod .= '<tr>'; $minYht = 0; foreach ($days as $key => $val) { $p = date("Y-m-d", strtotime($year . "-W" . $week . "-" . $key)); $criteria = new CDbCriteria(); $criteria->condition = "\n\t\t\tuser_id='" . Yii::app()->user->id . "'\n\t\t\tAND pvm='" . $p . "'\n\t\t"; $data = LiikuntaUser::model()->findAll($criteria); $kestoYht = 0; $lasketaan = 0; foreach ($data as $m) { $kestoYht += $m->kesto; $tavYht += $m->kesto; } $la = 0; $lasketaan = $kestoYht / round($min / $kvko, 1) * 100; if ($min > 0 and $kvko > 0) { $la = round($lasketaan / 100 * ($min / $kvko), 1); } if ($kvko == 1 and $key == 3) { // yksi kerta vkossa $minYht += $la; $bod .= '<td>' . $la . 'min<br>' . round($lasketaan, 1) . '%</td>'; } elseif ($kvko == 2 and ($key == 1 or $key == 5)) { // kaksi kerta vkossa $minYht += $la; $bod .= '<td>' . $la . 'min<br>' . round($lasketaan, 1) . '%</td>'; } elseif ($kvko == 3 and ($key == 1 or $key == 3 or $key == 5)) { // kolme kerta vkossa $minYht += $la; $bod .= '<td>' . $la . 'min<br>' . round($lasketaan, 1) . '%</td>'; } elseif ($kvko == 4 and ($key == 1 or $key == 3 or $key == 5 or $key == 7)) { // nelja kerta vkossa $minYht += $la; $bod .= '<td>' . $la . 'min<br>' . round($lasketaan, 1) . '%</td>'; } elseif ($kvko == 5 and ($key == 1 or $key == 2 or $key == 3 or $key == 4 or $key == 5)) { // viisi kerta vkossa $minYht += $la; $bod .= '<td>' . $la . 'min<br>' . round($lasketaan, 1) . '%</td>'; } elseif ($kvko == 6 and ($key == 1 or $key == 2 or $key == 3 or $key == 4 or $key == 5 or $key == 6)) { // kuusi kerta vkossa $minYht += $la; $bod .= '<td>' . $la . 'min<br>' . round($lasketaan, 1) . '%</td>'; } elseif ($kvko == 7 and ($key == 1 or $key == 2 or $key == 3 or $key == 4 or $key == 5 or $key == 6 or $key == 7)) { // setseman kerta vkossa $minYht += $la; $bod .= '<td>' . $la . 'min<br>' . round($lasketaan, 1) . '%</td>'; } else { $minYht += $la; $bod .= '<td>' . $la . 'min</td>'; } } if ($minYht > 0 and $min > 0) { $vkTavoite = $minYht / $min * 100; } $bod .= '<td>' . round($minYht) . 'min<br>' . round($vkTavoite, 1) . '%</td>'; $bod .= ' </tr>'; $bod .= ' </table> </div> </div> </div> </div> </div> </div>'; echo json_encode($bod); exit; } $this->render('liikunta_paivakirja'); }