Completed
Push — master ( fea2cb...118c60 )
by Litera
21s
created

ProgramModel::getDetail()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 22
nc 2
nop 3
dl 0
loc 34
rs 8.8571
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A ProgramModel::getTutor() 0 9 1
A ProgramModel::findByBlockId() 0 13 1
1
<?php
2
3
namespace App\Models;
4
5
use Nette\Database\Context;
6
7
/**
8
 * Program model
9
 *
10
 * class for handling programs
11
 *
12
 * @created 2012-10-01
13
 * @author Tomas Litera <[email protected]>
14
 */
15
class ProgramModel extends BaseModel
16
{
17
18
	/**
19
	 * @var array
20
	 */
21
	public $columns = [
22
		'guid',
23
		'name',
24
		'block',
25
		'display_in_reg',
26
		'description',
27
		'tutor',
28
		'email',
29
		'category',
30
		'material',
31
		'capacity',
32
	];
33
34
	/**
35
	 * Array of form names
36
	 *
37
	 * @var array	form_names[]
38
	 */
39
	public $formNames = array();
40
41
	protected $table = 'kk_programs';
42
43
	private static $connection;
44
45
	/** Constructor */
46
	public function __construct(Context $database)
47
	{
48
		$this->formNames = array('guid', "name", "description", "material", "tutor", "email", "capacity", "display_in_reg", "block", "category");
49
		$this->setDatabase($database);
50
		self::$connection = $this->getDatabase();
51
	}
52
53
	/**
54
	 * Get programs
55
	 *
56
	 * @param	int		$block_id	ID of block
0 ignored issues
show
Bug introduced by
There is no parameter named $block_id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
57
	 * @param	int		$visitor_id	ID of visitor
0 ignored issues
show
Bug introduced by
There is no parameter named $visitor_id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
58
	 * @return	string				html
59
	 */
60
	public function getPrograms($blockId, $vid)
61
	{
62
		$blocks = $this->database
63
			->table($this->getTable())
64
			->where('block ? AND deleted ?', $blockId, '0')
65
			->limit(10)
66
			->fetchAll();
67
68
		if(!$blocks){
0 ignored issues
show
Bug Best Practice introduced by
The expression $blocks of type Nette\Database\IRow[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
69
			$html = "";
70
		} else {
71
			/*
72
			$progSql = "SELECT progs.name AS prog_name
73
						FROM kk_programs AS progs
74
						LEFT JOIN `kk_visitor-program` AS visprog ON progs.id = visprog.program
75
						LEFT JOIN kk_visitors AS vis ON vis.id = visprog.visitor
76
						WHERE vis.id = '".$id."'";
77
			*/
78
79
			$html = "<div>\n";
80
81
			$checked_flag = false;
82
			$html_input = "";
83
			foreach($blocks as $data){
84
				// full program capacity with visitors
85
				$fullProgramData = $this->database
86
					->query('SELECT COUNT(visitor) AS visitors FROM `kk_visitor-program` AS visprog
87
							LEFT JOIN kk_visitors AS vis ON vis.id = visprog.visitor
88
							WHERE program = ? AND vis.deleted = ?',
89
							$data['id'], '0')->fetch();
90
91
				// if the program is checked
92
				$program = $this->database
93
					->table('kk_visitor-program')
94
					->where('program ? AND visitor ?', $data['id'], $vid)
95
					->fetch();
96
97
				if($program){
98
					$checked = "checked='checked'";
99
					$checked_flag = true;
100
				} else {
101
					$checked = "";
102
				}
103
				// if the capacity is full
104
				if($fullProgramData['visitors'] >= $data['capacity']){
105
					$html_input .= "<input id='".$data['id'].$blockId."' ".$checked." disabled type='radio' name='blck_".$blockId."' value='".$data['id']."' />\n";
106
					$fullProgramInfo = " (NELZE ZAPSAT - kapacita programu je již naplněna!)";
107
				} else {
108
					$html_input .= "<input id='".$data['id'].$blockId."' ".$checked." type='radio' name='blck_".$blockId."' value='".$data['id']."' /> \n";
109
					$fullProgramInfo = "";
110
				}
111
				$html_input .= '<label for="'.$data['id'].$blockId.'">'.$data['name'].'</label>';
112
				$html_input .= $fullProgramInfo;
113
				$html_input .= "<br />\n";
114
			}
115
116
			// pokud uz jednou bylo zaskrtnuto, nezaskrtavam znovu
117
			if(!$checked_flag) $checked = "checked='checked'";
118
			else $checked = "";
119
120
			$html .= "<input ".$checked." type='radio' name='blck_".$blockId."' value='0' /> Nebudu přítomen <br />\n";
121
			$html .= $html_input;
122
123
			$html .= "</div>\n";
124
		}
125
126
		return $html;
127
	}
128
129
	public function getExportPrograms($blockId)
130
	{
131
		$exportPrograms = $this->database
132
			->table($this->getTable())
133
			->where('block ? AND deleted ?', $blockId, '0')
134
			->limit(10)
135
			->fetchAll();
136
137
		if(!$exportPrograms) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $exportPrograms of type Nette\Database\IRow[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
138
			$html = "";
139
		} else {
140
			$html = "<table>\n";
141
			foreach($exportPrograms as $data){
142
				$html .= "<tr>";
143
				//// resim kapacitu programu a jeho naplneni navstevniky
144
				$fullProgramData = $this->database
145
					->query('SELECT COUNT(visitor) AS visitors
146
							FROM `kk_visitor-program` AS visprog
147
							LEFT JOIN `kk_visitors` AS vis ON vis.id = visprog.visitor
148
							WHERE program = ?
149
							AND vis.deleted = ?',
150
							$data['id'], '0')->fetch();
151
152
				if($fullProgramData['visitors'] >= $data['capacity']){
153
					//$html .= "<input disabled type='radio' name='".$id."' value='".$data['id']."' />\n";
154
					$fullProgramInfo = "<span style='font-size:12px; font-weight:bold;'>".$fullProgramData['visitors']."/".$data['capacity']."</span> (kapacita programu je naplněna!)";
155
				}
156
				else {
157
					//$html .= "<input type='radio' name='".$id."' value='".$data['id']."' /> \n";
158
					$fullProgramInfo = "<span style='font-size:12px; font-weight:bold;'>".$fullProgramData['visitors']."/".$data['capacity']."</span>";
159
				}
160
				$html .= "<td style='min-width:270px;'>";
161
				$html .= "<a rel='programDetail' href='".PRJ_DIR."program/?id=".$data['id']."&cms=edit&page=export' title='".$data['name']."'>".$data['name']."</a>\n";
162
				$html .= "</td>";
163
				$html .= "<td>";
164
				$html .= $fullProgramInfo;
165
				$html .= "</td>";
166
				$html .= "</tr>\n";
167
			}
168
			$html .= "</table>\n";
169
		}
170
		return $html;
171
	}
172
173
	public function renderExportPrograms()
174
	{
175
		$programs = "";
176
177
		$progSql = $this->database
178
			->table('kk_blocks')
179
			->select('
180
				id,
181
				day,
182
				DATE_FORMAT(`from`, "%H:%i") AS `from`,
183
				DATE_FORMAT(`to`, "%H:%i") AS `to`,
184
				name,
185
				program'
186
			)
187
			->where('deleted = ? AND program = ? AND meeting = ?', '0', '1', $this->meetingId)
188
			->order('day ASC, from ASC')
189
			->fetchAll();
190
191
		if(!$progSql){
0 ignored issues
show
Bug Best Practice introduced by
The expression $progSql of type Nette\Database\IRow[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
192
			$programs .= "<div class='emptyTable' style='width:400px;'>Nejsou žádná aktuální data.</div>\n";
193
		} else {
194
			//// prasarnicka kvuli programu raftu - resim obsazenost dohromady u dvou polozek
195
			//$raftCountSql = "SELECT COUNT(visitor) AS raft FROM `kk_visitor-program` WHERE program='56|57'";
196
197
			foreach($progSql as $progData){
198
				//nemoznost volit predsnemovni dikusi
199
				if($progData['id'] == 63) $notDisplayed = "style='display:none;'";
200
				//obsazenost raftu
201
				/*
202
				elseif($raftCountData['raft'] >= 18){
203
					if($progData['id'] == 86) $notDisplayed = "style='display:none;'";
204
					else $notDisplayed = "";
205
				}
206
				*/
207
				else $notDisplayed = "";
208
				$programs .= "<div ".$notDisplayed.">".$progData['day'].", ".$progData['from']." - ".$progData['to']." : ".$progData['name']."</div>\n";
209
				if($progData['program'] == 1) $programs .= "<div ".$notDisplayed.">".$this->getExportPrograms($progData['id'])."</div>";
210
				$programs .= "<br />";
211
			}
212
		}
213
214
		return $programs;
215
	}
216
217
	/**
218
	 * Render data in a table
219
	 *
220
	 * @return	string	html of a table
221
	 */
222
	public function getData($program_id = NULL)
223
	{
224
		if(isset($program_id)) {
225
			$data = $this->database
226
				->table($this->getTable())
227
				->where('id ? AND deleted ?', $program_id, '0')
228
				->limit(1)
229
				->fetch();
230
		} else {
231
			$data = $this->database
232
				->query('SELECT programs.id AS id,
233
						programs.name AS name,
234
						programs.description AS description,
235
						programs.tutor AS tutor,
236
						programs.email AS email,
237
						blocks.name AS block,
238
						programs.capacity AS capacity,
239
						style,
240
						cat.name AS cat_name
241
				FROM kk_programs AS programs
242
				LEFT JOIN kk_blocks AS blocks ON blocks.id = programs.block
243
				LEFT JOIN kk_categories AS cat ON cat.id = programs.category
244
				WHERE blocks.meeting = ? AND programs.deleted = ? AND blocks.deleted = ?
245
				ORDER BY programs.id ASC',
246
				$this->meetingId, '0', '0')->fetchAll();
247
		}
248
249
		return $data;
250
	}
251
252
	/**
253
	 * @return Nette\Database\Table\ActiveRow
254
	 */
255
	public function all()
256
	{
257
		return $this->getDatabase()
258
				->query('SELECT programs.id AS id,
259
						programs.name AS name,
260
						programs.description AS description,
261
						programs.tutor AS tutor,
262
						programs.email AS email,
263
						blocks.name AS block,
264
						programs.capacity AS capacity,
265
						style,
266
						cat.name AS cat_name
267
				FROM kk_programs AS programs
268
				LEFT JOIN kk_blocks AS blocks ON blocks.id = programs.block
269
				LEFT JOIN kk_categories AS cat ON cat.id = programs.category
270
				WHERE blocks.meeting = ? AND programs.deleted = ? AND blocks.deleted = ?
271
				ORDER BY programs.id ASC',
272
				$this->getMeetingId(), '0', '0')->fetchAll();
273
	}
274
275
	/**
276
	 * @param  string $guid
277
	 * @return Nette\Database\Table\ActiveRow
278
	 */
279
	public function annotation($guid)
280
	{
281
		return $this->getDatabase()
282
				->table($this->getTable())
283
				->where('guid ? AND deleted ?', $guid, '0')
284
				->limit(1)
285
				->fetch();
286
	}
287
288
	/**
289
	 * Get programs on registration
290
	 *
291
	 * @param	int		ID of program
292
	 * @param	string	disabled
293
	 * @return	string	html
294
	 */
295
	public function getProgramsRegistration ($id, $disabled)
296
	{
297
		$result = $this->database
298
			->table($this->getTable())
299
			->where('block ? AND deleted ?', $id, '0')
300
			->limit(10)
301
			->fetchAll();
302
303
		if(!$result){
0 ignored issues
show
Bug Best Practice introduced by
The expression $result of type Nette\Database\IRow[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
304
			$html = "";
305
		}
306
		else{
307
			$html = "<div>\n";
308
			$html .= "<input ".$disabled." checked type='radio' name='".$id."' value='0' /> Nebudu přítomen <br />\n";
309
			foreach($result as $data){
310
				//// resim kapacitu programu a jeho naplneni navstevniky
311
				$fullProgramData = $this->database
312
					->query('SELECT COUNT(visitor) AS visitors FROM `kk_visitor-program` AS visprog
313
							LEFT JOIN kk_visitors AS vis ON vis.id = visprog.visitor
314
							WHERE program = ? AND vis.deleted = ?',
315
							$data['id'], '0')->fetch();
316
317
				// nezobrazeni programu v registraci, v adminu zaskrtavatko u programu
318
				if($data['display_in_reg'] == 0) $notDisplayedProg = "style='display:none;'";
319
				else $notDisplayedProg = "";
320
321
				if($fullProgramData['visitors'] >= $data['capacity']){
322
					$html .= "<div ".$notDisplayedProg."><input disabled type='radio' name='".$id."' value='".$data['id']."' />\n";
323
					$fullProgramInfo = " (NELZE ZAPSAT - kapacita programu je již naplněna!)";
324
				}
325
				else {
326
					$html .= "<div ".$notDisplayedProg."><input ".$disabled." type='radio' name='".$id."' value='".$data['id']."' /> \n";
327
					$fullProgramInfo = "";
328
				}
329
				$html .= "<a class='programLink' rel='programDetail' href='".HTTP_DIR."srazvs/detail.php?id=".$data['id']."&type=program' title='".file_get_contents(HTTP_DIR.'srazvs/detail.php?id='.$data['id'].'&type=program')."' target='_blank'>".$data['name']."</a>\n";
330
				$html .= $fullProgramInfo;
331
				$html .= "</div>\n";
332
			}
333
			$html .= "</div>\n";
334
		}
335
		return $html;
336
	}
337
338
	/**
339
	 * Get visitors registred on program
340
	 *
341
	 * @param	int		$programId	ID of program
342
	 * @return	string	html or null
343
	 */
344
	public function getProgramVisitors($programId = NULL)
345
	{
346
		if(!isset($programId)) {
347
			return NULL;
348
		} else {
349
			$html = "  <div style='border-bottom:1px solid black;text-align:right;'>účastníci</div>";
350
351
			$html .= "<br /><a style='text-decoration:none; display:block; margin-bottom:4px;' href='?cms=export-visitors&id=".$programId."'>
352
	      	<img style='border:none;' align='absbottom' src='".IMG_DIR."icons/pdf.png' />Účastníci programu</a>";
353
354
			$result = $this->database
355
				->query('SELECT vis.name AS name,
356
								vis.surname AS surname,
357
								vis.nick AS nick
358
						FROM kk_visitors AS vis
359
						LEFT JOIN `kk_visitor-program` AS visprog ON vis.id = visprog.visitor
360
						WHERE visprog.program = ? AND vis.deleted = ?',
361
						$programId, '0')->fetchAll();
362
			$i = 1;
363
			foreach($result as $data){
364
				$html .= $i.". ".$data['name']." ".$data['surname']." - ".$data['nick']."<br />";
365
				$i++;
366
			}
367
368
			return $html;
369
		}
370
	}
371
372
	public function getSelectedPrograms($visitorId) {
373
		$programs = "  <div style='border-bottom:1px solid black;text-align:right;'>vybrané programy</div>";
374
375
		$result = $this->database
376
			->query('SELECT progs.name AS prog_name,
377
							day,
378
							DATE_FORMAT(`from`, "%H:%i") AS `from`,
379
							DATE_FORMAT(`to`, "%H:%i") AS `to`
380
					FROM kk_programs AS progs
381
					LEFT JOIN `kk_visitor-program` AS visprog ON progs.id = visprog.program
382
					LEFT JOIN kk_visitors AS vis ON vis.id = visprog.visitor
383
					LEFT JOIN kk_blocks AS blocks ON progs.block = blocks.id
384
					WHERE vis.id = ?
385
					ORDER BY `day`, `from` ASC',
386
					$visitorId)->fetchAll();
387
388
		foreach($result as $progData){
389
			$programs .= $progData['day'].", ".$progData['from']." - ".$progData['to']."";
390
			$programs .= "<div style='padding:5px 0px 5px 20px;'>- ".$progData['prog_name']."</div>";
391
		}
392
393
		return $programs;
394
	}
395
396
	/**
397
	 * @param  int    $visitorId
398
	 * @return array
399
	 */
400
	public function findByVisitorId(int $visitorId): array
401
	{
402
		return $this->getDatabase()
403
			->query('SELECT progs.name AS prog_name,
404
							day,
405
							DATE_FORMAT(`from`, "%H:%i") AS `from`,
406
							DATE_FORMAT(`to`, "%H:%i") AS `to`
407
					FROM kk_programs AS progs
408
					LEFT JOIN `kk_visitor-program` AS visprog ON progs.id = visprog.program
409
					LEFT JOIN kk_visitors AS vis ON vis.id = visprog.visitor
410
					LEFT JOIN kk_blocks AS blocks ON progs.block = blocks.id
411
					WHERE vis.id = ?
412
					ORDER BY `day`, `from` ASC',
413
					$visitorId)
414
			->fetchAll();
415
	}
416
417
	public static function getPdfPrograms($id, $vid, $database){
418
		$result = $database
419
			->table('kk_programs')
420
			->where('block ? AND deleted ?', $id, '0')
421
			->limit(10)
422
			->fetchAll();
423
424
		if(!$result){
425
			$html = "";
426
		} else {
427
428
			$html = "<div class='program'>\n";
429
430
			foreach($result as $data){
431
				$rows = $database
432
					->table('kk_visitor-program')
433
					->where('program ? AND visitor ?', $data->id, $vid)
434
					->fetchAll();
435
				if($rows) $html .= $data['name'];
436
			}
437
			$html .= "</div>\n";
438
		}
439
440
		return $html;
441
	}
442
443
	public function getProgramsLarge($id){
444
		$result = $this->database
445
			->query('SELECT progs.name AS name,
446
						cat.style AS style
447
				FROM kk_programs AS progs
448
				LEFT JOIN kk_categories AS cat ON cat.id = progs.category
449
				WHERE block = ? AND progs.deleted = ?
450
				LIMIT 10',
451
				$id, '0')->fetchAll();
452
453
		if(!$result) $html = "";
0 ignored issues
show
Bug Best Practice introduced by
The expression $result of type Nette\Database\IRow[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
454
		else {
455
			$html = "<table>";
456
			$html .= " <tr>";
457
			foreach($result as $data){
458
				$html .= "<td class='category cat-".$data['style']."' >".$data['name']."</td>\n";
459
			}
460
			$html .= " </tr>\n";
461
			$html .= "</table>\n";
462
		}
463
		return $html;
464
	}
465
466
	public static function getProgramNames($block_id)
467
	{
468
		$result = self::$connection
469
			->table('kk_programs')
470
			->select('name')
471
			->where('block ? AND deleted ?', $block_id, '0')
472
			->limit(10)
473
			->fetchAll();
474
475
		$html = '';
476
477
		if(!$result) $html = "";
0 ignored issues
show
Bug Best Practice introduced by
The expression $result of type Nette\Database\IRow[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
478
		else {
479
			foreach($result as $data){
480
				$html .= $data['name'].",\n";
481
			}
482
		}
483
		return $html;
484
	}
485
486
	/**
487
	 * Get tutor e-mail address
488
	 *
489
	 * @param int $id id of block item
490
	 * @return Nette\Database\Table\ActiveRow object with e-mail address
491
	 */
492
	public function getTutor($id)
493
	{
494
		return $this->getDatabase()
495
			->table($this->getTable())
496
			->select('guid, email, tutor')
497
			->where('id ? AND deleted ?', $id, '0')
498
			->limit(1)
499
			->fetch();
500
	}
501
502
	/**
503
	 * @param  integer $blockId
504
	 * @return Row
505
	 */
506
	public function findByBlockId($blockId = null)
507
	{
508
		return $this->getDatabase()
509
			->query('SELECT	progs.id AS id,
510
						progs.name AS name,
511
						style
512
				FROM kk_programs AS progs
513
				LEFT JOIN kk_categories AS cat ON cat.id = progs.category
514
				WHERE block = ? AND progs.deleted = ?
515
				LIMIT 10',
516
				$blockId, '0')
517
			->fetchAll();
518
	}
519
520
	/**
521
	 * @param  int  $programId
522
	 * @return Row
523
	 */
524
	public function findByProgramId(int $programId)
525
	{
526
		return $this->getDatabase()
527
			->table($this->getTable())
528
			->where('id ? AND deleted ?', $programId, '0')
529
			->limit(1)
530
			->fetch();
531
	}
532
533
	/**
534
	 * @param  int  $programId
535
	 * @return Row
536
	 */
537
	public function findProgramVisitors(int $programId)
538
	{
539
		return $this->getDatabase()
540
				->query('SELECT vis.name AS name,
541
								vis.surname AS surname,
542
								vis.nick AS nick
543
						FROM kk_visitors AS vis
544
						LEFT JOIN `kk_visitor-program` AS visprog ON vis.id = visprog.visitor
545
						WHERE visprog.program = ? AND vis.deleted = ?',
546
						$programId, '0')->fetchAll();
547
	}
548
549
	/**
550
	 * @param  int  $programId
551
	 * @return Row
552
	 */
553
	public function countProgramVisitors(int $programId)
554
	{
555
		return $this->getDatabase()
556
				->query('SELECT COUNT(*)
557
						FROM kk_visitors AS vis
558
						LEFT JOIN `kk_visitor-program` AS visprog ON vis.id = visprog.visitor
559
						WHERE visprog.program = ? AND vis.deleted = ?',
560
						$programId, '0')->fetch()[0];
561
	}
562
563
}
564