3
* $RCSfile: module.inc,v $
5
* Gallery - a web based photo album viewer and editor
6
* Copyright (C) 2000-2006 Bharat Mediratta
8
* This program is free software; you can redistribute it and/or modify
9
* it under the terms of the GNU General Public License as published by
10
* the Free Software Foundation; either version 2 of the License, or (at
11
* your option) any later version.
13
* This program is distributed in the hope that it will be useful, but
14
* WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
* General Public License for more details.
18
* You should have received a copy of the GNU General Public License
19
* along with this program; if not, write to the Free Software
20
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
24
* @version $Revision: 1.4 $ $Date: 2006/03/22 03:52:34 $
25
* @author Don Seiler <don@seiler.us>
31
* Provides item rating mechanism for Gallery2
35
class RatingModule extends GalleryModule {
37
function RatingModule() {
40
$this->setId('rating');
41
$this->setName($gallery->i18n('Rating'));
42
$this->setDescription($gallery->i18n('Item Rating Interface'));
43
$this->setVersion('1.0.3'); /* Update upgrade() too */
44
$this->setCallbacks('getSiteAdminViews|getItemSummaries|registerEventListeners');
45
$this->setGroup('data', $gallery->i18n('Extra Data'));
46
$this->setRequiredCoreApi(array(7, 0));
47
$this->setRequiredModuleApi(array(3, 0));
51
* @see GalleryModule::registerEventListeners()
53
function registerEventListeners() {
54
GalleryCoreApi::registerEventListener('GalleryEntity::delete', new RatingModule());
58
* @see GalleryModule::performFactoryRegistrations()
60
function performFactoryRegistrations() {
61
$ret = GalleryCoreApi::registerFactoryImplementation( 'ItemEditOption', 'RatingItemEdit',
62
'RatingItemEdit', 'modules/rating/RatingItemEdit.inc', 'rating',
63
array('ItemEditAlbum'));
65
return $ret->wrap(__FILE__, __LINE__);
68
$ret = GalleryCoreApi::registerFactoryImplementation( 'GallerySortInterface_1_1',
69
'RatingSortOrder', 'RatingSortOrder',
70
'modules/rating/classes/RatingSortOrder.class', 'rating', null);
72
return $ret->wrap(__FILE__, __LINE__);
80
* @see GalleryModule::getItemSummaries()
82
function getItemSummaries($items, $permissions, &$template) {
86
$enabledAlbums = array();
87
$numEnabledAlbums = 0;
88
foreach ($items as $item) {
89
$itemIds[] = $item->getId();
91
$parentId = $item->getParentId();
92
if (isset($enabledAlbums[$parentId])) {
96
list ($ret, $enabled) = GalleryCoreApi::getPluginParameter('module', 'rating',
97
'enabled', $parentId);
99
return array($ret->wrap(__FILE__, __LINE__), null);
102
$enabledAlbums[$parentId] = !empty($enabled);
103
if ($enabledAlbums[$parentId]) {
108
if (empty($numEnabledAlbums)) {
109
return array(null, array());
112
/* Load the G2 templating engine */
113
GalleryCoreApi::requireOnce('modules/rating/classes/RatingHelper.class');
115
$template->style('modules/rating/rating.css');
116
$template->javascript('lib/javascript/XmlHttp.js');
117
$template->javascript('modules/rating/rating.js');
119
$template->setVariable('l10Domain', $this->getL10Domain());
120
$RatingSummary = array('ratingValues' => array(1, 2, 3, 4, 5), 'firstCall' => true);
122
/* Check to see if album rating is allowed */
123
list ($ret, $allowAlbumRating) = GalleryCoreApi::getPluginParameter('module', 'rating',
126
return array($ret->wrap(__FILE__, __LINE__), null);
129
/* Acquire the current rating for the items (populate $RatingData) */
131
list ($ret, $RatingData) = RatingHelper::fetchRatings($itemIds,
132
$gallery->getActiveUserId());
134
return array($ret->wrap(__FILE__, __LINE__), null);
137
$summaries = array();
138
foreach ($items as $item) {
139
$itemId = $item->getId();
140
$parentId = $item->getParentId();
142
/* Check if item's parent album allows rating */
143
if (!$enabledAlbums[$parentId]) {
147
/* Check if this user can view rating for this item */
148
if (!isset($permissions[$itemId]['rating.view'])) {
152
if (GalleryUtilities::isA($item, 'GalleryAlbumItem') && !$allowAlbumRating) {
156
if (!isset($RatingData[$itemId])) {
157
$RatingData[$itemId] = array('itemId' => $itemId, 'rating' => 0, 'votes' => 0);
160
/* Set star information */
161
$RatingData[$itemId]['stars'] = round($RatingData[$itemId]['rating'] * 2);
163
if ($RatingData[$itemId]['rating'] == 0) {
164
$RatingData[$itemId]['stars'] = 1;
167
if (!isset($RatingData[$itemId]['userRating'])) {
168
$RatingData[$itemId]['userRating'] = 'N/A';
169
$RatingData[$itemId]['userStars'] = 0;
171
$RatingData[$itemId]['userStars'] = round($RatingData[$itemId]['userRating'] * 2);
174
$RatingData[$itemId]['averagePercent'] = $RatingData[$itemId]['rating'] * 100 / 5;
175
$RatingData[$itemId]['canRate'] = isset($permissions[$itemId]['rating.add']);
176
$template->setVariable('RatingData', $RatingData[$itemId]);
177
$template->setVariable('RatingSummary', $RatingSummary);
179
/* Render and get the html */
180
list ($ret, $summaries[$itemId]) =
181
$template->fetch('gallery:modules/rating/templates/RatingInterface.tpl');
183
return array($ret->wrap(__FILE__, __LINE__), null);
186
$RatingSummary['firstCall'] = false;
189
return array(null, $summaries);
194
* @see GalleryModule::upgrade()
196
function upgrade($currentVersion) {
199
if (!isset($currentVersion)) {
200
$currentVersion = '0';
203
switch ($currentVersion) {
205
list ($ret, $coreParams) = GalleryCoreApi::fetchAllPluginParameters('module', 'core');
207
return $ret->wrap(__FILE__, __LINE__);
210
/* Initial install. Register our permissions */
211
$permissions[] = array('add', $gallery->i18n('[rating] Add ratings'), 0, array());
212
$permissions[] = array('view', $gallery->i18n('[rating] View ratings'), 0, array());
213
$permissions[] = array('all', $gallery->i18n('[rating] All access'),
214
GALLERY_PERMISSION_COMPOSITE, array('rating.add', 'rating.view'));
215
foreach ($permissions as $p) {
216
$ret = GalleryCoreApi::registerPermission($this->getId(),
218
$p[1], $p[2], $p[3]);
220
return $ret->wrap(__FILE__, __LINE__);
224
/* Give everybody rating permission by default */
225
$ret = GalleryCoreApi::addGroupPermission(
226
$coreParams['id.rootAlbum'], $coreParams['id.everybodyGroup'],
229
return $ret->wrap(__FILE__, __LINE__);
237
case 'end of upgrade path':
241
return GalleryCoreApi::error(ERROR_BAD_PLUGIN, __FILE__, __LINE__,
242
sprintf('Unknown module version %s', $currentVersion));
250
* @see GalleryModule::getSiteAdminViews()
252
function getSiteAdminViews() {
254
array(array('name' => $this->translate('Rating'),
255
'view' => 'rating.RatingSiteAdmin')));
259
* Event handler for GalleryEntity::delete events
260
* Delete any item ratings if the associated item is deleted
262
* @see GalleryEventListener::handleEvent
264
function handleEvent($event) {
266
if ($event->getEventName() == 'GalleryEntity::delete') {
267
$entity = $event->getEntity();
269
if (GalleryUtilities::isA($entity, 'GalleryItem')) {
270
$ret = GalleryCoreApi::removeMapEntry('RatingMap',
271
array('itemId' => $entity->getId()));
273
return array($ret->wrap(__FILE__, __LINE__), null);
275
$ret = GalleryCoreApi::removeMapEntry('RatingCacheMap',
276
array('itemId' => $entity->getId()));
278
return array($ret->wrap(__FILE__, __LINE__), null);
282
return array(null, $result);