adding some strings
[moodle-linuxchix.git] / mod / resource / lib.php
blobacd438d89f5ba4bfaea887fd56459f5a7362b632
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;
86 /**
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) {
97 global $CFG, $COURSE;
99 if ($cmid) {
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}");
126 } else {
127 $this->course = $COURSE;
133 * Display function does nothing in the base class
135 function display() {
141 * Display the resource with the course blocks.
143 function display_course_blocks_start() {
145 global $CFG;
146 global $USER;
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);
153 $this->PAGE = $PAGE;
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();
178 echo '</td>';
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() {
193 global $CFG;
195 $PAGE = $this->PAGE;
196 $pageblocks = blocks_setup($PAGE);
197 $blocks_preferred_width = bounded_number(180, blocks_preferred_width($pageblocks[BLOCK_POS_RIGHT]), 210);
199 echo '</div>';
200 if (!empty($THEME->customcorners)) print_custom_corners_end();
201 echo '</td>';
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();
208 echo '</td>';
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.
247 $result = true;
249 if (! delete_records("resource", "id", "$resource->id")) {
250 $result = false;
253 return $result;
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) {
269 global $CFG;
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) {
281 global $CFG;
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) {
293 global $CFG;
295 if (! $resource = get_record("resource", "id", "$id")) {
296 return false;
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;
320 return $result;
322 return NULL;
326 function resource_user_complete($course, $user, $mod, $resource) {
327 global $CFG;
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);
339 } else {
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 !!)
348 return false;
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
359 global $CFG;
361 $info = NULL;
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&amp;id=".
367 $coursemodule->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";
377 } else {
378 $info->icon ="f/web.gif";
380 } else if ($resource->type == 'directory') {
381 $info->icon ="f/folder.gif";
385 return $info;
388 function resource_fetch_remote_file ($cm, $url, $headers = "" ) {
389 /// Snoopy is an HTTP client in PHP
391 global $CFG;
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 ) {
398 $ua = $ua . ')';
399 } else {
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=",
412 "IMG" => "src=",
413 "LINK" => "href=",
414 "AREA" => "href=",
415 "FRAME" => "src=",
416 "IFRAME" => "src=",
417 "FORM" => "action=");
419 foreach ($tags as $tag => $key) {
420 $prefix = "fetch.php?id=$cm->id&amp;url=";
421 if ( $tag == "IMG" or $tag == "LINK" or $tag == "FORM") {
422 $prefix = "";
424 $client->results = resource_redirect_tags($client->results, $url, $tag, $key,$prefix);
426 } else {
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
431 } else {
432 $client->results = get_string("fetcherror","resource"); // Redirection? HEAD? Unknown error.
435 return $client;
438 function resource_redirect_tags($text, $url, $tagtoparse, $keytoparse,$prefix = "" ) {
439 $valid = 1;
440 if ( strpos($url,"?") == FALSE ) {
441 $valid = 1;
443 if ( $valid ) {
444 $lastpoint = strrpos($url,".");
445 $lastslash = strrpos($url,"/");
446 if ( $lastpoint > $lastslash ) {
447 $root = substr($url,0,$lastslash+1);
448 } else {
449 $root = $url;
451 if ( $root == "http://" or
452 $root == "https://") {
453 $root = $url;
455 if ( substr($root,strlen($root)-1) == '/' ) {
456 $root = substr($root,0,-1);
459 $mainroot = $root;
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] == '"' ) {
476 $left .= '"';
477 $start++;
479 $posspace = strpos($tag," ", $start+1);
480 $right = "";
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
502 $valid = 1;
504 if ( strpos($finalurl,"?") == FALSE ) {
505 $valid = 1;
507 if ( $valid ) {
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;
514 } else {
515 $finalurl = "$root/$finalurl";
519 $text = str_replace($tag,"$left$prefix$finalurl$right",$text);
523 return $text;
526 function resource_is_url($path) {
527 if (strpos($path, '://')) { // eg http:// https:// ftp:// etc
528 return true;
530 if (strpos($path, '/') === 0) { // Starts with slash
531 return true;
533 return false;
536 function resource_get_types() {
537 global $CFG;
539 $types = array();
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&amp;type=$resourcetype";
546 $type->typestr = get_string("resourcetype$resourcetype", 'resource');
547 $types[] = $type;
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
554 continue;
556 if (!in_array($resourcetype, $standardresources)) {
557 $type = new object();
558 $type->modclass = MOD_CLASS_RESOURCE;
559 $type->type = "resource&amp;type=$resourcetype";
560 $type->typestr = get_string("resourcetype$resourcetype", 'resource');
561 $types[] = $type;
565 return $types;
568 function resource_get_view_actions() {
569 return array('view','view all');
572 function resource_get_post_actions() {
573 return array();
576 function resource_renamefiles($course, $wdir, $oldname, $name) {
577 global $CFG;
579 $status = '<p align=\"center\"><strong>'.get_string('affectedresources', 'resource').':</strong><ul>';
580 $updates = false;
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'
591 AND cm.module = m.id
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) {
597 $r = new object();
598 $r->id = $resource->id;
599 $r->reference = '';
600 if ($resource->reference == $old) {
601 $r->reference = addslashes($new);
602 } else {
603 $r->reference = addslashes(preg_replace('|^'.preg_quote($old, '|').'/|', $new.'/', $resource->reference));
605 if ($r->reference !== '') {
606 $updates = true;
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>';
618 if ($updates) {
619 echo $status;
620 if (empty($CFG->resource_autofilerename)) {
621 notify(get_string('warningdisabledrename', 'resource'));
626 function resource_delete_warning($course, $files) {
627 global $CFG;
629 $found = array();
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'
640 AND cm.module = m.id
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;
650 } else {
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>";
667 echo '</ul></p>';
668 print_simple_box_end();
670 return true;
671 } else {
672 return false;