1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
120
121
122
123
124
125
126
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
164
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
|
<?php
/**
* banner.php - model class for the banner administration
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* @author Nico Müller <nico.mueller@uni-oldenburg.de>
* @copyright 2012 Stud.IP Core-Group
* @license http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
* @category Stud.IP
* @package admin
* @since 2.4
*
* @property string $id alias column for ad_id
* @property string $ad_id database column
* @property string $banner_path database column
* @property string|null $description database column
* @property string|null $alttext database column
* @property string $target_type database column
* @property string $target database column
* @property int $startdate database column
* @property int $enddate database column
* @property int $priority database column
* @property int $views database column
* @property int $clicks database column
* @property int $mkdate database column
* @property int $chdate database column
* @property SimpleORMapCollection<BannerRoles> $banner_roles has_many BannerRoles
*/
class Banner extends SimpleORMap
{
protected static function configure($config = [])
{
$config['db_table'] = 'banner_ads';
$config['has_many']['banner_roles'] = [
'class_name' => BannerRoles::class,
'assoc_foreign_key' => 'ad_id',
'on_delete' => 'delete'
];
parent::configure($config);
}
/**
* Returns a random banner
*/
public static function getRandomBanner()
{
$query = "SELECT ad_id, priority, startdate, enddate
FROM banner_ads
WHERE priority > 0
AND (startdate = 0 OR startdate < UNIX_TIMESTAMP())
AND (enddate = 0 OR enddate > UNIX_TIMESTAMP())";
$statement = DBManager::get()->query($query);
// array that contains banner ids and an offset
// offsets start with 0 and increase by pow(2, priority)
// a random number between 0 and sum(pow(2,priorities)) is
// drawn and the banner with the highest offset smaller than
// this number is chosen
$banners = [];
$sum = 0;
// collect banners to consider, build banners array
while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
if (BannerRoles::checkUserAccess($row['ad_id'])) {
$sum += pow(2, $row['priority']);
$banners[] = [
'ad_id' => $row['ad_id'],
'offset' => $sum
];
}
}
// draw random number and select banner
$x = mt_rand(0, $sum);
$ad_id = false;
foreach ($banners as $i) {
if ($i['offset'] >= $x) {
$ad_id = $i['ad_id'];
break;
}
}
return new Banner($ad_id);
}
/**
* Get all banners
*
* @return array() list of banners
*/
public static function getAllBanners()
{
$query = "SELECT ad_id FROM banner_ads ORDER BY priority DESC";
$statement = DBManager::get()->query($query);
$ids = $statement->fetchAll(PDO::FETCH_COLUMN);
$banners = [];
foreach ($ids as $id) {
$banners[$id] = new Banner($id);
}
return $banners;
}
/**
* delete entry from database
* the object is cleared and turned to new state
* @return boolean
*/
public function delete()
{
if (!$this->isNew()) {
// Remove banner file
unlink($GLOBALS['DYNAMIC_CONTENT_PATH'] . '/banner/' . $this->banner_path);
}
return parent::delete();
}
/*
* Check the priority for a banner
* @param Int $prio priority (1-10)
* @return
*/
public function getViewProbability()
{
static $computed = false, $sum = null;
if ($this->priority == 0) {
return '--';
}
if ($computed === false) {
$sum = DBManager::get()->query("SELECT SUM(POW(2, priority)) FROM banner_ads WHERE priority > 0")
->fetchColumn();
$computed = true;
}
// return '1/' . (1 / (pow(2, $prio) / $sum));
return number_format(100 / (1 / (pow(2, $this->priority) / $sum)), 2, ',', '.') . '%';
}
/**
* Returns the appropriate link for this banner.
*
* @return string
*/
public function getLink($internal = false)
{
if ($this->isNew()) {
return '';
}
if ($internal) {
return URLHelper::getLink('dispatch.php/banner/click/' . $this->ad_id);
}
if ($this->target_type === 'url') {
return $this->target;
}
if ($this->target_type === 'seminar') {
return URLHelper::getLink('dispatch.php/course/details/', ['sem_id' => $this->target]);
}
if ($this->target_type === 'user') {
return URLHelper::getLink('dispatch.php/profile', ['username' => $this->target]);
}
if ($this->target_type === 'inst') {
return URLHelper::getLink(
'dispatch.php/institute/overview',
['auswahl' => $this->target]
);
}
return '';
}
/**
* Returns the img-tag for this banner
*
* @return string
*/
public function toImg($attributes = [])
{
$attr = [
'src' => $GLOBALS['DYNAMIC_CONTENT_URL'] . '/banner/' . $this->banner_path,
'border' => '0',
];
if ($this->alttext) {
$attr['title'] = $attr['alt'] = $this->alttext;
}
$attr = array_merge($attr, $attributes);
$attr_string = '';
foreach ($attr as $key => $value) {
$attr_string .= sprintf(' %s="%s"', $key, htmlReady($value));
}
return '<img' . $attr_string . '>';
}
/**
* Returns the complete html (link + img) for this banner
*
* @return string
*/
public function toHTML($internal = true)
{
if ($this->isNew()) {
return '';
}
if ($this->target_type === 'url') {
$template = '<a href="%s" target="_blank" rel="noopener noreferrer">%s</a>';
} elseif ($this->target_type === 'none') {
$template = '%2$s';
} else {
$template = '<a href="%s">%s</a>';
}
$link = sprintf($template, $this->getLink($internal), $this->toImg());
$this->views += 1;
$this->store();
return sprintf('<div class="studip-banner">%s</div>', $link);
}
}
|