MDL-66481 mod_forum: Implement additional column sorting
authorJun Pataleta <jun@moodle.com>
Tue, 27 Aug 2019 12:28:11 +0000 (20:28 +0800)
committerJun Pataleta <jun@moodle.com>
Thu, 26 Sep 2019 02:47:05 +0000 (10:47 +0800)
Implement sorting for the following columns:
* Discussion name
* Discussion starter name
* Group

mod/forum/classes/local/builders/exported_discussion_summaries.php
mod/forum/classes/local/exporters/forum.php
mod/forum/classes/local/vaults/discussion_list.php
mod/forum/lang/en/forum.php
mod/forum/templates/discussion_list.mustache

index 6e49a62..33c1ced 100644 (file)
@@ -180,7 +180,13 @@ class exported_discussion_summaries {
             'isrepliesdesc' => $sortorder == $discussionlistvault::SORTORDER_REPLIES_DESC,
             'isrepliesasc' => $sortorder == $discussionlistvault::SORTORDER_REPLIES_ASC,
             'iscreateddesc' => $sortorder == $discussionlistvault::SORTORDER_CREATED_DESC,
             'isrepliesdesc' => $sortorder == $discussionlistvault::SORTORDER_REPLIES_DESC,
             'isrepliesasc' => $sortorder == $discussionlistvault::SORTORDER_REPLIES_ASC,
             'iscreateddesc' => $sortorder == $discussionlistvault::SORTORDER_CREATED_DESC,
-            'iscreatedasc' => $sortorder == $discussionlistvault::SORTORDER_CREATED_ASC
+            'iscreatedasc' => $sortorder == $discussionlistvault::SORTORDER_CREATED_ASC,
+            'isdiscussiondesc' => $sortorder == $discussionlistvault::SORTORDER_DISCUSSION_DESC,
+            'isdiscussionasc' => $sortorder == $discussionlistvault::SORTORDER_DISCUSSION_ASC,
+            'isstarterdesc' => $sortorder == $discussionlistvault::SORTORDER_STARTER_DESC,
+            'isstarterasc' => $sortorder == $discussionlistvault::SORTORDER_STARTER_ASC,
+            'isgroupdesc' => $sortorder == $discussionlistvault::SORTORDER_GROUP_DESC,
+            'isgroupasc' => $sortorder == $discussionlistvault::SORTORDER_GROUP_ASC,
         );
 
         $exportedposts['state']['sortorder'] = $sortoptions;
         );
 
         $exportedposts['state']['sortorder'] = $sortoptions;
index ad39615..e12ddfc 100644 (file)
@@ -89,6 +89,12 @@ class forum extends exporter {
                     'sortlastpostdesc' => ['type' => PARAM_URL],
                     'sortcreatedasc' => ['type' => PARAM_URL],
                     'sortcreateddesc' => ['type' => PARAM_URL],
                     'sortlastpostdesc' => ['type' => PARAM_URL],
                     'sortcreatedasc' => ['type' => PARAM_URL],
                     'sortcreateddesc' => ['type' => PARAM_URL],
+                    'sortdiscussionasc' => ['type' => PARAM_URL],
+                    'sortdiscussiondesc' => ['type' => PARAM_URL],
+                    'sortstarterasc' => ['type' => PARAM_URL],
+                    'sortstarterdesc' => ['type' => PARAM_URL],
+                    'sortgroupasc' => ['type' => PARAM_URL],
+                    'sortgroupdesc' => ['type' => PARAM_URL],
                 ],
             ],
         ];
                 ],
             ],
         ];
@@ -137,7 +143,19 @@ class forum extends exporter {
                 'sortcreatedasc' => $urlfactory->get_forum_view_url_from_forum($this->forum, null,
                     $discussionvault::SORTORDER_CREATED_ASC)->out(false),
                 'sortcreateddesc' => $urlfactory->get_forum_view_url_from_forum($this->forum, null,
                 'sortcreatedasc' => $urlfactory->get_forum_view_url_from_forum($this->forum, null,
                     $discussionvault::SORTORDER_CREATED_ASC)->out(false),
                 'sortcreateddesc' => $urlfactory->get_forum_view_url_from_forum($this->forum, null,
-                    $discussionvault::SORTORDER_CREATED_DESC)->out(false)
+                    $discussionvault::SORTORDER_CREATED_DESC)->out(false),
+                'sortdiscussionasc' => $urlfactory->get_forum_view_url_from_forum($this->forum, null,
+                    $discussionvault::SORTORDER_DISCUSSION_ASC)->out(false),
+                'sortdiscussiondesc' => $urlfactory->get_forum_view_url_from_forum($this->forum, null,
+                    $discussionvault::SORTORDER_DISCUSSION_DESC)->out(false),
+                'sortstarterasc' => $urlfactory->get_forum_view_url_from_forum($this->forum, null,
+                    $discussionvault::SORTORDER_STARTER_ASC)->out(false),
+                'sortstarterdesc' => $urlfactory->get_forum_view_url_from_forum($this->forum, null,
+                    $discussionvault::SORTORDER_STARTER_DESC)->out(false),
+                'sortgroupasc' => $urlfactory->get_forum_view_url_from_forum($this->forum, null,
+                    $discussionvault::SORTORDER_GROUP_ASC)->out(false),
+                'sortgroupdesc' => $urlfactory->get_forum_view_url_from_forum($this->forum, null,
+                    $discussionvault::SORTORDER_GROUP_DESC)->out(false),
             ],
         ];
     }
             ],
         ];
     }
index a4a345b..07bc0db 100644 (file)
@@ -26,6 +26,7 @@ namespace mod_forum\local\vaults;
 
 defined('MOODLE_INTERNAL') || die();
 
 
 defined('MOODLE_INTERNAL') || die();
 
+use core_group\output\group_details;
 use mod_forum\local\vaults\preprocessors\extract_record as extract_record_preprocessor;
 use mod_forum\local\vaults\preprocessors\extract_user as extract_user_preprocessor;
 use mod_forum\local\renderers\discussion_list as discussion_list_renderer;
 use mod_forum\local\vaults\preprocessors\extract_record as extract_record_preprocessor;
 use mod_forum\local\vaults\preprocessors\extract_user as extract_user_preprocessor;
 use mod_forum\local\renderers\discussion_list as discussion_list_renderer;
@@ -70,6 +71,18 @@ class discussion_list extends db_table_vault {
     public const SORTORDER_REPLIES_DESC = 5;
     /** Sort by number of replies desc */
     public const SORTORDER_REPLIES_ASC = 6;
     public const SORTORDER_REPLIES_DESC = 5;
     /** Sort by number of replies desc */
     public const SORTORDER_REPLIES_ASC = 6;
+    /** Sort by discussion name desc */
+    public const SORTORDER_DISCUSSION_DESC = 7;
+    /** Sort by discussion name asc */
+    public const SORTORDER_DISCUSSION_ASC = 8;
+    /** Sort by discussion starter's name desc */
+    public const SORTORDER_STARTER_DESC = 9;
+    /** Sort by discussion starter's name asc */
+    public const SORTORDER_STARTER_ASC = 10;
+    /** Sort by group name desc */
+    public const SORTORDER_GROUP_DESC = 11;
+    /** Sort by group name asc */
+    public const SORTORDER_GROUP_ASC = 12;
 
     /**
      * Get the table alias.
 
     /**
      * Get the table alias.
@@ -94,14 +107,12 @@ class discussion_list extends db_table_vault {
      *
      * @param string|null $wheresql Where conditions for the SQL
      * @param string|null $sortsql Order by conditions for the SQL
      *
      * @param string|null $wheresql Where conditions for the SQL
      * @param string|null $sortsql Order by conditions for the SQL
-     * @param string|null $joinsql Additional join conditions for the sql
-     * @param int|null    $userid The ID of the user we are performing this query for
+     * @param int|null $userid The ID of the user we are performing this query for
      *
      * @return string
      */
     protected function generate_get_records_sql(string $wheresql = null, ?string $sortsql = null, ?int $userid = null) : string {
         $alias = $this->get_table_alias();
      *
      * @return string
      */
     protected function generate_get_records_sql(string $wheresql = null, ?string $sortsql = null, ?int $userid = null) : string {
         $alias = $this->get_table_alias();
-        $db = $this->get_db();
 
         $includefavourites = $userid ? true : false;
 
 
         $includefavourites = $userid ? true : false;
 
@@ -153,6 +164,18 @@ class discussion_list extends db_table_vault {
                          ) r ON d.id = r.id';
         }
 
                          ) r ON d.id = r.id';
         }
 
+        $groupsortorders = [
+            $this->get_sort_order(self::SORTORDER_GROUP_DESC, $includefavourites),
+            $this->get_sort_order(self::SORTORDER_GROUP_ASC, $includefavourites)
+        ];
+        $sortbygroup = in_array($sortsql, $groupsortorders);
+        if ($sortbygroup) {
+            $groupstable = new dml_table('groups', 'g', 'g');
+            $fields .= ', ' . $groupstable->get_field_select();
+            // Join groups.
+            $tables .= 'LEFT JOIN {groups} g ON g.id = d.groupid';
+        }
+
         $selectsql = 'SELECT ' . $fields . ' FROM ' . $tables;
         $selectsql .= $wheresql ? ' WHERE ' . $wheresql : '';
         $selectsql .= $sortsql ? ' ORDER BY ' . $sortsql : '';
         $selectsql = 'SELECT ' . $fields . ' FROM ' . $tables;
         $selectsql .= $wheresql ? ' WHERE ' . $wheresql : '';
         $selectsql .= $sortsql ? ' ORDER BY ' . $sortsql : '';
@@ -226,6 +249,8 @@ class discussion_list extends db_table_vault {
      * @return string
      */
     protected function get_keyfield(?int $sortmethod) : string {
      * @return string
      */
     protected function get_keyfield(?int $sortmethod) : string {
+        global $CFG;
+
         switch ($sortmethod) {
             case self::SORTORDER_CREATED_DESC:
             case self::SORTORDER_CREATED_ASC:
         switch ($sortmethod) {
             case self::SORTORDER_CREATED_DESC:
             case self::SORTORDER_CREATED_ASC:
@@ -233,6 +258,30 @@ class discussion_list extends db_table_vault {
             case self::SORTORDER_REPLIES_DESC:
             case self::SORTORDER_REPLIES_ASC:
                 return 'replycount';
             case self::SORTORDER_REPLIES_DESC:
             case self::SORTORDER_REPLIES_ASC:
                 return 'replycount';
+            case self::SORTORDER_DISCUSSION_DESC:
+            case self::SORTORDER_DISCUSSION_ASC:
+                return 'dname';
+            case self::SORTORDER_STARTER_DESC:
+            case self::SORTORDER_STARTER_ASC:
+                // We'll sort by the first name field of the discussion starter's name.
+
+                // Let's get the full name display config first.
+                $nameformat = $CFG->fullnamedisplay;
+                if ($CFG->fullnamedisplay === 'language') {
+                    $nameformat = get_string('fullnamedisplay', '', (object)['firstname' => 'firstname', 'lastname' => 'lastname']);
+                }
+                // Fetch all the available user name fields.
+                $availablefields = order_in_string(get_all_user_name_fields(), $nameformat);
+                // We'll default to the first name if there's no available name field.
+                $returnfield = 'firstname';
+                if (!empty($availablefields)) {
+                    // Use the first name field.
+                    $returnfield = reset($availablefields);
+                }
+                return 'fauserrecord' . $returnfield;
+            case self::SORTORDER_GROUP_DESC:
+            case self::SORTORDER_GROUP_ASC:
+                return 'gname';
             default:
                 global $CFG;
                 $alias = $this->get_table_alias();
             default:
                 global $CFG;
                 $alias = $this->get_table_alias();
@@ -255,11 +304,16 @@ class discussion_list extends db_table_vault {
             case self::SORTORDER_LASTPOST_ASC:
             case self::SORTORDER_CREATED_ASC:
             case self::SORTORDER_REPLIES_ASC:
             case self::SORTORDER_LASTPOST_ASC:
             case self::SORTORDER_CREATED_ASC:
             case self::SORTORDER_REPLIES_ASC:
+            case self::SORTORDER_DISCUSSION_ASC:
+            case self::SORTORDER_STARTER_ASC:
+            case self::SORTORDER_GROUP_ASC:
                 return "ASC";
             case self::SORTORDER_LASTPOST_DESC:
             case self::SORTORDER_CREATED_DESC:
             case self::SORTORDER_REPLIES_DESC:
                 return "ASC";
             case self::SORTORDER_LASTPOST_DESC:
             case self::SORTORDER_CREATED_DESC:
             case self::SORTORDER_REPLIES_DESC:
-                return "DESC";
+            case self::SORTORDER_DISCUSSION_DESC:
+            case self::SORTORDER_STARTER_DESC:
+            case self::SORTORDER_GROUP_DESC:
             default:
                 return "DESC";
         }
             default:
                 return "DESC";
         }
index 23aaea3..6069f6d 100644 (file)
@@ -163,10 +163,16 @@ $string['disallowsubscribeteacher'] = 'Subscriptions not allowed (except for tea
 $string['discussion'] = 'Discussion';
 $string['discussionlistsortbycreatedasc'] = 'Sort by creation date in ascending order';
 $string['discussionlistsortbycreateddesc'] = 'Sort by creation date in descending order';
 $string['discussion'] = 'Discussion';
 $string['discussionlistsortbycreatedasc'] = 'Sort by creation date in ascending order';
 $string['discussionlistsortbycreateddesc'] = 'Sort by creation date in descending order';
+$string['discussionlistsortbydiscussionasc'] = 'Sort by discussion name in ascending order';
+$string['discussionlistsortbydiscussiondesc'] = 'Sort by discussion name in descending order';
+$string['discussionlistsortbygroupasc'] = 'Sort by group in ascending order';
+$string['discussionlistsortbygroupdesc'] = 'Sort by group in descending order';
 $string['discussionlistsortbylastpostdesc'] = 'Sort by last post creation date in descending order';
 $string['discussionlistsortbylastpostasc'] = 'Sort by last post creation date in ascending order';
 $string['discussionlistsortbyrepliesasc'] = 'Sort by number of replies in ascending order';
 $string['discussionlistsortbyrepliesdesc'] = 'Sort by number of replies in descending order';
 $string['discussionlistsortbylastpostdesc'] = 'Sort by last post creation date in descending order';
 $string['discussionlistsortbylastpostasc'] = 'Sort by last post creation date in ascending order';
 $string['discussionlistsortbyrepliesasc'] = 'Sort by number of replies in ascending order';
 $string['discussionlistsortbyrepliesdesc'] = 'Sort by number of replies in descending order';
+$string['discussionlistsortbystarterasc'] = 'Sort by discussion starter name in ascending order';
+$string['discussionlistsortbystarterdesc'] = 'Sort by discussion starter name in descending order';
 $string['discussionlocked'] = 'This discussion has been locked so you can no longer reply to it.';
 $string['discussionlockingheader'] = 'Discussion locking';
 $string['discussionlockingdisabled'] = 'Do not lock discussions';
 $string['discussionlocked'] = 'This discussion has been locked so you can no longer reply to it.';
 $string['discussionlockingheader'] = 'Discussion locking';
 $string['discussionlockingdisabled'] = 'Do not lock discussions';
index 831f53a..24bd365 100644 (file)
                         <th scope="col">
                             <span class="accesshide">{{#str}}status{{/str}}</span>
                         </th>
                         <th scope="col">
                             <span class="accesshide">{{#str}}status{{/str}}</span>
                         </th>
-                        <th scope="col" class="p-l-0">{{#str}}discussion, mod_forum{{/str}}</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="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="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}}
                         {{#forum.state.groupmode}}
-                            <th scope="col" class="group">{{#str}}group{{/str}}</th>
+                        <th scope="col" class="group">
+                            {{#state.sortorder.isgroupdesc}}
+                                <a href="{{{forum.urls.sortgroupasc}}}" aria-label="{{#str}}discussionlistsortbygroupasc, mod_forum{{/str}}">{{#str}}group{{/str}}</a> <span class="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="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">
                         {{/forum.state.groupmode}}
                         <th scope="col" class="author">
-                            {{#str}}startedby, mod_forum{{/str}}
+                            {{#state.sortorder.isstarterdesc}}
+                                <a href="{{{forum.urls.sortstarterasc}}}" aria-label="{{#str}}discussionlistsortbystarterasc, mod_forum{{/str}}">{{#str}}startedby, mod_forum{{/str}}</a> <span class="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="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">
                             {{#state.sortorder.islastpostdesc}}
                         </th>
                         <th scope="col" class="lastpost">
                             {{#state.sortorder.islastpostdesc}}