[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/resource/ -> locallib.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   * Private resource module utility functions
  20   *
  21   * @package    mod_resource
  22   * @copyright  2009 Petr Skoda  {@link http://skodak.org}
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die;
  27  
  28  require_once("$CFG->libdir/filelib.php");
  29  require_once("$CFG->libdir/resourcelib.php");
  30  require_once("$CFG->dirroot/mod/resource/lib.php");
  31  
  32  /**
  33   * Redirected to migrated resource if needed,
  34   * return if incorrect parameters specified
  35   * @param int $oldid
  36   * @param int $cmid
  37   * @return void
  38   */
  39  function resource_redirect_if_migrated($oldid, $cmid) {
  40      global $DB, $CFG;
  41  
  42      if ($oldid) {
  43          $old = $DB->get_record('resource_old', array('oldid'=>$oldid));
  44      } else {
  45          $old = $DB->get_record('resource_old', array('cmid'=>$cmid));
  46      }
  47  
  48      if (!$old) {
  49          return;
  50      }
  51  
  52      redirect("$CFG->wwwroot/mod/$old->newmodule/view.php?id=".$old->cmid);
  53  }
  54  
  55  /**
  56   * Display embedded resource file.
  57   * @param object $resource
  58   * @param object $cm
  59   * @param object $course
  60   * @param stored_file $file main file
  61   * @return does not return
  62   */
  63  function resource_display_embed($resource, $cm, $course, $file) {
  64      global $CFG, $PAGE, $OUTPUT;
  65  
  66      $clicktoopen = resource_get_clicktoopen($file, $resource->revision);
  67  
  68      $context = context_module::instance($cm->id);
  69      $path = '/'.$context->id.'/mod_resource/content/'.$resource->revision.$file->get_filepath().$file->get_filename();
  70      $fullurl = file_encode_url($CFG->wwwroot.'/pluginfile.php', $path, false);
  71      $moodleurl = new moodle_url('/pluginfile.php' . $path);
  72  
  73      $mimetype = $file->get_mimetype();
  74      $title    = $resource->name;
  75  
  76      $extension = resourcelib_get_extension($file->get_filename());
  77  
  78      $mediarenderer = $PAGE->get_renderer('core', 'media');
  79      $embedoptions = array(
  80          core_media::OPTION_TRUSTED => true,
  81          core_media::OPTION_BLOCK => true,
  82      );
  83  
  84      if (file_mimetype_in_typegroup($mimetype, 'web_image')) {  // It's an image
  85          $code = resourcelib_embed_image($fullurl, $title);
  86  
  87      } else if ($mimetype === 'application/pdf') {
  88          // PDF document
  89          $code = resourcelib_embed_pdf($fullurl, $title, $clicktoopen);
  90  
  91      } else if ($mediarenderer->can_embed_url($moodleurl, $embedoptions)) {
  92          // Media (audio/video) file.
  93          $code = $mediarenderer->embed_url($moodleurl, $title, 0, 0, $embedoptions);
  94  
  95      } else {
  96          // anything else - just try object tag enlarged as much as possible
  97          $code = resourcelib_embed_general($fullurl, $title, $clicktoopen, $mimetype);
  98      }
  99  
 100      resource_print_header($resource, $cm, $course);
 101      resource_print_heading($resource, $cm, $course);
 102  
 103      echo $code;
 104  
 105      resource_print_intro($resource, $cm, $course);
 106  
 107      echo $OUTPUT->footer();
 108      die;
 109  }
 110  
 111  /**
 112   * Display resource frames.
 113   * @param object $resource
 114   * @param object $cm
 115   * @param object $course
 116   * @param stored_file $file main file
 117   * @return does not return
 118   */
 119  function resource_display_frame($resource, $cm, $course, $file) {
 120      global $PAGE, $OUTPUT, $CFG;
 121  
 122      $frame = optional_param('frameset', 'main', PARAM_ALPHA);
 123  
 124      if ($frame === 'top') {
 125          $PAGE->set_pagelayout('frametop');
 126          resource_print_header($resource, $cm, $course);
 127          resource_print_heading($resource, $cm, $course);
 128          resource_print_intro($resource, $cm, $course);
 129          echo $OUTPUT->footer();
 130          die;
 131  
 132      } else {
 133          $config = get_config('resource');
 134          $context = context_module::instance($cm->id);
 135          $path = '/'.$context->id.'/mod_resource/content/'.$resource->revision.$file->get_filepath().$file->get_filename();
 136          $fileurl = file_encode_url($CFG->wwwroot.'/pluginfile.php', $path, false);
 137          $navurl = "$CFG->wwwroot/mod/resource/view.php?id=$cm->id&amp;frameset=top";
 138          $title = strip_tags(format_string($course->shortname.': '.$resource->name));
 139          $framesize = $config->framesize;
 140          $contentframetitle = s(format_string($resource->name));
 141          $modulename = s(get_string('modulename','resource'));
 142          $dir = get_string('thisdirection', 'langconfig');
 143  
 144          $file = <<<EOF
 145  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
 146  <html dir="$dir">
 147    <head>
 148      <meta http-equiv="content-type" content="text/html; charset=utf-8" />
 149      <title>$title</title>
 150    </head>
 151    <frameset rows="$framesize,*">
 152      <frame src="$navurl" title="$modulename" />
 153      <frame src="$fileurl" title="$contentframetitle" />
 154    </frameset>
 155  </html>
 156  EOF;
 157  
 158          @header('Content-Type: text/html; charset=utf-8');
 159          echo $file;
 160          die;
 161      }
 162  }
 163  
 164  /**
 165   * Internal function - create click to open text with link.
 166   */
 167  function resource_get_clicktoopen($file, $revision, $extra='') {
 168      global $CFG;
 169  
 170      $filename = $file->get_filename();
 171      $path = '/'.$file->get_contextid().'/mod_resource/content/'.$revision.$file->get_filepath().$file->get_filename();
 172      $fullurl = file_encode_url($CFG->wwwroot.'/pluginfile.php', $path, false);
 173  
 174      $string = get_string('clicktoopen2', 'resource', "<a href=\"$fullurl\" $extra>$filename</a>");
 175  
 176      return $string;
 177  }
 178  
 179  /**
 180   * Internal function - create click to open text with link.
 181   */
 182  function resource_get_clicktodownload($file, $revision) {
 183      global $CFG;
 184  
 185      $filename = $file->get_filename();
 186      $path = '/'.$file->get_contextid().'/mod_resource/content/'.$revision.$file->get_filepath().$file->get_filename();
 187      $fullurl = file_encode_url($CFG->wwwroot.'/pluginfile.php', $path, true);
 188  
 189      $string = get_string('clicktodownload', 'resource', "<a href=\"$fullurl\">$filename</a>");
 190  
 191      return $string;
 192  }
 193  
 194  /**
 195   * Print resource info and workaround link when JS not available.
 196   * @param object $resource
 197   * @param object $cm
 198   * @param object $course
 199   * @param stored_file $file main file
 200   * @return does not return
 201   */
 202  function resource_print_workaround($resource, $cm, $course, $file) {
 203      global $CFG, $OUTPUT;
 204  
 205      resource_print_header($resource, $cm, $course);
 206      resource_print_heading($resource, $cm, $course, true);
 207      resource_print_intro($resource, $cm, $course, true);
 208  
 209      $resource->mainfile = $file->get_filename();
 210      echo '<div class="resourceworkaround">';
 211      switch (resource_get_final_display_type($resource)) {
 212          case RESOURCELIB_DISPLAY_POPUP:
 213              $path = '/'.$file->get_contextid().'/mod_resource/content/'.$resource->revision.$file->get_filepath().$file->get_filename();
 214              $fullurl = file_encode_url($CFG->wwwroot.'/pluginfile.php', $path, false);
 215              $options = empty($resource->displayoptions) ? array() : unserialize($resource->displayoptions);
 216              $width  = empty($options['popupwidth'])  ? 620 : $options['popupwidth'];
 217              $height = empty($options['popupheight']) ? 450 : $options['popupheight'];
 218              $wh = "width=$width,height=$height,toolbar=no,location=no,menubar=no,copyhistory=no,status=no,directories=no,scrollbars=yes,resizable=yes";
 219              $extra = "onclick=\"window.open('$fullurl', '', '$wh'); return false;\"";
 220              echo resource_get_clicktoopen($file, $resource->revision, $extra);
 221              break;
 222  
 223          case RESOURCELIB_DISPLAY_NEW:
 224              $extra = 'onclick="this.target=\'_blank\'"';
 225              echo resource_get_clicktoopen($file, $resource->revision, $extra);
 226              break;
 227  
 228          case RESOURCELIB_DISPLAY_DOWNLOAD:
 229              echo resource_get_clicktodownload($file, $resource->revision);
 230              break;
 231  
 232          case RESOURCELIB_DISPLAY_OPEN:
 233          default:
 234              echo resource_get_clicktoopen($file, $resource->revision);
 235              break;
 236      }
 237      echo '</div>';
 238  
 239      echo $OUTPUT->footer();
 240      die;
 241  }
 242  
 243  /**
 244   * Print resource header.
 245   * @param object $resource
 246   * @param object $cm
 247   * @param object $course
 248   * @return void
 249   */
 250  function resource_print_header($resource, $cm, $course) {
 251      global $PAGE, $OUTPUT;
 252  
 253      $PAGE->set_title($course->shortname.': '.$resource->name);
 254      $PAGE->set_heading($course->fullname);
 255      $PAGE->set_activity_record($resource);
 256      $PAGE->set_button(update_module_button($cm->id, '', get_string('modulename', 'resource')));
 257      echo $OUTPUT->header();
 258  }
 259  
 260  /**
 261   * Print resource heading.
 262   * @param object $resource
 263   * @param object $cm
 264   * @param object $course
 265   * @param bool $notused This variable is no longer used
 266   * @return void
 267   */
 268  function resource_print_heading($resource, $cm, $course, $notused = false) {
 269      global $OUTPUT;
 270      echo $OUTPUT->heading(format_string($resource->name), 2);
 271  }
 272  
 273  
 274  /**
 275   * Gets details of the file to cache in course cache to be displayed using {@link resource_get_optional_details()}
 276   *
 277   * @param object $resource Resource table row (only property 'displayoptions' is used here)
 278   * @param object $cm Course-module table row
 279   * @return string Size and type or empty string if show options are not enabled
 280   */
 281  function resource_get_file_details($resource, $cm) {
 282      $options = empty($resource->displayoptions) ? array() : @unserialize($resource->displayoptions);
 283      $filedetails = array();
 284      if (!empty($options['showsize']) || !empty($options['showtype']) || !empty($options['showdate'])) {
 285          $context = context_module::instance($cm->id);
 286          $fs = get_file_storage();
 287          $files = $fs->get_area_files($context->id, 'mod_resource', 'content', 0, 'sortorder DESC, id ASC', false);
 288          // For a typical file resource, the sortorder is 1 for the main file
 289          // and 0 for all other files. This sort approach is used just in case
 290          // there are situations where the file has a different sort order.
 291          $mainfile = $files ? reset($files) : null;
 292          if (!empty($options['showsize'])) {
 293              $filedetails['size'] = 0;
 294              foreach ($files as $file) {
 295                  // This will also synchronize the file size for external files if needed.
 296                  $filedetails['size'] += $file->get_filesize();
 297                  if ($file->get_repository_id()) {
 298                      // If file is a reference the 'size' attribute can not be cached.
 299                      $filedetails['isref'] = true;
 300                  }
 301              }
 302          }
 303          if (!empty($options['showtype'])) {
 304              if ($mainfile) {
 305                  $filedetails['type'] = get_mimetype_description($mainfile);
 306                  // Only show type if it is not unknown.
 307                  if ($filedetails['type'] === get_mimetype_description('document/unknown')) {
 308                      $filedetails['type'] = '';
 309                  }
 310              } else {
 311                  $filedetails['type'] = '';
 312              }
 313          }
 314          if (!empty($options['showdate'])) {
 315              if ($mainfile) {
 316                  // Modified date may be up to several minutes later than uploaded date just because
 317                  // teacher did not submit the form promptly. Give teacher up to 5 minutes to do it.
 318                  if ($mainfile->get_timemodified() > $mainfile->get_timecreated() + 5 * MINSECS) {
 319                      $filedetails['modifieddate'] = $mainfile->get_timemodified();
 320                  } else {
 321                      $filedetails['uploadeddate'] = $mainfile->get_timecreated();
 322                  }
 323                  if ($mainfile->get_repository_id()) {
 324                      // If main file is a reference the 'date' attribute can not be cached.
 325                      $filedetails['isref'] = true;
 326                  }
 327              } else {
 328                  $filedetails['uploadeddate'] = '';
 329              }
 330          }
 331      }
 332      return $filedetails;
 333  }
 334  
 335  /**
 336   * Gets optional details for a resource, depending on resource settings.
 337   *
 338   * Result may include the file size and type if those settings are chosen,
 339   * or blank if none.
 340   *
 341   * @param object $resource Resource table row (only property 'displayoptions' is used here)
 342   * @param object $cm Course-module table row
 343   * @return string Size and type or empty string if show options are not enabled
 344   */
 345  function resource_get_optional_details($resource, $cm) {
 346      global $DB;
 347  
 348      $details = '';
 349  
 350      $options = empty($resource->displayoptions) ? array() : @unserialize($resource->displayoptions);
 351      if (!empty($options['showsize']) || !empty($options['showtype']) || !empty($options['showdate'])) {
 352          if (!array_key_exists('filedetails', $options)) {
 353              $filedetails = resource_get_file_details($resource, $cm);
 354          } else {
 355              $filedetails = $options['filedetails'];
 356          }
 357          $size = '';
 358          $type = '';
 359          $date = '';
 360          $langstring = '';
 361          $infodisplayed = 0;
 362          if (!empty($options['showsize'])) {
 363              if (!empty($filedetails['size'])) {
 364                  $size = display_size($filedetails['size']);
 365                  $langstring .= 'size';
 366                  $infodisplayed += 1;
 367              }
 368          }
 369          if (!empty($options['showtype'])) {
 370              if (!empty($filedetails['type'])) {
 371                  $type = $filedetails['type'];
 372                  $langstring .= 'type';
 373                  $infodisplayed += 1;
 374              }
 375          }
 376          if (!empty($options['showdate']) && (!empty($filedetails['modifieddate']) || !empty($filedetails['uploadeddate']))) {
 377              if (!empty($filedetails['modifieddate'])) {
 378                  $date = get_string('modifieddate', 'mod_resource', userdate($filedetails['modifieddate'],
 379                      get_string('strftimedatetimeshort', 'langconfig')));
 380              } else if (!empty($filedetails['uploadeddate'])) {
 381                  $date = get_string('uploadeddate', 'mod_resource', userdate($filedetails['uploadeddate'],
 382                      get_string('strftimedatetimeshort', 'langconfig')));
 383              }
 384              $langstring .= 'date';
 385              $infodisplayed += 1;
 386          }
 387  
 388          if ($infodisplayed > 1) {
 389              $details = get_string("resourcedetails_{$langstring}", 'resource',
 390                      (object)array('size' => $size, 'type' => $type, 'date' => $date));
 391          } else {
 392              // Only one of size, type and date is set, so just append.
 393              $details = $size . $type . $date;
 394          }
 395      }
 396  
 397      return $details;
 398  }
 399  
 400  /**
 401   * Print resource introduction.
 402   * @param object $resource
 403   * @param object $cm
 404   * @param object $course
 405   * @param bool $ignoresettings print even if not specified in modedit
 406   * @return void
 407   */
 408  function resource_print_intro($resource, $cm, $course, $ignoresettings=false) {
 409      global $OUTPUT;
 410  
 411      $options = empty($resource->displayoptions) ? array() : unserialize($resource->displayoptions);
 412  
 413      $extraintro = resource_get_optional_details($resource, $cm);
 414      if ($extraintro) {
 415          // Put a paragaph tag around the details
 416          $extraintro = html_writer::tag('p', $extraintro, array('class' => 'resourcedetails'));
 417      }
 418  
 419      if ($ignoresettings || !empty($options['printintro']) || $extraintro) {
 420          $gotintro = trim(strip_tags($resource->intro));
 421          if ($gotintro || $extraintro) {
 422              echo $OUTPUT->box_start('mod_introbox', 'resourceintro');
 423              if ($gotintro) {
 424                  echo format_module_intro('resource', $resource, $cm->id);
 425              }
 426              echo $extraintro;
 427              echo $OUTPUT->box_end();
 428          }
 429      }
 430  }
 431  
 432  /**
 433   * Print warning that instance not migrated yet.
 434   * @param object $resource
 435   * @param object $cm
 436   * @param object $course
 437   * @return void, does not return
 438   */
 439  function resource_print_tobemigrated($resource, $cm, $course) {
 440      global $DB, $OUTPUT;
 441  
 442      $resource_old = $DB->get_record('resource_old', array('oldid'=>$resource->id));
 443      resource_print_header($resource, $cm, $course);
 444      resource_print_heading($resource, $cm, $course);
 445      resource_print_intro($resource, $cm, $course);
 446      echo $OUTPUT->notification(get_string('notmigrated', 'resource', $resource_old->type));
 447      echo $OUTPUT->footer();
 448      die;
 449  }
 450  
 451  /**
 452   * Print warning that file can not be found.
 453   * @param object $resource
 454   * @param object $cm
 455   * @param object $course
 456   * @return void, does not return
 457   */
 458  function resource_print_filenotfound($resource, $cm, $course) {
 459      global $DB, $OUTPUT;
 460  
 461      $resource_old = $DB->get_record('resource_old', array('oldid'=>$resource->id));
 462      resource_print_header($resource, $cm, $course);
 463      resource_print_heading($resource, $cm, $course);
 464      resource_print_intro($resource, $cm, $course);
 465      if ($resource_old) {
 466          echo $OUTPUT->notification(get_string('notmigrated', 'resource', $resource_old->type));
 467      } else {
 468          echo $OUTPUT->notification(get_string('filenotfound', 'resource'));
 469      }
 470      echo $OUTPUT->footer();
 471      die;
 472  }
 473  
 474  /**
 475   * Decide the best display format.
 476   * @param object $resource
 477   * @return int display type constant
 478   */
 479  function resource_get_final_display_type($resource) {
 480      global $CFG, $PAGE;
 481  
 482      if ($resource->display != RESOURCELIB_DISPLAY_AUTO) {
 483          return $resource->display;
 484      }
 485  
 486      if (empty($resource->mainfile)) {
 487          return RESOURCELIB_DISPLAY_DOWNLOAD;
 488      } else {
 489          $mimetype = mimeinfo('type', $resource->mainfile);
 490      }
 491  
 492      if (file_mimetype_in_typegroup($mimetype, 'archive')) {
 493          return RESOURCELIB_DISPLAY_DOWNLOAD;
 494      }
 495      if (file_mimetype_in_typegroup($mimetype, array('web_image', '.htm', 'web_video', 'web_audio'))) {
 496          return RESOURCELIB_DISPLAY_EMBED;
 497      }
 498  
 499      // let the browser deal with it somehow
 500      return RESOURCELIB_DISPLAY_OPEN;
 501  }
 502  
 503  /**
 504   * File browsing support class
 505   */
 506  class resource_content_file_info extends file_info_stored {
 507      public function get_parent() {
 508          if ($this->lf->get_filepath() === '/' and $this->lf->get_filename() === '.') {
 509              return $this->browser->get_file_info($this->context);
 510          }
 511          return parent::get_parent();
 512      }
 513      public function get_visible_name() {
 514          if ($this->lf->get_filepath() === '/' and $this->lf->get_filename() === '.') {
 515              return $this->topvisiblename;
 516          }
 517          return parent::get_visible_name();
 518      }
 519  }
 520  
 521  function resource_set_mainfile($data) {
 522      global $DB;
 523      $fs = get_file_storage();
 524      $cmid = $data->coursemodule;
 525      $draftitemid = $data->files;
 526  
 527      $context = context_module::instance($cmid);
 528      if ($draftitemid) {
 529          file_save_draft_area_files($draftitemid, $context->id, 'mod_resource', 'content', 0, array('subdirs'=>true));
 530      }
 531      $files = $fs->get_area_files($context->id, 'mod_resource', 'content', 0, 'sortorder', false);
 532      if (count($files) == 1) {
 533          // only one file attached, set it as main file automatically
 534          $file = reset($files);
 535          file_set_sortorder($context->id, 'mod_resource', 'content', 0, $file->get_filepath(), $file->get_filename(), 1);
 536      }
 537  }


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