Completed
Push — master ( 1ce70a...0adf65 )
by Litera
11:25 queued 21s
created

BlockModel::findProgramBlocksByMeeting()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
rs 9.4285
1
<?php
2
3
namespace App\Models;
4
5
use Nette\Database\Context;
6
7
/**
8
 * Blocks
9
 *
10
 * class for handling program blocks
11
 *
12
 * @created 2012-09-14
13
 * @author Tomas Litera <[email protected]>
14
 */
15
class BlockModel extends BaseModel
16
{
17
18
	/** @var string */
19
	protected $table = 'kk_blocks';
20
21
	/** @var array */
22
	public $columns = [
23
		'guid',
24
		'name',
25
		'day',
26
		'from',
27
		'to',
28
		'program',
29
		'display_progs',
30
		'description',
31
		'tutor',
32
		'email',
33
		'category',
34
		'material',
35
		'capacity',
36
		//"meeting",
37
	];
38
39
	private static $connection;
40
41
	/**
42
	 * @param Context  $database
43
	 */
44
	public function __construct(Context $database)
45
	{
46
		$this->setDatabase($database);
47
		self::$connection = $this->getDatabase();
48
	}
49
50
	/**
51
	 * @return	ActiveRow
52
	 */
53
	public function all()
54
	{
55
		return $this->getDatabase()
56
			->query('SELECT blocks.guid AS guid,
57
						blocks.id AS id,
58
						blocks.name AS name,
59
						cat.name AS cat_name,
60
						day,
61
						DATE_FORMAT(`from`, "%H:%i") AS `from`,
62
						DATE_FORMAT(`to`, "%H:%i") AS `to`,
63
						description,
64
						tutor,
65
						email,
66
						style
67
				FROM kk_blocks AS blocks
68
				LEFT JOIN kk_categories AS cat ON cat.id = blocks.category
69
				WHERE blocks.meeting = ? AND blocks.deleted = ?
70
				ORDER BY day, `from` ASC',
71
				$this->getMeetingId(), '0')
72
			->fetchAll();
73
	}
74
75
	/**
76
	 * Render select box of blocks
77
	 *
78
	 * @param	int		selected option
79
	 * @return	string	html select box
80
	 */
81
	public function renderHtmlSelect($blockId)
82
	{
83
		$blocks = $this->findByMeeting($this->getMeetingId());
84
85
		$htmlSelect = "<select style='width: 300px; font-size: 10px' name='block'>\n";
86
87
		foreach($blocks as $block){
88
			if($block->id == $blockId) {
89
				$selected = 'selected';
90
			} else {
91
				$selected = '';
92
			}
93
			$htmlSelect .= sprintf(
94
				"<option %s value='%d'>%s, %s - %s : %s</option>\n",
95
				$selected,
96
				$block->id,
97
				$block->day,
98
				$block->from->format('%H:%I:%S'),
99
				$block->to->format('%H:%I:%S'),
100
				$block->name
101
			);
102
		}
103
104
		$htmlSelect .= "</select>\n";
105
106
		return $htmlSelect;
107
	}
108
109
	/**
110
	 * Return blocks that contents programs
111
	 *
112
	 * @param	int		meeting ID
113
	 * @return	array	result and number of affected rows
114
	 */
115
	public function getProgramBlocks($meetingId)
116
	{
117
		$data = $this->getDatabase()
118
			->query('SELECT id,
119
					day,
120
					DATE_FORMAT(`from`, "%H:%i") AS `from`,
121
					DATE_FORMAT(`to`, "%H:%i") AS `to`,
122
					name,
123
					program
124
				FROM kk_blocks
125
				WHERE deleted = ? AND program = ? AND meeting = ?
126
				ORDER BY `day`, `from` ASC',
127
				'0', '1', $meetingId)->fetchAll();
128
129
		return $data;
130
	}
131
132
	/**
133
	 * @param  integer $meetingId
134
	 * @return ActiveRow
135
	 */
136
	public function idsFromCurrentMeeting($meetingId)
137
	{
138
		return $this->getDatabase()
139
			->table($this->getTable())
140
			->select('id')
141
			->where('meeting ? AND program ? AND deleted ?', $meetingId, '1', '0')
142
			->fetchAll();
143
	}
144
145
	/**
146
	 * @param  integer $meetingId
147
	 * @param  string  $dayVal
148
	 * @return ActiveRow
149
	 */
150
	public function getExportBlocks($meetingId, $dayVal)
151
	{
152
		$result = $this->getDatabase()
153
			->query('SELECT blocks.id AS id,
154
						day,
155
						DATE_FORMAT(`from`, "%H:%i") AS `from`,
156
						DATE_FORMAT(`to`, "%H:%i") AS `to`,
157
						blocks.name AS name,
158
						program,
159
						display_progs,
160
						style,
161
						cat.id AS category
162
				FROM kk_blocks AS blocks
163
				LEFT JOIN kk_categories AS cat ON cat.id = blocks.category
164
				/* 18 - pauzy */
165
				WHERE blocks.deleted = ? AND day = ? AND meeting = ? AND category != ?
166
				ORDER BY `from` ASC',
167
				'0', $dayVal, $meetingId, '18')->fetchAll();
168
169
		return $result;
170
	}
171
172
	/**
173
	 * Get tutor e-mail address
174
	 *
175
	 * @param int $blockId id of block item
176
	 * @return Nette\Database\Table\ActiveRow object with e-mail address
177
	 */
178
	public function getTutor($blockId)
179
	{
180
		return $this->getDatabase()
181
			->table($this->getTable())
182
			->select('guid, email, tutor')
183
			->where('id ? AND deleted ?', $blockId, '0')
184
			->limit(1)
185
			->fetch();
186
	}
187
188
	/**
189
	 * @param  int $meetingId
190
	 * @return Database
191
	 */
192
	public function findByMeeting($meetingId)
193
	{
194
		return $this->getDatabase()
195
			->table($this->getTable())
196
			->select('id, day, from, to, name')
197
			->where('meeting ? AND program ? AND deleted ?', $meetingId, '1', '0')
198
			->fetchAll();
199
	}
200
201
	/**
202
	 * @param  string $day
203
	 * @return Row
204
	 */
205
	public function findByDay($day = '')
206
	{
207
		return $this->getDatabase()
208
				->query('SELECT	blocks.id AS id,
209
							day,
210
							DATE_FORMAT(`from`, "%H:%i") AS `from`,
211
							DATE_FORMAT(`to`, "%H:%i") AS `to`,
212
							blocks.name AS name,
213
							program,
214
							style
215
					FROM kk_blocks AS blocks
216
					LEFT JOIN kk_categories AS cat ON cat.id = blocks.category
217
					WHERE blocks.deleted = ? AND day = ? AND blocks.meeting = ?
218
					ORDER BY `from` ASC',
219
					'0', $day, $this->getMeetingId())
220
				->fetchAll();
221
	}
222
223
	/**
224
	 * Return blocks that contents programs
225
	 *
226
	 * @param	int		meeting ID
227
	 * @return	array	result and number of affected rows
228
	 */
229
	public function findProgramBlocksByMeeting(int $meetingId)
230
	{
231
		return $this->getDatabase()
232
			->query('SELECT id,
233
					day,
234
					DATE_FORMAT(`from`, "%H:%i") AS `from`,
235
					DATE_FORMAT(`to`, "%H:%i") AS `to`,
236
					name,
237
					program
238
				FROM kk_blocks
239
				WHERE deleted = ? AND program = ? AND meeting = ?
240
				ORDER BY `day`, `from` ASC',
241
				'0', '1', $meetingId)->fetchAll();
242
	}
243
244
	public function findByProgramId(int $programId)
0 ignored issues
show
Unused Code introduced by
The parameter $programId is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
245
	{
246
		return $this->getDatabase()
247
			->query()
248
			->fetch();
249
	}
250
251
}
252