PageRenderTime 54ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/application/models/ModelRecovery.php

https://bitbucket.org/nando123/iamperform
PHP | 304 lines | 263 code | 31 blank | 10 comment | 42 complexity | 6da3fa00c81885518a00e4fad2116ff8 MD5 | raw file
  1. <?php
  2. class ModelRecovery extends CI_Model {
  3. function getRecoveryCalendar($atletID){
  4. $sql = " SELECT a.atlet_id, a.recovery_dttm, SUM(b.recovery_point) as recovery_point FROM `master_recovery` as a
  5. LEFT JOIN master_recovery_point as b on a.point_id = b.point_id
  6. WHERE a.atlet_id = '$atletID'
  7. GROUP BY a.recovery_dttm";
  8. $query = $this->db->query($sql);
  9. $events = array();
  10. if($query->num_rows()>0){
  11. foreach($query->result() as $row){
  12. $point = $row->recovery_point;
  13. $date = $row->recovery_dttm;
  14. $e = array();
  15. $e['id'] = $date;
  16. $e['title'] = $point." Point";
  17. $e['start'] = date("Y-m-d", strtotime($date));
  18. // Merge the event array into the return array
  19. array_push($events, $e);
  20. }
  21. }
  22. return json_encode($events);
  23. }
  24. function getTableDetailRecovery($week,$start_date,$end_date,$atletID){
  25. $atletInfo = $this->ModelUsers->getAtletInfo($atletID);
  26. list($atletName,$atletID,$atletGroup,$atletEvent,$atletPic,$atletWellnessValue,$atletWellnessDate,$group_cat) = $atletInfo;
  27. $sql = " SELECT a.atlet_id,a.recovery_dttm, SUM(b.recovery_point) as recovery_point FROM master_recovery as a
  28. LEFT JOIN master_recovery_point as b on b.point_id = a.point_id
  29. WHERE a.recovery_dttm >= '$start_date'
  30. AND a.recovery_dttm <= '$end_date'
  31. AND a.atlet_id = '$atletID'
  32. GROUP BY a.recovery_dttm";
  33. $query = $this->db->query($sql);
  34. $ret = "<table class='table bordered'><thead><tr><td>Tanggal</td><td>Point</td><td>Plan</td></tr></thead>";
  35. if($query->num_rows()>0){
  36. $total = 0;
  37. foreach($query->result() as $row){
  38. $sql = "SELECT SUM(b.monotonyVolume*b.monotonyIntensity) as monotonyPerDay, a.monotonyID FROM master_monotony as a
  39. LEFT JOIN master_monotony_detail as b on b.monotonyDetailDate = '$row->recovery_dttm'
  40. WHERE a.monotonyAtletID = '$atletID'
  41. AND a.monotonyActive = '0'
  42. AND a.monotonyStartDttm = '$start_date'";
  43. $query = $this->db->query($sql);
  44. if($query->num_rows()>0){
  45. $item = $query->row();
  46. $monotonyPerDay = $item->monotonyPerDay;
  47. $monotonyID = $item->monotonyID;
  48. }else{
  49. $monotonyPerDay = "1";
  50. }
  51. $sqlx = " SELECT SUM(a.monotonyVolume*a.monotonyIntensity) as monotonyPerWeek"
  52. . " FROM master_monotony_detail as a"
  53. . " LEFT JOIN master_monotony as b on b.monotonyID = a.monotonyID"
  54. . " WHERE a.monotonyID = '$monotonyID' "
  55. . " AND b.monotonyActive = '0'";
  56. $queryx = $this->db->query($sqlx);
  57. if($queryx->num_rows()>0){
  58. $itemx = $queryx->row();
  59. if($itemx != ""){
  60. $monotonyPerWeek = $itemx->monotonyPerWeek;
  61. }else{
  62. $monotonyPerWeek = "1";
  63. }
  64. }
  65. if($group_cat == "GW400"){
  66. if($monotonyPerWeek <= 3500 OR $monotonyPerWeek < 5000){
  67. $target = 25;
  68. }else if($monotonyPerWeek <= 5000 OR $monotonyPerWeek < 6500){
  69. $target = 35;
  70. }else{
  71. $target = 40;
  72. }
  73. }else{
  74. if($monotonyPerWeek <= 2000 OR $monotonyPerWeek < 3500){
  75. $target = 25;
  76. }else if($monotonyPerWeek <= 3500 OR $monotonyPerWeek < 5000){
  77. $target = 35;
  78. }else{
  79. $target = 40;
  80. }
  81. }
  82. if($monotonyPerWeek == ""){
  83. $monotonyPerWeek = 1;
  84. }
  85. if($monotonyPerDay == ""){
  86. $monotonyPerDay = 1;
  87. }
  88. $plan = ($target/$monotonyPerWeek)*$monotonyPerDay;
  89. $recoveryPlan = number_format($plan);
  90. // $recoveryPlan = "";
  91. $ret .="<tr><td>".date("d M Y",strtotime($row->recovery_dttm))."</td><td>$row->recovery_point Pts</td><td>$recoveryPlan Pts</td></tr>";
  92. $total = $total+$row->recovery_point;
  93. }
  94. if($group_cat == "GW400"){
  95. if($monotonyPerWeek <= 3500 OR $monotonyPerWeek < 5000){
  96. $CM = "#388E3C";
  97. $target = 25;
  98. }else if($monotonyPerWeek <= 5000 OR $monotonyPerWeek < 6500){
  99. $CM = "#FF9800";
  100. $target = 35;
  101. }else{
  102. $CM = "#D50000";
  103. $target = 40;
  104. }
  105. }else{
  106. if($monotonyPerWeek <= 2000 OR $monotonyPerWeek < 3500){
  107. $CM = "#388E3C";
  108. $target = 25;
  109. }else if($monotonyPerWeek <= 3500 OR $monotonyPerWeek < 5000){
  110. $CM = "#FF9800";
  111. $target = 35;
  112. }else{
  113. $CM = "#D50000";
  114. $target = 40;
  115. }
  116. }
  117. // $ttlRecover = array_sum($arrRecovery);
  118. $remains = $target - $total;
  119. $ret .= "<tr><td class='cyan'>Total Recovery</td><td class='cyan' colspan='2'>$total Pts</td></tr>";
  120. $ret .= "<tr><td class='cyan'>Target</td><td class='cyan' colspan='2'>$target Pts</td></tr>";
  121. $ret .= "<tr><td class='cyan'>Remains Point</td><td class='cyan' colspan='2'>$remains Pts</td></tr>";
  122. }
  123. $ret .= "</table>";
  124. return $ret;
  125. // return $monotonyPerWeek."-".$target;
  126. }
  127. function saveRecovery($point,$atletID){
  128. $this->db->trans_start();
  129. $this->db->trans_strict(FALSE);
  130. $recovery_dttm = date("Y-m-d");
  131. $sql = "DELETE FROM master_recovery WHERE recovery_dttm = '$recovery_dttm' AND atlet_id = '$atletID'";
  132. $query = $this->db->query($sql);
  133. for($i = 0; $i < count($point); $i++){
  134. $recovery_id = $this->RecoveryID();
  135. $sql = " INSERT INTO master_recovery (recovery_id,point_id,atlet_id,recovery_dttm,created_dttm)"
  136. . " VALUES('$recovery_id','{$point[$i]}','$atletID','$recovery_dttm',now())";
  137. $query = $this->db->query($sql);
  138. }
  139. $this->db->trans_complete(); # Completing transaction
  140. /*Optional*/
  141. if ($this->db->trans_status() === FALSE) {
  142. # Something went wrong.
  143. $this->db->trans_rollback();
  144. return "error";
  145. }
  146. else {
  147. # Everything is Perfect.
  148. # Committing data to the database.
  149. $this->ModelActivityUser->setActivityUser($atletID,3,11);
  150. $this->db->trans_commit();
  151. return "sukses";
  152. }
  153. }
  154. function cekPoint($point_id,$username){
  155. $now = date("Y-m-d");
  156. $sql = " SELECT * FROM master_recovery "
  157. . " WHERE point_id = '$point_id'"
  158. . " AND recovery_dttm = '$now'"
  159. . " AND atlet_id = '$username'";
  160. $query = $this->db->query($sql);
  161. if($query->num_rows()>0){
  162. $row = $query->row();
  163. $point_id = $row->point_id;
  164. }else{
  165. $point_id = "";
  166. }
  167. return $point_id;
  168. }
  169. function getOptRecovery(){
  170. $sql = "SELECT a.*, b.recovery_type, b.recovery_image as image FROM master_recovery_point as a"
  171. . " LEFT JOIN master_recovery_type as b on b.type_id = a.type_id";
  172. $query = $this->db->query($sql);
  173. if($query -> num_rows() > 0){
  174. return $query->result();
  175. }else{
  176. return false;
  177. }
  178. }
  179. function getRecoveryDetail($monotonyID){
  180. $sql = " SELECT a.* FROM `master_monotony_detail` as a "
  181. // . " LEFT JOIN (SELECT * FROM master_pmc WHERE pmcType = 'core' AND pmcCouchType = 'Physic') as b"
  182. // . " on b.monotonyDetailID = a.monotonyDetailID"
  183. . " WHERE a.monotonyID = '$monotonyID'";
  184. $query = $this->db->query($sql);
  185. if($query -> num_rows() > 0){
  186. return $query->result();
  187. }else{
  188. return false;
  189. }
  190. }
  191. function getMonotony($atletID,$month,$year){
  192. $sql = " SELECT a.* FROM d_year_weekly as a WHERE a.`year` = '$year'"
  193. . "AND MONTH(start_date) = '$month'";
  194. $query = $this->db->query($sql);
  195. if($query -> num_rows() > 0){
  196. return $query->result();
  197. }else{
  198. return false;
  199. }
  200. }
  201. function getLoadWeek($weekly,$atletID){
  202. $sql = "SELECT SUM(b.monotonyIntensity*b.monotonyVolume) as monotonyPerWeek FROM `master_monotony` as a
  203. LEFT JOIN master_monotony_detail as b on b.monotonyID = a.monotonyID
  204. WHERE a.monotonyWeek = '$weekly' AND a.monotonyAtletID = '$atletID'
  205. group by b.monotonyID";
  206. $query = $this->db->query($sql);
  207. if($query->num_rows()>0){
  208. $row = $query->row();
  209. $monotonyPerWeek = $row->monotonyPerWeek;
  210. }else{
  211. $monotonyPerWeek = "-";
  212. }
  213. return $monotonyPerWeek;
  214. }
  215. function getRecoveryPoint($atletID,$dttm1){
  216. $sql = " SELECT SUM(b.recovery_point) as recovery_point FROM `master_recovery` as a"
  217. . " LEFT JOIN master_recovery_point as b on b.point_id = a.point_id"
  218. . " WHERE a.atlet_id = '$atletID'"
  219. . " AND a.recovery_dttm = '$dttm1'";
  220. $query = $this->db->query($sql);
  221. if($query->num_rows()>0){
  222. $row = $query->row();
  223. $recovery_point = $row->recovery_point;
  224. if($recovery_point == ""){
  225. $recovery_point = 0;
  226. }
  227. }else{
  228. $recovery_point = 0;
  229. }
  230. return $recovery_point;
  231. }
  232. function RecoveryID(){
  233. $tr = "RECO_";
  234. $year = date("Y");
  235. $month = date("m");
  236. $day = date("d");
  237. $sql = "SELECT left(a.recovery_id,5) as tr, mid(a.recovery_id,6,4) as fyear,"
  238. . " mid(a.recovery_id,10,2) as fmonth, mid(a.recovery_id,12,2) as fday,"
  239. . " right(a.recovery_id,4) as fno FROM master_recovery AS a"
  240. . " where left(a.recovery_id,5) = '$tr' and mid(a.recovery_id,6,4) = '$year'"
  241. . " and mid(a.recovery_id,10,2) = '$month' and mid(a.recovery_id,12,2)= '$day'"
  242. . " order by fyear desc, CAST(fno AS SIGNED) DESC LIMIT 1";
  243. $result = $this->db->query($sql);
  244. if($result->num_rows($result) > 0) {
  245. $row = $result->row();
  246. $tr = $row->tr;
  247. $fyear = $row->fyear;
  248. $fmonth = $row->fmonth;
  249. $fday = $row->fday;
  250. $fno = $row->fno;
  251. $fno++;
  252. } else {
  253. $tr = $tr;
  254. $fyear = $year;
  255. $fmonth = $month;
  256. $fday = $day;
  257. $fno = 0;
  258. $fno++;
  259. }
  260. if (strlen($fno)==1){
  261. $strfno = "000".$fno;
  262. } else if (strlen($fno)==2){
  263. $strfno = "00".$fno;
  264. } else if (strlen($fno)==3){
  265. $strfno = "0".$fno;
  266. } else if (strlen($fno)==4){
  267. $strfno = $fno;
  268. }
  269. $RecoveryID = $tr.$fyear.$fmonth.$fday.$strfno;
  270. return $RecoveryID;
  271. }
  272. }
  273. ?>