groupcategories.php 7.68 KB
Newer Older
1 2 3 4 5 6
<?php
/**
 *
 * @package    mahara
 * @subpackage admin
 * @author     Catalyst IT Ltd
7 8
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
 * @copyright  For copyright information on Mahara, please see the README file distributed with this software.
9 10 11 12 13
 *
 */

define('INTERNAL', 1);
define('ADMIN', 1);
14
define('MENUITEM', 'managegroups/categories');
15 16

require(dirname(dirname(dirname(__FILE__))) . '/init.php');
17
define('TITLE', get_string('groupcategories', 'admin'));
18

19 20
$optionform = pieform(array(
    'name'       => 'groupcategories',
Naomi Guyer's avatar
Naomi Guyer committed
21 22
    'renderer'   => 'div',
    'class'      => '',
23 24 25 26
    'plugintype' => 'core',
    'pluginname' => 'admin',
    'elements'   => array(
        'allowgroupcategories' => array(
27
            'class'        => 'with-label-widthauto',
28
            'type'         => 'switchbox',
29 30 31 32
            'title'        => get_string('enablegroupcategories', 'admin'),
            'defaultvalue' => get_config('allowgroupcategories'),
        ),
        'submit' => array(
33
            'class'        => 'btn-primary',
34 35 36 37 38 39 40 41 42
            'type'         => 'submit',
            'value'        => get_string('submit'),
        ),
    )
));

function groupcategories_submit(Pieform $form, $values) {
    set_config('allowgroupcategories', (int) $values['allowgroupcategories']);
    redirect(get_config('wwwroot') . 'admin/groups/groupcategories.php');
43 44
}

45
$strings = array('edit', 'delete', 'update', 'cancel', 'add', 'name', 'unknownerror');
46
$adminstrings = array('confirmdeletecategory', 'deletefailed', 'addnewgroupcategory');
47
$argumentstrings = array('editspecific', 'deletespecific');
48 49 50 51 52 53
foreach ($strings as $string) {
    $getstring[$string] = json_encode(get_string($string));
}
foreach ($adminstrings as $string) {
    $getstring[$string] = json_encode(get_string($string, 'admin'));
}
54 55 56
foreach ($argumentstrings as $string) {
    $getstring[$string] = json_encode(get_string($string, 'mahara', '%s'));
}
57 58 59 60 61 62

$thead = array(json_encode(get_string('name', 'admin')), '""');
$ijs = "var thead = TR(null,map(partial(TH,null),[" . implode($thead,",") . "]));\n";

$ijs .= <<< EOJS
// Request a list of menu items from the server
63 64 65 66 67
function getitems(r) {
    sendjsonrequest('getgroupcategories.json.php', {}, 'GET', function(data) {
        data.focusid = (typeof r != 'undefined') ? r.id : false;
        displaymenuitems(data);
    });
68 69 70 71
}


// Puts the list of menu items into the empty table.
72 73
function displaymenuitems(data) {
    var itemlist = data.groupcategories;
74 75 76
    var rows = map(formatrow,itemlist);
    var form = FORM({'id':'form','method':'post','enctype':'multipart/form-data',
                         'encoding':'multipart/form-data'},
77
                    TABLE({'class':'nohead table table-short'},TBODY(null,[thead,rows,addform()])));
78
    replaceChildNodes($('menuitemlist'),form);
79 80 81
    if (data.focusid) {
        $('item' + data.focusid).focus();
    }
82 83 84 85
}

// Creates one table row
function formatrow (item) {
86 87 88 89 90 91 92 93
    // item has class, id, type, name, link, linkedto

    var edit = BUTTON({
        'class':'btn btn-default btn-sm',
        'id':'item' + item.id,
        'type':'button',
        'title':{$getstring['edit']},
        'alt':{$getstring['editspecific']}.replace('%s', item.name)},
94
            SPAN({'class':'icon icon-cog icon-lg', 'role':'presentation'}),
95 96
            SPAN({'class':'sr-only'}, {$getstring['editspecific']}.replace('%s', item.name))
        );
97

98
    connect(edit, 'onclick', function (e) { e.stop(); edititem(item); });
99

100

101 102 103 104 105 106
    var del = BUTTON({
        'class':'btn btn-default btn-sm',
        'id':'item' + item.id,
        'type':'button',
        'title':{$getstring['delete']},
        'alt':{$getstring['deletespecific']}.replace('%s', item.name)},
107
            SPAN({'class':'icon icon-trash text-danger icon-lg','role':'presentation'}),
108 109 110
            SPAN({'class':'sr-only'}, {$getstring['deletespecific']}.replace('%s', item.name))
        );

111
    connect(del, 'onclick', function (e) { e.stop(); delitem(item.id); });
112 113 114

    var buttongroup = SPAN({'class': 'btn-group'}, edit, del);

115 116 117 118
    var cells = map(
        partial(TD,null),
        [
            item.name,
119
            [buttongroup]
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
        ]
    );
    return TR({'id':'menuitem_'+item.id},cells);
}

// Returns the form which adds a new menu item
function addform(type) {
    var item = {'id':'new'};
    return editform(item);
}

// Creates the contents of a menu item edit form
// This is formatted as a table within the form (which is within a row of the table).
function editform(item) {
    // item has id, name

    // Either a save, a cancel button, or both.
    var savecancel = [];
138
    var save = INPUT({'type':'button','class':'button btn btn-default btn-add-group'});
139 140 141 142 143
    connect(save, 'onclick', function () { saveitem(item.id); });

    var rowtype = 'add';
    if (!item.name) {
        item.name = '';
144
        item.label = {$getstring['addnewgroupcategory']};
145 146 147 148 149 150 151 152
        // The save button says 'add', and there's no cancel button.
        setNodeAttribute(save,'value',{$getstring['add']});
        savecancel = [save];
    }
    else { // Editing an existing menu item.
        // The save button says 'update' and there's a cancel button.
        var rowtype = 'edit';
        setNodeAttribute(save,'value',{$getstring['update']});
Naomi Guyer's avatar
Naomi Guyer committed
153
        var cancel = INPUT({'type':'button','class':'button btn btn-sm btn-default','value':{$getstring['cancel']}});
154 155
        connect(cancel, 'onclick', closeopenedits);
        savecancel = [save,cancel];
156
        item.label = {$getstring['edit']};
157 158 159
    }

    // A text field for the name
160
    var label = LABEL({'for':'name'+item.id,'class':'accessible-hidden'}, null, item.label);
Naomi Guyer's avatar
Naomi Guyer committed
161
    var name = INPUT({'type':'text','class':'text form-control input-sm','id':'name'+item.id,'value':item.name});
162 163 164 165 166 167
    connect(name, 'onkeydown', function(e) {
        if (keypressKeyCode(e) == 13) {
            signal(save, 'onclick');
            e.stop();
        }
    });
168
    var parentspan = createDOM('span',null,label,name);
169
    var row = TR({'id':'row'+item.id, 'class':rowtype},
170
                 map(partial(TD,null),[parentspan,savecancel]));
171 172 173 174 175 176 177 178 179 180
    return row;
}

// Close all open edit forms
function closeopenedits() {
    var rows = getElementsByTagAndClassName('tr',null,$('menuitemlist'));
    for (var i=0; i<rows.length; i++) {
        if (hasElementClass(rows[i],'edit')) {
            removeElement(rows[i]);
        }
181 182
        else if (hasElementClass(rows[i],'hidden')) {
            removeElementClass(rows[i],'hidden');
183 184 185 186 187 188 189 190
        }
    }
}

// Open a new edit form
function edititem(item) {
    closeopenedits();
    var menuitem = $('menuitem_'+item.id);
191
    addElementClass(menuitem,'hidden');
192 193
    var newrow = editform(item);
    insertSiblingNodesBefore(menuitem, newrow);
194
    $('name' + item.id).focus();
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
}

// Receive standard json error message
// Request deletion of a menu item from the db
function delitem(itemid) {
    if (confirm({$getstring['confirmdeletecategory']})) {
        sendjsonrequest('deletegroupcategory.json.php',{'itemid':itemid}, 'POST', getitems);
    }
}

// Send the menu item in the form to the database.
function saveitem(itemid) {
    var f = $('form');
    var name = $('name'+itemid).value;
    if (name == '') {
        displayMessage(get_string('namedfieldempty',{$getstring['name']}),'error');
        return false;
    }

    var data = {'name':name,
215
                'itemid':itemid};
216 217 218
    sendjsonrequest('updategroup.json.php', data, 'POST', function(r) {
        getitems(r);
    });
219 220 221 222 223 224 225 226
    return false;
}

addLoadEvent(function () {
    getitems();
});
EOJS;

227
$smarty = smarty();
228 229
setpageicon($smarty, 'icon-users');

230 231
$smarty->assign('PAGEHEADING', hsc(get_string('groupcategories', 'admin')));
$smarty->assign('INLINEJAVASCRIPT', $ijs);
232 233
$smarty->assign('optionform', $optionform);
$smarty->display('admin/groups/groupcategories.tpl');