[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/mod/resource/ -> 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   * @package    mod_resource
  20   * @copyright  2009 Petr Skoda  {@link http://skodak.org}
  21   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22   */
  23  
  24  defined('MOODLE_INTERNAL') || die;
  25  
  26  /**
  27   * List of features supported in Resource module
  28   * @param string $feature FEATURE_xx constant for requested feature
  29   * @return mixed True if module supports feature, false if not, null if doesn't know
  30   */
  31  function resource_supports($feature) {
  32      switch($feature) {
  33          case FEATURE_MOD_ARCHETYPE:           return MOD_ARCHETYPE_RESOURCE;
  34          case FEATURE_GROUPS:                  return false;
  35          case FEATURE_GROUPINGS:               return false;
  36          case FEATURE_MOD_INTRO:               return true;
  37          case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
  38          case FEATURE_GRADE_HAS_GRADE:         return false;
  39          case FEATURE_GRADE_OUTCOMES:          return false;
  40          case FEATURE_BACKUP_MOODLE2:          return true;
  41          case FEATURE_SHOW_DESCRIPTION:        return true;
  42  
  43          default: return null;
  44      }
  45  }
  46  
  47  /**
  48   * Returns all other caps used in module
  49   * @return array
  50   */
  51  function resource_get_extra_capabilities() {
  52      return array('moodle/site:accessallgroups');
  53  }
  54  
  55  /**
  56   * This function is used by the reset_course_userdata function in moodlelib.
  57   * @param $data the data submitted from the reset course.
  58   * @return array status array
  59   */
  60  function resource_reset_userdata($data) {
  61      return array();
  62  }
  63  
  64  /**
  65   * List the actions that correspond to a view of this module.
  66   * This is used by the participation report.
  67   *
  68   * Note: This is not used by new logging system. Event with
  69   *       crud = 'r' and edulevel = LEVEL_PARTICIPATING will
  70   *       be considered as view action.
  71   *
  72   * @return array
  73   */
  74  function resource_get_view_actions() {
  75      return array('view','view all');
  76  }
  77  
  78  /**
  79   * List the actions that correspond to a post of this module.
  80   * This is used by the participation report.
  81   *
  82   * Note: This is not used by new logging system. Event with
  83   *       crud = ('c' || 'u' || 'd') and edulevel = LEVEL_PARTICIPATING
  84   *       will be considered as post action.
  85   *
  86   * @return array
  87   */
  88  function resource_get_post_actions() {
  89      return array('update', 'add');
  90  }
  91  
  92  /**
  93   * Add resource instance.
  94   * @param object $data
  95   * @param object $mform
  96   * @return int new resource instance id
  97   */
  98  function resource_add_instance($data, $mform) {
  99      global $CFG, $DB;
 100      require_once("$CFG->libdir/resourcelib.php");
 101      require_once("$CFG->dirroot/mod/resource/locallib.php");
 102      $cmid = $data->coursemodule;
 103      $data->timemodified = time();
 104  
 105      resource_set_display_options($data);
 106  
 107      $data->id = $DB->insert_record('resource', $data);
 108  
 109      // we need to use context now, so we need to make sure all needed info is already in db
 110      $DB->set_field('course_modules', 'instance', $data->id, array('id'=>$cmid));
 111      resource_set_mainfile($data);
 112      return $data->id;
 113  }
 114  
 115  /**
 116   * Update resource instance.
 117   * @param object $data
 118   * @param object $mform
 119   * @return bool true
 120   */
 121  function resource_update_instance($data, $mform) {
 122      global $CFG, $DB;
 123      require_once("$CFG->libdir/resourcelib.php");
 124      $data->timemodified = time();
 125      $data->id           = $data->instance;
 126      $data->revision++;
 127  
 128      resource_set_display_options($data);
 129  
 130      $DB->update_record('resource', $data);
 131      resource_set_mainfile($data);
 132      return true;
 133  }
 134  
 135  /**
 136   * Updates display options based on form input.
 137   *
 138   * Shared code used by resource_add_instance and resource_update_instance.
 139   *
 140   * @param object $data Data object
 141   */
 142  function resource_set_display_options($data) {
 143      $displayoptions = array();
 144      if ($data->display == RESOURCELIB_DISPLAY_POPUP) {
 145          $displayoptions['popupwidth']  = $data->popupwidth;
 146          $displayoptions['popupheight'] = $data->popupheight;
 147      }
 148      if (in_array($data->display, array(RESOURCELIB_DISPLAY_AUTO, RESOURCELIB_DISPLAY_EMBED, RESOURCELIB_DISPLAY_FRAME))) {
 149          $displayoptions['printintro']   = (int)!empty($data->printintro);
 150      }
 151      if (!empty($data->showsize)) {
 152          $displayoptions['showsize'] = 1;
 153      }
 154      if (!empty($data->showtype)) {
 155          $displayoptions['showtype'] = 1;
 156      }
 157      $data->displayoptions = serialize($displayoptions);
 158  }
 159  
 160  /**
 161   * Delete resource instance.
 162   * @param int $id
 163   * @return bool true
 164   */
 165  function resource_delete_instance($id) {
 166      global $DB;
 167  
 168      if (!$resource = $DB->get_record('resource', array('id'=>$id))) {
 169          return false;
 170      }
 171  
 172      // note: all context files are deleted automatically
 173  
 174      $DB->delete_records('resource', array('id'=>$resource->id));
 175  
 176      return true;
 177  }
 178  
 179  /**
 180   * Given a course_module object, this function returns any
 181   * "extra" information that may be needed when printing
 182   * this activity in a course listing.
 183   *
 184   * See {@link get_array_of_activities()} in course/lib.php
 185   *
 186   * @param stdClass $coursemodule
 187   * @return cached_cm_info info
 188   */
 189  function resource_get_coursemodule_info($coursemodule) {
 190      global $CFG, $DB;
 191      require_once("$CFG->libdir/filelib.php");
 192      require_once("$CFG->dirroot/mod/resource/locallib.php");
 193      require_once($CFG->libdir.'/completionlib.php');
 194  
 195      $context = context_module::instance($coursemodule->id);
 196  
 197      if (!$resource = $DB->get_record('resource', array('id'=>$coursemodule->instance),
 198              'id, name, display, displayoptions, tobemigrated, revision, intro, introformat')) {
 199          return NULL;
 200      }
 201  
 202      $info = new cached_cm_info();
 203      $info->name = $resource->name;
 204      if ($coursemodule->showdescription) {
 205          // Convert intro to html. Do not filter cached version, filters run at display time.
 206          $info->content = format_module_intro('resource', $resource, $coursemodule->id, false);
 207      }
 208  
 209      if ($resource->tobemigrated) {
 210          $info->icon ='i/invalid';
 211          return $info;
 212      }
 213      $fs = get_file_storage();
 214      $files = $fs->get_area_files($context->id, 'mod_resource', 'content', 0, 'sortorder DESC, id ASC', false); // TODO: this is not very efficient!!
 215      if (count($files) >= 1) {
 216          $mainfile = reset($files);
 217          $info->icon = file_file_icon($mainfile, 24);
 218          $resource->mainfile = $mainfile->get_filename();
 219      }
 220  
 221      $display = resource_get_final_display_type($resource);
 222  
 223      if ($display == RESOURCELIB_DISPLAY_POPUP) {
 224          $fullurl = "$CFG->wwwroot/mod/resource/view.php?id=$coursemodule->id&amp;redirect=1";
 225          $options = empty($resource->displayoptions) ? array() : unserialize($resource->displayoptions);
 226          $width  = empty($options['popupwidth'])  ? 620 : $options['popupwidth'];
 227          $height = empty($options['popupheight']) ? 450 : $options['popupheight'];
 228          $wh = "width=$width,height=$height,toolbar=no,location=no,menubar=no,copyhistory=no,status=no,directories=no,scrollbars=yes,resizable=yes";
 229          $info->onclick = "window.open('$fullurl', '', '$wh'); return false;";
 230  
 231      } else if ($display == RESOURCELIB_DISPLAY_NEW) {
 232          $fullurl = "$CFG->wwwroot/mod/resource/view.php?id=$coursemodule->id&amp;redirect=1";
 233          $info->onclick = "window.open('$fullurl'); return false;";
 234  
 235      }
 236  
 237      // If any optional extra details are turned on, store in custom data
 238      $info->customdata = resource_get_optional_details($resource, $coursemodule);
 239  
 240      return $info;
 241  }
 242  
 243  /**
 244   * Called when viewing course page. Shows extra details after the link if
 245   * enabled.
 246   *
 247   * @param cm_info $cm Course module information
 248   */
 249  function resource_cm_info_view(cm_info $cm) {
 250      $details = $cm->customdata;
 251      if ($details) {
 252          $cm->set_after_link(' ' . html_writer::tag('span', $details,
 253                  array('class' => 'resourcelinkdetails')));
 254      }
 255  }
 256  
 257  /**
 258   * Lists all browsable file areas
 259   *
 260   * @package  mod_resource
 261   * @category files
 262   * @param stdClass $course course object
 263   * @param stdClass $cm course module object
 264   * @param stdClass $context context object
 265   * @return array
 266   */
 267  function resource_get_file_areas($course, $cm, $context) {
 268      $areas = array();
 269      $areas['content'] = get_string('resourcecontent', 'resource');
 270      return $areas;
 271  }
 272  
 273  /**
 274   * File browsing support for resource module content area.
 275   *
 276   * @package  mod_resource
 277   * @category files
 278   * @param stdClass $browser file browser instance
 279   * @param stdClass $areas file areas
 280   * @param stdClass $course course object
 281   * @param stdClass $cm course module object
 282   * @param stdClass $context context object
 283   * @param string $filearea file area
 284   * @param int $itemid item ID
 285   * @param string $filepath file path
 286   * @param string $filename file name
 287   * @return file_info instance or null if not found
 288   */
 289  function resource_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
 290      global $CFG;
 291  
 292      if (!has_capability('moodle/course:managefiles', $context)) {
 293          // students can not peak here!
 294          return null;
 295      }
 296  
 297      $fs = get_file_storage();
 298  
 299      if ($filearea === 'content') {
 300          $filepath = is_null($filepath) ? '/' : $filepath;
 301          $filename = is_null($filename) ? '.' : $filename;
 302  
 303          $urlbase = $CFG->wwwroot.'/pluginfile.php';
 304          if (!$storedfile = $fs->get_file($context->id, 'mod_resource', 'content', 0, $filepath, $filename)) {
 305              if ($filepath === '/' and $filename === '.') {
 306                  $storedfile = new virtual_root_file($context->id, 'mod_resource', 'content', 0);
 307              } else {
 308                  // not found
 309                  return null;
 310              }
 311          }
 312          require_once("$CFG->dirroot/mod/resource/locallib.php");
 313          return new resource_content_file_info($browser, $context, $storedfile, $urlbase, $areas[$filearea], true, true, true, false);
 314      }
 315  
 316      // note: resource_intro handled in file_browser automatically
 317  
 318      return null;
 319  }
 320  
 321  /**
 322   * Serves the resource files.
 323   *
 324   * @package  mod_resource
 325   * @category files
 326   * @param stdClass $course course object
 327   * @param stdClass $cm course module object
 328   * @param stdClass $context context object
 329   * @param string $filearea file area
 330   * @param array $args extra arguments
 331   * @param bool $forcedownload whether or not force download
 332   * @param array $options additional options affecting the file serving
 333   * @return bool false if file not found, does not return if found - just send the file
 334   */
 335  function resource_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
 336      global $CFG, $DB;
 337      require_once("$CFG->libdir/resourcelib.php");
 338  
 339      if ($context->contextlevel != CONTEXT_MODULE) {
 340          return false;
 341      }
 342  
 343      require_course_login($course, true, $cm);
 344      if (!has_capability('mod/resource:view', $context)) {
 345          return false;
 346      }
 347  
 348      if ($filearea !== 'content') {
 349          // intro is handled automatically in pluginfile.php
 350          return false;
 351      }
 352  
 353      array_shift($args); // ignore revision - designed to prevent caching problems only
 354  
 355      $fs = get_file_storage();
 356      $relativepath = implode('/', $args);
 357      $fullpath = rtrim("/$context->id/mod_resource/$filearea/0/$relativepath", '/');
 358      do {
 359          if (!$file = $fs->get_file_by_hash(sha1($fullpath))) {
 360              if ($fs->get_file_by_hash(sha1("$fullpath/."))) {
 361                  if ($file = $fs->get_file_by_hash(sha1("$fullpath/index.htm"))) {
 362                      break;
 363                  }
 364                  if ($file = $fs->get_file_by_hash(sha1("$fullpath/index.html"))) {
 365                      break;
 366                  }
 367                  if ($file = $fs->get_file_by_hash(sha1("$fullpath/Default.htm"))) {
 368                      break;
 369                  }
 370              }
 371              $resource = $DB->get_record('resource', array('id'=>$cm->instance), 'id, legacyfiles', MUST_EXIST);
 372              if ($resource->legacyfiles != RESOURCELIB_LEGACYFILES_ACTIVE) {
 373                  return false;
 374              }
 375              if (!$file = resourcelib_try_file_migration('/'.$relativepath, $cm->id, $cm->course, 'mod_resource', 'content', 0)) {
 376                  return false;
 377              }
 378              // file migrate - update flag
 379              $resource->legacyfileslast = time();
 380              $DB->update_record('resource', $resource);
 381          }
 382      } while (false);
 383  
 384      // should we apply filters?
 385      $mimetype = $file->get_mimetype();
 386      if ($mimetype === 'text/html' or $mimetype === 'text/plain') {
 387          $filter = $DB->get_field('resource', 'filterfiles', array('id'=>$cm->instance));
 388          $CFG->embeddedsoforcelinktarget = true;
 389      } else {
 390          $filter = 0;
 391      }
 392  
 393      // finally send the file
 394      send_stored_file($file, null, $filter, $forcedownload, $options);
 395  }
 396  
 397  /**
 398   * Return a list of page types
 399   * @param string $pagetype current page type
 400   * @param stdClass $parentcontext Block's parent context
 401   * @param stdClass $currentcontext Current context of block
 402   */
 403  function resource_page_type_list($pagetype, $parentcontext, $currentcontext) {
 404      $module_pagetype = array('mod-resource-*'=>get_string('page-mod-resource-x', 'resource'));
 405      return $module_pagetype;
 406  }
 407  
 408  /**
 409   * Export file resource contents
 410   *
 411   * @return array of file content
 412   */
 413  function resource_export_contents($cm, $baseurl) {
 414      global $CFG, $DB;
 415      $contents = array();
 416      $context = context_module::instance($cm->id);
 417      $resource = $DB->get_record('resource', array('id'=>$cm->instance), '*', MUST_EXIST);
 418  
 419      $fs = get_file_storage();
 420      $files = $fs->get_area_files($context->id, 'mod_resource', 'content', 0, 'sortorder DESC, id ASC', false);
 421  
 422      foreach ($files as $fileinfo) {
 423          $file = array();
 424          $file['type'] = 'file';
 425          $file['filename']     = $fileinfo->get_filename();
 426          $file['filepath']     = $fileinfo->get_filepath();
 427          $file['filesize']     = $fileinfo->get_filesize();
 428          $file['fileurl']      = file_encode_url("$CFG->wwwroot/" . $baseurl, '/'.$context->id.'/mod_resource/content/'.$resource->revision.$fileinfo->get_filepath().$fileinfo->get_filename(), true);
 429          $file['timecreated']  = $fileinfo->get_timecreated();
 430          $file['timemodified'] = $fileinfo->get_timemodified();
 431          $file['sortorder']    = $fileinfo->get_sortorder();
 432          $file['userid']       = $fileinfo->get_userid();
 433          $file['author']       = $fileinfo->get_author();
 434          $file['license']      = $fileinfo->get_license();
 435          $contents[] = $file;
 436      }
 437  
 438      return $contents;
 439  }
 440  
 441  /**
 442   * Register the ability to handle drag and drop file uploads
 443   * @return array containing details of the files / types the mod can handle
 444   */
 445  function resource_dndupload_register() {
 446      return array('files' => array(
 447                       array('extension' => '*', 'message' => get_string('dnduploadresource', 'mod_resource'))
 448                   ));
 449  }
 450  
 451  /**
 452   * Handle a file that has been uploaded
 453   * @param object $uploadinfo details of the file / content that has been uploaded
 454   * @return int instance id of the newly created mod
 455   */
 456  function resource_dndupload_handle($uploadinfo) {
 457      // Gather the required info.
 458      $data = new stdClass();
 459      $data->course = $uploadinfo->course->id;
 460      $data->name = $uploadinfo->displayname;
 461      $data->intro = '';
 462      $data->introformat = FORMAT_HTML;
 463      $data->coursemodule = $uploadinfo->coursemodule;
 464      $data->files = $uploadinfo->draftitemid;
 465  
 466      // Set the display options to the site defaults.
 467      $config = get_config('resource');
 468      $data->display = $config->display;
 469      $data->popupheight = $config->popupheight;
 470      $data->popupwidth = $config->popupwidth;
 471      $data->printintro = $config->printintro;
 472      $data->showsize = (isset($config->showsize)) ? $config->showsize : 0;
 473      $data->showtype = (isset($config->showtype)) ? $config->showtype : 0;
 474      $data->filterfiles = $config->filterfiles;
 475  
 476      return resource_add_instance($data, null);
 477  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1