-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart.php
171 lines (144 loc) · 3.74 KB
/
start.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
166
167
168
169
170
171
<?php
/**
* ratings plugin
*
*/
elgg_register_event_handler('init', 'system', 'ratings_init');
function ratings_init() {
elgg_extend_view('css/elgg', 'ratings/css');
elgg_extend_view('js/elgg', 'ratings/js');
// registered with priority < 500 so other plugins can remove ratings
elgg_register_plugin_hook_handler('register', 'menu:river', 'ratings_river_menu_setup', 400);
elgg_register_plugin_hook_handler('register', 'menu:entity', 'ratings_entity_menu_setup', 400);
$actions_base = elgg_get_plugins_path() . 'ratings/actions/ratings';
elgg_register_action('ratings/add', "$actions_base/add.php");
elgg_register_action('ratings/delete', "$actions_base/delete.php");
}
/**
* Add ratings to entity menu at end of the menu
*/
function ratings_entity_menu_setup($hook, $type, $return, $params) {
if (elgg_in_context('widgets')) {
return $return;
}
$entity = $params['entity'];
// ratings button
$options = array(
'name' => 'ratings',
'text' => elgg_view('ratings/button', array('entity' => $entity)),
'href' => false,
'priority' => 1000,
);
$return[] = ElggMenuItem::factory($options);
// ratings count
$count = elgg_view('ratings/count', array('entity' => $entity));
if ($count) {
$options = array(
'name' => 'ratings_count',
'text' => $count,
'href' => false,
'priority' => 1001,
);
$return[] = ElggMenuItem::factory($options);
}
return $return;
}
/**
* Add a rate button to river actions
*/
function ratings_river_menu_setup($hook, $type, $return, $params) {
if (elgg_is_logged_in()) {
$item = $params['item'];
// only rate group creation #3958
if ($item->type == "group" && $item->view != "river/group/create") {
return $return;
}
// don't rate users #4116
if ($item->type == "user") {
return $return;
}
$object = $item->getObjectEntity();
if (!elgg_in_context('widgets') && $item->annotation_id == 0) {
if ($object->canAnnotate(0, 'ratings')) {
// rating ui
$options = array(
'name' => 'ratings',
'href' => false,
'text' => elgg_view('ratings/button', array('entity' => $object)),
'is_action' => true,
'priority' => 100,
);
$return[] = ElggMenuItem::factory($options);
// ratings count
$count = elgg_view('ratings/count', array('entity' => $object));
if ($count) {
$options = array(
'name' => 'ratings_count',
'text' => $count,
'href' => false,
'priority' => 101,
);
$return[] = ElggMenuItem::factory($options);
}
}
}
}
return $return;
}
/**
* Count how many people have rated an entity.
*
* @param ElggEntity $entity
*
* @return int Number of ratings
*/
function ratings_count($entity) {
$type = $entity->getType();
$params = array('entity' => $entity);
$number = elgg_trigger_plugin_hook('ratings:count', $type, $params, false);
if ($number) {
return $number;
} else {
return $entity->countAnnotations('rating');
}
}
/**
* Notify $user that $voter rated his $entity.
*
* @param type $user
* @param type $voter
* @param type $entity
*/
function ratings_notify_user(ElggUser $user, ElggUser $r, ElggEntity $entity) {
if (!$user instanceof ElggUser) {
return false;
}
if (!$voter instanceof ElggUser) {
return false;
}
if (!$entity instanceof ElggEntity) {
return false;
}
$title_str = $entity->title;
if (!$title_str) {
$title_str = elgg_get_excerpt($entity->description);
}
$site = get_config('site');
$subject = elgg_echo('ratings:notifications:subject', array(
$voter->name,
$title_str
));
$body = elgg_echo('ratings:notifications:body', array(
$user->name,
$voter->name,
$title_str,
$site->name,
$entity->getURL(),
$voter->getURL()
));
notify_user($user->guid,
$voter->guid,
$subject,
$body
);
}