]> git.mjollnir.org Git - moodle.git/commitdiff
"MDL-20470, add a texturl element, which can use filepicker to choose a url"
authordongsheng <dongsheng>
Wed, 7 Oct 2009 05:23:36 +0000 (05:23 +0000)
committerdongsheng <dongsheng>
Wed, 7 Oct 2009 05:23:36 +0000 (05:23 +0000)
lang/en_utf8/repository.php
lib/form/texturl.js [new file with mode: 0644]
lib/form/texturl.php [new file with mode: 0755]
lib/formslib.php
mod/url/mod_form.php

index b1d38e742c9a02be6249cd9a2bd0641f8dc146d9..4a258fba1bd62f7ce0d9ba1f8d719c6c9ca9bddd 100644 (file)
@@ -22,6 +22,7 @@ $string['cannotdelete'] = 'Cannot delete this file.';
 $string['cannotdownload'] = 'Cannot download this file';
 $string['cannotdownloaddir'] = 'Cannot download this folder';
 $string['cannotinitplugin'] = 'Call plugin_init failed';
+$string['choosealink'] = 'Choose a link...';
 $string['clicktohide'] = 'Click here to hide';
 $string['clicktoshow'] = 'Click here to show';
 $string['close'] = 'Close';
diff --git a/lib/form/texturl.js b/lib/form/texturl.js
new file mode 100644 (file)
index 0000000..84ee211
--- /dev/null
@@ -0,0 +1,19 @@
+function texturl_callback(params) {
+}
+function texturl_launch_filepicker(id, client_id, itemid) {
+    var picker = document.createElement('DIV');
+    picker.id = 'file-picker-'+client_id;
+    picker.className = 'file-picker';
+    document.body.appendChild(picker);
+    var el=document.getElementById(id);
+    var params = {};
+    params.env = 'texturl';
+    params.itemid = itemid;
+    params.maxbytes = -1;
+    params.maxfiles = -1;
+    params.savepath = '/';
+    params.target = el;
+    params.callback = texturl_callback;
+    var fp = open_filepicker(client_id, params);
+    return false;
+}
diff --git a/lib/form/texturl.php b/lib/form/texturl.php
new file mode 100755 (executable)
index 0000000..6ea41b1
--- /dev/null
@@ -0,0 +1,128 @@
+<?php
+require_once("HTML/QuickForm/text.php");
+
+/**
+ * HTML class for a texturl type element
+ *
+ * @author       Jamie Pratt
+ * @access       public
+ */
+class MoodleQuickForm_texturl extends HTML_QuickForm_text{
+    /**
+     * html for help button, if empty then no help
+     *
+     * @var string
+     */
+    var $_helpbutton='';
+    var $_hiddenLabel=false;
+
+    function MoodleQuickForm_texturl($elementName=null, $elementLabel=null, $attributes=null, $options=null) {
+        global $CFG;
+        require_once("$CFG->dirroot/repository/lib.php");
+        $options = (array)$options;
+        foreach ($options as $name=>$value) {
+            $this->_options[$name] = $value;
+        }
+        parent::HTML_QuickForm_text($elementName, $elementLabel, $attributes);
+        repository_head_setup();
+    }
+    
+    function setHiddenLabel($hiddenLabel){
+        $this->_hiddenLabel = $hiddenLabel;
+    }
+    function toHtml(){
+        global $CFG, $COURSE, $USER, $PAGE;
+        $strsaved = get_string('filesaved', 'repository');
+        $straddlink = get_string('choosealink', 'repository');
+        if ($COURSE->id == SITEID) {
+            $context = get_context_instance(CONTEXT_SYSTEM);
+        } else {
+            $context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
+        }
+        $client_id = uniqid();
+
+        $repojs = repository_get_client($context, $client_id, '*', 'link');
+
+        $PAGE->requires->js('lib/form/texturl.js');
+
+        $id     = $this->_attributes['id'];
+        $elname = $this->_attributes['name'];
+
+        $str = $repojs;
+        if ($this->_hiddenLabel) {
+            $this->_generateId();
+            $str .= '<label class="accesshide" for="'.$this->getAttribute('id').'" >'.
+                        $this->getLabel().'</label>'.parent::toHtml();
+        } else {
+            $str .= parent::toHtml();
+        }
+        $str .= <<<EOD
+<button id="filepicker-btn-{$client_id}" style="display:none" onclick="return texturl_launch_filepicker('$id', '$client_id', 0)">$straddlink</button>
+EOD;
+        // hide the button if javascript is not enabled
+        $str .= $PAGE->requires->js_function_call('show_item', array("filepicker-btn-{$client_id}"))->asap();
+        return $str;
+    }
+   /**
+    * Automatically generates and assigns an 'id' attribute for the element.
+    *
+    * Currently used to ensure that labels work on radio buttons and
+    * checkboxes. Per idea of Alexander Radivanovich.
+    * Overriden in moodleforms to remove qf_ prefix.
+    *
+    * @access private
+    * @return void
+    */
+    function _generateId()
+    {
+        static $idx = 1;
+
+        if (!$this->getAttribute('id')) {
+            $this->updateAttributes(array('id' => 'id_'. substr(md5(microtime() . $idx++), 0, 6)));
+        }
+    } // end func _generateId
+    /**
+     * set html for help button
+     *
+     * @access   public
+     * @param array $help array of arguments to make a help button
+     * @param string $function function name to call to get html
+     */
+    function setHelpButton($helpbuttonargs, $function='helpbutton'){
+        if (!is_array($helpbuttonargs)){
+            $helpbuttonargs=array($helpbuttonargs);
+        }else{
+            $helpbuttonargs=$helpbuttonargs;
+        }
+        //we do this to to return html instead of printing it
+        //without having to specify it in every call to make a button.
+        if ('helpbutton' == $function){
+            $defaultargs=array('', '', 'moodle', true, false, '', true);
+            $helpbuttonargs=$helpbuttonargs + $defaultargs ;
+        }
+        $this->_helpbutton=call_user_func_array($function, $helpbuttonargs);
+    }
+    /**
+     * get html for help button
+     *
+     * @access   public
+     * @return  string html for help button
+     */
+    function getHelpButton(){
+        return $this->_helpbutton;
+    }
+    /**
+     * Slightly different container template when frozen. Don't want to use a label tag
+     * with a for attribute in that case for the element label but instead use a div.
+     * Templates are defined in renderer constructor.
+     *
+     * @return string
+     */
+    function getElementTemplateType(){
+        if ($this->_flagFrozen){
+            return 'static';
+        } else {
+            return 'default';
+        }
+    }
+}
index 42aa86ca58874fb653f8a73b50ce536fc91cb6e5..757ee789cc38f0968fd7eb378dc74d8f62998da8 100644 (file)
@@ -2240,5 +2240,6 @@ MoodleQuickForm::registerElementType('submitlink', "$CFG->libdir/form/submitlink
 MoodleQuickForm::registerElementType('tags', "$CFG->libdir/form/tags.php", 'MoodleQuickForm_tags');
 MoodleQuickForm::registerElementType('text', "$CFG->libdir/form/text.php", 'MoodleQuickForm_text');
 MoodleQuickForm::registerElementType('textarea', "$CFG->libdir/form/textarea.php", 'MoodleQuickForm_textarea');
+MoodleQuickForm::registerElementType('texturl', "$CFG->libdir/form/texturl.php", 'MoodleQuickForm_texturl');
 MoodleQuickForm::registerElementType('warning', "$CFG->libdir/form/warning.php", 'MoodleQuickForm_warning');
 ?>
index 489a3852b6dfffe32afd84bbc79d4e3ab53c7936..58f6ca29d0d2fe65f5656444007e27f7d64252b4 100644 (file)
@@ -46,9 +46,7 @@ class mod_url_mod_form extends moodleform_mod {
 
         //-------------------------------------------------------
         $mform->addElement('header', 'content', get_string('contentheader', 'url'));
-
-        $mform->addElement('text', 'externalurl', get_string('externalurl', 'url'), array('size'=>'100'));
-
+        $mform->addElement('texturl', 'externalurl', get_string('externalurl', 'url'), array('size'=>'60'), null); 
         //-------------------------------------------------------
         $mform->addElement('header', 'optionssection', get_string('optionsheader', 'url'));