Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
// This file is part of Moodle - http://moodle.org/
2
//
3
// Moodle is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, either version 3 of the License, or
6
// (at your option) any later version.
7
//
8
// Moodle is distributed in the hope that it will be useful,
9
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
// GNU General Public License for more details.
12
//
13
// You should have received a copy of the GNU General Public License
14
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
15
 
16
/**
17
 * A javascript module to enhance the event form.
18
 *
19
 * @module     core_calendar/event_form
20
 * @copyright  2017 Ryan Wyllie <ryan@moodle.com>
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
define(['jquery', 'core_calendar/repository', 'core/notification'], function($, CalendarRepository, Notification) {
24
 
25
    var SELECTORS = {
26
        EVENT_GROUP_COURSE_ID: '[name="groupcourseid"]',
27
        EVENT_GROUP_ID: '[name="groupid"]',
28
        SELECT_OPTION: 'option'
29
    };
30
 
31
    /**
32
     * Listen for when the user changes the group course when configuring
33
     * a group event and filter the options in the group select to only
34
     * show the groups available within the course the user has selected.
35
     *
36
     * @method addCourseGroupSelectListeners
37
     * @param {object} formElement The root form element
38
     */
39
    var addCourseGroupSelectListeners = function(formElement) {
40
        var courseGroupSelect = formElement.find(SELECTORS.EVENT_GROUP_COURSE_ID);
41
 
42
        var loadGroupSelectOptions = function(groups) {
43
            var groupSelect = formElement.find(SELECTORS.EVENT_GROUP_ID),
44
                groupSelectOptions = groupSelect.find(SELECTORS.SELECT_OPTION),
45
                courseGroups = $(groups);
46
 
47
            // Let's clear all options first.
48
            groupSelectOptions.remove();
49
            groupSelect.prop("disabled", false);
50
            courseGroups.each(function(id, group) {
51
                $(groupSelect).append($("<option></option>").attr("value", group.id).text(group.name));
52
            });
53
        };
54
 
55
        // If the user choose a course in the selector do a WS request to get groups.
56
        courseGroupSelect.on('change', function() {
57
            var courseId = formElement.find(SELECTORS.EVENT_GROUP_COURSE_ID).val();
11 efrain 58
            if (isNaN(courseId) || courseId <= 0) {
59
                loadGroupSelectOptions([]);
60
            } else {
61
                CalendarRepository.getCourseGroupsData(courseId)
62
                    .then(function(groups) {
63
                        return loadGroupSelectOptions(groups);
64
                    })
65
                    .catch(Notification.exception);
66
            }
1 efrain 67
        });
68
    };
69
 
70
    /**
71
     * Initialise all of the form enhancements.
72
     *
73
     * @method init
74
     * @param {string} formId The value of the form's id attribute
75
     */
76
    var init = function(formId) {
77
        var formElement = $('#' + formId);
78
        addCourseGroupSelectListeners(formElement);
79
    };
80
 
81
    return {
82
        init: init,
83
    };
84
});