2002-10-17 14:03:59 +00:00
|
|
|
<?PHP // $Id$
|
|
|
|
|
|
|
|
define("REFERENCE", "1");
|
|
|
|
define("WEBPAGE", "2");
|
|
|
|
define("UPLOADEDFILE","3");
|
|
|
|
define("PLAINTEXT", "4");
|
|
|
|
define("WEBLINK", "5");
|
|
|
|
define("HTML", "6");
|
2002-12-24 07:01:03 +00:00
|
|
|
define("PROGRAM", "7");
|
2003-05-01 03:43:16 +00:00
|
|
|
define("WIKITEXT", "8");
|
2002-10-17 14:03:59 +00:00
|
|
|
|
|
|
|
$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"),
|
2002-12-24 07:01:03 +00:00
|
|
|
HTML => get_string("resourcetype6", "resource"),
|
2003-05-01 03:43:16 +00:00
|
|
|
PROGRAM => get_string("resourcetype7", "resource"),
|
|
|
|
WIKITEXT => get_string("resourcetype8", "resource") );
|
2002-10-17 14:03:59 +00:00
|
|
|
|
2003-08-18 17:46:51 +00:00
|
|
|
if (!isset($CFG->resource_framesize)) {
|
|
|
|
set_config("resource_framesize", 130);
|
|
|
|
}
|
2003-01-05 10:10:05 +00:00
|
|
|
|
2003-11-25 08:01:52 +00:00
|
|
|
if (!isset($CFG->resource_websearch)) {
|
|
|
|
set_config("resource_websearch", "http://google.com/");
|
|
|
|
}
|
|
|
|
|
2003-11-26 11:47:51 +00:00
|
|
|
if (!isset($CFG->resource_defaulturl)) {
|
|
|
|
set_config("resource_defaulturl", "http://");
|
|
|
|
}
|
|
|
|
|
2003-07-20 13:53:31 +00:00
|
|
|
$RESOURCE_WINDOW_OPTIONS = array("resizable", "scrollbars", "directories", "location",
|
|
|
|
"menubar", "toolbar", "status", "height", "width");
|
2003-01-05 10:10:05 +00:00
|
|
|
|
2003-07-12 05:19:18 +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.
|
2002-10-17 14:03:59 +00:00
|
|
|
|
2003-07-20 13:53:31 +00:00
|
|
|
global $RESOURCE_WINDOW_OPTIONS;
|
|
|
|
|
2003-07-12 05:19:18 +00:00
|
|
|
$resource->timemodified = time();
|
2002-10-17 14:03:59 +00:00
|
|
|
|
2003-07-20 13:53:31 +00:00
|
|
|
if (isset($resource->setnewwindow)) {
|
|
|
|
$optionlist = array();
|
|
|
|
foreach ($RESOURCE_WINDOW_OPTIONS as $option) {
|
|
|
|
if (isset($resource->$option)) {
|
|
|
|
$optionlist[] = $option."=".$resource->$option;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$resource->alltext = implode(',', $optionlist);
|
|
|
|
}
|
|
|
|
|
2003-07-12 05:19:18 +00:00
|
|
|
return insert_record("resource", $resource);
|
|
|
|
}
|
2002-10-17 14:03:59 +00:00
|
|
|
|
2003-07-12 05:19:18 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
2003-07-20 13:53:31 +00:00
|
|
|
global $RESOURCE_WINDOW_OPTIONS;
|
|
|
|
|
2003-07-12 05:19:18 +00:00
|
|
|
$resource->id = $resource->instance;
|
|
|
|
$resource->timemodified = time();
|
|
|
|
|
2003-07-20 13:53:31 +00:00
|
|
|
if (isset($resource->setnewwindow)) {
|
|
|
|
$optionlist = array();
|
|
|
|
foreach ($RESOURCE_WINDOW_OPTIONS as $option) {
|
|
|
|
if (isset($resource->$option)) {
|
|
|
|
$optionlist[] = $option."=".$resource->$option;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$resource->alltext = implode(',', $optionlist);
|
|
|
|
}
|
|
|
|
|
2003-07-12 05:19:18 +00:00
|
|
|
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;
|
2002-10-17 14:03:59 +00:00
|
|
|
}
|
|
|
|
|
2003-07-12 05:19:18 +00:00
|
|
|
$result = true;
|
|
|
|
|
|
|
|
if (! delete_records("resource", "id", "$resource->id")) {
|
|
|
|
$result = false;
|
2002-10-17 14:03:59 +00:00
|
|
|
}
|
|
|
|
|
2003-07-12 05:19:18 +00:00
|
|
|
return $result;
|
2002-10-17 14:03:59 +00:00
|
|
|
}
|
2003-07-12 05:19:18 +00:00
|
|
|
|
2002-10-17 14:03:59 +00:00
|
|
|
|
|
|
|
function resource_user_outline($course, $user, $mod, $resource) {
|
2002-12-23 09:39:26 +00:00
|
|
|
if ($logs = get_records_select("log", "userid='$user->id' AND module='resource'
|
|
|
|
AND action='view' AND info='$resource->id'", "time ASC")) {
|
2002-10-17 14:03:59 +00:00
|
|
|
|
|
|
|
$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;
|
|
|
|
|
2002-12-23 09:39:26 +00:00
|
|
|
if ($logs = get_records_select("log", "userid='$user->id' AND module='resource'
|
|
|
|
AND action='view' AND 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) {
|
|
|
|
/// 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;
|
|
|
|
}
|
2003-11-27 19:04:36 +00:00
|
|
|
|
|
|
|
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,"/");
|
|
|
|
}
|
2003-10-22 13:14:56 +00:00
|
|
|
|
2003-11-27 19:04:36 +00:00
|
|
|
$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;
|
|
|
|
}
|
2003-10-22 13:14:56 +00:00
|
|
|
|
2002-10-17 14:03:59 +00:00
|
|
|
?>
|