[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/mod/imscp/ -> 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   * Mandatory public API of imscp module
  20   *
  21   * @package mod_imscp
  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  /**
  29   * List of features supported in IMS CP module
  30   * @param string $feature FEATURE_xx constant for requested feature
  31   * @return mixed True if module supports feature, false if not, null if doesn't know
  32   */
  33  function imscp_supports($feature) {
  34      switch($feature) {
  35          case FEATURE_MOD_ARCHETYPE:           return MOD_ARCHETYPE_RESOURCE;
  36          case FEATURE_GROUPS:                  return false;
  37          case FEATURE_GROUPINGS:               return false;
  38          case FEATURE_MOD_INTRO:               return true;
  39          case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
  40          case FEATURE_GRADE_HAS_GRADE:         return false;
  41          case FEATURE_GRADE_OUTCOMES:          return false;
  42          case FEATURE_BACKUP_MOODLE2:          return true;
  43          case FEATURE_SHOW_DESCRIPTION:        return true;
  44  
  45          default: return null;
  46      }
  47  }
  48  
  49  /**
  50   * Returns all other caps used in module
  51   * @return array
  52   */
  53  function imscp_get_extra_capabilities() {
  54      return array('moodle/site:accessallgroups');
  55  }
  56  
  57  /**
  58   * This function is used by the reset_course_userdata function in moodlelib.
  59   * @param $data the data submitted from the reset course.
  60   * @return array status array
  61   */
  62  function imscp_reset_userdata($data) {
  63      return array();
  64  }
  65  
  66  /**
  67   * List the actions that correspond to a view of this module.
  68   * This is used by the participation report.
  69   *
  70   * Note: This is not used by new logging system. Event with
  71   *       crud = 'r' and edulevel = LEVEL_PARTICIPATING will
  72   *       be considered as view action.
  73   *
  74   * @return array
  75   */
  76  function imscp_get_view_actions() {
  77      return array('view', 'view all');
  78  }
  79  
  80  /**
  81   * List the actions that correspond to a post of this module.
  82   * This is used by the participation report.
  83   *
  84   * Note: This is not used by new logging system. Event with
  85   *       crud = ('c' || 'u' || 'd') and edulevel = LEVEL_PARTICIPATING
  86   *       will be considered as post action.
  87   *
  88   * @return array
  89   */
  90  function imscp_get_post_actions() {
  91      return array('update', 'add');
  92  }
  93  
  94  /**
  95   * Add imscp instance.
  96   * @param object $data
  97   * @param object $mform
  98   * @return int new imscp instance id
  99   */
 100  function imscp_add_instance($data, $mform) {
 101      global $CFG, $DB;
 102      require_once("$CFG->dirroot/mod/imscp/locallib.php");
 103  
 104      $cmid = $data->coursemodule;
 105  
 106      $data->timemodified = time();
 107      $data->revision     = 1;
 108      $data->structure    = null;
 109  
 110      $data->id = $DB->insert_record('imscp', $data);
 111  
 112      // we need to use context now, so we need to make sure all needed info is already in db
 113      $DB->set_field('course_modules', 'instance', $data->id, array('id'=>$cmid));
 114      $context = context_module::instance($cmid);
 115      $imscp = $DB->get_record('imscp', array('id'=>$data->id), '*', MUST_EXIST);
 116  
 117      if (!empty($data->package)) {
 118          // Save uploaded files to 'backup' filearea.
 119          $fs = get_file_storage();
 120          $fs->delete_area_files($context->id, 'mod_imscp', 'backup', 1);
 121          file_save_draft_area_files($data->package, $context->id, 'mod_imscp', 'backup',
 122              1, array('subdirs' => 0, 'maxfiles' => 1));
 123          // Get filename of zip that was uploaded.
 124          $files = $fs->get_area_files($context->id, 'mod_imscp', 'backup', 1, '', false);
 125          if ($files) {
 126              // Extract package content to 'content' filearea.
 127              $package = reset($files);
 128              $packer = get_file_packer('application/zip');
 129              $package->extract_to_storage($packer, $context->id, 'mod_imscp', 'content', 1, '/');
 130              $structure = imscp_parse_structure($imscp, $context);
 131              $imscp->structure = is_array($structure) ? serialize($structure) : null;
 132              $DB->update_record('imscp', $imscp);
 133          }
 134      }
 135  
 136      return $data->id;
 137  }
 138  
 139  /**
 140   * Update imscp instance.
 141   * @param object $data
 142   * @param object $mform
 143   * @return bool true
 144   */
 145  function imscp_update_instance($data, $mform) {
 146      global $CFG, $DB;
 147      require_once("$CFG->dirroot/mod/imscp/locallib.php");
 148  
 149      $cmid = $data->coursemodule;
 150  
 151      $data->timemodified = time();
 152      $data->id           = $data->instance;
 153      $data->structure   = null; // better reparse structure after each update
 154  
 155      $DB->update_record('imscp', $data);
 156  
 157      $context = context_module::instance($cmid);
 158      $imscp = $DB->get_record('imscp', array('id'=>$data->id), '*', MUST_EXIST);
 159  
 160      if (!empty($data->package) && ($draftareainfo = file_get_draft_area_info($data->package)) &&
 161              $draftareainfo['filecount']) {
 162          $fs = get_file_storage();
 163  
 164          $imscp->revision++;
 165          $DB->update_record('imscp', $imscp);
 166  
 167          // get a list of existing packages before adding new package
 168          if ($imscp->keepold > -1) {
 169              $packages = $fs->get_area_files($context->id, 'mod_imscp', 'backup', false, "itemid ASC", false);
 170          } else {
 171              $packages = array();
 172          }
 173  
 174          file_save_draft_area_files($data->package, $context->id, 'mod_imscp', 'backup',
 175              $imscp->revision, array('subdirs' => 0, 'maxfiles' => 1));
 176          $files = $fs->get_area_files($context->id, 'mod_imscp', 'backup', $imscp->revision, '', false);
 177          $package = reset($files);
 178  
 179          // purge all extracted content
 180          $fs->delete_area_files($context->id, 'mod_imscp', 'content');
 181  
 182          // extract package content
 183          if ($package) {
 184              $packer = get_file_packer('application/zip');
 185              $package->extract_to_storage($packer, $context->id, 'mod_imscp', 'content', $imscp->revision, '/');
 186          }
 187  
 188          // cleanup old package files, keep current + keepold
 189          while ($packages and (count($packages) > $imscp->keepold)) {
 190              $package = array_shift($packages);
 191              $fs->delete_area_files($context->id, 'mod_imscp', 'backup', $package->get_itemid());
 192          }
 193      }
 194  
 195      $structure = imscp_parse_structure($imscp, $context);
 196      $imscp->structure = is_array($structure) ? serialize($structure) : null;
 197      $DB->update_record('imscp', $imscp);
 198  
 199      return true;
 200  }
 201  
 202  /**
 203   * Delete imscp instance.
 204   * @param int $id
 205   * @return bool true
 206   */
 207  function imscp_delete_instance($id) {
 208      global $DB;
 209  
 210      if (!$imscp = $DB->get_record('imscp', array('id'=>$id))) {
 211          return false;
 212      }
 213  
 214      // note: all context files are deleted automatically
 215  
 216      $DB->delete_records('imscp', array('id'=>$imscp->id));
 217  
 218      return true;
 219  }
 220  
 221  /**
 222   * Lists all browsable file areas
 223   *
 224   * @package  mod_imscp
 225   * @category files
 226   * @param stdClass $course course object
 227   * @param stdClass $cm course module object
 228   * @param stdClass $context context object
 229   * @return array
 230   */
 231  function imscp_get_file_areas($course, $cm, $context) {
 232      $areas = array();
 233  
 234      $areas['content'] = get_string('areacontent', 'imscp');
 235      $areas['backup']  = get_string('areabackup', 'imscp');
 236  
 237      return $areas;
 238  }
 239  
 240  /**
 241   * File browsing support for imscp module ontent area.
 242   *
 243   * @package  mod_imscp
 244   * @category files
 245   * @param stdClass $browser file browser
 246   * @param stdClass $areas file areas
 247   * @param stdClass $course course object
 248   * @param stdClass $cm course module object
 249   * @param stdClass $context context object
 250   * @param string $filearea file area
 251   * @param int $itemid item ID
 252   * @param string $filepath file path
 253   * @param string $filename file name
 254   * @return file_info instance or null if not found
 255   */
 256  function imscp_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
 257      global $CFG, $DB;
 258  
 259      // note: imscp_intro handled in file_browser automatically
 260  
 261      if (!has_capability('moodle/course:managefiles', $context)) {
 262          // no peaking here for students!!
 263          return null;
 264      }
 265  
 266      if ($filearea !== 'content' and $filearea !== 'backup') {
 267          return null;
 268      }
 269  
 270      require_once("$CFG->dirroot/mod/imscp/locallib.php");
 271  
 272      if (is_null($itemid)) {
 273          return new imscp_file_info($browser, $course, $cm, $context, $areas, $filearea, $itemid);
 274      }
 275  
 276      $fs = get_file_storage();
 277      $filepath = is_null($filepath) ? '/' : $filepath;
 278      $filename = is_null($filename) ? '.' : $filename;
 279      if (!$storedfile = $fs->get_file($context->id, 'mod_imscp', $filearea, $itemid, $filepath, $filename)) {
 280          return null;
 281      }
 282  
 283      // do not allow manual modification of any files!
 284      $urlbase = $CFG->wwwroot.'/pluginfile.php';
 285      return new file_info_stored($browser, $context, $storedfile, $urlbase, $itemid, true, true, false, false); //no writing here!
 286  }
 287  
 288  /**
 289   * Serves the imscp files.
 290   *
 291   * @package  mod_imscp
 292   * @category files
 293   * @param stdClass $course course object
 294   * @param stdClass $cm course module object
 295   * @param stdClass $context context object
 296   * @param string $filearea file area
 297   * @param array $args extra arguments
 298   * @param bool $forcedownload whether or not force download
 299   * @param array $options additional options affecting the file serving
 300   * @return bool false if file not found, does not return if found - justsend the file
 301   */
 302  function imscp_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
 303      global $CFG, $DB;
 304  
 305      if ($context->contextlevel != CONTEXT_MODULE) {
 306          return false;
 307      }
 308  
 309      require_login($course, true, $cm);
 310  
 311      if ($filearea === 'content') {
 312          if (!has_capability('mod/imscp:view', $context)) {
 313              return false;
 314          }
 315          $revision = array_shift($args);
 316          $fs = get_file_storage();
 317          $relativepath = implode('/', $args);
 318          if ($relativepath === 'imsmanifest.xml') {
 319              if (!has_capability('moodle/course:managefiles', $context)) {
 320                  // no stealing of detailed package info ;-)
 321                  return false;
 322              }
 323          }
 324          $fullpath = "/$context->id/mod_imscp/$filearea/$revision/$relativepath";
 325          if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
 326              return false;
 327          }
 328  
 329          // finally send the file
 330          send_stored_file($file, null, 0, $forcedownload, $options);
 331  
 332      } else if ($filearea === 'backup') {
 333          if (!has_capability('moodle/course:managefiles', $context)) {
 334              // no stealing of package backups
 335              return false;
 336          }
 337          $revision = array_shift($args);
 338          $fs = get_file_storage();
 339          $relativepath = implode('/', $args);
 340          $fullpath = "/$context->id/mod_imscp/$filearea/$revision/$relativepath";
 341          if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
 342              return false;
 343          }
 344  
 345          // finally send the file
 346          send_stored_file($file, null, 0, $forcedownload, $options);
 347  
 348      } else {
 349          return false;
 350      }
 351  }
 352  
 353  /**
 354   * Return a list of page types
 355   * @param string $pagetype current page type
 356   * @param stdClass $parentcontext Block's parent context
 357   * @param stdClass $currentcontext Current context of block
 358   */
 359  function imscp_page_type_list($pagetype, $parentcontext, $currentcontext) {
 360      $module_pagetype = array('mod-imscp-*'=>get_string('page-mod-imscp-x', 'imscp'));
 361      return $module_pagetype;
 362  }


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