certificate_invalidation_view.js 5.86 KB
Newer Older
1
// Backbone Application View: CertificateInvalidationView
2
/* global define, RequireJS */
3

4
(function(define) {
5 6 7 8 9 10
    'use strict';
    define(
        ['jquery', 'underscore', 'gettext', 'backbone', 'js/certificates/models/certificate_invalidation'],

        function($, _, gettext, Backbone, CertificateInvalidationModel) {
            return Backbone.View.extend({
11 12
                el: '#certificate-invalidation',
                messages: 'div.message',
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
                events: {
                    'click #invalidate-certificate': 'invalidateCertificate',
                    'click .re-validate-certificate': 'reValidateCertificate'
                },

                initialize: function() {
                    this.listenTo(this.collection, 'change add remove', this.render);
                },

                render: function() {
                    var template = this.loadTemplate('certificate-invalidation');
                    this.$el.html(template({certificate_invalidations: this.collection.models}));
                },

                loadTemplate: function(name) {
28 29
                    var templateSelector = '#' + name + '-tpl',
                        templateText = $(templateSelector).text();
30 31 32 33
                    return _.template(templateText);
                },

                invalidateCertificate: function() {
34 35 36
                    var user = this.$('#certificate-invalidation-user').val();
                    var notes = this.$('#certificate-invalidation-notes').val();
                    var message = '';
37

38 39 40 41 42 43 44 45 46
                    var certificate_invalidation = new CertificateInvalidationModel(
                        {
                            user: user,
                            notes: notes
                        },
                        {
                            url: this.collection.url
                        }
                    );
47 48

                    if (this.collection.findWhere({user: user})) {
49
                        message = gettext('Certificate of <%= user %> has already been invalidated. Please check your spelling and retry.');  // eslint-disable-line max-len
50
                        this.escapeAndShowMessage(_.template(message)({user: user}));
51 52 53 54 55 56 57 58
                    }
                    else if (certificate_invalidation.isValid()) {
                        var self = this;
                        certificate_invalidation.save(null, {
                            wait: true,

                            success: function(model) {
                                self.collection.add(model);
59
                                message = gettext('Certificate has been successfully invalidated for <%= user %>.');
60
                                self.escapeAndShowMessage(_.template(message)({user: user}));
61 62 63 64 65
                            },

                            error: function(model, response) {
                                try {
                                    var response_data = JSON.parse(response.responseText);
66
                                    self.escapeAndShowMessage(response_data.message);
67
                                }
68
                                catch (exception) {
69
                                    self.escapeAndShowMessage(
70
                                        gettext('Server Error, Please refresh the page and try again.')
71
                                    );
72 73 74 75 76
                                }
                            }
                        });
                    }
                    else {
77
                        this.escapeAndShowMessage(certificate_invalidation.validationError);
78 79 80 81 82 83 84 85 86 87 88
                    }
                },

                reValidateCertificate: function(event) {
                    var certificate_invalidation = $(event.target).data();
                    var model = this.collection.get(certificate_invalidation),
                        self = this;

                    if (model) {
                        model.destroy({
                            success: function() {
89
                                self.escapeAndShowMessage(
90
                                    gettext('The certificate for this learner has been re-validated and the system is re-running the grade for this learner.')  // eslint-disable-line max-len
91
                                );
92 93 94 95
                            },
                            error: function(model, response) {
                                try {
                                    var response_data = JSON.parse(response.responseText);
96
                                    self.escapeAndShowMessage(response_data.message);
97
                                }
98
                                catch (exception) {
99
                                    self.escapeAndShowMessage(
100
                                        gettext('Server Error, Please refresh the page and try again.')
101
                                    );
102 103 104 105 106 107 108
                                }
                            },
                            wait: true,
                            data: JSON.stringify(model.attributes)
                        });
                    }
                    else {
109
                        self.escapeAndShowMessage(
110
                            gettext('Could not find Certificate Invalidation in the list. Please refresh the page and try again')  // eslint-disable-line max-len
111
                        );
112 113 114 115 116 117 118 119
                    }
                },

                isEmailAddress: function validateEmail(email) {
                    var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
                    return re.test(email);
                },

120
                escapeAndShowMessage: function(message) {
121 122
                    $(this.messages + '>p').remove();
                    this.$(this.messages).removeClass('hidden').append('<p>' + _.escape(message) + '</p>');
123 124 125 126 127
                }

            });
        }
    );
128
}).call(this, define || RequireJS.define);