Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/page/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 page_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 page_get_extra_capabilities() {
00054     return array('moodle/site:accessallgroups');
00055 }
00056 
00062 function page_reset_userdata($data) {
00063     return array();
00064 }
00065 
00070 function page_get_view_actions() {
00071     return array('view','view all');
00072 }
00073 
00078 function page_get_post_actions() {
00079     return array('update', 'add');
00080 }
00081 
00088 function page_add_instance($data, $mform) {
00089     global $CFG, $DB;
00090     require_once("$CFG->libdir/resourcelib.php");
00091 
00092     $cmid        = $data->coursemodule;
00093     $draftitemid = $data->page['itemid'];
00094 
00095     $data->timemodified = time();
00096     $displayoptions = array();
00097     if ($data->display == RESOURCELIB_DISPLAY_POPUP) {
00098         $displayoptions['popupwidth']  = $data->popupwidth;
00099         $displayoptions['popupheight'] = $data->popupheight;
00100     }
00101     $displayoptions['printheading'] = $data->printheading;
00102     $displayoptions['printintro']   = $data->printintro;
00103     $data->displayoptions = serialize($displayoptions);
00104 
00105     $data->content       = $data->page['text'];
00106     $data->contentformat = $data->page['format'];
00107 
00108     $data->id = $DB->insert_record('page', $data);
00109 
00110     // we need to use context now, so we need to make sure all needed info is already in db
00111     $DB->set_field('course_modules', 'instance', $data->id, array('id'=>$cmid));
00112     $context = get_context_instance(CONTEXT_MODULE, $cmid);
00113 
00114     if ($draftitemid) {
00115         $data->content = file_save_draft_area_files($draftitemid, $context->id, 'mod_page', 'content', 0, page_get_editor_options($context), $data->content);
00116         $DB->update_record('page', $data);
00117     }
00118 
00119     return $data->id;
00120 }
00121 
00128 function page_update_instance($data, $mform) {
00129     global $CFG, $DB;
00130     require_once("$CFG->libdir/resourcelib.php");
00131 
00132     $cmid        = $data->coursemodule;
00133     $draftitemid = $data->page['itemid'];
00134 
00135     $data->timemodified = time();
00136     $data->id           = $data->instance;
00137     $data->revision++;
00138 
00139     $displayoptions = array();
00140     if ($data->display == RESOURCELIB_DISPLAY_POPUP) {
00141         $displayoptions['popupwidth']  = $data->popupwidth;
00142         $displayoptions['popupheight'] = $data->popupheight;
00143     }
00144     $displayoptions['printheading'] = $data->printheading;
00145     $displayoptions['printintro']   = $data->printintro;
00146     $data->displayoptions = serialize($displayoptions);
00147 
00148     $data->content       = $data->page['text'];
00149     $data->contentformat = $data->page['format'];
00150 
00151     $DB->update_record('page', $data);
00152 
00153     $context = get_context_instance(CONTEXT_MODULE, $cmid);
00154     if ($draftitemid) {
00155         $data->content = file_save_draft_area_files($draftitemid, $context->id, 'mod_page', 'content', 0, page_get_editor_options($context), $data->content);
00156         $DB->update_record('page', $data);
00157     }
00158 
00159     return true;
00160 }
00161 
00167 function page_delete_instance($id) {
00168     global $DB;
00169 
00170     if (!$page = $DB->get_record('page', array('id'=>$id))) {
00171         return false;
00172     }
00173 
00174     // note: all context files are deleted automatically
00175 
00176     $DB->delete_records('page', array('id'=>$page->id));
00177 
00178     return true;
00179 }
00180 
00189 function page_user_outline($course, $user, $mod, $page) {
00190     global $DB;
00191 
00192     if ($logs = $DB->get_records('log', array('userid'=>$user->id, 'module'=>'page',
00193                                               'action'=>'view', 'info'=>$page->id), 'time ASC')) {
00194 
00195         $numviews = count($logs);
00196         $lastlog = array_pop($logs);
00197 
00198         $result = new stdClass();
00199         $result->info = get_string('numviews', '', $numviews);
00200         $result->time = $lastlog->time;
00201 
00202         return $result;
00203     }
00204     return NULL;
00205 }
00206 
00214 function page_user_complete($course, $user, $mod, $page) {
00215     global $CFG, $DB;
00216 
00217     if ($logs = $DB->get_records('log', array('userid'=>$user->id, 'module'=>'page',
00218                                               'action'=>'view', 'info'=>$page->id), 'time ASC')) {
00219         $numviews = count($logs);
00220         $lastlog = array_pop($logs);
00221 
00222         $strmostrecently = get_string('mostrecently');
00223         $strnumviews = get_string('numviews', '', $numviews);
00224 
00225         echo "$strnumviews - $strmostrecently ".userdate($lastlog->time);
00226 
00227     } else {
00228         print_string('neverseen', 'page');
00229     }
00230 }
00231 
00240 function page_get_participants($pageid) {
00241     return false;
00242 }
00243 
00254 function page_get_coursemodule_info($coursemodule) {
00255     global $CFG, $DB;
00256     require_once("$CFG->libdir/resourcelib.php");
00257 
00258     if (!$page = $DB->get_record('page', array('id'=>$coursemodule->instance),
00259             'id, name, display, displayoptions, intro, introformat')) {
00260         return NULL;
00261     }
00262 
00263     $info = new cached_cm_info();
00264     $info->name = $page->name;
00265 
00266     if ($coursemodule->showdescription) {
00267         // Convert intro to html. Do not filter cached version, filters run at display time.
00268         $info->content = format_module_intro('page', $page, $coursemodule->id, false);
00269     }
00270 
00271     if ($page->display != RESOURCELIB_DISPLAY_POPUP) {
00272         return $info;
00273     }
00274 
00275     $fullurl = "$CFG->wwwroot/mod/page/view.php?id=$coursemodule->id&amp;inpopup=1";
00276     $options = empty($page->displayoptions) ? array() : unserialize($page->displayoptions);
00277     $width  = empty($options['popupwidth'])  ? 620 : $options['popupwidth'];
00278     $height = empty($options['popupheight']) ? 450 : $options['popupheight'];
00279     $wh = "width=$width,height=$height,toolbar=no,location=no,menubar=no,copyhistory=no,status=no,directories=no,scrollbars=yes,resizable=yes";
00280     $info->onclick = "window.open('$fullurl', '', '$wh'); return false;";
00281 
00282     return $info;
00283 }
00284 
00285 
00293 function page_get_file_areas($course, $cm, $context) {
00294     $areas = array();
00295     $areas['content'] = get_string('content', 'page');
00296     return $areas;
00297 }
00298 
00312 function page_get_file_info($browser, $areas, $course, $cm, $context, $filearea, $itemid, $filepath, $filename) {
00313     global $CFG;
00314 
00315     if (!has_capability('moodle/course:managefiles', $context)) {
00316         // students can not peak here!
00317         return null;
00318     }
00319 
00320     $fs = get_file_storage();
00321 
00322     if ($filearea === 'content') {
00323         $filepath = is_null($filepath) ? '/' : $filepath;
00324         $filename = is_null($filename) ? '.' : $filename;
00325 
00326         $urlbase = $CFG->wwwroot.'/pluginfile.php';
00327         if (!$storedfile = $fs->get_file($context->id, 'mod_page', 'content', 0, $filepath, $filename)) {
00328             if ($filepath === '/' and $filename === '.') {
00329                 $storedfile = new virtual_root_file($context->id, 'mod_page', 'content', 0);
00330             } else {
00331                 // not found
00332                 return null;
00333             }
00334         }
00335         require_once("$CFG->dirroot/mod/page/locallib.php");
00336         return new page_content_file_info($browser, $context, $storedfile, $urlbase, $areas[$filearea], true, true, true, false);
00337     }
00338 
00339     // note: page_intro handled in file_browser automatically
00340 
00341     return null;
00342 }
00343 
00354 function page_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) {
00355     global $CFG, $DB;
00356     require_once("$CFG->libdir/resourcelib.php");
00357 
00358     if ($context->contextlevel != CONTEXT_MODULE) {
00359         return false;
00360     }
00361 
00362     require_course_login($course, true, $cm);
00363     if (!has_capability('mod/page:view', $context)) {
00364         return false;
00365     }
00366 
00367     if ($filearea !== 'content') {
00368         // intro is handled automatically in pluginfile.php
00369         return false;
00370     }
00371 
00372     // $arg could be revision number or index.html
00373     $arg = array_shift($args);
00374     if ($arg == 'index.html' || $arg == 'index.htm') {
00375         // serve page content
00376         $filename = $arg;
00377 
00378         if (!$page = $DB->get_record('page', array('id'=>$cm->instance), '*', MUST_EXIST)) {
00379             return false;
00380         }
00381 
00382         // remove @@PLUGINFILE@@/
00383         $content = str_replace('@@PLUGINFILE@@/', '', $page->content);
00384 
00385         $formatoptions = new stdClass;
00386         $formatoptions->noclean = true;
00387         $formatoptions->overflowdiv = true;
00388         $formatoptions->context = $context;
00389         $content = format_text($content, $page->contentformat, $formatoptions);
00390 
00391         send_file($content, $filename, 0, 0, true, true);
00392     } else {
00393         $fs = get_file_storage();
00394         $relativepath = implode('/', $args);
00395         $fullpath = "/$context->id/mod_page/$filearea/0/$relativepath";
00396         if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
00397             $page = $DB->get_record('page', array('id'=>$cm->instance), 'id, legacyfiles', MUST_EXIST);
00398             if ($page->legacyfiles != RESOURCELIB_LEGACYFILES_ACTIVE) {
00399                 return false;
00400             }
00401             if (!$file = resourcelib_try_file_migration('/'.$relativepath, $cm->id, $cm->course, 'mod_page', 'content', 0)) {
00402                 return false;
00403             }
00404             //file migrate - update flag
00405             $page->legacyfileslast = time();
00406             $DB->update_record('page', $page);
00407         }
00408 
00409         // finally send the file
00410         send_stored_file($file, 86400, 0, $forcedownload);
00411     }
00412 }
00413 
00414 
00426 function page_extend_navigation($navigation, $course, $module, $cm) {
00432     $navigation->nodetype = navigation_node::NODETYPE_LEAF;
00433 }
00434 
00441 function page_page_type_list($pagetype, $parentcontext, $currentcontext) {
00442     $module_pagetype = array('mod-page-*'=>get_string('page-mod-page-x', 'page'));
00443     return $module_pagetype;
00444 }
00445 
00451 function page_export_contents($cm, $baseurl) {
00452     global $CFG, $DB;
00453     $contents = array();
00454     $context = get_context_instance(CONTEXT_MODULE, $cm->id);
00455 
00456     $page = $DB->get_record('page', array('id'=>$cm->instance), '*', MUST_EXIST);
00457 
00458     // page contents
00459     $fs = get_file_storage();
00460     $files = $fs->get_area_files($context->id, 'mod_page', 'content', 0, 'sortorder DESC, id ASC', false);
00461     foreach ($files as $fileinfo) {
00462         $file = array();
00463         $file['type']         = 'file';
00464         $file['filename']     = $fileinfo->get_filename();
00465         $file['filepath']     = $fileinfo->get_filepath();
00466         $file['filesize']     = $fileinfo->get_filesize();
00467         $file['fileurl']      = file_encode_url("$CFG->wwwroot/" . $baseurl, '/'.$context->id.'/mod_page/content/'.$page->revision.$fileinfo->get_filepath().$fileinfo->get_filename(), true);
00468         $file['timecreated']  = $fileinfo->get_timecreated();
00469         $file['timemodified'] = $fileinfo->get_timemodified();
00470         $file['sortorder']    = $fileinfo->get_sortorder();
00471         $file['userid']       = $fileinfo->get_userid();
00472         $file['author']       = $fileinfo->get_author();
00473         $file['license']      = $fileinfo->get_license();
00474         $contents[] = $file;
00475     }
00476 
00477     // page html conent
00478     $filename = 'index.html';
00479     $pagefile = array();
00480     $pagefile['type']         = 'file';
00481     $pagefile['filename']     = $filename;
00482     $pagefile['filepath']     = '/';
00483     $pagefile['filesize']     = 0;
00484     $pagefile['fileurl']      = file_encode_url("$CFG->wwwroot/" . $baseurl, '/'.$context->id.'/mod_page/content/' . $filename, true);
00485     $pagefile['timecreated']  = null;
00486     $pagefile['timemodified'] = $page->timemodified;
00487     // make this file as main file
00488     $pagefile['sortorder']    = 1;
00489     $pagefile['userid']       = null;
00490     $pagefile['author']       = null;
00491     $pagefile['license']      = null;
00492     $contents[] = $pagefile;
00493 
00494     return $contents;
00495 }
 All Data Structures Namespaces Files Functions Variables Enumerations