<commit_before>define(['jquery', 'backbone', 'underscore', 'hbs!courses/templates/course', 'hbs!courses/templates/auth_status', 'leaflet', 'moxie.conf'], 
    function($, Backbone, _, courseTemplate, authTemplate, L, MoxieConf){
        var CourseView = Backbone.View.extend({

            initialize: function() {
                _.bindAll(this);
            },

            events: {
                'click .bookLink': "bookCourse"
            },

            render: function() {
                // Get course information
                $.ajax({
                    url: MoxieConf.urlFor('course_id') + this.options.id,
                    dataType: 'json'
                }).success([this.renderCourse, this.checkAuthorization])
                .error(this.handleError);
                return this;
            },

            renderCourse: function(data) {
                this.$el.html(courseTemplate(data));
            },

            renderAuthRequired: function() {
                data = {};
                data.authorized = false;
                data.authorization_url = this.authorization_url;
                this.$('#authStatus').html(authTemplate(data));
            },

            checkAuthorization: function(data) {
                this.authorization_url = MoxieConf.urlFor('courses_auth_authorize')+ '?callback_uri=' + window.escape(window.location.href);
                // Check if the user has to be verified (back from oauth auth)
                if (this.options.params && this.options.params.oauth_verifier) {
                    $.ajax({
                        url: MoxieConf.urlFor('courses_auth_verify'),
                        data: {'oauth_verifier': this.options.params.oauth_verifier},
                        dataType: 'json',
                        xhrFields: {
                            withCredentials: true
                        }
                    }).success(this.verifyAuth).error(this.handleError);
                } else {
                    // Check if the user is authorized
                    $.ajax({
                        url: MoxieConf.urlFor('courses_auth_authorized'),
                        dataType: 'json',
                        xhrFields: {
                            withCredentials: true
                        }
                    }).success(this.verifyAuth).error(this.handleError);
                    this.renderAuthRequired();
                }
            },

            bookCourse: function(ev) {
                id = this.$(ev.currentTarget).attr("data-id");

                url = MoxieConf.urlFor('presentation_id') + id + "/book";

                data = {'jsj': 'ksjdjf', 'sjsj': 'ksjsj' };

                $.ajax({
                    url: url,
                    data: JSON.stringify(data),
                    processData: false,
                    dataType: 'json',
                    contentType: 'application/json',
                    type: 'POST',
                    xhrFields: {
                        withCredentials: true
                    }
                }).success(this.callbackBookCourse)
                .error(this.handleError);

            },

            callbackBookCourse: function(data) {
                alert("Course booked!");
            },

            verifyAuth: function(data) {
                data.authorization_url = this.authorization_url;
                this.$('#authStatus').html(authTemplate(data));
                if(data.authorized === false) {
                    $('.bookable').hide();
                } else {<commit_msg>Couple of todo's here, should be using _links and posting actual supervisor information.<commit_after>define(['jquery', 'backbone', 'underscore', 'hbs!courses/templates/course', 'hbs!courses/templates/auth_status', 'leaflet', 'moxie.conf'], 
    function($, Backbone, _, courseTemplate, authTemplate, L, MoxieConf){
        var CourseView = Backbone.View.extend({

            initialize: function() {
                _.bindAll(this);
            },

            events: {
                'click .bookLink': "bookCourse"
            },

            render: function() {
                // Get course information
                $.ajax({
                    url: MoxieConf.urlFor('course_id') + this.options.id,
                    dataType: 'json'
                }).success([this.renderCourse, this.checkAuthorization])
                .error(this.handleError);
                return this;
            },

            renderCourse: function(data) {
                this.$el.html(courseTemplate(data));
            },

            renderAuthRequired: function() {
                data = {};
                data.authorized = false;
                data.authorization_url = this.authorization_url;
                this.$('#authStatus').html(authTemplate(data));
            },

            checkAuthorization: function(data) {
                this.authorization_url = MoxieConf.urlFor('courses_auth_authorize')+ '?callback_uri=' + window.escape(window.location.href);
                // Check if the user has to be verified (back from oauth auth)
                if (this.options.params && this.options.params.oauth_verifier) {
                    $.ajax({
                        url: MoxieConf.urlFor('courses_auth_verify'),
                        data: {'oauth_verifier': this.options.params.oauth_verifier},
                        dataType: 'json',
                        xhrFields: {
                            withCredentials: true
                        }
                    }).success(this.verifyAuth).error(this.handleError);
                } else {
                    // Check if the user is authorized
                    $.ajax({
                        url: MoxieConf.urlFor('courses_auth_authorized'),
                        dataType: 'json',
                        xhrFields: {
                            withCredentials: true
                        }
                    }).success(this.verifyAuth).error(this.handleError);
                    this.renderAuthRequired();
                }
            },

            bookCourse: function(ev) {
                // TODO: This should really be using the _links
                // However they're on the embedded elements in the response, could be tricky?
                var id = this.$(ev.currentTarget).attr("data-id");
                var url = MoxieConf.urlFor('presentation_id') + id + "/booking";
                // TODO: We need to send supervisor credentials
                data = {'jsj': 'ksjdjf', 'sjsj': 'ksjsj' };

                $.ajax({
                    url: url,
                    data: JSON.stringify(data),
                    processData: false,
                    dataType: 'json',
                    contentType: 'application/json',
                    type: 'POST',
                    xhrFields: {
                        withCredentials: true
                    }
                }).success(this.callbackBookCourse)
                .error(this.handleError);

            },

            callbackBookCourse: function(data) {
                alert("Course booked!");
            },

            verifyAuth: function(data) {
                data.authorization_url = this.authorization_url;
                this.$('#authStatus').html(authTemplate(data));
                if(data.authorized === false) {
                    $('.bookable').hide();
                } else {
{'fraction_non_alphanumeric': 0.11233539371136791, 'fraction_numerical': 0.0, 'mean_word_length': 1.2775397796817625, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7367427', 'n_tokens_mistral': 1875, 'n_tokens_neox': 1720, 'n_words': 329}