~ubuntu-branches/ubuntu/hardy/gallery2/hardy-security

« back to all changes in this revision

Viewing changes to modules/rating/module.inc

  • Committer: Bazaar Package Importer
  • Author(s): Michael C. Schultheiss
  • Date: 2006-04-16 16:42:35 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060416164235-8uy0u4bfjdxpge2o
Tags: 2.1.1-1
* New upstream release (Closes: #362936)
  + Bugfixes for Postgres7 (Closes: #359000, #362152)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/*
 
3
 * $RCSfile: module.inc,v $
 
4
 *
 
5
 * Gallery - a web based photo album viewer and editor
 
6
 * Copyright (C) 2000-2006 Bharat Mediratta
 
7
 *
 
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.
 
12
 *
 
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.
 
17
 *
 
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.
 
21
 */
 
22
/**
 
23
 * @package Rating
 
24
 * @version $Revision: 1.4 $ $Date: 2006/03/22 03:52:34 $
 
25
 * @author  Don Seiler <don@seiler.us>
 
26
 */
 
27
 
 
28
/**
 
29
 * Rating
 
30
 *
 
31
 * Provides item rating mechanism for Gallery2
 
32
 *
 
33
 * @package Rating
 
34
 */
 
35
class RatingModule extends GalleryModule {
 
36
 
 
37
    function RatingModule() {
 
38
        global $gallery;
 
39
 
 
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));
 
48
    }
 
49
 
 
50
    /**
 
51
     * @see GalleryModule::registerEventListeners()
 
52
     */
 
53
    function registerEventListeners() {
 
54
        GalleryCoreApi::registerEventListener('GalleryEntity::delete', new RatingModule());
 
55
    }
 
56
 
 
57
    /**
 
58
     * @see GalleryModule::performFactoryRegistrations()
 
59
     */
 
60
    function performFactoryRegistrations() {
 
61
        $ret = GalleryCoreApi::registerFactoryImplementation( 'ItemEditOption', 'RatingItemEdit',
 
62
                'RatingItemEdit', 'modules/rating/RatingItemEdit.inc', 'rating',
 
63
                array('ItemEditAlbum'));
 
64
        if ($ret) {
 
65
            return $ret->wrap(__FILE__, __LINE__);
 
66
        }
 
67
 
 
68
        $ret = GalleryCoreApi::registerFactoryImplementation( 'GallerySortInterface_1_1',
 
69
                'RatingSortOrder', 'RatingSortOrder',
 
70
                'modules/rating/classes/RatingSortOrder.class', 'rating', null);
 
71
        if ($ret) {
 
72
            return $ret->wrap(__FILE__, __LINE__);
 
73
        }
 
74
 
 
75
        return null;
 
76
    }
 
77
 
 
78
 
 
79
    /**
 
80
     * @see GalleryModule::getItemSummaries()
 
81
     */
 
82
    function getItemSummaries($items, $permissions, &$template) {
 
83
        global $gallery;
 
84
 
 
85
        $itemIds = array();
 
86
        $enabledAlbums = array();
 
87
        $numEnabledAlbums = 0;
 
88
        foreach ($items as $item) {
 
89
            $itemIds[] = $item->getId();
 
90
 
 
91
            $parentId = $item->getParentId();
 
92
            if (isset($enabledAlbums[$parentId])) {
 
93
                continue;
 
94
            }
 
95
 
 
96
            list ($ret, $enabled) = GalleryCoreApi::getPluginParameter('module', 'rating',
 
97
                                        'enabled', $parentId);
 
98
            if ($ret) {
 
99
                return array($ret->wrap(__FILE__, __LINE__), null);
 
100
            }
 
101
 
 
102
            $enabledAlbums[$parentId] = !empty($enabled);
 
103
            if ($enabledAlbums[$parentId]) {
 
104
                $numEnabledAlbums++;
 
105
            }
 
106
        }
 
107
 
 
108
        if (empty($numEnabledAlbums)) {
 
109
            return array(null, array());
 
110
        }
 
111
 
 
112
        /* Load the G2 templating engine */
 
113
        GalleryCoreApi::requireOnce('modules/rating/classes/RatingHelper.class');
 
114
 
 
115
        $template->style('modules/rating/rating.css');
 
116
        $template->javascript('lib/javascript/XmlHttp.js');
 
117
        $template->javascript('modules/rating/rating.js');
 
118
 
 
119
        $template->setVariable('l10Domain', $this->getL10Domain());
 
120
        $RatingSummary = array('ratingValues' => array(1, 2, 3, 4, 5), 'firstCall' => true);
 
121
 
 
122
        /* Check to see if album rating is allowed */
 
123
        list ($ret, $allowAlbumRating) = GalleryCoreApi::getPluginParameter('module', 'rating',
 
124
                'allowAlbumRating');
 
125
        if ($ret) {
 
126
            return array($ret->wrap(__FILE__, __LINE__), null);
 
127
        }
 
128
 
 
129
        /* Acquire the current rating for the items (populate $RatingData) */
 
130
 
 
131
        list ($ret, $RatingData) = RatingHelper::fetchRatings($itemIds,
 
132
                $gallery->getActiveUserId());
 
133
        if ($ret) {
 
134
            return array($ret->wrap(__FILE__, __LINE__), null);
 
135
        }
 
136
 
 
137
        $summaries = array();
 
138
        foreach ($items as $item) {
 
139
            $itemId = $item->getId();
 
140
            $parentId = $item->getParentId();
 
141
 
 
142
            /* Check if item's parent album allows rating */
 
143
            if (!$enabledAlbums[$parentId]) {
 
144
                continue;
 
145
            }
 
146
 
 
147
            /* Check if this user can view rating for this item */
 
148
            if (!isset($permissions[$itemId]['rating.view'])) {
 
149
                continue;
 
150
            }
 
151
 
 
152
            if (GalleryUtilities::isA($item, 'GalleryAlbumItem') && !$allowAlbumRating) {
 
153
                continue;
 
154
            }
 
155
 
 
156
            if (!isset($RatingData[$itemId])) {
 
157
                $RatingData[$itemId] = array('itemId' => $itemId, 'rating' => 0, 'votes' => 0);
 
158
            }
 
159
 
 
160
            /* Set star information */
 
161
            $RatingData[$itemId]['stars'] = round($RatingData[$itemId]['rating'] * 2);
 
162
 
 
163
            if ($RatingData[$itemId]['rating'] == 0) {
 
164
                $RatingData[$itemId]['stars'] = 1;
 
165
            }
 
166
 
 
167
            if (!isset($RatingData[$itemId]['userRating'])) {
 
168
                $RatingData[$itemId]['userRating'] = 'N/A';
 
169
                $RatingData[$itemId]['userStars'] = 0;
 
170
            } else {
 
171
                $RatingData[$itemId]['userStars'] = round($RatingData[$itemId]['userRating'] * 2);
 
172
            }
 
173
 
 
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);
 
178
 
 
179
            /* Render and get the html */
 
180
            list ($ret, $summaries[$itemId]) =
 
181
                $template->fetch('gallery:modules/rating/templates/RatingInterface.tpl');
 
182
            if ($ret) {
 
183
                return array($ret->wrap(__FILE__, __LINE__), null);
 
184
            }
 
185
 
 
186
            $RatingSummary['firstCall'] = false;
 
187
        }
 
188
 
 
189
        return array(null, $summaries);
 
190
    }
 
191
 
 
192
 
 
193
    /**
 
194
     * @see GalleryModule::upgrade()
 
195
     */
 
196
    function upgrade($currentVersion) {
 
197
        global $gallery;
 
198
 
 
199
        if (!isset($currentVersion)) {
 
200
            $currentVersion = '0';
 
201
        }
 
202
 
 
203
        switch ($currentVersion) {
 
204
        case '0':
 
205
            list ($ret, $coreParams) = GalleryCoreApi::fetchAllPluginParameters('module', 'core');
 
206
            if ($ret) {
 
207
                return $ret->wrap(__FILE__, __LINE__);
 
208
            }
 
209
 
 
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(),
 
217
                        'rating.' . $p[0],
 
218
                        $p[1], $p[2], $p[3]);
 
219
                if ($ret) {
 
220
                    return $ret->wrap(__FILE__, __LINE__);
 
221
                }
 
222
            }
 
223
 
 
224
            /* Give everybody rating permission by default */
 
225
            $ret = GalleryCoreApi::addGroupPermission(
 
226
                $coreParams['id.rootAlbum'], $coreParams['id.everybodyGroup'],
 
227
                'rating.all', true);
 
228
            if ($ret) {
 
229
                return $ret->wrap(__FILE__, __LINE__);
 
230
            }
 
231
            break;
 
232
 
 
233
        case '1.0.0':
 
234
        case '1.0.1':
 
235
        case '1.0.2':
 
236
 
 
237
        case 'end of upgrade path':
 
238
            break;
 
239
 
 
240
        default:
 
241
            return GalleryCoreApi::error(ERROR_BAD_PLUGIN, __FILE__, __LINE__,
 
242
                    sprintf('Unknown module version %s', $currentVersion));
 
243
        }
 
244
 
 
245
        return null;
 
246
    }
 
247
 
 
248
 
 
249
    /**
 
250
     * @see GalleryModule::getSiteAdminViews()
 
251
     */
 
252
    function getSiteAdminViews() {
 
253
        return array(null,
 
254
                array(array('name' => $this->translate('Rating'),
 
255
                        'view' => 'rating.RatingSiteAdmin')));
 
256
    }
 
257
 
 
258
    /**
 
259
     * Event handler for GalleryEntity::delete events
 
260
     * Delete any item ratings if the associated item is deleted
 
261
     *
 
262
     * @see GalleryEventListener::handleEvent
 
263
     */
 
264
    function handleEvent($event) {
 
265
        $result = null;
 
266
        if ($event->getEventName() == 'GalleryEntity::delete') {
 
267
            $entity = $event->getEntity();
 
268
 
 
269
            if (GalleryUtilities::isA($entity, 'GalleryItem')) {
 
270
                $ret = GalleryCoreApi::removeMapEntry('RatingMap',
 
271
                                                      array('itemId' => $entity->getId()));
 
272
                if ($ret) {
 
273
                    return array($ret->wrap(__FILE__, __LINE__), null);
 
274
                }
 
275
                $ret = GalleryCoreApi::removeMapEntry('RatingCacheMap',
 
276
                                                      array('itemId' => $entity->getId()));
 
277
                if ($ret) {
 
278
                    return array($ret->wrap(__FILE__, __LINE__), null);
 
279
                }
 
280
            }
 
281
        }
 
282
        return array(null, $result);
 
283
    }
 
284
 
 
285
}
 
286
?>