certificate_invalidation_view.js 5.83 KB
Newer Older
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
// Backbone Application View: CertificateInvalidationView
/*global define, RequireJS */

;(function(define) {
    'use strict';
    define(
        ['jquery', 'underscore', 'gettext', 'backbone', 'js/certificates/models/certificate_invalidation'],

        function($, _, gettext, Backbone, CertificateInvalidationModel) {
            return Backbone.View.extend({
                el: "#certificate-invalidation",
                messages: "div.message",
                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) {
                    var templateSelector = "#" + name + "-tpl",
                    templateText = $(templateSelector).text();
                    return _.template(templateText);
                },

                invalidateCertificate: function() {
                    var user = this.$("#certificate-invalidation-user").val();
                    var notes = this.$("#certificate-invalidation-notes").val();
36
                    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."); // jshint ignore:line
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 70 71
                                    self.escapeAndShowMessage(
                                        gettext("Server Error, Please refresh the page and try again.")
                                    );
72 73 74 75 76 77
                                }
                            }
                        });

                    }
                    else {
78
                        this.escapeAndShowMessage(certificate_invalidation.validationError);
79 80 81 82 83 84 85 86 87 88 89
                    }
                },

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

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

                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);
                },

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

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