<commit_before>/*jslint vars: false, browser: true, nomen: true */
/*global MOOC: true, Backbone, $, _ */

// Copyright 2012 Rooter Analysis S.L.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

if (_.isUndefined(window.MOOC)) {
    window.MOOC = {};
}

MOOC.views = {};

MOOC.views.capitalize = function (text) {
    "use strict";
    return text.charAt(0).toUpperCase() + text.slice(1);
};

MOOC.views.Unit = Backbone.View.extend({

    initialize: function () {
        "use strict";
        var unit = this.model, collection = unit.get('knowledgeQuantumList');
        this._kqViews = collection.map(function (kq) {
            var kqView = MOOC.views.kqViews[kq.id], reviews = null;
            if (_.isUndefined(kqView)) {
                if (kq.get('peer_review_assignment') !== null) {
                    reviews = unit.getPeerReviewReviewsForKq(kq);
                }

                MOOC.views.kqViews[kq.id] = new MOOC.views.KnowledgeQuantum({
                    model: kq,
                    id: "kq" + kq.id,
                    reviews: reviews
                });
                kqView = MOOC.views.kqViews[kq.id];
            }
            return kqView;
        });
    },

    render: function () {
        "use strict";
        var html,
            progress,
            helpText;

        this.$el.parent().children().removeClass("active");
        this.$el.addClass("active");

        $("#unit-title").html(this.model.get("title"));

        html = '<div class="progress progress-info" title="' + MOOC.trans.progress.completed + '"><div class="bar completed" style="width: 0%;"></div></div>';
        html += '<div class="progress progress-success" title="' + MOOC.trans.progress.correct + '"><div class="bar correct" style="width: 0%;"></div></div>';
        $("#unit-progress-bar").html(html);

        progress = this.model.calculateProgress({ completed: true });
        helpText = "<div><span>" + Math.round(progress) + "% " + MOOC.trans.progress.completed + "</span></div>";
        $("#unit-progress-bar").find("div.bar.completed").css("width", progress + "%");

        progress = this.model.calculateProgress({ completed: true, correct: true });
        helpText += "<div><span>" + Math.round(progress) + "% " + MOOC.trans.progress.correct + "</span></div>";
        $("#unit-progress-bar").find("div.bar.correct").css("width", progress + "%");

        $("#unit-progress-text").html(helpText);


        $("#unit-kqs").empty();
        _(this._kqViews).each(function (kqView) {
            $("#unit-kqs").append(kqView.render().el);
        });

        return this;
    }
});

MOOC.views.unitViews = {};

MOOC.views.KnowledgeQuantum = Backbone.View.extend({
    tagName: "li",

    initialize: function (options) {
        "use strict";
        var pra = this.model.get('peerReviewAssignmentInstance');
        this.reviews = options.reviews;

        this._prrViews = _.map(this.reviews, function (review, index) {
            var prrView = MOOC.views.peerReviewReviewViews[review.id];
            if (_.isUndefined(prrView)) {

                MOOC.views.peerReviewReviewViews[review.id] = new MOOC.views.PeerReviewReview({
                    model: review,
                    index: index,
                    peerReviewAssignment: pra
                });
                prrView = MOOC.views.peerReviewReviewViews[review.id];
            }
            return prrView;
        });
    },

    render_badge: function (minimum_reviewers) {
        "use strict";
        var icon = '', score = 0, badge_class = '';

        if (this.reviews.length < minimum_reviewers) {
            icon = '<i class="icon-exclamation-sign icon-white" title="' + MOOC.trans.progress.score_dont_apply + '"></i> ';
        }

        badge_class = (score >= 2.5) ? 'success' : 'important';

        return '<span class="badge badge-' + badge_class + ' pull-right">' + icon + score + '</span>';
    },

    render: function () {
        "use strict";
        var html = [
                "<b>" + this.model.truncateTitle(40) + "</b>"<commit_msg>Compute the score of a peer review assignment for a user<commit_after>/*jslint vars: false, browser: true, nomen: true */
/*global MOOC: true, Backbone, $, _ */

// Copyright 2012 Rooter Analysis S.L.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

if (_.isUndefined(window.MOOC)) {
    window.MOOC = {};
}

MOOC.views = {};

MOOC.views.capitalize = function (text) {
    "use strict";
    return text.charAt(0).toUpperCase() + text.slice(1);
};

MOOC.views.Unit = Backbone.View.extend({

    initialize: function () {
        "use strict";
        var unit = this.model, collection = unit.get('knowledgeQuantumList');
        this._kqViews = collection.map(function (kq) {
            var kqView = MOOC.views.kqViews[kq.id], reviews = null;
            if (_.isUndefined(kqView)) {
                if (kq.get('peer_review_assignment') !== null) {
                    reviews = unit.getPeerReviewReviewsForKq(kq);
                }

                MOOC.views.kqViews[kq.id] = new MOOC.views.KnowledgeQuantum({
                    model: kq,
                    id: "kq" + kq.id,
                    reviews: reviews
                });
                kqView = MOOC.views.kqViews[kq.id];
            }
            return kqView;
        });
    },

    render: function () {
        "use strict";
        var html,
            progress,
            helpText;

        this.$el.parent().children().removeClass("active");
        this.$el.addClass("active");

        $("#unit-title").html(this.model.get("title"));

        html = '<div class="progress progress-info" title="' + MOOC.trans.progress.completed + '"><div class="bar completed" style="width: 0%;"></div></div>';
        html += '<div class="progress progress-success" title="' + MOOC.trans.progress.correct + '"><div class="bar correct" style="width: 0%;"></div></div>';
        $("#unit-progress-bar").html(html);

        progress = this.model.calculateProgress({ completed: true });
        helpText = "<div><span>" + Math.round(progress) + "% " + MOOC.trans.progress.completed + "</span></div>";
        $("#unit-progress-bar").find("div.bar.completed").css("width", progress + "%");

        progress = this.model.calculateProgress({ completed: true, correct: true });
        helpText += "<div><span>" + Math.round(progress) + "% " + MOOC.trans.progress.correct + "</span></div>";
        $("#unit-progress-bar").find("div.bar.correct").css("width", progress + "%");

        $("#unit-progress-text").html(helpText);


        $("#unit-kqs").empty();
        _(this._kqViews).each(function (kqView) {
            $("#unit-kqs").append(kqView.render().el);
        });

        return this;
    }
});

MOOC.views.unitViews = {};

MOOC.views.KnowledgeQuantum = Backbone.View.extend({
    tagName: "li",

    initialize: function (options) {
        "use strict";
        var pra = this.model.get('peerReviewAssignmentInstance');
        this.reviews = options.reviews;

        this._prrViews = _.map(this.reviews, function (review, index) {
            var prrView = MOOC.views.peerReviewReviewViews[review.id];
            if (_.isUndefined(prrView)) {

                MOOC.views.peerReviewReviewViews[review.id] = new MOOC.views.PeerReviewReview({
                    model: review,
                    index: index,
                    peerReviewAssignment: pra
                });
                prrView = MOOC.views.peerReviewReviewViews[review.id];
            }
            return prrView;
        });
    },

    compute_score: function () {
        "use strict";
        var sum = _.reduce(this.reviews, function (memo, review) {
            return memo + review.get('score');
        }, 0),
            n = this.reviews.length;
        if (n > 0) {
            return sum / n;
        } else {
            return 0;
        }
    },

    render_badge: function (minimum_reviewers) {
        "use strict";
        var icon = '', score = 0, badge_class = '';

        if (this.reviews.length < minimum_reviewers) {
            icon = '<i class="icon-exclamation-sign icon-white" title="' + MOOC.trans.progress.score_dont_apply + '"></i> ';
        }

        score = this.compute_score();

        badge_class = (score >= 2.5) ? 'success' : 'important';

        return '<span class="badge badge-' + badge_class + ' pull-right">' + icon + score + '</span>';
    },

    render: function () {
        "use strict";
        var html = [
                "<b>" + this.model.truncateTitle(40) + "</b>"
{'fraction_non_alphanumeric': 0.15859399518778114, 'fraction_numerical': 0.0038706977717334447, 'mean_word_length': 2.218855218855219, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4872602', 'n_tokens_mistral': 2871, 'n_tokens_neox': 2760, 'n_words': 620}