-
Notifications
You must be signed in to change notification settings - Fork 0
/
Habitos.php
131 lines (125 loc) · 3.04 KB
/
Habitos.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
class Habitos
{
private $_username;
private $_mysqli;
function __construct($username)
{
global $usuario_db,$passwd_db,$servidor_db,$nombre_db;
$this->_username=$username;
$this->_mysqli=new mysqli($servido_db,$usuario_db,$passwd_db,$nombre_db);
}
public function estadisticas()
{
$username=$this->_username;
$consulta=<<<sql
SELECT dia,AVG(cumplido) AS promedio,COUNT(*) AS total
FROM cumplimientos
INNER JOIN habitos USING(id_habito)
WHERE dia<=NOW()
AND visible
AND username='$username'
AND cumplido IS NOT NULL
GROUP BY dia
ORDER BY dia desc
sql;
if($res=$this->_mysqli->query($consulta))
{
while($esta_fila=$res->fetch_assoc())
{
$arr_estadisticas[]=$esta_fila;
}
if(count($arr_estadisticas))
{
return $arr_estadisticas;
}
}
return false;
}
public function cumple($id_cumplimiento,$cumplido=1)
{
$username=$this->_username;
$consulta=<<<sql
UPDATE cumplimientos
SET cumplido=$cumplido
WHERE id_cumplimiento=$id_cumplimiento
AND cumplido IS NULL
AND id_habito IN
(SELECT id_habito
FROM habitos
WHERE username='$username')
sql;
$this->_mysqli->query($consulta);
}
public function obtieneVencidos()
{
$username=$this->_username;
$consulta=<<<sql
SELECT id_cumplimiento,id_habito,dia,habito,descripcion
FROM cumplimientos
INNER JOIN habitos USING(id_habito)
WHERE dia<=NOW()
AND visible
AND username='$username'
AND cumplido IS NULL
sql;
if($res=$this->_mysqli->query($consulta))
{
while($esta_fila=$res->fetch_assoc())
{
$arr_vencidos[]=$esta_fila;
}
if(count($arr_vencidos))
{
return $arr_vencidos;
}
}
return false;
}
public function creaCumplimientos()
{
global $dias_cache;
if($arr_habitos=$this->obtieneHabitos())
{
foreach($arr_habitos as $este_habito)
{
$epoch=time();
$id_habito=$este_habito["id_habito"];
for($i=0;$i<$dias_cache;$i++)
{
$consulta=<<<sql
INSERT IGNORE INTO cumplimientos
(ctime,dia,id_habito)
VALUES
(NOW(),FROM_UNIXTIME($epoch,'%Y-%m-%d'),$id_habito)
sql;
$this->_mysqli->query($consulta);
$epoch+=24*60*60;
}
}
}
}
public function obtieneHabitos()
{
$username=$this->_username;
$consulta=<<<sql
SELECT id_habito,ctime,habito,descripcion
FROM habitos
WHERE username='$username'
AND visible
sql;
if($res=$this->_mysqli->query($consulta))
{
while($esta_fila=$res->fetch_assoc())
{
$arr_habitos[]=$esta_fila;
}
if(count($arr_habitos))
{
return $arr_habitos;
}
}
return false;
}
}
?>