moodle/mod/resource/lib.php

283 lines
9.3 KiB
PHP
Raw Normal View History

<?PHP // $Id$
define("REFERENCE", "1");
define("WEBPAGE", "2");
define("UPLOADEDFILE","3");
define("PLAINTEXT", "4");
define("WEBLINK", "5");
define("HTML", "6");
define("PROGRAM", "7");
define("WIKITEXT", "8");
$RESOURCE_TYPE = array (REFERENCE => get_string("resourcetype1", "resource"),
WEBPAGE => get_string("resourcetype2", "resource"),
UPLOADEDFILE => get_string("resourcetype3", "resource"),
PLAINTEXT => get_string("resourcetype4", "resource"),
WEBLINK => get_string("resourcetype5", "resource"),
HTML => get_string("resourcetype6", "resource"),
PROGRAM => get_string("resourcetype7", "resource"),
WIKITEXT => get_string("resourcetype8", "resource") );
if (!isset($CFG->resource_framesize)) {
set_config("resource_framesize", 130);
}
2003-01-05 10:10:05 +00:00
if (!isset($CFG->resource_websearch)) {
set_config("resource_websearch", "http://google.com/");
}
if (!isset($CFG->resource_defaulturl)) {
set_config("resource_defaulturl", "http://");
}
$RESOURCE_WINDOW_OPTIONS = array("resizable", "scrollbars", "directories", "location",
"menubar", "toolbar", "status", "height", "width");
2003-01-05 10:10:05 +00:00
function resource_add_instance($resource) {
// Given an object containing all the necessary data,
// (defined by the form in mod.html) this function
// will create a new instance and return the id number
// of the new instance.
global $RESOURCE_WINDOW_OPTIONS;
$resource->timemodified = time();
if (isset($resource->setnewwindow)) {
$optionlist = array();
foreach ($RESOURCE_WINDOW_OPTIONS as $option) {
if (isset($resource->$option)) {
$optionlist[] = $option."=".$resource->$option;
}
}
$resource->alltext = implode(',', $optionlist);
}
return insert_record("resource", $resource);
}
function resource_update_instance($resource) {
// Given an object containing all the necessary data,
// (defined by the form in mod.html) this function
// will update an existing instance with new data.
global $RESOURCE_WINDOW_OPTIONS;
$resource->id = $resource->instance;
$resource->timemodified = time();
if (isset($resource->setnewwindow)) {
$optionlist = array();
foreach ($RESOURCE_WINDOW_OPTIONS as $option) {
if (isset($resource->$option)) {
$optionlist[] = $option."=".$resource->$option;
}
}
$resource->alltext = implode(',', $optionlist);
}
return update_record("resource", $resource);
}
function resource_delete_instance($id) {
// Given an ID of an instance of this module,
// this function will permanently delete the instance
// and any data that depends on it.
if (! $resource = get_record("resource", "id", "$id")) {
return false;
}
$result = true;
if (! delete_records("resource", "id", "$resource->id")) {
$result = false;
}
return $result;
}
function resource_user_outline($course, $user, $mod, $resource) {
if ($logs = get_records_select("log", "userid='$user->id' AND module='resource'
AND action='view' AND info='$resource->id'", "time ASC")) {
$numviews = count($logs);
$lastlog = array_pop($logs);
$result->info = get_string("numviews", "", $numviews);
$result->time = $lastlog->time;
return $result;
}
return NULL;
}
function resource_user_complete($course, $user, $mod, $resource) {
global $CFG, $THEME;
if ($logs = get_records_select("log", "userid='$user->id' AND module='resource'
AND action='view' AND info='$resource->id'", "time ASC")) {
$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");
}
}
function resource_get_participants($resourceid) {
//Returns the users with data in one resource
//(NONE, byt must exists on EVERY mod !!)
return false;
}
function resource_get_coursemodule_info($coursemodule) {
/// Given a course_module object, this function returns any
/// "extra" information that may be needed when printing
/// this activity in a course listing.
///
/// See get_array_of_activities() in course/lib.php
///
if ($resource = get_record("resource", "id", $coursemodule->instance)) {
if (($resource->type == UPLOADEDFILE or $resource->type == WEBLINK) and !empty($resource->alltext)) {
return urlencode("target=\"resource$resource->id\" onClick=\"return ".
"openpopup('/mod/resource/view.php?inpopup=true&id=".
$coursemodule->id.
"','resource$resource->id','$resource->alltext');\"");
}
}
return false;
}
function resource_fetch_remote_file ($url, $headers = "" ) {
// Snoopy is an HTTP client in PHP
$client = new Snoopy();
$client->agent = MAGPIE_USER_AGENT;
$client->read_timeout = MAGPIE_FETCH_TIME_OUT;
$client->use_gzip = MAGPIE_USE_GZIP;
if (is_array($headers) ) {
$client->rawheaders = $headers;
}
@$client->fetch($url);
$tags = array("A" => "href=",
"IMG" => "src=",
"LINK" => "href=",
"AREA" => "href=",
"FRAME" => "src=",
"IFRAME" => "src=",
"FORM" => "action=");
foreach ($tags as $tag => $key) {
$prefix = "fetch.php?id=$cm->id&url=";
if ( $tag == "IMG" or $tag == "LINK" or $tag == "FORM") {
$prefix = "";
}
$client->results = resource_redirect_tags($client->results, $url, $tag, $key,$prefix);
}
return $client;
}
function resource_redirect_tags($text, $url, $tagtoparse, $keytoparse,$prefix = "" ) {
$valid = 0;
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;
}
if ( $root == "http://" or
$root == "https://") {
$root = $url;
}
if ( substr($root,strlen($root)-1) == '/' ) {
$root = substr($root,0,-1);
}
$mainroot = $root;
$lastslash = strrpos($mainroot,"/");
while ( $lastslash > 9) {
$mainroot = substr($mainroot,0,$lastslash);
$lastslash = strrpos($mainroot,"/");
}
$regex = "/<$tagtoparse (.+?)>/is";
$count = preg_match_all($regex, $text, $hrefs);
for ( $i = 0; $i < $count; $i++) {
$tag = $hrefs[1][$i];
$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
$valid = 0;
if ( strpos($finalurl,"?") == FALSE ) {
$valid = 1;
}
if ( $valid ) {
if ( $finalurl[0] == "/" ) {
$finalurl = $mainroot . $finalurl;
} elseif ( strtolower(substr($finalurl,0,7)) != "http://" and
strtolower(substr($finalurl,0,8)) != "https://") {
if ( $finalurl[0] == "/") {
$finalurl = $mainroot . $finalurl;
} else {
$finalurl = "$root/$finalurl";
}
}
$text = str_replace($tag,"$left$prefix$finalurl$right",$text);
}
}
}
return $text;
}
?>