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.
/filter/ -> manage.php (source)

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

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * Lets users configure which filters are active in a sub-context.
  20   *
  21   * @license    http://www.gnu.org/copyleft/gpl.html GNU Public License
  22   * @package    core
  23   * @subpackage filter
  24   */
  25  
  26  require_once(__DIR__ . '/../config.php');
  27  require_once($CFG->libdir . '/adminlib.php');
  28  
  29  $contextid = required_param('contextid',PARAM_INT);
  30  $forfilter = optional_param('filter', '', PARAM_SAFEDIR);
  31  $returnto  = optional_param('return', null, PARAM_ALPHANUMEXT);
  32  
  33  list($context, $course, $cm) = get_context_info_array($contextid);
  34  
  35  /// Check login and permissions.
  36  require_login($course, false, $cm);
  37  require_capability('moodle/filter:manage', $context);
  38  $PAGE->set_context($context);
  39  
  40  $args = array('contextid'=>$contextid);
  41  $baseurl = new moodle_url('/filter/manage.php', $args);
  42  if (!empty($forfilter)) {
  43      $args['filter'] = $forfilter;
  44  }
  45  $PAGE->set_url($baseurl, $args);
  46  if ($returnto !== null) {
  47      $baseurl->param('return', $returnto);
  48  }
  49  
  50  // This is a policy decision, rather than something that would be impossible to implement.
  51  if (!in_array($context->contextlevel, array(CONTEXT_COURSECAT, CONTEXT_COURSE, CONTEXT_MODULE))) {
  52      throw new \moodle_exception('cannotcustomisefiltersblockuser', 'error');
  53  }
  54  
  55  $isfrontpage = ($context->contextlevel == CONTEXT_COURSE && $context->instanceid == SITEID);
  56  
  57  $contextname = $context->get_context_name();
  58  
  59  if ($context->contextlevel == CONTEXT_COURSECAT) {
  60      core_course_category::page_setup();
  61  } else if ($context->contextlevel == CONTEXT_COURSE) {
  62      $PAGE->set_heading($course->fullname);
  63  } else if ($context->contextlevel == CONTEXT_MODULE) {
  64      // Must be module context.
  65      $PAGE->set_heading($PAGE->activityrecord->name);
  66  }
  67  
  68  /// Check login and permissions.
  69  require_login($course, false, $cm);
  70  require_capability('moodle/filter:manage', $context);
  71  
  72  $PAGE->set_context($context);
  73  
  74  /// Get the list of available filters.
  75  $availablefilters = filter_get_available_in_context($context);
  76  if (!$isfrontpage && empty($availablefilters)) {
  77      throw new \moodle_exception('nofiltersenabled', 'error');
  78  }
  79  
  80  // If we are handling local settings for a particular filter, start processing.
  81  if ($forfilter) {
  82      if (!filter_has_local_settings($forfilter)) {
  83          throw new \moodle_exception('filterdoesnothavelocalconfig', 'error', $forfilter);
  84      }
  85      require_once($CFG->dirroot . '/filter/local_settings_form.php');
  86      require_once($CFG->dirroot . '/filter/' . $forfilter . '/filterlocalsettings.php');
  87      $formname = $forfilter . '_filter_local_settings_form';
  88      $settingsform = new $formname($CFG->wwwroot . '/filter/manage.php', $forfilter, $context);
  89      if ($settingsform->is_cancelled()) {
  90          redirect($baseurl);
  91      } else if ($data = $settingsform->get_data()) {
  92          $settingsform->save_changes($data);
  93          redirect($baseurl);
  94      }
  95  }
  96  
  97  /// Process any form submission.
  98  if ($forfilter == '' && optional_param('savechanges', false, PARAM_BOOL) && confirm_sesskey()) {
  99      foreach ($availablefilters as $filter => $filterinfo) {
 100          $newstate = optional_param($filter, false, PARAM_INT);
 101          if ($newstate !== false && $newstate != $filterinfo->localstate) {
 102              filter_set_local_state($filter, $context->id, $newstate);
 103          }
 104      }
 105      redirect($baseurl, get_string('changessaved'), 1);
 106  }
 107  
 108  /// Work out an appropriate page title.
 109  if ($forfilter) {
 110      $a = new stdClass;
 111      $a->filter = filter_get_name($forfilter);
 112      $a->context = $contextname;
 113      $title = get_string('filtersettingsforin', 'filters', $a);
 114  } else {
 115      $title = get_string('filtersettingsin', 'filters', $contextname);
 116  }
 117  $straction = get_string('filters', 'admin'); // Used by tabs.php
 118  
 119  // Print the header and tabs.
 120  $PAGE->set_cacheable(false);
 121  $PAGE->set_title($title);
 122  $PAGE->set_pagelayout('admin');
 123  $PAGE->activityheader->disable();
 124  echo $OUTPUT->header();
 125  
 126  /// Print heading.
 127  echo $OUTPUT->heading_with_help($title, 'filtersettings', 'filters');
 128  
 129  if (empty($availablefilters)) {
 130      echo '<p class="centerpara">' . get_string('nofiltersenabled', 'filters') . "</p>\n";
 131  } else if ($forfilter) {
 132      $current = filter_get_local_config($forfilter, $contextid);
 133      $settingsform->set_data((object) $current);
 134      $settingsform->display();
 135  } else {
 136      $settingscol = false;
 137      foreach ($availablefilters as $filter => $notused) {
 138          $hassettings = filter_has_local_settings($filter);
 139          $availablefilters[$filter]->hassettings = $hassettings;
 140          $settingscol = $settingscol || $hassettings;
 141      }
 142  
 143      $strsettings = get_string('settings');
 144      $stroff = get_string('off', 'filters');
 145      $stron = get_string('on', 'filters');
 146      $strdefaultoff = get_string('defaultx', 'filters', $stroff);
 147      $strdefaulton = get_string('defaultx', 'filters', $stron);
 148      $activechoices = array(
 149          TEXTFILTER_INHERIT => '',
 150          TEXTFILTER_OFF => $stroff,
 151          TEXTFILTER_ON => $stron,
 152      );
 153  
 154      echo html_writer::start_tag('form', array('action'=>$baseurl->out_omit_querystring(), 'method'=>'post'));
 155      echo html_writer::start_tag('div');
 156      echo html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=>sesskey()));
 157      foreach ($baseurl->params() as $key => $value) {
 158          echo html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>$key, 'value'=>$value));
 159      }
 160  
 161      $table = new html_table();
 162      $table->head  = array(get_string('filter'), get_string('isactive', 'filters'));
 163      $table->colclasses = array('leftalign', 'leftalign');
 164      if ($settingscol) {
 165          $table->head[] = $strsettings;
 166          $table->colclasses[] = 'leftalign';
 167      }
 168      $table->id = 'frontpagefiltersettings';
 169      $table->attributes['class'] = 'admintable generaltable';
 170      $table->data = array();
 171  
 172      // iterate through filters adding to display table
 173      foreach ($availablefilters as $filter => $filterinfo) {
 174          $row = array();
 175  
 176          // Filter name.
 177          $row[] = filter_get_name($filter);
 178  
 179          // Default/on/off choice.
 180          if ($filterinfo->inheritedstate == TEXTFILTER_ON) {
 181              $activechoices[TEXTFILTER_INHERIT] = $strdefaulton;
 182          } else {
 183              $activechoices[TEXTFILTER_INHERIT] = $strdefaultoff;
 184          }
 185          $select = html_writer::label($filterinfo->localstate, 'menu'. $filter, false, array('class' => 'accesshide'));
 186          $select .= html_writer::select($activechoices, $filter, $filterinfo->localstate, false);
 187          $row[] = $select;
 188  
 189          // Settings link, if required
 190          if ($settingscol) {
 191              $settings = '';
 192              if ($filterinfo->hassettings) {
 193                  $settings = '<a href="' . $baseurl->out(true, array('filter'=>$filter)). '">' . $strsettings . '</a>';
 194              }
 195              $row[] = $settings;
 196          }
 197  
 198          $table->data[] = $row;
 199      }
 200  
 201      echo html_writer::table($table);
 202      echo html_writer::start_tag('div', array('class'=>'buttons'));
 203      $submitattr = ['type' => 'submit', 'name' => 'savechanges', 'value' => get_string('savechanges'), 'class' => 'btn btn-primary'];
 204      echo html_writer::empty_tag('input', $submitattr);
 205      echo html_writer::end_tag('div');
 206      echo html_writer::end_tag('div');
 207      echo html_writer::end_tag('form');
 208  }
 209  
 210  /// Appropriate back link.
 211  if (!$isfrontpage) {
 212  
 213      if ($context->contextlevel === CONTEXT_COURSECAT && $returnto === 'management') {
 214          $url = new moodle_url('/course/management.php', array('categoryid' => $context->instanceid));
 215      } else {
 216          $url = $context->get_url();
 217      }
 218  
 219      echo html_writer::start_tag('div', array('class'=>'backlink'));
 220      echo html_writer::tag('a', get_string('backto', '', $contextname), array('href' => $url));
 221      echo html_writer::end_tag('div');
 222  }
 223  
 224  echo $OUTPUT->footer();
 225