"MDL-20470, rename formslib element name and add an option to turn the picker button...
authordongsheng <dongsheng>
Tue, 13 Oct 2009 08:31:50 +0000 (08:31 +0000)
committerdongsheng <dongsheng>
Tue, 13 Oct 2009 08:31:50 +0000 (08:31 +0000)
admin/settings/plugins.php
lang/en_utf8/repository.php
lib/form/url.js [new file with mode: 0644]
lib/form/url.php [new file with mode: 0755]
lib/formslib.php
mod/url/mod_form.php

index 0fd5a1c7f6ea57283daa57f47d0087021178716f..fd30e8239e0e8b37f6f158b6898a771ad2939d25 100644 (file)
@@ -206,6 +206,7 @@ if ($hassiteconfig || has_capability('moodle/question:config', $systemcontext))
     $temp->add(new admin_setting_heading('managerepositoriescommonheading', get_string('commonsettings', 'admin'), ''));
     $temp->add(new admin_setting_configtext('repositorycacheexpire', get_string('cacheexpire', 'repository'), get_string('configcacheexpire', 'repository'), 120));
     $temp->add(new admin_setting_configcheckbox('repositoryuseexternallink', get_string('useexternallink', 'repository'), get_string('configuseexternallink', 'repository'), 0));
+    $temp->add(new admin_setting_configcheckbox('repositoryusepickerbutton', get_string('usepickerbutton', 'repository'), '', 1));
     $ADMIN->add('repositorysettings', $temp);
     $ADMIN->add('repositorysettings', new admin_externalpage('repositorynew',
         get_string('addplugin', 'repository'), $url, 'moodle/site:config', true),
index 586236c07db90dc607bb1b7dd76f9865a3e887e8..486a5f6187e5295789cb22835f6112f96db2fb84 100644 (file)
@@ -121,6 +121,7 @@ $string['uploading'] = 'Uploading...';
 $string['uploadsucc'] = 'The file has been uploaded successfully';
 $string['useexternallink'] = 'Use external link instead downloading files';
 $string['configuseexternallink'] = 'Will return the external link to file picker instead downloading external files';
+$string['usepickerbutton'] = 'Use file picker button beside text element';
 $string['wrongcontext'] = 'You cannot access to this context';
 $string['xhtmlerror'] = 'You are probably using XHTML strict header, some YUI Component doesn\'t work in this mode, please turn it off in moodle';
 $string['ziped'] = 'Compress folder successfully';
diff --git a/lib/form/url.js b/lib/form/url.js
new file mode 100644 (file)
index 0000000..b5aecf3
--- /dev/null
@@ -0,0 +1,19 @@
+function url_callback(params) {
+}
+function url_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 = url_callback;
+    var fp = open_filepicker(client_id, params);
+    return false;
+}
diff --git a/lib/form/url.php b/lib/form/url.php
new file mode 100755 (executable)
index 0000000..a167e36
--- /dev/null
@@ -0,0 +1,131 @@
+<?php
+require_once("HTML/QuickForm/text.php");
+
+/**
+ * HTML class for a url type element
+ *
+ * @author       Jamie Pratt
+ * @access       public
+ */
+class MoodleQuickForm_url extends HTML_QuickForm_text{
+    /**
+     * html for help button, if empty then no help
+     *
+     * @var string
+     */
+    var $_helpbutton='';
+    var $_hiddenLabel=false;
+
+    function MoodleQuickForm_url($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;
+
+        $id     = $this->_attributes['id'];
+        $elname = $this->_attributes['name'];
+
+        if ($this->_hiddenLabel) {
+            $this->_generateId();
+            $str = '<label class="accesshide" for="'.$this->getAttribute('id').'" >'.
+                        $this->getLabel().'</label>'.parent::toHtml();
+        } else {
+            $str = parent::toHtml();
+        }
+        if (!$CFG->repositoryusepickerbutton) {
+            return $str;
+        }
+        $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/url.js');
+        $str .= $repojs;
+        $str .= <<<EOD
+<button id="filepicker-btn-{$client_id}" style="display:none" onclick="return url_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 757ee789cc38f0968fd7eb378dc74d8f62998da8..e13d49630d44317da8d44af7c10a6b8353583716 100644 (file)
@@ -2240,6 +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('url', "$CFG->libdir/form/url.php", 'MoodleQuickForm_url');
 MoodleQuickForm::registerElementType('warning', "$CFG->libdir/form/warning.php", 'MoodleQuickForm_warning');
 ?>
index 58f6ca29d0d2fe65f5656444007e27f7d64252b4..ce9b3db9b5b4326fb57845b86d8749aac8d095ba 100644 (file)
@@ -46,7 +46,7 @@ class mod_url_mod_form extends moodleform_mod {
 
         //-------------------------------------------------------
         $mform->addElement('header', 'content', get_string('contentheader', 'url'));
-        $mform->addElement('texturl', 'externalurl', get_string('externalurl', 'url'), array('size'=>'60'), null); 
+        $mform->addElement('url', 'externalurl', get_string('externalurl', 'url'), array('size'=>'60'), null); 
         //-------------------------------------------------------
         $mform->addElement('header', 'optionssection', get_string('optionsheader', 'url'));