stars
[ class tree: stars ] [ index: stars ] [ all elements ]

Source for file LibertyStars.php

Documentation is available at LibertyStars.php

  1. <?php
  2. /**
  3. * $Header$
  4. * date created 2006/02/10
  5. * @author xing <xing@synapse.plus.com>
  6. * @version $Revision$
  7. * @package stars
  8. */
  9.  
  10. /**
  11.  * Setup
  12.  */
  13. require_once( KERNEL_PKG_PATH.'BitBase.php' );
  14.  
  15. /**
  16.  * Liberty Stars
  17.  * 
  18.  * @package stars
  19.  */
  20. class LibertyStars extends LibertyBase {
  21.     var $mContentId;
  22.  
  23.     /**
  24.      * Initiate Liberty Stars
  25.      * 
  26.      * @param array $pContentId Content id of the item being rated
  27.      * @access public
  28.      * @return void 
  29.      */
  30.     function __construct( $pContentId=NULL ) {
  31.         parent::__construct();
  32.         $this->mContentId = $pContentId;
  33.     }
  34.  
  35.     /**
  36.      * Load the data from the database
  37.      * 
  38.      * @access public
  39.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  40.      */
  41.     function load() {
  42.         if( $this->isValid() ) {
  43.             global $gBitSystem;
  44.             $stars = $gBitSystem->getConfig( 'stars_used_in_display', 5 );
  45.             $pixels = $stars *  $gBitSystem->getConfig( 'stars_icon_width', 22 );
  46.             $query = "
  47.                 SELECT ( `rating` * $pixels / 100 ) AS `stars_pixels`, `rating` AS `stars_rating`, `update_count` AS `stars_update_count`, `content_id`
  48.                 FROM `".BIT_DB_PREFIX."stars`
  49.                 WHERE `content_id`=?";
  50.             $this->mInfo = $this->mDb->getRow( $query, array( $this->mContentId ) );
  51.         }
  52.         return( count( $this->mInfo ) );
  53.     }
  54.  
  55.     /**
  56.      * get list of all rated content
  57.      *
  58.      * @param $pListHash contains array of items used to limit search results
  59.      * @param $pListHash[sort_mode] column and orientation by which search results are sorted
  60.      * @param $pListHash[find] search for a pigeonhole title - case insensitive
  61.      * @param $pListHash[max_records] maximum number of rows to return
  62.      * @param $pListHash[offset] number of results data is offset by
  63.      * @access public
  64.      * @return array of rated content
  65.      ***/
  66.     function getList( &$pListHash ) {
  67.         global $gBitSystem, $gBitUser, $gLibertySystem;
  68.  
  69.         $ret = $bindVars = array();
  70.         $where = $join = $select = '';
  71.  
  72.         // set custom sorting before we call prepGetList()
  73.         if( !empty( $pListHash['sort_mode'] )) {
  74.             $order = " ORDER BY ".$this->mDb->convertSortmode( $pListHash['sort_mode'] )." ";
  75.         } else {
  76.             // set a default sort_mode
  77.             $order = " ORDER BY sts.`rating` DESC";
  78.         }
  79.  
  80.         LibertyBase::prepGetList( $pListHash );
  81.  
  82.         if( !empty( $pListHash['user_id'] )) {
  83.             $where      .= empty( $where ) ? ' WHERE ' : ' AND ';
  84.             $where      .= " sth.`user_id`=? ";
  85.             $bindVars[]  = $pListHash['user_id'];
  86.             $select     .= ", sth.`rating` AS `user_rating`";
  87.             $join       .= " LEFT OUTER JOIN `".BIT_DB_PREFIX."stars_history` sth ON( sts.`content_id` = sth.`content_id` ) ";
  88.             $order       = " ORDER BY sth.`rating` DESC";
  89.         }
  90.  
  91.         if( !empty( $pListHash['find'] )) {
  92.             $where      .= empty( $where ) ? ' WHERE ' : ' AND ';
  93.             $where      .= " UPPER( lc.`title` ) LIKE ? ";
  94.             $bindVars[]  = '%'.strtoupper( $pListHash['find'] ).'%';
  95.         }
  96.  
  97.         $query = "
  98.             SELECT sts.*, lch.`hits`, lch.`last_hit`, lc.`event_time`, lc.`title`,
  99.             lc.`last_modified`, lc.`content_type_guid`, lc.`ip`, lc.`created` $select
  100.             FROM `".BIT_DB_PREFIX."stars` sts
  101.                 INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON ( lc.`content_id` = sts.`content_id` )
  102.                 LEFT JOIN `".BIT_DB_PREFIX."liberty_content_hits` lch ON ( lc.`content_id` = lch.`content_id` )
  103.             $join $where $order";
  104.  
  105.         $result = $this->mDb->query( $query, $bindVars, $pListHash['max_records'], $pListHash['offset'] );
  106.  
  107.         $stars = $gBitSystem->getConfig( 'stars_used_in_display', 5 );
  108.         $pixels = $stars *  $gBitSystem->getConfig( 'stars_icon_width', 22 );
  109.         while( $aux = $result->fetchRow() ) {
  110.             $type = &$gLibertySystem->mContentTypes[$aux['content_type_guid']];
  111.             if( empty( $type['content_object'] )) {
  112.                 include_once( $gBitSystem->mPackages[$type['handler_package']]['path'].$type['handler_file'] );
  113.                 $type['content_object'] = new $type['handler_class']();
  114.             }
  115.             if( !empty( $gBitSystem->mPackages[$type['handler_package']] )) {
  116.                 $aux['display_link'] = $type['content_object']->getDisplayLink( $aux['title'], $aux );
  117.                 $aux['title']        = $type['content_object']->getTitle( $aux );
  118.                 $aux['display_url']  = $type['content_object']->getDisplayUrl( $aux );
  119.                 $aux['stars_pixels'] = ($aux['rating'] * $pixels) / 100;
  120.             }
  121.             $ret[] = $aux;
  122.         }
  123.  
  124.         $query = "
  125.             SELECT COUNT( sts.`content_id` )
  126.             FROM `".BIT_DB_PREFIX."stars` sts
  127.                 INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON ( lc.`content_id` = sts.`content_id` )
  128.                 LEFT JOIN `".BIT_DB_PREFIX."liberty_content_hits` lch ON ( lc.`content_id` = lch.`content_id` )
  129.             $join $where";
  130.         $pListHash['cant'] = $this->mDb->getOne( $query, $bindVars );
  131.  
  132.         LibertyContent::postGetList( $pListHash );
  133.         return $ret;
  134.     }
  135.  
  136.     /**
  137.      * Get the rating history of a loaded content
  138.      * 
  139.      * @param boolean $pExtras loading the extras will get all users who have rated in the past and their ratings
  140.      * @access public
  141.      * @return TRUE on success, FALSE on failure
  142.      */
  143.     function loadRatingDetails() {
  144.         if( $this->isValid() ) {
  145.             global $gBitSystem;
  146.             $stars = $gBitSystem->getConfig( 'stars_used_in_display', 5 );
  147.             $pixels = $stars *  $gBitSystem->getConfig( 'stars_icon_width', 22 );
  148.             $query = "
  149.                 SELECT ( `rating` * $pixels / 100 ) AS `stars_pixels`, `rating` AS `stars_rating`, `update_count` AS `stars_update_count`, `content_id`
  150.                 FROM `".BIT_DB_PREFIX."stars`
  151.                 WHERE `content_id`=?";
  152.             $obj = $this->getLibertyObject( $this->mContentId );
  153.             $this->mInfo = $this->mDb->getRow( $query, array( $this->mContentId ) );
  154.             $this->mInfo = array_merge( $this->mInfo, $obj->mInfo );
  155.             $query = "
  156.                 SELECT sth.`content_id` as `hash_key`, sth.*, uu.`login`, uu.`real_name`
  157.                 FROM `".BIT_DB_PREFIX."stars_history` sth
  158.                     INNER JOIN `".BIT_DB_PREFIX."users_users` uu ON sth.`user_id`=uu.`user_id`
  159.                 WHERE sth.`content_id`=?
  160.                 ORDER BY sth.`rating` ASC";
  161.             $this->mInfo['user_ratings'] = $this->mDb->getAll( $query, array( $this->mContentId ) );
  162.         }
  163.         return( count( $this->mInfo ) );
  164.     }
  165.  
  166.     /**
  167.      * Quick method to get a nice summary of past ratings for a given content
  168.      * 
  169.      * @param array $pContentId 
  170.      * @access public
  171.      * @return usable hash with a summary of ratings of a given content id
  172.      */
  173.     function getRatingSummary( $pContentId = NULL ) {
  174.         if( !@BitBase::verifyId( $pContentId ) && $this->isValid() ) {
  175.             $pContentId = $this->mContentId;
  176.         }
  177.  
  178.         $ret['sum'] = $ret['weight'] = $ret['count'] = 0;
  179.         if( @BitBase::verifyId( $pContentId ) ) {
  180.             $query = "
  181.                 SELECT
  182.                     sth.`rating`,
  183.                     COUNT( sth.`rating`) AS `update_count`,
  184.                     SUM( sth.`weight` ) AS `weight`
  185.                 FROM `".BIT_DB_PREFIX."stars_history` sth
  186.                 WHERE sth.`content_id`=?
  187.                 GROUP BY sth.`rating`";
  188.             $result = $this->mDb->getAll( $query, array( $pContentId ) );
  189.  
  190.             foreach( $result as $set ) {
  191.                 $ret['sum']    += $set['weight'] * $set['rating'];
  192.                 $ret['weight'] += $set['weight'];
  193.                 $ret['count']  += $set['update_count'];
  194.             }
  195.         }
  196.         return $ret;
  197.     }
  198.  
  199.     /**
  200.      * @param array pParams hash of values that will be used to store the page
  201.      *
  202.      * @return bool TRUE on success, FALSE if store could not occur. If FALSE, $this->mErrors will have reason why
  203.      * @access public
  204.      ***/
  205.     function store( &$pParamHash ) {
  206.         global $gBitUser;
  207.         if( $this->verify( $pParamHash ) ) {
  208.             $table = BIT_DB_PREFIX."stars";
  209.             $this->mDb->StartTrans();
  210.             if( !empty( $this->mInfo ) ) {
  211.                 if( $this->getUserRating( $pParamHash['content_id'] ) ) {
  212.                     $result = $this->mDb->associateUpdate( $table."_history", $pParamHash['stars_history_store'], array( "content_id" => $this->mContentId, "user_id" => $gBitUser->mUserId ) );
  213.                     // we don't have a new entry in the database and the update_count stays the same
  214.                     unset( $pParamHash['stars_store']['update_count'] );
  215.                 } else {
  216.                     $result = $this->mDb->associateInsert( $table."_history", $pParamHash['stars_history_store'] );
  217.                 }
  218.                 $result = $this->mDb->associateUpdate( $table, $pParamHash['stars_store'], array( "content_id" => $this->mContentId ) );
  219.             } else {
  220.                 $result = $this->mDb->associateInsert( $table, $pParamHash['stars_store'] );
  221.                 $result = $this->mDb->associateInsert( $table."_history", $pParamHash['stars_history_store'] );
  222.             }
  223.             $this->mDb->CompleteTrans();
  224.         }
  225.         return( count( $this->mErrors )== 0 );
  226.     }
  227.  
  228.     /**
  229.      * Make sure the data is safe to store
  230.      *
  231.      * @param array pParams reference to hash of values that will be used to store the page, they will be modified where necessary
  232.      * @return bool TRUE on success, FALSE if verify failed. If FALSE, $this->mErrors will have reason why
  233.      * @access private
  234.      ***/
  235.     function verify( &$pParamHash ) {
  236.         global $gBitUser, $gBitSystem;
  237.  
  238.         if( $gBitUser->isRegistered() && $this->isValid() ) {
  239.             $this->load();
  240.             $pParamHash['content_id'] = $this->mContentId;
  241.  
  242.             // only store stuff if user hasn't rated this content before
  243.             if( $this->calculateRating( $pParamHash ) ) {
  244.                 // stars table
  245.                 $pParamHash['stars_store']['rating']              = ( int )$pParamHash['summary']['rating'];
  246.                 $pParamHash['stars_store']['update_count']        = ( int )$pParamHash['summary']['count'] + 1;
  247.  
  248.                 // keep this entry in the history
  249.                 $pParamHash['stars_history_store']['content_id']  = $pParamHash['stars_store']['content_id'] = ( int )$this->mContentId;
  250.                 $pParamHash['stars_history_store']['rating']      = ( int )$pParamHash['rating'];
  251.                 $pParamHash['stars_history_store']['weight']      = ( int )$pParamHash['user']['weight'];
  252.                 $pParamHash['stars_history_store']['rating_time'] = ( int )BitDate::getUTCTime();
  253.                 $pParamHash['stars_history_store']['user_id']     = ( int )$gBitUser->mUserId;
  254.             } else {
  255.                 $this->mErrors['calculate_rating'] = "There was a problem calculating the rating.";
  256.             }
  257.         } else {
  258.             $this->mErrors['unregistered'] = "You have to be registered to rate content.";
  259.         }
  260.  
  261.         return( count( $this->mErrors )== 0 );
  262.     }
  263.  
  264.     /**
  265.      * Get the rating of the currently active user for the specified content
  266.      * 
  267.      * @param array $pContentId 
  268.      * @access public
  269.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  270.      */
  271.     function getUserRating( $pContentId = NULL ) {
  272.         global $gBitSystem, $gBitUser;
  273.         $ret = FALSE;
  274.         if( !@BitBase::verifyId( $pContentId ) && $this->isValid() ) {
  275.             $pContentId = $this->mContentId;
  276.         }
  277.  
  278.         if( @BitBase::verifyId( $pContentId ) ) {
  279.             $stars = $gBitSystem->getConfig( 'stars_used_in_display', 5 );
  280.             $pixels = $stars *  $gBitSystem->getConfig( 'stars_icon_width', 22 );
  281.             $query = "
  282.                 SELECT (`rating` * $pixels / 100) AS `stars_user_pixels`, ( `rating` * $stars / 100 ) AS `stars_user_rating`
  283.                 FROM `".BIT_DB_PREFIX."stars_history`
  284.                 WHERE `content_id`=? AND `user_id`=?";
  285.             $ret = $this->mDb->getRow( $query, array( $pContentId, $gBitUser->mUserId ) );
  286.         }
  287.         return $ret;
  288.     }
  289.  
  290.     /**
  291.      * Check if the mContentId is set and valid
  292.      * 
  293.      * @access public
  294.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  295.      */
  296.     function isValid() {
  297.         return( @BitBase::verifyId( $this->mContentId ) );
  298.     }
  299.  
  300.     /**
  301.      * This function removes a stars entry
  302.      * 
  303.      * @access public
  304.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  305.      */
  306.     function expunge() {
  307.         $ret = FALSE;
  308.         if( $this->isValid() ) {
  309.             $query = "DELETE FROM `".BIT_DB_PREFIX."stars` WHERE `content_id` = ?";
  310.             $result = $this->mDb->query( $query, array( $this->mContentId ) );
  311.             $query = "DELETE FROM `".BIT_DB_PREFIX."stars_history` WHERE `content_id` = ?";
  312.             $result = $this->mDb->query( $query, array( $this->mContentId ) );
  313.         }
  314.         return $ret;
  315.     }
  316.  
  317.     // ============================ calculations ============================
  318.  
  319.     /**
  320.      * recalculate the rating of all objects - important when user changes weighting opions
  321.      * TODO: add some check to see if this was successfull, currenlty only returns true
  322.      * 
  323.      * @access public
  324.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  325.      */
  326.     function reCalculateRating() {
  327.         global $gBitSystem;
  328.  
  329.         // get all users that have rated and the content that has been rated
  330.         $result = $this->mDb->query( "SELECT `user_id`, `content_id` FROM `".BIT_DB_PREFIX."stars_history`" );
  331.         while( $aux = $result->fetchRow() ) {
  332.             $userIds[]    = $aux['user_id'];
  333.             $contentIds[] = $aux['content_id'];
  334.         }
  335.         $userIds    = array_unique( $userIds );
  336.         $contentIds = array_unique( $contentIds );
  337.  
  338.         // --- Update user weighting first
  339.         // update user weight in accordance with new settings
  340.         foreach( $userIds as $userId ) {
  341.             $userWeight = $this->calculateUserWeight( $userId );
  342.             $result = $this->mDb->query( "UPDATE `".BIT_DB_PREFIX."stars_history` SET `weight`=? WHERE `user_id`=?", array( $userWeight, $userId ) );
  343.         }
  344.  
  345.         // --- Update content rating
  346.         // remove all entries in the aggregated list
  347.         $result = $this->mDb->query( "DELETE FROM `".BIT_DB_PREFIX."stars`" );
  348.  
  349.         // update the calculations in the stars table
  350.         foreach( $contentIds as $content_id ) {
  351.             // get the rating history summary
  352.             $summary = $this->getRatingSummary( $content_id );
  353.  
  354.             // set the aggregated rating to 0 if we aren't displaying the rating yet.
  355.             $minRatings = $gBitSystem->getConfig( 'stars_minimum_ratings', 5 );
  356.             if( $summary['count'] < $minRatings ) {
  357.                 $rating = 0;
  358.             } else {
  359.                 $rating = round( $summary['sum'] / $summary['weight'] );
  360.             }
  361.  
  362.             $storeHash = array(
  363.                 'content_id'   => $content_id,
  364.                 'rating'       => $rating,
  365.                 'update_count' => $summary['count'],
  366.             );
  367.             $result = $this->mDb->associateInsert( BIT_DB_PREFIX."stars", $storeHash );
  368.         }
  369.         return TRUE;
  370.     }
  371.  
  372.     /**
  373.      * Calculate the correct value to insert into the database
  374.      * 
  375.      * @param array $pParamHash 
  376.      * @access public
  377.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  378.      */
  379.     function calculateRating( &$pParamHash ) {
  380.         global $gBitSystem, $gBitUser;
  381.         $stars = $gBitSystem->getConfig( 'stars_used_in_display', 5 );
  382.         $ret = FALSE;
  383.  
  384.         // TODO: factors that haven't been taken into accound yet:
  385.         //       - time since last rating(s) - how should this be dealt with?
  386.         //       - age of document - ???
  387.  
  388.         // number of ratings needed before value is displayed
  389.         if( @BitBase::verifyId( $pParamHash['stars_rating'] ) && $pParamHash['stars_rating'] > 0 && $pParamHash['stars_rating'] <= $stars && $this->isValid() ) {
  390.             // normalise to 100 weight
  391.             $pParamHash['rating'] = $pParamHash['stars_rating'] / $stars * 100;
  392.  
  393.             // if the user is submitting his rating again, we need to update the value in the db before we get the summary
  394.             if( $userRating = $this->getUserRating() ) {
  395.                 $tmpUpdate['rating'] = ( int )$pParamHash['rating'];
  396.                 $result = $this->mDb->associateUpdate( BIT_DB_PREFIX."stars_history", $tmpUpdate, array( "content_id" => $this->mContentId, "user_id" => $gBitUser->mUserId ) );
  397.             }
  398.  
  399.             $pParamHash['user']['weight'] = $this->calculateUserWeight();
  400.  
  401.             // get the rating history summary
  402.             $summary = $this->getRatingSummary();
  403.  
  404.             $minRatings = $gBitSystem->getConfig( 'stars_minimum_ratings', 5 );
  405.             if( ( $summary['count'] + 1 ) < $minRatings ) {
  406.                 $pParamHash['summary']['rating'] = 0;
  407.             } else {
  408.                 $pParamHash['summary']['rating'] = round( ( $summary['sum'] + ( $pParamHash['rating'] * $pParamHash['user']['weight'] ) ) / ( $summary['weight'] + $pParamHash['user']['weight'] ) );
  409.             }
  410.             $pParamHash['summary']['count'] = $summary['count'];
  411.             $ret = TRUE;
  412.         }
  413.         return $ret;
  414.     }
  415.  
  416.     /**
  417.      * Calculate the importance of a users rating
  418.      * 
  419.      * @param array $pUserId User id
  420.      * @access public
  421.      * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  422.      */
  423.     function calculateUserWeight( $pUserId = NULL ) {
  424.         global $gBitUser, $gBitSystem;
  425.         if( $gBitSystem->isFeatureActive( 'stars_user_weight' ) ) {
  426.  
  427.             // allow overriding of currently loaded user
  428.             if( @BitBase::verifyId( $pUserId ) ) {
  429.                 $tmpUser = new BitPermUser( $pUserId );
  430.                 $tmpUser->load( TRUE );
  431.             } else {
  432.                 $tmpUser = &$gBitUser;
  433.             }
  434.  
  435.             // age relative to site age
  436.             $query = "SELECT MIN( `registration_date` ) FROM `".BIT_DB_PREFIX."users_users`";
  437.             $age['site'] = BitDate::getUTCTime() - $this->mDb->getOne( $query );
  438.             $age['user'] = BitDate::getUTCTime() - $tmpUser->getField( 'registration_date' );
  439.             $userWeight['age'] = $age['user'] / $age['site'];
  440.  
  441.             // permissioning relative to full number of permissions
  442.             $query = "SELECT COUNT( `perm_name` ) FROM `".BIT_DB_PREFIX."users_permissions`";
  443.             if( $tmpUser->isAdmin() ) {
  444.                 $userWeight['permission'] = 1;
  445.             } else {
  446.                 $userWeight['permission'] = count( $tmpUser->mPerms ) / $this->mDb->getOne( $query );
  447.             }
  448.  
  449.             // activity - we could to the same using the history as well.
  450.             $query = "SELECT COUNT( `content_id` ) FROM `".BIT_DB_PREFIX."liberty_content` WHERE `user_id`=?";
  451.             $activity['user'] = $this->mDb->getOne( $query, array( $tmpUser->getField( 'user_id' ) ) );
  452.  
  453.             $query = "SELECT COUNT( `content_id` ) FROM `".BIT_DB_PREFIX."liberty_content`";
  454.             $activity['site'] = $this->mDb->getOne( $query );
  455.  
  456.             $userWeight['activity'] = $activity['user'] / $activity['site'];
  457.  
  458.             // here we can add some weight to various areas
  459.             $custom['age']        = $gBitSystem->getConfig( 'stars_weight_age' );
  460.             $custom['permission'] = $gBitSystem->getConfig( 'stars_weight_permission' );
  461.             $custom['activity']   = $gBitSystem->getConfig( 'stars_weight_activity' );
  462.  
  463.             foreach( $userWeight as $type => $value ) {
  464.                 $$type = 10 * $value * $custom[$type];
  465.                 if( empty( $$type ) ) {
  466.                     $$type = 1;
  467.                 }
  468.             }
  469.  
  470.             // TODO: run some tests to see if this is a good way of evaluating power of a user
  471.             // ensure that we always have a positive number here to avoid chaos - this alse makes sure new users have at least a bit of a say
  472.             if( ( $ret = round( log( $age * $permission * $activity, 2 ) ) ) < 1 ) {
  473.                 $ret = 1;
  474.             }
  475.         } else {
  476.             $ret = 1;
  477.         }
  478.  
  479.         return $ret;
  480.     }
  481. }
  482.  
  483. /********* SERVICE FUNCTIONS *********/
  484.  
  485. /**
  486.  * Prepare and assign data to templates
  487.  * 
  488.  * @access public
  489.  * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  490.  */
  491. function stars_template_setup() {
  492.     global $gBitSystem, $gBitUser, $gBitSmarty;
  493.     $stars = $gBitSystem->getConfig( 'stars_used_in_display', 5 );
  494.     $default_names = array();
  495.     for( $i = 0; $i < $stars; $i++ ) {
  496.         $default_names[] = ( $i + 1 )." / ".$gBitSystem->getConfig( 'stars_used_in_display' );
  497.     }
  498.     $default_names_flat = implode( ",", $default_names );
  499.     $ratingNames = explode( ",", "," . $gBitSystem->getConfig( 'stars_rating_names', $default_names_flat ));
  500.     $gBitSmarty->assign( 'ratingNames', $ratingNames );
  501.     $gBitSmarty->assign( 'starsLinks', $hash = array_fill( 1, $stars, 1 ));
  502.     $gBitSmarty->assign( 'loadStars', TRUE );
  503. }
  504.  
  505. /**
  506.  * Content list sql service function
  507.  * 
  508.  * @param array $pObject 
  509.  * @access public
  510.  * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  511.  */
  512. function stars_content_list_sql( &$pObject ) {
  513.     global $gBitSystem, $gBitUser, $gBitSmarty, $gBitThemes;
  514.     if( method_exists( $pObject, 'getContentType' ) && $gBitSystem->isFeatureActive( 'stars_rate_'.$pObject->getContentType() ) ) {
  515.         // in some cases, such as articles, rating is allowed when getList is called.
  516.         $stars = $gBitSystem->getConfig( 'stars_used_in_display', 5 );
  517.         $pixels = $stars *  $gBitSystem->getConfig( 'stars_icon_width', 22 );
  518.         $ret['select_sql'] = ",
  519.             lc.`content_id` AS `stars_load`,
  520.             sts.`update_count` AS stars_update_count,
  521.             sts.`rating` AS stars_rating,
  522.             ROUND( sts.`rating` * $pixels / 100 ) AS stars_pixels,
  523.             ROUND( sth.`rating` * $stars / 100 ) AS stars_user_rating,
  524.             ROUND( sth.`rating` * $pixels / 100 ) AS stars_user_pixels ";
  525.         $ret['join_sql'] = "
  526.             LEFT JOIN `".BIT_DB_PREFIX."stars` sts
  527.                 ON ( lc.`content_id`=sts.`content_id` )
  528.             LEFT JOIN `".BIT_DB_PREFIX."stars_history` sth
  529.                 ON ( lc.`content_id`=sth.`content_id` AND sth.`user_id`='".$gBitUser->mUserId."' )";
  530.         return $ret;
  531.     }
  532. }
  533.  
  534. /**
  535.  * Content load sql service function
  536.  * 
  537.  * @param array $pObject 
  538.  * @access public
  539.  * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  540.  */
  541. function stars_content_load_sql( &$pObject ) {
  542.     global $gBitSystem, $gBitUser, $gBitSmarty, $gBitThemes;
  543.     if( method_exists( $pObject, 'getContentType' ) && $gBitSystem->isFeatureActive( 'stars_rate_'.$pObject->getContentType() ) ) {
  544.         $stars = $gBitSystem->getConfig( 'stars_used_in_display', 5 );
  545.         $pixels = $stars *  $gBitSystem->getConfig( 'stars_icon_width', 22 );
  546.  
  547.         $ret['select_sql'] = ",
  548.             lc.`content_id` AS `stars_load`,
  549.             sts.`update_count` AS stars_update_count,
  550.             sts.`rating` AS stars_rating,
  551.             ROUND( sts.`rating` * $pixels / 100 ) AS stars_pixels,
  552.             ROUND( sth.`rating` * $stars / 100 ) AS stars_user_rating,
  553.             ROUND( sth.`rating` * $pixels / 100 ) AS stars_user_pixels ";
  554.         $ret['join_sql'] = "
  555.             LEFT JOIN `".BIT_DB_PREFIX."stars` sts
  556.                 ON ( lc.`content_id`=sts.`content_id` )
  557.             LEFT JOIN `".BIT_DB_PREFIX."stars_history` sth
  558.                 ON ( lc.`content_id`=sth.`content_id` AND sth.`user_id`='".$gBitUser->mUserId."' )";
  559.  
  560.         return $ret;
  561.     }
  562. }
  563.  
  564. /**
  565.  * Content expunge sql service function
  566.  * 
  567.  * @param array $pObject 
  568.  * @param array $pParamHash 
  569.  * @access public
  570.  * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  571.  */
  572. function stars_content_expunge( &$pObject, &$pParamHash ) {
  573.     $stars = new LibertyStars( $pObject->mContentId );
  574.     $stars->expunge();
  575. }
  576.  
  577. ?>

Documentation generated on Wed, 29 Jul 2015 13:56:57 +0000 by phpDocumentor 1.5.0-lsces