aboutsummaryrefslogtreecommitdiff
path: root/resources/assets/javascripts/lib/admission.js
blob: 33e96aabc7aa2ff91a9305342772a357ba4e3c89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/* ------------------------------------------------------------------------
 * Anmeldeverfahren und -sets
 * ------------------------------------------------------------------------ */
import { $gettext } from './gettext.js';
import Dialog from './dialog.js';
import Dialogs from './dialogs.js';

const Admission = {
    getCourses: function(targetUrl) {
        var courseFilter = $('input[name="course_filter"]').val();
        if (courseFilter == '') {
            courseFilter = '%%%';
        }
        var data = {
            'courses[]': _.map($('#courselist input:checked'), 'id'),
            course_filter: courseFilter,
            semester: $('select[name="semester"]').val(),
            'institutes[]': $.merge(
                _.map($('input[name="institutes[]"]:hidden'), 'value'),
                _.map($('input[name="institutes[]"]:checked'), 'value')
            )
        };
        var loading = $gettext('Wird geladen');
        $('#instcourses').empty();
        $('<img/>', {
            src: STUDIP.ASSETS_URL + 'images/ajax_indicator_small.gif'
        }).appendTo('#instcourses');
        $('#instcourses').append(loading);
        $('#instcourses').load(targetUrl, data);
        return false;
    },

    configureRule: function(ruleType, targetUrl, ruleId) {
        var urlparts = targetUrl.split('?');
        targetUrl = urlparts[0] + '/' + ruleType;
        if (urlparts[1]) {
            targetUrl += '?' + urlparts[1];
        }

        Dialog.fromURL(targetUrl, {
            method: 'post',
            size: 'auto',
            title: $gettext('Anmelderegel konfigurieren'),
            id: 'configurerule',
            data: { ruleId: ruleId, rules: _.map($('#rules input[name="rules[]"]'), 'value') }
        });

        return false;
    },

    selectRuleType: function(source) {
        Dialog.fromURL(source, {
            title: $gettext('Anmelderegel konfigurieren'),
            size: 'auto',
            data: { rules: _.map($('#rules input[name="rules[]"]'), 'value') },
            method: 'post',
            id: 'configurerule'
        });
        return false;
    },

    saveRule: function(ruleId, targetId, targetUrl) {
        if ($('#action').val() !== 'cancel') {
            $.ajax({
                type: 'post',
                url: targetUrl,
                data: $('#ruleform').serialize(),
                dataType: 'html',
                success: function(data, textStatus, jqXHR) {
                    if (data !== '') {
                        var result = '';
                        if ($('#norules').length > 0) {
                            $('#norules').remove();
                            $('#' + targetId).prepend('<div id="rulelist"></div>');
                        }
                        result += data;
                        if ($('#rule_' + ruleId).length !== 0) {
                            $('#rule_' + ruleId).replaceWith(result);
                        } else {
                            $('#rulelist').append(result);
                        }
                    }
                },
                error: function(jqXHR, textStatus, errorThrown) {
                    alert('Status: ' + textStatus + '\nError: ' + errorThrown);
                }
            });
        }
        Admission.closeDialog('configurerule');
        Admission.toggleNotSavedAlert();
        return false;
    },

    removeRule: function(targetId, containerId) {
        var parent = $('#' + targetId).parent();
        $('#' + targetId).remove();
        if (parent.children('div').length === 0) {
            parent.remove();
            var norules = $gettext('Sie haben noch keine Anmelderegeln festgelegt.');
            $('#' + containerId).prepend('<span id="norules">' + '<i>' + norules + '</i></span>');
        }
        Dialogs.closeConfirmDialog();
        Admission.toggleNotSavedAlert();
    },

    toggleRuleDescription: function(targetId) {
        $('#' + targetId).toggle();
        return false;
    },

    toggleDetails: function(arrowId, detailId) {
        var oldSrc = $('#' + arrowId).attr('src');
        var newSrc = $('#' + arrowId).attr('rel');
        $('#' + arrowId).attr('src', newSrc);
        $('#' + arrowId).attr('rel', oldSrc);
        $('#' + detailId).slideToggle();
        return false;
    },

    /**
     *
     * @param String ruleId      The rule to save.
     * @param String errorTarget Target element ID where error messages will be
     *                           shown.
     * @param String validateUrl URL to call for validation.
     * @param String savedTarget Target element ID where the saved rule will be
     *                           displayed.
     * @param String saveUrl     URL to save the rule.
     */
    checkAndSaveRule: function(ruleId, errorTarget, validateUrl, savedTarget, saveUrl) {
        if (Admission.validateRuleConfig(errorTarget, validateUrl)) {
            Admission.saveRule(ruleId, savedTarget, saveUrl);
            Dialog.close({ id: 'configurerule' });
        }
        return false;
    },

    validateRuleConfig: function(containerId, targetUrl) {
        var valid = true;
        var error = $.ajax({
            type: 'post',
            async: false,
            url: targetUrl,
            data: $('#ruleform').serialize(),
            dataType: 'html',

            error: function(jqXHR, textStatus, errorThrown) {
                alert('Status: ' + textStatus + '\nError: ' + errorThrown);
            }
        }).responseText;
        error = error.replace(/(\r\n|\n|\r)/gm, '');
        if ($.trim(error) != '') {
            $('#' + containerId).html(error);
            valid = false;
        }
        return valid;
    },

    removeUserFromUserlist: function(userId) {
        var parent = $('#user_' + userId).parent();
        $('#user_' + userId).remove();
        if (parent.children('li').length === 0) {
            var nousers = $gettext('Sie haben noch niemanden hinzugefügt.');
            $(parent)
                .parent()
                .append('<span id="nousers">' + '<i>' + nousers + '</i></span>');
        }
        return false;
    },

    /**
     * Creates a tree view from the HTML list in <elementId> using the
     * given data for special node types.
     *
     * @param String elementId
     * @param typesData JS object with tree nodes types
     *          (@see http://www.jstree.com/documentation/types)
     */
    makeTree: function(elementId, typesData) {
        var config = {
            core: {
                animation: 100,
                open_parents: true,
                initially_open: ['root']
            },
            checkbox: {
                real_checkboxes: true,
                selected_parent_open: true,
                override_ui: false,
                two_state: true
            },
            plugins: ['html_data', 'themes', 'types', 'checkbox', 'ui']
        };
        config.types = { types: typesData };
        $('#' + elementId)
            .on('loaded.jstree', function(event, data) {
                // Show checked checkboxes.
                var checkedItems = $('#' + elementId).find('.jstree-checked');
                checkedItems.removeClass('jstree-unchecked');
                // Open parent nodes of checked nodes.
                checkedItems.parents().each(function() {
                    data.inst.open_node(this, false, true);
                });
            })
            .jstree(config);
    },

    updateInstitutes: function(elementId, instURL, courseURL, mode) {
        if (elementId !== '') {
            var query = '';
            $('.institute').each(function() {
                query += '&institutes[]=' + this.value;
            });
            switch (mode) {
                case 'delete':
                    $('#' + elementId).remove();
                    break;
                case 'add':
                    query += '&institutes[]=' + elementId;
                    $.post(instURL, query, function(data) {
                        $('#institutes').html(data);
                    });
                    break;
            }
            $('#instcourses :checked').each(function() {
                query += '&courses[]=' + this.value;
            });
            this.getCourses(courseURL);
            Admission.toggleNotSavedAlert();
        }
    },

    checkRuleActivation: function(target) {
        var form = $('#' + target);
        var globalActivation = form.find('input[name=enabled]');
        if (globalActivation.prop('checked')) {
            $('#activation').show();
            if (form.find('input[name=activated]:checked').val() === 'studip') {
                $('#institutes_activation').hide();
            } else {
                $('#institutes_activation').show();
            }
        } else {
            $('#activation').hide();
            $('#institutes_activation').hide();
        }
    },

    closeDialog: function(elementId) {
        $('#' + elementId).remove();
    },

    checkUncheckAll: function(inputName, mode) {
        switch (mode) {
            case 'check':
                $('input[name*="' + inputName + '"]').each(function() {
                    $(this).prop('checked', true);
                });
                break;
            case 'uncheck':
                $('input[name*="' + inputName + '"]').each(function() {
                    $(this).prop('checked', false);
                });
                break;
            case 'invert':
                $('input[name*="' + inputName + '"]').each(function() {
                    $(this).prop('checked', !$(this).prop('checked'));
                });
                break;
        }
        return false;
    },

    toggleNotSavedAlert: function() {
        $('.hidden-alert').show();
    },

    autosaveCourseset: function(event) {
        $.post({
            url: $('#courseset-form').attr('action'),
            data: $('#courseset-form').serialize() + '&submit=1',
            dataType: 'html',
            success: function(data, textStatus, jqXHR) {
                $('.hidden-alert').hide();
            },
            error: function(jqXHR, textStatus, errorThrown) {
                alert('Status: ' + textStatus + '\nError: ' + errorThrown);
            }
        });
    }

};

export default Admission;