MDL-65129 mod_forum: Ability to search based on favourite status

This commit is contained in:
Peter 2019-05-15 15:26:52 +08:00
parent 8e9e9a5f7e
commit dd223096b8
5 changed files with 71 additions and 22 deletions

View File

@ -55,6 +55,10 @@ class big_search_form implements renderable, templatable {
public $tags;
/** @var string The URL of the search form. */
public $actionurl;
/** @var bool Is the user a guest user? */
public $guestuser;
/** @var bool Whether the include starredonly checkbox is checked. */
public $starredonly;
/**
* Constructor.
@ -63,9 +67,10 @@ class big_search_form implements renderable, templatable {
* @param object $user The user.
*/
public function __construct($course) {
global $DB;
global $DB, $USER;
$this->course = $course;
$this->tags = [];
$this->guestuser = !isloggedin() || isguestuser($USER);
$this->showfullwords = $DB->get_dbfamily() == 'mysql' || $DB->get_dbfamily() == 'postgres';
$this->actionurl = new moodle_url('/mod/forum/search.php');
@ -159,6 +164,15 @@ class big_search_form implements renderable, templatable {
$this->tags = $value;
}
/**
* Set starred only value.
*
* @param mixed $value Bool.
*/
public function set_starredonly($value) {
$this->starredonly = $value;
}
/**
* Forum ID setter search criteria.
*
@ -182,6 +196,8 @@ class big_search_form implements renderable, templatable {
$data->subject = $this->subject;
$data->user = $this->user;
$data->showfullwords = $this->showfullwords;
$data->guestuser = $this->guestuser;
$data->starredonly = $this->starredonly;
$data->actionurl = $this->actionurl->out(false);
$tagtypestoshow = \core_tag_area::get_showstandard('mod_forum', 'forum_posts');

View File

@ -310,6 +310,7 @@ $string['indicator:cognitivedepth'] = 'Forum cognitive';
$string['indicator:cognitivedepth_help'] = 'This indicator is based on the cognitive depth reached by the student in a Forum activity.';
$string['indicator:socialbreadth'] = 'Forum social';
$string['indicator:socialbreadth_help'] = 'This indicator is based on the social breadth reached by the student in a Forum activity.';
$string['starredonly'] = 'Search starred discussions only';
$string['inforum'] = 'in {$a}';
$string['inreplyto'] = 'In reply to {$a}';
$string['introblog'] = 'The posts in this forum were copied here automatically from blogs of users in this course because those blog entries are no longer available';

View File

@ -1107,6 +1107,18 @@ function forum_search_posts($searchterms, $courseid=0, $limitfrom=0, $limitnum=5
$where[] = "(d.forum $fullid_sql)";
}
$favjoin = "";
if (in_array('starredonly:on', $searchterms)) {
$usercontext = context_user::instance($USER->id);
$ufservice = \core_favourites\service_factory::get_service_for_user_context($usercontext);
list($favjoin, $favparams) = $ufservice->get_join_sql_by_type('mod_forum', 'discussions',
"favourited", "d.id");
$searchterms = array_values(array_diff($searchterms, array('starredonly:on')));
$params = array_merge($params, $favparams);
$extrasql .= " AND favourited.itemid IS NOT NULL AND favourited.itemid != 0";
}
$selectdiscussion = "(".implode(" OR ", $where).")";
$messagesearch = '';
@ -1132,36 +1144,39 @@ function forum_search_posts($searchterms, $courseid=0, $limitfrom=0, $limitnum=5
$tagjoins = '';
$tagfields = [];
$tagfieldcount = 0;
foreach ($parsearray as $token) {
if ($token->getType() == TOKEN_TAGS) {
for ($i = 0; $i <= substr_count($token->getValue(), ','); $i++) {
// Queries can only have a limited number of joins so set a limit sensible users won't exceed.
if ($tagfieldcount > 10) {
continue;
}
$tagjoins .= " LEFT JOIN {tag_instance} ti_$tagfieldcount
if ($parsearray) {
foreach ($parsearray as $token) {
if ($token->getType() == TOKEN_TAGS) {
for ($i = 0; $i <= substr_count($token->getValue(), ','); $i++) {
// Queries can only have a limited number of joins so set a limit sensible users won't exceed.
if ($tagfieldcount > 10) {
continue;
}
$tagjoins .= " LEFT JOIN {tag_instance} ti_$tagfieldcount
ON p.id = ti_$tagfieldcount.itemid
AND ti_$tagfieldcount.component = 'mod_forum'
AND ti_$tagfieldcount.itemtype = 'forum_posts'";
$tagjoins .= " LEFT JOIN {tag} t_$tagfieldcount ON t_$tagfieldcount.id = ti_$tagfieldcount.tagid";
$tagfields[] = "t_$tagfieldcount.rawname";
$tagfieldcount++;
$tagjoins .= " LEFT JOIN {tag} t_$tagfieldcount ON t_$tagfieldcount.id = ti_$tagfieldcount.tagid";
$tagfields[] = "t_$tagfieldcount.rawname";
$tagfieldcount++;
}
}
}
list($messagesearch, $msparams) = search_generate_SQL($parsearray, 'p.message', 'p.subject',
'p.userid', 'u.id', 'u.firstname',
'u.lastname', 'p.modified', 'd.forum',
$tagfields);
$params = ($msparams ? array_merge($params, $msparams) : $params);
}
list($messagesearch, $msparams) = search_generate_SQL($parsearray, 'p.message', 'p.subject',
'p.userid', 'u.id', 'u.firstname',
'u.lastname', 'p.modified', 'd.forum',
$tagfields);
$params = array_merge($params, $msparams);
}
$fromsql = "{forum_posts} p
INNER JOIN {forum_discussions} d ON d.id = p.discussion
INNER JOIN {user} u ON u.id = p.userid $tagjoins";
INNER JOIN {user} u ON u.id = p.userid $tagjoins $favjoin";
$selectsql = " $messagesearch
AND p.discussion = d.id
$selectsql = ($messagesearch ? $messagesearch . " AND " : "").
" p.discussion = d.id
AND p.userid = u.id
AND $selectdiscussion
$extrasql";

View File

@ -67,6 +67,7 @@ if ($timetorestrict) {
} else {
$dateto = optional_param('dateto', 0, PARAM_INT); // Ending date
}
$starredonly = optional_param('starredonly', false, PARAM_BOOL); // Include only favourites.
$PAGE->set_pagelayout('standard');
$PAGE->set_url($FULLME); //TODO: this is very sloppy --skodak
@ -105,6 +106,9 @@ if (empty($search)) { // Check the other parameters instead
if (!empty($tags)) {
$search .= ' tags:' . implode(',', $tags);
}
if (!empty($starredonly)) {
$search .= ' starredonly:on';
}
$individualparams = true;
} else {
$individualparams = false;
@ -203,7 +207,8 @@ $params = [
'notwords' => $notwords,
'dateto' => $dateto,
'datefrom' => $datefrom,
'showform' => 1
'showform' => 1,
'starredonly' => $starredonly
];
$url = new moodle_url("/mod/forum/search.php", $params);
foreach ($tags as $tag) {
@ -318,7 +323,8 @@ echo $OUTPUT->footer();
* @return void The function prints the form.
*/
function forum_print_big_search_form($course) {
global $PAGE, $words, $subject, $phrase, $user, $fullwords, $notwords, $datefrom, $dateto, $forumid, $tags;
global $PAGE, $words, $subject, $phrase, $user, $fullwords, $notwords, $datefrom,
$dateto, $forumid, $tags, $starredonly;
$renderable = new \mod_forum\output\big_search_form($course, $user);
$renderable->set_words($words);
@ -331,6 +337,7 @@ function forum_print_big_search_form($course) {
$renderable->set_user($user);
$renderable->set_forumid($forumid);
$renderable->set_tags($tags);
$renderable->set_starredonly($starredonly);
$output = $PAGE->get_renderer('mod_forum');
echo $output->render($renderable);

View File

@ -148,6 +148,16 @@
<input type="text" class="form-control" name="user" id="user" value="{{user}}">
</td>
</tr>
{{^guestuser}}
<tr>
<td class="c0 text-xs-right align-bottom">
<label for="starredonly">{{#str}}starredonly, forum{{/str}}</label>
</td>
<td class="c1 align-middle form-inline">
<input type="checkbox" class="form-control" name="starredonly" id="starredonly" value="1" {{#starredonly}}checked{{/starredonly}} />
</td>
</tr>
{{/guestuser}}
{{#tagsenabled}}
<tr>
<td class="c0">