Center::updateCenter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
nc 1
nop 2
dl 0
loc 9
c 0
b 0
f 0
cc 1
rs 10
1
<?php
2
/**
3
 Copyright (C) 2018-2020 KANOUN Salim
4
 This program is free software; you can redistribute it and/or modify
5
 it under the terms of the Affero GNU General Public v.3 License as published by
6
 the Free Software Foundation;
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
 Affero GNU General Public Public for more details.
11
 You should have received a copy of the Affero GNU General Public Public along
12
 with this program; if not, write to the Free Software Foundation, Inc.,
13
 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
14
 */
15
16
/**
17
 * Center management in the plateform
18
 */
19
20
Class Center {
21
	public $name;
22
	public $code;
23
	public $countryCode;
24
	public $countryName;
25
	private $linkpdo;
26
27
	/**
28
	 * Construct the center from center code
29
	 * @param PDO $linkpdo
30
	 * @param $code
31
	 * @throws Exception
32
	 */
33
	public function __construct(PDO $linkpdo, $code) {
34
	    
35
		$centerQuery=$linkpdo->prepare('SELECT * FROM centers WHERE code=:code');
36
		$centerQuery->execute(array('code'=>$code));
37
		$center=$centerQuery->fetch(PDO::FETCH_ASSOC);
38
	    
39
		if (empty($center)) {
40
			throw new Exception("Non Existing Center");
41
		}
42
		$this->linkpdo=$linkpdo;
43
		$this->name=$center['name'];
44
		$this->code=$center['code'];
45
		$this->countryCode=$center['country_code'];
46
		$this->countryName=Global_Data::getCountryName($linkpdo, $this->countryCode);
47
	}
48
49
	/**
50
	 * Update Center with new name and new country code
51
	 * @param string $name
52
	 * @param string $countryCode
53
	 */
54
	public function updateCenter(string $name, string $countryCode) {
55
		$updatePatient=$this->linkpdo->prepare("UPDATE centers
56
                                            SET name = :centerName,
57
                                            country_code = :countryCode
58
                                            WHERE code = :code");
59
	    
60
		$updatePatient->execute(array('centerName'=>$name,
61
			'countryCode'=>$countryCode,
62
			'code'=>$this->code));
63
	    
64
	}
65
66
	/**
67
	 * Return all users having a specific center in main of affiliated centers
68
	 * @param PDO $linkpdo
69
	 * @param $center
70
	 * @return User[]
71
	 */
72
	public static function getUsersAffiliatedToCenter(PDO $linkpdo, $center) {
73
		
74
		//Select All users that has a matching center
75
		$queryUsersEmail=$linkpdo->prepare('
76
								    SELECT users.username
77
									FROM users
78
									WHERE (center=:center)
79
									UNION
80
									SELECT affiliated_centers.username
81
									FROM affiliated_centers,
82
									     users
83
									WHERE (affiliated_centers.center=:center
84
									       AND affiliated_centers.username=users.username)');
85
		
86
		$queryUsersEmail->execute(array('center'=>$center));
87
		$users=$queryUsersEmail->fetchAll(PDO::FETCH_COLUMN);
88
		
89
		$usersObjects=[];
90
		foreach ($users as $user) {
91
			$usersObjects[]=new User($user, $linkpdo);
92
			
93
		}
94
		
95
		return $usersObjects;
96
	}
97
98
	/**
99
	 * Create a new center in the database
100
	 * @param PDO $linkpdo
101
	 * @param int $code
102
	 * @param $name
103
	 * @param string $countryCode
104
	 */
105
	public static function addCenter(PDO $linkpdo, $code, String $name, String $countryCode) {
106
		$insertion=$linkpdo->prepare('INSERT INTO centers (code, name, country_code) VALUES (:code, :name, :countryCode)');
107
		$insertion->execute(array(
108
				'name' => $name,
109
				'code' => $code,
110
				'countryCode' =>$countryCode
111
		));
112
	}
113
}