MDL-11082 Improved groups upgrade performance 1.8x -> 1.9; thanks Eloy for telling...
[moodle-pu.git] / mod / resource / lib.php
blob2a2fc62a50a438a2d152bf6c3c40c5e54adef8bc
1 <?php // $Id$
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);
63 } else {
64 set_config($popupoption, 'checked');
69 if (!empty($THEME->customcorners)) {
70 require_once($CFG->dirroot.'/lib/custom_corners_lib.php');
73 /**
74 * resource_base is the base class for resource types
76 * This class provides all the functionality for a resource
79 class resource_base {
81 var $cm;
82 var $course;
83 var $resource;
84 var $navigation;
85 var $navlinks;
87 /**
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) {
98 global $CFG, $COURSE;
100 if ($cmid) {
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}");
127 } else {
128 $this->course = $COURSE;
134 * Display function does nothing in the base class
136 function display() {
142 * Display the resource with the course blocks.
144 function display_course_blocks_start() {
146 global $CFG;
147 global $USER;
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);
154 $this->PAGE = $PAGE;
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();
179 echo '</td>';
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() {
194 global $CFG;
196 $PAGE = $this->PAGE;
197 $pageblocks = blocks_setup($PAGE);
198 $blocks_preferred_width = bounded_number(180, blocks_preferred_width($pageblocks[BLOCK_POS_RIGHT]), 210);
200 echo '</div>';
201 if (!empty($THEME->customcorners)) print_custom_corners_end();
202 echo '</td>';
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();
209 echo '</td>';
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.
248 $result = true;
250 if (! delete_records("resource", "id", "$resource->id")) {
251 $result = false;
254 return $result;
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) {
270 global $CFG;
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) {
282 global $CFG;
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) {
294 global $CFG;
296 if (! $resource = get_record("resource", "id", "$id")) {
297 return false;
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;
321 return $result;
323 return NULL;
327 function resource_user_complete($course, $user, $mod, $resource) {
328 global $CFG;
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);
340 } else {
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 !!)
349 return false;
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
360 global $CFG;
362 $info = NULL;
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&amp;id=".
368 $coursemodule->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";
378 } else {
379 $info->icon ="f/web.gif";
381 } else if ($resource->type == 'directory') {
382 $info->icon ="f/folder.gif";
386 return $info;
389 function resource_fetch_remote_file ($cm, $url, $headers = "" ) {
390 /// Snoopy is an HTTP client in PHP
392 global $CFG;
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 ) {
399 $ua = $ua . ')';
400 } else {
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=",
413 "IMG" => "src=",
414 "LINK" => "href=",
415 "AREA" => "href=",
416 "FRAME" => "src=",
417 "IFRAME" => "src=",
418 "FORM" => "action=");
420 foreach ($tags as $tag => $key) {
421 $prefix = "fetch.php?id=$cm->id&amp;url=";
422 if ( $tag == "IMG" or $tag == "LINK" or $tag == "FORM") {
423 $prefix = "";
425 $client->results = resource_redirect_tags($client->results, $url, $tag, $key,$prefix);
427 } else {
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
432 } else {
433 $client->results = get_string("fetcherror","resource"); // Redirection? HEAD? Unknown error.
436 return $client;
439 function resource_redirect_tags($text, $url, $tagtoparse, $keytoparse,$prefix = "" ) {
440 $valid = 1;
441 if ( strpos($url,"?") == FALSE ) {
442 $valid = 1;
444 if ( $valid ) {
445 $lastpoint = strrpos($url,".");
446 $lastslash = strrpos($url,"/");
447 if ( $lastpoint > $lastslash ) {
448 $root = substr($url,0,$lastslash+1);
449 } else {
450 $root = $url;
452 if ( $root == "http://" or
453 $root == "https://") {
454 $root = $url;
456 if ( substr($root,strlen($root)-1) == '/' ) {
457 $root = substr($root,0,-1);
460 $mainroot = $root;
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] == '"' ) {
477 $left .= '"';
478 $start++;
480 $posspace = strpos($tag," ", $start+1);
481 $right = "";
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
503 $valid = 1;
505 if ( strpos($finalurl,"?") == FALSE ) {
506 $valid = 1;
508 if ( $valid ) {
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;
515 } else {
516 $finalurl = "$root/$finalurl";
520 $text = str_replace($tag,"$left$prefix$finalurl$right",$text);
524 return $text;
527 function resource_is_url($path) {
528 if (strpos($path, '://')) { // eg http:// https:// ftp:// etc
529 return true;
531 if (strpos($path, '/') === 0) { // Starts with slash
532 return true;
534 return false;
537 function resource_get_types() {
538 global $CFG;
540 $types = array();
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&amp;type=$resourcetype";
547 $type->typestr = get_string("resourcetype$resourcetype", 'resource');
548 $types[] = $type;
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
555 continue;
557 if (!in_array($resourcetype, $standardresources)) {
558 $type = new object();
559 $type->modclass = MOD_CLASS_RESOURCE;
560 $type->type = "resource&amp;type=$resourcetype";
561 $type->typestr = get_string("resourcetype$resourcetype", 'resource');
562 $types[] = $type;
566 return $types;
569 function resource_get_view_actions() {
570 return array('view','view all');
573 function resource_get_post_actions() {
574 return array();
577 function resource_renamefiles($course, $wdir, $oldname, $name) {
578 global $CFG;
580 $status = '<p align=\"center\"><strong>'.get_string('affectedresources', 'resource').':</strong><ul>';
581 $updates = false;
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'
592 AND cm.module = m.id
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) {
598 $r = new object();
599 $r->id = $resource->id;
600 $r->reference = '';
601 if ($resource->reference == $old) {
602 $r->reference = addslashes($new);
603 } else {
604 $r->reference = addslashes(preg_replace('|^'.preg_quote($old, '|').'/|', $new.'/', $resource->reference));
606 if ($r->reference !== '') {
607 $updates = true;
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>';
619 if ($updates) {
620 echo $status;
621 if (empty($CFG->resource_autofilerename)) {
622 notify(get_string('warningdisabledrename', 'resource'));
627 function resource_delete_warning($course, $files) {
628 global $CFG;
630 $found = array();
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'
641 AND cm.module = m.id
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;
651 } else {
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>";
668 echo '</ul></p>';
669 print_simple_box_end();
671 return true;
672 } else {
673 return false;