Update lua versions
[ryzomcore.git] / web / private_php / ams / autoload / ticket_user.php
bloba52ffaec88c463692de0d5500a2eeccb45c89f95
1 <?php
2 /**
3 * user entry point in the ticket system.
4 * The ticket_user makes a link between the entire ticket system's lib db and the www user, which is stored in another db (this is the external ID).
5 * The externalID could be the ID of a drupal user or wordpress user,.. The ticket_user also stores the permission of that user, this way the permission system
6 * is inside the lib itself and can be used in any www version that you like. permission 1 = user, 2 = mod, 3 = admin.
7 * @author Daan Janssens, mentored by Matthew Lagoe
8 */
9 class Ticket_User{
11 private $tUserId; /**< The id of the user inside the ticket system*/
12 private $permission; /**< The permission of the user */
13 private $externId; /**< The id of the user account in the www (could be drupal,...) that is linked to the ticket_user */
15 ////////////////////////////////////////////Functions////////////////////////////////////////////////////
17 /**
18 * create a new ticket user.
19 * @param $extern_id the id of the user account in the www version (drupal,...)
20 * @param $permission the permission that will be given to the user. 1=user, 2=mod, 3=admin
22 public static function createTicketUser( $extern_id, $permission) {
23 try {
24 //make connection with and put into db
25 $dbl = new DBLayer("lib");
26 $dbl->insert("ticket_user",array('TUserId' => $extern_id, 'Permission' => $permission, 'ExternId' => $extern_id));
28 catch (PDOException $e) {
29 //oh noooz...
30 //error_log(print_r($e, true));
31 //print_r($e);
32 echo "Problem creating user in database!";
38 /**
39 * check if a ticket_user object is a mod or not.
40 * @param $user the ticket_user object itself
41 * @return true or false
43 public static function isMod($user){
44 if(isset($user) && $user->getPermission() > 1){
45 return true;
47 return false;
51 /**
52 * check if a ticket_user object is an admin or not.
53 * @param $user the ticket_user object itself
54 * @return true or false
56 public static function isAdmin($user){
57 if(isset($user) && $user->getPermission() == 3){
58 return true;
60 return false;
64 /**
65 * return constructed ticket_user object based on TUserId.
66 * @param $id the TUserId of the entry.
67 * @return constructed ticket_user object
69 public static function constr_TUserId( $id) {
70 $instance = new self();
71 $instance->setTUserId($id);
72 return $instance;
77 /**
78 * return a list of all mods/admins.
79 * @return an array consisting of ticket_user objects that are mods & admins.
81 public static function getModsAndAdmins() {
82 $dbl = new DBLayer("lib");
83 $statement = $dbl->select("ticket_user", array(null), "`Permission` > 1" );
84 $rows = $statement->fetchAll();
85 $result = Array();
86 foreach($rows as $user){
87 $instanceUser = new self();
88 $instanceUser->set($user);
89 $result[] = $instanceUser;
91 return $result;
95 /**
96 * return constructed ticket_user object based on ExternId.
97 * @param $id the ExternId of the entry.
98 * @return constructed ticket_user object
100 public static function constr_ExternId( $id) {
101 $instance = new self();
102 $dbl = new DBLayer("lib");
103 $statement = $dbl->select("ticket_user" ,array('id'=>$id) ,"ExternId=:id");
104 $row = $statement->fetch();
105 $instance->tUserId = $row['TUserId'];
106 $instance->permission = $row['Permission'];
107 $instance->externId = $row['ExternId'];
108 return $instance;
113 * change the permission of a ticket_user.
114 * @param $user_id the TUserId of the entry.
115 * @param $perm the new permission value.
117 public static function change_permission($user_id, $perm){
118 $user = new Ticket_User();
119 $user->load_With_TUserId($user_id);
120 $user->setPermission($perm);
121 $user->update();
126 * return the email address of a ticket_user.
127 * @param $id the TUserId of the entry.
128 * @return string containing the email address of that user.
130 public static function get_email_by_user_id($id){
131 $user = new Ticket_User();
132 $user->load_With_TUserId($id);
133 $webUser = new WebUsers($user->getExternId());
134 return $webUser->getEmail();
139 * return the username of a ticket_user.
140 * @param $id the TUserId of the entry.
141 * @return string containing username of that user.
143 public static function get_username_from_id($id){
144 $user = new Ticket_User();
145 $user->load_With_TUserId($id);
146 $webUser = new WebUsers($user->getExternId());
147 return $webUser->getUsername();
152 * return the TUserId of a ticket_user by giving a username.
153 * @param $username the username of a user.
154 * @return the TUserId related to that username.
156 public static function get_id_from_username($username){
157 $externId = WebUsers::getId($username);
158 $user = Ticket_User::constr_ExternId($externId);
159 return $user->getTUserId();
163 * return the ticket_user id from an email address.
164 * @param $email the emailaddress of a user.
165 * @return the ticket_user id related to that email address, in case none, return "FALSE".
167 public static function get_id_from_email($email){
168 $webUserId = WebUsers::getIdFromEmail($email);
169 if($webUserId != "FALSE"){
170 $user = Ticket_User::constr_ExternId($webUserId);
171 return $user->getTUserId();
172 }else{
173 return "FALSE";
178 ////////////////////////////////////////////Methods////////////////////////////////////////////////////
181 * A constructor.
182 * Empty constructor
184 public function __construct() {
189 * sets the object's attributes.
190 * @param $values should be an array of the form array('TUserId' => id, 'Permission' => perm, 'ExternId' => ext_id).
192 public function set($values) {
193 $this->setTUserId($values['TUserId']);
194 $this->setPermission($values['Permission']);
195 $this->setExternId($values['ExternId']);
200 * loads the object's attributes.
201 * loads the object's attributes by giving a TUserId.
202 * @param $id the id of the ticket_user that should be loaded
204 public function load_With_TUserId( $id) {
205 $dbl = new DBLayer("lib");
206 $statement = $dbl->select("ticket_user" ,array('id'=>$id), "TUserId=:id" );
207 $row = $statement->fetch();
208 $this->tUserId = $row['TUserId'];
209 $this->permission = $row['Permission'];
210 $this->externId = $row['ExternId'];
215 * update the object's attributes to the db.
217 public function update(){
218 $dbl = new DBLayer("lib");
219 $dbl->update("ticket_user" ,array('Permission' => $this->permission, 'ExternId' => $this->externId) ,"TUserId=$this->tUserId");
222 ////////////////////////////////////////////Getters////////////////////////////////////////////////////
225 * get permission attribute of the object.
227 public function getPermission(){
228 return $this->permission;
232 * get externId attribute of the object.
234 public function getExternId(){
235 return $this->externId;
239 * get tUserId attribute of the object.
241 public function getTUserId(){
242 return $this->tUserId;
246 ////////////////////////////////////////////Setters////////////////////////////////////////////////////
249 * set permission attribute of the object.
250 * @param $perm integer that indicates the permission level. (1= user, 2= mod, 3= admin)
252 public function setPermission($perm){
253 $this->permission = $perm;
258 * set externId attribute of the object.
259 * @param $id the external id.
261 public function setExternId($id){
262 $this->externId = $id;
266 * set tUserId attribute of the object.
267 * @param $id the ticket_user id
269 public function setTUserId($id){
270 $this->tUserId= $id;