MDL-30070 message: Optimised search for users over multiple courses
[moodle.git] / filter / local_settings_form.php
CommitLineData
4317f92f 1<?php
604eb7be 2
35716b86
PS
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/>.
604eb7be 17
18/**
19 * A Moodle form base class for editing local filter settings.
20 *
21 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
35716b86
PS
22 * @package core
23 * @subpackage filter
24 */
25defined('MOODLE_INTERNAL') || die();
bfebaf64 26
604eb7be 27require_once($CFG->libdir . '/formslib.php');
28
29abstract class filter_local_settings_form extends moodleform {
30 protected $filter;
31 protected $context;
32
33 public function __construct($submiturl, $filter, $context) {
34 $this->filter = $filter;
35 $this->context = $context;
36 parent::moodleform($submiturl);
37 }
38
39 /**
40 * Build the form definition. Rather than overriding this method, you
41 * should probably override definition_inner instead.
42 *
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.
45 */
46 public function definition() {
35716b86 47 $mform = $this->_form;
604eb7be 48
49 $this->definition_inner($mform);
50
51 $mform->addElement('hidden', 'contextid');
52 $mform->setType('contextid', PARAM_INT);
53 $mform->setDefault('contextid', $this->context->id);
54
55 $mform->addElement('hidden', 'filter');
5a2bd486 56 $mform->setType('filter', PARAM_SAFEPATH);
604eb7be 57 $mform->setDefault('filter', $this->filter);
58
59 $this->add_action_buttons();
60 }
61
62 /**
63 * Override this method to add your form controls.
eab8ed9f 64 * @param $mform the form we are building. $this->_form, but passed in for convenience.
604eb7be 65 */
66 abstract protected function definition_inner($mform);
67
68 /**
69 * Override this method to save the settings to the database. The default
eab8ed9f 70 * implementation will probably be sufficient for most simple cases.
604eb7be 71 * @param object $data the form data that was submitted.
72 */
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);
82 }
83 }
84 }
85}