Completed
Branch master (573628)
by Pierre-Henry
33:37
created

RatingDesignCore::getRatingData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 2
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author         Pierre-Henry Soria <[email protected]>
4
 * @copyright      (c) 2012-2018, Pierre-Henry Soria. All Rights Reserved.
5
 * @license        GNU General Public License; See PH7.LICENSE.txt and PH7.COPYRIGHT.txt in the root directory.
6
 * @package        PH7 / App / System / Core / Class / Design
7
 */
8
9
namespace PH7;
10
11
use PH7\Framework\Layout\Html\Design;
12
use PH7\Framework\Mvc\Router\Uri;
13
use PH7\Framework\Pattern\Statik;
14
15
class RatingDesignCore
16
{
17
    /**
18
     * Import the trait to set the class static.
19
     * The trait sets constructor/clone private to prevent instantiation.
20
     */
21
    use Statik;
22
23
    /**
24
     * Generates design the voting system.
25
     *
26
     * @param int $iId Unique ID of the column of the table. EX: ID of 'profileId' column for the 'Members' table.
27
     * @param string $sTable See the list of data tables available in the class: PH7\Framework\Mvc\Model\Engine\Util\Various::checkTable().
28
     * @param string $sCssClass Default value is empty. You can add the name of a CSS class (attention, only its name) e.g. 'center'.
29
     *
30
     * @return void
31
     */
32
    public static function voting($iId, $sTable, $sCssClass = '')
33
    {
34
        $aRating = self::getRatingData($iId, $sTable);
35
36
        // Note: The rating.css style file is included by default in the CMS
37
        (new Design)->staticFiles('js', PH7_STATIC . PH7_JS, 'jquery/rating.js');
38
39
40
        $fRate = ($aRating['votes'] > 0) ? number_format($aRating['score'] / $aRating['votes'], 1) : 0;
41
42
        $sPHSClass = 'pHS' . $iId . $sTable;
43
44
        echo '<div class="', $sCssClass, ' ', $sPHSClass, '" id="', $fRate, '_', $iId, '_', $sTable, '"></div><p class="', $sPHSClass, '_txt">', t('Score: %0% - Votes: %1%', $fRate, $aRating['votes']), '</p>
45
              <script>$(".', $sPHSClass, '").pHRating({length:5,decimalLength:1,rateMax:5});</script>';
46
47
        /**
48
         * Redirect the member to the registration page if not logged in.
49
         * For security reason, a check on the server-side is already present, because this JS code allows users to login easily by modifying it.
50
         */
51
        if (!UserCore::auth()) {
52
            $sUrl = Uri::get('user', 'signup', 'step1', '?msg=' . t('You need to be a member for voting.'), false);
53
            echo '<script>$(".', $sPHSClass, '").click(function(){window.location=\'', $sUrl, '\'});</script>';
54
        }
55
    }
56
57
    /**
58
     * @param int $iId
59
     * @param string $sTable
60
     *
61
     * @return array
62
     */
63
    private static function getRatingData($iId, $sTable)
64
    {
65
        $oRatingModel = new RatingCoreModel;
66
67
        $aRatingData = [
68
            'votes' => $oRatingModel->getVote($iId, $sTable),
69
            'score' => $oRatingModel->getScore($iId, $sTable)
70
        ];
71
        unset($oRatingModel);
72
73
        return $aRatingData;
74
    }
75
}
76