{{!
    This file is part of Moodle - http://moodle.org/

    Moodle is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Moodle is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
}}
{{!
    @template mod_forum/discussion_list

    Template to display the list of forum discussions.

    Classes required for JS:
    * none

    Data attributes required for JS:
    * none

    Context variables required for this template:
    * TODO

    Example context (json):
    {
    }
}}
<div id="discussion-list-{{uniqid}}" {{!
    }}data-contextid="{{contextid}}" {{!
    }}data-cmid="{{cmid}}" {{!
    }}data-name="{{forum.name}}" {{!
    }}data-group="{{forum.groupid}}" {{!
    }}data-grading-component="{{gradingcomponent}}" {{!
    }}data-grading-component-subtype="{{gradingcomponentsubtype}}" {{!
    }}data-gradable-itemtype="forum" {{!
    }}{{#firstgradeduserid}}data-initialuserid="{{firstgradeduserid}}" {{/firstgradeduserid}}{{!
    }}>

    {{#notifications}}
        {{> core/notification}}
    {{/notifications}}

    {{{groupchangemenu}}}


    <div class="p-t-1 p-b-1">
        {{#forum.capabilities.create}}
            <a class="btn dbxshad btn-md btn-thm white ccn-mod-forum-btn" data-toggle="collapse" href="#collapseAddForm">
                {{$discussion_create_text}}
                    {{#str}}addanewdiscussion, forum{{/str}}
                {{/discussion_create_text}}
            </a>
        {{/forum.capabilities.create}}
        {{^forum.capabilities.create}}
            {{#forum.capabilities.selfenrol}}
                <div class="p-t-1 p-b-1">
                    <a class="btn dbxshad btn-md btn-thm white ccn-mod-forum-btn" href="{{forum.urls.create}}">
                        {{$discussion_create_text}}
                            {{#str}}addanewdiscussion, forum{{/str}}
                        {{/discussion_create_text}}
                    </a>
                </div>
            {{/forum.capabilities.selfenrol}}
        {{/forum.capabilities.create}}
        {{#forum.capabilities.grade}}
            {{#forum.state.gradingenabled}}
                {{> mod_forum/grades/grade_button }}
            {{/forum.state.gradingenabled}}
        {{/forum.capabilities.grade}}
        {{^forum.capabilities.grade}}
            {{#forum.state.gradingenabled}}
                {{> mod_forum/grades/view_grade_button }}
            {{/forum.state.gradingenabled}}
        {{/forum.capabilities.grade}}
    </div>
    {{#forum.capabilities.create}}
        <div class="collapse m-t-1 p-b-1" id="collapseAddForm">
            {{{newdiscussionhtml}}}
        </div>
    {{/forum.capabilities.create}}

    {{#state.hasdiscussions}}
        {{$discussion_top_pagination}}
            {{{ pagination }}}
        {{/discussion_top_pagination}}
        {{$discussion_list_output}}
            <span id="discussion-table-description-{{uniqid}}" class="sr-only">
                {{#str}} showingcountoftotaldiscussions, mod_forum, {"count": "{{visiblediscussioncount}}", "total":"{{totaldiscussioncount}}"} {{/str}}
            </span>
            <div class="ui_kit_table">
            <table
                class="table discussion-list"
                aria-label='{{#str}} showingcountoftotaldiscussions, mod_forum, {"count": "{{visiblediscussioncount}}", "total":"{{totaldiscussioncount}}"} {{/str}}'
                aria-describedby="discussion-table-description-{{uniqid}}"
            >
                {{$discussion_list_header}}
                <thead class="thead-light">
                    <tr>
                        <th scope="col">
                            <span class="accesshide">{{#str}}status{{/str}}</span>
                        </th>
                        <th scope="col" class="p-l-0">
                            {{#state.sortorder.isdiscussiondesc}}
                                <a href="{{{forum.urls.sortdiscussionasc}}}" aria-label="{{#str}}discussionlistsortbydiscussionasc, mod_forum{{/str}}">{{#str}}discussion, mod_forum{{/str}}</a> <span class="ccn-text-primary">{{#pix}}t/downlong, core, {{#str}}desc, core{{/str}}{{/pix}}</span>
                            {{/state.sortorder.isdiscussiondesc}}
                            {{#state.sortorder.isdiscussionasc}}
                                <a href="{{{forum.urls.sortdiscussiondesc}}}" aria-label="{{#str}}discussionlistsortbydiscussiondesc, mod_forum{{/str}}">{{#str}}discussion, mod_forum{{/str}}</a> <span class="ccn-text-primary">{{#pix}}t/uplong, core, {{#str}}asc, core{{/str}}{{/pix}}</span>
                            {{/state.sortorder.isdiscussionasc}}
                            {{^state.sortorder.isdiscussiondesc}}
                                {{^state.sortorder.isdiscussionasc}}
                                    <a href="{{{forum.urls.sortdiscussiondesc}}}" aria-label="{{#str}}discussionlistsortbydiscussiondesc, mod_forum{{/str}}">{{#str}}discussion, mod_forum{{/str}}</a>
                                {{/state.sortorder.isdiscussionasc}}
                            {{/state.sortorder.isdiscussiondesc}}
                        </th>
                        {{#forum.state.groupmode}}
                        <th scope="col" class="group px-3">
                            {{#state.sortorder.isgroupdesc}}
                                <a href="{{{forum.urls.sortgroupasc}}}" aria-label="{{#str}}discussionlistsortbygroupasc, mod_forum{{/str}}">{{#str}}group{{/str}}</a> <span class="ccn-text-primary">{{#pix}}t/downlong, core, {{#str}}desc, core{{/str}}{{/pix}}</span>
                            {{/state.sortorder.isgroupdesc}}
                            {{#state.sortorder.isgroupasc}}
                                <a href="{{{forum.urls.sortgroupdesc}}}" aria-label="{{#str}}discussionlistsortbygroupdesc, mod_forum{{/str}}">{{#str}}group{{/str}}</a> <span class="ccn-text-primary">{{#pix}}t/uplong, core, {{#str}}asc, core{{/str}}{{/pix}}</span>
                            {{/state.sortorder.isgroupasc}}
                            {{^state.sortorder.isgroupdesc}}
                                {{^state.sortorder.isgroupasc}}
                                    <a href="{{{forum.urls.sortgroupdesc}}}" aria-label="{{#str}}discussionlistsortbygroupdesc, mod_forum{{/str}}">{{#str}}group{{/str}}</a>
                                {{/state.sortorder.isgroupasc}}
                            {{/state.sortorder.isgroupdesc}}
                        </th>
                        {{/forum.state.groupmode}}
                        <th scope="col" class="author px-3">
                            {{#state.sortorder.isstarterdesc}}
                                <a href="{{{forum.urls.sortstarterasc}}}" aria-label="{{#str}}discussionlistsortbystarterasc, mod_forum{{/str}}">{{#str}}startedby, mod_forum{{/str}}</a> <span class="ccn-text-primary">{{#pix}}t/downlong, core, {{#str}}desc, core{{/str}}{{/pix}}</span>
                            {{/state.sortorder.isstarterdesc}}
                            {{#state.sortorder.isstarterasc}}
                                <a href="{{{forum.urls.sortstarterdesc}}}" aria-label="{{#str}}discussionlistsortbystarterdesc, mod_forum{{/str}}">{{#str}}startedby, mod_forum{{/str}}</a> <span class="ccn-text-primary">{{#pix}}t/uplong, core, {{#str}}asc, core{{/str}}{{/pix}}</span>
                            {{/state.sortorder.isstarterasc}}
                            {{^state.sortorder.isstarterdesc}}
                                {{^state.sortorder.isstarterasc}}
                                    <a href="{{{forum.urls.sortstarterdesc}}}" aria-label="{{#str}}discussionlistsortbystarterdesc, mod_forum{{/str}}">{{#str}}startedby, mod_forum{{/str}}</a>
                                {{/state.sortorder.isstarterasc}}
                            {{/state.sortorder.isstarterdesc}}
                        </th>
                        <th scope="col" class="lastpost px-3">
                            {{#state.sortorder.islastpostdesc}}
                                <a href="{{{forum.urls.sortlastpostasc}}}" aria-label="{{#str}}discussionlistsortbylastpostasc, mod_forum{{/str}}">{{#str}}lastpost, mod_forum{{/str}}</a> <span class="ccn-text-primary">{{#pix}}t/downlong, core, {{#str}}desc, core{{/str}}{{/pix}}</span>
                            {{/state.sortorder.islastpostdesc}}
                            {{#state.sortorder.islastpostasc}}
                                <a href="{{{forum.urls.sortlastpostdesc}}}" aria-label="{{#str}}discussionlistsortbylastpostdesc, mod_forum{{/str}}">{{#str}}lastpost, mod_forum{{/str}}</a> <span class="ccn-text-primary">{{#pix}}t/uplong, core, {{#str}}asc, core{{/str}}{{/pix}}</span>
                            {{/state.sortorder.islastpostasc}}
                            {{^state.sortorder.islastpostdesc}}
                                {{^state.sortorder.islastpostasc}}
                                    <a href="{{{forum.urls.sortlastpostdesc}}}" aria-label="{{#str}}discussionlistsortbylastpostdesc, mod_forum{{/str}}">{{#str}}lastpost, mod_forum{{/str}}</a>
                                {{/state.sortorder.islastpostasc}}
                            {{/state.sortorder.islastpostdesc}}
                        </th>
                        {{#forum.capabilities.viewdiscussions}}
                            <th scope="col" class="text-center px-2">
                                {{#state.sortorder.isrepliesdesc}}
                                    <a href="{{{forum.urls.sortrepliesasc}}}" aria-label="{{#str}}discussionlistsortbyrepliesasc, mod_forum{{/str}}">{{#str}}replies, mod_forum{{/str}}</a> <span class="ccn-text-primary">{{#pix}}t/downlong, core, {{#str}}desc, core{{/str}}{{/pix}}</span>
                                {{/state.sortorder.isrepliesdesc}}
                                {{#state.sortorder.isrepliesasc}}
                                    <a href="{{{forum.urls.sortrepliesdesc}}}" aria-label="{{#str}}discussionlistsortbyrepliesdesc, mod_forum{{/str}}">{{#str}}replies, mod_forum{{/str}}</a> <span class="ccn-text-primary">{{#pix}}t/uplong, core, {{#str}}asc, core{{/str}}{{/pix}}</span>
                                {{/state.sortorder.isrepliesasc}}
                                {{^state.sortorder.isrepliesdesc}}
                                    {{^state.sortorder.isrepliesasc}}
                                        <a href="{{{forum.urls.sortrepliesdesc}}}" aria-label="{{#str}}discussionlistsortbyrepliesdesc, mod_forum{{/str}}">{{#str}}replies, mod_forum{{/str}}</a>
                                    {{/state.sortorder.isrepliesasc}}
                                {{/state.sortorder.isrepliesdesc}}
                                {{#forum.userstate.tracked}}
                                    <a href="{{{forum.urls.markasread}}}">{{#pix}}t/markasread, core, {{#str}}markallread, mod_forum{{/str}}{{/pix}}</a>
                                {{/forum.userstate.tracked}}
                            </th>
                        {{/forum.capabilities.viewdiscussions}}
                        {{#forum.capabilities.subscribe}}
                            <th scope="col" class="text-center px-2">{{#str}} notsubscribed, mod_forum {{/str}}</th>
                        {{/forum.capabilities.subscribe}}
                        <th scope="col" class="discussionsubscription">
                            <span class="accesshide">{{#str}}actions{{/str}}</span>
                        </th>
                    </tr>
                </thead>
                {{/discussion_list_header}}
                {{$discussion_list_body}}
                <tbody>
                    {{#summaries}}
                        {{! The discussion class is only required for behat tests to identify the row. }}
                        <tr class="discussion{{!
                                }}{{#discussion.pinned}} pinned{{/discussion.pinned}}{{!
                                }}{{#discussion.userstate.favourited}} favourited{{/discussion.userstate.favourited}}{{!
                                }}{{#discussion.userstate.subscribed}} subscribed{{/discussion.userstate.subscribed}}{{!
                                }}{{#unread}} hasunread{{/unread}}{{!
                                }}{{#discussion.timed}}{{#istimed}}{{^visible}} dimmed_text{{/visible}}{{/istimed}}{{/discussion.timed}}"
                            data-region="discussion-list-item"
                            data-discussionid="{{id}}"
                            data-forumid="{{forumid}}">
                            <td class="p-0 text-center align-middle icon-no-margin" style="width: 1px;">
                                {{#discussion.pinned}}
                                    {{#pix}}i/pinned, mod_forum, {{#str}}discussionpinned, mod_forum{{/str}}{{/pix}}
                                {{/discussion.pinned}}
                                {{^discussion.pinned}}
                                    {{#discussion}}
                                        {{> mod_forum/forum_discussion_favourite_toggle}}
                                    {{/discussion}}
                                {{/discussion.pinned}}
                            </td>
                            <td scope="row" class="topic p-0 align-middle">
                                <div class="p-3 p-l-0">
                                    <a class="w-100 h-100 d-block" href="{{discussion.urls.view}}" title="{{discussion.name}}" aria-label="{{discussion.name}}">
                                        {{#shortentext}}100, {{{discussion.name}}}{{/shortentext}}
                                    </a>
                                    <div>
                                        <span class="badge badge-danger rounded" data-region="locked-label" {{^discussion.locked}}hidden{{/discussion.locked}}>
                                            {{#str}}locked, forum{{/str}}
                                        </span>
                                        {{#discussion.timed.istimed}}
                                        <span>
                                             <button class="btn badge badge-primary rounded border-0" data-region="timed-label"
                                                data-toggle="popover" data-container="body" data-placement="right" data-html="true"
                                                data-content={{#quote}}{{#discussion.timed}}{{#discussion.times}}{{> mod_forum/discussion_times}}{{/discussion.times}}{{/discussion.timed}}{{/quote}}
                                                data-trigger="click" aria-label="{{#str}}timeddiscussion, forum{{/str}}" title="{{#str}}timeddiscussion, forum{{/str}}">
                                                 {{#str}}timed, forum{{/str}}
                                             </button>
                                        </span>
                                        {{/discussion.timed.istimed}}
                                    </div>
                                </div>
                            </td>
                            {{#forum.state.groupmode}}
                                <td class="group align-middle fit-content limit-width px-3">
                                    {{#discussion.group}}
                                        <img alt="{{#str}} pictureof, core, {{name}} {{/str}}"
                                             class="border rounded h-auto rounded-circle grouppicture"
                                             src="{{{urls.picture}}}"
                                             title="{{#str}} pictureof, core, {{name}} {{/str}}">
                                        {{#urls.userlist}}
                                            <a href="{{{urls.userlist}}}" aria-label='{{#str}} memberofgroup, group, {{name}}{{/str}}'
                                               title='{{#str}} memberofgroup, group, {{name}}{{/str}}'>
                                                {{#shortentext}}30, {{name}}{{/shortentext}}
                                            </a>
                                        {{/urls.userlist}}
                                        {{^urls.userlist}}
                                            <span>{{name}}</span>
                                        {{/urls.userlist}}
                                    {{/discussion.group}}
                                </td>
                            {{/forum.state.groupmode}}
                            <td class="author align-middle px-3">
                                {{#firstpostauthor}}
                                    <div class="d-flex">
                                        <div class="align-middle p-0">
                                            <img class="rounded-circle userpicture" src="{{urls.profileimage}}"
                                                 alt="{{#str}}pictureof, moodle, {{fullname}}{{/str}}"
                                                 title="{{#str}}pictureof, moodle, {{fullname}}{{/str}}" >
                                        </div>
                                        <div class="author-info align-middle">
                                            <div class="text-truncate">{{fullname}}</div>
                                            <div class="">
                                                {{#userdate}}{{discussion.times.created}}, %d %b %Y{{/userdate}}
                                            </div>
                                        </div>
                                    </div>
                                {{/firstpostauthor}}
                            </td>
                            <td class="author text-left align-middle px-3">
                                {{! TODO Check q&a, eachuser }}
                                {{#latestpostauthor}}
                                    <div class="d-flex">
                                        <div class="align-middle p-0">
                                            <img class="rounded-circle userpicture" src="{{urls.profileimage}}"
                                                 alt="{{#str}}pictureof, moodle, {{fullname}}{{/str}}"
                                                 title="{{#str}}pictureof, moodle, {{fullname}}{{/str}}" >
                                        </div>
                                        <div class="author-info align-middle">
                                            <div class="text-truncate">{{fullname}}</div>
                                            <div class="">
                                                {{#latestpostid}}
                                                    <a href="{{{discussion.urls.viewlatest}}}" title="{{#userdate}}{{discussion.times.modified}},{{#str}}strftimerecentfull{{/str}}{{/userdate}}">
                                                        {{#userdate}}{{discussion.times.modified}}, %d %b %Y{{/userdate}}
                                                    </a>
                                                {{/latestpostid}}
                                            </div>
                                        </div>
                                    </div>
                                {{/latestpostauthor}}
                            </td>
                            {{#forum.capabilities.viewdiscussions}}
                                <td class="p-0 text-center align-middle fit-content px-2">
                                    <span>{{replies}}</span>
                                    {{#forum.userstate.tracked}}
                                        {{#unread}}
                                        {{! TODO Rewrite as AJAX}}
                                            <span class="lead">
                                                <a href="{{{discussion.urls.viewfirstunread}}}" class="badge badge-primary rounded-pill font-weight-normal"
                                                   title="{{#str}}unreadpostsnumber, mod_forum, {{unread}}{{/str}}" aria-label="{{#str}}unreadpostsnumber, mod_forum, {{unread}}{{/str}}">
                                                    {{unread}}
                                                </a>
                                            </span>
                                        {{/unread}}
                                    {{/forum.userstate.tracked}}
                                </td>
                            {{/forum.capabilities.viewdiscussions}}
                            {{#forum.capabilities.subscribe}}
                                <td class="text-center align-middle fit-content px-2">
                                    {{#discussion}}
                                        <span class="switch sr-only-label">
                                            <input
                                                type="checkbox"
                                                id="subscription-toggle-{{id}}"
                                                data-type="subscription-toggle"
                                                data-action="toggle"
                                                data-discussionid="{{id}}"
                                                data-forumid="{{forumid}}"
                                                {{#userstate.subscribed}}data-targetstate="0" checked{{/userstate.subscribed}}
                                                {{^userstate.subscribed}}data-targetstate="1"{{/userstate.subscribed}}
                                            >
                                            <label for="subscription-toggle-{{id}}">
                                                {{#userstate.subscribed}}
                                                    {{#str}}unsubscribediscussion, forum{{/str}}
                                                {{/userstate.subscribed}}
                                                {{^userstate.subscribed}}
                                                    {{#str}}subscribediscussion, forum{{/str}}
                                                {{/userstate.subscribed}}
                                            </label>
                                        </span>
                                    {{/discussion}}
                                </td>
                            {{/forum.capabilities.subscribe}}
                            <td class="p-0 align-middle fit-content" data-container="discussion-summary-actions">
                                {{#discussion}}
                                    <div class="d-flex flex-wrap justify-content-end icon-no-margin">
                                        {{#hasanyactions}}
                                            <div data-container='discussion-tools'>
                                                {{> mod_forum/forum_action_menu}}
                                            </div>
                                        {{/hasanyactions}}
                                    </div>
                                {{/discussion}}
                            </td>
                        </tr>
                    {{/summaries}}
                </tbody>
                {{/discussion_list_body}}
            </table>
          </div>
        {{/discussion_list_output}}
        {{$discussion_bottom_pagination}}
            {{{ pagination }}}
        {{/discussion_bottom_pagination}}
        {{#can_create_discussion}}
            <div class="forumaddnew">
                <a href="{{create_discussion_link}}" class="btn dbxshad btn-md btn-thm white ccn-mod-forum-btn">{{create_discussion_link_text}}</a>
            </div>
        {{/can_create_discussion}}
    {{/state.hasdiscussions}}
    {{^state.hasdiscussions}}
        <div class="forumnodiscuss">
            {{$no_discussions_text}}
                ({{#str}}nodiscussions, forum{{/str}})
            {{/no_discussions_text}}
        </div>
    {{/state.hasdiscussions}}

</div>
{{#js}}
    require(['jquery', 'mod_forum/discussion_list'], function($, View) {
        var root = $('#discussion-list-{{uniqid}}');
        View.init(root);
    });
{{/js}}
