Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]

   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  
  25  namespace mod_forum\output;
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  use html_writer;
  29  use moodle_url;
  30  use renderable;
  31  use renderer_base;
  32  use stdClass;
  33  use 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   */
  42  class 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 $showfullwords;
  52      public $subject;
  53      public $user;
  54      public $words;
  55      public $tags;
  56      /** @var string The URL of the search form. */
  57      public $actionurl;
  58      /** @var bool Is the user a guest user? */
  59      public $guestuser;
  60      /** @var bool Whether the include starredonly checkbox is checked. */
  61      public $starredonly;
  62      /** @var int forum ID. */
  63      public $forumid;
  64  
  65      /**
  66       * Constructor.
  67       *
  68       * @param object $course The course.
  69       * @param object $user The user.
  70       */
  71      public function __construct($course) {
  72          global $DB, $USER;
  73          $this->course = $course;
  74          $this->tags = [];
  75          $this->guestuser = !isloggedin() || isguestuser($USER);
  76          $this->showfullwords = $DB->get_dbfamily() == 'mysql' || $DB->get_dbfamily() == 'postgres';
  77          $this->actionurl = new moodle_url('/mod/forum/search.php');
  78  
  79          $forumoptions = ['' => get_string('allforums', 'forum')] + forum_menu_list($course);
  80          $this->forumoptions = array_map(function($option) use ($forumoptions) {
  81              return [
  82                  'value' => $option,
  83                  'name' => $forumoptions[$option]
  84              ];
  85          }, array_keys($forumoptions));
  86      }
  87  
  88      /**
  89       * Set date from.
  90       *
  91       * @param mixed $value Date from.
  92       */
  93      public function set_datefrom($value) {
  94          $this->datefrom = $value;
  95      }
  96  
  97      /**
  98       * Set date to.
  99       *
 100       * @param mixed $value Date to.
 101       */
 102      public function set_dateto($value) {
 103          $this->dateto = $value;
 104      }
 105  
 106      /**
 107       * Set full words.
 108       *
 109       * @param mixed $value Full words.
 110       */
 111      public function set_fullwords($value) {
 112          $this->fullwords = $value;
 113      }
 114  
 115      /**
 116       * Set not words.
 117       *
 118       * @param mixed $value Not words.
 119       */
 120      public function set_notwords($value) {
 121          $this->notwords = $value;
 122      }
 123  
 124      /**
 125       * Set phrase.
 126       *
 127       * @param mixed $value Phrase.
 128       */
 129      public function set_phrase($value) {
 130          $this->phrase = $value;
 131      }
 132  
 133      /**
 134       * Set subject.
 135       *
 136       * @param mixed $value Subject.
 137       */
 138      public function set_subject($value) {
 139          $this->subject = $value;
 140      }
 141  
 142      /**
 143       * Set user.
 144       *
 145       * @param mixed $value User.
 146       */
 147      public function set_user($value) {
 148          $this->user = $value;
 149      }
 150  
 151      /**
 152       * Set words.
 153       *
 154       * @param mixed $value Words.
 155       */
 156      public function set_words($value) {
 157          $this->words = $value;
 158      }
 159  
 160      /**
 161       * Set tags.
 162       *
 163       * @param mixed $value Tags.
 164       */
 165      public function set_tags($value) {
 166          $this->tags = $value;
 167      }
 168  
 169      /**
 170       * Set starred only value.
 171       *
 172       * @param mixed $value Bool.
 173       */
 174      public function set_starredonly($value) {
 175          $this->starredonly = $value;
 176      }
 177  
 178      /**
 179       * Forum ID setter search criteria.
 180       *
 181       * @param int $forumid The forum ID.
 182       */
 183      public function set_forumid($forumid) {
 184          $this->forumid = $forumid;
 185      }
 186  
 187      public function export_for_template(renderer_base $output) {
 188          global $DB, $CFG, $PAGE;
 189          $data = new stdClass();
 190  
 191          $data->courseid = $this->course->id;
 192          $data->words = $this->words;
 193          $data->phrase = $this->phrase;
 194          $data->notwords = $this->notwords;
 195          $data->fullwords = $this->fullwords;
 196          $data->datefromchecked = !empty($this->datefrom);
 197          $data->datetochecked = !empty($this->dateto);
 198          $data->subject = $this->subject;
 199          $data->user = $this->user;
 200          $data->showfullwords = $this->showfullwords;
 201          $data->guestuser = $this->guestuser;
 202          $data->starredonly = $this->starredonly;
 203          $data->actionurl = $this->actionurl->out(false);
 204  
 205          $tagtypestoshow = \core_tag_area::get_showstandard('mod_forum', 'forum_posts');
 206          $showstandard = ($tagtypestoshow != \core_tag_tag::HIDE_STANDARD);
 207          $typenewtags = ($tagtypestoshow != \core_tag_tag::STANDARD_ONLY);
 208  
 209          $PAGE->requires->js_call_amd('core/form-autocomplete', 'enhance', $params = array('#tags', $typenewtags, '',
 210                                get_string('entertags', 'tag'), false, $showstandard, get_string('noselection', 'form')));
 211  
 212          $data->tagsenabled = \core_tag_tag::is_enabled('mod_forum', 'forum_posts');
 213          $namefield = empty($CFG->keeptagnamecase) ? 'name' : 'rawname';
 214          $tags = $DB->get_records('tag',
 215              array('isstandard' => 1, 'tagcollid' => \core_tag_area::get_collection('mod_forum', 'forum_posts')),
 216              $namefield, 'rawname,' . $namefield . ' as fieldname');
 217          $data->tags = [];
 218          foreach ($tags as $tag) {
 219              $data->tagoptions[] = ['value'    => $tag->rawname,
 220                                     'text'     => $tag->fieldname,
 221                                     'selected' => in_array($tag->rawname, $this->tags)
 222              ];
 223          }
 224  
 225          $datefrom = $this->datefrom;
 226          if (empty($datefrom)) {
 227              $datefrom = make_timestamp(2000, 1, 1, 0, 0, 0);
 228          }
 229  
 230          $dateto = $this->dateto;
 231          if (empty($dateto)) {
 232              $dateto = time() + HOURSECS;
 233          }
 234  
 235          $data->datefromfields = html_writer::div(html_writer::select_time('days', 'fromday', $datefrom), 'form-group fitem ml-2')
 236                                . html_writer::div(html_writer::select_time('months', 'frommonth', $datefrom),
 237                             'form-group fitem ml-2')
 238                                . html_writer::div(html_writer::select_time('years', 'fromyear', $datefrom), 'form-group fitem ml-2')
 239                                . html_writer::div(html_writer::select_time('hours', 'fromhour', $datefrom), 'form-group fitem ml-2')
 240                                . html_writer::div(html_writer::select_time('minutes', 'fromminute', $datefrom),
 241                             'form-group fitem ml-2');
 242  
 243          $data->datetofields = html_writer::div(html_writer::select_time('days', 'today', $dateto), 'form-group fitem ml-2')
 244                              . html_writer::div(html_writer::select_time('months', 'tomonth', $dateto), 'form-group fitem ml-2')
 245                              . html_writer::div(html_writer::select_time('years', 'toyear', $dateto), 'form-group fitem ml-2')
 246                              . html_writer::div(html_writer::select_time('hours', 'tohour', $dateto), 'form-group fitem ml-2')
 247                              . html_writer::div(html_writer::select_time('minutes', 'tominute', $dateto), 'form-group fitem ml-2');
 248  
 249          if ($this->forumid && !empty($this->forumoptions)) {
 250              foreach ($this->forumoptions as $index => $option) {
 251                  if ($option['value'] == $this->forumid) {
 252                      $this->forumoptions[$index]['selected'] = true;
 253                  } else {
 254                      $this->forumoptions[$index]['selected'] = false;
 255                  }
 256              }
 257          }
 258          $data->forumoptions = $this->forumoptions;
 259  
 260          return $data;
 261      }
 262  
 263  }