[ticket/9549] Enhance teampage functionality with a new class, group_positions.

PHPBB3-9549
This commit is contained in:
Joas Schilling 2011-02-09 21:02:18 +01:00
parent 19a0c16ed6
commit b7d3d57b70
3 changed files with 450 additions and 0 deletions

View file

@ -0,0 +1,220 @@
<?php
/**
*
* @package phpBB3
* @version $Id$
* @copyright (c) 2005 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
exit;
}
/**
* Group Position class, containing all functions to manage the groups in the teampage and legend.
*
* group_teampage/group_legend is an ascending list 1, 2, ..., n for groups which are displayed. 1 is the first group, n the last.
* If the value is 0 (self::GROUP_DISABLED) the group is not displayed.
* @package phpBB3
*/
class phpbb_group_positions
{
/**
* Group is not displayed
*/
const GROUP_DISABLED = 0;
/**
* Returns the group_{$field} for a given group, if the group exists.
* @param string $field name of the field to be selected
* @param int $group_id group_id of the group to be selected
* @return int position of the group
*/
static function get_group_value($field, $group_id)
{
global $db;
$sql = 'SELECT group_' . $field . '
FROM ' . GROUPS_TABLE . '
WHERE group_id = ' . (int) $group_id;
$result = $db->sql_query($sql);
$current_value = $db->sql_fetchfield('group_' . $field);
$db->sql_freeresult($result);
if ($current_value === false)
{
// Group not found.
global $user;
trigger_error($user->lang['NO_GROUP'] . adm_back_link($this->u_action), E_USER_WARNING);
}
return (int) $current_value;
}
/**
* Get number of groups, displayed on the teampage/legend
* @param string $field name of the field to be counted
* @return int value of the last group displayed
*/
static function get_group_count($field)
{
global $db;
$sql = 'SELECT group_' . $field . '
FROM ' . GROUPS_TABLE . '
ORDER BY group_' . $field . ' DESC';
$result = $db->sql_query_limit($sql, 1);
$group_count = (int) $db->sql_fetchfield('group_' . $field);
$db->sql_freeresult($result);
return $group_count;
}
/**
* Addes a group by group_id
* @param string $field name of the field the group is added to
* @param int $group_id group_id of the group to be added
* @return void
*/
static function add_group($field, $group_id)
{
$current_value = self::get_group_value($field, $group_id);
if ($current_value == self::GROUP_DISABLED)
{
global $db;
// Group is currently not displayed, add it at the end.
$next_value = 1 + self::get_group_count($field, $field);
$sql = 'UPDATE ' . GROUPS_TABLE . '
SET group_' . $field . ' = ' . $next_value . '
WHERE group_' . $field . ' = ' . self::GROUP_DISABLED . '
AND group_id = ' . (int) $group_id;
$db->sql_query($sql);
}
}
/**
* Deletes a group by group_id
* @param string $field name of the field the group is deleted from
* @param int $group_id group_id of the group to be deleted
* @return void
*/
static function delete_group($field, $group_id)
{
$current_value = self::get_group_value($field, $group_id);
if ($current_value != self::GROUP_DISABLED)
{
global $db;
$db->sql_transaction('begin');
$sql = 'UPDATE ' . GROUPS_TABLE . '
SET group_' . $field . ' = group_' . $field . ' - 1
WHERE group_' . $field . ' > ' . $current_value;
$db->sql_query($sql);
$sql = 'UPDATE ' . GROUPS_TABLE . '
SET group_' . $field . ' = ' . self::GROUP_DISABLED . '
WHERE group_id = ' . (int) $group_id;
$db->sql_query($sql);
$db->sql_transaction('commit');
}
}
/**
* Moves a group up by group_id
* @param string $field name of the field the group is moved by
* @param int $group_id group_id of the group to be moved
* @return void
*/
static function move_up($field, $group_id)
{
$current_value = self::get_group_value($field, $group_id);
// Only move the group, if it is in the list and not already on top.
if ($current_value > 1)
{
global $db;
$db->sql_transaction('begin');
$sql = 'UPDATE ' . GROUPS_TABLE . '
SET group_' . $field . ' = group_' . $field . ' + 1
WHERE group_' . $field . ' = ' . ($current_value - 1);
$db->sql_query($sql);
$sql = 'UPDATE ' . GROUPS_TABLE . '
SET group_' . $field . ' = ' . ($current_value - 1) . '
WHERE group_id = ' . (int) $group_id;
$db->sql_query($sql);
$db->sql_transaction('commit');
}
}
/**
* Moves a group down by group_id
* @param string $field name of the field the group is moved by
* @param int $group_id group_id of the group to be moved
* @return void
*/
static function move_down($field, $group_id)
{
$current_value = self::get_group_value($field, $group_id);
if ($current_value != self::GROUP_DISABLED)
{
global $db;
$db->sql_transaction('begin');
$sql = 'UPDATE ' . GROUPS_TABLE . '
SET group_' . $field . ' = group_' . $field . ' - 1
WHERE group_' . $field . ' = ' . ($current_value + 1);
$db->sql_query($sql);
if ($db->sql_affectedrows() == 1)
{
// Only update when we move another one up, otherwise it was the last.
$sql = 'UPDATE ' . GROUPS_TABLE . '
SET group_' . $field . ' = ' . ($current_value + 1) . '
WHERE group_id = ' . (int) $group_id;
$db->sql_query($sql);
}
$db->sql_transaction('commit');
}
}
/**
* Get group type language var
* @param int $group_type group_type from the groups-table
* @return string name of the language variable for the given group-type.
*/
static function group_type_language($group_type)
{
switch ($group_type)
{
case GROUP_OPEN:
return 'GROUP_REQUEST';
case GROUP_CLOSED:
return 'GROUP_CLOSED';
case GROUP_HIDDEN:
return 'GROUP_HIDDEN';
case GROUP_SPECIAL:
return 'GROUP_SPECIAL';
case GROUP_FREE:
return 'GROUP_OPEN';
}
}
}

View file

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8" ?>
<dataset>
<table name="phpbb_groups">
<column>group_id</column>
<column>group_teampage</column>
<column>group_legend</column>
<row>
<value>1</value>
<value>0</value>
<value>0</value>
</row>
<row>
<value>2</value>
<value>1</value>
<value>0</value>
</row>
<row>
<value>3</value>
<value>2</value>
<value>1</value>
</row>
</table>
</dataset>

View file

@ -0,0 +1,207 @@
<?php
/**
*
* @package testing
* @copyright (c) 2008 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
class phpbb_group_positions_test extends phpbb_database_test_case
{
public function getDataSet()
{
return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/group_positions.xml');
}
public static function get_group_value_data()
{
return array(
array('teampage', 1, 0),
array('teampage', 2, 1),
array('legend', 1, 0),
array('legend', 3, 1),
);
}
/**
* @dataProvider get_group_value_data
*/
public function test_get_group_value($field, $group_id, $expected)
{
global $db;
$db = $this->new_dbal();
$this->assertEquals($expected, phpbb_group_positions::get_group_value($field, $group_id));
}
public static function get_group_count_data()
{
return array(
array('teampage', 2),
array('legend', 1),
);
}
/**
* @dataProvider get_group_count_data
*/
public function test_get_group_count($field, $expected)
{
global $db;
$db = $this->new_dbal();
$this->assertEquals($expected, phpbb_group_positions::get_group_count($field));
}
public static function add_group_data()
{
return array(
array('teampage', 1, array(
array('group_id' => 1, 'group_teampage' => 3, 'group_legend' => 0),
array('group_id' => 2, 'group_teampage' => 1, 'group_legend' => 0),
array('group_id' => 3, 'group_teampage' => 2, 'group_legend' => 1),
)),
array('teampage', 2, array(
array('group_id' => 1, 'group_teampage' => 0, 'group_legend' => 0),
array('group_id' => 2, 'group_teampage' => 1, 'group_legend' => 0),
array('group_id' => 3, 'group_teampage' => 2, 'group_legend' => 1),
)),
);
}
/**
* @dataProvider add_group_data
*/
public function test_add_group($field, $group_id, $expected)
{
global $db;
$db = $this->new_dbal();
phpbb_group_positions::add_group($field, $group_id);
$result = $db->sql_query('SELECT group_id, group_teampage, group_legend
FROM ' . GROUPS_TABLE . '
ORDER BY group_id ASC');
$this->assertEquals($expected, $db->sql_fetchrowset($result));
}
public static function delete_group_data()
{
return array(
array('teampage', 1, array(
array('group_id' => 1, 'group_teampage' => 0, 'group_legend' => 0),
array('group_id' => 2, 'group_teampage' => 1, 'group_legend' => 0),
array('group_id' => 3, 'group_teampage' => 2, 'group_legend' => 1),
)),
array('teampage', 2, array(
array('group_id' => 1, 'group_teampage' => 0, 'group_legend' => 0),
array('group_id' => 2, 'group_teampage' => 0, 'group_legend' => 0),
array('group_id' => 3, 'group_teampage' => 1, 'group_legend' => 1),
)),
array('teampage', 3, array(
array('group_id' => 1, 'group_teampage' => 0, 'group_legend' => 0),
array('group_id' => 2, 'group_teampage' => 1, 'group_legend' => 0),
array('group_id' => 3, 'group_teampage' => 0, 'group_legend' => 1),
)),
);
}
/**
* @dataProvider delete_group_data
*/
public function test_delete_group($field, $group_id, $expected)
{
global $db;
$db = $this->new_dbal();
phpbb_group_positions::delete_group($field, $group_id);
$result = $db->sql_query('SELECT group_id, group_teampage, group_legend
FROM ' . GROUPS_TABLE . '
ORDER BY group_id ASC');
$this->assertEquals($expected, $db->sql_fetchrowset($result));
}
public static function move_up_data()
{
return array(
array('teampage', 1, array(
array('group_id' => 1, 'group_teampage' => 0, 'group_legend' => 0),
array('group_id' => 2, 'group_teampage' => 1, 'group_legend' => 0),
array('group_id' => 3, 'group_teampage' => 2, 'group_legend' => 1),
)),
array('teampage', 2, array(
array('group_id' => 1, 'group_teampage' => 0, 'group_legend' => 0),
array('group_id' => 2, 'group_teampage' => 1, 'group_legend' => 0),
array('group_id' => 3, 'group_teampage' => 2, 'group_legend' => 1),
)),
array('teampage', 3, array(
array('group_id' => 1, 'group_teampage' => 0, 'group_legend' => 0),
array('group_id' => 2, 'group_teampage' => 2, 'group_legend' => 0),
array('group_id' => 3, 'group_teampage' => 1, 'group_legend' => 1),
)),
);
}
/**
* @dataProvider move_up_data
*/
public function test_move_up($field, $group_id, $expected)
{
global $db;
$db = $this->new_dbal();
phpbb_group_positions::move_up($field, $group_id);
$result = $db->sql_query('SELECT group_id, group_teampage, group_legend
FROM ' . GROUPS_TABLE . '
ORDER BY group_id ASC');
$this->assertEquals($expected, $db->sql_fetchrowset($result));
}
public static function move_down_data()
{
return array(
array('teampage', 1, array(
array('group_id' => 1, 'group_teampage' => 0, 'group_legend' => 0),
array('group_id' => 2, 'group_teampage' => 1, 'group_legend' => 0),
array('group_id' => 3, 'group_teampage' => 2, 'group_legend' => 1),
)),
array('teampage', 2, array(
array('group_id' => 1, 'group_teampage' => 0, 'group_legend' => 0),
array('group_id' => 2, 'group_teampage' => 2, 'group_legend' => 0),
array('group_id' => 3, 'group_teampage' => 1, 'group_legend' => 1),
)),
array('teampage', 3, array(
array('group_id' => 1, 'group_teampage' => 0, 'group_legend' => 0),
array('group_id' => 2, 'group_teampage' => 1, 'group_legend' => 0),
array('group_id' => 3, 'group_teampage' => 2, 'group_legend' => 1),
)),
);
}
/**
* @dataProvider move_down_data
*/
public function test_move_down($field, $group_id, $expected)
{
global $db;
$db = $this->new_dbal();
phpbb_group_positions::move_down($field, $group_id);
$result = $db->sql_query('SELECT group_id, group_teampage, group_legend
FROM ' . GROUPS_TABLE . '
ORDER BY group_id ASC');
$this->assertEquals($expected, $db->sql_fetchrowset($result));
}
}