Merge branch 'MDL-42026-master' of git://github.com/damyon/moodle
[moodle.git] / filter / local_settings_form.php
blob1721ffa74bcfc2fbb03a308240644cc798891799
1 <?php
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.
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/>.
18 /**
19 * A Moodle form base class for editing local filter settings.
21 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
22 * @package core
23 * @subpackage filter
25 defined('MOODLE_INTERNAL') || die();
27 require_once($CFG->libdir . '/formslib.php');
29 abstract class filter_local_settings_form extends moodleform {
30 protected $filter;
31 protected $context;
33 public function __construct($submiturl, $filter, $context) {
34 $this->filter = $filter;
35 $this->context = $context;
36 parent::moodleform($submiturl);
39 /**
40 * Build the form definition. Rather than overriding this method, you
41 * should probably override definition_inner instead.
43 * This method adds the necessary hidden fields and submit buttons,
44 * and calls definition_inner to insert the custom controls in the appropriate place.
46 public function definition() {
47 $mform = $this->_form;
49 $this->definition_inner($mform);
51 $mform->addElement('hidden', 'contextid');
52 $mform->setType('contextid', PARAM_INT);
53 $mform->setDefault('contextid', $this->context->id);
55 $mform->addElement('hidden', 'filter');
56 $mform->setType('filter', PARAM_SAFEPATH);
57 $mform->setDefault('filter', $this->filter);
59 $this->add_action_buttons();
62 /**
63 * Override this method to add your form controls.
64 * @param $mform the form we are building. $this->_form, but passed in for convenience.
66 abstract protected function definition_inner($mform);
68 /**
69 * Override this method to save the settings to the database. The default
70 * implementation will probably be sufficient for most simple cases.
71 * @param object $data the form data that was submitted.
73 public function save_changes($data) {
74 $data = (array) $data;
75 unset($data['filter']);
76 unset($data['contextid']);
77 foreach ($data as $name => $value) {
78 if ($value !== '') {
79 filter_set_local_config($this->filter, $this->context->id, $name, $value);
80 } else {
81 filter_unset_local_config($this->filter, $this->context->id, $name);