Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 402] [Versions 39 and 403]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   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.
   8  //
   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.
  13  //
  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/>.
  16  
  17  /**
  18   * This file contains all necessary code to define a wiki file table form element
  19   *
  20   * @package mod_wiki
  21   * @copyright 2009 Marc Alier, Jordi Piguillem marc.alier@upc.edu
  22   * @copyright 2009 Universitat Politecnica de Catalunya http://www.upc.edu
  23   *
  24   * @author Josep Arus
  25   *
  26   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  27   */
  28  
  29  require_once('HTML/QuickForm/element.php');
  30  require_once($CFG->dirroot.'/lib/filelib.php');
  31  
  32  class MoodleQuickForm_wikifiletable extends HTML_QuickForm_element {
  33  
  34      private $_contextid;
  35      private $_filearea;
  36      private $_fileareaitemid;
  37      private $_fileinfo;
  38      private $_value = array();
  39  
  40      public function __construct($elementName = null, $elementLabel = null, $attributes = null, $fileinfo = null, $format = null) {
  41  
  42          parent::__construct($elementName, $elementLabel, $attributes);
  43          $this->_fileinfo = $fileinfo;
  44          $this->_format = $format;
  45      }
  46  
  47      /**
  48       * Old syntax of class constructor. Deprecated in PHP7.
  49       *
  50       * @deprecated since Moodle 3.1
  51       */
  52      public function MoodleQuickForm_wikifiletable($elementName = null, $elementLabel = null, $attributes = null, $fileinfo = null, $format = null) {
  53          debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
  54          self::__construct($elementName, $elementLabel, $attributes, $fileinfo, $format);
  55      }
  56  
  57      function onQuickFormEvent($event, $arg, &$caller) {
  58          global $OUTPUT;
  59  
  60          switch ($event) {
  61              case 'addElement':
  62                  $this->_contextid = $arg[3]['contextid'];
  63                  $this->_filearea = $arg[3]['filearea'];
  64                  $this->_fileareaitemid = $arg[3]['itemid'];
  65                  $this->_format = $arg[4];
  66                  break;
  67          }
  68  
  69          return parent::onQuickFormEvent($event, $arg, $caller);
  70      }
  71  
  72      function setName($name) {
  73          $this->updateAttributes(array('name' => $name));
  74      }
  75  
  76      function getName() {
  77          return $this->getAttribute('name');
  78      }
  79  
  80      function setValue($value) {
  81          $this->_value = $value;
  82      }
  83  
  84      function getValue() {
  85          return $this->_value;
  86      }
  87  
  88      function toHtml() {
  89          global $CFG, $OUTPUT;
  90  
  91          $htmltable = new html_table();
  92  
  93          $htmltable->head = array(get_string('deleteupload', 'wiki'), get_string('uploadname', 'wiki'), get_string('uploadactions', 'wiki'));
  94  
  95          $fs = get_file_storage();
  96  
  97          $files = $fs->get_area_files($this->_fileinfo['contextid'], 'mod_wiki', 'attachments', $this->_fileinfo['itemid']); //TODO: verify where this is coming from, all params must be validated (skodak)
  98  
  99          if (count($files) < 2) {
 100              return get_string('noattachments', 'wiki');
 101          }
 102  
 103          //get tags
 104          foreach (array('image', 'attach', 'link') as $tag) {
 105              $tags[$tag] = wiki_parser_get_token($this->_format, $tag);
 106          }
 107  
 108          foreach ($files as $file) {
 109              if (!$file->is_directory()) {
 110                  $checkbox = '<input type="checkbox" name="'.$this->_attributes['name'].'[]" value="'.$file->get_pathnamehash().'"';
 111  
 112                  if (in_array($file->get_pathnamehash(), $this->_value)) {
 113                      $checkbox .= ' checked="checked"';
 114                  }
 115                  $checkbox .= " />";
 116  
 117                  //actions
 118                  $icon = file_file_icon($file);
 119                  $file_url = file_encode_url($CFG->wwwroot.'/pluginfile.php', "/{$this->_contextid}/mod_wiki/attachments/{$this->_fileareaitemid}/".$file->get_filename());
 120  
 121                  $action_icons = "";
 122                  if(!empty($tags['attach'])) {
 123                      $action_icons .= "<a href=\"javascript:void(0)\" class=\"wiki-attachment-attach\" ".$this->printInsertTags($tags['attach'], $file->get_filename())." title=\"".get_string('attachmentattach', 'wiki')."\">".$OUTPUT->pix_icon($icon, "Attach")."</a>"; //TODO: localize
 124                  }
 125  
 126                  $action_icons .= "&nbsp;&nbsp;<a href=\"javascript:void(0)\" class=\"wiki-attachment-link\" ".$this->printInsertTags($tags['link'], $file_url)." title=\"".get_string('attachmentlink', 'wiki')."\">".$OUTPUT->pix_icon($icon, "Link")."</a>";
 127  
 128                  if (file_mimetype_in_typegroup($file->get_mimetype(), 'web_image')) {
 129                      $action_icons .= "&nbsp;&nbsp;<a href=\"javascript:void(0)\" class=\"wiki-attachment-image\" ".$this->printInsertTags($tags['image'], $file->get_filename())." title=\"".get_string('attachmentimage', 'wiki')."\">".$OUTPUT->pix_icon($icon, "Image")."</a>"; //TODO: localize
 130                  }
 131  
 132                  $htmltable->data[] = array($checkbox, '<a href="'.$file_url.'">'.$file->get_filename().'</a>', $action_icons);
 133              }
 134          }
 135  
 136          return html_writer::table($htmltable);
 137      }
 138  
 139      private function printInsertTags($tags, $value) {
 140          return "onclick=\"javascript:insertTags('{$tags[0]}', '{$tags[1]}', '$value');\"";
 141      }
 142  }
 143  
 144  //register wikieditor
 145  MoodleQuickForm::registerElementType('wikifiletable', $CFG->dirroot."/mod/wiki/editors/wikifiletable.php", 'MoodleQuickForm_wikifiletable');
 146  
 147