<commit_before>var templates = [
    "root/externallib/text!root/plugins/forum/view.html",
    "root/externallib/text!root/plugins/forum/discussion.html",
    "root/externallib/text!root/plugins/forum/discussions.html",
    "root/externallib/text!root/plugins/forum/attachments.html",
    "root/externallib/text!root/plugins/forum/worker.js"
];

define(templates, function (filesTpl, discussionTpl, discussionsTpl, attachmentsTpl, workerCode) {
    var plugin = {
        settings: {
            name: "forum",
            type: "mod",
            component: "mod_forum",
            lang: {
                component: "core"
            }
        },

        storage: {
            "forum_file": {type: "model"},
            "forum_files": {type: "collection", model: "forum_file"},
            "forum_sync": {type: "model"},
            "forum_syncs": {type: "collection", model: "forum_sync"}
        },

        routes: [
            ["forum/view/:courseId/:cmid/:page", "view_forum", "viewForum"],
        ],

        // Sync function, every 2 hours (time is in millisecs).
        sync: {
            handler: function() {
                // We need to define here a function since MM.plugins.forum is not yet defined.
                MM.plugins.forum._syncForums();
            },
            time: 7200000
        },

        wsPrefix: "",

        sectionsCache: {},

         /**
         * Determines is the plugin is visible.
         * It may check Moodle remote site version, device OS, device type, etc...
         * This function is called when a alink to a plugin functinality is going to be rendered.
         *
         * @return {bool} True if the plugin is visible for the site and device
         */
        isPluginVisible: function() {
            // First check core services.
            var visible =   MM.util.wsAvailable('mod_forum_get_forums_by_courses') &&
                            MM.util.wsAvailable('mod_forum_get_forum_discussions_paginated') &&
                            MM.util.wsAvailable('mod_forum_get_forum_discussion_posts');

            // Fallback to local_mobile plugin ones.
            if (!visible) {
                visible =   MM.util.wsAvailable('local_mobile_mod_forum_get_forums_by_courses') &&
                            MM.util.wsAvailable('local_mobile_mod_forum_get_forum_discussions_paginated') &&
                            MM.util.wsAvailable('local_mobile_mod_forum_get_forum_discussion_posts');

                if (visible) {
                    MM.plugins.forum.wsPrefix = "local_mobile_";
                }
            }
            return visible;
        },

        render: function(courseId, sectionId, section, module) {
            var data = {
                "courseId": courseId,
                "sectionId": sectionId,
                "section": section,
                "module": module
            };
            // Store the section name.
            MM.plugins.forum.sectionsCache[module.contentid] = MM.util.formatText(section.name);

            return MM.tpl.render(MM.plugins.forum.templates.view.html, data);
        },

        perPage: 20,

        /**
         * Display a forum and discussions
         * @param  {Number} cmid The course module number id
         *
         */
        viewForum: function(courseId, cmid, page) {
            // Loading ....
            $("#info-" + cmid, "#panel-right").attr("src", "img/loadingblack.gif");

            // First, load the complete information of forums in this course.
            var params = {
                "courseids[0]": courseId
            };

            MM.moodleWSCall(MM.plugins.forum.wsPrefix + "mod_forum_get_forums_by_courses",
                params,
                function(forums) {
                    var currentForum;
                    _.each(forums, function(forum) {
                        if (forum.cmid == cmid) {
                            currentForum = forum;
                        }
                    });
                    if (currentForum) {
                        MM.plugins.forum._showDiscussions(currentForum, page);
                    }
                },
                null,
                function (error) {
                    $("#info-" + cmid, "#panel-right").attr("src", "img/info.png");
                    MM.popErrorMessage(error);
                }
            );
        },

        /**
         * Display discussions of a forum
         * @param  {Object} forum Forum object
         *
         */
        _showDiscussions: function(forum, page) {

            var params = {
                "forumid": forum.id,        // Forum module instance id.
                "sortby":  "timemodified",
                "sortdirection":  "DESC",
                "page": page,
                "perpage": MM.plugins.forum.perPage
            };

            MM.moodleWSCall(MM.plugins.forum.wsPrefix + "mod_forum_get_forum_discussions_paginated",
                params,
                // Success callback.
                function(discussions) {
                    // Stops loading...
                    $("#info-" + forum.cmid, "#panel-right").attr("src", "img/info.png");
                    var siteId = MM.config.current_site.id;

                    var syncStatus = "";
                    if (MM.db.get('forum_syncs', siteId + "-" + forum.cmid)) {
                        syncStatus = 'checked="checked"';
                    }

                    var sectionName = "";
                    if (MM.plugins.forum.sectionsCache[forum.cmid]) {
                        sectionName = MM.plugins.forum.sectionsCache[forum.cmid];
                    }

                    var pageTitle = MM.util.formatText(forum.name);
                    var data = {
                        "page": page,
                        "perpage": MM.plugins.forum.perPage,
                        "forum": forum,
                        "discussions": discussions.discussions,
                        "syncStatus": syncStatus,
                        "sectionName": sectionName
                    };

                    MM.plugins.forum.discussionsCache = discussions.discussions;

                    var html = MM.tpl.render(MM.plugins.forum.templates.discussions.html, data);
                    MM.panels.show("right", html, {title: pageTitle});

                    // Handlers for view complete discussions and posts.
                    $(".subject.toogler").on(MM.clickType, function(e) {
                        e.preventDefault();
                        if ($(this).hasClass("discussion-loaded")) {
                            $(this).parent().find(".discussion-body").toggle();
                            return;
                        }

                        var discussionId = $(this).data("discussionid");
                        $(this).addClass("discussion-loaded");
                        $(this).parent().find(".discussion-body").html('<div class="centered"><img src="img/loading.gif"></div>');
                        MM.plugins.forum._showDiscussion(discussionId, forum.course);
                    });

                    // Handler for sync.
                    if (MM.util.WebWorkersSupported()) {
                        $("#keepsynch").bind("change", function(e) {
                            var uniqueId = siteId + "-" + $(this).data("cmid");

                            if ($(this).prop("checked")) {
                                var el = {
                                    id: uniqueId,
                                    forumid: $(this).data("forumid"),
                                    cmid: $(this).data("cmid"),
                                    site: siteId
                                };
                                MM.db.insert("forum_syncs", el);
                            } else {<commit_msg>MOBILE-744 forum: Tap or click in the number of replies should display the posts for the discussion<commit_after>var templates = [
    "root/externallib/text!root/plugins/forum/view.html",
    "root/externallib/text!root/plugins/forum/discussion.html",
    "root/externallib/text!root/plugins/forum/discussions.html",
    "root/externallib/text!root/plugins/forum/attachments.html",
    "root/externallib/text!root/plugins/forum/worker.js"
];

define(templates, function (filesTpl, discussionTpl, discussionsTpl, attachmentsTpl, workerCode) {
    var plugin = {
        settings: {
            name: "forum",
            type: "mod",
            component: "mod_forum",
            lang: {
                component: "core"
            }
        },

        storage: {
            "forum_file": {type: "model"},
            "forum_files": {type: "collection", model: "forum_file"},
            "forum_sync": {type: "model"},
            "forum_syncs": {type: "collection", model: "forum_sync"}
        },

        routes: [
            ["forum/view/:courseId/:cmid/:page", "view_forum", "viewForum"],
        ],

        // Sync function, every 2 hours (time is in millisecs).
        sync: {
            handler: function() {
                // We need to define here a function since MM.plugins.forum is not yet defined.
                MM.plugins.forum._syncForums();
            },
            time: 7200000
        },

        wsPrefix: "",

        sectionsCache: {},

         /**
         * Determines is the plugin is visible.
         * It may check Moodle remote site version, device OS, device type, etc...
         * This function is called when a alink to a plugin functinality is going to be rendered.
         *
         * @return {bool} True if the plugin is visible for the site and device
         */
        isPluginVisible: function() {
            // First check core services.
            var visible =   MM.util.wsAvailable('mod_forum_get_forums_by_courses') &&
                            MM.util.wsAvailable('mod_forum_get_forum_discussions_paginated') &&
                            MM.util.wsAvailable('mod_forum_get_forum_discussion_posts');

            // Fallback to local_mobile plugin ones.
            if (!visible) {
                visible =   MM.util.wsAvailable('local_mobile_mod_forum_get_forums_by_courses') &&
                            MM.util.wsAvailable('local_mobile_mod_forum_get_forum_discussions_paginated') &&
                            MM.util.wsAvailable('local_mobile_mod_forum_get_forum_discussion_posts');

                if (visible) {
                    MM.plugins.forum.wsPrefix = "local_mobile_";
                }
            }
            return visible;
        },

        render: function(courseId, sectionId, section, module) {
            var data = {
                "courseId": courseId,
                "sectionId": sectionId,
                "section": section,
                "module": module
            };
            // Store the section name.
            MM.plugins.forum.sectionsCache[module.contentid] = MM.util.formatText(section.name);

            return MM.tpl.render(MM.plugins.forum.templates.view.html, data);
        },

        perPage: 20,

        /**
         * Display a forum and discussions
         * @param  {Number} cmid The course module number id
         *
         */
        viewForum: function(courseId, cmid, page) {
            // Loading ....
            $("#info-" + cmid, "#panel-right").attr("src", "img/loadingblack.gif");

            // First, load the complete information of forums in this course.
            var params = {
                "courseids[0]": courseId
            };

            MM.moodleWSCall(MM.plugins.forum.wsPrefix + "mod_forum_get_forums_by_courses",
                params,
                function(forums) {
                    var currentForum;
                    _.each(forums, function(forum) {
                        if (forum.cmid == cmid) {
                            currentForum = forum;
                        }
                    });
                    if (currentForum) {
                        MM.plugins.forum._showDiscussions(currentForum, page);
                    }
                },
                null,
                function (error) {
                    $("#info-" + cmid, "#panel-right").attr("src", "img/info.png");
                    MM.popErrorMessage(error);
                }
            );
        },

        /**
         * Display discussions of a forum
         * @param  {Object} forum Forum object
         *
         */
        _showDiscussions: function(forum, page) {

            var params = {
                "forumid": forum.id,        // Forum module instance id.
                "sortby":  "timemodified",
                "sortdirection":  "DESC",
                "page": page,
                "perpage": MM.plugins.forum.perPage
            };

            MM.moodleWSCall(MM.plugins.forum.wsPrefix + "mod_forum_get_forum_discussions_paginated",
                params,
                // Success callback.
                function(discussions) {
                    // Stops loading...
                    $("#info-" + forum.cmid, "#panel-right").attr("src", "img/info.png");
                    var siteId = MM.config.current_site.id;

                    var syncStatus = "";
                    if (MM.db.get('forum_syncs', siteId + "-" + forum.cmid)) {
                        syncStatus = 'checked="checked"';
                    }

                    var sectionName = "";
                    if (MM.plugins.forum.sectionsCache[forum.cmid]) {
                        sectionName = MM.plugins.forum.sectionsCache[forum.cmid];
                    }

                    var pageTitle = MM.util.formatText(forum.name);
                    var data = {
                        "page": page,
                        "perpage": MM.plugins.forum.perPage,
                        "forum": forum,
                        "discussions": discussions.discussions,
                        "syncStatus": syncStatus,
                        "sectionName": sectionName
                    };

                    MM.plugins.forum.discussionsCache = discussions.discussions;

                    var html = MM.tpl.render(MM.plugins.forum.templates.discussions.html, data);
                    MM.panels.show("right", html, {title: pageTitle});

                    // Handlers for view complete discussions and posts.
                    $(".subject.toogler").on(MM.clickType, function(e) {
                        e.preventDefault();
                        if ($(this).hasClass("discussion-loaded")) {
                            $(this).parent().find(".discussion-body").toggle();
                            return;
                        }

                        var discussionId = $(this).data("discussionid");
                        $(this).addClass("discussion-loaded");
                        $(this).parent().find(".discussion-body").html('<div class="centered"><img src="img/loading.gif"></div>');
                        MM.plugins.forum._showDiscussion(discussionId, forum.course);
                    });

                    // Handlers for post-info (replies).
                    $(".post-info").on(MM.clickType, function(e) {
                        var parent = $(this).parent().find(".subject.toogler");
                        if (parent) {
                            parent.trigger(MM.clickType);
                        }
                    });

                    // Handler for sync.
                    if (MM.util.WebWorkersSupported()) {
                        $("#keepsynch").bind("change", function(e) {
                            var uniqueId = siteId + "-" + $(this).data("cmid");

                            if ($(this).prop("checked")) {
                                var el = {
                                    id: uniqueId,
                                    forumid: $(this).data("forumid"),
                                    cmid: $(this).data("cmid"),
                                    site: siteId
                                };
                                MM.db.insert("forum_syncs", el);
                            } else {
{'fraction_non_alphanumeric': 0.11638866497178645, 'fraction_numerical': 0.001550195324610901, 'mean_word_length': 1.2712294043092522, 'pattern_counts': {'":': 40, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7352546', 'n_tokens_mistral': 4375, 'n_tokens_neox': 3924, 'n_words': 774}