MDL-20700 coding style cleanup - cvs keywords removed, closign php tag removed, trail...
[moodle.git] / filter / local_settings_form.php
1 <?php // $Id$
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 ///////////////////////////////////////////////////////////////////////////
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  *//** */
32 require_once($CFG->libdir . '/formslib.php');
34 abstract class filter_local_settings_form extends moodleform {
35     protected $filter;
36     protected $context;
38     public function __construct($submiturl, $filter, $context) {
39         $this->filter = $filter;
40         $this->context = $context;
41         parent::moodleform($submiturl);
42     }
44     /**
45      * Build the form definition. Rather than overriding this method, you
46      * should probably override definition_inner instead.
47      *
48      * This method adds the necessary hidden fields and submit buttons,
49      * and calls definition_inner to insert the custom controls in the appropriate place.
50      */
51     public function definition() {
52         $mform =& $this->_form;
54         $this->definition_inner($mform);
56         $mform->addElement('hidden', 'contextid');
57         $mform->setType('contextid', PARAM_INT);
58         $mform->setDefault('contextid', $this->context->id);
60         $mform->addElement('hidden', 'filter');
61         $mform->setType('filter', PARAM_ALPHAEXT);
62         $mform->setDefault('filter', $this->filter);
64         $this->add_action_buttons();
65     }
67     /**
68      * Override this method to add your form controls.
69      * @param $mform the form we are building. $this->_form, but passed in for convinience.
70      */
71     abstract protected function definition_inner($mform);
73     /**
74      * Override this method to save the settings to the database. The default
75      * implementation will probalby be sufficient for most simple cases.
76      * @param object $data the form data that was submitted.
77      */
78     public function save_changes($data) {
79         $data = (array) $data;
80         unset($data['filter']);
81         unset($data['contextid']);
82         foreach ($data as $name => $value) {
83             if ($value !== '') {
84                 filter_set_local_config($this->filter, $this->context->id, $name, $value);
85             } else {
86                 filter_unset_local_config($this->filter, $this->context->id, $name);
87             }
88         }
89     }
90 }
91 ?>