3 if (!isset($CFG->resource_framesize
)) {
4 set_config("resource_framesize", 130);
7 if (!isset($CFG->resource_websearch
)) {
8 set_config("resource_websearch", "http://google.com/");
11 if (!isset($CFG->resource_defaulturl
)) {
12 set_config("resource_defaulturl", "http://");
15 if (!isset($CFG->resource_filterexternalpages
)) {
16 set_config("resource_filterexternalpages", false);
19 if (!isset($CFG->resource_secretphrase
)) {
20 set_config("resource_secretphrase", random_string(20));
23 if (!isset($CFG->resource_popup
)) {
24 set_config("resource_popup", "");
27 if (!isset($CFG->resource_windowsettings
)) {
28 set_config("resource_windowsettings", "0");
31 if (!isset($CFG->resource_parametersettings
)) {
32 set_config("resource_parametersettings", "0");
35 if (!isset($CFG->resource_allowlocalfiles
)) {
36 set_config("resource_allowlocalfiles", "0");
39 if (!isset($CFG->resource_hide_repository
)) {
40 set_config("resource_hide_repository", "1");
43 if (!isset($CFG->resource_autofilerename
)) {
44 set_config("resource_autofilerename", "1");
47 if (!isset($CFG->resource_blockdeletingfile
)) {
48 set_config("resource_blockdeletingfile", "1");
51 define('RESOURCE_LOCALPATH', 'LOCALPATH');
53 $RESOURCE_WINDOW_OPTIONS = array('resizable', 'scrollbars', 'directories', 'location',
54 'menubar', 'toolbar', 'status', 'width', 'height');
56 foreach ($RESOURCE_WINDOW_OPTIONS as $popupoption) {
57 $popupoption = "resource_popup$popupoption";
58 if (!isset($CFG->$popupoption)) {
59 if ($popupoption == 'resource_popupheight') {
60 set_config($popupoption, 450);
61 } else if ($popupoption == 'resource_popupwidth') {
62 set_config($popupoption, 620);
64 set_config($popupoption, 'checked');
69 if (!empty($THEME->customcorners
)) {
70 require_once($CFG->dirroot
.'/lib/custom_corners_lib.php');
74 * resource_base is the base class for resource types
76 * This class provides all the functionality for a resource
87 * Constructor for the base resource class
89 * Constructor for the base resource class.
90 * If cmid is set create the cm, course, resource objects.
91 * and do some checks to make sure people can be here, and so on.
93 * @param cmid integer, the current course module id - not set for new resources
95 function resource_base($cmid=0) {
100 if (! $this->cm
= get_coursemodule_from_id('resource', $cmid)) {
101 error("Course Module ID was incorrect");
104 if (! $this->course
= get_record("course", "id", $this->cm
->course
)) {
105 error("Course is misconfigured");
108 if (! $this->resource = get_record("resource", "id", $this->cm
->instance
)) {
109 error("Resource ID was incorrect");
112 $this->strresource
= get_string("modulename", "resource");
113 $this->strresources
= get_string("modulenameplural", "resource");
115 $this->navlinks
[] = array('name' => $this->strresources
, 'link' => "index.php?id={$this->course->id}", 'type' => 'activity');
117 if (!$this->cm
->visible
and !has_capability('moodle/course:viewhiddenactivities', get_context_instance(CONTEXT_MODULE
, $this->cm
->id
))) {
118 $pagetitle = strip_tags($this->course
->shortname
.': '.$this->strresource
);
119 $this->navlinks
[] = array('name' => $this->strresource
, 'link' => '', 'type' => 'activityinstance');
120 $this->navigation
= build_navigation($this->navlinks
);
122 print_header($pagetitle, $this->course
->fullname
, $this->navigation
, "", "", true, '', navmenu($this->course
, $this->cm
));
123 notice(get_string("activityiscurrentlyhidden"), "$CFG->wwwroot/course/view.php?id={$this->course->id}");
127 $this->course
= $COURSE;
133 * Display function does nothing in the base class
141 * Display the resource with the course blocks.
143 function display_course_blocks_start() {
148 require_once($CFG->libdir
.'/blocklib.php');
149 require_once($CFG->libdir
.'/pagelib.php');
150 require_once($CFG->dirroot
.'/course/lib.php'); //required by some blocks
152 $PAGE = page_create_object(PAGE_COURSE_VIEW
, $this->course
->id
);
154 $pageblocks = blocks_setup($PAGE);
156 $blocks_preferred_width = bounded_number(180, blocks_preferred_width($pageblocks[BLOCK_POS_LEFT
]), 210);
158 /// Print the page header
160 $edit = optional_param('edit', -1, PARAM_BOOL
);
162 if (($edit != -1) and $PAGE->user_allowed_editing()) {
163 $USER->editing
= $edit;
166 $morenavlinks = array($this->strresources
=> 'index.php?id='.$this->course
->id
,
167 $this->resource->name
=> '');
169 $PAGE->print_header($this->course
->shortname
.': %fullname%', $morenavlinks);
171 echo '<table id="layout-table"><tr>';
173 if((blocks_have_content($pageblocks, BLOCK_POS_LEFT
) ||
$PAGE->user_is_editing())) {
174 echo '<td style="width: '.$blocks_preferred_width.'px;" id="left-column">';
175 if (!empty($THEME->customcorners
)) print_custom_corners_start();
176 blocks_print_group($PAGE, $pageblocks, BLOCK_POS_LEFT
);
177 if (!empty($THEME->customcorners
)) print_custom_corners_end();
181 echo '<td id="middle-column">';
182 if (!empty($THEME->customcorners
)) print_custom_corners_start();
183 echo '<div id="resource">';
189 * Finish displaying the resource with the course blocks
191 function display_course_blocks_end() {
196 $pageblocks = blocks_setup($PAGE);
197 $blocks_preferred_width = bounded_number(180, blocks_preferred_width($pageblocks[BLOCK_POS_RIGHT
]), 210);
200 if (!empty($THEME->customcorners
)) print_custom_corners_end();
203 if((blocks_have_content($pageblocks, BLOCK_POS_RIGHT
) ||
$PAGE->user_is_editing())) {
204 echo '<td style="width: '.$blocks_preferred_width.'px;" id="right-column">';
205 if (!empty($THEME->customcorners
)) print_custom_corners_start();
206 blocks_print_group($PAGE, $pageblocks, BLOCK_POS_RIGHT
);
207 if (!empty($THEME->customcorners
)) print_custom_corners_end();
211 echo '</tr></table>';
213 print_footer($this->course
);
218 function add_instance($resource) {
219 // Given an object containing all the necessary data,
220 // (defined by the form in mod.html) this function
221 // will create a new instance and return the id number
222 // of the new instance.
224 $resource->timemodified
= time();
226 return insert_record("resource", $resource);
230 function update_instance($resource) {
231 // Given an object containing all the necessary data,
232 // (defined by the form in mod.html) this function
233 // will update an existing instance with new data.
235 $resource->id
= $resource->instance
;
236 $resource->timemodified
= time();
238 return update_record("resource", $resource);
242 function delete_instance($resource) {
243 // Given an object containing the resource data
244 // this function will permanently delete the instance
245 // and any data that depends on it.
249 if (! delete_records("resource", "id", "$resource->id")) {
256 function setup_elements(&$mform) {
257 //override to add your own options
260 function setup_preprocessing(&$default_values){
261 //override to add your own options
264 } /// end of class definition
268 function resource_add_instance($resource) {
271 $resource->type
= clean_param($resource->type
, PARAM_SAFEDIR
); // Just to be safe
273 require_once("$CFG->dirroot/mod/resource/type/$resource->type/resource.class.php");
274 $resourceclass = "resource_$resource->type";
275 $res = new $resourceclass();
277 return $res->add_instance($resource);
280 function resource_update_instance($resource) {
283 $resource->type
= clean_param($resource->type
, PARAM_SAFEDIR
); // Just to be safe
285 require_once("$CFG->dirroot/mod/resource/type/$resource->type/resource.class.php");
286 $resourceclass = "resource_$resource->type";
287 $res = new $resourceclass();
289 return $res->update_instance($resource);
292 function resource_delete_instance($id) {
295 if (! $resource = get_record("resource", "id", "$id")) {
299 $resource->type
= clean_param($resource->type
, PARAM_SAFEDIR
); // Just to be safe
301 require_once("$CFG->dirroot/mod/resource/type/$resource->type/resource.class.php");
302 $resourceclass = "resource_$resource->type";
303 $res = new $resourceclass();
305 return $res->delete_instance($resource);
309 function resource_user_outline($course, $user, $mod, $resource) {
310 if ($logs = get_records_select("log", "userid='$user->id' AND module='resource'
311 AND action='view' AND info='$resource->id'", "time ASC")) {
313 $numviews = count($logs);
314 $lastlog = array_pop($logs);
316 $result = new object();
317 $result->info
= get_string("numviews", "", $numviews);
318 $result->time
= $lastlog->time
;
326 function resource_user_complete($course, $user, $mod, $resource) {
329 if ($logs = get_records_select("log", "userid='$user->id' AND module='resource'
330 AND action='view' AND info='$resource->id'", "time ASC")) {
331 $numviews = count($logs);
332 $lastlog = array_pop($logs);
334 $strmostrecently = get_string("mostrecently");
335 $strnumviews = get_string("numviews", "", $numviews);
337 echo "$strnumviews - $strmostrecently ".userdate($lastlog->time
);
340 print_string("neverseen", "resource");
344 function resource_get_participants($resourceid) {
345 //Returns the users with data in one resource
346 //(NONE, byt must exists on EVERY mod !!)
351 function resource_get_coursemodule_info($coursemodule) {
352 /// Given a course_module object, this function returns any
353 /// "extra" information that may be needed when printing
354 /// this activity in a course listing.
356 /// See get_array_of_activities() in course/lib.php
363 if ($resource = get_record("resource", "id", $coursemodule->instance
)) {
364 if (!empty($resource->popup
)) {
365 $info->extra
= urlencode("onclick=\"this.target='resource$resource->id'; return ".
366 "openpopup('/mod/resource/view.php?inpopup=true&id=".
368 "','resource$resource->id','$resource->popup');\"");
371 require_once($CFG->libdir
.'/filelib.php');
373 if ($resource->type
== 'file') {
374 $icon = mimeinfo("icon", $resource->reference
);
375 if ($icon != 'unknown.gif') {
376 $info->icon
="f/$icon";
378 $info->icon
="f/web.gif";
380 } else if ($resource->type
== 'directory') {
381 $info->icon
="f/folder.gif";
388 function resource_fetch_remote_file ($cm, $url, $headers = "" ) {
389 /// Snoopy is an HTTP client in PHP
393 require_once("$CFG->libdir/snoopy/Snoopy.class.inc");
395 $client = new Snoopy();
396 $ua = 'Moodle/'. $CFG->release
. ' (+http://moodle.org';
397 if ( $CFG->resource_usecache
) {
400 $ua = $ua . '; No cache)';
402 $client->agent
= $ua;
403 $client->read_timeout
= 5;
404 $client->use_gzip
= true;
405 if (is_array($headers) ) {
406 $client->rawheaders
= $headers;
409 @$client->fetch($url);
410 if ( $client->status
>= 200 && $client->status
< 300 ) {
411 $tags = array("A" => "href=",
417 "FORM" => "action=");
419 foreach ($tags as $tag => $key) {
420 $prefix = "fetch.php?id=$cm->id&url=";
421 if ( $tag == "IMG" or $tag == "LINK" or $tag == "FORM") {
424 $client->results
= resource_redirect_tags($client->results
, $url, $tag, $key,$prefix);
427 if ( $client->status
>= 400 && $client->status
< 500) {
428 $client->results
= get_string("fetchclienterror","resource"); // Client error
429 } elseif ( $client->status
>= 500 && $client->status
< 600) {
430 $client->results
= get_string("fetchservererror","resource"); // Server error
432 $client->results
= get_string("fetcherror","resource"); // Redirection? HEAD? Unknown error.
438 function resource_redirect_tags($text, $url, $tagtoparse, $keytoparse,$prefix = "" ) {
440 if ( strpos($url,"?") == FALSE ) {
444 $lastpoint = strrpos($url,".");
445 $lastslash = strrpos($url,"/");
446 if ( $lastpoint > $lastslash ) {
447 $root = substr($url,0,$lastslash+
1);
451 if ( $root == "http://" or
452 $root == "https://") {
455 if ( substr($root,strlen($root)-1) == '/' ) {
456 $root = substr($root,0,-1);
460 $lastslash = strrpos($mainroot,"/");
461 while ( $lastslash > 9) {
462 $mainroot = substr($mainroot,0,$lastslash);
464 $lastslash = strrpos($mainroot,"/");
467 $regex = "/<$tagtoparse (.+?)>/is";
468 $count = preg_match_all($regex, $text, $hrefs);
469 for ( $i = 0; $i < $count; $i++
) {
470 $tag = $hrefs[1][$i];
472 $poshref = strpos(strtolower($tag),strtolower($keytoparse));
473 $start = $poshref +
strlen($keytoparse);
474 $left = substr($tag,0,$start);
475 if ( $tag[$start] == '"' ) {
479 $posspace = strpos($tag," ", $start+
1);
481 if ( $posspace != FALSE) {
482 $right = substr($tag, $posspace);
484 $end = strlen($tag)-1;
485 if ( $tag[$end] == '"' ) {
486 $right = '"' . $right;
488 $finalurl = substr($tag,$start,$end-$start+
$diff);
489 // Here, we could have these possible values for $finalurl:
490 // file.ext Add current root dir
491 // http://(domain) don't care
492 // http://(domain)/ don't care
493 // http://(domain)/folder don't care
494 // http://(domain)/folder/ don't care
495 // http://(domain)/folder/file.ext don't care
496 // folder/ Add current root dir
497 // folder/file.ext Add current root dir
498 // /folder/ Add main root dir
499 // /folder/file.ext Add main root dir
501 // Special case: If finalurl contains a ?, it won't be parsed
504 if ( strpos($finalurl,"?") == FALSE ) {
508 if ( $finalurl[0] == "/" ) {
509 $finalurl = $mainroot . $finalurl;
510 } elseif ( strtolower(substr($finalurl,0,7)) != "http://" and
511 strtolower(substr($finalurl,0,8)) != "https://") {
512 if ( $finalurl[0] == "/") {
513 $finalurl = $mainroot . $finalurl;
515 $finalurl = "$root/$finalurl";
519 $text = str_replace($tag,"$left$prefix$finalurl$right",$text);
526 function resource_is_url($path) {
527 if (strpos($path, '://')) { // eg http:// https:// ftp:// etc
530 if (strpos($path, '/') === 0) { // Starts with slash
536 function resource_get_types() {
541 $standardresources = array('text','html','file','directory');
542 foreach ($standardresources as $resourcetype) {
543 $type = new object();
544 $type->modclass
= MOD_CLASS_RESOURCE
;
545 $type->type
= "resource&type=$resourcetype";
546 $type->typestr
= get_string("resourcetype$resourcetype", 'resource');
550 /// Drop-in extra resource types
551 $resourcetypes = get_list_of_plugins('mod/resource/type');
552 foreach ($resourcetypes as $resourcetype) {
553 if (!empty($CFG->{'resource_hide_'.$resourcetype})) { // Not wanted
556 if (!in_array($resourcetype, $standardresources)) {
557 $type = new object();
558 $type->modclass
= MOD_CLASS_RESOURCE
;
559 $type->type
= "resource&type=$resourcetype";
560 $type->typestr
= get_string("resourcetype$resourcetype", 'resource');
568 function resource_get_view_actions() {
569 return array('view','view all');
572 function resource_get_post_actions() {
576 function resource_renamefiles($course, $wdir, $oldname, $name) {
579 $status = '<p align=\"center\"><strong>'.get_string('affectedresources', 'resource').':</strong><ul>';
582 $old = trim($wdir.'/'.$oldname, '/');
583 $new = trim($wdir.'/'.$name, '/');
585 $sql = "SELECT r.id, r.reference, r.name, cm.id AS cmid
586 FROM {$CFG->prefix}resource r,
587 {$CFG->prefix}course_modules cm,
588 {$CFG->prefix}modules m
589 WHERE r.course = '{$course->id}'
590 AND m.name = 'resource'
592 AND cm.instance = r.id
593 AND (r.type = 'file' OR r.type = 'directory')
594 AND (r.reference LIKE '{$old}/%' OR r.reference = '{$old}')";
595 if ($resources = get_records_sql($sql)) {
596 foreach ($resources as $resource) {
598 $r->id
= $resource->id
;
600 if ($resource->reference
== $old) {
601 $r->reference
= addslashes($new);
603 $r->reference
= addslashes(preg_replace('|^'.preg_quote($old, '|').'/|', $new.'/', $resource->reference
));
605 if ($r->reference
!== '') {
607 $status .= "<li><a href=\"$CFG->wwwroot/mod/resource/view.php?id=$resource->cmid\" target=\"_blank\">$resource->name</a>: $resource->reference ==> $r->reference</li>";
608 if (!empty($CFG->resource_autofilerename
)) {
609 if (!update_record('resource', $r)) {
610 error("Error updating resource with ID $r->id.");
616 $status .= '</ul></p>';
620 if (empty($CFG->resource_autofilerename
)) {
621 notify(get_string('warningdisabledrename', 'resource'));
626 function resource_delete_warning($course, $files) {
631 foreach($files as $key=>$file) {
632 $files[$key] = trim($file, '/');
634 $sql = "SELECT r.id, r.reference, r.name, cm.id AS cmid
635 FROM {$CFG->prefix}resource r,
636 {$CFG->prefix}course_modules cm,
637 {$CFG->prefix}modules m
638 WHERE r.course = '{$course->id}'
639 AND m.name = 'resource'
641 AND cm.instance = r.id
642 AND (r.type = 'file' OR r.type = 'directory')";
643 if ($resources = get_records_sql($sql)) {
644 foreach ($resources as $resource) {
645 if ($resource->reference
== '') {
646 continue; // top shared directory does not prevent anything
648 if (in_array($resource->reference
, $files)) {
649 $found[$resource->id
] = $resource;
651 foreach($files as $file) {
652 if (preg_match('|^'.preg_quote($file, '|').'/|', $resource->reference
)) {
653 $found[$resource->id
] = $resource;
660 if (!empty($found)) {
662 print_simple_box_start("center");
663 echo '<p><strong>'.get_string('affectedresources', 'resource').':</strong><ul>';
664 foreach($found as $resource) {
665 echo "<li><a href=\"$CFG->wwwroot/mod/resource/view.php?id=$resource->cmid\" target=\"_blank\">$resource->name</a>: $resource->reference</li>";
668 print_simple_box_end();