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
88 * Constructor for the base resource class
90 * Constructor for the base resource class.
91 * If cmid is set create the cm, course, resource objects.
92 * and do some checks to make sure people can be here, and so on.
94 * @param cmid integer, the current course module id - not set for new resources
96 function resource_base($cmid=0) {
101 if (! $this->cm
= get_coursemodule_from_id('resource', $cmid)) {
102 error("Course Module ID was incorrect");
105 if (! $this->course
= get_record("course", "id", $this->cm
->course
)) {
106 error("Course is misconfigured");
109 if (! $this->resource = get_record("resource", "id", $this->cm
->instance
)) {
110 error("Resource ID was incorrect");
113 $this->strresource
= get_string("modulename", "resource");
114 $this->strresources
= get_string("modulenameplural", "resource");
116 $this->navlinks
[] = array('name' => $this->strresources
, 'link' => "index.php?id={$this->course->id}", 'type' => 'activity');
118 if (!$this->cm
->visible
and !has_capability('moodle/course:viewhiddenactivities', get_context_instance(CONTEXT_MODULE
, $this->cm
->id
))) {
119 $pagetitle = strip_tags($this->course
->shortname
.': '.$this->strresource
);
120 $this->navlinks
[] = array('name' => $this->strresource
, 'link' => '', 'type' => 'activityinstance');
121 $this->navigation
= build_navigation($this->navlinks
);
123 print_header($pagetitle, $this->course
->fullname
, $this->navigation
, "", "", true, '', navmenu($this->course
, $this->cm
));
124 notice(get_string("activityiscurrentlyhidden"), "$CFG->wwwroot/course/view.php?id={$this->course->id}");
128 $this->course
= $COURSE;
134 * Display function does nothing in the base class
142 * Display the resource with the course blocks.
144 function display_course_blocks_start() {
149 require_once($CFG->libdir
.'/blocklib.php');
150 require_once($CFG->libdir
.'/pagelib.php');
151 require_once($CFG->dirroot
.'/course/lib.php'); //required by some blocks
153 $PAGE = page_create_object(PAGE_COURSE_VIEW
, $this->course
->id
);
155 $pageblocks = blocks_setup($PAGE);
157 $blocks_preferred_width = bounded_number(180, blocks_preferred_width($pageblocks[BLOCK_POS_LEFT
]), 210);
159 /// Print the page header
161 $edit = optional_param('edit', -1, PARAM_BOOL
);
163 if (($edit != -1) and $PAGE->user_allowed_editing()) {
164 $USER->editing
= $edit;
167 $morenavlinks = array($this->strresources
=> 'index.php?id='.$this->course
->id
,
168 $this->resource->name
=> '');
170 $PAGE->print_header($this->course
->shortname
.': %fullname%', $morenavlinks);
172 echo '<table id="layout-table"><tr>';
174 if((blocks_have_content($pageblocks, BLOCK_POS_LEFT
) ||
$PAGE->user_is_editing())) {
175 echo '<td style="width: '.$blocks_preferred_width.'px;" id="left-column">';
176 if (!empty($THEME->customcorners
)) print_custom_corners_start();
177 blocks_print_group($PAGE, $pageblocks, BLOCK_POS_LEFT
);
178 if (!empty($THEME->customcorners
)) print_custom_corners_end();
182 echo '<td id="middle-column">';
183 if (!empty($THEME->customcorners
)) print_custom_corners_start();
184 echo '<div id="resource">';
190 * Finish displaying the resource with the course blocks
192 function display_course_blocks_end() {
197 $pageblocks = blocks_setup($PAGE);
198 $blocks_preferred_width = bounded_number(180, blocks_preferred_width($pageblocks[BLOCK_POS_RIGHT
]), 210);
201 if (!empty($THEME->customcorners
)) print_custom_corners_end();
204 if((blocks_have_content($pageblocks, BLOCK_POS_RIGHT
) ||
$PAGE->user_is_editing())) {
205 echo '<td style="width: '.$blocks_preferred_width.'px;" id="right-column">';
206 if (!empty($THEME->customcorners
)) print_custom_corners_start();
207 blocks_print_group($PAGE, $pageblocks, BLOCK_POS_RIGHT
);
208 if (!empty($THEME->customcorners
)) print_custom_corners_end();
212 echo '</tr></table>';
214 print_footer($this->course
);
219 function add_instance($resource) {
220 // Given an object containing all the necessary data,
221 // (defined by the form in mod.html) this function
222 // will create a new instance and return the id number
223 // of the new instance.
225 $resource->timemodified
= time();
227 return insert_record("resource", $resource);
231 function update_instance($resource) {
232 // Given an object containing all the necessary data,
233 // (defined by the form in mod.html) this function
234 // will update an existing instance with new data.
236 $resource->id
= $resource->instance
;
237 $resource->timemodified
= time();
239 return update_record("resource", $resource);
243 function delete_instance($resource) {
244 // Given an object containing the resource data
245 // this function will permanently delete the instance
246 // and any data that depends on it.
250 if (! delete_records("resource", "id", "$resource->id")) {
257 function setup_elements(&$mform) {
258 //override to add your own options
261 function setup_preprocessing(&$default_values){
262 //override to add your own options
265 } /// end of class definition
269 function resource_add_instance($resource) {
272 $resource->type
= clean_param($resource->type
, PARAM_SAFEDIR
); // Just to be safe
274 require_once("$CFG->dirroot/mod/resource/type/$resource->type/resource.class.php");
275 $resourceclass = "resource_$resource->type";
276 $res = new $resourceclass();
278 return $res->add_instance($resource);
281 function resource_update_instance($resource) {
284 $resource->type
= clean_param($resource->type
, PARAM_SAFEDIR
); // Just to be safe
286 require_once("$CFG->dirroot/mod/resource/type/$resource->type/resource.class.php");
287 $resourceclass = "resource_$resource->type";
288 $res = new $resourceclass();
290 return $res->update_instance($resource);
293 function resource_delete_instance($id) {
296 if (! $resource = get_record("resource", "id", "$id")) {
300 $resource->type
= clean_param($resource->type
, PARAM_SAFEDIR
); // Just to be safe
302 require_once("$CFG->dirroot/mod/resource/type/$resource->type/resource.class.php");
303 $resourceclass = "resource_$resource->type";
304 $res = new $resourceclass();
306 return $res->delete_instance($resource);
310 function resource_user_outline($course, $user, $mod, $resource) {
311 if ($logs = get_records_select("log", "userid='$user->id' AND module='resource'
312 AND action='view' AND info='$resource->id'", "time ASC")) {
314 $numviews = count($logs);
315 $lastlog = array_pop($logs);
317 $result = new object();
318 $result->info
= get_string("numviews", "", $numviews);
319 $result->time
= $lastlog->time
;
327 function resource_user_complete($course, $user, $mod, $resource) {
330 if ($logs = get_records_select("log", "userid='$user->id' AND module='resource'
331 AND action='view' AND info='$resource->id'", "time ASC")) {
332 $numviews = count($logs);
333 $lastlog = array_pop($logs);
335 $strmostrecently = get_string("mostrecently");
336 $strnumviews = get_string("numviews", "", $numviews);
338 echo "$strnumviews - $strmostrecently ".userdate($lastlog->time
);
341 print_string("neverseen", "resource");
345 function resource_get_participants($resourceid) {
346 //Returns the users with data in one resource
347 //(NONE, byt must exists on EVERY mod !!)
352 function resource_get_coursemodule_info($coursemodule) {
353 /// Given a course_module object, this function returns any
354 /// "extra" information that may be needed when printing
355 /// this activity in a course listing.
357 /// See get_array_of_activities() in course/lib.php
364 if ($resource = get_record("resource", "id", $coursemodule->instance
)) {
365 if (!empty($resource->popup
)) {
366 $info->extra
= urlencode("onclick=\"this.target='resource$resource->id'; return ".
367 "openpopup('/mod/resource/view.php?inpopup=true&id=".
369 "','resource$resource->id','$resource->popup');\"");
372 require_once($CFG->libdir
.'/filelib.php');
374 if ($resource->type
== 'file') {
375 $icon = mimeinfo("icon", $resource->reference
);
376 if ($icon != 'unknown.gif') {
377 $info->icon
="f/$icon";
379 $info->icon
="f/web.gif";
381 } else if ($resource->type
== 'directory') {
382 $info->icon
="f/folder.gif";
389 function resource_fetch_remote_file ($cm, $url, $headers = "" ) {
390 /// Snoopy is an HTTP client in PHP
394 require_once("$CFG->libdir/snoopy/Snoopy.class.inc");
396 $client = new Snoopy();
397 $ua = 'Moodle/'. $CFG->release
. ' (+http://moodle.org';
398 if ( $CFG->resource_usecache
) {
401 $ua = $ua . '; No cache)';
403 $client->agent
= $ua;
404 $client->read_timeout
= 5;
405 $client->use_gzip
= true;
406 if (is_array($headers) ) {
407 $client->rawheaders
= $headers;
410 @$client->fetch($url);
411 if ( $client->status
>= 200 && $client->status
< 300 ) {
412 $tags = array("A" => "href=",
418 "FORM" => "action=");
420 foreach ($tags as $tag => $key) {
421 $prefix = "fetch.php?id=$cm->id&url=";
422 if ( $tag == "IMG" or $tag == "LINK" or $tag == "FORM") {
425 $client->results
= resource_redirect_tags($client->results
, $url, $tag, $key,$prefix);
428 if ( $client->status
>= 400 && $client->status
< 500) {
429 $client->results
= get_string("fetchclienterror","resource"); // Client error
430 } elseif ( $client->status
>= 500 && $client->status
< 600) {
431 $client->results
= get_string("fetchservererror","resource"); // Server error
433 $client->results
= get_string("fetcherror","resource"); // Redirection? HEAD? Unknown error.
439 function resource_redirect_tags($text, $url, $tagtoparse, $keytoparse,$prefix = "" ) {
441 if ( strpos($url,"?") == FALSE ) {
445 $lastpoint = strrpos($url,".");
446 $lastslash = strrpos($url,"/");
447 if ( $lastpoint > $lastslash ) {
448 $root = substr($url,0,$lastslash+
1);
452 if ( $root == "http://" or
453 $root == "https://") {
456 if ( substr($root,strlen($root)-1) == '/' ) {
457 $root = substr($root,0,-1);
461 $lastslash = strrpos($mainroot,"/");
462 while ( $lastslash > 9) {
463 $mainroot = substr($mainroot,0,$lastslash);
465 $lastslash = strrpos($mainroot,"/");
468 $regex = "/<$tagtoparse (.+?)>/is";
469 $count = preg_match_all($regex, $text, $hrefs);
470 for ( $i = 0; $i < $count; $i++
) {
471 $tag = $hrefs[1][$i];
473 $poshref = strpos(strtolower($tag),strtolower($keytoparse));
474 $start = $poshref +
strlen($keytoparse);
475 $left = substr($tag,0,$start);
476 if ( $tag[$start] == '"' ) {
480 $posspace = strpos($tag," ", $start+
1);
482 if ( $posspace != FALSE) {
483 $right = substr($tag, $posspace);
485 $end = strlen($tag)-1;
486 if ( $tag[$end] == '"' ) {
487 $right = '"' . $right;
489 $finalurl = substr($tag,$start,$end-$start+
$diff);
490 // Here, we could have these possible values for $finalurl:
491 // file.ext Add current root dir
492 // http://(domain) don't care
493 // http://(domain)/ don't care
494 // http://(domain)/folder don't care
495 // http://(domain)/folder/ don't care
496 // http://(domain)/folder/file.ext don't care
497 // folder/ Add current root dir
498 // folder/file.ext Add current root dir
499 // /folder/ Add main root dir
500 // /folder/file.ext Add main root dir
502 // Special case: If finalurl contains a ?, it won't be parsed
505 if ( strpos($finalurl,"?") == FALSE ) {
509 if ( $finalurl[0] == "/" ) {
510 $finalurl = $mainroot . $finalurl;
511 } elseif ( strtolower(substr($finalurl,0,7)) != "http://" and
512 strtolower(substr($finalurl,0,8)) != "https://") {
513 if ( $finalurl[0] == "/") {
514 $finalurl = $mainroot . $finalurl;
516 $finalurl = "$root/$finalurl";
520 $text = str_replace($tag,"$left$prefix$finalurl$right",$text);
527 function resource_is_url($path) {
528 if (strpos($path, '://')) { // eg http:// https:// ftp:// etc
531 if (strpos($path, '/') === 0) { // Starts with slash
537 function resource_get_types() {
542 $standardresources = array('text','html','file','directory');
543 foreach ($standardresources as $resourcetype) {
544 $type = new object();
545 $type->modclass
= MOD_CLASS_RESOURCE
;
546 $type->type
= "resource&type=$resourcetype";
547 $type->typestr
= get_string("resourcetype$resourcetype", 'resource');
551 /// Drop-in extra resource types
552 $resourcetypes = get_list_of_plugins('mod/resource/type');
553 foreach ($resourcetypes as $resourcetype) {
554 if (!empty($CFG->{'resource_hide_'.$resourcetype})) { // Not wanted
557 if (!in_array($resourcetype, $standardresources)) {
558 $type = new object();
559 $type->modclass
= MOD_CLASS_RESOURCE
;
560 $type->type
= "resource&type=$resourcetype";
561 $type->typestr
= get_string("resourcetype$resourcetype", 'resource');
569 function resource_get_view_actions() {
570 return array('view','view all');
573 function resource_get_post_actions() {
577 function resource_renamefiles($course, $wdir, $oldname, $name) {
580 $status = '<p align=\"center\"><strong>'.get_string('affectedresources', 'resource').':</strong><ul>';
583 $old = trim($wdir.'/'.$oldname, '/');
584 $new = trim($wdir.'/'.$name, '/');
586 $sql = "SELECT r.id, r.reference, r.name, cm.id AS cmid
587 FROM {$CFG->prefix}resource r,
588 {$CFG->prefix}course_modules cm,
589 {$CFG->prefix}modules m
590 WHERE r.course = '{$course->id}'
591 AND m.name = 'resource'
593 AND cm.instance = r.id
594 AND (r.type = 'file' OR r.type = 'directory')
595 AND (r.reference LIKE '{$old}/%' OR r.reference = '{$old}')";
596 if ($resources = get_records_sql($sql)) {
597 foreach ($resources as $resource) {
599 $r->id
= $resource->id
;
601 if ($resource->reference
== $old) {
602 $r->reference
= addslashes($new);
604 $r->reference
= addslashes(preg_replace('|^'.preg_quote($old, '|').'/|', $new.'/', $resource->reference
));
606 if ($r->reference
!== '') {
608 $status .= "<li><a href=\"$CFG->wwwroot/mod/resource/view.php?id=$resource->cmid\" target=\"_blank\">$resource->name</a>: $resource->reference ==> $r->reference</li>";
609 if (!empty($CFG->resource_autofilerename
)) {
610 if (!update_record('resource', $r)) {
611 error("Error updating resource with ID $r->id.");
617 $status .= '</ul></p>';
621 if (empty($CFG->resource_autofilerename
)) {
622 notify(get_string('warningdisabledrename', 'resource'));
627 function resource_delete_warning($course, $files) {
632 foreach($files as $key=>$file) {
633 $files[$key] = trim($file, '/');
635 $sql = "SELECT r.id, r.reference, r.name, cm.id AS cmid
636 FROM {$CFG->prefix}resource r,
637 {$CFG->prefix}course_modules cm,
638 {$CFG->prefix}modules m
639 WHERE r.course = '{$course->id}'
640 AND m.name = 'resource'
642 AND cm.instance = r.id
643 AND (r.type = 'file' OR r.type = 'directory')";
644 if ($resources = get_records_sql($sql)) {
645 foreach ($resources as $resource) {
646 if ($resource->reference
== '') {
647 continue; // top shared directory does not prevent anything
649 if (in_array($resource->reference
, $files)) {
650 $found[$resource->id
] = $resource;
652 foreach($files as $file) {
653 if (preg_match('|^'.preg_quote($file, '|').'/|', $resource->reference
)) {
654 $found[$resource->id
] = $resource;
661 if (!empty($found)) {
663 print_simple_box_start("center");
664 echo '<p><strong>'.get_string('affectedresources', 'resource').':</strong><ul>';
665 foreach($found as $resource) {
666 echo "<li><a href=\"$CFG->wwwroot/mod/resource/view.php?id=$resource->cmid\" target=\"_blank\">$resource->name</a>: $resource->reference</li>";
669 print_simple_box_end();