-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPlus1Minus1Plugin.php
165 lines (141 loc) · 6.96 KB
/
Plus1Minus1Plugin.php
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
<?php
# Copyright (c) 2012 <tobias.thelen@iais.fraunhofer.de>
#
# Plus1Minus1Plugin
#
# Enables [+1-1] Markup to embed small +1/-1-Votings
#
# Version 0.2.0 (2012/02/07): Voting id will be generated on save
# Version 0.1.0 (2012/02/02): User has to invent unique md5 hash, markup is [+1-1:<unique md5 hash]
#
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
class Plus1Minus1Plugin extends StudipPlugin implements SystemPlugin
{
function __construct()
{
parent::__construct();
// transformation markup for voting element
StudipTransformFormat::addStudipMarkup('plus1minus1', '\[(\+1-1|\+1)\]', NULL, 'Plus1Minus1Plugin::transformMarkupPlus1Minus1');
// markup for voting element
StudipFormat::addStudipMarkup('plus1minus1', '\[(\+1-1|\+1):([a-f0-9]{32})\]', NULL, 'Plus1Minus1Plugin::markupPlus1Minus1');
// use template mechanism to inject plugin url into jquery code
// template is in templates/vote_click.php
$template_path = $this->getPluginPath() . '/templates';
$this->template_factory = new Flexi_TemplateFactory($template_path);
$template = $this->template_factory->open('vote_click');
PageLayout::addHeadElement('script', array(), $template->render());
//
// <!-- UI Tools: Tabs, Tooltip, Scrollable and Overlay (4.45 Kb) -->
// PageLayout::addScript("http://cdn.jquerytools.org/1.2.6/tiny/jquery.tools.min.js");
// add some nice css
PageLayout::addStyleSheet($this->getPluginURL().'/css/plus1minus1.css');
}
function vote_action()
{
// vote_action is called via ajax: registers vote and sends replacements widget
// check valid user
// NOTE: every user can vote on every plus1minus1vote regardless of access rights to embedding object (wiki, forum, ...)
global $user;
if (!is_object($user) || $user->id == 'nobody') {
echo "invalid user.";
die();
}
// check vote and vote_id parameters
if (!($_REQUEST['vote']=='+1' || $_REQUEST['vote']=='-1') || !preg_match("/[a-z0-9]{32}/",$_REQUEST['vote_id'])) {
echo "invalid parms.";
die();
}
// check markup
if ($_REQUEST['markup']=='+1') {
$markup='+1';
} else if ($_REQUEST['markup']=='+1-1') {
$markup='+1-1';
} else {
$markup='+1-1';
}
$st = DBManager::get()->prepare("INSERT IGNORE INTO plus1minus1_vote_user (vote_id, user_id, vote) VALUES (?,?,?)");
$st->execute(array($_REQUEST['vote_id'], $user->id, $_REQUEST['vote']));
echo Plus1Minus1Plugin::getWidget($markup, $_REQUEST['vote_id'], $user->id);
die();
}
static function getWidget($markup, $vote_id, $user_id=NULL) {
global $user;
// count + and -
$st = DBManager::get()->prepare("SELECT COUNT(*) FROM plus1minus1_vote_user WHERE vote_id=? AND vote=?");
$st->execute(array($vote_id, '+1'));
$plus = $st->fetchColumn();
$st->execute(array($vote_id, '-1'));
$minus = $st->fetchColumn();
// $st = DBManager::get()->prepare("SELECT user_id,vote FROM plus1minus1_vote_user WHERE vote_id=? LIMIT 10");
// $st->execute(array($vote_id));
// $users=array();
// while ($row = $st->fetch(PDO::FETCH_ASSOC)) {
// $users[] = $row['vote'].' <a href="about.php?username='.get_username($row['user_id']).'">'.htmlReady(get_fullname($row["user_id"]))."</a>";
// }
// print_r($users);
// $users_title = implode("<br/>",$users);
// nice vote icon in front
// $out="<img src='".$GLOBALS['ASSETS_URL']."images/icons/16/black/vote.png' style='margin-bottom:-3px; margin-right:5px;' class='plus1minus1listvotes' title='".$users_title."'>";
$out="<img src='".$GLOBALS['ASSETS_URL']."images/icons/16/black/vote.png' style='margin-bottom:-3px; margin-right:5px;'>";
if (!is_object($user) || $user->id == 'nobody') {
// nobody and anonymous can't vote
if ($markup=='+1-1') {
$out .= '+%d -%d';
} else if ($markup=='+1') {
$out .= '+%d';
}
} else {
// users may vote if they have not
$st = DBManager::get()->prepare("SELECT vote FROM plus1minus1_vote_user WHERE vote_id=? AND user_id=?");
$st->execute(array($vote_id, $user->id));
if ($st->rowCount() > 0) { // already voted
$vote = $st->fetchColumn();
if ($markup=='+1-1') {
$out .= ($vote=='+1' ? '<b>+%d</b> ' : '+%d ');
$out .= ($vote=='-1' ? '<b>-%d</b>' : '-%d');
} else if ($markup=='+1') {
$out .= ($vote=='+1' ? '<b>+%d</b>' : '+%d');
}
} else { // not voted yet
if ($markup=='+1-1') {
$out .= '<a href="#" class="plus1minus1vote" data-vote="+1" data-voteid="'.$vote_id.'" data-markup="+1-1">+1</a> (%d) ';
$out .= '<a href="#" class="plus1minus1vote" data-vote="-1" data-voteid="'.$vote_id.'" data-markup="+1-1">-1</a> (%d)';
} else if ($markup=='+1') {
$out .= '<a href="#" class="plus1minus1vote" data-vote="+1" data-voteid="'.$vote_id.'" data-markup="+1">+1</a> (%d)';
}
}
}
if ($markup=='+1-1') {
return sprintf($out, $plus, $minus);
} else if ($markup=='+1') {
return sprintf($out, $plus);
}
}
static function transformMarkupPlus1Minus1($markup, $matches, $contents)
{
return "\[".$matches[1].":".md5(uniqid("Plus1Minus1Plugin::transformMarkupPlus1Minus1"))."\]";
}
static function markupPlus1Minus1($markup, $matches, $contents)
{
// create a widget for given id (md5 hash - ensured by markup regex)
return "<span class='plus1minus1widget'>".Plus1Minus1Plugin::getWidget($matches[1], $matches[2])."</span>";
# return sprintf('<a href="https://develop.studip.de/trac/search?q=%s">%s</a>', htmlReady($matches[0]), htmlReady($matches[0]));
}
}