MDL-65071 forum: Sort starred discussions

This commit is contained in:
Mihail Geshoski
2019-05-01 22:51:14 +08:00
parent 28c2f2b266
commit b4a1bbbb6a
10 changed files with 47 additions and 45 deletions
@@ -213,7 +213,7 @@ class discussion extends exporter {
'pin' => $capabilitymanager->can_pin_discussion($user, $discussion),
'post' => $capabilitymanager->can_post_in_discussion($user, $discussion),
'manage' => $capabilitymanager->can_manage_forum($user),
'favourite' => $capabilitymanager->can_favourite_discussion($user, $discussion) // Defaulting to true until we get capabilities sorted
'favourite' => $capabilitymanager->can_favourite_discussion($user) // Defaulting to true until we get capabilities sorted
],
'urls' => [
'view' => $urlfactory->get_discussion_view_url_from_discussion($discussion)->out(false),
@@ -325,11 +325,9 @@ class capability {
* Can the user favourite the discussion
*
* @param stdClass $user The user to check
* @param discussion_entity $discussion The discussion to check
* @return bool
*/
public function can_favourite_discussion(stdClass $user, discussion_entity $discussion) : bool
{
public function can_favourite_discussion(stdClass $user) : bool {
$context = $this->get_context();
return has_capability('mod/forum:cantogglefavourite', $context, $user);
}
+2 -2
View File
@@ -55,10 +55,10 @@ class author extends db_table_vault {
*
* @param string|null $wheresql Where conditions for the SQL
* @param string|null $sortsql Order by conditions for the SQL
* @param \stdClass|null $user The user object
* @param int|null $userid The user ID
* @return string
*/
protected function generate_get_records_sql(string $wheresql = null, string $sortsql = null, \stdClass $user = null) : string {
protected function generate_get_records_sql(string $wheresql = null, string $sortsql = null, ?int $userid = null) : string {
$selectsql = 'SELECT * FROM {' . self::TABLE . '} ' . $this->get_table_alias();
$selectsql .= $wheresql ? ' WHERE ' . $wheresql : '';
$selectsql .= $sortsql ? ' ORDER BY ' . $sortsql : '';
@@ -76,7 +76,7 @@ abstract class db_table_vault {
* @return string
*/
abstract protected function generate_get_records_sql(string $wheresql = null, string $sortsql = null,
\stdClass $user = null) : string;
?int $userid = null) : string;
/**
* Convert the DB records into entities. The list of records will have been
@@ -58,10 +58,10 @@ class discussion extends db_table_vault {
*
* @param string|null $wheresql Where conditions for the SQL
* @param string|null $sortsql Order by conditions for the SQL
* @param \stdClass|null $user The user object
* @param int|null $userid The user ID
* @return string
*/
protected function generate_get_records_sql(string $wheresql = null, string $sortsql = null, \stdClass $user = null) : string {
protected function generate_get_records_sql(string $wheresql = null, string $sortsql = null, ?int $userid = null) : string {
$selectsql = 'SELECT * FROM {' . self::TABLE . '} ' . $this->get_table_alias();
$selectsql .= $wheresql ? ' WHERE ' . $wheresql : '';
$selectsql .= $sortsql ? ' ORDER BY ' . $sortsql : '';
@@ -95,17 +95,22 @@ 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 $joinsql Additional join conditions for the sql
* @param stdClass|null $user 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, stdClass $user = null) : 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();
list($favsql, $favparams) = $this->get_favourite_sql($user);
foreach ($favparams as $key => $param) {
$favsql = str_replace(":$key", "'$param'", $favsql);
$includefavourites = $userid ? true : false;
$favsql = '';
if ($includefavourites) {
list($favsql, $favparams) = $this->get_favourite_sql($userid);
foreach ($favparams as $key => $param) {
$favsql = str_replace(":$key", "'$param'", $favsql);
}
}
// Fetch:
@@ -126,8 +131,8 @@ class discussion_list extends db_table_vault {
]);
$sortkeys = [
$this->get_sort_order(self::SORTORDER_REPLIES_DESC),
$this->get_sort_order(self::SORTORDER_REPLIES_ASC)
$this->get_sort_order(self::SORTORDER_REPLIES_DESC, $includefavourites),
$this->get_sort_order(self::SORTORDER_REPLIES_ASC, $includefavourites)
];
$issortbyreplies = in_array($sortsql, $sortkeys);
@@ -135,7 +140,7 @@ class discussion_list extends db_table_vault {
$tables .= ' JOIN {user} fa ON fa.id = ' . $alias . '.userid';
$tables .= ' JOIN {user} la ON la.id = ' . $alias . '.usermodified';
$tables .= ' JOIN ' . $posttable->get_from_sql() . ' ON fp.id = ' . $alias . '.firstpost';
$tables .= isset($favsql) ? $favsql : '';
$tables .= $favsql;
if ($issortbyreplies) {
// Join the discussion replies.
@@ -263,10 +268,11 @@ class discussion_list extends db_table_vault {
/**
* Get the sort order SQL for a sort method.
*
* @param int|null $sortmethod
* @param int|null $sortmethod
* @param bool|null $includefavourites
* @return string
*/
public function get_sort_order(?int $sortmethod, $includefavourites = true) : string {
private function get_sort_order(?int $sortmethod, bool $includefavourites = true) : string {
$alias = $this->get_table_alias();
// TODO consider user favourites...
@@ -284,7 +290,7 @@ class discussion_list extends db_table_vault {
// After the null favourite fields are deprioritised and appear below the favourited discussions we
// need to order the favourited discussions by id so that the most recently favourited discussions
// appear at the top of the list.
$favouritesort .= ", {$favalias}.id DESC";
$favouritesort .= ", {$favalias}.itemtype DESC";
}
return "{$alias}.pinned DESC $favouritesort , {$keyfield} {$direction}";
@@ -294,7 +300,7 @@ class discussion_list extends db_table_vault {
* Fetch any required SQL to respect timed posts.
*
* @param bool $includehiddendiscussions Whether to include hidden discussions or not
* @param int $includepostsforuser Which user to include posts for, if any
* @param int|null $includepostsforuser Which user to include posts for, if any
* @return array The SQL and parameters to include
*/
protected function get_hidden_post_sql(bool $includehiddendiscussions, ?int $includepostsforuser) {
@@ -336,8 +342,7 @@ class discussion_list extends db_table_vault {
?int $includepostsforuser,
?int $sortorder,
int $limit,
int $offset,
stdClass $user
int $offset
) {
$alias = $this->get_table_alias();
$wheresql = "{$alias}.forum = :forumid";
@@ -351,7 +356,9 @@ class discussion_list extends db_table_vault {
'forumid' => $forumid,
]);
$sql = $this->generate_get_records_sql($wheresql, $this->get_sort_order($sortorder, isloggedin()), $user);
$includefavourites = $includepostsforuser ? true : false;
$sql = $this->generate_get_records_sql($wheresql, $this->get_sort_order($sortorder, $includefavourites),
$includepostsforuser);
$records = $this->get_db()->get_records_sql($sql, $params, $offset, $limit);
return $this->transform_db_records_to_entities($records);
@@ -377,8 +384,7 @@ class discussion_list extends db_table_vault {
?int $includepostsforuser,
?int $sortorder,
int $limit,
int $offset,
stdClass $user
int $offset
) {
$alias = $this->get_table_alias();
@@ -402,7 +408,9 @@ class discussion_list extends db_table_vault {
'allgroupsid' => -1,
]);
$sql = $this->generate_get_records_sql($wheresql, $this->get_sort_order($sortorder, isloggedin()), $user);
$includefavourites = $includepostsforuser ? true : false;
$sql = $this->generate_get_records_sql($wheresql, $this->get_sort_order($sortorder, $includefavourites),
$includepostsforuser);
$records = $this->get_db()->get_records_sql($sql, $params, $offset, $limit);
return $this->transform_db_records_to_entities($records);
@@ -483,20 +491,16 @@ class discussion_list extends db_table_vault {
/**
* Get the standard favouriting sql.
*
* @param stdClass $user The user we are getting the sql for
* @param int $userid The ID of the user we are getting the sql for
* @return [$sql, $params] An array comprising of the sql and any associated params
*/
private function get_favourite_sql(?stdClass $user): array {
$favsql = "";
$favparams = [];
private function get_favourite_sql(int $userid): array {
if ($user && isloggedin()) {
$usercontext = \context_user::instance($user->id);
$alias = $this->get_table_alias();
$ufservice = \core_favourites\service_factory::get_service_for_user_context($usercontext);
list($favsql, $favparams) = $ufservice->get_join_sql_by_type('mod_forum', 'discussions',
$this->get_favourite_alias(), "$alias.id");
}
$usercontext = \context_user::instance($userid);
$alias = $this->get_table_alias();
$ufservice = \core_favourites\service_factory::get_service_for_user_context($usercontext);
list($favsql, $favparams) = $ufservice->get_join_sql_by_type('mod_forum', 'discussions',
$this->get_favourite_alias(), "$alias.id");
return [$favsql, $favparams];
}
+2 -2
View File
@@ -61,10 +61,10 @@ class forum extends db_table_vault {
*
* @param string|null $wheresql Where conditions for the SQL
* @param string|null $sortsql Order by conditions for the SQL
* @param \stdClass|null $user The user object
* @param int|null $userid The user ID
* @return string
*/
protected function generate_get_records_sql(string $wheresql = null, string $sortsql = null, \stdClass $user = null) : string {
protected function generate_get_records_sql(string $wheresql = null, string $sortsql = null, ?int $userid = null) : string {
$db = $this->get_db();
$alias = $this->get_table_alias();
+2 -2
View File
@@ -63,10 +63,10 @@ class post extends db_table_vault {
*
* @param string|null $wheresql Where conditions for the SQL
* @param string|null $sortsql Order by conditions for the SQL
* @param \stdClass|null $user The user object
* @param int|null $userid The user ID
* @return string
*/
protected function generate_get_records_sql(string $wheresql = null, string $sortsql = null, stdClass $user = null) : string {
protected function generate_get_records_sql(string $wheresql = null, string $sortsql = null, ?int $userid = null) : string {
$table = self::TABLE;
$alias = $this->get_table_alias();
$fields = $alias . '.*';
@@ -57,10 +57,10 @@ class post_read_receipt_collection extends db_table_vault {
*
* @param string|null $wheresql Where conditions for the SQL
* @param string|null $sortsql Order by conditions for the SQL
* @param \stdClass|null $user The user object
* @param int|null $userid The user ID
* @return string
*/
protected function generate_get_records_sql(string $wheresql = null, string $sortsql = null, \stdClass $user = null) : string {
protected function generate_get_records_sql(string $wheresql = null, string $sortsql = null, ?int $userid = null) : string {
$selectsql = 'SELECT * FROM {' . self::TABLE . '} ' . $this->get_table_alias();
$selectsql .= $wheresql ? ' WHERE ' . $wheresql : '';
$selectsql .= $sortsql ? ' ORDER BY ' . $sortsql : '';
+1 -1
View File
@@ -1407,7 +1407,7 @@ class mod_forum_external extends external_api {
$capabilitymanager = $managerfactory->get_capability_manager($forum);
// Does the user have the ability to favourite the discussion?
if (!$capabilitymanager->can_favourite_discussion($USER, $discussion)) {
if (!$capabilitymanager->can_favourite_discussion($USER)) {
throw new moodle_exception('cannotfavourite', 'forum');
}
$usercontext = context_user::instance($USER->id);