MDL-55594 mod_forum: Convert 'big' search form to templates

Part of MDL-55071
This commit is contained in:
Frederic Massart 2016-08-17 14:29:04 +08:00 committed by Dan Poltawski
parent 14395f9789
commit 10deddd34e
8 changed files with 473 additions and 123 deletions

View File

@ -0,0 +1,192 @@
<?php
// 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/>.
/**
* Big search form.
*
* @package mod_forum
* @copyright 2016 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_forum\output;
defined('MOODLE_INTERNAL') || die();
use html_writer;
use moodle_url;
use renderable;
use renderer_base;
use stdClass;
use templatable;
/**
* Big search form class.
*
* @package mod_forum
* @copyright 2016 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class big_search_form implements renderable, templatable {
public $course;
public $datefrom;
public $dateto;
public $forumoptions;
public $fullwords;
public $notwords;
public $phrase;
public $scripturl;
public $showfullwords;
public $subject;
public $user;
public $words;
/**
* Constructor.
*
* @param object $course The course.
* @param object $user The user.
*/
public function __construct($course) {
global $DB;
$this->course = $course;
$this->scripturl = new moodle_url('/mod/forum/forum.js');
$this->showfullwords = $DB->get_dbfamily() == 'mysql' || $DB->get_dbfamily() == 'postgres';
$forumoptions = ['' => get_string('allforums', 'forum')] + forum_menu_list($course);
$this->forumoptions = array_map(function($option) use ($forumoptions) {
return [
'value' => $option,
'name' => $forumoptions[$option]
];
}, array_keys($forumoptions));
}
/**
* Set date from.
*
* @param mixed $value Date from.
*/
public function set_datefrom($value) {
$this->datefrom = $value;
}
/**
* Set date to.
*
* @param mixed $value Date to.
*/
public function set_dateto($value) {
$this->dateto = $value;
}
/**
* Set full words.
*
* @param mixed $value Full words.
*/
public function set_fullwords($value) {
$this->fullwords = $value;
}
/**
* Set not words.
*
* @param mixed $value Not words.
*/
public function set_notwords($value) {
$this->notwords = $value;
}
/**
* Set phrase.
*
* @param mixed $value Phrase.
*/
public function set_phrase($value) {
$this->phrase = $value;
}
/**
* Set subject.
*
* @param mixed $value Subject.
*/
public function set_subject($value) {
$this->subject = $value;
}
/**
* Set user.
*
* @param mixed $value User.
*/
public function set_user($value) {
$this->user = $value;
}
/**
* Set words.
*
* @param mixed $value Words.
*/
public function set_words($value) {
$this->words = $value;
}
public function export_for_template(renderer_base $output) {
$data = new stdClass();
$data->scripturl = $this->scripturl->out(false);
$data->courseid = $this->course->id;
$data->words = $this->words;
$data->phrase = $this->phrase;
$data->notwords = $this->notwords;
$data->fullwords = $this->fullwords;
$data->datefromchecked = !empty($this->datefrom);
$data->datetochecked = !empty($this->dateto);
$data->subject = $this->subject;
$data->user = $this->user;
$data->showfullwords = $this->showfullwords;
$datefrom = $this->datefrom;
if (empty($datefrom)) {
$datefrom = make_timestamp(2000, 1, 1, 0, 0, 0);
}
$dateto = $this->dateto;
if (empty($dateto)) {
$dateto = time() + HOURSECS;
}
$data->datefromfields = html_writer::select_time('days', 'fromday', $datefrom)
. html_writer::select_time('months', 'frommonth', $datefrom)
. html_writer::select_time('years', 'fromyear', $datefrom)
. html_writer::select_time('hours', 'fromhour', $datefrom)
. html_writer::select_time('minutes', 'fromminute', $datefrom);
$data->datetofields = html_writer::select_time('days', 'today', $dateto)
. html_writer::select_time('months', 'tomonth', $dateto)
. html_writer::select_time('years', 'toyear', $dateto)
. html_writer::select_time('hours', 'tohour', $dateto)
. html_writer::select_time('minutes', 'tominute', $dateto);
$data->forumoptions = $this->forumoptions;
return $data;
}
}

View File

@ -236,4 +236,14 @@ class mod_forum_renderer extends plugin_renderer_base {
public function render_quick_search_form(\mod_forum\output\quick_search_form $form) {
return $this->render_from_template('mod_forum/quick_search_form', $form->export_for_template($this));
}
/**
* Render big search form.
*
* @param \mod_forum\output\big_search_form $form The renderable.
* @return string
*/
public function render_big_search_form(\mod_forum\output\big_search_form $form) {
return $this->render_from_template('mod_forum/big_search_form', $form->export_for_template($this));
}
}

View File

@ -318,122 +318,20 @@ echo $OUTPUT->footer();
* @return void The function prints the form.
*/
function forum_print_big_search_form($course) {
global $CFG, $DB, $words, $subject, $phrase, $user, $userid, $fullwords, $notwords, $datefrom, $dateto, $PAGE, $OUTPUT;
global $PAGE, $words, $subject, $phrase, $user, $userid, $fullwords, $notwords, $datefrom, $dateto, $OUTPUT;
echo $OUTPUT->box(get_string('searchforumintro', 'forum'), 'searchbox boxaligncenter', 'intro');
$renderable = new \mod_forum\output\big_search_form($course, $user);
$renderable->set_words($words);
$renderable->set_phrase($phrase);
$renderable->set_notwords($notwords);
$renderable->set_fullwords($fullwords);
$renderable->set_datefrom($datefrom);
$renderable->set_dateto($dateto);
$renderable->set_subject($subject);
$renderable->set_user($user);
echo $OUTPUT->box_start('generalbox boxaligncenter');
echo html_writer::script('', $CFG->wwwroot.'/mod/forum/forum.js');
echo '<form id="searchform" action="search.php" method="get">';
echo '<table cellpadding="10" class="searchbox" id="form">';
echo '<tr>';
echo '<td class="c0"><label for="words">'.get_string('searchwords', 'forum').'</label>';
echo '<input type="hidden" value="'.$course->id.'" name="id" alt="" /></td>';
echo '<td class="c1"><input type="text" size="35" name="words" id="words"value="'.s($words, true).'" alt="" /></td>';
echo '</tr>';
echo '<tr>';
echo '<td class="c0"><label for="phrase">'.get_string('searchphrase', 'forum').'</label></td>';
echo '<td class="c1"><input type="text" size="35" name="phrase" id="phrase" value="'.s($phrase, true).'" alt="" /></td>';
echo '</tr>';
echo '<tr>';
echo '<td class="c0"><label for="notwords">'.get_string('searchnotwords', 'forum').'</label></td>';
echo '<td class="c1"><input type="text" size="35" name="notwords" id="notwords" value="'.s($notwords, true).'" alt="" /></td>';
echo '</tr>';
if ($DB->get_dbfamily() == 'mysql' || $DB->get_dbfamily() == 'postgres') {
echo '<tr>';
echo '<td class="c0"><label for="fullwords">'.get_string('searchfullwords', 'forum').'</label></td>';
echo '<td class="c1"><input type="text" size="35" name="fullwords" id="fullwords" value="'.s($fullwords, true).'" alt="" /></td>';
echo '</tr>';
}
echo '<tr>';
echo '<td class="c0">'.get_string('searchdatefrom', 'forum').'</td>';
echo '<td class="c1">';
if (empty($datefrom)) {
$datefromchecked = '';
$datefrom = make_timestamp(2000, 1, 1, 0, 0, 0);
}else{
$datefromchecked = 'checked="checked"';
}
echo '<input name="timefromrestrict" type="checkbox" value="1" alt="'.get_string('searchdatefrom', 'forum').'" onclick="return lockoptions(\'searchform\', \'timefromrestrict\', timefromitems)" '. $datefromchecked . ' /> ';
$selectors = html_writer::select_time('days', 'fromday', $datefrom)
. html_writer::select_time('months', 'frommonth', $datefrom)
. html_writer::select_time('years', 'fromyear', $datefrom)
. html_writer::select_time('hours', 'fromhour', $datefrom)
. html_writer::select_time('minutes', 'fromminute', $datefrom);
echo $selectors;
echo '<input type="hidden" name="hfromday" value="0" />';
echo '<input type="hidden" name="hfrommonth" value="0" />';
echo '<input type="hidden" name="hfromyear" value="0" />';
echo '<input type="hidden" name="hfromhour" value="0" />';
echo '<input type="hidden" name="hfromminute" value="0" />';
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<td class="c0">'.get_string('searchdateto', 'forum').'</td>';
echo '<td class="c1">';
if (empty($dateto)) {
$datetochecked = '';
$dateto = time()+3600;
}else{
$datetochecked = 'checked="checked"';
}
echo '<input name="timetorestrict" type="checkbox" value="1" alt="'.get_string('searchdateto', 'forum').'" onclick="return lockoptions(\'searchform\', \'timetorestrict\', timetoitems)" ' .$datetochecked. ' /> ';
$selectors = html_writer::select_time('days', 'today', $dateto)
. html_writer::select_time('months', 'tomonth', $dateto)
. html_writer::select_time('years', 'toyear', $dateto)
. html_writer::select_time('hours', 'tohour', $dateto)
. html_writer::select_time('minutes', 'tominute', $dateto);
echo $selectors;
echo '<input type="hidden" name="htoday" value="0" />';
echo '<input type="hidden" name="htomonth" value="0" />';
echo '<input type="hidden" name="htoyear" value="0" />';
echo '<input type="hidden" name="htohour" value="0" />';
echo '<input type="hidden" name="htominute" value="0" />';
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<td class="c0"><label for="menuforumid">'.get_string('searchwhichforums', 'forum').'</label></td>';
echo '<td class="c1">';
echo html_writer::select(forum_menu_list($course), 'forumid', '', array(''=>get_string('allforums', 'forum')));
echo '</td>';
echo '</tr>';
echo '<tr>';
echo '<td class="c0"><label for="subject">'.get_string('searchsubject', 'forum').'</label></td>';
echo '<td class="c1"><input type="text" size="35" name="subject" id="subject" value="'.s($subject, true).'" alt="" /></td>';
echo '</tr>';
echo '<tr>';
echo '<td class="c0"><label for="user">'.get_string('searchuser', 'forum').'</label></td>';
echo '<td class="c1"><input type="text" size="35" name="user" id="user" value="'.s($user, true).'" alt="" /></td>';
echo '</tr>';
echo '<tr>';
echo '<td class="submit" colspan="2" align="center">';
echo '<input type="submit" value="'.get_string('searchforums', 'forum').'" alt="" /></td>';
echo '</tr>';
echo '</table>';
echo '</form>';
echo html_writer::script(js_writer::function_call('lockoptions_timetoitems'));
echo html_writer::script(js_writer::function_call('lockoptions_timefromitems'));
echo $OUTPUT->box_end();
$output = $PAGE->get_renderer('mod_forum');
echo $output->render($renderable);
}
/**

View File

@ -0,0 +1,129 @@
{{!
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/>.
}}
{{!
Big search form.
}}
<div id="intro" class="box searchbox boxaligncenter">
{{#str}}searchforumintro, forum{{/str}}
</div>
<div class="box generalbox boxaligncenter">
<script type="text/javascript" src="{{scripturl}}"></script>
<form id="searchform" action="{{actionurl}}" method="get">
<table class="searchbox" id="form" cellpadding="10">
<tr>
<td class="c0">
<label for="words">{{#str}}searchwords, forum{{/str}}</label>
<input type="hidden" value="{{courseid}}" name="id">
</td>
<td class="c1">
<input type="text" size="35" name="words" id="words" value="{{words}}">
</td>
</tr>
<tr>
<td class="c0">
<label for="phrase">{{#str}}searchphrase, forum{{/str}}</label>
</td>
<td class="c1">
<input type="text" size="35" name="phrase" id="phrase" value="{{phrase}}">
</td>
</tr>
<tr>
<td class="c0">
<label for="notwords">{{#str}}searchnotwords, forum{{/str}}</label>
</td>
<td class="c1">
<input type="text" size="35" name="notwords" id="notwords" value="{{notwords}}">
</td>
</tr>
{{#showfullwords}}
<tr>
<td class="c0">
<label for="fullwords">{{#str}}searchfullwords, forum{{/str}}</label>
</td>
<td class="c1">
<input type="text" size="35" name="fullwords" id="fullwords" value="{{fullwords}}">
</td>
</tr>
{{/showfullwords}}
<tr>
<td class="c0">
{{#str}}searchdatefrom, forum{{/str}}
</td>
<td class="c1">
<input type="checkbox" name="timefromrestrict" value="1" onclick="return lockoptions('searchform', 'timefromrestrict', timefromitems)" {{#datefromchecked}}checked{{/datefromchecked}}>
{{{datefromfields}}}
<input type="hidden" name="hfromday" value="0">
<input type="hidden" name="hfrommonth" value="0">
<input type="hidden" name="hfromyear" value="0">
<input type="hidden" name="hfromhour" value="0">
<input type="hidden" name="hfromminute" value="0">
</td>
</tr>
<tr>
<td class="c0">
{{#str}}searchdateto, forum{{/str}}
</td>
<td class="c1">
<input type="checkbox" name="timetorestrict" value="1" onclick="return lockoptions('searchform', 'timetorestrict', timetoitems)" {{#datetochecked}}checked{{/datetochecked}}>
{{{datetofields}}}
<input type="hidden" name="htoday" value="0">
<input type="hidden" name="htomonth" value="0">
<input type="hidden" name="htoyear" value="0">
<input type="hidden" name="htohour" value="0">
<input type="hidden" name="htominute" value="0">
</td>
</tr>
<tr>
<td class="c0">
<label for="menuforumid">{{#str}}searchwhichforums, forum{{/str}}</label>
</td>
<td class="c1">
<select name="menuforumid" id="menuforumid">
{{#forumoptions}}
<option value="{{value}}">{{name}}</option>
{{/forumoptions}}
</select>
</td>
</tr>
<tr>
<td class="c0">
<label for="subject">{{#str}}searchsubject, forum{{/str}}</label>
</td>
<td class="c1">
<input type="text" size="35" name="subject" id="subject" value="{{subject}}">
</td>
</tr>
<tr>
<td class="c0">
<label for="user">{{#str}}searchuser, forum{{/str}}</label>
</td>
<td class="c1">
<input type="text" size="35" name="user" id="user" value="{{user}}">
</td>
</tr>
<tr>
<td colspan="2" class="submit" align="center">
<input type="submit" value={{#quote}}{{#str}}searchforums, forum{{/str}}{{/quote}}>
</td>
</tr>
</table>
</form>
{{#js}}
lockoptions_timetoitems();
lockoptions_timefromitems();
{{/js}}
</div>

View File

@ -4,7 +4,6 @@ table.grading-report,
table#listdirectories,
table.rolecaps,
table.userenrolment,
table#form,
form#movecourses table,
#page-admin-course-index .editcourse,
.forumheaderlist,

View File

@ -63,10 +63,6 @@ input#id_externalurl {
display: block;
}
#page-mod-forum-search .c1 .form-horizontal {
@extend .controls;
}
.form-item .form-inline {
display: inline;
}
@ -88,7 +84,6 @@ input#id_externalurl {
}
// Pale grey container for submit buttons.
table#form td.submit,
.form-buttons,
.path-admin .buttons,
#fitem_id_submitbutton,

View File

@ -28,7 +28,6 @@
.form-item .form-setting,
.form-item .form-description,
.mform .fitem .felement,
#page-mod-forum-search .c1,
.mform .fdescription.required,
.userprofile dl.list dd,
.form-horizontal .controls {
@ -92,7 +91,6 @@
.form-item .form-setting,
.form-item .form-description,
.mform .fitem .felement,
#page-mod-forum-search .c1,
.mform .fdescription.required,
.userprofile dl.list dd,
.form-horizontal .controls {
@ -285,7 +283,6 @@
.form-item .form-description {
margin-left: 0;
}
table#form td.submit,
.form-buttons,
#fitem_id_submitbutton,
.fp-content-center form + div,

View File

@ -0,0 +1,130 @@
{{!
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/>.
}}
{{!
Big search form.
}}
<div id="intro" class="box searchbox boxaligncenter">
{{#str}}searchforumintro, forum{{/str}}
</div>
<div class="box generalbox boxaligncenter">
<script type="text/javascript" src="{{scripturl}}"></script>
<form id="searchform" action="{{actionurl}}" method="get">
<table class="searchbox table table-striped" id="form" cellpadding="10">
<tr>
<td class="c0 text-xs-right">
<label for="words">{{#str}}searchwords, forum{{/str}}</label>
<input type="hidden" value="{{courseid}}" name="id">
</td>
<td class="c1">
<input type="text" class="form-control" name="words" id="words" value="{{words}}">
</td>
</tr>
<tr>
<td class="c0 text-xs-right">
<label for="phrase">{{#str}}searchphrase, forum{{/str}}</label>
</td>
<td class="c1">
<input type="text" class="form-control" name="phrase" id="phrase" value="{{phrase}}">
</td>
</tr>
<tr>
<td class="c0 text-xs-right">
<label for="notwords">{{#str}}searchnotwords, forum{{/str}}</label>
</td>
<td class="c1">
<input type="text" class="form-control" name="notwords" id="notwords" value="{{notwords}}">
</td>
</tr>
{{#showfullwords}}
<tr>
<td class="c0 text-xs-right">
<label for="fullwords">{{#str}}searchfullwords, forum{{/str}}</label>
</td>
<td class="c1">
<input type="text" class="form-control" name="fullwords" id="fullwords" value="{{fullwords}}">
</td>
</tr>
{{/showfullwords}}
<tr>
<td class="c0 text-xs-right">
{{#str}}searchdatefrom, forum{{/str}}
</td>
<td class="c1 text-nowrap form-inline">
<input type="checkbox" name="timefromrestrict" value="1" onclick="return lockoptions('searchform', 'timefromrestrict', timefromitems)" {{#datefromchecked}}checked{{/datefromchecked}}>
{{{datefromfields}}}
<input type="hidden" name="hfromday" value="0">
<input type="hidden" name="hfrommonth" value="0">
<input type="hidden" name="hfromyear" value="0">
<input type="hidden" name="hfromhour" value="0">
<input type="hidden" name="hfromminute" value="0">
</td>
</tr>
<tr>
<td class="c0 text-xs-right">
{{#str}}searchdateto, forum{{/str}}
</td>
<td class="c1 text-nowrap form-inline">
<input type="checkbox" name="timetorestrict" value="1" onclick="return lockoptions('searchform', 'timetorestrict', timetoitems)" {{#datetochecked}}checked{{/datetochecked}}>
{{{datetofields}}}
<input type="hidden" name="htoday" value="0">
<input type="hidden" name="htomonth" value="0">
<input type="hidden" name="htoyear" value="0">
<input type="hidden" name="htohour" value="0">
<input type="hidden" name="htominute" value="0">
</td>
</tr>
<tr>
<td class="c0 text-xs-right">
<label for="menuforumid">{{#str}}searchwhichforums, forum{{/str}}</label>
</td>
<td class="c1">
<select name="menuforumid" id="menuforumid" class="form-control">
{{#forumoptions}}
<option value="{{value}}">{{name}}</option>
{{/forumoptions}}
</select>
</td>
</tr>
<tr>
<td class="c0 text-xs-right">
<label for="subject">{{#str}}searchsubject, forum{{/str}}</label>
</td>
<td class="c1">
<input type="text" class="form-control" name="subject" id="subject" value="{{subject}}">
</td>
</tr>
<tr>
<td class="c0 text-xs-right">
<label for="user">{{#str}}searchuser, forum{{/str}}</label>
</td>
<td class="c1">
<input type="text" class="form-control" name="user" id="user" value="{{user}}">
</td>
</tr>
<tr>
<td> </td>
<td class="submit">
<button type="submit" class="btn btn-primary">{{#str}}searchforums, forum{{/str}}</button>
</td>
</tr>
</table>
</form>
{{#js}}
lockoptions_timetoitems();
lockoptions_timefromitems();
{{/js}}
</div>