Ajout d'une extension
This commit is contained in:
158
ext/phpbbstudio/aps/points/blockader.php
Normal file
158
ext/phpbbstudio/aps/points/blockader.php
Normal file
@@ -0,0 +1,158 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
* phpBB Studio - Advanced Points System. An extension for the phpBB Forum Software package.
|
||||
*
|
||||
* @copyright (c) 2019, phpBB Studio, https://www.phpbbstudio.com
|
||||
* @license GNU General Public License, version 2 (GPL-2.0)
|
||||
*
|
||||
*/
|
||||
|
||||
namespace phpbbstudio\aps\points;
|
||||
|
||||
/**
|
||||
* phpBB Studio - Advanced Points System blockader.
|
||||
*/
|
||||
class blockader
|
||||
{
|
||||
/** @var \phpbb\db\driver\driver_interface */
|
||||
protected $db;
|
||||
|
||||
/** @var string APS Display table */
|
||||
protected $blocks_table;
|
||||
|
||||
/** @var int User identifier used for admin desired blocks */
|
||||
protected $admin_id = 0;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param \phpbb\db\driver\driver_interface $db Database object
|
||||
* @param string $blocks_table APS Display table
|
||||
* @return void
|
||||
* @access public
|
||||
*/
|
||||
public function __construct(\phpbb\db\driver\driver_interface $db, $blocks_table)
|
||||
{
|
||||
$this->db = $db;
|
||||
$this->blocks_table = $blocks_table;
|
||||
}
|
||||
|
||||
/**
|
||||
* User identifier user for admin desired blocks.
|
||||
*
|
||||
* @return int The admin identifier
|
||||
* @access public
|
||||
*/
|
||||
public function get_admin_id()
|
||||
{
|
||||
return $this->admin_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch a row from the database for the provided user identifier.
|
||||
*
|
||||
* @param int $user_id The user identifier
|
||||
* @return array The json decoded database row
|
||||
* @access public
|
||||
*/
|
||||
public function row($user_id)
|
||||
{
|
||||
$sql = 'SELECT aps_display FROM ' . $this->blocks_table . ' WHERE user_id = ' . (int) $user_id;
|
||||
$result = $this->db->sql_query_limit($sql, 1);
|
||||
$display = $this->db->sql_fetchfield('aps_display');
|
||||
$this->db->sql_freeresult($result);
|
||||
|
||||
return $display ? (array) json_decode($display, true) : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch a rowset from the database for the provided user identifier and admin identifier.
|
||||
*
|
||||
* @param int $user_id The user identifier
|
||||
* @return array The json decoded database rowset
|
||||
* @access public
|
||||
*/
|
||||
public function rowset($user_id)
|
||||
{
|
||||
$rowset = [];
|
||||
|
||||
$sql = 'SELECT user_id, aps_display
|
||||
FROM ' . $this->blocks_table . '
|
||||
WHERE ' . $this->db->sql_in_set('user_id', [$this->admin_id, (int) $user_id]);
|
||||
$result = $this->db->sql_query($sql);
|
||||
while ($row = $this->db->sql_fetchrow($result))
|
||||
{
|
||||
if ($row['user_id'] != ANONYMOUS)
|
||||
{
|
||||
$rowset[(int) $row['user_id']] = (array) json_decode($row['aps_display'], true);
|
||||
}
|
||||
}
|
||||
$this->db->sql_freeresult($result);
|
||||
|
||||
return (array) $rowset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the user desired blocks in the database.
|
||||
*
|
||||
* @param int $user_id The user identifier
|
||||
* @param array $blocks The user desired blocks
|
||||
* @param bool $insert Whether to insert or update
|
||||
* @return bool|int Bool on update, integer on insert
|
||||
* @access public
|
||||
*/
|
||||
public function set_blocks($user_id, array $blocks, $insert)
|
||||
{
|
||||
if ($user_id == ANONYMOUS)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($insert)
|
||||
{
|
||||
return $this->insert($user_id, $blocks);
|
||||
}
|
||||
else
|
||||
{
|
||||
return $this->update($user_id, $blocks);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a user desired blocks into the database.
|
||||
*
|
||||
* @param int $user_id The user identifier
|
||||
* @param array $blocks The user desired blocks
|
||||
* @return int
|
||||
* @access public
|
||||
*/
|
||||
public function insert($user_id, array $blocks)
|
||||
{
|
||||
$sql = 'INSERT INTO ' . $this->blocks_table . ' ' . $this->db->sql_build_array('INSERT', [
|
||||
'user_id' => (int) $user_id,
|
||||
'aps_display' => json_encode($blocks),
|
||||
]);
|
||||
$this->db->sql_query($sql);
|
||||
|
||||
return (int) $this->db->sql_nextid();
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a user desired blocks in the database.
|
||||
*
|
||||
* @param int $user_id The user identifier
|
||||
* @param array $blocks The user desired blocks
|
||||
* @return bool
|
||||
* @access public
|
||||
*/
|
||||
public function update($user_id, array $blocks)
|
||||
{
|
||||
$sql = 'UPDATE ' . $this->blocks_table . ' SET ' . $this->db->sql_build_array('UPDATE', [
|
||||
'aps_display' => json_encode($blocks),
|
||||
]) . ' WHERE user_id = ' . (int) $user_id;
|
||||
$this->db->sql_query($sql);
|
||||
|
||||
return (bool) $this->db->sql_affectedrows();
|
||||
}
|
||||
}
|
||||
206
ext/phpbbstudio/aps/points/distributor.php
Normal file
206
ext/phpbbstudio/aps/points/distributor.php
Normal file
@@ -0,0 +1,206 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
* phpBB Studio - Advanced Points System. An extension for the phpBB Forum Software package.
|
||||
*
|
||||
* @copyright (c) 2019, phpBB Studio, https://www.phpbbstudio.com
|
||||
* @license GNU General Public License, version 2 (GPL-2.0)
|
||||
*
|
||||
*/
|
||||
|
||||
namespace phpbbstudio\aps\points;
|
||||
|
||||
/**
|
||||
* phpBB Studio - Advanced Points System distributor.
|
||||
*/
|
||||
class distributor
|
||||
{
|
||||
/** @var \phpbb\config\config */
|
||||
protected $config;
|
||||
|
||||
/** @var \phpbb\db\driver\driver_interface */
|
||||
protected $db;
|
||||
|
||||
/** @var \phpbb\event\dispatcher */
|
||||
protected $dispatcher;
|
||||
|
||||
/** @var \phpbbstudio\aps\core\functions */
|
||||
protected $functions;
|
||||
|
||||
/** @var \phpbbstudio\aps\core\log */
|
||||
protected $log;
|
||||
|
||||
/** @var \phpbb\user */
|
||||
protected $user;
|
||||
|
||||
/** @var \phpbbstudio\aps\points\valuator */
|
||||
protected $valuator;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param \phpbb\config\config $config Configuration object
|
||||
* @param \phpbb\db\driver\driver_interface $db Database object
|
||||
* @param \phpbb\event\dispatcher $dispatcher Event dispatcher object
|
||||
* @param \phpbbstudio\aps\core\functions $functions APS Core functions
|
||||
* @param \phpbbstudio\aps\core\log $log APS Log object
|
||||
* @param \phpbb\user $user User object
|
||||
* @param \phpbbstudio\aps\points\valuator $valuator APS Valuator object
|
||||
* @return void
|
||||
* @access public
|
||||
*/
|
||||
public function __construct(
|
||||
\phpbb\config\config $config,
|
||||
\phpbb\db\driver\driver_interface $db,
|
||||
\phpbb\event\dispatcher $dispatcher,
|
||||
\phpbbstudio\aps\core\functions $functions,
|
||||
\phpbbstudio\aps\core\log $log,
|
||||
\phpbb\user $user,
|
||||
valuator $valuator
|
||||
)
|
||||
{
|
||||
$this->config = $config;
|
||||
$this->db = $db;
|
||||
$this->dispatcher = $dispatcher;
|
||||
$this->functions = $functions;
|
||||
$this->log = $log;
|
||||
$this->user = $user;
|
||||
$this->valuator = $valuator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Distribute a user's points.
|
||||
*
|
||||
* @param int $user_id The user identifier
|
||||
* @param double $points The points gained
|
||||
* @param array $logs The logs array
|
||||
* @param null $user_points The current user's points, if available. Has to be null as 0 is a valid current points value.
|
||||
* @return bool Whether the user points were updated or not
|
||||
* @access public
|
||||
*/
|
||||
public function distribute($user_id, $points, $logs, $user_points = null)
|
||||
{
|
||||
// Calculate the new total for this user
|
||||
$total = $this->total($user_id, $points, $user_points);
|
||||
|
||||
// If logging was successful
|
||||
if ($this->log->add_multi($logs))
|
||||
{
|
||||
// Update the points for this user
|
||||
return $this->update_points($total, $user_id);
|
||||
}
|
||||
|
||||
// Points were not updated, return false (logs were invalid)
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a user's points.
|
||||
*
|
||||
* @param double $points The user points
|
||||
* @param int $user_id The user identifier
|
||||
* @return bool Whether or not the user's row was updated
|
||||
* @access public
|
||||
*/
|
||||
public function update_points($points, $user_id = 0)
|
||||
{
|
||||
$user_id = $user_id ? $user_id : $this->user->data['user_id'];
|
||||
|
||||
$sql = 'UPDATE ' . $this->functions->table('users') . '
|
||||
SET user_points = ' . (double) $points . '
|
||||
WHERE user_id = ' . (int) $user_id;
|
||||
$this->db->sql_query($sql);
|
||||
$success = (bool) $this->db->sql_affectedrows();
|
||||
|
||||
/**
|
||||
* Event to perform additional actions after APS Points have been distributed.
|
||||
*
|
||||
* @event phpbbstudio.aps.update_points
|
||||
* @var int user_id The user identifier
|
||||
* @var double points The user points
|
||||
* @var bool success Whether or not the points were updated
|
||||
* @since 1.0.3
|
||||
*/
|
||||
$vars = ['user_id', 'points', 'success'];
|
||||
extract($this->dispatcher->trigger_event('phpbbstudio.aps.update_points', compact($vars)));
|
||||
|
||||
return $success;
|
||||
}
|
||||
|
||||
/**
|
||||
* Approve log entries and distribute the on-hold points for a certain user.
|
||||
*
|
||||
* @param int $user_id The user identifier
|
||||
* @param array $post_ids The post identifiers
|
||||
* @param null $user_points The current user's points, if available. Has to be null as 0 is a valid current points value.
|
||||
* @return void
|
||||
* @access public
|
||||
*/
|
||||
public function approve($user_id, array $post_ids, $user_points = null)
|
||||
{
|
||||
// Get points gained from the log entries
|
||||
$points = $this->log->get_values($user_id, $post_ids, false);
|
||||
|
||||
// Equate the points gained to a single value
|
||||
$points = $this->functions->equate_array($points);
|
||||
|
||||
// Calculate the new total for this user
|
||||
$total = $this->total($user_id, $user_points, $points);
|
||||
|
||||
$this->db->sql_transaction('begin');
|
||||
|
||||
// Approve the log entries
|
||||
$this->log->approve($user_id, $post_ids);
|
||||
|
||||
// Update the points for this user
|
||||
$this->update_points($total, $user_id);
|
||||
|
||||
$this->db->sql_transaction('commit');
|
||||
}
|
||||
|
||||
/**
|
||||
* Disapprove log entries for a certain user.
|
||||
*
|
||||
* @param int $user_id The user identifier
|
||||
* @param array $post_ids The post identifiers
|
||||
* @return void
|
||||
* @access public
|
||||
*/
|
||||
public function disapprove($user_id, array $post_ids)
|
||||
{
|
||||
// Delete the log entries
|
||||
$this->log->delete([
|
||||
'log_approved' => (int) false,
|
||||
'user_id' => (int) $user_id,
|
||||
'post_id' => [
|
||||
'IN' => (array) $post_ids,
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the new total (current points + gained points) for a specific user.
|
||||
*
|
||||
* @param int $user_id The user identifier
|
||||
* @param double $points The user's gained points
|
||||
* @param double $user_points The user's current points
|
||||
* @return double The new total for this user
|
||||
* @access public
|
||||
*/
|
||||
public function total($user_id, $points, $user_points = null)
|
||||
{
|
||||
// If the current user's points is null, get it from the database
|
||||
if ($user_points === null)
|
||||
{
|
||||
$user_points = $this->valuator->user((int) $user_id);
|
||||
}
|
||||
|
||||
// Calculate the new total for this user
|
||||
$total = $this->functions->equate_points($user_points, $points);
|
||||
|
||||
// Check total boundaries (not higher than X, not lower than X)
|
||||
$total = $this->functions->boundaries($total);
|
||||
|
||||
return $total;
|
||||
}
|
||||
}
|
||||
193
ext/phpbbstudio/aps/points/reasoner.php
Normal file
193
ext/phpbbstudio/aps/points/reasoner.php
Normal file
@@ -0,0 +1,193 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
* phpBB Studio - Advanced Points System. An extension for the phpBB Forum Software package.
|
||||
*
|
||||
* @copyright (c) 2019, phpBB Studio, https://www.phpbbstudio.com
|
||||
* @license GNU General Public License, version 2 (GPL-2.0)
|
||||
*
|
||||
*/
|
||||
|
||||
namespace phpbbstudio\aps\points;
|
||||
|
||||
/**
|
||||
* phpBB Studio - Advanced Points System reasoner.
|
||||
*/
|
||||
class reasoner
|
||||
{
|
||||
/** @var \phpbb\db\driver\driver_interface */
|
||||
protected $db;
|
||||
|
||||
/** @var string APS Reasons table */
|
||||
protected $reasons_table;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param \phpbb\db\driver\driver_interface $db Database object
|
||||
* @param string $reasons_table APS Reasons table
|
||||
* @return void
|
||||
* @access public
|
||||
*/
|
||||
public function __construct(\phpbb\db\driver\driver_interface $db, $reasons_table)
|
||||
{
|
||||
$this->db = $db;
|
||||
$this->reasons_table = $reasons_table;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a reason in the database for the first time.
|
||||
*
|
||||
* @param array $reason The array to insert
|
||||
* @return int The new reason identifier
|
||||
* @access public
|
||||
*/
|
||||
public function insert(array $reason)
|
||||
{
|
||||
unset($reason['reason_id']);
|
||||
|
||||
$sql = 'SELECT MAX(reason_order) as reason_order FROM ' . $this->reasons_table;
|
||||
$result = $this->db->sql_query($sql);
|
||||
$order = $this->db->sql_fetchfield('reason_order');
|
||||
$this->db->sql_freeresult($result);
|
||||
|
||||
$reason['reason_order'] = ++$order;
|
||||
|
||||
$sql = 'INSERT INTO ' . $this->reasons_table . ' ' . $this->db->sql_build_array('INSERT', $reason);
|
||||
$this->db->sql_query($sql);
|
||||
|
||||
return (int) $this->db->sql_nextid();
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates an already existing reason in the database.
|
||||
*
|
||||
* @param array $reason The array to update
|
||||
* @param int $reason_id The reason identifier
|
||||
* @return bool Whether the reason's row in the database was updated or not
|
||||
* @access public
|
||||
*/
|
||||
public function update(array $reason, $reason_id)
|
||||
{
|
||||
unset($reason['reason_id']);
|
||||
|
||||
$sql = 'UPDATE ' . $this->reasons_table . ' SET ' . $this->db->sql_build_array('UPDATE', $reason) . ' WHERE reason_id = ' . (int) $reason_id;
|
||||
$this->db->sql_query($sql);
|
||||
|
||||
return (bool) $this->db->sql_affectedrows();
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a reason row from the database.
|
||||
*
|
||||
* @param int $reason_id The reason identifier
|
||||
* @return bool Whether or not the reason's row was deleted from the database.
|
||||
* @access public
|
||||
*/
|
||||
public function delete($reason_id)
|
||||
{
|
||||
$sql = 'DELETE FROM ' . $this->reasons_table . ' WHERE reason_id = ' . (int) $reason_id;
|
||||
$this->db->sql_query($sql);
|
||||
|
||||
return (bool) $this->db->sql_affectedrows();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a reason row from the database.
|
||||
*
|
||||
* @param int $reason_id The reason identifier
|
||||
* @return mixed The reason row or false if the row does not exists.
|
||||
* @access public
|
||||
*/
|
||||
public function row($reason_id)
|
||||
{
|
||||
if (empty($reason_id))
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
$sql = 'SELECT * FROM ' . $this->reasons_table . ' WHERE reason_id = ' . (int) $reason_id;
|
||||
$result = $this->db->sql_query_limit($sql, 1);
|
||||
$row = $this->db->sql_fetchrow($result);
|
||||
$this->db->sql_freeresult($result);
|
||||
|
||||
return (array) $row;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves all the reason rows from the database.
|
||||
*
|
||||
* @return array The reasons rowset
|
||||
* @access public
|
||||
*/
|
||||
public function rowset()
|
||||
{
|
||||
$rowset = [];
|
||||
|
||||
$sql = 'SELECT * FROM ' . $this->reasons_table . ' ORDER BY reason_order ASC';
|
||||
$result = $this->db->sql_query($sql);
|
||||
while ($row = $this->db->sql_fetchrow($result))
|
||||
{
|
||||
$rowset[$row['reason_id']] = $row;
|
||||
}
|
||||
$this->db->sql_freeresult($result);
|
||||
|
||||
return $rowset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fills a reason row to make sure all values are set.
|
||||
*
|
||||
* @param array $reason The reason array to fill
|
||||
* @return array The filled reason array
|
||||
* @access public
|
||||
*/
|
||||
public function fill(array $reason)
|
||||
{
|
||||
$reason = !empty($reason) ? $reason : [];
|
||||
|
||||
$fields = [
|
||||
'title' => '',
|
||||
'desc' => '',
|
||||
'points' => 0.00,
|
||||
'order' => 0,
|
||||
];
|
||||
|
||||
foreach ($fields as $field => $default)
|
||||
{
|
||||
$reason['reason_' . $field] = !empty($reason['reason_' . $field]) ? $reason['reason_' . $field] : $default;
|
||||
}
|
||||
|
||||
return $reason;
|
||||
}
|
||||
|
||||
/**
|
||||
* Re-orders a reason row.
|
||||
*
|
||||
* @param int $reason_id The reason identifier
|
||||
* @param string $direction The direction to move it (up|down).
|
||||
* @return void
|
||||
* @access public
|
||||
*/
|
||||
public function order($reason_id, $direction)
|
||||
{
|
||||
// Select the current order
|
||||
$sql = 'SELECT reason_order FROM ' . $this->reasons_table . ' WHERE reason_id = ' . (int) $reason_id;
|
||||
$result = $this->db->sql_query_limit($sql, 1);
|
||||
$order = (int) $this->db->sql_fetchfield('reason_order');
|
||||
$this->db->sql_freeresult($result);
|
||||
|
||||
// Set the new (other) order
|
||||
$other_order = $direction === 'up' ? $order - 1 : $order + 1;
|
||||
|
||||
// Select the other reason identifier (with which it is being swapped)
|
||||
$sql = 'SELECT reason_id FROM ' . $this->reasons_table . ' WHERE reason_order = ' . (int) $other_order;
|
||||
$result = $this->db->sql_query_limit($sql, 1);
|
||||
$other_id = (int) $this->db->sql_fetchfield('reason_id');
|
||||
$this->db->sql_freeresult($result);
|
||||
|
||||
// Update both the reason rows
|
||||
$this->update(['reason_order' => $other_order], $reason_id);
|
||||
$this->update(['reason_order' => $order], $other_id);
|
||||
}
|
||||
}
|
||||
378
ext/phpbbstudio/aps/points/valuator.php
Normal file
378
ext/phpbbstudio/aps/points/valuator.php
Normal file
@@ -0,0 +1,378 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
* phpBB Studio - Advanced Points System. An extension for the phpBB Forum Software package.
|
||||
*
|
||||
* @copyright (c) 2019, phpBB Studio, https://www.phpbbstudio.com
|
||||
* @license GNU General Public License, version 2 (GPL-2.0)
|
||||
*
|
||||
*/
|
||||
|
||||
namespace phpbbstudio\aps\points;
|
||||
|
||||
/**
|
||||
* phpBB Studio - Advanced Points System valuator.
|
||||
*/
|
||||
class valuator
|
||||
{
|
||||
/** @var \phpbb\db\driver\driver_interface */
|
||||
protected $db;
|
||||
|
||||
/** @var \phpbbstudio\aps\core\functions */
|
||||
protected $functions;
|
||||
|
||||
/** @var \phpbb\user */
|
||||
protected $user;
|
||||
|
||||
/** @var string APS Values table */
|
||||
protected $values_table;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param \phpbb\db\driver\driver_interface $db Database object
|
||||
* @param \phpbbstudio\aps\core\functions $functions APS Core functions
|
||||
* @param \phpbb\user $user User object
|
||||
* @param string $values_table APS Values table
|
||||
* @return void
|
||||
* @access public
|
||||
*/
|
||||
public function __construct(
|
||||
\phpbb\db\driver\driver_interface $db,
|
||||
\phpbbstudio\aps\core\functions $functions,
|
||||
\phpbb\user $user,
|
||||
$values_table
|
||||
)
|
||||
{
|
||||
$this->db = $db;
|
||||
$this->functions = $functions;
|
||||
$this->user = $user;
|
||||
|
||||
$this->values_table = $values_table;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the points for the provided user identifier.
|
||||
*
|
||||
* @param int $user_id The user identifier
|
||||
* @return double The current user's points
|
||||
* @access public
|
||||
*/
|
||||
public function user($user_id)
|
||||
{
|
||||
if ($user_id == $this->user->data['user_id'])
|
||||
{
|
||||
return (double) $this->user->data['user_points'];
|
||||
}
|
||||
|
||||
$sql = 'SELECT user_points
|
||||
FROM ' . $this->functions->table('users') . '
|
||||
WHERE user_id = ' . (int) $user_id . '
|
||||
AND user_type <> ' . USER_IGNORE;
|
||||
$result = $this->db->sql_query_limit($sql, 1);
|
||||
$user_points = $this->db->sql_fetchfield('user_points');
|
||||
$this->db->sql_freeresult($result);
|
||||
|
||||
return (double) $user_points;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the points for the provided user identifiers.
|
||||
*
|
||||
* @param array|int $user_ids The user identifier(s)
|
||||
* @return array|float Array with the users' point values or Double if an integer was provided
|
||||
* @access public
|
||||
*/
|
||||
public function users($user_ids)
|
||||
{
|
||||
// If it's just a single user
|
||||
if (!is_array($user_ids))
|
||||
{
|
||||
return $this->user($user_ids);
|
||||
}
|
||||
|
||||
// Make sure the array is full with integers
|
||||
$user_ids = array_map('intval', $user_ids);
|
||||
|
||||
// Set up base array
|
||||
$user_points = [];
|
||||
|
||||
$sql = 'SELECT user_id, user_points
|
||||
FROM ' . $this->functions->table('users') . '
|
||||
WHERE ' . $this->db->sql_in_set('user_id', $user_ids) . '
|
||||
AND user_type <> ' . USER_IGNORE;
|
||||
$result = $this->db->sql_query($sql);
|
||||
while ($row = $this->db->sql_fetchrow($result))
|
||||
{
|
||||
$user_points[(int) $row['user_id']] = (double) $row['user_points'];
|
||||
}
|
||||
$this->db->sql_freeresult($result);
|
||||
|
||||
return $user_points;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve point values from the database.
|
||||
*
|
||||
* @param array $fields Array of action type fields
|
||||
* @param array|int $forum_ids The forum identifier(s)
|
||||
* @param bool $fill Whether the point values should be filled
|
||||
* @return array
|
||||
* @access public
|
||||
*/
|
||||
public function get_points(array $fields, $forum_ids, $fill = true)
|
||||
{
|
||||
// Set up base arrays
|
||||
$sql_where = $values = [];
|
||||
|
||||
// Iterate over the fields
|
||||
foreach ($fields as $scope => $fields_array)
|
||||
{
|
||||
// If the fields array is not empty, add it to the SQL WHERE clause
|
||||
if (!empty($fields_array))
|
||||
{
|
||||
$sql_where[] = $this->get_sql_where($scope, $fields_array, $forum_ids);
|
||||
}
|
||||
}
|
||||
|
||||
$sql = 'SELECT *
|
||||
FROM ' . $this->values_table . '
|
||||
WHERE ' . implode(' OR ', $sql_where);
|
||||
$result = $this->db->sql_query($sql);
|
||||
while ($row = $this->db->sql_fetchrow($result))
|
||||
{
|
||||
$f = (int) $row['forum_id'];
|
||||
$n = (string) $row['points_name'];
|
||||
$v = (double) $row['points_value'];
|
||||
|
||||
$values[$f][$n] = $v;
|
||||
}
|
||||
$this->db->sql_freeresult($result);
|
||||
|
||||
// Make sure all values are set
|
||||
if ($fill)
|
||||
{
|
||||
$this->fill_values($values, $fields, $forum_ids);
|
||||
}
|
||||
|
||||
return $values;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes all points from the database that do not belong to any of the action types.
|
||||
*
|
||||
* @param array $fields Array of action types fields
|
||||
* @param int $forum_id The forum identifier
|
||||
* @return void
|
||||
* @access public
|
||||
*/
|
||||
public function clean_points($fields, $forum_id)
|
||||
{
|
||||
$sql = 'DELETE FROM ' . $this->values_table . '
|
||||
WHERE forum_id = ' . (int) $forum_id . '
|
||||
AND ' . $this->db->sql_in_set('points_name', $fields, true, true);
|
||||
$this->db->sql_query($sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes all points from the database that do not belong to any of the action types.
|
||||
*
|
||||
* @param array $fields Array of action types fields
|
||||
* @return void
|
||||
* @access public
|
||||
*/
|
||||
public function clean_all_points($fields)
|
||||
{
|
||||
// Set up base arrays
|
||||
$sql_where = $forum_ids = [];
|
||||
|
||||
$sql = 'SELECT forum_id FROM ' . $this->functions->table('forums');
|
||||
$result = $this->db->sql_query($sql);
|
||||
while ($row = $this->db->sql_fetchrow($result))
|
||||
{
|
||||
$forum_ids[] = (int) $row['forum_id'];
|
||||
}
|
||||
$this->db->sql_freeresult($result);
|
||||
|
||||
// Iterate over the fields
|
||||
foreach ($fields as $scope => $fields_array)
|
||||
{
|
||||
// If the fields array is not empty, add it to the SQL WHERE clause
|
||||
if (!empty($fields_array))
|
||||
{
|
||||
$sql_where[] = $this->get_sql_where($scope, $fields_array, $forum_ids, true);
|
||||
}
|
||||
}
|
||||
|
||||
$sql = 'DELETE FROM ' . $this->values_table . '
|
||||
WHERE points_value = 0
|
||||
OR ' . $this->db->sql_in_set('forum_id', ($forum_ids + [0]), true) . '
|
||||
OR ' . implode(' OR ', $sql_where);
|
||||
$this->db->sql_query($sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the point values in the database for a specific forum.
|
||||
*
|
||||
* @param int $forum_id The forum identifier
|
||||
* @return bool Whether the values were deleted or not.
|
||||
* @access public
|
||||
*/
|
||||
public function delete_points($forum_id)
|
||||
{
|
||||
$sql = 'DELETE FROM ' . $this->values_table . '
|
||||
WHERE forum_id = ' . (int) $forum_id;
|
||||
$this->db->sql_query($sql);
|
||||
|
||||
return (bool) $this->db->sql_affectedrows();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the point values in the database.
|
||||
*
|
||||
* @param array $points Array of point values
|
||||
* @param int $forum_id The forum identifier
|
||||
* @return void
|
||||
* @access public
|
||||
*/
|
||||
public function set_points($points, $forum_id)
|
||||
{
|
||||
$existing = [(int) empty($forum_id) => array_keys($points)];
|
||||
$existing = $this->get_points($existing, $forum_id, false);
|
||||
$existing = $existing ? array_keys($existing[(int) $forum_id]) : [];
|
||||
|
||||
$this->db->sql_transaction('begin');
|
||||
|
||||
foreach ($points as $name => $value)
|
||||
{
|
||||
// If the value already exists in the database, update it
|
||||
if (in_array($name, $existing))
|
||||
{
|
||||
$sql = 'UPDATE ' . $this->values_table . '
|
||||
SET points_value = ' . (double) $value . '
|
||||
WHERE points_name = "' . $this->db->sql_escape($name) . '"
|
||||
AND forum_id = ' . (int) $forum_id;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Otherwise insert it for the first time
|
||||
$row['forum_id'] = (int) $forum_id;
|
||||
|
||||
$sql = 'INSERT INTO ' . $this->values_table . ' ' . $this->db->sql_build_array('INSERT', [
|
||||
'points_name' => (string) $name,
|
||||
'points_value' => (double) $value,
|
||||
'forum_id' => (int) $forum_id,
|
||||
]);
|
||||
}
|
||||
|
||||
$this->db->sql_query($sql);
|
||||
}
|
||||
|
||||
$this->db->sql_transaction('commit');
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy the point values from one forum to an other.
|
||||
*
|
||||
* @param int $from The "from" forum identifier
|
||||
* @param array|int $to The "to" forum identifier(s)
|
||||
* @return void
|
||||
* @access public
|
||||
*/
|
||||
public function copy_points($from, $to)
|
||||
{
|
||||
$this->db->sql_transaction('begin');
|
||||
|
||||
// Select "from" points
|
||||
$sql = 'SELECT points_name, points_value
|
||||
FROM ' . $this->values_table . '
|
||||
WHERE forum_id = ' . (int) $from;
|
||||
$result = $this->db->sql_query($sql);
|
||||
$rowset = $this->db->sql_fetchrowset($result);
|
||||
$this->db->sql_freeresult($result);
|
||||
|
||||
$to = array_map('intval', array_unique(array_filter($to)));
|
||||
|
||||
// Delete "to" points
|
||||
$sql = 'DELETE FROM ' . $this->values_table . '
|
||||
WHERE ' . $this->db->sql_in_set('forum_id', $to);
|
||||
$this->db->sql_query($sql);
|
||||
|
||||
foreach ($to as $forum_id)
|
||||
{
|
||||
for ($i = 0; $i < count($rowset); $i++)
|
||||
{
|
||||
$rowset[$i]['forum_id'] = (int) $forum_id;
|
||||
}
|
||||
|
||||
$this->db->sql_multi_insert($this->values_table, $rowset);
|
||||
}
|
||||
|
||||
$this->db->sql_transaction('commit');
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a SQL WHERE clause based on the scope (local|global) and the provided fields.
|
||||
*
|
||||
* @param int $scope The scope for the fields (local|global)
|
||||
* @param array $fields Array of action types fields
|
||||
* @param array|int $forum_ids The forum identifier(s)
|
||||
* @param bool $negate Whether it should be a SQL IN or SQL NOT IN clause
|
||||
* @return string The SQL WHERE clause
|
||||
* @access protected
|
||||
*/
|
||||
protected function get_sql_where($scope, array $fields, $forum_ids, $negate = false)
|
||||
{
|
||||
$sql_where = '(';
|
||||
$sql_where .= $this->db->sql_in_set('points_name', $fields, $negate);
|
||||
$sql_where .= ' AND ';
|
||||
|
||||
switch ($scope)
|
||||
{
|
||||
// Local
|
||||
case 0:
|
||||
$sql_where .= is_array($forum_ids) ? $this->db->sql_in_set('forum_id', array_map('intval', $forum_ids), $negate) : 'forum_id ' . ($negate ? '!= ' : '= ') . (int) $forum_ids;
|
||||
break;
|
||||
|
||||
// Global
|
||||
case 1:
|
||||
$sql_where .= 'forum_id = 0';
|
||||
break;
|
||||
}
|
||||
|
||||
$sql_where .= ')';
|
||||
|
||||
return $sql_where;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fills the values array, meaning that if a point value is not available in the database
|
||||
* the key is still set with a default value of 0.00
|
||||
*
|
||||
* @param array $values Array of point values
|
||||
* @param array $fields Array of action types fields
|
||||
* @param array|int $forum_ids The forum identifier(s)
|
||||
* @return void
|
||||
* @access protected
|
||||
*/
|
||||
protected function fill_values(array &$values, $fields, $forum_ids)
|
||||
{
|
||||
// Make sure all forum ids are set
|
||||
$fill = is_array($forum_ids) ? array_map('intval', $forum_ids) : [(int) $forum_ids];
|
||||
$fill = array_fill_keys($fill, []);
|
||||
$values = $values + $fill;
|
||||
|
||||
// Iterate over the set values
|
||||
foreach ($values as $forum_id => $values_array)
|
||||
{
|
||||
// The scope: 0 - local | 1 - global
|
||||
$scope = (int) empty($forum_id);
|
||||
|
||||
// Set up an array with all fields as array and a default value
|
||||
$requested = array_fill_keys($fields[$scope], 0.00);
|
||||
|
||||
// Merge the set values with the requested values, where the set values take precedence
|
||||
$values[$forum_id] = array_merge($requested, $values_array);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user