Merge branch 'wip_MDL-57590_master_forum_search' of https://github.com/mchurchward...
[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;
51 public $scripturl;
52 public $showfullwords;
53 public $subject;
54 public $user;
55 public $words;
d679a45f
MC
56 /** @var string The URL of the search form. */
57 public $actionurl;
10deddd3
FM
58
59 /**
60 * Constructor.
61 *
62 * @param object $course The course.
63 * @param object $user The user.
64 */
65 public function __construct($course) {
66 global $DB;
67 $this->course = $course;
68 $this->scripturl = new moodle_url('/mod/forum/forum.js');
69 $this->showfullwords = $DB->get_dbfamily() == 'mysql' || $DB->get_dbfamily() == 'postgres';
d679a45f 70 $this->actionurl = new moodle_url('/mod/forum/search.php');
10deddd3
FM
71
72 $forumoptions = ['' => get_string('allforums', 'forum')] + forum_menu_list($course);
73 $this->forumoptions = array_map(function($option) use ($forumoptions) {
74 return [
75 'value' => $option,
76 'name' => $forumoptions[$option]
77 ];
78 }, array_keys($forumoptions));
79 }
80
81 /**
82 * Set date from.
83 *
84 * @param mixed $value Date from.
85 */
86 public function set_datefrom($value) {
87 $this->datefrom = $value;
88 }
89
90 /**
91 * Set date to.
92 *
93 * @param mixed $value Date to.
94 */
95 public function set_dateto($value) {
96 $this->dateto = $value;
97 }
98
99 /**
100 * Set full words.
101 *
102 * @param mixed $value Full words.
103 */
104 public function set_fullwords($value) {
105 $this->fullwords = $value;
106 }
107
108 /**
109 * Set not words.
110 *
111 * @param mixed $value Not words.
112 */
113 public function set_notwords($value) {
114 $this->notwords = $value;
115 }
116
117 /**
118 * Set phrase.
119 *
120 * @param mixed $value Phrase.
121 */
122 public function set_phrase($value) {
123 $this->phrase = $value;
124 }
125
126 /**
127 * Set subject.
128 *
129 * @param mixed $value Subject.
130 */
131 public function set_subject($value) {
132 $this->subject = $value;
133 }
134
135 /**
136 * Set user.
137 *
138 * @param mixed $value User.
139 */
140 public function set_user($value) {
141 $this->user = $value;
142 }
143
144 /**
145 * Set words.
146 *
147 * @param mixed $value Words.
148 */
149 public function set_words($value) {
150 $this->words = $value;
151 }
152
153 public function export_for_template(renderer_base $output) {
154 $data = new stdClass();
155
156 $data->scripturl = $this->scripturl->out(false);
157 $data->courseid = $this->course->id;
158 $data->words = $this->words;
159 $data->phrase = $this->phrase;
160 $data->notwords = $this->notwords;
161 $data->fullwords = $this->fullwords;
162 $data->datefromchecked = !empty($this->datefrom);
163 $data->datetochecked = !empty($this->dateto);
164 $data->subject = $this->subject;
165 $data->user = $this->user;
166 $data->showfullwords = $this->showfullwords;
d679a45f 167 $data->actionurl = $this->actionurl->out(false);
10deddd3
FM
168
169 $datefrom = $this->datefrom;
170 if (empty($datefrom)) {
171 $datefrom = make_timestamp(2000, 1, 1, 0, 0, 0);
172 }
173
174 $dateto = $this->dateto;
175 if (empty($dateto)) {
176 $dateto = time() + HOURSECS;
177 }
178
179 $data->datefromfields = html_writer::select_time('days', 'fromday', $datefrom)
180 . html_writer::select_time('months', 'frommonth', $datefrom)
181 . html_writer::select_time('years', 'fromyear', $datefrom)
182 . html_writer::select_time('hours', 'fromhour', $datefrom)
183 . html_writer::select_time('minutes', 'fromminute', $datefrom);
184
185 $data->datetofields = html_writer::select_time('days', 'today', $dateto)
186 . html_writer::select_time('months', 'tomonth', $dateto)
187 . html_writer::select_time('years', 'toyear', $dateto)
188 . html_writer::select_time('hours', 'tohour', $dateto)
189 . html_writer::select_time('minutes', 'tominute', $dateto);
190
191 $data->forumoptions = $this->forumoptions;
192
193 return $data;
194 }
195
196}