📄 resource.class.php
字号:
<?php // $Id: resource.class.php,v 1.47.2.5 2008/07/10 09:48:47 scyrma Exp $///////////////////////////////////////////////////////////////////////////// //// NOTICE OF COPYRIGHT //// //// Moodle - Modular Object-Oriented Dynamic Learning Environment //// http://moodle.com //// //// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //// (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com //// //// This program is free software; you can redistribute it and/or modify //// it under the terms of the GNU General Public License as published by //// the Free Software Foundation; either version 2 of the License, or //// (at your option) any later version. //// //// This program is distributed in the hope that it will be useful, //// but WITHOUT ANY WARRANTY; without even the implied warranty of //// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //// GNU General Public License for more details: //// //// http://www.gnu.org/copyleft/gpl.html //// //////////////////////////////////////////////////////////////////////////////// Options presented to user :/// (1) Side navigation menu (navigationmenu)/// (2) TOC (tableofcontents)/// (3) Navigation buttons (navigationbuttons)/// (4) Navigation up button (navigationupbutton)/// (5) Skip submenu pages (skipsubmenus)////// (1) forces (2), (4) false and (5) true. Forced on setup/// (2) is a bit silly with (5). Maybe make a rule?/// (3) false => (5) false. Add graying out on setup.require_once($CFG->libdir.'/filelib.php');require_once($CFG->dirroot.'/mod/resource/type/ims/repository_config.php');/*** Extend the base resource class for ims resources*/class resource_ims extends resource_base { var $parameters; //Attribute of this class where we'll store all the IMS deploy preferences function resource_ims($cmid=0) { /// super constructor parent::resource_base($cmid); /// prevent notice if (empty($this->resource->alltext)) { $this->resource->alltext=''; } /// set own attributes $this->parameters = $this->alltext2parameters($this->resource->alltext); /// navigation menu forces other settings if ($this->parameters->navigationmenu) { $this->parameters->tableofcontents = 0; $this->parameters->navigationuparrow = 0; $this->parameters->skipsubmenus = 1; } /// Is it in the repository material or not? if (isset($this->resource->reference)) { $file = $this->resource->reference; if ($file[0] == '#') { $this->isrepository = true; $file = ltrim($file, '#'); $this->resource->reference = $file; } else { $this->isrepository = false; } } else { $this->isrepository = false; } } /*** * This function converts parameters stored in the alltext field to the proper * this->parameters object storing the special configuration of this resource type */ function alltext2parameters($alltext) { /// set parameter defaults $alltextfield = new stdClass(); $alltextfield->tableofcontents=0; $alltextfield->navigationbuttons=0; $alltextfield->navigationmenu=1; $alltextfield->skipsubmenus=1; $alltextfield->navigationupbutton=1; /// load up any stored parameters if (!empty($alltext)) { $parray = explode(',', $alltext); foreach ($parray as $key => $fieldstring) { $field = explode('=', $fieldstring); $alltextfield->$field[0] = $field[1]; } } return $alltextfield; } /*** * This function converts the this->parameters attribute (object) to the format * needed to save them in the alltext field to store all the special configuration * of this resource type */ function parameters2alltext($parameters) { $optionlist = array(); $optionlist[] = 'tableofcontents='.$parameters->tableofcontents; $optionlist[] = 'navigationbuttons='.$parameters->navigationbuttons; $optionlist[] = 'skipsubmenus='.$parameters->skipsubmenus; $optionlist[] = 'navigationmenu='.$parameters->navigationmenu; $optionlist[] = 'navigationupbutton='.$parameters->navigationupbutton; return implode(',', $optionlist); } /*** * This function will convert all the parameters configured in the resource form * to a this->parameter attribute (object) */ function form2parameters($resource) { $parameters = new stdClass; $parameters->tableofcontents = isset($resource->param_tableofcontents) ? $resource->param_tableofcontents : 0; $parameters->navigationbuttons = $resource->param_navigationbuttons; $parameters->skipsubmenus = isset($resource->param_skipsubmenus) ? $resource->param_skipsubmenus : 0; $parameters->navigationmenu = $resource->param_navigationmenu; $parameters->navigationupbutton = isset($resource->param_navigationupbutton) ? $resource->param_navigationupbutton : 0; return $parameters; } /*** This function checks for errors in the status or deployment of the IMS * Content Package returning an error code: * 1 = Not a .zip file. * 2 = Zip file doesn't exist * 3 = Package not deployed. * 4 = Package has changed since deployed. * If the IMS CP is one from the central repository, then we instead check * with the following codes: * 5 = Not deployed. Since repository is central must be admin to deploy so terminate */ function check4errors($file, $course, $resource) { global $CFG; if ($this->isrepository) { /// Calculate the path were the IMS package must be deployed $deploydir = $CFG->repository . $file; /// Confirm that the IMS package has been deployed. These files must exist if /// the package is deployed: moodle_index.ser and moodle_hash.ser if (!file_exists($deploydir.'/moodle_inx.ser')) { return 5; //Error } } else { /// Check for zip file type $mimetype = mimeinfo("type", $file); if ($mimetype != "application/zip") { return 1; //Error } /// Check if the uploaded file exists if (!file_exists($CFG->dataroot.'/'.$course->id.'/'.$file)) { return 2; //Error } /// Calculate the path were the IMS package must be deployed $deploydir = $CFG->dataroot.'/'.$course->id.'/'.$CFG->moddata.'/resource/'.$resource->id; /// Confirm that the IMS package has been deployed. These files must exist if /// the package is deployed: moodle_index.ser and moodle_hash.ser if (!file_exists($deploydir.'/moodle_inx.ser') || !file_exists($deploydir.'/moodle_hash.ser')) { return 3; //Error } /// If teacheredit, make, hash check. It's the md5 of the name of the file /// plus its size and modification date /// not sure if this capability is suitable if (has_capability('moodle/course:manageactivities', get_context_instance(CONTEXT_COURSE, $course->id))) { if (!$this->checkpackagehash($file, $course, $resource)) { return 4; } } } /// We've arrived here. Everything is ok return 0; } /*** This function will check that the ims package (zip file) uploaded * isn't changed since it was deployed. */ function checkpackagehash($file, $course, $resource) { global $CFG; /// Calculate paths $zipfile = $CFG->dataroot.'/'.$course->id.'/'.$file; $hashfile= $CFG->dataroot.'/'.$course->id.'/'.$CFG->moddata.'/resource/'.$resource->id.'/moodle_hash.ser'; /// Get deloyed hash value $f = fopen ($hashfile,'r'); $deployedhash = fread($f, filesize($hashfile)); fclose ($f); /// Unserialize the deployed hash $deployedhash = unserialize($deployedhash); /// Calculate uploaded file hash value $uploadedhash = $this->calculatefilehash($zipfile); /// Compare them return ($deployedhash == $uploadedhash); } /*** This function will calculate the hash of any file passes as argument. * It's based in a md5 of the filename, filesize and 20 first bytes (it includes * the zip CRC at byte 15). */ function calculatefilehash($filefullpath) { /// Name and size $filename = basename($filefullpath); $filesize = filesize($filefullpath); /// Read first 20cc $f = fopen ($filefullpath,'r'); $data = fread($f, 20); fclose ($f); return md5($filename.'-'.$filesize.'-'.$data); } /** * Add new instance of file resource * * Create alltext field before calling base class function. * * @param resource object */ function add_instance($resource) { $this->_postprocess($resource); return parent::add_instance($resource); } /** * Update instance of file resource * * Create alltext field before calling base class function. * * @param resource object */ function update_instance($resource) { $this->_postprocess($resource); return parent::update_instance($resource); } function _postprocess(&$resource) { global $RESOURCE_WINDOW_OPTIONS; $alloptions = $RESOURCE_WINDOW_OPTIONS; if ($resource->windowpopup) { $optionlist = array(); foreach ($alloptions as $option) { $optionlist[] = $option."=".$resource->$option; unset($resource->$option); } $resource->popup = implode(',', $optionlist); unset($resource->windowpopup); } else { $resource->popup = ''; } /// Load parameters to this->parameters $this->parameters = $this->form2parameters($resource); /// Save parameters into the alltext field $resource->alltext = $this->parameters2alltext($this->parameters); } /** Delete instance of IMS-CP resource * * Delete all the moddata files for the resource * @param resource object */ function delete_instance($resource) { global $CFG; /// Delete moddata resource dir completely unless repository. if (!$this->isrepository) { $resource_dir = $CFG->dataroot.'/'.$resource->course.'/'.$CFG->moddata.'/resource/'.$resource->id; if (file_exists($resource_dir)) { if (!$status = fulldelete($resource_dir)) { return false; } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -