discussions.js 12.3 KB
Newer Older
1 2 3 4 5
(function(define) {
    'use strict';
    define(['jquery', 'underscore', 'backbone', 'gettext',
        'js/discussions_management/views/divided_discussions_inline',
        'js/discussions_management/views/divided_discussions_course_wide',
6 7
        'edx-ui-toolkit/js/utils/html-utils',
        'edx-ui-toolkit/js/utils/string-utils',
8
        'js/views/base_dashboard_view',
9 10
        'js/models/notification',
        'js/views/notification'
11 12
    ],

13 14 15
        function($, _, Backbone, gettext, InlineDiscussionsView,
                 CourseWideDiscussionsView,
                 HtmlUtils, StringUtils, BaseDashboardView) {
16 17
            /* global NotificationModel, NotificationView */

18 19 20 21 22 23
            var HIDDEN_CLASS = 'hidden';
            var TWO_COLUMN_CLASS = 'two-column-layout';
            var THREE_COLUMN_CLASS = 'three-column-layout';
            var COHORT = 'cohort';
            var NONE = 'none';
            var ENROLLMENT_TRACK = 'enrollment_track';
24

25
            var DiscussionsView = BaseDashboardView.extend({
26
                events: {
27 28
                    'click .division-scheme': 'divisionSchemeChanged',
                    'change .cohorts-state': 'onCohortsEnabledChanged'
29
                },
30 31 32 33 34

                initialize: function(options) {
                    this.template = HtmlUtils.template($('#discussions-tpl').text());
                    this.context = options.context;
                    this.discussionSettings = options.discussionSettings;
35
                    this.listenTo(this.pubSub, 'cohorts:state', this.cohortStateUpdate, this);
36 37 38
                },

                render: function() {
39
                    var numberAvailableSchemes = this.discussionSettings.attributes.available_division_schemes.length;
40

41 42
                    HtmlUtils.setHtml(this.$el, this.template({
                        availableSchemes: this.getDivisionSchemeData(this.discussionSettings.attributes.division_scheme), //  eslint-disable-line max-len
43
                        layoutClass: numberAvailableSchemes === 2 ? THREE_COLUMN_CLASS : TWO_COLUMN_CLASS
44 45 46
                    }));
                    this.updateTopicVisibility(this.getSelectedScheme(), this.getTopicNav());
                    this.renderTopics();
47 48 49 50 51

                    if (this.isSchemeAvailable(COHORT) ||
                        (!this.isSchemeAvailable(COHORT) && this.getSelectedScheme() === COHORT)) {
                        this.showCohortSchemeControl(true);
                    }
52 53 54
                    return this;
                },

55 56 57
                getDivisionSchemeData: function(selectedScheme) {
                    return [
                        {
58
                            key: NONE,
59 60
                            displayName: gettext('Not divided'),
                            descriptiveText: gettext('Discussions are unified; all learners interact with posts from other learners, regardless of the group they are in.'), //  eslint-disable-line max-len
61
                            selected: selectedScheme === NONE,
62 63 64
                            enabled: true // always leave none enabled
                        },
                        {
65
                            key: ENROLLMENT_TRACK,
66 67
                            displayName: gettext('Enrollment Tracks'),
                            descriptiveText: gettext('Use enrollment tracks as the basis for dividing discussions. All learners, regardless of their enrollment track, see the same discussion topics, but within divided topics, only learners who are in the same enrollment track see and respond to each others’ posts.'), //  eslint-disable-line max-len
68 69
                            selected: selectedScheme === ENROLLMENT_TRACK,
                            enabled: this.isSchemeAvailable(ENROLLMENT_TRACK) || selectedScheme === ENROLLMENT_TRACK
70 71
                        },
                        {
72
                            key: COHORT,
73 74
                            displayName: gettext('Cohorts'),
                            descriptiveText: gettext('Use cohorts as the basis for dividing discussions. All learners, regardless of cohort, see the same discussion topics, but within divided topics, only members of the same cohort see and respond to each others’ posts. '), //  eslint-disable-line max-len
75 76
                            selected: selectedScheme === COHORT,
                            enabled: this.isSchemeAvailable(COHORT) || selectedScheme === COHORT
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
                        }

                    ];
                },

                isSchemeAvailable: function(scheme) {
                    return this.discussionSettings.attributes.available_division_schemes.indexOf(scheme) !== -1;
                },

                showMessage: function(message, type) {
                    var model = new NotificationModel({type: type || 'confirmation', title: message});
                    this.removeNotification();
                    this.notification = new NotificationView({
                        model: model
                    });
                    this.$('.division-scheme-container').prepend(this.notification.$el);
                    this.notification.render();
                },

96
                cohortStateUpdate: function(state) {
97 98 99 100 101 102 103 104 105 106
                    var cohortIndex;
                    if (state.is_cohorted && !this.isSchemeAvailable(COHORT)) {
                        this.discussionSettings.attributes.available_division_schemes.push(COHORT);
                    } else if (!state.is_cohorted && this.getSelectedScheme() !== COHORT) {
                        cohortIndex = this.discussionSettings.attributes.available_division_schemes.indexOf(COHORT);
                        if (cohortIndex > -1) {
                            this.discussionSettings.attributes.available_division_schemes.splice(cohortIndex, 1);
                        }
                    }

107
                    if (!this.isSchemeAvailable(ENROLLMENT_TRACK)) {
108
                        this.showDiscussionManagement(state.is_cohorted);
109 110
                    }
                    if (this.getSelectedScheme() !== COHORT) {
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
                        this.showCohortSchemeControl(state.is_cohorted);
                    }
                },

                showDiscussionManagement: function(show) {
                    if (!show) {
                        $('.btn-link.discussions_management').addClass(HIDDEN_CLASS);
                        $('#discussions_management').addClass(HIDDEN_CLASS);
                    } else {
                        $('.btn-link.discussions_management').removeClass(HIDDEN_CLASS);
                        $('#discussions_management').removeClass(HIDDEN_CLASS);
                    }
                },

                showCohortSchemeControl: function(show) {
                    if (!show) {
                        $('.division-scheme-item.cohort').addClass(HIDDEN_CLASS);
128
                        // Since we are removing the cohort scheme, we can have at most 2 columns.
129 130
                        $('.division-scheme-item').removeClass(THREE_COLUMN_CLASS).addClass(TWO_COLUMN_CLASS);
                    } else {
131 132 133 134 135 136
                        $('.division-scheme-item.cohort').removeClass(HIDDEN_CLASS);
                        if (this.isSchemeAvailable(ENROLLMENT_TRACK)) {
                            $('.division-scheme-item').removeClass(TWO_COLUMN_CLASS).addClass(THREE_COLUMN_CLASS);
                        } else {
                            $('.division-scheme-item').removeClass(THREE_COLUMN_CLASS).addClass(TWO_COLUMN_CLASS);
                        }
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
                removeNotification: function() {
                    if (this.notification) {
                        this.notification.remove();
                    }
                },

                getSelectedScheme: function() {
                    return this.$('input[name="division-scheme"]:checked').val();
                },

                getTopicNav: function() {
                    return this.$('.topic-division-nav');
                },

                divisionSchemeChanged: function() {
                    var selectedScheme = this.getSelectedScheme(),
                        topicNav = this.getTopicNav(),
                        fieldData = {
                            division_scheme: selectedScheme
                        };

                    this.updateTopicVisibility(selectedScheme, topicNav);
                    this.saveDivisionScheme(topicNav, fieldData, selectedScheme);
                },

                saveDivisionScheme: function($element, fieldData, selectedScheme) {
                    var self = this,
                        discussionSettingsModel = this.discussionSettings,
                        showErrorMessage,
                        details = '';

                    this.removeNotification();
                    showErrorMessage = function(message) {
                        self.showMessage(message, 'error');
                    };

                    discussionSettingsModel.save(
                        fieldData, {patch: true, wait: true}
                    ).done(function() {
                        switch (selectedScheme) {
180
                        case NONE:
181 182
                            details = gettext('Discussion topics in the course are not divided.');
                            break;
183
                        case ENROLLMENT_TRACK:
184 185
                            details = gettext('Any divided discussion topics are divided based on enrollment track.'); //  eslint-disable-line max-len
                            break;
186
                        case COHORT:
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
                            details = gettext('Any divided discussion topics are divided based on cohort.');
                            break;
                        default:
                            break;
                        }
                        self.showMessage(
                            StringUtils.interpolate(
                                gettext('Your changes have been saved. {details}'),
                                {details: details},
                                true
                            )
                        );
                    }).fail(function(result) {
                        var errorMessage = null,
                            jsonResponse;
                        try {
                            jsonResponse = JSON.parse(result.responseText);
                            errorMessage = jsonResponse.error;
                        } catch (e) {
                            // Ignore the exception and show the default error message instead.
                        }
                        if (!errorMessage) {
                            errorMessage = gettext('We have encountered an error. Refresh your browser and then try again.'); //  eslint-disable-line max-len
                        }
                        showErrorMessage(errorMessage);
                    });
                },

                updateTopicVisibility: function(selectedScheme, topicNav) {
216 217
                    if (selectedScheme === NONE) {
                        topicNav.addClass(HIDDEN_CLASS);
218
                    } else {
219
                        topicNav.removeClass(HIDDEN_CLASS);
220 221 222
                    }
                },

223 224 225 226
                getSectionCss: function(section) {
                    return ".instructor-nav .nav-item [data-section='" + section + "']";
                },

227
                renderTopics: function() {
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
                    var dividedDiscussionsElement = this.$('.discussions-nav');
                    if (!this.CourseWideDiscussionsView) {
                        this.CourseWideDiscussionsView = new CourseWideDiscussionsView({
                            el: dividedDiscussionsElement,
                            model: this.context.courseDiscussionTopicDetailsModel,
                            discussionSettings: this.discussionSettings
                        }).render();
                    }

                    if (!this.InlineDiscussionsView) {
                        this.InlineDiscussionsView = new InlineDiscussionsView({
                            el: dividedDiscussionsElement,
                            model: this.context.courseDiscussionTopicDetailsModel,
                            discussionSettings: this.discussionSettings
                        }).render();
                    }
                }
            });
            return DiscussionsView;
        });
}).call(this, define || RequireJS.define);