<commit_before>define(function (require) {
    var $ = require('jquery');
    var _ = require('underscore');
    var Backbone = require('util/backbone-helper');
    var Hbs = require('handlebars');
    var Mn = require('backbone.marionette');
    var Q = require('q');
    var Radio = require('backbone.radio');
    var pageChannel = Radio.channel('page');
    var courseChannel = Radio.channel('course');
    var template = require('text!ctemplates/addNewAssignmentView.hbs');
    var alertTemplate = require('text!ctemplates/alert-block.hbs');

    var Course = require('course/model/course');
    //var currentAssignment;

    var NewAssignmentView = Mn.ItemView.extend({
        tagName: 'div',
        className: 'new-assignment modal-dialog  modal-lg',
        template: Hbs.compile(template),
        ui: {
            //'newAssignmentButton' : '.newAssignmentButton',
            'save' : '.save',
            'cancel' : '.cancel',
            'name' : '.name',
            'weight' : '.weight',
            'totalScore' : '.total-score',
            'category' : '.category',
            'error': '.error'
        },
        /**
         * Hides the dialog on initial load
         */

        events : {
            'click @ui.save' :  'saveNewAssignment',
            'click @ui.cancel' :  'closeNewAssignment'
        },
        traverseCat : function(indent, o) {

            for (i in o) {
                if (o[i] &&
                    typeof o[i].tree === 'function'
                    && typeof(o[i].tree()) == "object") {
                    this.categoryList.push({
                        name: indent + " " + o[i].get('name'),
                        cid: o[i].cid,
                        path: o[i].get('path')
                    });
                    this.traverseCat(indent + "---", o[i].tree());
                }
            }

        },

        initialize : function() {
            this.model = courseChannel.request('current:course');
            this.alertTemplate = Hbs.compile(alertTemplate);
        },
        onShow : function() {
            var categories = this.model.categories;
            console.log(categories)
            var categoryTree = categories.tree();
            this.categoryList = [];
            var self = this;

            self.traverseCat("", categoryTree);

            var optionString;
            this.categoryList.forEach(function(c) {

                optionString = '<option value="' + c.path +'" >'+ c.name + '</option>';
                $('#new-assignment-category').append(optionString);
            });
        },
        saveNewAssignment : function() {
            var self = this;
            var ui = this.ui;
            var newAssignment = {};
            newAssignment.course = this.model.get('colloquialUrl');

            if(ui.name.val() == null || ui.name.val().length === 0) {
                self.ui.error.html(self.alertTemplate({
                        message: "Invalid assignment name"
                    }));
                    
                return;
            }
            else {
                newAssignment.name = ui.name.val();
            }

            if(ui.weight.val() == null || ui.weight.val().length === 0 || isNaN(ui.weight.val())) {
                self.ui.error.html(self.alertTemplate({
                    message: "Weight must be a number"
                }));
                    
                return;
            }
            else {
                newAssignment.weight = ui.weight.val();
            }

            

            //ADDING THESE LINES CAUSES ERROR IN CONSOLE, EITHER CANNOT READ PROPERTY OR IS NOT A FUNCTION

            if(ui.totalScore.val() == null || ui.totalScore.val().length === 0 || isNaN(ui.totalScore.val())) {
                self.ui.error.html(self.alertTemplate({
                    message: "Total score must be a number"
                }));
            }
            else {
                newAssignment.totalScore = ui.totalScore.val();<commit_msg>validation for new assignment weight<commit_after>define(function (require) {
    var $ = require('jquery');
    var _ = require('underscore');
    var Backbone = require('util/backbone-helper');
    var Hbs = require('handlebars');
    var Mn = require('backbone.marionette');
    var Q = require('q');
    var Radio = require('backbone.radio');
    var pageChannel = Radio.channel('page');
    var courseChannel = Radio.channel('course');
    var template = require('text!ctemplates/addNewAssignmentView.hbs');
    var alertTemplate = require('text!ctemplates/alert-block.hbs');

    var Course = require('course/model/course');
    //var currentAssignment;

    var NewAssignmentView = Mn.ItemView.extend({
        tagName: 'div',
        className: 'new-assignment modal-dialog  modal-lg',
        template: Hbs.compile(template),
        ui: {
            //'newAssignmentButton' : '.newAssignmentButton',
            'save' : '.save',
            'cancel' : '.cancel',
            'name' : '.name',
            'weight' : '.weight',
            'totalScore' : '.total-score',
            'category' : '.category',
            'error': '.error'
        },
        /**
         * Hides the dialog on initial load
         */

        events : {
            'click @ui.save' :  'saveNewAssignment',
            'click @ui.cancel' :  'closeNewAssignment'
        },
        traverseCat : function(indent, o) {

            for (i in o) {
                if (o[i] &&
                    typeof o[i].tree === 'function'
                    && typeof(o[i].tree()) == "object") {
                    this.categoryList.push({
                        name: indent + " " + o[i].get('name'),
                        cid: o[i].cid,
                        path: o[i].get('path')
                    });
                    this.traverseCat(indent + "---", o[i].tree());
                }
            }

        },

        initialize : function() {
            this.model = courseChannel.request('current:course');
            this.alertTemplate = Hbs.compile(alertTemplate);
        },
        onShow : function() {
            var categories = this.model.categories;
            console.log(categories)
            var categoryTree = categories.tree();
            this.categoryList = [];
            var self = this;

            self.traverseCat("", categoryTree);

            var optionString;
            this.categoryList.forEach(function(c) {

                optionString = '<option value="' + c.path +'" >'+ c.name + '</option>';
                $('#new-assignment-category').append(optionString);
            });
        },
        saveNewAssignment : function() {
            var self = this;
            var ui = this.ui;
            var newAssignment = {};
            newAssignment.course = this.model.get('colloquialUrl');

            if(ui.name.val() == null || ui.name.val().length === 0) {
                self.ui.error.html(self.alertTemplate({
                        message: "Invalid assignment name"
                    }));
                    
                return;
            }
            else {
                newAssignment.name = ui.name.val();
            }

            if(ui.weight.val() == null || ui.weight.val().length === 0 || isNaN(ui.weight.val())
                || ui.weight.val() < 0 || ui.weight.val() > 1) {
                self.ui.error.html(self.alertTemplate({
                    message: "Weight must be a number between 0 and 1"
                }));
                    
                return;
            }
            else {
                newAssignment.weight = ui.weight.val();
            }

            
            if(ui.totalScore.val() == null || ui.totalScore.val().length === 0 || isNaN(ui.totalScore.val())) {
                self.ui.error.html(self.alertTemplate({
                    message: "Total score must be a number"
                }));
            }
            else {
                newAssignment.totalScore = ui.totalScore.val();
{'fraction_non_alphanumeric': 0.1417705735660848, 'fraction_numerical': 0.0012468827930174563, 'mean_word_length': 1.4446814995428223, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, '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': '6538860', 'n_tokens_mistral': 2148, 'n_tokens_neox': 2085, 'n_words': 410}