Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/resource/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 
00025 defined('MOODLE_INTERNAL') || die;
00026 
00032 function resource_supports($feature) {
00033     switch($feature) {
00034         case FEATURE_MOD_ARCHETYPE:           return MOD_ARCHETYPE_RESOURCE;
00035         case FEATURE_GROUPS:                  return false;
00036         case FEATURE_GROUPINGS:               return false;
00037         case FEATURE_GROUPMEMBERSONLY:        return true;
00038         case FEATURE_MOD_INTRO:               return true;
00039         case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
00040         case FEATURE_GRADE_HAS_GRADE:         return false;
00041         case FEATURE_GRADE_OUTCOMES:          return false;
00042         case FEATURE_BACKUP_MOODLE2:          return true;
00043         case FEATURE_SHOW_DESCRIPTION:        return true;
00044 
00045         default: return null;
00046     }
00047 }
00048 
00053 function resource_get_extra_capabilities() {
00054     return array('moodle/site:accessallgroups');
00055 }
00056 
00062 function resource_reset_userdata($data) {
00063     return array();
00064 }
00065 
00070 function resource_get_view_actions() {
00071     return array('view','view all');
00072 }
00073 
00078 function resource_get_post_actions() {
00079     return array('update', 'add');
00080 }
00081 
00088 function resource_add_instance($data, $mform) {
00089     global $CFG, $DB;
00090     require_once("$CFG->libdir/resourcelib.php");
00091     $cmid = $data->coursemodule;
00092     $data->timemodified = time();
00093     $displayoptions = array();
00094     if ($data->display == RESOURCELIB_DISPLAY_POPUP) {
00095         $displayoptions['popupwidth']  = $data->popupwidth;
00096         $displayoptions['popupheight'] = $data->popupheight;
00097     }
00098     if (in_array($data->display, array(RESOURCELIB_DISPLAY_AUTO, RESOURCELIB_DISPLAY_EMBED, RESOURCELIB_DISPLAY_FRAME))) {
00099         $displayoptions['printheading'] = (int)!empty($data->printheading);
00100         $displayoptions['printintro']   = (int)!empty($data->printintro);
00101     }
00102     $data->displayoptions = serialize($displayoptions);
00103 
00104     $data->id = $DB->insert_record('resource', $data);
00105 
00106     // we need to use context now, so we need to make sure all needed info is already in db
00107     $DB->set_field('course_modules', 'instance', $data->id, array('id'=>$cmid));
00108     resource_set_mainfile($data);
00109     return $data->id;
00110 }
00111 
00118 function resource_update_instance($data, $mform) {
00119     global $CFG, $DB;
00120     require_once("$CFG->libdir/resourcelib.php");
00121     $data->timemodified = time();
00122     $data->id           = $data->instance;
00123     $data->revision++;
00124 
00125     $displayoptions = array();
00126     if ($data->display == RESOURCELIB_DISPLAY_POPUP) {
00127         $displayoptions['popupwidth']  = $data->popupwidth;
00128         $displayoptions['popupheight'] = $data->popupheight;
00129     }
00130     if (in_array($data->display, array(RESOURCELIB_DISPLAY_AUTO, RESOURCELIB_DISPLAY_EMBED, RESOURCELIB_DISPLAY_FRAME))) {
00131         $displayoptions['printheading'] = (int)!empty($data->printheading);
00132         $displayoptions['printintro']   = (int)!empty($data->printintro);
00133     }
00134     $data->displayoptions = serialize($displayoptions);
00135 
00136     $DB->update_record('resource', $data);
00137     resource_set_mainfile($data);
00138     return true;
00139 }
00140 
00146 function resource_delete_instance($id) {
00147     global $DB;
00148 
00149     if (!$resource = $DB->get_record('resource', array('id'=>$id))) {
00150         return false;
00151     }
00152 
00153     // note: all context files are deleted automatically
00154 
00155     $DB->delete_records('resource', array('id'=>$resource->id));
00156 
00157     return true;
00158 }
00159 
00168 function resource_user_outline($course, $user, $mod, $resource) {
00169     global $DB;
00170 
00171     if ($logs = $DB->get_records('log', array('userid'=>$user->id, 'module'=>'resource',
00172                                               'action'=>'view', 'info'=>$resource->id), 'time ASC')) {
00173 
00174         $numviews = count($logs);
00175         $lastlog = array_pop($logs);
00176 
00177         $result = new stdClass();
00178         $result->info = get_string('numviews', '', $numviews);
00179         $result->time = $lastlog->time;
00180 
00181         return $result;
00182     }
00183     return NULL;
00184 }
00185 
00193 function resource_user_complete($course, $user, $mod, $resource) {
00194     global $CFG, $DB;
00195 
00196     if ($logs = $DB->get_records('log', array('userid'=>$user->id, 'module'=>'resource',
00197                                               'action'=>'view', 'info'=>$resource->id), 'time ASC')) {
00198         $numviews = count($logs);
00199         $lastlog = array_pop($logs);
00200 
00201         $strmostrecently = get_string('mostrecently');
00202         $strnumviews = get_string('numviews', '', $numviews);
00203 
00204         echo "$strnumviews - $strmostrecently ".userdate($lastlog->time);
00205 
00206     } else {
00207         print_string('neverseen', 'resource');
00208     }
00209 }
00210 
00219 function resource_get_participants($resourceid) {
00220     return false;
00221 }
00222 
00233 function resource_get_coursemodule_info($coursemodule) {
00234     global $CFG, $DB;
00235     require_once("$CFG->libdir/filelib.php");
00236     require_once("$CFG->dirroot/mod/resource/locallib.php");
00237     require_once($CFG->libdir.'/completionlib.php');
00238 
00239     $context = get_context_instance(CONTEXT_MODULE, $coursemodule->id);
00240 
00241     if (!$resource = $DB->get_record('resource', array('id'=>$coursemodule->instance),
00242             'id, name, display, displayoptions, tobemigrated, revision, intro, introformat')) {
00243         return NULL;
00244     }
00245 
00246     $info = new cached_cm_info();
00247     $info->name = $resource->name;
00248     if ($coursemodule->showdescription) {
00249         // Convert intro to html. Do not filter cached version, filters run at display time.
00250         $info->content = format_module_intro('resource', $resource, $coursemodule->id, false);
00251     }
00252 
00253     if ($resource->tobemigrated) {
00254         $info->icon ='i/cross_red_big';
00255         return $info;
00256     }
00257     $fs = get_file_storage();
00258     $files = $fs->get_area_files($context->id, 'mod_resource', 'content', 0, 'sortorder DESC, id ASC', false); // TODO: this is not very efficient!!
00259     if (count($files) >= 1) {
00260         $mainfile = reset($files);
00261         $info->icon = file_extension_icon($mainfile->get_filename());
00262         $resource->mainfile = $mainfile->get_filename();
00263     }
00264 
00265     $display = resource_get_final_display_type($resource);
00266 
00267     if ($display == RESOURCELIB_DISPLAY_POPUP) {
00268         $fullurl = "$CFG->wwwroot/mod/resource/view.php?id=$coursemodule->id&amp;redirect=1";
00269         $options = empty($resource->displayoptions) ? array() : unserialize($resource->displayoptions);
00270         $width  = empty($options['popupwidth'])  ? 620 : $options['popupwidth'];
00271         $height = empty($options['popupheight']) ? 450 : $options['popupheight'];
00272         $wh = "width=$width,height=$height,toolbar=no,location=no,menubar=no,copyhistory=no,status=no,directories=no,scrollbars=yes,resizable=yes";
00273         $info->onclick = "window.open('$fullurl', '', '$wh'); return false;";
00274 
00275     } else if ($display == RESOURCELIB_DISPLAY_NEW) {
00276         $fullurl = "$CFG->wwwroot/mod/resource/view.php?id=$coursemodule->id&amp;redirect=1";
00277         $info->onclick = "window.open('$fullurl'); return false;";
00278 
00279     }
00280 
00281     return $info;
00282 }
00283 
00284 
00292 function resource_get_file_areas($course, $cm, $context) {
00293     $areas = array();
00294     $areas['content'] = get_string('resourcecontent', 'resource');
00295     return $areas;
00296 }
00297 
00311 function resource_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
00312     global $CFG;
00313 
00314     if (!has_capability('moodle/course:managefiles', $context)) {
00315         // students can not peak here!
00316         return null;
00317     }
00318 
00319     $fs = get_file_storage();
00320 
00321     if ($filearea === 'content') {
00322         $filepath = is_null($filepath) ? '/' : $filepath;
00323         $filename = is_null($filename) ? '.' : $filename;
00324 
00325         $urlbase = $CFG->wwwroot.'/pluginfile.php';
00326         if (!$storedfile = $fs->get_file($context->id, 'mod_resource', 'content', 0, $filepath, $filename)) {
00327             if ($filepath === '/' and $filename === '.') {
00328                 $storedfile = new virtual_root_file($context->id, 'mod_resource', 'content', 0);
00329             } else {
00330                 // not found
00331                 return null;
00332             }
00333         }
00334         require_once("$CFG->dirroot/mod/resource/locallib.php");
00335         return new resource_content_file_info($browser, $context, $storedfile, $urlbase, $areas[$filearea], true, true, true, false);
00336     }
00337 
00338     // note: resource_intro handled in file_browser automatically
00339 
00340     return null;
00341 }
00342 
00353 function resource_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) {
00354     global $CFG, $DB;
00355     require_once("$CFG->libdir/resourcelib.php");
00356 
00357     if ($context->contextlevel != CONTEXT_MODULE) {
00358         return false;
00359     }
00360 
00361     require_course_login($course, true, $cm);
00362     if (!has_capability('mod/resource:view', $context)) {
00363         return false;
00364     }
00365 
00366     if ($filearea !== 'content') {
00367         // intro is handled automatically in pluginfile.php
00368         return false;
00369     }
00370 
00371     array_shift($args); // ignore revision - designed to prevent caching problems only
00372 
00373     $fs = get_file_storage();
00374     $relativepath = implode('/', $args);
00375     $fullpath = rtrim("/$context->id/mod_resource/$filearea/0/$relativepath", '/');
00376     do {
00377         if (!$file = $fs->get_file_by_hash(sha1($fullpath))) {
00378             if ($fs->get_file_by_hash(sha1("$fullpath/."))) {
00379                 if ($file = $fs->get_file_by_hash(sha1("$fullpath/index.htm"))) {
00380                     break;
00381                 }
00382                 if ($file = $fs->get_file_by_hash(sha1("$fullpath/index.html"))) {
00383                     break;
00384                 }
00385                 if ($file = $fs->get_file_by_hash(sha1("$fullpath/Default.htm"))) {
00386                     break;
00387                 }
00388             }
00389             $resource = $DB->get_record('resource', array('id'=>$cm->instance), 'id, legacyfiles', MUST_EXIST);
00390             if ($resource->legacyfiles != RESOURCELIB_LEGACYFILES_ACTIVE) {
00391                 return false;
00392             }
00393             if (!$file = resourcelib_try_file_migration('/'.$relativepath, $cm->id, $cm->course, 'mod_resource', 'content', 0)) {
00394                 return false;
00395             }
00396             // file migrate - update flag
00397             $resource->legacyfileslast = time();
00398             $DB->update_record('resource', $resource);
00399         }
00400     } while (false);
00401 
00402     // should we apply filters?
00403     $mimetype = $file->get_mimetype();
00404     if ($mimetype === 'text/html' or $mimetype === 'text/plain') {
00405         $filter = $DB->get_field('resource', 'filterfiles', array('id'=>$cm->instance));
00406         $CFG->embeddedsoforcelinktarget = true;
00407     } else {
00408         $filter = 0;
00409     }
00410 
00411     // finally send the file
00412     send_stored_file($file, 86400, $filter, $forcedownload);
00413 }
00414 
00421 function resource_page_type_list($pagetype, $parentcontext, $currentcontext) {
00422     $module_pagetype = array('mod-resource-*'=>get_string('page-mod-resource-x', 'resource'));
00423     return $module_pagetype;
00424 }
00425 
00431 function resource_export_contents($cm, $baseurl) {
00432     global $CFG, $DB;
00433     $contents = array();
00434     $context = get_context_instance(CONTEXT_MODULE, $cm->id);
00435     $resource = $DB->get_record('resource', array('id'=>$cm->instance), '*', MUST_EXIST);
00436 
00437     $fs = get_file_storage();
00438     $files = $fs->get_area_files($context->id, 'mod_resource', 'content', 0, 'sortorder DESC, id ASC', false);
00439 
00440     foreach ($files as $fileinfo) {
00441         $file = array();
00442         $file['type'] = 'file';
00443         $file['filename']     = $fileinfo->get_filename();
00444         $file['filepath']     = $fileinfo->get_filepath();
00445         $file['filesize']     = $fileinfo->get_filesize();
00446         $file['fileurl']      = file_encode_url("$CFG->wwwroot/" . $baseurl, '/'.$context->id.'/mod_resource/content/'.$resource->revision.$fileinfo->get_filepath().$fileinfo->get_filename(), true);
00447         $file['timecreated']  = $fileinfo->get_timecreated();
00448         $file['timemodified'] = $fileinfo->get_timemodified();
00449         $file['sortorder']    = $fileinfo->get_sortorder();
00450         $file['userid']       = $fileinfo->get_userid();
00451         $file['author']       = $fileinfo->get_author();
00452         $file['license']      = $fileinfo->get_license();
00453         $contents[] = $file;
00454     }
00455 
00456     return $contents;
00457 }
 All Data Structures Namespaces Files Functions Variables Enumerations