groupcategories.php 6.97 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
21
22
23
24
25
$optionform = pieform(array(
    'name'       => 'groupcategories',
    'renderer'   => 'table',
    'plugintype' => 'core',
    'pluginname' => 'admin',
    'elements'   => array(
        'allowgroupcategories' => array(
26
            'type'         => 'switchbox',
27
28
29
30
31
32
33
34
35
36
37
38
39
            'title'        => get_string('enablegroupcategories', 'admin'),
            'defaultvalue' => get_config('allowgroupcategories'),
        ),
        'submit' => array(
            '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');
40
41
}

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

$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
60
61
62
63
64
function getitems(r) {
    sendjsonrequest('getgroupcategories.json.php', {}, 'GET', function(data) {
        data.focusid = (typeof r != 'undefined') ? r.id : false;
        displaymenuitems(data);
    });
65
66
67
68
}


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

// Creates one table row
function formatrow (item) {
    // item has id, type, name, link, linkedto
84
    var edit = INPUT({'id':'item' + item.id,'type':'image','src':config.theme['images/btn_edit.png'],
85
        'title':{$getstring['edit']},'alt':{$getstring['editspecific']}.replace('%s', item.name)});
86
    connect(edit, 'onclick', function (e) { e.stop(); edititem(item); });
87
88
    var del = INPUT({'type':'image','src':config.theme['images/btn_deleteremove.png'],
        'title':{$getstring['delete']},'alt':{$getstring['deletespecific']}.replace('%s', item.name)});
89
    connect(del, 'onclick', function (e) { e.stop(); delitem(item.id); });
90
91
92
93
    var cells = map(
        partial(TD,null),
        [
            item.name,
94
            [edit,' ',del]
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
        ]
    );
    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 = [];
    var save = INPUT({'type':'button','class':'button'});
    connect(save, 'onclick', function () { saveitem(item.id); });

    var rowtype = 'add';
    if (!item.name) {
        item.name = '';
119
        item.label = {$getstring['addnewgroupcategory']};
120
121
122
123
124
125
126
127
128
129
130
        // 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']});
        var cancel = INPUT({'type':'button','class':'button','value':{$getstring['cancel']}});
        connect(cancel, 'onclick', closeopenedits);
        savecancel = [save,cancel];
131
        item.label = {$getstring['edit']};
132
133
134
    }

    // A text field for the name
135
    var label = LABEL({'for':'name'+item.id,'class':'accessible-hidden'}, null, item.label);
136
    var name = INPUT({'type':'text','class':'text','id':'name'+item.id,'value':item.name});
137
138
139
140
141
142
    connect(name, 'onkeydown', function(e) {
        if (keypressKeyCode(e) == 13) {
            signal(save, 'onclick');
            e.stop();
        }
    });
143
    var parentspan = createDOM('span',null,label,name);
144
    var row = TR({'id':'row'+item.id, 'class':rowtype},
145
                 map(partial(TD,null),[parentspan,savecancel]));
146
147
148
149
150
151
152
153
154
155
    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]);
        }
156
157
        else if (hasElementClass(rows[i],'hidden')) {
            removeElementClass(rows[i],'hidden');
158
159
160
161
162
163
164
165
        }
    }
}

// Open a new edit form
function edititem(item) {
    closeopenedits();
    var menuitem = $('menuitem_'+item.id);
166
    addElementClass(menuitem,'hidden');
167
168
    var newrow = editform(item);
    insertSiblingNodesBefore(menuitem, newrow);
169
    $('name' + item.id).focus();
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
}

// 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,
190
                'itemid':itemid};
191
192
193
    sendjsonrequest('updategroup.json.php', data, 'POST', function(r) {
        getitems(r);
    });
194
195
196
197
198
199
200
201
    return false;
}

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

202
$smarty = smarty();
203
$smarty->assign('PAGEHEADING', hsc(get_string('groupcategories', 'admin')));
Naomi Guyer's avatar
Naomi Guyer committed
204
$smarty->assign('PAGEICON', 'fa fa-users');
205
$smarty->assign('INLINEJAVASCRIPT', $ijs);
206
207
$smarty->assign('optionform', $optionform);
$smarty->display('admin/groups/groupcategories.tpl');