Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/resource/locallib.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 
00029 require_once("$CFG->libdir/filelib.php");
00030 require_once("$CFG->libdir/resourcelib.php");
00031 require_once("$CFG->dirroot/mod/resource/lib.php");
00032 
00040 function resource_redirect_if_migrated($oldid, $cmid) {
00041     global $DB, $CFG;
00042 
00043     if ($oldid) {
00044         $old = $DB->get_record('resource_old', array('oldid'=>$oldid));
00045     } else {
00046         $old = $DB->get_record('resource_old', array('cmid'=>$cmid));
00047     }
00048 
00049     if (!$old) {
00050         return;
00051     }
00052 
00053     redirect("$CFG->wwwroot/mod/$old->newmodule/view.php?id=".$old->cmid);
00054 }
00055 
00064 function resource_display_embed($resource, $cm, $course, $file) {
00065     global $CFG, $PAGE, $OUTPUT;
00066 
00067     $clicktoopen = resource_get_clicktoopen($file, $resource->revision);
00068 
00069     $context = get_context_instance(CONTEXT_MODULE, $cm->id);
00070     $path = '/'.$context->id.'/mod_resource/content/'.$resource->revision.$file->get_filepath().$file->get_filename();
00071     $fullurl = file_encode_url($CFG->wwwroot.'/pluginfile.php', $path, false);
00072 
00073     $mimetype = $file->get_mimetype();
00074     $title    = $resource->name;
00075 
00076     $extension = resourcelib_get_extension($file->get_filename());
00077 
00078     if (in_array($mimetype, array('image/gif','image/jpeg','image/png'))) {  // It's an image
00079         $code = resourcelib_embed_image($fullurl, $title);
00080 
00081     } else if ($mimetype === 'application/pdf') {
00082         // PDF document
00083         $code = resourcelib_embed_pdf($fullurl, $title, $clicktoopen);
00084 
00085     } else if ($mimetype === 'audio/mp3') {
00086         // MP3 audio file
00087         $code = resourcelib_embed_mp3($fullurl, $title, $clicktoopen);
00088 
00089     } else if ($mimetype === 'video/x-flv' or $extension === 'f4v') {
00090         // Flash video file
00091         $code = resourcelib_embed_flashvideo($fullurl, $title, $clicktoopen);
00092 
00093     } else if ($mimetype === 'application/x-shockwave-flash') {
00094         // Flash file
00095         $code = resourcelib_embed_flash($fullurl, $title, $clicktoopen);
00096 
00097     } else if (substr($mimetype, 0, 10) === 'video/x-ms') {
00098         // Windows Media Player file
00099         $code = resourcelib_embed_mediaplayer($fullurl, $title, $clicktoopen);
00100 
00101     } else if ($mimetype === 'video/quicktime') {
00102         // Quicktime file
00103         $code = resourcelib_embed_quicktime($fullurl, $title, $clicktoopen);
00104 
00105     } else if ($mimetype === 'video/mpeg') {
00106         // Mpeg file
00107         $code = resourcelib_embed_mpeg($fullurl, $title, $clicktoopen);
00108 
00109     } else if ($mimetype === 'audio/x-pn-realaudio') {
00110         // RealMedia file
00111         $code = resourcelib_embed_real($fullurl, $title, $clicktoopen);
00112 
00113     } else {
00114         // anything else - just try object tag enlarged as much as possible
00115         $code = resourcelib_embed_general($fullurl, $title, $clicktoopen, $mimetype);
00116     }
00117 
00118     resource_print_header($resource, $cm, $course);
00119     resource_print_heading($resource, $cm, $course);
00120 
00121     echo $code;
00122 
00123     resource_print_intro($resource, $cm, $course);
00124 
00125     echo $OUTPUT->footer();
00126     die;
00127 }
00128 
00137 function resource_display_frame($resource, $cm, $course, $file) {
00138     global $PAGE, $OUTPUT, $CFG;
00139 
00140     $frame = optional_param('frameset', 'main', PARAM_ALPHA);
00141 
00142     if ($frame === 'top') {
00143         $PAGE->set_pagelayout('frametop');
00144         resource_print_header($resource, $cm, $course);
00145         resource_print_heading($resource, $cm, $course);
00146         resource_print_intro($resource, $cm, $course);
00147         echo $OUTPUT->footer();
00148         die;
00149 
00150     } else {
00151         $config = get_config('resource');
00152         $context = get_context_instance(CONTEXT_MODULE, $cm->id);
00153         $path = '/'.$context->id.'/mod_resource/content/'.$resource->revision.$file->get_filepath().$file->get_filename();
00154         $fileurl = file_encode_url($CFG->wwwroot.'/pluginfile.php', $path, false);
00155         $navurl = "$CFG->wwwroot/mod/resource/view.php?id=$cm->id&amp;frameset=top";
00156         $title = strip_tags(format_string($course->shortname.': '.$resource->name));
00157         $framesize = $config->framesize;
00158         $modulename = s(get_string('modulename','resource'));
00159         $dir = get_string('thisdirection', 'langconfig');
00160 
00161         $file = <<<EOF
00162 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
00163 <html dir="$dir">
00164   <head>
00165     <meta http-equiv="content-type" content="text/html; charset=utf-8" />
00166     <title>$title</title>
00167   </head>
00168   <frameset rows="$framesize,*">
00169     <frame src="$navurl" title="$modulename" />
00170     <frame src="$fileurl" title="$modulename" />
00171   </frameset>
00172 </html>
00173 EOF;
00174 
00175         @header('Content-Type: text/html; charset=utf-8');
00176         echo $file;
00177         die;
00178     }
00179 }
00180 
00184 function resource_get_clicktoopen($file, $revision, $extra='') {
00185     global $CFG;
00186 
00187     $filename = $file->get_filename();
00188     $path = '/'.$file->get_contextid().'/mod_resource/content/'.$revision.$file->get_filepath().$file->get_filename();
00189     $fullurl = file_encode_url($CFG->wwwroot.'/pluginfile.php', $path, false);
00190 
00191     $string = get_string('clicktoopen2', 'resource', "<a href=\"$fullurl\" $extra>$filename</a>");
00192 
00193     return $string;
00194 }
00195 
00199 function resource_get_clicktodownload($file, $revision) {
00200     global $CFG;
00201 
00202     $filename = $file->get_filename();
00203     $path = '/'.$file->get_contextid().'/mod_resource/content/'.$revision.$file->get_filepath().$file->get_filename();
00204     $fullurl = file_encode_url($CFG->wwwroot.'/pluginfile.php', $path, true);
00205 
00206     $string = get_string('clicktodownload', 'resource', "<a href=\"$fullurl\">$filename</a>");
00207 
00208     return $string;
00209 }
00210 
00219 function resource_print_workaround($resource, $cm, $course, $file) {
00220     global $CFG, $OUTPUT;
00221 
00222     resource_print_header($resource, $cm, $course);
00223     resource_print_heading($resource, $cm, $course, true);
00224     resource_print_intro($resource, $cm, $course, true);
00225 
00226     $resource->mainfile = $file->get_filename();
00227     echo '<div class="resourceworkaround">';
00228     switch (resource_get_final_display_type($resource)) {
00229         case RESOURCELIB_DISPLAY_POPUP:
00230             $path = '/'.$file->get_contextid().'/mod_resource/content/'.$resource->revision.$file->get_filepath().$file->get_filename();
00231             $fullurl = file_encode_url($CFG->wwwroot.'/pluginfile.php', $path, false);
00232             $options = empty($resource->displayoptions) ? array() : unserialize($resource->displayoptions);
00233             $width  = empty($options['popupwidth'])  ? 620 : $options['popupwidth'];
00234             $height = empty($options['popupheight']) ? 450 : $options['popupheight'];
00235             $wh = "width=$width,height=$height,toolbar=no,location=no,menubar=no,copyhistory=no,status=no,directories=no,scrollbars=yes,resizable=yes";
00236             $extra = "onclick=\"window.open('$fullurl', '', '$wh'); return false;\"";
00237             echo resource_get_clicktoopen($file, $resource->revision, $extra);
00238             break;
00239 
00240         case RESOURCELIB_DISPLAY_NEW:
00241             $extra = 'onclick="this.target=\'_blank\'"';
00242             echo resource_get_clicktoopen($file, $resource->revision, $extra);
00243             break;
00244 
00245         case RESOURCELIB_DISPLAY_DOWNLOAD:
00246             echo resource_get_clicktodownload($file, $resource->revision);
00247             break;
00248 
00249         case RESOURCELIB_DISPLAY_OPEN:
00250         default:
00251             echo resource_get_clicktoopen($file, $resource->revision);
00252             break;
00253     }
00254     echo '</div>';
00255 
00256     echo $OUTPUT->footer();
00257     die;
00258 }
00259 
00267 function resource_print_header($resource, $cm, $course) {
00268     global $PAGE, $OUTPUT;
00269 
00270     $PAGE->set_title($course->shortname.': '.$resource->name);
00271     $PAGE->set_heading($course->fullname);
00272     $PAGE->set_activity_record($resource);
00273     $PAGE->set_button(update_module_button($cm->id, '', get_string('modulename', 'resource')));
00274     echo $OUTPUT->header();
00275 }
00276 
00285 function resource_print_heading($resource, $cm, $course, $ignoresettings=false) {
00286     global $OUTPUT;
00287 
00288     $options = empty($resource->displayoptions) ? array() : unserialize($resource->displayoptions);
00289     if ($ignoresettings or !empty($options['printheading'])) {
00290         echo $OUTPUT->heading(format_string($resource->name), 2, 'main', 'resourceheading');
00291     }
00292 }
00293 
00302 function resource_print_intro($resource, $cm, $course, $ignoresettings=false) {
00303     global $OUTPUT;
00304 
00305     $options = empty($resource->displayoptions) ? array() : unserialize($resource->displayoptions);
00306     if ($ignoresettings or !empty($options['printintro'])) {
00307         if (trim(strip_tags($resource->intro))) {
00308             echo $OUTPUT->box_start('mod_introbox', 'resourceintro');
00309             echo format_module_intro('resource', $resource, $cm->id);
00310             echo $OUTPUT->box_end();
00311         }
00312     }
00313 }
00314 
00322 function resource_print_tobemigrated($resource, $cm, $course) {
00323     global $DB, $OUTPUT;
00324 
00325     $resource_old = $DB->get_record('resource_old', array('oldid'=>$resource->id));
00326     resource_print_header($resource, $cm, $course);
00327     resource_print_heading($resource, $cm, $course);
00328     resource_print_intro($resource, $cm, $course);
00329     echo $OUTPUT->notification(get_string('notmigrated', 'resource', $resource_old->type));
00330     echo $OUTPUT->footer();
00331     die;
00332 }
00333 
00341 function resource_print_filenotfound($resource, $cm, $course) {
00342     global $DB, $OUTPUT;
00343 
00344     $resource_old = $DB->get_record('resource_old', array('oldid'=>$resource->id));
00345     resource_print_header($resource, $cm, $course);
00346     resource_print_heading($resource, $cm, $course);
00347     resource_print_intro($resource, $cm, $course);
00348     if ($resource_old) {
00349         echo $OUTPUT->notification(get_string('notmigrated', 'resource', $resource_old->type));
00350     } else {
00351         echo $OUTPUT->notification(get_string('filenotfound', 'resource'));
00352     }
00353     echo $OUTPUT->footer();
00354     die;
00355 }
00356 
00362 function resource_get_final_display_type($resource) {
00363     global $CFG;
00364 
00365     if ($resource->display != RESOURCELIB_DISPLAY_AUTO) {
00366         return $resource->display;
00367     }
00368 
00369     static $download = array('application/zip', 'application/x-tar', 'application/g-zip');    // binary formats
00370     static $embed    = array('image/gif', 'image/jpeg', 'image/png', 'image/svg+xml',         // images
00371                              'application/x-shockwave-flash', 'video/x-flv', 'video/x-ms-wm', // video formats
00372                              'video/quicktime', 'video/mpeg', 'video/mp4',
00373                              'audio/mp3', 'audio/x-realaudio-plugin', 'x-realaudio-plugin',   // audio formats
00374                              'application/pdf', 'text/html',
00375                             );
00376 
00377     if (empty($resource->mainfile)) {
00378         return RESOURCELIB_DISPLAY_DOWNLOAD;
00379     } else {
00380         $mimetype = mimeinfo('type', $resource->mainfile);
00381     }
00382 
00383     if (in_array($mimetype, $download)) {
00384         return RESOURCELIB_DISPLAY_DOWNLOAD;
00385     }
00386     if (in_array($mimetype, $embed)) {
00387         return RESOURCELIB_DISPLAY_EMBED;
00388     }
00389 
00390     // let the browser deal with it somehow
00391     return RESOURCELIB_DISPLAY_OPEN;
00392 }
00393 
00397 class resource_content_file_info extends file_info_stored {
00398     public function get_parent() {
00399         if ($this->lf->get_filepath() === '/' and $this->lf->get_filename() === '.') {
00400             return $this->browser->get_file_info($this->context);
00401         }
00402         return parent::get_parent();
00403     }
00404     public function get_visible_name() {
00405         if ($this->lf->get_filepath() === '/' and $this->lf->get_filename() === '.') {
00406             return $this->topvisiblename;
00407         }
00408         return parent::get_visible_name();
00409     }
00410 }
00411 
00412 function resource_set_mainfile($data) {
00413     global $DB;
00414     $fs = get_file_storage();
00415     $cmid = $data->coursemodule;
00416     $draftitemid = $data->files;
00417 
00418     $context = get_context_instance(CONTEXT_MODULE, $cmid);
00419     if ($draftitemid) {
00420         file_save_draft_area_files($draftitemid, $context->id, 'mod_resource', 'content', 0, array('subdirs'=>true));
00421     }
00422     $files = $fs->get_area_files($context->id, 'mod_resource', 'content', 0, 'sortorder', false);
00423     if (count($files) == 1) {
00424         // only one file attached, set it as main file automatically
00425         $file = reset($files);
00426         file_set_sortorder($context->id, 'mod_resource', 'content', 0, $file->get_filepath(), $file->get_filename(), 1);
00427     }
00428 }
 All Data Structures Namespaces Files Functions Variables Enumerations