MDL-22388 Added some checks to kill these scripts dead with an unequivocal notice...
[moodle.git] / filter / local_settings_form.php
CommitLineData
4317f92f 1<?php
604eb7be 2
3///////////////////////////////////////////////////////////////////////////
4// //
5// NOTICE OF COPYRIGHT //
6// //
7// Moodle - Modular Object-Oriented Dynamic Learning Environment //
8// http://moodle.org //
9// //
10// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
11// //
12// This program is free software; you can redistribute it and/or modify //
13// it under the terms of the GNU General Public License as published by //
14// the Free Software Foundation; either version 2 of the License, or //
15// (at your option) any later version. //
16// //
17// This program is distributed in the hope that it will be useful, //
18// but WITHOUT ANY WARRANTY; without even the implied warranty of //
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20// GNU General Public License for more details: //
21// //
22// http://www.gnu.org/copyleft/gpl.html //
23// //
24///////////////////////////////////////////////////////////////////////////
25
26/**
27 * A Moodle form base class for editing local filter settings.
28 *
29 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
30 * @package moodlecore
31 *//** */
bfebaf64
MD
32if (!defined('MOODLE_INTERNAL')) {
33 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
34}
35
604eb7be 36require_once($CFG->libdir . '/formslib.php');
37
38abstract class filter_local_settings_form extends moodleform {
39 protected $filter;
40 protected $context;
41
42 public function __construct($submiturl, $filter, $context) {
43 $this->filter = $filter;
44 $this->context = $context;
45 parent::moodleform($submiturl);
46 }
47
48 /**
49 * Build the form definition. Rather than overriding this method, you
50 * should probably override definition_inner instead.
51 *
52 * This method adds the necessary hidden fields and submit buttons,
53 * and calls definition_inner to insert the custom controls in the appropriate place.
54 */
55 public function definition() {
56 $mform =& $this->_form;
57
58 $this->definition_inner($mform);
59
60 $mform->addElement('hidden', 'contextid');
61 $mform->setType('contextid', PARAM_INT);
62 $mform->setDefault('contextid', $this->context->id);
63
64 $mform->addElement('hidden', 'filter');
65 $mform->setType('filter', PARAM_ALPHAEXT);
66 $mform->setDefault('filter', $this->filter);
67
68 $this->add_action_buttons();
69 }
70
71 /**
72 * Override this method to add your form controls.
73 * @param $mform the form we are building. $this->_form, but passed in for convinience.
74 */
75 abstract protected function definition_inner($mform);
76
77 /**
78 * Override this method to save the settings to the database. The default
79 * implementation will probalby be sufficient for most simple cases.
80 * @param object $data the form data that was submitted.
81 */
82 public function save_changes($data) {
83 $data = (array) $data;
84 unset($data['filter']);
85 unset($data['contextid']);
86 foreach ($data as $name => $value) {
87 if ($value !== '') {
88 filter_set_local_config($this->filter, $this->context->id, $name, $value);
89 } else {
90 filter_unset_local_config($this->filter, $this->context->id, $name);
91 }
92 }
93 }
94}