certificates.js 4.82 KB
Newer Older
1
var edx = edx || {};
2
var onCertificatesReady = null;
3

4
(function($, gettext, _) {
5 6 7 8 9
    'use strict';

    edx.instructor_dashboard = edx.instructor_dashboard || {};
    edx.instructor_dashboard.certificates = {};

10
    onCertificatesReady = function() {
11 12 13 14
        /**
         * Show a confirmation message before letting staff members
         * enable/disable self-generated certificates for a course.
         */
15
        $('#enable-certificates-form').on('submit', function(event) {
16 17 18
            var isEnabled = $('#certificates-enabled').val() === 'true',
                confirmMessage = '';

19
            if (isEnabled) {
20 21 22 23 24
                confirmMessage = gettext('Allow students to generate certificates for this course?');
            } else {
                confirmMessage = gettext('Prevent students from generating certificates in this course?');
            }

25
            if (!confirm(confirmMessage)) {
26 27 28 29 30 31 32 33 34 35 36
                event.preventDefault();
            }
        });

        /**
         * Refresh the status for example certificate generation
         * by reloading the instructor dashboard.
         */
        $('#refresh-example-certificate-status').on('click', function() {
            window.location.reload();
        });
37 38 39 40 41


        /**
         * Start generating certificates for all students.
         */
42
        var $section = $('section#certificates');
43
        $section.on('click', '#btn-start-generating-certificates', function(event) {
44
            if (!confirm(gettext('Start generating certificates for all students in this course?'))) {
45 46 47 48
                event.preventDefault();
                return;
            }

Eric Fischer committed
49 50
            var $btn_generating_certs = $(this),
                $certificate_generation_status = $('.certificate-generation-status');
51 52
            var url = $btn_generating_certs.data('endpoint');
            $.ajax({
53
                type: 'POST',
54
                url: url,
55 56
                success: function(data) {
                    $btn_generating_certs.attr('disabled', 'disabled');
57 58 59 60 61 62 63
                    $certificate_generation_status.text(data.message);
                },
                error: function(jqXHR, textStatus, errorThrown) {
                    $certificate_generation_status.text(gettext('Error while generating certificates. Please try again.'));
                }
            });
        });
64 65 66 67 68

        /**
         * Start regenerating certificates for students.
         */
        $section.on('click', '#btn-start-regenerating-certificates', function(event) {
69
            if (!confirm(gettext('Start regenerating certificates for students in this course?'))) {
70 71 72 73 74 75 76 77 78
                event.preventDefault();
                return;
            }

            var $btn_regenerating_certs = $(this),
                $certificate_regeneration_status = $('.certificate-regeneration-status'),
                url = $btn_regenerating_certs.data('endpoint');

            $.ajax({
79 80
                type: 'POST',
                data: $('#certificate-regenerating-form').serializeArray(),
81
                url: url,
82 83 84 85
                success: function(data) {
                    $btn_regenerating_certs.attr('disabled', 'disabled');
                    if (data.success) {
                        $certificate_regeneration_status.text(data.message).addClass('message');
Eric Fischer committed
86
                    } else {
87
                        $certificate_regeneration_status.text(data.message).addClass('message');
88 89 90
                    }
                },
                error: function(jqXHR) {
91
                    try {
92
                        var response = JSON.parse(jqXHR.responseText);
93 94
                        $certificate_regeneration_status.text(gettext(response.message)).addClass('message');
                    } catch (error) {
95 96
                        $certificate_regeneration_status.
                            text(gettext('Error while regenerating certificates. Please try again.')).
97
                            addClass('message');
98 99 100 101 102 103 104 105
                    }
                }
            });
        });
    };

    // Call onCertificatesReady on document.ready event
    $(onCertificatesReady);
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

    var Certificates = (function() {
        function Certificates($section) {
            $section.data('wrapper', this);
            this.instructor_tasks = new window.InstructorDashboard.util.PendingInstructorTasks($section);
        }

        Certificates.prototype.onClickTitle = function() {
            return this.instructor_tasks.task_poller.start();
        };

        Certificates.prototype.onExit = function() {
            return this.instructor_tasks.task_poller.stop();
        };
        return Certificates;
Eric Fischer committed
121
    }());
122 123 124

    _.defaults(window, {
        InstructorDashboard: {}
125
    });
126 127 128 129 130 131 132 133

    _.defaults(window.InstructorDashboard, {
        sections: {}
    });

    _.defaults(window.InstructorDashboard.sections, {
        Certificates: Certificates
    });
Eric Fischer committed
134
}($, gettext, _));