Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 402] [Versions 311 and 403]

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