MDL-20068 new Page module, includes migrate from old mod/resource; remaining issues...
[moodle.git] / mod / page / mod_form.php
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.
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/>.
18 /**
19  * Page configuration form
20  *
21  * @package   mod-page
22  * @copyright 2009 Petr Skoda (http://skodak.org)
23  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
26 require_once($CFG->dirroot.'/course/moodleform_mod.php');
27 require_once($CFG->dirroot.'/mod/page/locallib.php');
28 require_once($CFG->libdir.'/filelib.php');
30 class mod_page_mod_form extends moodleform_mod {
31     function definition() {
32         global $CFG, $DB;
33         $mform =& $this->_form;
35         $config = get_config('page');
37         //-------------------------------------------------------
38         $mform->addElement('header', 'general', get_string('general', 'form'));
39         $mform->addElement('text', 'name', get_string('name'), array('size'=>'48'));
40         if (!empty($CFG->formatstringstriptags)) {
41             $mform->setType('name', PARAM_TEXT);
42         } else {
43             $mform->setType('name', PARAM_CLEAN);
44         }
45         $mform->addRule('name', null, 'required', null, 'client');
46         $this->add_intro_editor($config->requiremodintro);
48         //-------------------------------------------------------
49         $mform->addElement('header', 'contentsection', get_string('contentheader', 'page'));
50         $mform->addElement('editor', 'page', get_string('content', 'page'), null, page_get_editor_options($this->context));
51         $mform->addElement('static', 'note', '', '<i>(TODO: The format switching is not implemented yet, sorry...)</i>');
53         //-------------------------------------------------------
54         $mform->addElement('header', 'optionssection', get_string('optionsheader', 'page'));
56         if ($this->current->instance) {
57             $options = resourcelib_get_displayoptions(explode(',', $config->displayoptions), $this->current->display);
58         } else {
59             $options = resourcelib_get_displayoptions(explode(',', $config->displayoptions));
60         }
61         if (count($options) == 1) {
62             $mform->addElement('hidden', 'display');
63             reset($options);
64             $mform->setDefault('display', key($options));
65         } else {
66             $mform->addElement('select', 'display', get_string('displayselect', 'page'), $options);
67             $mform->setDefault('display', $config->display);
68             $mform->setAdvanced('display', $config->display_adv);
69         }
71         if (array_key_exists(RESOURCELIB_DISPLAY_POPUP, $options)) {
72             $mform->addElement('text', 'popupwidth', get_string('popupwidth', 'page'), array('size'=>3));
73             if (count($options) > 1) {
74                 $mform->disabledIf('popupwidth', 'display', 'noteq', RESOURCELIB_DISPLAY_POPUP);
75             }
76             $mform->setType('popupwidth', PARAM_INT);
77             $mform->setDefault('popupwidth', $config->popupwidth);
78             $mform->setAdvanced('popupwidth', $config->popupwidth_adv);
80             $mform->addElement('text', 'popupheight', get_string('popupheight', 'page'), array('size'=>3));
81             if (count($options) > 1) {
82                 $mform->disabledIf('popupheight', 'display', 'noteq', RESOURCELIB_DISPLAY_POPUP);
83             }
84             $mform->setType('popupheight', PARAM_INT);
85             $mform->setDefault('popupheight', $config->popupheight);
86             $mform->setAdvanced('popupheight', $config->popupheight_adv);
87         }
89         $mform->addElement('advcheckbox', 'printheading', get_string('printheading', 'page'));
90         $mform->setDefault('printheading', $config->printheading);
91         $mform->setAdvanced('printintro', $config->printheading_adv);
92         $mform->addElement('advcheckbox', 'printintro', get_string('printintro', 'page'));
93         $mform->setDefault('printintro', $config->printintro);
94         $mform->setAdvanced('printintro', $config->printintro_adv);
96         // add legacy files flag only if used
97         if (isset($this->current->legacyfiles) and $this->current->legacyfiles != RESOURCELIB_LEGACYFILES_NO) {
98             $options = array(RESOURCE_LEGACYYFILES_DONE     => get_string('legacyfilesdone', 'page'),
99                              RESOURCELIB_LEGACYFILES_ACTIVE => get_string('legacyfilesactive', 'page'));
100             $mform->addElement('select', 'legacyfiles', get_string('legacyfiles', 'page'), $options);
101             $mform->setAdvanced('legacyfiles', 1);
102         }
104         //-------------------------------------------------------
105         $this->standard_coursemodule_elements();
107         //-------------------------------------------------------
108         $this->add_action_buttons();
110         //-------------------------------------------------------
111         $mform->addElement('hidden', 'revision');
112         $mform->setType('revision', PARAM_INT);
113         $mform->setDefault('revision', 1);
114     }
116     function data_preprocessing(&$default_values) {
117         if ($this->current->instance) {
118             $draftitemid = file_get_submitted_draft_itemid('page');
119             $default_values['page']['format'] = $default_values['contentformat'];
120             $default_values['page']['text']   = file_prepare_draft_area($draftitemid, $this->context->id, 'page_content', 0, page_get_editor_options($this->context), $default_values['content']);
121             $default_values['page']['itemid'] = $draftitemid;
122         }
123         if (!empty($default_values['displayoptions'])) {
124             $displayoptions = unserialize($default_values['displayoptions']);
125             if (isset($displayoptions['printintro'])) {
126                 $default_values['printintro'] = $displayoptions['printintro'];
127             }
128             if (isset($displayoptions['printheading'])) {
129                 $default_values['printheading'] = $displayoptions['printheading'];
130             }
131             if (!empty($displayoptions['popupwidth'])) {
132                 $default_values['popupwidth'] = $displayoptions['popupwidth'];
133             }
134             if (!empty($displayoptions['popupheight'])) {
135                 $default_values['popupheight'] = $displayoptions['popupheight'];
136             }
137         }
138     }