MDL-57660 mod_forum: Preserve forum ID selection after form submission
[moodle.git] / mod / forum / classes / output / big_search_form.php
CommitLineData
10deddd3
FM
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Big search form.
19 *
20 * @package mod_forum
21 * @copyright 2016 Frédéric Massart - FMCorz.net
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25namespace mod_forum\output;
26defined('MOODLE_INTERNAL') || die();
27
28use html_writer;
29use moodle_url;
30use renderable;
31use renderer_base;
32use stdClass;
33use templatable;
34
35/**
36 * Big search form class.
37 *
38 * @package mod_forum
39 * @copyright 2016 Frédéric Massart - FMCorz.net
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 */
42class big_search_form implements renderable, templatable {
43
44 public $course;
45 public $datefrom;
46 public $dateto;
47 public $forumoptions;
48 public $fullwords;
49 public $notwords;
50 public $phrase;
10deddd3
FM
51 public $showfullwords;
52 public $subject;
53 public $user;
54 public $words;
d679a45f
MC
55 /** @var string The URL of the search form. */
56 public $actionurl;
10deddd3
FM
57
58 /**
59 * Constructor.
60 *
61 * @param object $course The course.
62 * @param object $user The user.
63 */
64 public function __construct($course) {
65 global $DB;
66 $this->course = $course;
10deddd3 67 $this->showfullwords = $DB->get_dbfamily() == 'mysql' || $DB->get_dbfamily() == 'postgres';
d679a45f 68 $this->actionurl = new moodle_url('/mod/forum/search.php');
10deddd3
FM
69
70 $forumoptions = ['' => get_string('allforums', 'forum')] + forum_menu_list($course);
71 $this->forumoptions = array_map(function($option) use ($forumoptions) {
72 return [
73 'value' => $option,
74 'name' => $forumoptions[$option]
75 ];
76 }, array_keys($forumoptions));
77 }
78
79 /**
80 * Set date from.
81 *
82 * @param mixed $value Date from.
83 */
84 public function set_datefrom($value) {
85 $this->datefrom = $value;
86 }
87
88 /**
89 * Set date to.
90 *
91 * @param mixed $value Date to.
92 */
93 public function set_dateto($value) {
94 $this->dateto = $value;
95 }
96
97 /**
98 * Set full words.
99 *
100 * @param mixed $value Full words.
101 */
102 public function set_fullwords($value) {
103 $this->fullwords = $value;
104 }
105
106 /**
107 * Set not words.
108 *
109 * @param mixed $value Not words.
110 */
111 public function set_notwords($value) {
112 $this->notwords = $value;
113 }
114
115 /**
116 * Set phrase.
117 *
118 * @param mixed $value Phrase.
119 */
120 public function set_phrase($value) {
121 $this->phrase = $value;
122 }
123
124 /**
125 * Set subject.
126 *
127 * @param mixed $value Subject.
128 */
129 public function set_subject($value) {
130 $this->subject = $value;
131 }
132
133 /**
134 * Set user.
135 *
136 * @param mixed $value User.
137 */
138 public function set_user($value) {
139 $this->user = $value;
140 }
141
142 /**
143 * Set words.
144 *
145 * @param mixed $value Words.
146 */
147 public function set_words($value) {
148 $this->words = $value;
149 }
150
1da58a1a
JP
151 /**
152 * Forum ID setter search criteria.
153 *
154 * @param int $forumid The forum ID.
155 */
156 public function set_forumid($forumid) {
157 $this->forumid = $forumid;
158 }
159
10deddd3
FM
160 public function export_for_template(renderer_base $output) {
161 $data = new stdClass();
162
10deddd3
FM
163 $data->courseid = $this->course->id;
164 $data->words = $this->words;
165 $data->phrase = $this->phrase;
166 $data->notwords = $this->notwords;
167 $data->fullwords = $this->fullwords;
168 $data->datefromchecked = !empty($this->datefrom);
169 $data->datetochecked = !empty($this->dateto);
170 $data->subject = $this->subject;
171 $data->user = $this->user;
172 $data->showfullwords = $this->showfullwords;
d679a45f 173 $data->actionurl = $this->actionurl->out(false);
10deddd3
FM
174
175 $datefrom = $this->datefrom;
176 if (empty($datefrom)) {
177 $datefrom = make_timestamp(2000, 1, 1, 0, 0, 0);
178 }
179
180 $dateto = $this->dateto;
181 if (empty($dateto)) {
182 $dateto = time() + HOURSECS;
183 }
184
185 $data->datefromfields = html_writer::select_time('days', 'fromday', $datefrom)
186 . html_writer::select_time('months', 'frommonth', $datefrom)
187 . html_writer::select_time('years', 'fromyear', $datefrom)
188 . html_writer::select_time('hours', 'fromhour', $datefrom)
189 . html_writer::select_time('minutes', 'fromminute', $datefrom);
190
191 $data->datetofields = html_writer::select_time('days', 'today', $dateto)
192 . html_writer::select_time('months', 'tomonth', $dateto)
193 . html_writer::select_time('years', 'toyear', $dateto)
194 . html_writer::select_time('hours', 'tohour', $dateto)
195 . html_writer::select_time('minutes', 'tominute', $dateto);
196
1da58a1a
JP
197 if ($this->forumid && !empty($this->forumoptions)) {
198 foreach ($this->forumoptions as $index => $option) {
199 if ($option['value'] == $this->forumid) {
200 $this->forumoptions[$index]['selected'] = true;
201 } else {
202 $this->forumoptions[$index]['selected'] = false;
203 }
204 }
205 }
10deddd3
FM
206 $data->forumoptions = $this->forumoptions;
207
208 return $data;
209 }
210
211}