Skip to content

Instantly share code, notes, and snippets.

@elivz
Forked from kmgdevelopment/gist:3836913
Created October 4, 2012 23:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elivz/3837046 to your computer and use it in GitHub Desktop.
Save elivz/3837046 to your computer and use it in GitHub Desktop.
Average Rating Extension
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* ExpressionEngine - by EllisLab
*
* @package ExpressionEngine
* @author ExpressionEngine Dev Team
* @copyright Copyright (c) 2003 - 2011, EllisLab, Inc.
* @license http://expressionengine.com/user_guide/license.html
* @link http://expressionengine.com
* @since Version 2.0
* @filesource
*/
// ------------------------------------------------------------------------
/**
* Average Rating Extension
*
* @package ExpressionEngine
* @subpackage Addons
* @category Extension
* @author Kristen Grote
* @link http://www.kristengrote.com
*/
class Average_rating_ext {
public $settings = array();
public $description = 'Calculates the average value of a group of individual rating fields and populates a separate field with the value.';
public $docs_url = '';
public $name = 'Average Rating';
public $settings_exist = 'n';
public $version = '1.0';
private $EE;
/**
* Constructor
*
* @param mixed Settings array or empty string if none exist.
*/
public function __construct($settings = '')
{
$this->EE =& get_instance();
$this->settings = $settings;
}// ----------------------------------------------------------------------
/**
* Activate Extension
*
* This function enters the extension into the exp_extensions table
*
* @see http://codeigniter.com/user_guide/database/index.html for
* more information on the db class.
*
* @return void
*/
public function activate_extension()
{
// Setup custom settings in this array.
$this->settings = array();
$data = array(
'class' => __CLASS__,
'method' => 'update_average',
'hook' => 'entry_submission_ready',
'settings' => serialize($this->settings),
'version' => $this->version,
'enabled' => 'y'
);
$this->EE->db->insert('extensions', $data);
}
// ----------------------------------------------------------------------
/**
* update_average
*
* @param
* @return
*/
public function update_average($entry_id,$meta,$data)
{
// Make sure it's in the right channel
if ($data['channel_id'] != 2) return;
// Calculate the average
$average = ($data['field_id_5'] + $data['field_id_6'] + $data['field_id_7']) / 3;
// Save it!
$this->EE->db->update(
'exp_channel_data',
array('field_id_10' => $average),
array('entry_id' => $entry_id)
);
}
// ----------------------------------------------------------------------
/**
* Disable Extension
*
* This method removes information from the exp_extensions table
*
* @return void
*/
function disable_extension()
{
$this->EE->db->where('class', __CLASS__);
$this->EE->db->delete('extensions');
}
// ----------------------------------------------------------------------
/**
* Update Extension
*
* This function performs any necessary db updates when the extension
* page is visited
*
* @return mixed void on update / false if none
*/
function update_extension($current = '')
{
if ($current == '' OR $current == $this->version)
{
return FALSE;
}
}
// ----------------------------------------------------------------------
}
/* End of file ext.average_rating.php */
/* Location: /system/expressionengine/third_party/average_rating/ext.average_rating.php */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment