[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/label/ -> lib.php (source)

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * Library of functions and constants for module label
  20   *
  21   * @package mod_label
  22   * @copyright  1999 onwards Martin Dougiamas  {@link http://moodle.com}
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die;
  27  
  28  /** LABEL_MAX_NAME_LENGTH = 50 */
  29  define("LABEL_MAX_NAME_LENGTH", 50);
  30  
  31  /**
  32   * @uses LABEL_MAX_NAME_LENGTH
  33   * @param object $label
  34   * @return string
  35   */
  36  function get_label_name($label) {
  37      $name = strip_tags(format_string($label->intro,true));
  38      if (core_text::strlen($name) > LABEL_MAX_NAME_LENGTH) {
  39          $name = core_text::substr($name, 0, LABEL_MAX_NAME_LENGTH)."...";
  40      }
  41  
  42      if (empty($name)) {
  43          // arbitrary name
  44          $name = get_string('modulename','label');
  45      }
  46  
  47      return $name;
  48  }
  49  /**
  50   * Given an object containing all the necessary data,
  51   * (defined by the form in mod_form.php) this function
  52   * will create a new instance and return the id number
  53   * of the new instance.
  54   *
  55   * @global object
  56   * @param object $label
  57   * @return bool|int
  58   */
  59  function label_add_instance($label) {
  60      global $DB;
  61  
  62      $label->name = get_label_name($label);
  63      $label->timemodified = time();
  64  
  65      return $DB->insert_record("label", $label);
  66  }
  67  
  68  /**
  69   * Given an object containing all the necessary data,
  70   * (defined by the form in mod_form.php) this function
  71   * will update an existing instance with new data.
  72   *
  73   * @global object
  74   * @param object $label
  75   * @return bool
  76   */
  77  function label_update_instance($label) {
  78      global $DB;
  79  
  80      $label->name = get_label_name($label);
  81      $label->timemodified = time();
  82      $label->id = $label->instance;
  83  
  84      return $DB->update_record("label", $label);
  85  }
  86  
  87  /**
  88   * Given an ID of an instance of this module,
  89   * this function will permanently delete the instance
  90   * and any data that depends on it.
  91   *
  92   * @global object
  93   * @param int $id
  94   * @return bool
  95   */
  96  function label_delete_instance($id) {
  97      global $DB;
  98  
  99      if (! $label = $DB->get_record("label", array("id"=>$id))) {
 100          return false;
 101      }
 102  
 103      $result = true;
 104  
 105      if (! $DB->delete_records("label", array("id"=>$label->id))) {
 106          $result = false;
 107      }
 108  
 109      return $result;
 110  }
 111  
 112  /**
 113   * Given a course_module object, this function returns any
 114   * "extra" information that may be needed when printing
 115   * this activity in a course listing.
 116   * See get_array_of_activities() in course/lib.php
 117   *
 118   * @global object
 119   * @param object $coursemodule
 120   * @return cached_cm_info|null
 121   */
 122  function label_get_coursemodule_info($coursemodule) {
 123      global $DB;
 124  
 125      if ($label = $DB->get_record('label', array('id'=>$coursemodule->instance), 'id, name, intro, introformat')) {
 126          if (empty($label->name)) {
 127              // label name missing, fix it
 128              $label->name = "label{$label->id}";
 129              $DB->set_field('label', 'name', $label->name, array('id'=>$label->id));
 130          }
 131          $info = new cached_cm_info();
 132          // no filtering hre because this info is cached and filtered later
 133          $info->content = format_module_intro('label', $label, $coursemodule->id, false);
 134          $info->name  = $label->name;
 135          return $info;
 136      } else {
 137          return null;
 138      }
 139  }
 140  
 141  /**
 142   * This function is used by the reset_course_userdata function in moodlelib.
 143   *
 144   * @param object $data the data submitted from the reset course.
 145   * @return array status array
 146   */
 147  function label_reset_userdata($data) {
 148      return array();
 149  }
 150  
 151  /**
 152   * Returns all other caps used in module
 153   *
 154   * @return array
 155   */
 156  function label_get_extra_capabilities() {
 157      return array('moodle/site:accessallgroups');
 158  }
 159  
 160  /**
 161   * @uses FEATURE_IDNUMBER
 162   * @uses FEATURE_GROUPS
 163   * @uses FEATURE_GROUPINGS
 164   * @uses FEATURE_MOD_INTRO
 165   * @uses FEATURE_COMPLETION_TRACKS_VIEWS
 166   * @uses FEATURE_GRADE_HAS_GRADE
 167   * @uses FEATURE_GRADE_OUTCOMES
 168   * @param string $feature FEATURE_xx constant for requested feature
 169   * @return bool|null True if module supports feature, false if not, null if doesn't know
 170   */
 171  function label_supports($feature) {
 172      switch($feature) {
 173          case FEATURE_IDNUMBER:                return false;
 174          case FEATURE_GROUPS:                  return false;
 175          case FEATURE_GROUPINGS:               return false;
 176          case FEATURE_MOD_INTRO:               return true;
 177          case FEATURE_COMPLETION_TRACKS_VIEWS: return false;
 178          case FEATURE_GRADE_HAS_GRADE:         return false;
 179          case FEATURE_GRADE_OUTCOMES:          return false;
 180          case FEATURE_MOD_ARCHETYPE:           return MOD_ARCHETYPE_RESOURCE;
 181          case FEATURE_BACKUP_MOODLE2:          return true;
 182          case FEATURE_NO_VIEW_LINK:            return true;
 183  
 184          default: return null;
 185      }
 186  }
 187  
 188  /**
 189   * Register the ability to handle drag and drop file uploads
 190   * @return array containing details of the files / types the mod can handle
 191   */
 192  function label_dndupload_register() {
 193      $strdnd = get_string('dnduploadlabel', 'mod_label');
 194      if (get_config('label', 'dndmedia')) {
 195          $mediaextensions = file_get_typegroup('extension', 'web_image');
 196          $files = array();
 197          foreach ($mediaextensions as $extn) {
 198              $extn = trim($extn, '.');
 199              $files[] = array('extension' => $extn, 'message' => $strdnd);
 200          }
 201          $ret = array('files' => $files);
 202      } else {
 203          $ret = array();
 204      }
 205  
 206      $strdndtext = get_string('dnduploadlabeltext', 'mod_label');
 207      return array_merge($ret, array('types' => array(
 208          array('identifier' => 'text/html', 'message' => $strdndtext, 'noname' => true),
 209          array('identifier' => 'text', 'message' => $strdndtext, 'noname' => true)
 210      )));
 211  }
 212  
 213  /**
 214   * Handle a file that has been uploaded
 215   * @param object $uploadinfo details of the file / content that has been uploaded
 216   * @return int instance id of the newly created mod
 217   */
 218  function label_dndupload_handle($uploadinfo) {
 219      global $USER;
 220  
 221      // Gather the required info.
 222      $data = new stdClass();
 223      $data->course = $uploadinfo->course->id;
 224      $data->name = $uploadinfo->displayname;
 225      $data->intro = '';
 226      $data->introformat = FORMAT_HTML;
 227      $data->coursemodule = $uploadinfo->coursemodule;
 228  
 229      // Extract the first (and only) file from the file area and add it to the label as an img tag.
 230      if (!empty($uploadinfo->draftitemid)) {
 231          $fs = get_file_storage();
 232          $draftcontext = context_user::instance($USER->id);
 233          $context = context_module::instance($uploadinfo->coursemodule);
 234          $files = $fs->get_area_files($draftcontext->id, 'user', 'draft', $uploadinfo->draftitemid, '', false);
 235          if ($file = reset($files)) {
 236              if (file_mimetype_in_typegroup($file->get_mimetype(), 'web_image')) {
 237                  // It is an image - resize it, if too big, then insert the img tag.
 238                  $config = get_config('label');
 239                  $data->intro = label_generate_resized_image($file, $config->dndresizewidth, $config->dndresizeheight);
 240              } else {
 241                  // We aren't supposed to be supporting non-image types here, but fallback to adding a link, just in case.
 242                  $url = moodle_url::make_draftfile_url($file->get_itemid(), $file->get_filepath(), $file->get_filename());
 243                  $data->intro = html_writer::link($url, $file->get_filename());
 244              }
 245              $data->intro = file_save_draft_area_files($uploadinfo->draftitemid, $context->id, 'mod_label', 'intro', 0,
 246                                                        null, $data->intro);
 247          }
 248      } else if (!empty($uploadinfo->content)) {
 249          $data->intro = $uploadinfo->content;
 250          if ($uploadinfo->type != 'text/html') {
 251              $data->introformat = FORMAT_PLAIN;
 252          }
 253      }
 254  
 255      return label_add_instance($data, null);
 256  }
 257  
 258  /**
 259   * Resize the image, if required, then generate an img tag and, if required, a link to the full-size image
 260   * @param stored_file $file the image file to process
 261   * @param int $maxwidth the maximum width allowed for the image
 262   * @param int $maxheight the maximum height allowed for the image
 263   * @return string HTML fragment to add to the label
 264   */
 265  function label_generate_resized_image(stored_file $file, $maxwidth, $maxheight) {
 266      global $CFG;
 267  
 268      $fullurl = moodle_url::make_draftfile_url($file->get_itemid(), $file->get_filepath(), $file->get_filename());
 269      $link = null;
 270      $attrib = array('alt' => $file->get_filename(), 'src' => $fullurl);
 271  
 272      if ($imginfo = $file->get_imageinfo()) {
 273          // Work out the new width / height, bounded by maxwidth / maxheight
 274          $width = $imginfo['width'];
 275          $height = $imginfo['height'];
 276          if (!empty($maxwidth) && $width > $maxwidth) {
 277              $height *= (float)$maxwidth / $width;
 278              $width = $maxwidth;
 279          }
 280          if (!empty($maxheight) && $height > $maxheight) {
 281              $width *= (float)$maxheight / $height;
 282              $height = $maxheight;
 283          }
 284  
 285          $attrib['width'] = $width;
 286          $attrib['height'] = $height;
 287  
 288          // If the size has changed and the image is of a suitable mime type, generate a smaller version
 289          if ($width != $imginfo['width']) {
 290              $mimetype = $file->get_mimetype();
 291              if ($mimetype === 'image/gif' or $mimetype === 'image/jpeg' or $mimetype === 'image/png') {
 292                  require_once($CFG->libdir.'/gdlib.php');
 293                  $data = $file->generate_image_thumbnail($width, $height);
 294  
 295                  if (!empty($data)) {
 296                      $fs = get_file_storage();
 297                      $record = array(
 298                          'contextid' => $file->get_contextid(),
 299                          'component' => $file->get_component(),
 300                          'filearea'  => $file->get_filearea(),
 301                          'itemid'    => $file->get_itemid(),
 302                          'filepath'  => '/',
 303                          'filename'  => 's_'.$file->get_filename(),
 304                      );
 305                      $smallfile = $fs->create_file_from_string($record, $data);
 306  
 307                      // Replace the image 'src' with the resized file and link to the original
 308                      $attrib['src'] = moodle_url::make_draftfile_url($smallfile->get_itemid(), $smallfile->get_filepath(),
 309                                                                      $smallfile->get_filename());
 310                      $link = $fullurl;
 311                  }
 312              }
 313          }
 314  
 315      } else {
 316          // Assume this is an image type that get_imageinfo cannot handle (e.g. SVG)
 317          $attrib['width'] = $maxwidth;
 318      }
 319  
 320      $img = html_writer::empty_tag('img', $attrib);
 321      if ($link) {
 322          return html_writer::link($link, $img);
 323      } else {
 324          return $img;
 325      }
 326  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1