Commit db0e9b48 authored by Dmitrii Metelkin's avatar Dmitrii Metelkin
Browse files

Bug #826649: add group shortnames to manually created groups

behatnotneeded

Change-Id: Ic5d4fbb3faa16afb893e45c5303449122c7cd368
parent 4a4ba694
......@@ -761,6 +761,18 @@ class User {
return $this->get('admin') || $this->is_institutional_admin($institution);
}
public function can_edit_group_shortname(stdClass $group) {
if (!isset($group->id) || empty($group->id)) {
return false;
}
if ($this->get('admin')) {
return true;
}
return false;
}
public function institution_allows_public_views($institution = null) {
$user_institutions = $this->get('institutions');
if (empty($user_institutions)) {
......
......@@ -83,6 +83,7 @@ $form = array(
'rules' => array( 'required' => true, 'maxlength' => 128 ),
'defaultvalue' => $group_data->name,
),
'shortname' => group_get_shortname_element($group_data),
'description' => array(
'type' => 'wysiwyg',
'title' => get_string('groupdescription', 'group'),
......@@ -421,6 +422,21 @@ function editgroup_validate(Pieform $form, $values) {
}
}
if (isset($values['shortname']) && $group_data->id) {
if (!preg_match('/^[a-zA-Z0-9_.-]{2,255}$/', $values['shortname'])) {
$form->set_error('shortname', get_string('shortnameformat', 'group'));
}
if ($group_data->shortname != $values['shortname']) {
// This check has not always been case-insensitive; don't use get_record in case we get >1 row back.
if ($ids = get_records_sql_array('SELECT id FROM {group} WHERE LOWER(TRIM(shortname)) = ?', array(strtolower(trim($values['shortname']))))) {
if (count($ids) > 1 || $ids[0]->id != $group_data->id) {
$form->set_error('shortname', get_string('groupshortnamealreadyexists', 'group'));
}
}
}
}
if (isset($values['urlid']) && get_config('cleanurls')) {
$urlidlength = strlen($values['urlid']);
if ($group_data->urlid != $values['urlid']) {
......@@ -494,6 +510,11 @@ function editgroup_submit(Pieform $form, $values) {
'feedbacknotify' => intval($values['feedbacknotify']),
);
// Only admins can only update shortname.
if (isset($values['shortname']) && $USER->can_edit_group_shortname($group_data)) {
$newvalues['shortname'] = $values['shortname'];
}
if (
get_config('cleanurls')
&& isset($values['urlid'])
......
......@@ -1155,6 +1155,7 @@ $string['generalsettingslegend'] = 'General settings';
$string['loggingsettingslegend'] = 'Logging settings';
$string['groupname'] = 'Group name';
$string['groupshortname'] = 'Short name';
$string['groupmembers'] = 'Members';
$string['groupadmins'] = 'Administrators';
$string['grouptype'] = 'Group type';
......
......@@ -13,6 +13,7 @@ defined('INTERNAL') || die();
// my groups
$string['groupname'] = 'Group name';
$string['groupshortname'] = 'Short name';
$string['creategroup'] = 'Create group';
$string['groupmemberrequests'] = 'Pending membership requests';
$string['membershiprequests'] = 'Membership requests';
......@@ -57,6 +58,7 @@ $string['reason'] = 'Reason';
$string['approve'] = 'Approve';
$string['reject'] = 'Reject';
$string['groupalreadyexists'] = 'A group by this name already exists.';
$string['groupshortnamealreadyexists'] = 'A group by this short name already exists.';
$string['invalidshortname'] = 'Invalid group short name.';
$string['shortnameformat'] = 'Group short names can be from 2 to 255 characters in length and contain only alphanumeric characters, ".", "-", and "_".';
$string['Created'] = 'Created';
......
......@@ -4483,5 +4483,22 @@ function xmldb_core_upgrade($oldversion=0) {
insert_record('event_type', $e);
}
if ($oldversion < 2016062200) {
include_once(get_config('docroot') . 'lib/group.php');
log_debug('Assign a unique shortname for each existing group that doesn\'t have one.');
$groups = get_records_select_array(
'group',
"(shortname IS NULL OR shortname = '') AND deleted = 0"
);
if (!empty($groups)) {
foreach ($groups as $group) {
$group->shortname = group_generate_shortname($group->name);
update_record('group', $group, 'id');
}
}
}
return $status;
}
......@@ -386,7 +386,7 @@ function group_create($data) {
if (!empty($data['institution'])) {
throw new SystemException("group_create: group institution only available for api-controlled groups");
}
$data['shortname'] = null;
$data['shortname'] = group_generate_shortname($data['name']);
}
if (get_config('cleanurls') && (!isset($data['urlid']) || strlen($data['urlid']) == 0)) {
......@@ -596,9 +596,8 @@ function group_update($new, $create=false) {
$new->allowarchives = 0;
}
// Institution and shortname cannot be updated (yet)
// Institution cannot be updated (yet)
unset($new->institution);
unset($new->shortname);
foreach (array('id', 'grouptype', 'public', 'request', 'submittableto', 'allowarchives', 'editroles',
'hidden', 'hidemembers', 'hidemembersfrommembers', 'groupparticipationreports') as $f) {
......@@ -2560,3 +2559,74 @@ function group_sendnow($groupid) {
}
return !empty($sendnow);
}
/**
* Generate a valid shortname for the group.shortname column, based on the specified display name
*
* @param string $groupname
* @return string
*/
function group_generate_shortname($groupname) {
// iconv can crash on strings that are too long, so truncate before converting
$basename = mb_substr($groupname, 0, 255);
$basename = iconv('UTF-8', 'ASCII//TRANSLIT', $groupname);
$basename = strtolower($basename);
$basename = preg_replace('/[^a-z0-9_.-]/', '', $basename);
if (strlen($basename) < 2) {
$basename = 'group' . $basename;
}
else {
$basename = substr($basename, 0, 255);
}
// Make sure the name is unique. If it is not, add a suffix and see if
// that makes it unique
$finalname = $basename;
$suffix = 'a';
while (record_exists('group', 'shortname', $finalname)) {
// Add the suffix but make sure the name length doesn't go over 255
$finalname = substr($basename, 0, 255 - strlen($suffix)) . $suffix;
// Will iterate a-z, aa-az, ba-bz, etc.
// See: http://php.net/manual/en/language.operators.increment.php
$suffix++;
}
return $finalname;
}
/**
* Return an element for the shortname field of the group form.
*
* @param object $group_data Group data object.
*
* @return array
*/
function group_get_shortname_element($group_data) {
global $USER;
$required = $USER->can_edit_group_shortname($group_data);
$title = get_string('groupshortname', 'group');
$disabled = !$USER->can_edit_group_shortname($group_data);
$element = array(
'title' => $title,
'rules' => array(
'required' => $required,
'maxlength' => 255
),
'disabled' => $disabled,
'description' => get_string('shortnameformat', 'group'),
);
if (isset($group_data->id)) {
$element['type'] = 'text';
$element['defaultvalue'] = isset($group_data->shortname) ? $group_data->shortname : null;
}
else{
$element['type'] = 'hidden';
$element['value'] = isset($group_data->shortname) ? $group_data->shortname : null;
}
return $element;
}
......@@ -16,7 +16,7 @@ $config = new stdClass();
// See https://wiki.mahara.org/wiki/Developer_Area/Version_Numbering_Policy
// For upgrades on stable branches, increment the version by one. On master, use the date.
$config->version = 2016061700;
$config->version = 2016062200;
$config->series = '16.10';
$config->release = '16.10dev';
$config->minupgradefrom = 2012080604;
......
......@@ -820,8 +820,9 @@ class PluginSearchInternal extends PluginSearch {
WHERE (
name " . db_ilike() . " '%' || ? || '%'
OR description " . db_ilike() . " '%' || ? || '%'
OR shortname " . db_ilike() . " '%' || ? || '%'
) AND deleted = 0 ";
$values = array($query_string, $query_string);
$values = array($query_string, $query_string, $query_string);
if (!$grouproles = join(',', array_keys($USER->get('grouproles')))) {
$grouproles = '-1';
......@@ -856,7 +857,7 @@ class PluginSearchInternal extends PluginSearch {
$count = get_field_sql('SELECT COUNT(*) '.$sql, $values);
if ($count > 0) {
$sql = 'SELECT id, name, description, grouptype, jointype, request, public, ctime, mtime, category, urlid ' . $sql . ' ORDER BY name';
$sql = 'SELECT id, name, description, grouptype, jointype, request, public, ctime, mtime, category, urlid, shortname ' . $sql . ' ORDER BY name';
$data = get_records_sql_array($sql, $values, $offset, $limit);
}
......
......@@ -246,3 +246,16 @@ table.table tr {
.table>tbody>tr:first-child>td {
border-top-width: 0;
}
// Fixing very long words without spaces in the table body.
.table-group-list>tbody>tr>td,
.table-group-list>tbody>tr>th {
word-break: break-all;
}
// Specific width for group table.
.table>thead>tr>th.groupname,
.table>thead>tr>th.groupshortname {
width: 20%;
}
......@@ -2,10 +2,11 @@
{$searchform|safe}
<div class="panel panel-default view-container">
<table id="admgroupslist" class="fullwidth table table-striped">
<table id="admgroupslist" class="fullwidth table table-striped table-group-list">
<thead>
<tr>
<th>{str tag="groupname" section="admin"}</th>
<th class="groupname">{str tag="groupname" section="admin"}</th>
<th class="groupshortname">{str tag="groupshortname" section="admin"}</th>
<th class="center">{str tag="groupmembers" section="admin"}</th>
<th class="center">{str tag="groupadmins" section="admin"}</th>
<th>{str tag="grouptype" section="admin"}</th>
......
{foreach from=$groups item=group}
<tr class="{cycle values='r0,r1'}">
<td><a href="{$group->homepage_url}">{$group->name}</a></td>
<td>{$group->shortname}</td>
<td class="center">{$group->members}</td>
<td class="center">{$group->admins}</td>
<td>{strip}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment