Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-33006

Move File manager rendering to Output API

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Major
    • Resolution: Duplicate
    • Affects Version/s: 2.2.3
    • Fix Version/s: None
    • Component/s: Filepicker, Forms Library
    • Labels:
      None
    • Workaround:
      Hide

      To include on outputcomponents.php:

      /**
       * Data structure representing a file manager.
       *
       * @copyright 2010 Dongsheng Cai
       * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
       * @since     Moodle 2.0
       */
      class file_manager implements renderable {
          public $options;
          public function __construct(stdClass $options) {
          	global $CFG, $USER, $PAGE;
              require_once($CFG->dirroot. '/repository/lib.php');
              $defaults = array(
                  'maxbytes'=>-1,
                  'maxfiles'=>-1,
                  'itemid'=>0,
                  'subdirs'=>0,
                  'client_id'=>uniqid(),
                  'accepted_types'=>'*',
                  'return_types'=>FILE_INTERNAL,
                  'context'=>$PAGE->context
                  );
              foreach ($defaults as $key=>$value) {
                  if (empty($options->$key)) {
                      $options->$key = $value;
                  }
              }
       
              $fs = get_file_storage();
       
              // initilise options, getting files in root path
              $this->options = file_get_drafarea_files($options->itemid, '/');
       
              // calculate file count
              $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
              $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $options->itemid, 'id', false);
              $filecount = count($files);
              $this->options->filecount = $filecount;
       
              // copying other options
              foreach ($options as $name=>$value) {
                  $this->options->$name = $value;
              }
       
              // building file picker options
              $params = new stdClass();
              $params->accepted_types = $options->accepted_types;
              $params->return_types = $options->return_types;
              $params->context = $options->context;
              $params->env = 'filemanager';
              $params->disable_types = !empty($options->disable_types)?$options->disable_types:array();
              $filepicker_options = initialise_filepicker($params);
              $this->options->filepicker = $filepicker_options;
          }
      }

      To include on outputrenderers.php as part of core_renderer class:

              /**
      	 * Print the file manager
      	 *
      	 * <pre>
      	 * $OUTPUT->file_manager($options);
      	 * </pre>
      	 *
      	 * @param array $options associative array with file manager options
      	 *   options are:
      	 *       maxbytes=>-1,
      	 *       maxfiles=>-1,
      	 *       itemid=>0,
      	 *       subdirs=>false,
      	 *       client_id=>uniqid(),
      	 *       acepted_types=>'*',
      	 *       return_types=>FILE_INTERNAL,
      	 *       context=>$PAGE->context
      	 * @return string HTML fragment
      	 */
          public function file_manager($options) {
              $fp = new file_manager($options);
              return $this->render($fp);
          }
          
          /**
           * Internal implementation of file manager rendering.
           * @param file_manager $fm
           * @return string
           */
          public function render_file_manager(file_manager $fm) {
      	    global $CFG, $OUTPUT, $PAGE;
      	
      	
      	    static $filemanagertemplateloaded;
      	
      	    $html = '';
      	    $options = $fm->options;
      	    $straddfile  = get_string('addfile', 'repository');
      	    $strmakedir  = get_string('makeafolder', 'moodle');
      	    $strdownload = get_string('downloadfolder', 'repository');
      	    $strloading  = get_string('loading', 'repository');
      	
      	    $icon_progress = $OUTPUT->pix_icon('i/loading_small', $strloading).'';
      	
      	    $client_id = $options->client_id;
      	    $itemid    = $options->itemid;
      	    list($context, $course, $cm) = get_context_info_array($options->context->id);
      	    if (is_object($course)) {
      	        $course_maxbytes = $course->maxbytes;
      	    } else {
      	        $course_maxbytes = $CFG->maxbytes;
      	    }
      	
      	    if ($options->maxbytes == -1 || empty($options->maxbytes)) {
      	        $options->maxbytes = $CFG->maxbytes;
      	    }
      	
      	    if (empty($options->filecount)) {
      	        $extra = ' style="display:none"';
      	    } else {
      	        $extra = '';
      	    }
      	
      	    $maxsize = get_string('maxfilesize', 'moodle', display_size(get_max_upload_file_size($CFG->maxbytes, $course_maxbytes, $options->maxbytes)));
      	    $html .= <<<FMHTML
      <div class="filemanager-loading mdl-align" id='filemanager-loading-{$client_id}'>
      $icon_progress
      </div>
      <div id="filemanager-wrapper-{$client_id}" style="display:none">
          <div class="fm-breadcrumb" id="fm-path-{$client_id}"></div>
          <div class="filemanager-toolbar">
              <input type="button" class="fm-btn-add" id="btnadd-{$client_id}" onclick="return false" value="{$straddfile}" />
              <input type="button" class="fm-btn-mkdir" id="btncrt-{$client_id}" onclick="return false" value="{$strmakedir}" />
              <input type="button" class="fm-btn-download" id="btndwn-{$client_id}" onclick="return false" {$extra} value="{$strdownload}" />
              <span> $maxsize </span>
          </div>
          <div class="filemanager-container" id="filemanager-{$client_id}">
              <ul id="draftfiles-{$client_id}" class="fm-filelist">
                  <li>Loading...</li>
              </ul>
          </div>
      </div>
      <div class='clearer'></div>
      FMHTML;
          if (empty($filemanagertemplateloaded)) {
              $filemanagertemplateloaded = true;
              $html .= <<<FMHTML
      <div id="fm-template" style="display:none">___fullname___ ___action___</div>
      FMHTML;
      	    }
      	
      	    $module = array(
      	        'name'=>'form_filemanager',
      	        'fullpath'=>'/lib/form/filemanager.js',
      	        'requires' => array('core_filepicker', 'base', 'io-base', 'node', 'json', 'yui2-button', 'yui2-container', 'yui2-layout', 'yui2-menu', 'yui2-treeview'),
      	        'strings' => array(array('loading', 'repository'), array('nomorefiles', 'repository'), array('confirmdeletefile', 'repository'),
      	             array('add', 'repository'), array('accessiblefilepicker', 'repository'), array('move', 'moodle'),
      	             array('cancel', 'moodle'), array('download', 'moodle'), array('ok', 'moodle'),
      	             array('emptylist', 'repository'), array('nofilesattached', 'repository'), array('entername', 'repository'), array('enternewname', 'repository'),
      	             array('zip', 'editor'), array('unzip', 'moodle'), array('rename', 'moodle'), array('delete', 'moodle'),
      	             array('cannotdeletefile', 'error'), array('confirmdeletefile', 'repository'),
      	             array('nopathselected', 'repository'), array('popupblockeddownload', 'repository'),
      	             array('draftareanofiles', 'repository'), array('path', 'moodle'), array('setmainfile', 'repository'),
      	             array('moving', 'repository'), array('files', 'moodle')
      	        )
      	    );
      	    $PAGE->requires->js_module($module);
      	    $PAGE->requires->js_init_call('M.form_filemanager.init', array($options), true, $module);
      	
      	    // non javascript file manager
      	    $filemanagerurl = new moodle_url('/repository/draftfiles_manager.php', array(
      	        'env'=>'filemanager',
      	        'action'=>'browse',
      	        'itemid'=>$itemid,
      	        'subdirs'=>$options->subdirs,
      	        'maxbytes'=>$options->maxbytes,
      	        'maxfiles'=>$options->maxfiles,
      	        'ctx_id'=>$PAGE->context->id,
      	        'course'=>$PAGE->course->id,
      	        'sesskey'=>sesskey(),
      	        ));
      	
      	    $html .= '<noscript>';
      	    $html .= "<div><object type='text/html' data='$filemanagerurl' height='160' width='600' style='border:1px solid #000'></object></div>";
      	    $html .= '</noscript>';
      	
      	
      	    return $html;
          }

      To be replaced on forms/filemanager.php:

       - $html .= form_filemanager_render($options);
       + $fm = new file_manager($options);
       + $options = $fm->options;
       + $html .= $OUTPUT->render($fm);

      Also delete form_filemanaer_x class and form_filemanager_render function on the same file

      Show
      To include on outputcomponents.php: /** * Data structure representing a file manager. * * @copyright 2010 Dongsheng Cai * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @since Moodle 2.0 */ class file_manager implements renderable { public $options; public function __construct(stdClass $options) { global $CFG, $USER, $PAGE; require_once($CFG->dirroot. '/repository/lib.php'); $defaults = array( 'maxbytes'=>-1, 'maxfiles'=>-1, 'itemid'=>0, 'subdirs'=>0, 'client_id'=>uniqid(), 'accepted_types'=>'*', 'return_types'=>FILE_INTERNAL, 'context'=>$PAGE->context ); foreach ($defaults as $key=>$value) { if (empty($options->$key)) { $options->$key = $value; } }   $fs = get_file_storage();   // initilise options, getting files in root path $this->options = file_get_drafarea_files($options->itemid, '/');   // calculate file count $usercontext = get_context_instance(CONTEXT_USER, $USER->id); $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $options->itemid, 'id', false); $filecount = count($files); $this->options->filecount = $filecount;   // copying other options foreach ($options as $name=>$value) { $this->options->$name = $value; }   // building file picker options $params = new stdClass(); $params->accepted_types = $options->accepted_types; $params->return_types = $options->return_types; $params->context = $options->context; $params->env = 'filemanager'; $params->disable_types = !empty($options->disable_types)?$options->disable_types:array(); $filepicker_options = initialise_filepicker($params); $this->options->filepicker = $filepicker_options; } } To include on outputrenderers.php as part of core_renderer class: /** * Print the file manager * * <pre> * $OUTPUT->file_manager($options); * </pre> * * @param array $options associative array with file manager options * options are: * maxbytes=>-1, * maxfiles=>-1, * itemid=>0, * subdirs=>false, * client_id=>uniqid(), * acepted_types=>'*', * return_types=>FILE_INTERNAL, * context=>$PAGE->context * @return string HTML fragment */ public function file_manager($options) { $fp = new file_manager($options); return $this->render($fp); } /** * Internal implementation of file manager rendering. * @param file_manager $fm * @return string */ public function render_file_manager(file_manager $fm) { global $CFG, $OUTPUT, $PAGE; static $filemanagertemplateloaded; $html = ''; $options = $fm->options; $straddfile = get_string('addfile', 'repository'); $strmakedir = get_string('makeafolder', 'moodle'); $strdownload = get_string('downloadfolder', 'repository'); $strloading = get_string('loading', 'repository'); $icon_progress = $OUTPUT->pix_icon('i/loading_small', $strloading).''; $client_id = $options->client_id; $itemid = $options->itemid; list($context, $course, $cm) = get_context_info_array($options->context->id); if (is_object($course)) { $course_maxbytes = $course->maxbytes; } else { $course_maxbytes = $CFG->maxbytes; } if ($options->maxbytes == -1 || empty($options->maxbytes)) { $options->maxbytes = $CFG->maxbytes; } if (empty($options->filecount)) { $extra = ' style="display:none"'; } else { $extra = ''; } $maxsize = get_string('maxfilesize', 'moodle', display_size(get_max_upload_file_size($CFG->maxbytes, $course_maxbytes, $options->maxbytes))); $html .= <<<FMHTML <div class="filemanager-loading mdl-align" id='filemanager-loading-{$client_id}'> $icon_progress </div> <div id="filemanager-wrapper-{$client_id}" style="display:none"> <div class="fm-breadcrumb" id="fm-path-{$client_id}"></div> <div class="filemanager-toolbar"> <input type="button" class="fm-btn-add" id="btnadd-{$client_id}" onclick="return false" value="{$straddfile}" /> <input type="button" class="fm-btn-mkdir" id="btncrt-{$client_id}" onclick="return false" value="{$strmakedir}" /> <input type="button" class="fm-btn-download" id="btndwn-{$client_id}" onclick="return false" {$extra} value="{$strdownload}" /> <span> $maxsize </span> </div> <div class="filemanager-container" id="filemanager-{$client_id}"> <ul id="draftfiles-{$client_id}" class="fm-filelist"> <li>Loading...</li> </ul> </div> </div> <div class='clearer'></div> FMHTML; if (empty($filemanagertemplateloaded)) { $filemanagertemplateloaded = true; $html .= <<<FMHTML <div id="fm-template" style="display:none">___fullname___ ___action___</div> FMHTML; } $module = array( 'name'=>'form_filemanager', 'fullpath'=>'/lib/form/filemanager.js', 'requires' => array('core_filepicker', 'base', 'io-base', 'node', 'json', 'yui2-button', 'yui2-container', 'yui2-layout', 'yui2-menu', 'yui2-treeview'), 'strings' => array(array('loading', 'repository'), array('nomorefiles', 'repository'), array('confirmdeletefile', 'repository'), array('add', 'repository'), array('accessiblefilepicker', 'repository'), array('move', 'moodle'), array('cancel', 'moodle'), array('download', 'moodle'), array('ok', 'moodle'), array('emptylist', 'repository'), array('nofilesattached', 'repository'), array('entername', 'repository'), array('enternewname', 'repository'), array('zip', 'editor'), array('unzip', 'moodle'), array('rename', 'moodle'), array('delete', 'moodle'), array('cannotdeletefile', 'error'), array('confirmdeletefile', 'repository'), array('nopathselected', 'repository'), array('popupblockeddownload', 'repository'), array('draftareanofiles', 'repository'), array('path', 'moodle'), array('setmainfile', 'repository'), array('moving', 'repository'), array('files', 'moodle') ) ); $PAGE->requires->js_module($module); $PAGE->requires->js_init_call('M.form_filemanager.init', array($options), true, $module); // non javascript file manager $filemanagerurl = new moodle_url('/repository/draftfiles_manager.php', array( 'env'=>'filemanager', 'action'=>'browse', 'itemid'=>$itemid, 'subdirs'=>$options->subdirs, 'maxbytes'=>$options->maxbytes, 'maxfiles'=>$options->maxfiles, 'ctx_id'=>$PAGE->context->id, 'course'=>$PAGE->course->id, 'sesskey'=>sesskey(), )); $html .= '<noscript>'; $html .= "<div><object type='text/html' data='$filemanagerurl' height='160' width='600' style='border:1px solid #000'></object></div>"; $html .= '</noscript>'; return $html; } To be replaced on forms/filemanager.php: - $html .= form_filemanager_render($options); + $fm = new file_manager($options); + $options = $fm->options; + $html .= $OUTPUT->render($fm); Also delete form_filemanaer_x class and form_filemanager_render function on the same file
    • Affected Branches:
      MOODLE_22_STABLE

      Description

      File manager renderers are hardcoded in the form libs. The functions are yet implemented but cannot be overriden by the theme renderers.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                marina Marina Glancy
                Reporter:
                pferre22 Pau Ferrer
                Participants:
                Component watchers:
                Adrian Greeve, Mihail Geshoski, Peter Dias, Jake Dallimore, Jun Pataleta, Ryan Wyllie
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: