Permalink
Browse files

ForumNG: Advanced search has various problems #3802

  • Loading branch information...
1 parent 870da54 commit 3f8c104fa708a2393c44c2cbe3285a04f185bae0 @sammarshallou sammarshallou committed May 24, 2012
Showing with 112 additions and 115 deletions.
  1. +96 −85 advancedsearch.php
  2. +12 −23 advancedsearchlib.php
  3. +3 −6 lang/en/forumng.php
  4. +1 −1 version.php
View
@@ -49,16 +49,14 @@ public function definition() {
// Date range_from to be filtered
$mform->addElement('date_time_selector', 'datefrom',
get_string('daterangefrom', 'forumng'),
- array('optional'=>true, 'step'=>1));
- // setConstant is used rather than setDefault as this allows the unchecking of the
- // the 'enable' checkbox, otherwise no difference in operation
- $mform->setConstant('datefrom', $this->_customdata['datefrom']);
+ array('optional' => true, 'startyear' => 2011, 'step' => 1,
+ 'defaulttime' => $this->_customdata['datefrom']));
// Date range_to to be filtered
$mform->addElement('date_time_selector', 'dateto',
get_string('daterangeto', 'forumng'),
- array('optional'=>true, 'step'=>1));
- $mform->setConstant('dateto', $this->_customdata['dateto']);
+ array('optional'=>true, 'startyear' => 2011, 'step' => 1,
+ 'defaulttime' => $this->_customdata['dateto']));
// Add help buttons
$mform->addHelpButton('query', 'words', 'forumng');
@@ -75,20 +73,15 @@ public function definition() {
public function validation($data, $files) {
$errors = parent::validation($data, $files);
- $df = $data['datefrom'];
- $dt = $data['dateto'];
- $datefrom = make_timestamp($df['year'], $df['month'], $df['day'], $df['hour'],
- $df['minute']);
- $dateto = make_timestamp($dt['year'], $dt['month'], $dt['day'], $dt['hour'],
- $dt['minute']);
- if ($datefrom > time() && !empty($data['datefrom']['enabled'])) {
+ $datefrom = $data['datefrom'];
+ $dateto = $data['dateto'];
+ if ($datefrom > time()) {
$errors['datefrom'] = get_string('inappropriatedateortime', 'forumng');
}
- if (($datefrom > $dateto) && !empty($data['dateto']['enabled'])) {
+ if (($datefrom > $dateto) && $dateto) {
$errors['dateto'] = get_string('daterangemismatch', 'forumng');
}
- if (($data['query'] == '') && ($data['author'] == '') &&
- empty($data['datefrom']['enabled']) && empty($data['dateto']['enabled'])) {
+ if (($data['query'] == '') && ($data['author'] == '') && !$datefrom && !$dateto) {
$errors['query'] = get_string('nosearchcriteria', 'forumng');
}
return $errors;
@@ -107,11 +100,11 @@ public function validation($data, $files) {
}
$query = trim(optional_param('query', '', PARAM_RAW));
if ($query !== '') {
- $url->param('query', rawurlencode($query));
+ $url->param('query', $query);
}
$author = trim(optional_param('author', '', PARAM_RAW));
if ($author !== '') {
- $url->param('author', rawurlencode($author));
+ $url->param('author', $author);
}
$cloneid = optional_param('clone', 0, PARAM_INT);
if ($cloneid) {
@@ -130,6 +123,23 @@ public function validation($data, $files) {
$url->param('dateto[' . $key . ']', $value);
}
}
+// The below are necessary to fool the form into thinking it was submitted again
+// when further requests are made for multiple pages / changing group. This is
+// kind of a horrible way to make the page but it means we can use get_data
+// instead of manually interpreting date dropdowns (incorrectly).
+$submitbutton = optional_param('submitbutton', '', PARAM_RAW);
+if ($submitbutton) {
+ $url->param('submitbutton', $submitbutton);
+}
+$sesskey = optional_param('sesskey', '', PARAM_RAW);
+if ($sesskey) {
+ $url->param('sesskey', $sesskey);
+}
+$form = optional_param('_qf__advancedsearch_form', 0, PARAM_INT);
+if ($form) {
+ $url->param('_qf__advancedsearch_form', $form);
+}
+
// Search in a single forum
if ($cmid) {
@@ -180,32 +190,24 @@ public function validation($data, $files) {
$inputdata->author = $author;
$editform->set_data($inputdata);
-$datefromint = $datetoint = 0;
-
if ($editform->is_cancelled()) {
if (isset($forum) ) {
$returnurl = $forum->get_url(mod_forumng::PARAM_PLAIN);
} else {
$returnurl = $CFG->wwwroot . '/course/view.php?id=' . $course->id;
}
redirect($returnurl, '', 0);
-} else if ($data = $editform->get_data()) {
- $df = $data->datefrom;
- $dt = $data->dateto;
- $datefromint = make_timestamp($df['year'], $df['month'], $df['day'], $df['hour'],
- $df['minute']);
- $datetoint = make_timestamp($dt['year'], $dt['month'], $dt['day'], $dt['hour'],
- $dt['minute']);
}
-$action = $query !== '' || $author !== '' || $datefromint || $datetoint ||
- !empty($datefrom) || !empty($dateto);
+// Process form data.
+$data = $editform->get_data();
// Display header
$out = mod_forumng_utils::get_renderer();
print $out->header();
-$searchtitle = forumng_get_search_results_title($query, $author, $datefromint, $datetoint);
+$searchtitle = forumng_get_search_results_title($query, $data ? $data->author : '',
+ $data ? $data->datefrom : 0, $data ? $data->dateto : 0);
if (!$allforums) {
// Display group selector if required
@@ -216,71 +218,80 @@ public function validation($data, $files) {
}
$editform->display();
-// Searching for free text with or without filtering author and date range
-if ($query) {
- $result = new local_ousearch_search($query);
- // Search all forums
- if ($allforums) {
- $result->set_plugin('mod/forumng');
- $result->set_course_id($courseid);
- $result->set_visible_modules_in_course($COURSE);
+if ($data) {
+ // Searching for free text with or without filtering author and date range.
+ if ($query) {
+ $result = new local_ousearch_search($query);
+ if ($allforums) {
+ // Search all forums.
+ // NOTE: I think this code branch is no longer used as we removed
+ // the 'all forums' facility to the resources_search block, but
+ // perhaps it may be used in standard Moodle installs somehow.
+ $result->set_plugin('mod/forumng');
+ $result->set_course_id($courseid);
+ $result->set_visible_modules_in_course($COURSE);
+
+ // Restrict them to the groups they belong to.
+ if (!isset($USER->groupmember[$courseid])) {
+ $result->set_group_ids(array());
+ } else {
+ $result->set_group_ids($USER->groupmember[$courseid]);
+ }
+ // Add exceptions where they can see other groups.
+ $result->set_group_exceptions(local_ousearch_search::get_group_exceptions($courseid));
- // Restrict them to the groups they belong to
- if (!isset($USER->groupmember[$courseid])) {
- $result->set_group_ids(array());
+ $result->set_user_id($USER->id);
} else {
- $result->set_group_ids($USER->groupmember[$courseid]);
+ // Search this forum.
+ $result->set_coursemodule($forum->get_course_module(true));
+ if ($groupid && $groupid!=mod_forumng::NO_GROUPS) {
+ $result->set_group_id($groupid);
+ }
}
- // Add exceptions where they can see other groups
- $result->set_group_exceptions(local_ousearch_search::get_group_exceptions($courseid));
+ // Pass necessary data to filter function using ugly global.
+ global $forumngfilteroptions;
+ $forumngfilteroptions = (object)array('author' => trim($data->author),
+ 'datefrom' => $data->datefrom, 'dateto' => $data->dateto);
+ $result->set_filter('forumng_exclude_words_filter');
+ print $result->display_results($url, $searchtitle);
+ } else {
+ // Searching without free text using author and/or date range.
+ $page = optional_param('page', 0, PARAM_INT);
+ $prevpage = $page-FORUMNG_SEARCH_RESULTSPERPAGE;
+ $prevrange = ($page-FORUMNG_SEARCH_RESULTSPERPAGE+1) . ' - ' . $page;
- $result->set_user_id($USER->id);
- } else {// Search this forum
- $result->set_coursemodule($forum->get_course_module(true));
- if ($groupid && $groupid!=mod_forumng::NO_GROUPS) {
- $result->set_group_id($groupid);
+ // Get result from database query.
+ if ($allforums) {
+ $results = forumng_get_results_for_all_forums($course, trim($data->author),
+ $data->datefrom, $data->dateto, $page);
+ } else {
+ $results = forumng_get_results_for_this_forum($forum, $groupid, trim($data->author),
+ $data->datefrom, $data->dateto, $page);
}
- }
- $result->set_filter('forumng_exclude_words_filter');
- print $result->display_results($url, $searchtitle);
-
-// Searching without free text using author and/or date range
-} else if ($action) {
- $page = optional_param('page', 0, PARAM_INT);
- $prevpage = $page-FORUMNG_SEARCH_RESULTSPERPAGE;
- $prevrange = ($page-FORUMNG_SEARCH_RESULTSPERPAGE+1) . ' - ' . $page;
+ $nextpage = $page + FORUMNG_SEARCH_RESULTSPERPAGE;
- //Get result from db
- if ($allforums) {
- $results = forumng_get_results_for_all_forums($course, $author,
- $datefromint, $datetoint, $page);
- } else {
- $results = forumng_get_results_for_this_forum($forum, $groupid, $author,
- $datefromint, $datetoint, $page);
- }
- $nextpage = $page + FORUMNG_SEARCH_RESULTSPERPAGE;
+ $linknext = null;
+ $linkprev = null;
- $linknext = null;
- $linkprev = null;
-
- if ($results->success) {
- if (($page-FORUMNG_SEARCH_RESULTSPERPAGE+1)>0) {
- $url->param('page', $prevpage);
- $linkprev = $url->out(false);
- }
- if ($results->numberofentries == FORUMNG_SEARCH_RESULTSPERPAGE) {
- $url->param('page', $nextpage);
- $linknext = $url->out(false);
+ if ($results->success) {
+ if (($page-FORUMNG_SEARCH_RESULTSPERPAGE+1)>0) {
+ $url->param('page', $prevpage);
+ $linkprev = $url->out(false);
+ }
+ if ($results->numberofentries == FORUMNG_SEARCH_RESULTSPERPAGE) {
+ $url->param('page', $nextpage);
+ $linknext = $url->out(false);
+ }
}
- }
- if ($results->done ===1) {
- if (($page-FORUMNG_SEARCH_RESULTSPERPAGE+1)>0) {
- $url->param('page', $prevpage);
- $linkprev = $url->out(false);
+ if ($results->done ===1) {
+ if (($page-FORUMNG_SEARCH_RESULTSPERPAGE+1)>0) {
+ $url->param('page', $prevpage);
+ $linkprev = $url->out(false);
+ }
}
+ print local_ousearch_search::format_results($results, $searchtitle, $page+1, $linkprev,
+ $prevrange, $linknext, $results->searchtime);
}
- print local_ousearch_search::format_results($results, $searchtitle, $page+1, $linkprev,
- $prevrange, $linknext, $results->searchtime);
}
print $out->footer();
View
@@ -30,31 +30,24 @@
* @return boolean
*/
function forumng_exclude_words_filter($result) {
- $author = trim(optional_param('author', null, PARAM_RAW));
- $drfa = optional_param_array('datefrom', 0, PARAM_INT);
- $drta = optional_param_array('dateto', 0, PARAM_INT);
+ global $forumngfilteroptions;
+ $author = $forumngfilteroptions->author;
+ $daterangefrom = $forumngfilteroptions->datefrom;
+ $daterangeto = $forumngfilteroptions->dateto;
// Filter the output based on the input string for "Author name" field
if (!forumng_find_this_user($result->intref1, $author)) {
return false;
}
// Filter the output based on input date for "Date range from" field
- if (count($drfa) > 1 ) {
- $daterangefrom = make_timestamp($drfa['year'], $drfa['month'], $drfa['day'],
- $drfa['hour'], $drfa['minute'], 0);
- if ($daterangefrom && $daterangefrom > $result->timemodified) {
- return false;
- }
+ if ($daterangefrom && $daterangefrom > $result->timemodified) {
+ return false;
}
// Filter the output based on input date for "Date range to" field
- if (count($drta) > 1 ) {
- $daterangeto = make_timestamp($drta['year'], $drta['month'], $drta['day'],
- $drta['hour'], $drta['minute'], 0);
- if ($daterangeto && $daterangeto < $result->timemodified) {
- return false;
- }
+ if ($daterangeto && $daterangeto < $result->timemodified) {
+ return false;
}
return true;
}
@@ -99,6 +92,10 @@ function forumng_get_results_for_this_forum($forum, $groupid, $author=null, $dat
$where .= " AND p.modified<=?";
$params[] = $daterangeto;
}
+ if ($groupid) {
+ $where .= " AND (d.groupid = ? OR d.groupid IS NULL)";
+ $params[] = $groupid;
+ }
$sql = "SELECT p.modified, p.id, p.discussionid, p.userid, p.parentpostid,
p.subject AS title, p.message AS summary, u.username, u.firstname,
@@ -125,13 +122,6 @@ function forumng_get_results_for_this_forum($forum, $groupid, $author=null, $dat
$post->summary = s(strip_tags(shorten_text($post->summary, 250)));
$post->url = $CFG->wwwroot ."/mod/forumng/discuss.php?d=$post->discussionid" .
$forum->get_clone_param(mod_forumng::PARAM_PLAIN) . "#p$post->id";
-
- // Check group
- if ($groupid && $groupid!=mod_forumng::NO_GROUPS) {
- if (groups_is_member($groupid, $post->userid)) {
- $groupposts[] = $post;
- }
- }
}
$results->results = $groupposts ? $groupposts : $posts;
$results->searchtime = microtime(true) - $before;
@@ -171,7 +161,6 @@ function forumng_get_results_for_all_forums($course, $author=null, $daterangefro
$visibleforums[$cm->instance] = $cm->groupmode;
// Check access all groups for this forum, if they have it, add to list
- //$forum = mod_forumng::get_from_cmid($cm->id, 0);
$forum = mod_forumng::get_from_id($cm->instance, mod_forumng::CLONE_DIRECT);
if ($forum->get_group_mode() == SEPARATEGROUPS) {
if (has_capability('moodle/site:accessallgroups', $forum->get_context())) {
View
@@ -728,12 +728,9 @@
$string['author'] = ' author: \"{$a}\"';
$string['from'] = ' from: {$a}';
$string['to'] = ' to: {$a}';
-$string['inappropriatedateortime'] = '<strong>Date range from</strong> is after current date/time.
-Please check and try again!';
-$string['daterangemismatch'] = 'Date range mismatch: <strong>Date range from</strong> is after
-<strong>Date range to</strong>. Please check and try again!';
-$string['nosearchcriteria'] = 'No search criteria: Please use one or more of the criteria below
-and try again!';
+$string['inappropriatedateortime'] = 'From date cannot be after present.';
+$string['daterangemismatch'] = 'To date is before From date.';
+$string['nosearchcriteria'] = 'No search criteria. Please use one or more of the criteria below.';
$string['searchallforums'] = 'Search all forums';
$string['replies'] = 'Replies';
View
@@ -22,7 +22,7 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-$module->version = 2012030701;
+$module->version = 2012052200;
$module->requires = 2011120500; // 2.2.0
$module->cron = 60;

0 comments on commit 3f8c104

Please sign in to comment.