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

class planning2 extends stdentity
{
	var $name;
	var $weekly;
	var $group;
	var $admin_group;
	var $start;
	var $end;

	function load_by_id( $id )
	{
		$req = new requete( $this->db, "SELECT * from `pl2_planning`
Bate's avatar
Bate committed
15
						WHERE `id_planning` = '".
Bate's avatar
Bate committed
16
17
18
19
20
21
22
23
24
25
26
27
28
						mysql_real_escape_string($id).
						"' 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
29
		$this->id		= $row['id_planning'];
Bate's avatar
Bate committed
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
		$this->group		= $row['id_group'];
		$this->admin_group	= $row['id_admin_group'];
		$this->name		= $row['name_planning'];
		$this->weekly		= $row['weekly_planning'];
		$this->start		= strtotime($row['start']);
		$this->end		= strtotime($row['end']);
	}

	function add ( $name, $group, $admin_group, $weekly, $start, $end )
	{
		$this->name = $name;
		$this->group = $group;
		$this->admin_group = $admin_group;
		$this->weekly = $weekly;
		$this->start = $start;
		$this->end = $end;

		$sql = new insert ($this->dbrw,
                       "pl2_planning",
                       array(
                             "name_planning" => $this->name,
			     "id_group" => $this->group,
			     "id_admin_group" => $this->admin_group,
			     "start" => date("Y-m-d H:i:s",$this->start),
			     "end" => date("Y-m-d H:i:s",$this->end),
                             "weekly_planning" => $this->weekly
                            )
                      );
		if ( !$sql->is_success() )
		{
			$this->id = null;
			return false;
		}

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

		return true;
	}

	function update ( $name, $group, $admin_group, $start, $end )
	{
		$this->name = $name;
		$this->group = $group;
		$this->admin_group = $admin_group;
		$this->start = $start;
		$this->end = $end;

		$sql = new update ($this->dbrw,
                       "pl2_planning",
                       array(
                             "name_planning" => $this->name,
			     "id_group" => $this->group,
			     "id_admin_group" => $this->admin_group,
			     "start" => date("Y-m-d H:i:s",$this->start),
			     "end" => date("Y-m-d H:i:s",$this->end),
                             "weekly_planning" => $this->weekly
                            ),
			array("id_planning" => $this->id)
                      );
	}

	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())
		{
			delete_gap( $gap_id );
		}
		$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 )
	{
		$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
120
		if($this->weekly && $start < $this->start)
Bate's avatar
Bate committed
121
			return -1;
Bate's avatar
Bate committed
122
		if($this->weekly && $end > $this->end)
Bate's avatar
Bate committed
123
124
125
			return -1;
		if($this->weekly)
		{
Bate's avatar
Bate committed
126
			if($end >= $this->weekly*24*3600)
Bate's avatar
Bate committed
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
				return -1;
		}
		$sql = new insert ($this->dbrw,
                       "pl2_gap",
                       array(
			     "id_planning" => $this->id,
                             "name_gap" => $gap_name,
			     "start" => date("Y-m-d H:i:s",$start),
                             "end" => date("Y-m-d H:i:s",$end),
			     "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 )
	{
		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
164
			if($end >= $this->weekly*24*3600)
Bate's avatar
Bate committed
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
				return -1;
		}
		$sql = new update ($this->dbrw,
                       "pl2_gap",
                       array(
			     "id_planning" => $this->id,
                             "name_gap" => $gap_name,
			     "max_users" => $max_users,
			     "start" => date("Y-m-d H:i:s",$start),
                             "end" => date("Y-m-d H:i:s",$end)
                            ),
		       array(
			     "id_gap" => $gap_id
		       )
                      );
	}

	function delete_gap( $gap_id )
	{
		$sql = new delete($this->dbrw, "pl2_user_gap",
			array(
                       		"id_gap" => $gap_id
			)
                );
		$sql = new delete($this->dbrw, "pl2_gap",
			array(
                             "id_gap" => $gap_id
                            )
                       );
	}

	function get_max_users_for( $gap_id, $start, $end )
	{
		$gap_id = mysql_escape_string($gap_id);
		$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;
		while(true)
		{
			$to_break = false;
			$sql = new requete($this->db,
				"SELECT min(start) FROM pl2_absence
				 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'
				 AND pl2_absence.start < '".date("Y-m-d H:i:s",$end)."'
				 AND pl2_absence.end > '".date("Y-m-d H:i:s",$new_start)."'");
			if($sql->lines <= 0)
				$to_break = true;
			else
			{
				list( $date_absence ) = $sql->get_row();
				if(is_null($date_absence))
					$to_break = true;
			}

			$sql = new requete($this->db,
				"SELECT min(end) FROM pl2_user_gap
				 JOIN pl2_gap
				 ON pl2_gap.id_gap = pl2_user_gap.id_gap
				 WHERE pl2_user_gap.start <= '".date("Y-m-d H:i:s",$new_start)."' 
				 AND pl2_user_gap.end > '".date("Y-m-d H:i:s",$new_start)."'");

			if($sql->lines <= 0)
				if($to_break)
					break;
			else
			{
				list( $date_min ) = $sql->get_row();
				if(is_null($date_min) && $to_break)
					break;
			}

			$date_min = ($date_min<$date_absence)?$date_min:$date_absence;
			
			$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'
				 AND pl2_user_gap.start <= '".date("Y-m-d H:i:s",$new_start)."'
				 AND pl2_user_gap.end >= '".date("Y-m-d H:i:s",$date_min)."'");
			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 )
	{
		$sql = new requete($this->db, 
			"SELECT * from pl2_user_gap
			 WHERE id_gap = '$gap_id'
Bate's avatar
Bate committed
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
			 AND id_utilisateur = '$user_id'
			 AND 
			 (
			 	(	 start <= '".date("Y-m-d H:i:s",$end)."'
					 AND start >= '".date("Y-m-d H:i:s",$start)."'
				)
			  	OR
				(        end >= '".date("Y-m-d H:i:s",$start)."'
					 AND end <= '".date("Y-m-d H:i:s",$end)."'
				)
			 )");
		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
297
298
299
300
301
302
303
304
305
306
307
308
			 AND 
			 (
			 	(	 start <= '".date("Y-m-d H:i:s",$end)."'
					 AND start >= '".date("Y-m-d H:i:s",$start)."'
				)
			  	OR
				(        end >= '".date("Y-m-d H:i:s",$start)."'
					 AND end <= '".date("Y-m-d H:i:s",$end)."'
				)
			 )");
		if($sql->lines > 0)
			return false;
Bate's avatar
Bate committed
309
		$users = $this->get_max_users_for($gap_id,$start,$end);
Bate's avatar
Bate committed
310
311
312
313
314
315
316
317
318
319
320
		$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)
	{
Bate's avatar
Bate committed
321
		if(!$this->is_user_addable($gap_id,$user_id,$start,$end))
Bate's avatar
Bate committed
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
			return -1;
		$sql = new insert ($this->dbrw,
                       "pl2_user_gap",
                       array(
			     "id_gap" => $gap_id,
                             "id_utilisateur" => $user_id,
			     "start" => date("Y-m-d H:i:s",$start),
                             "end" => date("Y-m-d H:i:s",$end)
                            )
                      );
		if ( !$sql->is_success() )
		{
			return -1;
		}

		return $sql->get_id();
	}

	function remove_user_from_gap( $user_gap_id )
	{
		$sql = new delete($this->dbrw, "pl2_user_gap",
			array(
                             "id_user_gap" => $user_gap_id
                            )
                       );
	}

	function get_gaps_for_user( $user_id)
	{
		return new requete($this->db,
			"SELECT id_gap FROM pl2_user_gap
			 WHERE id_utilisateur = $user_id");
	}

Bate's avatar
Bate committed
356
	function get_gaps( $start, $end )
Bate's avatar
Bate committed
357
	{
Bate's avatar
Bate committed
358
		if($this->weekly)
Bate's avatar
Bate committed
359
			return new requete($this->db,
Bate's avatar
Bate committed
360
				"SELECT id_gap, start, end, name_gap, max_users FROM pl2_gap 
Bate's avatar
Bate committed
361
362
363
364
				 WHERE id_planning = $this->id 
				 ORDER BY start ASC");
		else
			return new requete($this->db,
Bate's avatar
Bate committed
365
                                "SELECT id_gap, start, end, name_gap FROM pl2_gap 
Bate's avatar
Bate committed
366
367
368
369
370
371
372
373
                                 WHERE id_planning = $this->id
				 AND end > '".date("Y-m-d H:i:s",$start)."'
				 AND start < '".date("Y-m-d H:i:s",$end)."'
                                 ORDER BY start ASC");
	}

	function get_gaps_time( $start, $end )
	{
Bate's avatar
Bate committed
374
		if($this->weekly)
Bate's avatar
Bate committed
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
			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 
                                 WHERE id_planning = $this->id
				 AND end > '".date("Y-m-d H:i:s",$start)."'
				 AND start < '".date("Y-m-d H:i:s",$end)."'
                                 UNION DISTINCT
				 SELECT end as date FROM pl2_gap 
                                 WHERE id_planning = $this->id
				 AND end > '".date("Y-m-d H:i:s",$start)."'
				 AND start < '".date("Y-m-d H:i:s",$end)."'
                                 ORDER BY date ASC");
Bate's avatar
Bate committed
394
395
	}

Bate's avatar
Bate committed
396
397
398
	function get_gaps_names()
	{
		return new requete($this->db,
Bate's avatar
Bate committed
399
			"SELECT DISTINCT name_gap FROM pl2_gap WHERE id_planning = $this->id ORDER BY name_gap");
Bate's avatar
Bate committed
400
401
	}

Bate's avatar
Bate committed
402
403
404
405
406
407
408
	function get_gap_info( $gap_id )
	{
		return new requete($this->db,
                        "SELECT id_gap, name_gap, start, end FROM pl2_gap 
			 WHERE id_gap = $gap_id");
	}

Bate's avatar
Bate committed
409
410
411
	function get_gaps_from_names( $name )
	{
		return new requete($this->db,
Bate's avatar
Bate committed
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
			"SELECT id_gap FROM pl2_gap WHERE id_planning = $this->id AND gap_name = '$name' ORDER BY start");
	}

	function get_week_start( $date )
	{
		if($this->weekly)
		{
			$diff = $date - $this->start;
                        $date = $date - ($diff % ($this->weekly*3600*24));
		}
		else
		{
			$req = new requete($this->db,
				"SELECT start FROM pl2_gap 
				 WHERE id_planning = $this->id
				 AND start > ".date("Y-m-d H:i:s",$date)."
				 ORDER BY start ASC LIMIT 1");
			if($req->lines == 1)
			{
				list( $tmp ) = $req->get_row();
				$date = strtotime( $tmp );
			}
		}
Bate's avatar
Bate committed
435
		return strtotime(date("Y-m-d 00:00:00",$date));
Bate's avatar
Bate committed
436
437
	}

Bate's avatar
Bate committed
438
439
440
441
442
443
444
445
446
447
448
449
	function get_users_for_gap( $gap_id, $date )
	{
		
		$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
450
451
452
			/*$date = strtotime(date('o-\\WW',$date));*/
			$diff = $date - $this->start;
			$date = $date - ($diff % ($this->weekly*3600*24));
Bate's avatar
Bate committed
453
454
455
456
457
458
459
460
461
462
463
			$start = strtotime($start)+$date;
			$end = strtotime($end)+$date;
			$start =date("Y-m-d H:i:s",$start);
			$end =date("Y-m-d H:i:s",$end);	
		}
		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`)) 
				as `nom_utilisateur`
Bate's avatar
Bate committed
464
			 FROM pl2_user_gap
Bate's avatar
Bate committed
465
			 JOIN utilisateurs
Bate's avatar
Bate committed
466
467
468
			 ON utilisateurs.id_utilisateur = pl2_user_gap.id_utilisateur
			 JOIN utl_etu_utbm
			 ON utilisateurs.id_utilisateur = utl_etu_utbm.id_utilisateur
Bate's avatar
Bate committed
469
470
471
472
473
474
475
476
			 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
477
478
			 AND pl2_user_gap.start <= '$start'
			 AND pl2_user_gap.end >= '$end'");
Bate's avatar
Bate committed
479
480
481
482
483
484
		
	}
	
}

?>