MDL-20069 new URL module, includes migrate from old mod/resource; remaining issues...
[moodle.git] / mod / url / mod_form.php
CommitLineData
58a27a74 1<?php
2
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/>.
17
18/**
19 * URL configuration form
20 *
21 * @package mod-url
22 * @copyright 2009 Petr Skoda (http://skodak.org)
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26require_once ($CFG->dirroot.'/course/moodleform_mod.php');
27require_once($CFG->dirroot.'/mod/url/locallib.php');
28
29class mod_url_mod_form extends moodleform_mod {
30 function definition() {
31 global $CFG, $DB;
32 $mform = $this->_form;
33
34 $config = get_config('url');
35
36 //-------------------------------------------------------
37 $mform->addElement('header', 'general', get_string('general', 'form'));
38 $mform->addElement('text', 'name', get_string('name'), array('size'=>'48'));
39 if (!empty($CFG->formatstringstriptags)) {
40 $mform->setType('name', PARAM_TEXT);
41 } else {
42 $mform->setType('name', PARAM_CLEAN);
43 }
44 $mform->addRule('name', null, 'required', null, 'client');
45 $this->add_intro_editor($config->requiremodintro);
46
47 //-------------------------------------------------------
48 $mform->addElement('header', 'content', get_string('contentheader', 'url'));
49
50 $mform->addElement('text', 'externalurl', get_string('externalurl', 'url'), array('size'=>'100'));
51
52 //-------------------------------------------------------
53 $mform->addElement('header', 'optionssection', get_string('optionsheader', 'url'));
54
55 if ($this->current->instance) {
56 $options = resourcelib_get_displayoptions(explode(',', $config->displayoptions), $this->current->display);
57 } else {
58 $options = resourcelib_get_displayoptions(explode(',', $config->displayoptions));
59 }
60 if (count($options) == 1) {
61 $mform->addElement('hidden', 'display');
62 reset($options);
63 $mform->setDefault('display', key($options));
64 } else {
65 $mform->addElement('select', 'display', get_string('displayselect', 'url'), $options);
66 $mform->setDefault('display', $config->display);
67 $mform->setAdvanced('display', $config->display_adv);
68 }
69
70 if (array_key_exists(RESOURCELIB_DISPLAY_POPUP, $options)) {
71 $mform->addElement('text', 'popupwidth', get_string('popupwidth', 'url'), array('size'=>3));
72 if (count($options) > 1) {
73 $mform->disabledIf('popupwidth', 'display', 'noteq', RESOURCELIB_DISPLAY_POPUP);
74 }
75 $mform->setType('popupwidth', PARAM_INT);
76 $mform->setDefault('popupwidth', $config->popupwidth);
77 $mform->setAdvanced('popupwidth', $config->popupwidth_adv);
78
79 $mform->addElement('text', 'popupheight', get_string('popupheight', 'url'), array('size'=>3));
80 if (count($options) > 1) {
81 $mform->disabledIf('popupheight', 'display', 'noteq', RESOURCELIB_DISPLAY_POPUP);
82 }
83 $mform->setType('popupheight', PARAM_INT);
84 $mform->setDefault('popupheight', $config->popupheight);
85 $mform->setAdvanced('popupheight', $config->popupheight_adv);
86 }
87
88 if (array_key_exists(RESOURCELIB_DISPLAY_AUTO, $options) or
89 array_key_exists(RESOURCELIB_DISPLAY_EMBED, $options) or
90 array_key_exists(RESOURCELIB_DISPLAY_FRAME, $options)) {
91 $mform->addElement('checkbox', 'printheading', get_string('printheading', 'url'));
92 $mform->disabledIf('printheading', 'display', 'eq', RESOURCELIB_DISPLAY_POPUP);
93 $mform->disabledIf('printheading', 'display', 'eq', RESOURCELIB_DISPLAY_OPEN);
94 $mform->disabledIf('printheading', 'display', 'eq', RESOURCELIB_DISPLAY_NEW);
95 $mform->setDefault('printheading', $config->printheading);
96 $mform->setAdvanced('printheading', $config->printheading_adv);
97
98 $mform->addElement('checkbox', 'printintro', get_string('printintro', 'url'));
99 $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_POPUP);
100 $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_OPEN);
101 $mform->disabledIf('printintro', 'display', 'eq', RESOURCELIB_DISPLAY_NEW);
102 $mform->setDefault('printintro', $config->printintro);
103 $mform->setAdvanced('printintro', $config->printintro_adv);
104 }
105
106 //-------------------------------------------------------
107 $mform->addElement('header', 'parameterssection', get_string('parametersheader', 'url'));
108
109
110 if (empty($this->current->parameters)) {
111 $parcount = 5;
112 } else {
113 $parcount = 5 + count(unserialize($this->current->parameters));
114 $parcount = ($parcount > 100) ? 100 : $parcount;
115 }
116 $options = url_get_variable_options($config);
117
118 for ($i=0; $i < $parcount; $i++) {
119 $parameter = "parameter_$i";
120 $variable = "variable_$i";
121 $pargroup = "pargoup_$i";
122 $group = array(
123 $mform->createElement('text', $parameter, '', array('size'=>'12')),
124 $mform->createElement('selectgroups', $variable, '', $options),
125 );
126 $mform->addGroup($group, $pargroup, get_string('parameterinfo', 'url'), ' ', false);
127 $mform->setAdvanced($pargroup);
128 }
129
130 //-------------------------------------------------------
131 $this->standard_coursemodule_elements();
132
133 //-------------------------------------------------------
134 $this->add_action_buttons();
135 }
136
137 function data_preprocessing(&$default_values) {
138 if (!empty($default_values['displayoptions'])) {
139 $displayoptions = unserialize($default_values['displayoptions']);
140 if (isset($displayoptions['printintro'])) {
141 $default_values['printintro'] = $displayoptions['printintro'];
142 }
143 if (isset($displayoptions['printheading'])) {
144 $default_values['printheading'] = $displayoptions['printheading'];
145 }
146 if (!empty($displayoptions['popupwidth'])) {
147 $default_values['popupwidth'] = $displayoptions['popupwidth'];
148 }
149 if (!empty($displayoptions['popupheight'])) {
150 $default_values['popupheight'] = $displayoptions['popupheight'];
151 }
152 }
153 if (!empty($default_values['parameters'])) {
154 $parameters = unserialize($default_values['parameters']);
155 $i = 0;
156 foreach ($parameters as $parameter=>$variable) {
157 $default_values['parameter_'.$i] = $parameter;
158 $default_values['variable_'.$i] = $variable;
159 $i++;
160 }
161 }
162 }
163
164}