Changed to require_capability where possible
[moodle.git] / mod / resource / lib.php
CommitLineData
d1290cec 1<?php // $Id$
2a439ba7 2
5925d0ef 3if (!isset($CFG->resource_framesize)) {
4 set_config("resource_framesize", 130);
ec81373f 5}
92a419a2 6
66c25030 7if (!isset($CFG->resource_websearch)) {
8 set_config("resource_websearch", "http://google.com/");
ec81373f 9}
66c25030 10
5e91dd3f 11if (!isset($CFG->resource_defaulturl)) {
12 set_config("resource_defaulturl", "http://");
ec81373f 13}
3bfe3922 14
15if (!isset($CFG->resource_filterexternalpages)) {
16 set_config("resource_filterexternalpages", false);
ec81373f 17}
5e91dd3f 18
d18830fe 19if (!isset($CFG->resource_secretphrase)) {
20 set_config("resource_secretphrase", random_string(20));
ec81373f 21}
d18830fe 22
83891eda 23if (!isset($CFG->resource_popup)) {
24 set_config("resource_popup", "");
ec81373f 25}
83891eda 26
33935242 27if (!isset($CFG->resource_windowsettings)) {
28 set_config("resource_windowsettings", "0");
ec81373f 29}
33935242 30
31if (!isset($CFG->resource_parametersettings)) {
32 set_config("resource_parametersettings", "0");
ec81373f 33}
33935242 34
713d78ea 35if (!isset($CFG->resource_allowlocalfiles)) {
36 set_config("resource_allowlocalfiles", "0");
37}
38
37147357 39if (!isset($CFG->resource_hide_repository)) {
40 set_config("resource_hide_repository", "1");
41}
42
713d78ea 43define('RESOURCE_LOCALPATH', 'LOCALPATH');
44
ec81373f 45$RESOURCE_WINDOW_OPTIONS = array('resizable', 'scrollbars', 'directories', 'location',
2e708fa0 46 'menubar', 'toolbar', 'status', 'height', 'width');
3d30a455 47
83891eda 48foreach ($RESOURCE_WINDOW_OPTIONS as $popupoption) {
49 $popupoption = "resource_popup$popupoption";
50 if (!isset($CFG->$popupoption)) {
2e708fa0 51 if ($popupoption == 'resource_popupheight') {
83891eda 52 set_config($popupoption, 450);
2e708fa0 53 } else if ($popupoption == 'resource_popupwidth') {
83891eda 54 set_config($popupoption, 620);
55 } else {
2e708fa0 56 set_config($popupoption, 'checked');
83891eda 57 }
ec81373f 58 }
83891eda 59}
60
d18830fe 61/**
62* resource_base is the base class for resource types
63*
64* This class provides all the functionality for a resource
65*/
66
67class resource_base {
68
69var $cm;
70var $course;
71var $resource;
72
73
74/**
75* Constructor for the base resource class
76*
77* Constructor for the base resource class.
78* If cmid is set create the cm, course, resource objects.
6aac6eef 79* and do some checks to make sure people can be here, and so on.
d18830fe 80*
81* @param cmid integer, the current course module id - not set for new resources
82*/
83function resource_base($cmid=0) {
84
6aac6eef 85 global $CFG;
3cc63301 86 global $course; // Ugly hack, needed for course language ugly hack
6aac6eef 87
d18830fe 88 if ($cmid) {
f9d5371b 89 if (! $this->cm = get_coursemodule_from_id('resource', $cmid)) {
d18830fe 90 error("Course Module ID was incorrect");
91 }
92
93 if (! $this->course = get_record("course", "id", $this->cm->course)) {
94 error("Course is misconfigured");
95 }
96
3cc63301 97 $course = $this->course; // Make it a global so we can see it later
98
ec81373f 99 require_course_login($this->course, true, $this->cm);
6aac6eef 100
d18830fe 101 if (! $this->resource = get_record("resource", "id", $this->cm->instance)) {
102 error("Resource ID was incorrect");
103 }
6aac6eef 104
105 $this->strresource = get_string("modulename", "resource");
106 $this->strresources = get_string("modulenameplural", "resource");
107
108 if ($this->course->category) {
6aac6eef 109 $this->navigation = "<a target=\"{$CFG->framename}\" href=\"$CFG->wwwroot/course/view.php?id={$this->course->id}\">{$this->course->shortname}</a> -> ".
110 "<a target=\"{$CFG->framename}\" href=\"index.php?id={$this->course->id}\">$this->strresources</a> ->";
111 } else {
ec81373f 112 $this->navigation = "<a target=\"{$CFG->framename}\" href=\"index.php?id={$this->course->id}\">$this->strresources</a> ->";
6aac6eef 113 }
114
d02eeded 115 if (!$this->cm->visible and !has_capability('moodle/course:viewhiddenactivities', get_context_instance(CONTEXT_MODULE, $this->cm->id))) {
6aac6eef 116 $pagetitle = strip_tags($this->course->shortname.': '.$this->strresource);
117 print_header($pagetitle, $this->course->fullname, "$this->navigation $this->strresource", "", "", true, '', navmenu($this->course, $this->cm));
118 notice(get_string("activityiscurrentlyhidden"), "$CFG->wwwroot/course/view.php?id={$this->course->id}");
119 }
ec81373f 120 }
d18830fe 121}
122
123
6aac6eef 124/**
125* Display function does nothing in the base class
126*/
d18830fe 127function display() {
6aac6eef 128
d18830fe 129}
130
131
0440482f 132/**
133* Display the resource with the course blocks.
134*/
135function display_course_blocks_start() {
136
137 global $CFG;
138 global $USER;
139
140 require_once($CFG->libdir.'/blocklib.php');
141 require_once($CFG->libdir.'/pagelib.php');
dbfa9ccd 142 require_once($CFG->dirroot.'/course/lib.php'); //required by some blocks
0440482f 143
144 $PAGE = page_create_object(PAGE_COURSE_VIEW, $this->course->id);
145 $this->PAGE = $PAGE;
146 $pageblocks = blocks_setup($PAGE);
147
148 $blocks_preferred_width = bounded_number(180, blocks_preferred_width($pageblocks[BLOCK_POS_LEFT]), 210);
149
150/// Print the page header
151
d2b23346 152 $edit = optional_param('edit', -1, PARAM_BOOL);
153
154 if (($edit != -1) and $PAGE->user_allowed_editing()) {
155 $USER->editing = $edit;
0440482f 156 }
d2b23346 157
0440482f 158 $morebreadcrumbs = array($this->strresources => 'index.php?id='.$this->course->id,
159 $this->resource->name => '');
160
161 $PAGE->print_header($this->course->shortname.': %fullname%', $morebreadcrumbs);
162
163 echo '<table id="layout-table"><tr>';
164
165 if((blocks_have_content($pageblocks, BLOCK_POS_LEFT) || $PAGE->user_is_editing())) {
166 echo '<td style="width: '.$blocks_preferred_width.'px;" id="left-column">';
167 blocks_print_group($PAGE, $pageblocks, BLOCK_POS_LEFT);
168 echo '</td>';
169 }
170
171 echo '<td id="middle-column">';
172 echo '<div id="resource">';
173
174}
175
176
177/**
178 * Finish displaying the resource with the course blocks
179 */
180function display_course_blocks_end() {
181
182 global $CFG;
183
184 $PAGE = $this->PAGE;
185 $pageblocks = blocks_setup($PAGE);
186 $blocks_preferred_width = bounded_number(180, blocks_preferred_width($pageblocks[BLOCK_POS_RIGHT]), 210);
187
188 echo '</div>';
189 echo '</td>';
190
191 if((blocks_have_content($pageblocks, BLOCK_POS_RIGHT) || $PAGE->user_is_editing())) {
192 echo '<td style="width: '.$blocks_preferred_width.'px;" id="right-column">';
193 blocks_print_group($PAGE, $pageblocks, BLOCK_POS_RIGHT);
194 echo '</td>';
195 }
196
197 echo '</tr></table>';
198
199 print_footer($this->course);
200
201}
202
203
1aef6fb7 204function setup(&$form) {
d18830fe 205 global $CFG, $usehtmleditor;
206
207 if (! empty($form->course)) {
208 if (! $this->course = get_record("course", "id", $form->course)) {
209 error("Course is misconfigured");
210 }
211 }
212
213 if (empty($form->name)) {
214 $form->name = "";
215 }
216 if (empty($form->type)) {
217 $form->type = "";
218 }
219 if (empty($form->summary)) {
220 $form->summary = "";
221 }
222 if (empty($form->reference)) {
223 $form->reference = "";
224 }
225 if (empty($form->alltext)) {
226 $form->alltext = "";
227 }
1aef6fb7 228 if (empty($form->options)) {
229 $form->options = "";
230 }
d18830fe 231 $nohtmleditorneeded = true;
232
6da4b261 233 print_heading_with_help(get_string("resourcetype$form->type", 'resource'), $form->type, 'resource/type');
d18830fe 234
235 include("$CFG->dirroot/mod/resource/type/common.html");
236}
237
238
239function setup_end() {
240 global $CFG;
241
242 include("$CFG->dirroot/mod/resource/type/common_end.html");
243}
244
245
246function add_instance($resource) {
ec81373f 247// Given an object containing all the necessary data,
248// (defined by the form in mod.html) this function
249// will create a new instance and return the id number
cccb016a 250// of the new instance.
2a439ba7 251
86aa7ccf 252 global $RESOURCE_WINDOW_OPTIONS;
253
cccb016a 254 $resource->timemodified = time();
2a439ba7 255
d18830fe 256 if (isset($resource->windowpopup)) {
7c990f68 257 if ($resource->windowpopup) {
258 $optionlist = array();
259 foreach ($RESOURCE_WINDOW_OPTIONS as $option) {
260 if (isset($resource->$option)) {
261 $optionlist[] = $option."=".$resource->$option;
262 }
86aa7ccf 263 }
7c990f68 264 $resource->popup = implode(',', $optionlist);
d18830fe 265 $resource->options = "";
7c990f68 266 } else {
267 if (isset($resource->framepage)) {
268 $resource->options = "frame";
269 } else {
270 $resource->options = "";
271 }
272 $resource->popup = "";
d18830fe 273 }
86aa7ccf 274 }
275
33935242 276 if (isset($resource->parametersettingspref)) {
277 set_user_preference('resource_parametersettingspref', $resource->parametersettingspref);
278 }
279 if (isset($resource->windowsettingspref)) {
280 set_user_preference('resource_windowsettingspref', $resource->windowsettingspref);
281 }
282
cccb016a 283 return insert_record("resource", $resource);
284}
2a439ba7 285
cccb016a 286
d18830fe 287function update_instance($resource) {
ec81373f 288// Given an object containing all the necessary data,
289// (defined by the form in mod.html) this function
cccb016a 290// will update an existing instance with new data.
291
86aa7ccf 292 global $RESOURCE_WINDOW_OPTIONS;
293
cccb016a 294 $resource->id = $resource->instance;
295 $resource->timemodified = time();
296
d18830fe 297 if (isset($resource->windowpopup)) {
7c990f68 298 if ($resource->windowpopup) {
299 $optionlist = array();
300 foreach ($RESOURCE_WINDOW_OPTIONS as $option) {
301 if (isset($resource->$option)) {
302 $optionlist[] = $option."=".$resource->$option;
303 }
86aa7ccf 304 }
7c990f68 305 $resource->popup = implode(',', $optionlist);
d18830fe 306 $resource->options = "";
7c990f68 307 } else {
308 if (isset($resource->framepage)) {
309 $resource->options = "frame";
310 } else {
311 $resource->options = "";
312 }
313 $resource->popup = "";
d18830fe 314 }
86aa7ccf 315 }
316
33935242 317 if (isset($resource->parametersettingspref)) {
318 set_user_preference('resource_parametersettingspref', $resource->parametersettingspref);
319 }
320 if (isset($resource->windowsettingspref)) {
321 set_user_preference('resource_windowsettingspref', $resource->windowsettingspref);
322 }
323
cccb016a 324 return update_record("resource", $resource);
325}
326
327
f0c6abbf 328function delete_instance($resource) {
329// Given an object containing the resource data
ec81373f 330// this function will permanently delete the instance
331// and any data that depends on it.
cccb016a 332
cccb016a 333 $result = true;
334
335 if (! delete_records("resource", "id", "$resource->id")) {
336 $result = false;
2a439ba7 337 }
338
cccb016a 339 return $result;
2a439ba7 340}
cccb016a 341
2a439ba7 342
d18830fe 343
344} /// end of class definition
345
346
347
348function resource_add_instance($resource) {
349 global $CFG;
ec81373f 350
1b1d3422 351 $resource->type = clean_param($resource->type, PARAM_SAFEDIR); // Just to be safe
79035d46 352
d18830fe 353 require_once("$CFG->dirroot/mod/resource/type/$resource->type/resource.class.php");
1aef6fb7 354 $resourceclass = "resource_$resource->type";
355 $res = new $resourceclass();
d18830fe 356
357 return $res->add_instance($resource);
358}
359
360function resource_update_instance($resource) {
361 global $CFG;
ec81373f 362
1b1d3422 363 $resource->type = clean_param($resource->type, PARAM_SAFEDIR); // Just to be safe
79035d46 364
d18830fe 365 require_once("$CFG->dirroot/mod/resource/type/$resource->type/resource.class.php");
1aef6fb7 366 $resourceclass = "resource_$resource->type";
367 $res = new $resourceclass();
d18830fe 368
369 return $res->update_instance($resource);
370}
371
372function resource_delete_instance($id) {
373 global $CFG;
ec81373f 374
d18830fe 375 if (! $resource = get_record("resource", "id", "$id")) {
376 return false;
377 }
79035d46 378
1b1d3422 379 $resource->type = clean_param($resource->type, PARAM_SAFEDIR); // Just to be safe
ec81373f 380
d18830fe 381 require_once("$CFG->dirroot/mod/resource/type/$resource->type/resource.class.php");
1aef6fb7 382 $resourceclass = "resource_$resource->type";
383 $res = new $resourceclass();
d18830fe 384
f0c6abbf 385 return $res->delete_instance($resource);
d18830fe 386}
387
388
2a439ba7 389function resource_user_outline($course, $user, $mod, $resource) {
ec81373f 390 if ($logs = get_records_select("log", "userid='$user->id' AND module='resource'
ebc3bd2b 391 AND action='view' AND info='$resource->id'", "time ASC")) {
2a439ba7 392
393 $numviews = count($logs);
394 $lastlog = array_pop($logs);
395
396 $result->info = get_string("numviews", "", $numviews);
397 $result->time = $lastlog->time;
398
399 return $result;
400 }
401 return NULL;
402}
403
404
405function resource_user_complete($course, $user, $mod, $resource) {
9fad2dec 406 global $CFG;
2a439ba7 407
ec81373f 408 if ($logs = get_records_select("log", "userid='$user->id' AND module='resource'
ebc3bd2b 409 AND action='view' AND info='$resource->id'", "time ASC")) {
2a439ba7 410 $numviews = count($logs);
411 $lastlog = array_pop($logs);
412
413 $strmostrecently = get_string("mostrecently");
414 $strnumviews = get_string("numviews", "", $numviews);
415
416 echo "$strnumviews - $strmostrecently ".userdate($lastlog->time);
417
418 } else {
4282d7dd 419 print_string("neverseen", "resource");
2a439ba7 420 }
421}
422
84caf038 423function resource_get_participants($resourceid) {
424//Returns the users with data in one resource
425//(NONE, byt must exists on EVERY mod !!)
426
427 return false;
428}
2a439ba7 429
8dddba42 430function resource_get_coursemodule_info($coursemodule) {
ec81373f 431/// Given a course_module object, this function returns any
8dddba42 432/// "extra" information that may be needed when printing
433/// this activity in a course listing.
434///
435/// See get_array_of_activities() in course/lib.php
436///
437
9d361034 438 global $CFG;
439
440 $info = NULL;
441
8dddba42 442 if ($resource = get_record("resource", "id", $coursemodule->instance)) {
85e8239e 443 if (!empty($resource->popup)) {
657e7903 444 $info->extra = urlencode("target=\"resource$resource->id\" onclick=\"return ".
839f2456 445 "openpopup('/mod/resource/view.php?inpopup=true&amp;id=".
8dddba42 446 $coursemodule->id.
d18830fe 447 "','resource$resource->id','$resource->popup');\"");
8dddba42 448 }
9d361034 449
f1e0649c 450 require_once($CFG->libdir.'/filelib.php');
9d361034 451
85e8239e 452 if ($resource->type == 'file') {
ec81373f 453 $icon = mimeinfo("icon", $resource->reference);
9d361034 454 if ($icon != 'unknown.gif') {
ec81373f 455 $info->icon ="f/$icon";
85e8239e 456 } else {
ec81373f 457 $info->icon ="f/web.gif";
9d361034 458 }
d18830fe 459 } else if ($resource->type == 'directory') {
ec81373f 460 $info->icon ="f/folder.gif";
9d361034 461 }
8dddba42 462 }
463
9d361034 464 return $info;
8dddba42 465}
ec81373f 466
8367faba 467function resource_fetch_remote_file ($cm, $url, $headers = "" ) {
3bfe3922 468/// Snoopy is an HTTP client in PHP
469
470 global $CFG;
471
472 require_once("$CFG->libdir/snoopy/Snoopy.class.inc");
473
b2b8471e 474 $client = new Snoopy();
ec81373f 475 $ua = 'Moodle/'. $CFG->release . ' (+http://moodle.org';
bf46cd22 476 if ( $CFG->resource_usecache ) {
477 $ua = $ua . ')';
478 } else {
479 $ua = $ua . '; No cache)';
480 }
481 $client->agent = $ua;
482 $client->read_timeout = 5;
483 $client->use_gzip = true;
b2b8471e 484 if (is_array($headers) ) {
485 $client->rawheaders = $headers;
486 }
ec81373f 487
b2b8471e 488 @$client->fetch($url);
bf46cd22 489 if ( $client->status >= 200 && $client->status < 300 ) {
490 $tags = array("A" => "href=",
491 "IMG" => "src=",
492 "LINK" => "href=",
493 "AREA" => "href=",
494 "FRAME" => "src=",
495 "IFRAME" => "src=",
496 "FORM" => "action=");
ec81373f 497
bf46cd22 498 foreach ($tags as $tag => $key) {
839f2456 499 $prefix = "fetch.php?id=$cm->id&amp;url=";
bf46cd22 500 if ( $tag == "IMG" or $tag == "LINK" or $tag == "FORM") {
501 $prefix = "";
502 }
503 $client->results = resource_redirect_tags($client->results, $url, $tag, $key,$prefix);
504 }
505 } else {
506 if ( $client->status >= 400 && $client->status < 500) {
507 $client->results = get_string("fetchclienterror","resource"); // Client error
508 } elseif ( $client->status >= 500 && $client->status < 600) {
509 $client->results = get_string("fetchservererror","resource"); // Server error
510 } else {
511 $client->results = get_string("fetcherror","resource"); // Redirection? HEAD? Unknown error.
af65e103 512 }
af65e103 513 }
b2b8471e 514 return $client;
af65e103 515}
516
517function resource_redirect_tags($text, $url, $tagtoparse, $keytoparse,$prefix = "" ) {
bf46cd22 518 $valid = 1;
af65e103 519 if ( strpos($url,"?") == FALSE ) {
520 $valid = 1;
521 }
522 if ( $valid ) {
523 $lastpoint = strrpos($url,".");
524 $lastslash = strrpos($url,"/");
525 if ( $lastpoint > $lastslash ) {
526 $root = substr($url,0,$lastslash+1);
527 } else {
528 $root = $url;
529 }
ec81373f 530 if ( $root == "http://" or
af65e103 531 $root == "https://") {
532 $root = $url;
533 }
534 if ( substr($root,strlen($root)-1) == '/' ) {
535 $root = substr($root,0,-1);
536 }
ec81373f 537
af65e103 538 $mainroot = $root;
539 $lastslash = strrpos($mainroot,"/");
540 while ( $lastslash > 9) {
541 $mainroot = substr($mainroot,0,$lastslash);
ec81373f 542
af65e103 543 $lastslash = strrpos($mainroot,"/");
544 }
8dddba42 545
ec81373f 546 $regex = "/<$tagtoparse (.+?)>/is";
547 $count = preg_match_all($regex, $text, $hrefs);
af65e103 548 for ( $i = 0; $i < $count; $i++) {
549 $tag = $hrefs[1][$i];
ec81373f 550
af65e103 551 $poshref = strpos(strtolower($tag),strtolower($keytoparse));
552 $start = $poshref + strlen($keytoparse);
553 $left = substr($tag,0,$start);
554 if ( $tag[$start] == '"' ) {
555 $left .= '"';
556 $start++;
557 }
558 $posspace = strpos($tag," ", $start+1);
559 $right = "";
560 if ( $posspace != FALSE) {
561 $right = substr($tag, $posspace);
562 }
563 $end = strlen($tag)-1;
564 if ( $tag[$end] == '"' ) {
565 $right = '"' . $right;
566 }
567 $finalurl = substr($tag,$start,$end-$start+$diff);
568 // Here, we could have these possible values for $finalurl:
569 // file.ext Add current root dir
570 // http://(domain) don't care
571 // http://(domain)/ don't care
572 // http://(domain)/folder don't care
573 // http://(domain)/folder/ don't care
574 // http://(domain)/folder/file.ext don't care
575 // folder/ Add current root dir
576 // folder/file.ext Add current root dir
577 // /folder/ Add main root dir
578 // /folder/file.ext Add main root dir
579
580 // Special case: If finalurl contains a ?, it won't be parsed
bf46cd22 581 $valid = 1;
af65e103 582
583 if ( strpos($finalurl,"?") == FALSE ) {
584 $valid = 1;
585 }
586 if ( $valid ) {
587 if ( $finalurl[0] == "/" ) {
588 $finalurl = $mainroot . $finalurl;
ec81373f 589 } elseif ( strtolower(substr($finalurl,0,7)) != "http://" and
af65e103 590 strtolower(substr($finalurl,0,8)) != "https://") {
591 if ( $finalurl[0] == "/") {
592 $finalurl = $mainroot . $finalurl;
593 } else {
594 $finalurl = "$root/$finalurl";
595 }
596 }
ec81373f 597
af65e103 598 $text = str_replace($tag,"$left$prefix$finalurl$right",$text);
599 }
600 }
601 }
602 return $text;
603}
8dddba42 604
d18830fe 605function resource_is_url($path) {
427c8ccb 606 if (strpos($path, '://')) { // eg http:// https:// ftp:// etc
d18830fe 607 return true;
608 }
427c8ccb 609 if (strpos($path, '/') === 0) { // Starts with slash
610 return true;
611 }
612 return false;
d18830fe 613}
614
6da4b261 615function resource_get_resource_types() {
616/// Returns a menu of current resource types, in standard order
37147357 617 global $resource_standard_order, $CFG;
6da4b261 618
619 $resources = array();
620
621 /// Standard resource types
3d30a455 622 $standardresources = array('text','html','file','directory');
6da4b261 623 foreach ($standardresources as $resourcetype) {
624 $resources[$resourcetype] = get_string("resourcetype$resourcetype", 'resource');
625 }
626
627 /// Drop-in extra resource types
628 $resourcetypes = get_list_of_plugins('mod/resource/type');
629 foreach ($resourcetypes as $resourcetype) {
37147357 630 if (!empty($CFG->{'resource_hide_'.$resourcetype})) { // Not wanted
631 continue;
632 }
6da4b261 633 if (!in_array($resourcetype, $resources)) {
634 $resources[$resourcetype] = get_string("resourcetype$resourcetype", 'resource');
635 }
636 }
637 return $resources;
638}
f3221af9 639
640function resource_get_view_actions() {
641 return array('view','view all');
642}
643
644function resource_get_post_actions() {
645 return array();
646}
647
2a439ba7 648?>