Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/imscp/lib.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00027 defined('MOODLE_INTERNAL') || die();
00028 
00034 function imscp_supports($feature) {
00035     switch($feature) {
00036         case FEATURE_MOD_ARCHETYPE:           return MOD_ARCHETYPE_RESOURCE;
00037         case FEATURE_GROUPS:                  return false;
00038         case FEATURE_GROUPINGS:               return false;
00039         case FEATURE_GROUPMEMBERSONLY:        return true;
00040         case FEATURE_MOD_INTRO:               return true;
00041         case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
00042         case FEATURE_GRADE_HAS_GRADE:         return false;
00043         case FEATURE_GRADE_OUTCOMES:          return false;
00044         case FEATURE_BACKUP_MOODLE2:          return true;
00045         case FEATURE_SHOW_DESCRIPTION:        return true;
00046 
00047         default: return null;
00048     }
00049 }
00050 
00055 function imscp_get_extra_capabilities() {
00056     return array('moodle/site:accessallgroups');
00057 }
00058 
00064 function imscp_reset_userdata($data) {
00065     return array();
00066 }
00067 
00072 function imscp_get_view_actions() {
00073     return array('view', 'view all');
00074 }
00075 
00080 function imscp_get_post_actions() {
00081     return array('update', 'add');
00082 }
00083 
00090 function imscp_add_instance($data, $mform) {
00091     global $CFG, $DB;
00092     require_once("$CFG->dirroot/mod/imscp/locallib.php");
00093 
00094     $cmid = $data->coursemodule;
00095 
00096     $data->timemodified = time();
00097     $data->revision     = 1;
00098     $data->structure    = null;
00099 
00100     $data->id = $DB->insert_record('imscp', $data);
00101 
00102     // we need to use context now, so we need to make sure all needed info is already in db
00103     $DB->set_field('course_modules', 'instance', $data->id, array('id'=>$cmid));
00104     $context = get_context_instance(CONTEXT_MODULE, $cmid);
00105     $imscp = $DB->get_record('imscp', array('id'=>$data->id), '*', MUST_EXIST);
00106 
00107     if ($filename = $mform->get_new_filename('package')) {
00108         if ($package = $mform->save_stored_file('package', $context->id, 'mod_imscp', 'backup', 1, '/', $filename)) {
00109             // extract package content
00110             $packer = get_file_packer('application/zip');
00111             $package->extract_to_storage($packer, $context->id, 'mod_imscp', 'content', 1, '/');
00112             $structure = imscp_parse_structure($imscp, $context);
00113             $imscp->structure = is_array($structure) ? serialize($structure) : null;
00114             $DB->update_record('imscp', $imscp);
00115         }
00116     }
00117 
00118     return $data->id;
00119 }
00120 
00127 function imscp_update_instance($data, $mform) {
00128     global $CFG, $DB;
00129     require_once("$CFG->dirroot/mod/imscp/locallib.php");
00130 
00131     $cmid = $data->coursemodule;
00132 
00133     $data->timemodified = time();
00134     $data->id           = $data->instance;
00135     $data->structure   = null; // better reparse structure after each update
00136 
00137     $DB->update_record('imscp', $data);
00138 
00139     $context = get_context_instance(CONTEXT_MODULE, $cmid);
00140     $imscp = $DB->get_record('imscp', array('id'=>$data->id), '*', MUST_EXIST);
00141 
00142     if ($filename = $mform->get_new_filename('package')) {
00143         $fs = get_file_storage();
00144 
00145         $imscp->revision++;
00146         $DB->update_record('imscp', $imscp);
00147 
00148         // get a list of existing packages before adding new package
00149         if ($imscp->keepold > -1) {
00150             $packages = $fs->get_area_files($context->id, 'mod_imscp', 'backup', false, "itemid ASC", false);
00151         } else {
00152             $packages = array();
00153         }
00154 
00155         $package = $mform->save_stored_file('package', $context->id, 'mod_imscp', 'backup', $imscp->revision, '/', $filename);
00156 
00157         // purge all extracted content
00158         $fs->delete_area_files($context->id, 'mod_imscp', 'content');
00159 
00160         // extract package content
00161         if ($package) {
00162             $packer = get_file_packer('application/zip');
00163             $package->extract_to_storage($packer, $context->id, 'mod_imscp', 'content', $imscp->revision, '/');
00164         }
00165 
00166         // cleanup old package files, keep current + keepold
00167         while ($packages and (count($packages) > $imscp->keepold)) {
00168             $package = array_shift($packages);
00169             $fs->delete_area_files($context->id, 'mod_imscp', 'backup', $package->get_itemid());
00170         }
00171     }
00172 
00173     $structure = imscp_parse_structure($imscp, $context);
00174     $imscp->structure = is_array($structure) ? serialize($structure) : null;
00175     $DB->update_record('imscp', $imscp);
00176 
00177     return true;
00178 }
00179 
00185 function imscp_delete_instance($id) {
00186     global $DB;
00187 
00188     if (!$imscp = $DB->get_record('imscp', array('id'=>$id))) {
00189         return false;
00190     }
00191 
00192     // note: all context files are deleted automatically
00193 
00194     $DB->delete_records('imscp', array('id'=>$imscp->id));
00195 
00196     return true;
00197 }
00198 
00207 function imscp_user_outline($course, $user, $mod, $imscp) {
00208     global $DB;
00209 
00210     if ($logs = $DB->get_records('log', array('userid'=>$user->id, 'module'=>'imscp',
00211                                               'action'=>'view', 'info'=>$imscp->id), 'time ASC')) {
00212 
00213         $numviews = count($logs);
00214         $lastlog = array_pop($logs);
00215 
00216         $result = new stdClass();
00217         $result->info = get_string('numviews', '', $numviews);
00218         $result->time = $lastlog->time;
00219 
00220         return $result;
00221     }
00222     return NULL;
00223 }
00224 
00232 function imscp_user_complete($course, $user, $mod, $imscp) {
00233     global $CFG, $DB;
00234 
00235     if ($logs = $DB->get_records('log', array('userid'=>$user->id, 'module'=>'imscp',
00236                                               'action'=>'view', 'info'=>$imscp->id), 'time ASC')) {
00237         $numviews = count($logs);
00238         $lastlog = array_pop($logs);
00239 
00240         $strmostrecently = get_string('mostrecently');
00241         $strnumviews = get_string('numviews', '', $numviews);
00242 
00243         echo "$strnumviews - $strmostrecently ".userdate($lastlog->time);
00244 
00245     } else {
00246         print_string('neverseen', 'imscp');
00247     }
00248 }
00249 
00258 function imscp_get_participants($imscpid) {
00259     return false;
00260 }
00261 
00269 function imscp_get_file_areas($course, $cm, $context) {
00270     $areas = array();
00271 
00272     $areas['content'] = get_string('areacontent', 'imscp');
00273     $areas['backup']  = get_string('areabackup', 'imscp');
00274 
00275     return $areas;
00276 }
00277 
00291 function imscp_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
00292     global $CFG, $DB;
00293 
00294     // note: imscp_intro handled in file_browser automatically
00295 
00296     if (!has_capability('moodle/course:managefiles', $context)) {
00297         // no peaking here for students!!
00298         return null;
00299     }
00300 
00301     if ($filearea !== 'content' and $filearea !== 'backup') {
00302         return null;
00303     }
00304 
00305     require_once("$CFG->dirroot/mod/imscp/locallib.php");
00306 
00307     if (is_null($itemid)) {
00308         return new imscp_file_info($browser, $course, $cm, $context, $areas, $filearea, $itemid);
00309     }
00310 
00311     $fs = get_file_storage();
00312     $filepath = is_null($filepath) ? '/' : $filepath;
00313     $filename = is_null($filename) ? '.' : $filename;
00314     if (!$storedfile = $fs->get_file($context->id, 'mod_imscp', $filearea, $itemid, $filepath, $filename)) {
00315         return null;
00316     }
00317 
00318     // do not allow manual modification of any files!
00319     $urlbase = $CFG->wwwroot.'/pluginfile.php';
00320     return new file_info_stored($browser, $context, $storedfile, $urlbase, $itemid, true, true, false, false); //no writing here!
00321 }
00322 
00334 function imscp_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) {
00335     global $CFG, $DB;
00336 
00337     if ($context->contextlevel != CONTEXT_MODULE) {
00338         return false;
00339     }
00340 
00341     require_login($course, true, $cm);
00342 
00343     if ($filearea === 'content') {
00344         if (!has_capability('mod/imscp:view', $context)) {
00345             return false;
00346         }
00347         $revision = array_shift($args);
00348         $fs = get_file_storage();
00349         $relativepath = implode('/', $args);
00350         if ($relativepath === 'imsmanifest.xml') {
00351             if (!has_capability('moodle/course:managefiles', $context)) {
00352                 // no stealing of detailed package info ;-)
00353                 return false;
00354             }
00355         }
00356         $fullpath = "/$context->id/mod_imscp/$filearea/$revision/$relativepath";
00357         if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
00358             return false;
00359         }
00360 
00361         // finally send the file
00362         send_stored_file($file, 86400, 0, $forcedownload);
00363 
00364     } else if ($filearea === 'backup') {
00365         if (!has_capability('moodle/course:managefiles', $context)) {
00366             // no stealing of package backups
00367             return false;
00368         }
00369         $revision = array_shift($args);
00370         $fs = get_file_storage();
00371         $relativepath = implode('/', $args);
00372         $fullpath = "/$context->id/mod_imscp/$filearea/$revision/$relativepath";
00373         if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
00374             return false;
00375         }
00376 
00377         // finally send the file
00378         send_stored_file($file, 86400, 0, $forcedownload);
00379 
00380     } else {
00381         return false;
00382     }
00383 }
00384 
00396 function imscp_extend_navigation($navigation, $course, $module, $cm) {
00402     $navigation->nodetype = navigation_node::NODETYPE_LEAF;
00403 }
00404 
00411 function imscp_page_type_list($pagetype, $parentcontext, $currentcontext) {
00412     $module_pagetype = array('mod-imscp-*'=>get_string('page-mod-imscp-x', 'imscp'));
00413     return $module_pagetype;
00414 }
 All Data Structures Namespaces Files Functions Variables Enumerations