planning2.inc.php 16.8 KB
Newer Older
Bate's avatar
Bate committed
1 2 3 4 5 6 7 8 9 10
<?php

class planning2 extends stdentity
{
	var $name;
	var $weekly;
	var $group;
	var $admin_group;
	var $start;
	var $end;
Bate's avatar
Bate committed
11
	var $is_public;
Bate's avatar
Bate committed
12 13 14

	function load_by_id( $id )
	{
15 16
		if(is_null($id) || empty($id))
			return false;
Bate's avatar
Bate committed
17
		$req = new requete( $this->db, "SELECT * from `pl2_planning`
Bate's avatar
Bate committed
18
						WHERE `id_planning` = '".
19
						intval($id).
Bate's avatar
Bate committed
20 21 22 23 24 25 26 27 28 29 30 31
						"' LIMIT 1");
		if( $req->lines != 1 )
		{
			$this->id = null;
			return false;
		}
		$this->_load($req->get_row());
		return true;
	}

	function _load( $row )
	{
Bate's avatar
Bate committed
32
		$this->id		= $row['id_planning'];
Bate's avatar
Bate committed
33 34 35 36
		$this->group		= $row['id_group'];
		$this->admin_group	= $row['id_admin_group'];
		$this->name		= $row['name_planning'];
		$this->weekly		= $row['weekly_planning'];
Bate's avatar
Bate committed
37 38
		$this->start		= strtotime($row['start']." UTC");
		$this->end		= strtotime($row['end']." UTC");
Bate's avatar
Bate committed
39
		$this->is_public	= $row['is_public'];
Bate's avatar
Bate committed
40 41
	}

Bate's avatar
Bate committed
42
	function add ( $name, $group, $admin_group, $weekly, $start, $end, $is_public = true )
Bate's avatar
Bate committed
43
	{
44 45 46 47 48 49 50
		$this->name = mysql_real_escape_string($name);
		$this->group = intval($group);
		$this->admin_group = intval($admin_group);
		$this->weekly = intval($weekly);
		$this->start = mysql_real_escape_string($start);
		$this->end = mysql_real_escape_string($end);
		$this->is_public= ((bool)$is_public);
Bate's avatar
Bate committed
51 52 53 54 55 56 57

		$sql = new insert ($this->dbrw,
                       "pl2_planning",
                       array(
                             "name_planning" => $this->name,
			     "id_group" => $this->group,
			     "id_admin_group" => $this->admin_group,
Bate's avatar
Bate committed
58 59
			     "start" => gmdate("Y-m-d H:i:s",$this->start),
			     "end" => gmdate("Y-m-d H:i:s",$this->end),
Bate's avatar
Bate committed
60 61
                             "weekly_planning" => $this->weekly,
                             "is_public" => $this->is_public
Bate's avatar
Bate committed
62 63 64 65 66 67 68 69 70 71 72 73 74
                            )
                      );
		if ( !$sql->is_success() )
		{
			$this->id = null;
			return false;
		}

		$this->id = $sql->get_id();

		return true;
	}

Bate's avatar
Bate committed
75
	function update ( $name, $group, $admin_group, $start, $end , $is_public)
Bate's avatar
Bate committed
76
	{
77 78 79 80 81 82
		$this->name = mysql_real_escape_string($name);
		$this->group = intval($group);
		$this->admin_group = intval($admin_group);
		$this->start = mysql_real_escape_string($start);
		$this->end = mysql_real_escape_string($end);
		$this->is_public = ((bool)$is_public);
Bate's avatar
Bate committed
83 84 85 86 87 88 89

		$sql = new update ($this->dbrw,
                       "pl2_planning",
                       array(
                             "name_planning" => $this->name,
			     "id_group" => $this->group,
			     "id_admin_group" => $this->admin_group,
Bate's avatar
Bate committed
90 91
			     "start" => gmdate("Y-m-d H:i:s",$this->start),
			     "end" => gmdate("Y-m-d H:i:s",$this->end),
Bate's avatar
Bate committed
92 93
                             "weekly_planning" => $this->weekly,
                             "is_public" => $this->is_public
Bate's avatar
Bate committed
94 95 96
                            ),
			array("id_planning" => $this->id)
                      );
Bate's avatar
Bate committed
97
		return $sql->is_success();
Bate's avatar
Bate committed
98 99 100 101 102 103 104 105
	}

	function remove()
	{
		$sql = new requete($this->db, "SELECT `id_gap` FROM `pl2_gap`
						WHERE id_planning = ".$this->id);
		while(list($gap_id) = $sql->get_row())
		{
Bate's avatar
Bate committed
106
			$this->delete_gap( $gap_id );
Bate's avatar
Bate committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
		}
		$sql = new delete($this->dbrw, "pl2_absence",
			array(
                             "id_planning" => $this->id
                            )
                       );
		$sql = new delete($this->dbrw, "pl2_planning",
			array(
                             "id_planning" => $this->id
                            )
                       );
	}

	function add_gap( $start, $end, $gap_name, $max_users )
	{
122 123 124 125 126
		$start = mysql_real_escape_string($start);
		$end = mysql_real_escape_string($end);
		$gap_name = mysql_real_escape_string($gap_name);
		$max_users = intval($max_users);

Bate's avatar
Bate committed
127 128 129 130 131 132 133
		$gap_name = trim($gap_name);
		if( $max_users <= 0 )
			return -1;
		if(empty($gap_name))
			return -1;
		if($start >= $end)
			return -1;
Bate's avatar
Bate committed
134
		if(!$this->weekly && $start < $this->start)
Bate's avatar
Bate committed
135
			return -1;
Bate's avatar
Bate committed
136
		if(!$this->weekly && $end > $this->end)
Bate's avatar
Bate committed
137 138 139
			return -1;
		if($this->weekly)
		{
Bate's avatar
Bate committed
140
			if($end >= $this->weekly*24*3600)
Bate's avatar
Bate committed
141 142 143 144 145 146 147
				return -1;
		}
		$sql = new insert ($this->dbrw,
                       "pl2_gap",
                       array(
			     "id_planning" => $this->id,
                             "name_gap" => $gap_name,
Bate's avatar
Bate committed
148 149
			     "start" => gmdate("Y-m-d H:i:s",$start),
                             "end" => gmdate("Y-m-d H:i:s",$end),
Bate's avatar
Bate committed
150 151 152 153 154 155 156 157 158 159 160 161 162
			     "max_users" => $max_users
                            )
                      );
		if ( !$sql->is_success() )
		{
			return -1;
		}

		return $sql->get_id();
	}

	function update_gap( $gap_id, $start, $end, $gap_name, $max_users )
	{
163 164 165 166 167
		$gap_id = intval($gap_id);
		$start = mysql_real_escape_string($start);
		$end = mysql_real_escape_string($end);
		$gap_name = mysql_real_escape_string($gap_name);
		$max_users = intval($max_users);
Bate's avatar
Bate committed
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
		if($gap_id <= 0 )
			return -1;
		if( $max_users <= 0 )
			return -1;
		$gap_name = trim($gap_name);
		if(empty($gap_name))
			return -1;
		if($start >= $end)
			return -1;
		if($start < $this->start)
			return -1;
		if($end > $this->end)
			return -1;
		if($this->weekly)
		{
Bate's avatar
Bate committed
183
			if($end >= $this->weekly*24*3600)
Bate's avatar
Bate committed
184 185 186 187 188 189 190 191
				return -1;
		}
		$sql = new update ($this->dbrw,
                       "pl2_gap",
                       array(
			     "id_planning" => $this->id,
                             "name_gap" => $gap_name,
			     "max_users" => $max_users,
Bate's avatar
Bate committed
192 193
			     "start" => gmdate("Y-m-d H:i:s",$start),
                             "end" => gmdate("Y-m-d H:i:s",$end)
Bate's avatar
Bate committed
194 195 196 197 198 199 200 201 202
                            ),
		       array(
			     "id_gap" => $gap_id
		       )
                      );
	}

	function delete_gap( $gap_id )
	{
203
		$gap_id = intval($gap_id);
Bate's avatar
Bate committed
204 205 206 207 208
		$sql = new delete($this->dbrw, "pl2_user_gap",
			array(
                       		"id_gap" => $gap_id
			)
                );
Bate's avatar
Bate committed
209 210
		if(!$sql->is_success())
			return false;
Bate's avatar
Bate committed
211 212 213 214 215
		$sql = new delete($this->dbrw, "pl2_gap",
			array(
                             "id_gap" => $gap_id
                            )
                       );
Bate's avatar
Bate committed
216
		return $sql->is_success();
Bate's avatar
Bate committed
217 218 219 220
	}

	function get_max_users_for( $gap_id, $start, $end )
	{
221
		$gap_id = intval($gap_id);
Bate's avatar
Bate committed
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
		$start = mysql_escape_string($start);
		$end = mysql_escape_string($end);
		if(!$this->weekly)
		{
			$sql = new requete($this->db,
				"SELECT count(*) FROM pl2_user_gap
        	                 JOIN pl2_gap ON pl2_gap.id_gap = pl2_user_gap.id_gap
                	         WHERE pl2_gap.id_gap = '$gap_id'");
			if( list($total) = $sql->get_row())
				return $total;
			else
				return -1;
		}
		$max_users = 0;
		$new_start = $start;
		$to_break = false;
Bate's avatar
Bate committed
238 239
		$date_absence = null;
		$date_min = null;
Bate's avatar
Bate committed
240 241 242 243
		while(true)
		{
			$to_break = false;
			$sql = new requete($this->db,
Bate's avatar
Bate committed
244
				"SELECT min(pl2_absence.start) FROM pl2_absence
Bate's avatar
Bate committed
245 246 247 248 249 250
				 JOIN pl2_gap 
				 ON pl2_gap.id_planning = pl2_absence.id_planning
				 JOIN pl2_user_gap
				 ON pl2_gap.id_gap = pl2_user_gap.id_gap
				 AND pl2_user_gap.id_utilisateur = pl2_absence.id_utilisateur
				 WHERE pl2_gap.id_gap = '$gap_id'
Bate's avatar
Bate committed
251 252
				 AND pl2_absence.start < '".gmdate("Y-m-d H:i:s",$end)."'
				 AND pl2_absence.end > '".gmdate("Y-m-d H:i:s",$new_start)."'");
Bate's avatar
Bate committed
253 254 255 256
			if($sql->lines <= 0)
				$to_break = true;
			else
			{
Bate's avatar
Bate committed
257
				$date_absence = null;
Bate's avatar
Bate committed
258 259 260 261 262 263
				list( $date_absence ) = $sql->get_row();
				if(is_null($date_absence))
					$to_break = true;
			}

			$sql = new requete($this->db,
Bate's avatar
Bate committed
264
				"SELECT min(pl2_user_gap.end) FROM pl2_user_gap
Bate's avatar
Bate committed
265 266
				 JOIN pl2_gap
				 ON pl2_gap.id_gap = pl2_user_gap.id_gap
Bate's avatar
Bate committed
267 268
				 WHERE pl2_user_gap.start <= '".gmdate("Y-m-d H:i:s",$new_start)."' 
				 AND pl2_user_gap.end > '".gmdate("Y-m-d H:i:s",$new_start)."'");
Bate's avatar
Bate committed
269 270

			if($sql->lines <= 0)
Bate's avatar
Bate committed
271
			{
Bate's avatar
Bate committed
272 273
				if($to_break)
					break;
Bate's avatar
Bate committed
274
			}
Bate's avatar
Bate committed
275 276
			else
			{
Bate's avatar
Bate committed
277
				$date_min = null;
Bate's avatar
Bate committed
278 279 280 281
				list( $date_min ) = $sql->get_row();
				if(is_null($date_min) && $to_break)
					break;
			}
Bate's avatar
Bate committed
282 283 284 285
			if(is_null($date_min))
			{
				$date_min = $date_absence;
			}
Bate's avatar
Bate committed
286 287 288 289 290
			elseif(is_null($date_absence))
			{
			}
			else
				$date_min = ($date_min<$date_absence)?$date_min:$date_absence;
Bate's avatar
Bate committed
291 292 293 294 295
			
			$sql = new requete($this->db,
				"SELECT count(*) FROM pl2_user_gap
        	                 JOIN pl2_gap ON pl2_gap.id_gap = pl2_user_gap.id_gap
                	         WHERE pl2_gap.id_gap = '$gap_id'
Bate's avatar
Bate committed
296 297
				 AND pl2_user_gap.start <= '".gmdate("Y-m-d H:i:s",$new_start)."'
				 AND pl2_user_gap.end >= '".gmdate("Y-m-d H:i:s",$date_min)."'");
Bate's avatar
Bate committed
298 299 300 301 302 303 304 305 306 307 308 309 310 311
			if(!$sql->is_success())
			{
				exit();
			}
			list( $my_max ) = $sql->get_row();
			$max_users = max($my_max,$max_users);
			$new_start = $date_min;
		}
		return $max_users;
		
	}
	
	function is_user_addable( $gap_id, $user_id, $start, $end )
	{
312
		$gap_id = intval($gap_id);
Bate's avatar
Typo  
Bate committed
313
		$user_id = intval($user_id);
314 315 316
		$start = mysql_real_escape_string($start);
		$end = mysql_real_escape_string($end);

Bate's avatar
Bate committed
317 318 319
		$sql = new requete($this->db, 
			"SELECT * from pl2_user_gap
			 WHERE id_gap = '$gap_id'
Bate's avatar
Bate committed
320 321 322
			 AND id_utilisateur = '$user_id'
			 AND 
			 (
Bate's avatar
Bate committed
323 324
			 	(	 start <= '".gmdate("Y-m-d H:i:s",$end)."'
					 AND start >= '".gmdate("Y-m-d H:i:s",$start)."'
Bate's avatar
Bate committed
325 326
				)
			  	OR
Bate's avatar
Bate committed
327 328
				(        end >= '".gmdate("Y-m-d H:i:s",$start)."'
					 AND end <= '".gmdate("Y-m-d H:i:s",$end)."'
Bate's avatar
Bate committed
329 330 331 332 333 334 335 336 337
				)
			 )");
		if($sql->lines > 0)
			return false;
		$sql = new requete($this->db, 
			"SELECT * from pl2_user_gap
			 JOIN pl2_gap ON pl2_gap.id_gap = pl2_user_gap.id_gap
			 WHERE id_utilisateur = '$user_id'
			 AND id_planning IN (SELECT id_planning FROM pl2_gap WHERE id_gap = '$gap_id')
Bate's avatar
Bate committed
338 339
			 AND 
			 (
Bate's avatar
Bate committed
340 341
			 	(	 pl2_user_gap.start <= '".gmdate("Y-m-d H:i:s",$end)."'
					 AND pl2_user_gap.start >= '".gmdate("Y-m-d H:i:s",$start)."'
Bate's avatar
Bate committed
342 343
				)
			  	OR
Bate's avatar
Bate committed
344 345
				(        pl2_user_gap.end >= '".gmdate("Y-m-d H:i:s",$start)."'
					 AND pl2_user_gap.end <= '".gmdate("Y-m-d H:i:s",$end)."'
Bate's avatar
Bate committed
346
				)
Bate's avatar
Bate committed
347 348 349 350 351 352 353 354
			  	OR
				(        pl2_user_gap.start <= '".gmdate("Y-m-d H:i:s",$start)."'
					 AND pl2_user_gap.end >= '".gmdate("Y-m-d H:i:s",$end)."'
				)
			  	OR
				(        pl2_user_gap.start >= '".gmdate("Y-m-d H:i:s",$start)."'
					 AND pl2_user_gap.end <= '".gmdate("Y-m-d H:i:s",$end)."'
				)
Bate's avatar
Bate committed
355 356 357 358
			 )
			 AND
			 (
				(
Bate's avatar
Bate committed
359 360
					pl2_gap.start < (SELECT tmp.end FROM pl2_gap AS tmp WHERE tmp.id_gap = '$gap_id')
					AND pl2_gap.start > (SELECT tmp.start FROM pl2_gap AS tmp WHERE tmp.id_gap = '$gap_id')
Bate's avatar
Bate committed
361 362 363
				)
				OR
				(
Bate's avatar
Bate committed
364 365
					pl2_gap.end > (SELECT tmp.start FROM pl2_gap AS tmp WHERE tmp.id_gap = '$gap_id')
                                        AND pl2_gap.end < (SELECT tmp.end FROM pl2_gap AS tmp WHERE tmp.id_gap = '$gap_id')
Bate's avatar
Bate committed
366
				)
Bate's avatar
Bate committed
367 368
				OR
				(
Bate's avatar
Bate committed
369 370
					pl2_gap.start <= (SELECT tmp.start FROM pl2_gap AS tmp WHERE tmp.id_gap = '$gap_id')
                                        AND pl2_gap.end >= (SELECT tmp.end FROM pl2_gap AS tmp WHERE tmp.id_gap = '$gap_id')
Bate's avatar
Bate committed
371 372 373
				)
				OR
				(
Bate's avatar
Bate committed
374 375
					pl2_gap.start >= (SELECT tmp.start FROM pl2_gap AS tmp WHERE tmp.id_gap = '$gap_id')
                                        AND pl2_gap.end <= (SELECT tmp.end FROM pl2_gap AS tmp WHERE tmp.id_gap = '$gap_id')
Bate's avatar
Bate committed
376
				)
Bate's avatar
Bate committed
377 378 379
			 )");
		if($sql->lines > 0)
			return false;
Bate's avatar
Bate committed
380
		$users = $this->get_max_users_for($gap_id,$start,$end);
Bate's avatar
Bate committed
381 382 383 384 385 386 387 388 389 390 391
		$sql = new requete($this->db,
			"SELECT max_users FROM pl2_gap
			 WHERE id_gap = $gap_id");
		if($sql->lines != 1)
			return false;
		list( $max_users ) = $sql->get_row();
		return ($users < $max_users);
	}

	function add_user_to_gap( $gap_id, $user_id, $start, $end)
	{
392
		$gap_id = intval($gap_id);
Bate's avatar
Typo  
Bate committed
393
		$user_id = intval($user_id);
394 395
		$start = mysql_real_escape_string($start);
		$end = mysql_real_escape_string($end);
Bate's avatar
Bate committed
396
		if(!$this->is_user_addable($gap_id,$user_id,$start,$end))
Bate's avatar
Bate committed
397 398 399 400 401 402
			return -1;
		$sql = new insert ($this->dbrw,
                       "pl2_user_gap",
                       array(
			     "id_gap" => $gap_id,
                             "id_utilisateur" => $user_id,
Bate's avatar
Bate committed
403 404
			     "start" => gmdate("Y-m-d H:i:s",$start),
                             "end" => gmdate("Y-m-d H:i:s",$end)
Bate's avatar
Bate committed
405 406 407 408 409 410 411 412 413 414 415 416
                            )
                      );
		if ( !$sql->is_success() )
		{
			return -1;
		}

		return $sql->get_id();
	}

	function remove_user_from_gap( $user_gap_id )
	{
417
		$user_gap_id = intval($user_gap_id);
Bate's avatar
Bate committed
418 419 420 421 422 423 424 425 426
		$sql = new delete($this->dbrw, "pl2_user_gap",
			array(
                             "id_user_gap" => $user_gap_id
                            )
                       );
	}

	function get_gaps_for_user( $user_id)
	{
427
		$user_id = intval($user_id);
Bate's avatar
Bate committed
428 429 430 431 432
		return new requete($this->db,
			"SELECT id_gap FROM pl2_user_gap
			 WHERE id_utilisateur = $user_id");
	}

Bate's avatar
Bate committed
433
	function get_gaps( $start, $end )
Bate's avatar
Bate committed
434
	{
435 436
		$start = mysql_real_escape_string($start);
		$end = mysql_real_escape_string($end);
Bate's avatar
Bate committed
437
		if($this->weekly)
Bate's avatar
Bate committed
438
			return new requete($this->db,
Bate's avatar
Bate committed
439
				"SELECT id_gap, start, end, name_gap, max_users FROM pl2_gap 
Bate's avatar
Bate committed
440 441 442 443
				 WHERE id_planning = $this->id 
				 ORDER BY start ASC");
		else
			return new requete($this->db,
Bate's avatar
Bate committed
444
                                "SELECT id_gap, start, end, name_gap, max_users FROM pl2_gap 
Bate's avatar
Bate committed
445
                                 WHERE id_planning = $this->id ".
446 447
				 ((empty($start)||is_null($start))?"":("AND end > '".gmdate("Y-m-d H:i:s",$start)."' ")).
				 ((empty($end)||is_null($end))?"":("AND start < '".gmdate("Y-m-d H:i:s",$end)."' ")).
Bate's avatar
Bate committed
448
                                 "ORDER BY start ASC");
Bate's avatar
Bate committed
449 450 451 452
	}

	function get_gaps_time( $start, $end )
	{
453 454
		$start = mysql_real_escape_string($start);
		$end = mysql_real_escape_string($end);
Bate's avatar
Bate committed
455
		if($this->weekly)
Bate's avatar
Bate committed
456 457 458 459 460 461 462 463 464 465
			return new requete($this->db,
				"SELECT start as date FROM pl2_gap 
				 WHERE id_planning = $this->id 
				 UNION DISTINCT SELECT end as date 
				 FROM pl2_gap 
				 WHERE id_planning = $this->id 
				 ORDER BY date ASC");
		else
			return new requete($this->db,
                                "SELECT start as date FROM pl2_gap 
Bate's avatar
Bate committed
466 467 468 469
                                 WHERE id_planning = $this->id ".
				 (is_null($start)?"":("AND end > '".gmdate("Y-m-d H:i:s",$start)."' ")).
				 (is_null($end)?"":("AND start < '".gmdate("Y-m-d H:i:s",$end)."' ")).
                                 "UNION DISTINCT
Bate's avatar
Bate committed
470
				 SELECT end as date FROM pl2_gap 
Bate's avatar
Bate committed
471 472 473 474
                                 WHERE id_planning = $this->id ".
				 (is_null($start)?"":("AND end > '".gmdate("Y-m-d H:i:s",$start)."' ")).
				 (is_null($end)?"":("AND start < '".gmdate("Y-m-d H:i:s",$end)."' ")).
                                 "ORDER BY date ASC");
Bate's avatar
Bate committed
475 476
	}

Bate's avatar
Bate committed
477 478 479
	function get_gaps_names()
	{
		return new requete($this->db,
Bate's avatar
Bate committed
480
			"SELECT DISTINCT name_gap FROM pl2_gap WHERE id_planning = $this->id ORDER BY name_gap");
Bate's avatar
Bate committed
481 482
	}

Bate's avatar
Bate committed
483 484
	function get_gap_info( $gap_id )
	{
485
		$gap_id = intval($gap_id);
Bate's avatar
Bate committed
486 487
		return new requete($this->db,
                        "SELECT id_gap, name_gap, start, end FROM pl2_gap 
Bate's avatar
Bate committed
488
			 WHERE id_gap = $gap_id AND id_planning = $this->id");
Bate's avatar
Bate committed
489 490
	}

Bate's avatar
Bate committed
491 492
	function get_user_gap_info( $user_gap_id )
	{
493
		$user_gap_id = intval($user_gap_id);
Bate's avatar
Bate committed
494 495 496 497 498
		return new requete($this->db,
                        "SELECT id_gap, id_utilisateur, start, end FROM pl2_user_gap
			 WHERE id_user_gap = $user_gap_id");
	}

Bate's avatar
Bate committed
499 500
	function get_gaps_from_names( $name )
	{
501
		$name = mysql_real_escape_string($name);
Bate's avatar
Bate committed
502
		return new requete($this->db,
Bate's avatar
Bate committed
503 504 505 506 507
			"SELECT id_gap FROM pl2_gap WHERE id_planning = $this->id AND gap_name = '$name' ORDER BY start");
	}

	function get_week_start( $date )
	{
508
		$date = mysql_real_escape_string($date);
Bate's avatar
Bate committed
509 510 511
		if($this->weekly)
		{
			$diff = $date - $this->start;
Bate's avatar
Bate committed
512
                        $date = $date - ($diff % ($this->weekly*3600*24));
Bate's avatar
Bate committed
513 514 515 516 517 518
		}
		else
		{
			$req = new requete($this->db,
				"SELECT start FROM pl2_gap 
				 WHERE id_planning = $this->id
Bate's avatar
Bate committed
519
				 AND start > '".gmdate("Y-m-d H:i:s",$date)."'
Bate's avatar
Bate committed
520 521 522 523
				 ORDER BY start ASC LIMIT 1");
			if($req->lines == 1)
			{
				list( $tmp ) = $req->get_row();
Bate's avatar
Bate committed
524
				$date = strtotime( $tmp." UTC" );
Bate's avatar
Bate committed
525 526
			}
		}
Bate's avatar
Bate committed
527
		return strtotime(gmdate("Y-m-d 00:00:00",$date)." UTC");
Bate's avatar
Bate committed
528 529
	}

Bate's avatar
Bate committed
530 531
	function get_users_for_gap( $gap_id, $date )
	{
532 533
		$gap_id = intval($gap_id);
		$date = mysql_real_escape_string($date);
Bate's avatar
Bate committed
534 535 536 537 538 539 540 541 542
		$sql = new requete($this->db,
			"SELECT start,end FROM pl2_gap
			 WHERE id_gap = $gap_id");
		if(!$sql->is_success())
			exit();
		list( $start, $end ) = $sql->get_row();

		if($this->weekly)
		{
Bate's avatar
Bate committed
543 544 545
			/*$date = strtotime(date('o-\\WW',$date));*/
			$diff = $date - $this->start;
			$date = $date - ($diff % ($this->weekly*3600*24));
Bate's avatar
Bate committed
546 547
			$start = strtotime($start." UTC")+$date;
			$end = strtotime($end." UTC")+$date;
Bate's avatar
Bate committed
548 549
			$start =gmdate("Y-m-d H:i:s",$start);
			$end =gmdate("Y-m-d H:i:s",$end);	
Bate's avatar
Bate committed
550 551 552 553 554 555
		}
		return new requete($this->db,
			"SELECT utilisateurs.id_utilisateur as id_utilisateur, 
				IF(utl_etu_utbm.surnom_utbm!='' AND utl_etu_utbm.surnom_utbm IS NOT NULL,
					utl_etu_utbm.surnom_utbm, 
					CONCAT(`utilisateurs`.`prenom_utl`,' ',`utilisateurs`.`nom_utl`)) 
Bate's avatar
Bate committed
556 557
				as `nom_utilisateur`,
				pl2_user_gap.id_user_gap as id_user_gap
Bate's avatar
Bate committed
558
			 FROM pl2_user_gap
Bate's avatar
Bate committed
559
			 JOIN utilisateurs
Bate's avatar
Bate committed
560
			 ON utilisateurs.id_utilisateur = pl2_user_gap.id_utilisateur
Bate's avatar
Bate committed
561
			 LEFT OUTER JOIN utl_etu_utbm
Bate's avatar
Bate committed
562
			 ON utilisateurs.id_utilisateur = utl_etu_utbm.id_utilisateur
Bate's avatar
Bate committed
563 564 565 566 567 568 569 570
			 WHERE id_gap = $gap_id
			 AND utilisateurs.id_utilisateur NOT IN
			 (	SELECT id_utilisateur FROM pl2_absence
				JOIN pl2_gap
				ON pl2_gap.id_planning = pl2_absence.id_planning
				WHERE (pl2_absence.start < '$start' AND pl2_absence.end > '$start')
				OR  (pl2_absence.start < '$end' AND pl2_absence.start > '$start')
			 )
Bate's avatar
Bate committed
571 572
			 AND pl2_user_gap.start <= '$start'
			 AND pl2_user_gap.end >= '$end'");
Bate's avatar
Bate committed
573 574 575 576 577 578
		
	}
	
}

?>