2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * url type form element
21 * Contains HTML class for a url type element
24 * @copyright 2009 Dongsheng Cai <dongsheng@moodle.com>
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 require_once("HTML/QuickForm/text.php");
31 * url type form element
33 * HTML class for a url type element
36 * @copyright 2009 Dongsheng Cai <dongsheng@moodle.com>
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class MoodleQuickForm_url
extends HTML_QuickForm_text
{
40 /** @var string html for help button, if empty then no help */
43 /** @var bool if true label will be hidden */
44 var $_hiddenLabel=false;
49 * @param string $elementName Element name
50 * @param mixed $elementLabel Label(s) for an element
51 * @param mixed $attributes Either a typical HTML attribute string or an associative array.
52 * @param array $options data which need to be posted.
54 function MoodleQuickForm_url($elementName=null, $elementLabel=null, $attributes=null, $options=null) {
56 require_once("$CFG->dirroot/repository/lib.php");
57 $options = (array)$options;
58 foreach ($options as $name=>$value) {
59 $this->_options
[$name] = $value;
61 if (!isset($this->_options
['usefilepicker'])) {
62 $this->_options
['usefilepicker'] = true;
64 parent
::HTML_QuickForm_text($elementName, $elementLabel, $attributes);
68 * Sets label to be hidden
70 * @param bool $hiddenLabel sets if label should be hidden
72 function setHiddenLabel($hiddenLabel){
73 $this->_hiddenLabel
= $hiddenLabel;
77 * Returns HTML for this form element.
82 global $PAGE, $OUTPUT;
84 $id = $this->_attributes
['id'];
85 $elname = $this->_attributes
['name'];
87 if ($this->_hiddenLabel
) {
89 $str = '<label class="accesshide" for="'.$this->getAttribute('id').'" >'.
90 $this->getLabel().'</label>'.parent
::toHtml();
92 $str = parent
::toHtml();
94 if (empty($this->_options
['usefilepicker'])) {
98 $client_id = uniqid();
100 $args = new stdClass();
101 $args->accepted_types
= '*';
102 $args->return_types
= FILE_EXTERNAL
;
103 $args->context
= $PAGE->context
;
104 $args->client_id
= $client_id;
106 $fp = new file_picker($args);
107 $options = $fp->options
;
109 if (count($options->repositories
) > 0) {
110 $straddlink = get_string('choosealink', 'repository');
112 <button id="filepicker-button-{$client_id}" class="visibleifjs">
118 // print out file picker
119 $str .= $OUTPUT->render($fp);
121 $module = array('name'=>'form_url', 'fullpath'=>'/lib/form/url.js', 'requires'=>array('core_filepicker'));
122 $PAGE->requires
->js_init_call('M.form_url.init', array($options), true, $module);
128 * get html for help button
130 * @return string html for help button
132 function getHelpButton(){
133 return $this->_helpbutton
;
137 * Slightly different container template when frozen. Don't want to use a label tag
138 * with a for attribute in that case for the element label but instead use a div.
139 * Templates are defined in renderer constructor.
143 function getElementTemplateType(){
144 if ($this->_flagFrozen
){