db.php 3.08 KB
Newer Older
stc's avatar
stc committed
1
2
3
4
5
6
<?php

class DB {

	private $conn;

Rémy Huet's avatar
Rémy Huet committed
7
	public function __construct () {
stc's avatar
stc committed
8
9
10
11
12
13
14
15
		include 'connexion.php';
		try {
			$this->conn = new PDO('pgsql:host=localhost;port=5432;dbname=apisub', $db_user, $db_pass);
		} catch (PDOException $e) {
			die('Connection failed: ' . $e->getMessage());
		}
	}

16
	public function subList($utclogin) {
17
		$sql = 'SELECT *
Stephane Crozat's avatar
Stephane Crozat committed
18
						FROM vsubscription
19
						WHERE	utclogin=:utclogin';
stc's avatar
stc committed
20
		$st = $this->conn->prepare($sql);
21
22
		$st->bindValue(':utclogin',$utclogin,PDO::PARAM_STR);
		$st->execute();
stc's avatar
stc committed
23
		$res = $st->fetchAll(PDO::FETCH_ASSOC);
24
		return $res;
stc's avatar
stc committed
25
26
	}

Stephane Crozat's avatar
Fix #9  
Stephane Crozat committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
	public function getExplanation($utclogin) {
		$sql = 'SELECT explanation
						FROM localuser
						WHERE	utclogin=:utclogin';
		$st = $this->conn->prepare($sql);
		$st->bindValue(':utclogin',$utclogin,PDO::PARAM_STR);
		$st->execute();
		$res = $st->fetch(PDO::FETCH_ASSOC);
		return $res['explanation'];
	}

	public function setExplanation($utclogin,$explanation) {
		$sql = 'UPDATE localuser
						SET explanation=:explanation
						WHERE	utclogin=:utclogin';
		$st = $this->conn->prepare($sql);
		$st->bindValue(':explanation',$explanation,PDO::PARAM_STR);
		$st->bindValue(':utclogin',$utclogin,PDO::PARAM_STR);
		$res = $st->execute();
		return $res;
	}



51
	public function apiList($semester, $year) {
52
		$sql = 'SELECT *
53
						FROM vapi
54
						WHERE semester=:semester AND year=:year';
stc's avatar
stc committed
55
		$st = $this->conn->prepare($sql);
56
57
58
		$st->bindValue(':semester',$semester,PDO::PARAM_STR);
		$st->bindValue(':year',$year,PDO::PARAM_INT);
		$st->execute();
stc's avatar
stc committed
59
		$res = $st->fetchAll(PDO::FETCH_ASSOC);
60
		return $res;
stc's avatar
stc committed
61
62
	}

63
	public function subToApi($utclogin, $api) {
64
65
66
		$today = date('Ymd');
		$sql = 'INSERT INTO subscribe(utclogin, api, subdate) VALUES (:utclogin, :api, :today)';
		$st = $this->conn->prepare($sql);
67
68
69
70
		$st->bindValue(':utclogin',$utclogin,PDO::PARAM_STR);
		$st->bindValue(':api',$api,PDO::PARAM_INT);
		$st->bindValue(':today',$today,PDO::PARAM_STR);
		$res = $st->execute();
71
		return $res;
stc's avatar
stc committed
72
73
	}

74
	public function unsubToApi($utclogin, $api) {
75
76
		$sql = 'DELETE FROM subscribe WHERE utclogin=:utclogin AND api=:api';
		$st = $this->conn->prepare($sql);
77
78
79
		$st->bindValue(':utclogin',$utclogin,PDO::PARAM_STR);
		$st->bindValue(':api',$api,PDO::PARAM_INT);
		$res = $st->execute();
80
81
		return $res;
	}
82
83
84
85
86
87
88

	public function copyUser($utclogin, $surname, $firstname) {
		// Function used to create a local copy of surname and firstname of each user, in order to link to DFP files without utclogin
		$sql = 'SELECT utclogin FROM localuser WHERE utclogin=:utclogin';
		$st1 = $this->conn->prepare($sql);
		$st1->bindValue(':utclogin',$utclogin,PDO::PARAM_STR);
		$st1->execute();
Stephane Crozat's avatar
Fix #9  
Stephane Crozat committed
89
		if (!$st1->fetch(PDO::FETCH_ASSOC)) {
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
			// If user has never logged in yet, he is added to local copy
			$sql = 'INSERT INTO localuser(utclogin, firstname, surname) VALUES (:utclogin, :firstname, :surname)';
			$st2 = $this->conn->prepare($sql);
			$st2->bindValue(':utclogin',$utclogin,PDO::PARAM_STR);
			$st2->bindValue(':firstname',$firstname,PDO::PARAM_STR);
			$st2->bindValue(':surname',$surname,PDO::PARAM_STR);
			$res = $st2->execute();
			return $res;
		}
		else {
			return 0;
		};

	}

stc's avatar
stc committed
105
}