Commit 0c6d05d2 authored by Robert Lyon's avatar Robert Lyon
Browse files

Bug 1597133: Adding institutions to groups that don't have them



And adding an institution selection to the Admin -> Group search

Also making sure that we can retain supplied shortname or throw error
if there is a clash

behatnotneeded

Change-Id: Ibbd6c05c1428e522c048b10c64389ebe3360473a
Signed-off-by: default avatarRobert Lyon <robertl@catalyst.net.nz>
parent 97bf0689
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -22,8 +22,9 @@ require_once('searchlib.php');
$query  = param_variable('query', '');
$offset = param_integer('offset', 0);
$limit = param_integer('limit', 10);
$institution = param_alphanum('institution', 'all');

$data = build_grouplist_html($query, $limit, $offset, $count);
$data = build_grouplist_html($query, $limit, $offset, $count, $institution);
$data['count'] = $count;
$data['offset'] = $offset;
$data['query'] = $query;
+36 −16
Original line number Diff line number Diff line
@@ -24,37 +24,53 @@ $query = param_variable('query', '');
$offset = param_integer('offset', 0);
$limit = param_integer('limit', 0);
$limit = user_preferred_limit($limit, 'itemsperpage');
$institution = param_alphanum('institution', null);

$data = build_grouplist_html($query, $limit, $offset);
// Build the institution select field that sits behind the search field
$inst_select = array();
$institution = !$institution ? 'all' : $institution;
$institutions = get_records_array('institution', '', '', 'displayname');
$inst_select['all'] = get_string('Allinstitutions');
if (is_array($institutions)) {
    foreach ($institutions as $inst) {
        $inst_select[$inst->name] = $inst->displayname;
    }
}
$count = 0;
$data = build_grouplist_html($query, $limit, $offset, $count, $institution);

$searchform = pieform(array(
    'name'   => 'search',
    'renderer' => 'div',
    'class' => 'form-inline with-heading',
    'class' => 'form-inline with-heading dropdown admin-user-search',
    'autofocus' => false,
    'elements' => array(
        'inputgroup' => array(
            'type'  => 'fieldset',
            'title' => get_string('Query') . ': ',
            'class' => 'input-group form-inline',
            'class' => 'dropdown-group js-dropdown-group',
            'elements'     => array(
                'query' => array(
                    'type'  => 'text',
                    'defaultvalue' => $query,
                    'hiddenlabel' => true,
                    'value' => get_string('search'),
                    'class' => 'emptyonfocus',
                    'title' => get_string('search'),
                    'class' => 'with-dropdown js-with-dropdown',
                    'title' => get_string('search') . ': ',
                ),
                'institution' => array(
                    'type'         => 'select',
                    'title'        => get_string('Institution', 'admin'),
                    'defaultvalue' => $institution,
                    'options'      => $inst_select,
                    'class'        => 'dropdown-connect js-dropdown-connect',
                ),
            ),
        ),
        'submit' => array(
            'type'  => 'button',
            'usebuttontag' => true,
                    'class' => 'btn-primary input-group-btn',
            'class' => 'btn-search btn btn-primary admin-groups',
            'value' => get_string('search'),
        )
    ),
        ),
    ),
));

$js = <<< EOF
@@ -62,7 +78,8 @@ addLoadEvent(function () {
p = {$data['pagination_js']}
connect('search_submit', 'onclick', function (event) {
    replaceChildNodes('messages');
    var params = {'query': $('search_query').value};
    var params = {'query': $('search_query').value,
                  'institution': $('search_institution').value};
    p.sendQuery(params);
    event.stop();
    });
@@ -78,5 +95,8 @@ $smarty->assign('results', $data);
$smarty->display('admin/groups/groups.tpl');

function search_submit(Pieform $form, $values) {
    redirect(get_config('wwwroot') . 'admin/groups/groups.php' . ((isset($values['query']) && ($values['query'] != '')) ? '?query=' . urlencode($values['query']) : ''));
    $search = (isset($values['query']) && $values['query'] != '') ? 'query=' . urlencode($values['query']) : null;
    $institution = (isset($values['institution']) && $values['institution'] != '') ? urlencode($values['institution']) : null;
    $query = '?search=1&query=' . $search . '&institution=' . $institution;
    redirect(get_config('wwwroot') . 'admin/groups/groups.php' . $query);
}
+1 −0
Original line number Diff line number Diff line
@@ -306,6 +306,7 @@ function uploadcsv_submit(Pieform $form, $values) {

        if (!$values['updategroups'] || !isset($UPDATES[$group->shortname])) {
            $group->members = array($USER->id => 'admin');
            $group->retainshortname = true;
            $group->id = group_create((array)$group);

            $addedgroups[] = $group;
+7 −1
Original line number Diff line number Diff line
@@ -4484,7 +4484,7 @@ function xmldb_core_upgrade($oldversion=0) {
    }

    if ($oldversion < 2016062200) {
        include_once(get_config('docroot') . 'lib/group.php');
        require_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(
@@ -4500,5 +4500,11 @@ function xmldb_core_upgrade($oldversion=0) {
        }
    }

    if ($oldversion < 2016062900) {
        log_debug('Assign an istitution for each existing group that doesn\'t have one.');
        $groups = execute_sql("UPDATE {group} SET institution = 'mahara'
                               WHERE (institution IS NULL OR institution = '') AND deleted = 0", array());
    }

    return $status;
}
+22 −30
Original line number Diff line number Diff line
@@ -354,39 +354,30 @@ function group_create($data) {
    $data['invitefriends'] = (isset($data['invitefriends'])) ? intval($data['invitefriends']) : 0;
    $data['suggestfriends'] = (isset($data['suggestfriends'])) ? intval($data['suggestfriends']) : 0;

    if (isset($data['shortname']) && strlen($data['shortname'])) {
        // This is a group whose details and membership can be updated automatically, using a
        // webservice api or possibly csv upload.

        // On updates to this group, it will be identified using the institution and shortname
        // which must be unique.

        // The $USER object will be set to someone with at least institutional admin permission.
        global $USER;

        if (empty($data['institution'])) {
            throw new SystemException("group_create: a group with a shortname must have an institution; shortname: " . $data['shortname']);
    if (!empty($data['shortname'])) {
        // make sure it is unique and is correct length
        $shortname = group_generate_shortname($data['shortname']);
        // If we want to retain the supplied shortname we need to make sure it can be done
        if (!empty($data['retainshortname'])) {
            if ($shortname != $data['shortname']) {
                throw new UserException('group_create: problem with supplied shortname ' . $data['shortname'] . ' not matching allowed shortname ' . $shortname);
            }
        if (!$USER->can_edit_institution($data['institution'])) {
            throw new AccessDeniedException("group_create: cannot create a group in this institution");
        }
        if (!preg_match('/^[a-zA-Z0-9_.-]{2,255}$/', $data['shortname'])) {
            $message = get_string('invalidshortname', 'group') . ': ' . $data['shortname'];
            $message .= "\n" . get_string('shortnameformat', 'group');
            throw new UserException($message);
        $data['shortname'] = $shortname;
    }
        if (record_exists('group', 'shortname', $data['shortname'], 'institution', $data['institution'])) {
            throw new UserException('group_create: group with shortname ' . $data['shortname'] . ' and institution ' . $data['institution'] . ' already exists');
    else {
        // Create it from group name
        $data['shortname'] = group_generate_shortname($data['name']);
    }
        if (empty($data['members'])) {
            $data['members'] = array($USER->get('id') => 'admin');

    if (!empty($data['institution']) && $data['institution'] != 'mahara') {
        global $USER;
        if (!$USER->can_edit_institution($data['institution'])) {
            throw new AccessDeniedException("group_create: cannot create a group in this institution");
        }
    }
    else {
        if (!empty($data['institution'])) {
            throw new SystemException("group_create: group institution only available for api-controlled groups");
        }
        $data['shortname'] = group_generate_shortname($data['name']);
        $data['institution'] = 'mahara';
    }

    if (get_config('cleanurls') && (!isset($data['urlid']) || strlen($data['urlid']) == 0)) {
@@ -394,8 +385,9 @@ function group_create($data) {
        $data['urlid'] = group_get_new_homepage_urlid($data['urlid']);
    }

    if (!is_array($data['members']) || count($data['members']) == 0) {
        throw new InvalidArgumentException("group_create: at least one member must be specified for adding to the group");
    // Need to make sure group has at least one member
    if (empty($data['members'])) {
        $data['members'] = array($USER->get('id') => 'admin');
    }

    if (!isset($data['submittableto'])) {
@@ -1599,9 +1591,9 @@ function group_format_editwindow($group) {
/*
 * Used by admin/groups/groups.php and admin/groups/groups.json.php for listing groups.
 */
function build_grouplist_html($query, $limit, $offset, &$count=null) {
function build_grouplist_html($query, $limit, $offset, &$count=null, $institution) {

    $groups = search_group($query, $limit, $offset, 'all');
    $groups = search_group($query, $limit, $offset, 'all', '', $institution);
    $count = $groups['count'];

    if ($ids = array_map(create_function('$a', 'return intval($a->id);'), $groups['data'])) {
Loading