2004-09-12 17:34:35 +00:00
|
|
|
<?php // $Id$
|
2002-10-17 14:03:59 +00:00
|
|
|
|
2005-04-02 12:11:27 +00:00
|
|
|
define('RESOURCE_LOCALPATH', 'LOCALPATH');
|
|
|
|
|
2007-12-19 17:35:20 +00:00
|
|
|
global $RESOURCE_WINDOW_OPTIONS; // must be global because it might be included from a function!
|
2005-02-16 10:40:48 +00:00
|
|
|
$RESOURCE_WINDOW_OPTIONS = array('resizable', 'scrollbars', 'directories', 'location',
|
2007-06-04 22:24:23 +00:00
|
|
|
'menubar', 'toolbar', 'status', 'width', 'height');
|
2004-08-08 14:33:23 +00:00
|
|
|
|
2007-12-19 17:35:20 +00:00
|
|
|
if (!isset($CFG->resource_hide_repository)) {
|
|
|
|
set_config("resource_hide_repository", "1");
|
2003-12-09 04:00:33 +00:00
|
|
|
}
|
|
|
|
|
2004-07-25 14:32:05 +00:00
|
|
|
/**
|
|
|
|
* resource_base is the base class for resource types
|
|
|
|
*
|
|
|
|
* This class provides all the functionality for a resource
|
|
|
|
*/
|
|
|
|
|
|
|
|
class resource_base {
|
|
|
|
|
2007-08-16 17:38:50 +00:00
|
|
|
var $cm;
|
|
|
|
var $course;
|
|
|
|
var $resource;
|
|
|
|
var $navlinks;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor for the base resource class
|
|
|
|
*
|
|
|
|
* Constructor for the base resource class.
|
|
|
|
* If cmid is set create the cm, course, resource objects.
|
|
|
|
* and do some checks to make sure people can be here, and so on.
|
|
|
|
*
|
|
|
|
* @param cmid integer, the current course module id - not set for new resources
|
|
|
|
*/
|
|
|
|
function resource_base($cmid=0) {
|
2008-06-04 00:04:03 +00:00
|
|
|
global $CFG, $COURSE, $DB;
|
2007-08-16 17:38:50 +00:00
|
|
|
|
2007-10-15 07:32:15 +00:00
|
|
|
$this->navlinks = array();
|
2007-08-16 17:38:50 +00:00
|
|
|
|
|
|
|
if ($cmid) {
|
|
|
|
if (! $this->cm = get_coursemodule_from_id('resource', $cmid)) {
|
2008-04-04 02:54:20 +00:00
|
|
|
print_error("Course Module ID was incorrect");
|
2007-08-16 17:38:50 +00:00
|
|
|
}
|
|
|
|
|
2008-06-04 00:04:03 +00:00
|
|
|
if (! $this->course = $DB->get_record("course", array("id"=>$this->cm->course))) {
|
2008-04-04 02:54:20 +00:00
|
|
|
print_error("Course is misconfigured");
|
2007-08-16 17:38:50 +00:00
|
|
|
}
|
|
|
|
|
2008-06-04 00:04:03 +00:00
|
|
|
if (! $this->resource = $DB->get_record("resource", array("id"=>$this->cm->instance))) {
|
2008-04-04 02:54:20 +00:00
|
|
|
print_error("Resource ID was incorrect");
|
2007-08-16 17:38:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->strresource = get_string("modulename", "resource");
|
|
|
|
$this->strresources = get_string("modulenameplural", "resource");
|
|
|
|
|
|
|
|
if (!$this->cm->visible and !has_capability('moodle/course:viewhiddenactivities', get_context_instance(CONTEXT_MODULE, $this->cm->id))) {
|
|
|
|
$pagetitle = strip_tags($this->course->shortname.': '.$this->strresource);
|
2007-10-12 15:55:49 +00:00
|
|
|
$navigation = build_navigation($this->navlinks, $this->cm);
|
2007-08-16 17:38:50 +00:00
|
|
|
|
2007-10-12 15:55:49 +00:00
|
|
|
print_header($pagetitle, $this->course->fullname, $navigation, "", "", true, '', navmenu($this->course, $this->cm));
|
2007-08-16 17:38:50 +00:00
|
|
|
notice(get_string("activityiscurrentlyhidden"), "$CFG->wwwroot/course/view.php?id={$this->course->id}");
|
|
|
|
}
|
2004-07-25 14:32:05 +00:00
|
|
|
|
2007-08-16 17:38:50 +00:00
|
|
|
} else {
|
|
|
|
$this->course = $COURSE;
|
2004-07-25 14:32:05 +00:00
|
|
|
}
|
2007-08-16 17:38:50 +00:00
|
|
|
}
|
2004-11-11 07:44:14 +00:00
|
|
|
|
|
|
|
|
2007-08-16 17:38:50 +00:00
|
|
|
/**
|
|
|
|
* Display function does nothing in the base class
|
|
|
|
*/
|
|
|
|
function display() {
|
2004-11-11 07:44:14 +00:00
|
|
|
|
2005-02-16 10:40:48 +00:00
|
|
|
}
|
2004-07-25 14:32:05 +00:00
|
|
|
|
|
|
|
|
2007-08-16 17:38:50 +00:00
|
|
|
/**
|
|
|
|
* Display the resource with the course blocks.
|
|
|
|
*/
|
|
|
|
function display_course_blocks_start() {
|
2004-11-11 07:44:14 +00:00
|
|
|
|
2008-06-04 00:04:03 +00:00
|
|
|
global $CFG, $USER, $THEME;
|
2004-07-25 14:32:05 +00:00
|
|
|
|
2007-08-16 17:38:50 +00:00
|
|
|
require_once($CFG->libdir.'/blocklib.php');
|
|
|
|
require_once($CFG->libdir.'/pagelib.php');
|
|
|
|
require_once($CFG->dirroot.'/course/lib.php'); //required by some blocks
|
2004-07-25 14:32:05 +00:00
|
|
|
|
2007-08-16 17:38:50 +00:00
|
|
|
$PAGE = page_create_object(PAGE_COURSE_VIEW, $this->course->id);
|
|
|
|
$this->PAGE = $PAGE;
|
|
|
|
$pageblocks = blocks_setup($PAGE);
|
2005-08-14 05:34:56 +00:00
|
|
|
|
2007-08-16 17:38:50 +00:00
|
|
|
$blocks_preferred_width = bounded_number(180, blocks_preferred_width($pageblocks[BLOCK_POS_LEFT]), 210);
|
2005-08-14 05:34:56 +00:00
|
|
|
|
2007-08-16 17:38:50 +00:00
|
|
|
/// Print the page header
|
2005-08-14 05:34:56 +00:00
|
|
|
|
2007-08-16 17:38:50 +00:00
|
|
|
$edit = optional_param('edit', -1, PARAM_BOOL);
|
2005-08-14 05:34:56 +00:00
|
|
|
|
2007-08-16 17:38:50 +00:00
|
|
|
if (($edit != -1) and $PAGE->user_allowed_editing()) {
|
|
|
|
$USER->editing = $edit;
|
|
|
|
}
|
2006-04-23 20:58:06 +00:00
|
|
|
|
2007-08-16 17:38:50 +00:00
|
|
|
$morenavlinks = array($this->strresources => 'index.php?id='.$this->course->id,
|
|
|
|
$this->resource->name => '');
|
2006-04-23 20:58:06 +00:00
|
|
|
|
2007-12-28 16:13:09 +00:00
|
|
|
$PAGE->print_header($this->course->shortname.': %fullname%', $morenavlinks, "", "",
|
|
|
|
update_module_button($this->cm->id, $this->course->id, $this->strresource));
|
2005-08-14 05:34:56 +00:00
|
|
|
|
2007-08-16 17:38:50 +00:00
|
|
|
echo '<table id="layout-table"><tr>';
|
2005-08-14 05:34:56 +00:00
|
|
|
|
2008-01-12 18:14:49 +00:00
|
|
|
$lt = (empty($THEME->layouttable)) ? array('left', 'middle', 'right') : $THEME->layouttable;
|
|
|
|
foreach ($lt as $column) {
|
|
|
|
$lt1[] = $column;
|
|
|
|
if ($column == 'middle') break;
|
|
|
|
}
|
|
|
|
foreach ($lt1 as $column) {
|
|
|
|
switch ($column) {
|
|
|
|
case 'left':
|
|
|
|
if((blocks_have_content($pageblocks, BLOCK_POS_LEFT) || $PAGE->user_is_editing())) {
|
|
|
|
echo '<td style="width: '.$blocks_preferred_width.'px;" id="left-column">';
|
|
|
|
print_container_start();
|
|
|
|
blocks_print_group($PAGE, $pageblocks, BLOCK_POS_LEFT);
|
|
|
|
print_container_end();
|
|
|
|
echo '</td>';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'middle':
|
|
|
|
echo '<td id="middle-column">';
|
|
|
|
print_container_start(false, 'middle-column-wrap');
|
|
|
|
echo '<div id="resource">';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'right':
|
|
|
|
if((blocks_have_content($pageblocks, BLOCK_POS_RIGHT) || $PAGE->user_is_editing())) {
|
|
|
|
echo '<td style="width: '.$blocks_preferred_width.'px;" id="right-column">';
|
|
|
|
print_container_start();
|
|
|
|
blocks_print_group($PAGE, $pageblocks, BLOCK_POS_RIGHT);
|
|
|
|
print_container_end();
|
|
|
|
echo '</td>';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2007-08-16 17:38:50 +00:00
|
|
|
}
|
|
|
|
}
|
2005-08-14 05:34:56 +00:00
|
|
|
|
|
|
|
|
2007-08-16 17:38:50 +00:00
|
|
|
/**
|
|
|
|
* Finish displaying the resource with the course blocks
|
|
|
|
*/
|
|
|
|
function display_course_blocks_end() {
|
2008-06-04 00:04:03 +00:00
|
|
|
global $CFG, $THEME;
|
2005-08-14 05:34:56 +00:00
|
|
|
|
2007-08-16 17:38:50 +00:00
|
|
|
$PAGE = $this->PAGE;
|
|
|
|
$pageblocks = blocks_setup($PAGE);
|
|
|
|
$blocks_preferred_width = bounded_number(180, blocks_preferred_width($pageblocks[BLOCK_POS_RIGHT]), 210);
|
2005-08-14 05:34:56 +00:00
|
|
|
|
2008-01-12 18:14:49 +00:00
|
|
|
$lt = (empty($THEME->layouttable)) ? array('left', 'middle', 'right') : $THEME->layouttable;
|
|
|
|
foreach ($lt as $column) {
|
|
|
|
if ($column != 'middle') {
|
|
|
|
array_shift($lt);
|
|
|
|
} else if ($column == 'middle') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach ($lt as $column) {
|
|
|
|
switch ($column) {
|
|
|
|
case 'left':
|
|
|
|
if((blocks_have_content($pageblocks, BLOCK_POS_LEFT) || $PAGE->user_is_editing())) {
|
|
|
|
echo '<td style="width: '.$blocks_preferred_width.'px;" id="left-column">';
|
|
|
|
print_container_start();
|
|
|
|
blocks_print_group($PAGE, $pageblocks, BLOCK_POS_LEFT);
|
|
|
|
print_container_end();
|
|
|
|
echo '</td>';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'middle':
|
|
|
|
echo '</div>';
|
|
|
|
print_container_end();
|
|
|
|
echo '</td>';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'right':
|
|
|
|
if((blocks_have_content($pageblocks, BLOCK_POS_RIGHT) || $PAGE->user_is_editing())) {
|
|
|
|
echo '<td style="width: '.$blocks_preferred_width.'px;" id="right-column">';
|
|
|
|
print_container_start();
|
|
|
|
blocks_print_group($PAGE, $pageblocks, BLOCK_POS_RIGHT);
|
|
|
|
print_container_end();
|
|
|
|
echo '</td>';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2007-08-16 17:38:50 +00:00
|
|
|
}
|
2005-08-14 05:34:56 +00:00
|
|
|
|
2007-08-16 17:38:50 +00:00
|
|
|
echo '</tr></table>';
|
2005-08-14 05:34:56 +00:00
|
|
|
|
2007-08-16 17:38:50 +00:00
|
|
|
print_footer($this->course);
|
|
|
|
|
|
|
|
}
|
2005-08-14 05:34:56 +00:00
|
|
|
|
|
|
|
|
2007-08-16 17:38:50 +00:00
|
|
|
function add_instance($resource) {
|
2008-06-01 21:36:11 +00:00
|
|
|
global $DB;
|
2007-08-16 17:38:50 +00:00
|
|
|
// Given an object containing all the necessary data,
|
2008-06-01 19:09:13 +00:00
|
|
|
// (defined by the form in mod_form.php) this function
|
2007-08-16 17:38:50 +00:00
|
|
|
// will create a new instance and return the id number
|
|
|
|
// of the new instance.
|
2002-10-17 14:03:59 +00:00
|
|
|
|
2007-08-16 17:38:50 +00:00
|
|
|
$resource->timemodified = time();
|
2002-10-17 14:03:59 +00:00
|
|
|
|
2008-06-01 21:36:11 +00:00
|
|
|
return $DB->insert_record("resource", $resource);
|
2007-08-16 17:38:50 +00:00
|
|
|
}
|
2002-10-17 14:03:59 +00:00
|
|
|
|
2003-07-12 05:19:18 +00:00
|
|
|
|
2007-08-16 17:38:50 +00:00
|
|
|
function update_instance($resource) {
|
2008-06-01 21:36:11 +00:00
|
|
|
global $DB;
|
2007-08-16 17:38:50 +00:00
|
|
|
// Given an object containing all the necessary data,
|
2008-06-01 19:09:13 +00:00
|
|
|
// (defined by the form in mod_form.php) this function
|
2007-08-16 17:38:50 +00:00
|
|
|
// will update an existing instance with new data.
|
2003-07-12 05:19:18 +00:00
|
|
|
|
2007-08-16 17:38:50 +00:00
|
|
|
$resource->id = $resource->instance;
|
|
|
|
$resource->timemodified = time();
|
2003-07-12 05:19:18 +00:00
|
|
|
|
2008-06-01 21:36:11 +00:00
|
|
|
return $DB->update_record("resource", $resource);
|
2007-08-16 17:38:50 +00:00
|
|
|
}
|
2003-07-12 05:19:18 +00:00
|
|
|
|
|
|
|
|
2007-08-16 17:38:50 +00:00
|
|
|
function delete_instance($resource) {
|
2008-06-01 21:36:11 +00:00
|
|
|
global $DB;
|
2007-08-16 17:38:50 +00:00
|
|
|
// Given an object containing the resource data
|
|
|
|
// this function will permanently delete the instance
|
|
|
|
// and any data that depends on it.
|
2003-07-12 05:19:18 +00:00
|
|
|
|
2007-08-16 17:38:50 +00:00
|
|
|
$result = true;
|
2003-07-12 05:19:18 +00:00
|
|
|
|
2008-06-01 21:36:11 +00:00
|
|
|
if (! $DB->delete_records("resource", array("id"=>$resource->id))) {
|
2007-08-16 17:38:50 +00:00
|
|
|
$result = false;
|
|
|
|
}
|
2002-10-17 14:03:59 +00:00
|
|
|
|
2007-08-16 17:38:50 +00:00
|
|
|
return $result;
|
|
|
|
}
|
2003-07-12 05:19:18 +00:00
|
|
|
|
2007-08-16 17:38:50 +00:00
|
|
|
function setup_elements(&$mform) {
|
|
|
|
//override to add your own options
|
|
|
|
}
|
2002-10-17 14:03:59 +00:00
|
|
|
|
2007-08-16 17:38:50 +00:00
|
|
|
function setup_preprocessing(&$default_values){
|
|
|
|
//override to add your own options
|
|
|
|
}
|
2004-07-25 14:32:05 +00:00
|
|
|
|
|
|
|
} /// end of class definition
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function resource_add_instance($resource) {
|
|
|
|
global $CFG;
|
2005-02-16 10:40:48 +00:00
|
|
|
|
2006-03-04 12:37:29 +00:00
|
|
|
$resource->type = clean_param($resource->type, PARAM_SAFEDIR); // Just to be safe
|
2004-09-29 03:12:31 +00:00
|
|
|
|
2004-07-25 14:32:05 +00:00
|
|
|
require_once("$CFG->dirroot/mod/resource/type/$resource->type/resource.class.php");
|
2004-07-28 06:56:59 +00:00
|
|
|
$resourceclass = "resource_$resource->type";
|
|
|
|
$res = new $resourceclass();
|
2004-07-25 14:32:05 +00:00
|
|
|
|
|
|
|
return $res->add_instance($resource);
|
|
|
|
}
|
|
|
|
|
|
|
|
function resource_update_instance($resource) {
|
|
|
|
global $CFG;
|
2005-02-16 10:40:48 +00:00
|
|
|
|
2006-03-04 12:37:29 +00:00
|
|
|
$resource->type = clean_param($resource->type, PARAM_SAFEDIR); // Just to be safe
|
2004-09-29 03:12:31 +00:00
|
|
|
|
2004-07-25 14:32:05 +00:00
|
|
|
require_once("$CFG->dirroot/mod/resource/type/$resource->type/resource.class.php");
|
2004-07-28 06:56:59 +00:00
|
|
|
$resourceclass = "resource_$resource->type";
|
|
|
|
$res = new $resourceclass();
|
2004-07-25 14:32:05 +00:00
|
|
|
|
|
|
|
return $res->update_instance($resource);
|
|
|
|
}
|
|
|
|
|
|
|
|
function resource_delete_instance($id) {
|
2008-06-01 21:36:11 +00:00
|
|
|
global $CFG, $DB;
|
2005-02-16 10:40:48 +00:00
|
|
|
|
2008-06-01 21:36:11 +00:00
|
|
|
if (! $resource = $DB->get_record("resource", array("id"=>$id))) {
|
2004-07-25 14:32:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
2004-09-29 03:12:31 +00:00
|
|
|
|
2006-03-04 12:37:29 +00:00
|
|
|
$resource->type = clean_param($resource->type, PARAM_SAFEDIR); // Just to be safe
|
2005-02-16 10:40:48 +00:00
|
|
|
|
2004-07-25 14:32:05 +00:00
|
|
|
require_once("$CFG->dirroot/mod/resource/type/$resource->type/resource.class.php");
|
2004-07-28 06:56:59 +00:00
|
|
|
$resourceclass = "resource_$resource->type";
|
|
|
|
$res = new $resourceclass();
|
2004-07-25 14:32:05 +00:00
|
|
|
|
2005-12-06 00:02:40 +00:00
|
|
|
return $res->delete_instance($resource);
|
2004-07-25 14:32:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-10-17 14:03:59 +00:00
|
|
|
function resource_user_outline($course, $user, $mod, $resource) {
|
2008-06-04 00:04:03 +00:00
|
|
|
global $DB;
|
|
|
|
|
|
|
|
if ($logs = $DB->get_records("log", array('userid'=>$user->id, 'module'=>'resource',
|
|
|
|
'action'=>'view', 'info'=>$resource->id), "time ASC")) {
|
2002-10-17 14:03:59 +00:00
|
|
|
|
|
|
|
$numviews = count($logs);
|
|
|
|
$lastlog = array_pop($logs);
|
|
|
|
|
2007-01-22 18:49:42 +00:00
|
|
|
$result = new object();
|
2002-10-17 14:03:59 +00:00
|
|
|
$result->info = get_string("numviews", "", $numviews);
|
|
|
|
$result->time = $lastlog->time;
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function resource_user_complete($course, $user, $mod, $resource) {
|
2008-06-04 00:04:03 +00:00
|
|
|
global $CFG, $DB;
|
2002-10-17 14:03:59 +00:00
|
|
|
|
2008-06-04 00:04:03 +00:00
|
|
|
if ($logs = $DB->get_records("log", array('userid'=>$user->id, 'module'=>'resource',
|
|
|
|
'action'=>'view', 'info'=>$resource->id), "time ASC")) {
|
2002-10-17 14:03:59 +00:00
|
|
|
$numviews = count($logs);
|
|
|
|
$lastlog = array_pop($logs);
|
|
|
|
|
|
|
|
$strmostrecently = get_string("mostrecently");
|
|
|
|
$strnumviews = get_string("numviews", "", $numviews);
|
|
|
|
|
|
|
|
echo "$strnumviews - $strmostrecently ".userdate($lastlog->time);
|
|
|
|
|
|
|
|
} else {
|
2002-10-17 14:28:12 +00:00
|
|
|
print_string("neverseen", "resource");
|
2002-10-17 14:03:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-07 22:01:47 +00:00
|
|
|
function resource_get_participants($resourceid) {
|
|
|
|
//Returns the users with data in one resource
|
|
|
|
//(NONE, byt must exists on EVERY mod !!)
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2002-10-17 14:03:59 +00:00
|
|
|
|
2003-10-22 13:14:56 +00:00
|
|
|
function resource_get_coursemodule_info($coursemodule) {
|
2005-02-16 10:40:48 +00:00
|
|
|
/// Given a course_module object, this function returns any
|
2003-10-22 13:14:56 +00:00
|
|
|
/// "extra" information that may be needed when printing
|
|
|
|
/// this activity in a course listing.
|
|
|
|
///
|
|
|
|
/// See get_array_of_activities() in course/lib.php
|
|
|
|
///
|
2008-06-04 00:04:03 +00:00
|
|
|
global $CFG, $DB;
|
2004-04-26 15:31:22 +00:00
|
|
|
|
|
|
|
$info = NULL;
|
|
|
|
|
2008-06-04 00:04:03 +00:00
|
|
|
if ($resource = $DB->get_record("resource", array("id"=>$coursemodule->instance), 'id, popup, reference, type, name')) {
|
2008-01-24 20:33:50 +00:00
|
|
|
$info = new object();
|
2008-02-01 08:15:38 +00:00
|
|
|
$info->name = $resource->name;
|
2004-07-26 16:21:56 +00:00
|
|
|
if (!empty($resource->popup)) {
|
2007-01-07 13:49:31 +00:00
|
|
|
$info->extra = urlencode("onclick=\"this.target='resource$resource->id'; return ".
|
2004-09-16 17:13:57 +00:00
|
|
|
"openpopup('/mod/resource/view.php?inpopup=true&id=".
|
2003-10-22 13:14:56 +00:00
|
|
|
$coursemodule->id.
|
2004-07-25 14:32:05 +00:00
|
|
|
"','resource$resource->id','$resource->popup');\"");
|
2003-10-22 13:14:56 +00:00
|
|
|
}
|
2004-04-26 15:31:22 +00:00
|
|
|
|
2005-03-07 11:32:03 +00:00
|
|
|
require_once($CFG->libdir.'/filelib.php');
|
2004-04-26 15:31:22 +00:00
|
|
|
|
2004-07-26 16:21:56 +00:00
|
|
|
if ($resource->type == 'file') {
|
2005-02-16 10:40:48 +00:00
|
|
|
$icon = mimeinfo("icon", $resource->reference);
|
2004-04-26 15:31:22 +00:00
|
|
|
if ($icon != 'unknown.gif') {
|
2005-02-16 10:40:48 +00:00
|
|
|
$info->icon ="f/$icon";
|
2004-07-26 16:21:56 +00:00
|
|
|
} else {
|
2005-02-16 10:40:48 +00:00
|
|
|
$info->icon ="f/web.gif";
|
2004-04-26 15:31:22 +00:00
|
|
|
}
|
2004-07-25 14:32:05 +00:00
|
|
|
} else if ($resource->type == 'directory') {
|
2005-02-16 10:40:48 +00:00
|
|
|
$info->icon ="f/folder.gif";
|
2004-04-26 15:31:22 +00:00
|
|
|
}
|
2003-10-22 13:14:56 +00:00
|
|
|
}
|
|
|
|
|
2004-04-26 15:31:22 +00:00
|
|
|
return $info;
|
2003-10-22 13:14:56 +00:00
|
|
|
}
|
2005-02-16 10:40:48 +00:00
|
|
|
|
2003-11-27 19:09:47 +00:00
|
|
|
function resource_fetch_remote_file ($cm, $url, $headers = "" ) {
|
2003-11-28 02:11:02 +00:00
|
|
|
/// Snoopy is an HTTP client in PHP
|
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
require_once("$CFG->libdir/snoopy/Snoopy.class.inc");
|
|
|
|
|
2003-11-28 02:19:20 +00:00
|
|
|
$client = new Snoopy();
|
2005-02-16 10:40:48 +00:00
|
|
|
$ua = 'Moodle/'. $CFG->release . ' (+http://moodle.org';
|
2003-11-28 03:27:24 +00:00
|
|
|
if ( $CFG->resource_usecache ) {
|
|
|
|
$ua = $ua . ')';
|
|
|
|
} else {
|
|
|
|
$ua = $ua . '; No cache)';
|
|
|
|
}
|
|
|
|
$client->agent = $ua;
|
|
|
|
$client->read_timeout = 5;
|
|
|
|
$client->use_gzip = true;
|
2003-11-28 02:19:20 +00:00
|
|
|
if (is_array($headers) ) {
|
|
|
|
$client->rawheaders = $headers;
|
|
|
|
}
|
2005-02-16 10:40:48 +00:00
|
|
|
|
2003-11-28 02:19:20 +00:00
|
|
|
@$client->fetch($url);
|
2003-11-28 03:27:24 +00:00
|
|
|
if ( $client->status >= 200 && $client->status < 300 ) {
|
|
|
|
$tags = array("A" => "href=",
|
|
|
|
"IMG" => "src=",
|
|
|
|
"LINK" => "href=",
|
|
|
|
"AREA" => "href=",
|
|
|
|
"FRAME" => "src=",
|
|
|
|
"IFRAME" => "src=",
|
|
|
|
"FORM" => "action=");
|
2005-02-16 10:40:48 +00:00
|
|
|
|
2003-11-28 03:27:24 +00:00
|
|
|
foreach ($tags as $tag => $key) {
|
2004-09-16 17:13:57 +00:00
|
|
|
$prefix = "fetch.php?id=$cm->id&url=";
|
2003-11-28 03:27:24 +00:00
|
|
|
if ( $tag == "IMG" or $tag == "LINK" or $tag == "FORM") {
|
|
|
|
$prefix = "";
|
|
|
|
}
|
|
|
|
$client->results = resource_redirect_tags($client->results, $url, $tag, $key,$prefix);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ( $client->status >= 400 && $client->status < 500) {
|
|
|
|
$client->results = get_string("fetchclienterror","resource"); // Client error
|
|
|
|
} elseif ( $client->status >= 500 && $client->status < 600) {
|
|
|
|
$client->results = get_string("fetchservererror","resource"); // Server error
|
|
|
|
} else {
|
|
|
|
$client->results = get_string("fetcherror","resource"); // Redirection? HEAD? Unknown error.
|
2003-11-27 19:04:36 +00:00
|
|
|
}
|
|
|
|
}
|
2003-11-28 02:19:20 +00:00
|
|
|
return $client;
|
2003-11-27 19:04:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function resource_redirect_tags($text, $url, $tagtoparse, $keytoparse,$prefix = "" ) {
|
2003-11-28 03:27:24 +00:00
|
|
|
$valid = 1;
|
2003-11-27 19:04:36 +00:00
|
|
|
if ( strpos($url,"?") == FALSE ) {
|
|
|
|
$valid = 1;
|
|
|
|
}
|
|
|
|
if ( $valid ) {
|
|
|
|
$lastpoint = strrpos($url,".");
|
|
|
|
$lastslash = strrpos($url,"/");
|
|
|
|
if ( $lastpoint > $lastslash ) {
|
|
|
|
$root = substr($url,0,$lastslash+1);
|
|
|
|
} else {
|
|
|
|
$root = $url;
|
|
|
|
}
|
2005-02-16 10:40:48 +00:00
|
|
|
if ( $root == "http://" or
|
2003-11-27 19:04:36 +00:00
|
|
|
$root == "https://") {
|
|
|
|
$root = $url;
|
|
|
|
}
|
|
|
|
if ( substr($root,strlen($root)-1) == '/' ) {
|
|
|
|
$root = substr($root,0,-1);
|
|
|
|
}
|
2005-02-16 10:40:48 +00:00
|
|
|
|
2003-11-27 19:04:36 +00:00
|
|
|
$mainroot = $root;
|
|
|
|
$lastslash = strrpos($mainroot,"/");
|
|
|
|
while ( $lastslash > 9) {
|
|
|
|
$mainroot = substr($mainroot,0,$lastslash);
|
2005-02-16 10:40:48 +00:00
|
|
|
|
2003-11-27 19:04:36 +00:00
|
|
|
$lastslash = strrpos($mainroot,"/");
|
|
|
|
}
|
2003-10-22 13:14:56 +00:00
|
|
|
|
2005-02-16 10:40:48 +00:00
|
|
|
$regex = "/<$tagtoparse (.+?)>/is";
|
|
|
|
$count = preg_match_all($regex, $text, $hrefs);
|
2003-11-27 19:04:36 +00:00
|
|
|
for ( $i = 0; $i < $count; $i++) {
|
|
|
|
$tag = $hrefs[1][$i];
|
2005-02-16 10:40:48 +00:00
|
|
|
|
2003-11-27 19:04:36 +00:00
|
|
|
$poshref = strpos(strtolower($tag),strtolower($keytoparse));
|
|
|
|
$start = $poshref + strlen($keytoparse);
|
|
|
|
$left = substr($tag,0,$start);
|
|
|
|
if ( $tag[$start] == '"' ) {
|
|
|
|
$left .= '"';
|
|
|
|
$start++;
|
|
|
|
}
|
|
|
|
$posspace = strpos($tag," ", $start+1);
|
|
|
|
$right = "";
|
|
|
|
if ( $posspace != FALSE) {
|
|
|
|
$right = substr($tag, $posspace);
|
|
|
|
}
|
|
|
|
$end = strlen($tag)-1;
|
|
|
|
if ( $tag[$end] == '"' ) {
|
|
|
|
$right = '"' . $right;
|
|
|
|
}
|
|
|
|
$finalurl = substr($tag,$start,$end-$start+$diff);
|
|
|
|
// Here, we could have these possible values for $finalurl:
|
|
|
|
// file.ext Add current root dir
|
|
|
|
// http://(domain) don't care
|
|
|
|
// http://(domain)/ don't care
|
|
|
|
// http://(domain)/folder don't care
|
|
|
|
// http://(domain)/folder/ don't care
|
|
|
|
// http://(domain)/folder/file.ext don't care
|
|
|
|
// folder/ Add current root dir
|
|
|
|
// folder/file.ext Add current root dir
|
|
|
|
// /folder/ Add main root dir
|
|
|
|
// /folder/file.ext Add main root dir
|
|
|
|
|
|
|
|
// Special case: If finalurl contains a ?, it won't be parsed
|
2003-11-28 03:27:24 +00:00
|
|
|
$valid = 1;
|
2003-11-27 19:04:36 +00:00
|
|
|
|
|
|
|
if ( strpos($finalurl,"?") == FALSE ) {
|
|
|
|
$valid = 1;
|
|
|
|
}
|
|
|
|
if ( $valid ) {
|
|
|
|
if ( $finalurl[0] == "/" ) {
|
|
|
|
$finalurl = $mainroot . $finalurl;
|
2005-02-16 10:40:48 +00:00
|
|
|
} elseif ( strtolower(substr($finalurl,0,7)) != "http://" and
|
2003-11-27 19:04:36 +00:00
|
|
|
strtolower(substr($finalurl,0,8)) != "https://") {
|
|
|
|
if ( $finalurl[0] == "/") {
|
|
|
|
$finalurl = $mainroot . $finalurl;
|
|
|
|
} else {
|
|
|
|
$finalurl = "$root/$finalurl";
|
|
|
|
}
|
|
|
|
}
|
2005-02-16 10:40:48 +00:00
|
|
|
|
2003-11-27 19:04:36 +00:00
|
|
|
$text = str_replace($tag,"$left$prefix$finalurl$right",$text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $text;
|
|
|
|
}
|
2003-10-22 13:14:56 +00:00
|
|
|
|
2004-07-25 14:32:05 +00:00
|
|
|
function resource_is_url($path) {
|
2004-08-01 06:36:42 +00:00
|
|
|
if (strpos($path, '://')) { // eg http:// https:// ftp:// etc
|
2004-07-25 14:32:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
2004-08-01 06:36:42 +00:00
|
|
|
if (strpos($path, '/') === 0) { // Starts with slash
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2004-07-25 14:32:05 +00:00
|
|
|
}
|
|
|
|
|
2007-01-02 09:33:07 +00:00
|
|
|
function resource_get_types() {
|
2007-01-22 18:49:42 +00:00
|
|
|
global $CFG;
|
|
|
|
|
2007-01-02 09:33:07 +00:00
|
|
|
$types = array();
|
2004-08-02 19:11:15 +00:00
|
|
|
|
2004-08-08 14:33:23 +00:00
|
|
|
$standardresources = array('text','html','file','directory');
|
2004-08-02 19:11:15 +00:00
|
|
|
foreach ($standardresources as $resourcetype) {
|
2007-01-02 09:33:07 +00:00
|
|
|
$type = new object();
|
|
|
|
$type->modclass = MOD_CLASS_RESOURCE;
|
|
|
|
$type->type = "resource&type=$resourcetype";
|
|
|
|
$type->typestr = get_string("resourcetype$resourcetype", 'resource');
|
|
|
|
$types[] = $type;
|
2004-08-02 19:11:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Drop-in extra resource types
|
|
|
|
$resourcetypes = get_list_of_plugins('mod/resource/type');
|
|
|
|
foreach ($resourcetypes as $resourcetype) {
|
2005-07-15 07:22:23 +00:00
|
|
|
if (!empty($CFG->{'resource_hide_'.$resourcetype})) { // Not wanted
|
|
|
|
continue;
|
|
|
|
}
|
2007-01-02 09:33:07 +00:00
|
|
|
if (!in_array($resourcetype, $standardresources)) {
|
|
|
|
$type = new object();
|
|
|
|
$type->modclass = MOD_CLASS_RESOURCE;
|
|
|
|
$type->type = "resource&type=$resourcetype";
|
|
|
|
$type->typestr = get_string("resourcetype$resourcetype", 'resource');
|
|
|
|
$types[] = $type;
|
2004-08-02 19:11:15 +00:00
|
|
|
}
|
|
|
|
}
|
2007-01-02 09:33:07 +00:00
|
|
|
|
|
|
|
return $types;
|
2004-08-02 19:11:15 +00:00
|
|
|
}
|
2005-09-01 04:14:31 +00:00
|
|
|
|
|
|
|
function resource_get_view_actions() {
|
|
|
|
return array('view','view all');
|
|
|
|
}
|
|
|
|
|
|
|
|
function resource_get_post_actions() {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
2006-09-25 14:12:46 +00:00
|
|
|
function resource_renamefiles($course, $wdir, $oldname, $name) {
|
2008-06-04 00:04:03 +00:00
|
|
|
global $CFG, $DB;
|
2006-09-25 14:12:46 +00:00
|
|
|
|
|
|
|
$status = '<p align=\"center\"><strong>'.get_string('affectedresources', 'resource').':</strong><ul>';
|
|
|
|
$updates = false;
|
|
|
|
|
|
|
|
$old = trim($wdir.'/'.$oldname, '/');
|
|
|
|
$new = trim($wdir.'/'.$name, '/');
|
|
|
|
|
|
|
|
$sql = "SELECT r.id, r.reference, r.name, cm.id AS cmid
|
2008-06-04 00:04:03 +00:00
|
|
|
FROM {resource} r, {course_modules} cm, {modules} m
|
|
|
|
WHERE r.course = :courseid
|
|
|
|
AND m.name = 'resource'
|
|
|
|
AND cm.module = m.id
|
|
|
|
AND cm.instance = r.id
|
|
|
|
AND (r.type = 'file' OR r.type = 'directory')
|
|
|
|
AND (r.reference LIKE :old1 OR r.reference = :old2)";
|
|
|
|
$params = array('courseid'=>$course->id, 'old1'=>"{$old}/%", 'old2'=>$old);
|
|
|
|
if ($resources = $DB->get_records_sql($sql, $params)) {
|
2006-09-25 14:12:46 +00:00
|
|
|
foreach ($resources as $resource) {
|
|
|
|
$r = new object();
|
|
|
|
$r->id = $resource->id;
|
|
|
|
$r->reference = '';
|
|
|
|
if ($resource->reference == $old) {
|
2008-06-04 00:04:03 +00:00
|
|
|
$r->reference = $new;
|
2006-09-25 14:12:46 +00:00
|
|
|
} else {
|
2008-06-04 00:04:03 +00:00
|
|
|
$r->reference = preg_replace('|^'.preg_quote($old, '|').'/|', $new.'/', $resource->reference);
|
2006-09-25 14:12:46 +00:00
|
|
|
}
|
|
|
|
if ($r->reference !== '') {
|
|
|
|
$updates = true;
|
|
|
|
$status .= "<li><a href=\"$CFG->wwwroot/mod/resource/view.php?id=$resource->cmid\" target=\"_blank\">$resource->name</a>: $resource->reference ==> $r->reference</li>";
|
|
|
|
if (!empty($CFG->resource_autofilerename)) {
|
2008-06-04 00:04:03 +00:00
|
|
|
if (!$DB->update_record('resource', $r)) {
|
2008-04-04 02:54:20 +00:00
|
|
|
print_error("Error updating resource with ID $r->id.");
|
2006-09-25 14:12:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$status .= '</ul></p>';
|
|
|
|
|
|
|
|
if ($updates) {
|
|
|
|
echo $status;
|
|
|
|
if (empty($CFG->resource_autofilerename)) {
|
|
|
|
notify(get_string('warningdisabledrename', 'resource'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function resource_delete_warning($course, $files) {
|
2008-06-04 00:04:03 +00:00
|
|
|
global $CFG, $DB;
|
2006-09-25 14:12:46 +00:00
|
|
|
|
|
|
|
$found = array();
|
|
|
|
|
|
|
|
foreach($files as $key=>$file) {
|
|
|
|
$files[$key] = trim($file, '/');
|
|
|
|
}
|
|
|
|
$sql = "SELECT r.id, r.reference, r.name, cm.id AS cmid
|
2008-06-04 00:04:03 +00:00
|
|
|
FROM {resource} r,
|
|
|
|
{course_modules} cm,
|
|
|
|
{modules} m
|
|
|
|
WHERE r.course = ?
|
|
|
|
AND m.name = 'resource'
|
|
|
|
AND cm.module = m.id
|
|
|
|
AND cm.instance = r.id
|
|
|
|
AND (r.type = 'file' OR r.type = 'directory')";
|
|
|
|
if ($resources = $DB->get_records_sql($sql, array($course->id))) {
|
2006-09-25 14:12:46 +00:00
|
|
|
foreach ($resources as $resource) {
|
|
|
|
if ($resource->reference == '') {
|
|
|
|
continue; // top shared directory does not prevent anything
|
|
|
|
}
|
|
|
|
if (in_array($resource->reference, $files)) {
|
|
|
|
$found[$resource->id] = $resource;
|
|
|
|
} else {
|
|
|
|
foreach($files as $file) {
|
|
|
|
if (preg_match('|^'.preg_quote($file, '|').'/|', $resource->reference)) {
|
|
|
|
$found[$resource->id] = $resource;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($found)) {
|
|
|
|
|
|
|
|
print_simple_box_start("center");
|
|
|
|
echo '<p><strong>'.get_string('affectedresources', 'resource').':</strong><ul>';
|
|
|
|
foreach($found as $resource) {
|
|
|
|
echo "<li><a href=\"$CFG->wwwroot/mod/resource/view.php?id=$resource->cmid\" target=\"_blank\">$resource->name</a>: $resource->reference</li>";
|
|
|
|
}
|
|
|
|
echo '</ul></p>';
|
|
|
|
print_simple_box_end();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-29 14:43:04 +00:00
|
|
|
/**
|
|
|
|
* This function is used by the reset_course_userdata function in moodlelib.
|
|
|
|
* @param $data the data submitted from the reset course.
|
|
|
|
* @return array status array
|
|
|
|
*/
|
|
|
|
function resource_reset_userdata($data) {
|
|
|
|
return array();
|
|
|
|
}
|
2002-10-17 14:03:59 +00:00
|
|
|
?>
|