MDL-18910 moving modedit features to modname_supports()
[moodle.git] / mod / resource / lib.php
CommitLineData
d1290cec 1<?php // $Id$
2a439ba7 2
214b1cf7 3require_once($CFG->libdir.'/portfoliolib.php');
4
713d78ea 5define('RESOURCE_LOCALPATH', 'LOCALPATH');
6
220a90c5 7global $RESOURCE_WINDOW_OPTIONS; // must be global because it might be included from a function!
ec81373f 8$RESOURCE_WINDOW_OPTIONS = array('resizable', 'scrollbars', 'directories', 'location',
6d77bd61 9 'menubar', 'toolbar', 'status', 'width', 'height');
3d30a455 10
220a90c5 11if (!isset($CFG->resource_hide_repository)) {
12 set_config("resource_hide_repository", "1");
83891eda 13}
14
d18830fe 15/**
16* resource_base is the base class for resource types
17*
18* This class provides all the functionality for a resource
19*/
20
21class resource_base {
22
65634a81 23 var $cm;
24 var $course;
25 var $resource;
65634a81 26 var $navlinks;
27
28 /**
29 * Constructor for the base resource class
30 *
31 * Constructor for the base resource class.
32 * If cmid is set create the cm, course, resource objects.
33 * and do some checks to make sure people can be here, and so on.
34 *
35 * @param cmid integer, the current course module id - not set for new resources
36 */
37 function resource_base($cmid=0) {
5f5cd33c 38 global $CFG, $COURSE, $DB;
65634a81 39
bddd9f6f 40 $this->navlinks = array();
65634a81 41
42 if ($cmid) {
43 if (! $this->cm = get_coursemodule_from_id('resource', $cmid)) {
baa336f0 44 print_error('invalidcoursemodule');
65634a81 45 }
46
5f5cd33c 47 if (! $this->course = $DB->get_record("course", array("id"=>$this->cm->course))) {
baa336f0 48 print_error('coursemisconf');
65634a81 49 }
50
5f5cd33c 51 if (! $this->resource = $DB->get_record("resource", array("id"=>$this->cm->instance))) {
baa336f0 52 print_error('invalidid', 'resource');
65634a81 53 }
54
55 $this->strresource = get_string("modulename", "resource");
56 $this->strresources = get_string("modulenameplural", "resource");
57
65634a81 58 if (!$this->cm->visible and !has_capability('moodle/course:viewhiddenactivities', get_context_instance(CONTEXT_MODULE, $this->cm->id))) {
59 $pagetitle = strip_tags($this->course->shortname.': '.$this->strresource);
38e179a4 60 $navigation = build_navigation($this->navlinks, $this->cm);
65634a81 61
38e179a4 62 print_header($pagetitle, $this->course->fullname, $navigation, "", "", true, '', navmenu($this->course, $this->cm));
65634a81 63 notice(get_string("activityiscurrentlyhidden"), "$CFG->wwwroot/course/view.php?id={$this->course->id}");
64 }
d18830fe 65
65634a81 66 } else {
67 $this->course = $COURSE;
d18830fe 68 }
65634a81 69 }
6aac6eef 70
6aac6eef 71
65634a81 72 /**
73 * Display function does nothing in the base class
74 */
75 function display() {
6aac6eef 76
ec81373f 77 }
d18830fe 78
79
65634a81 80 /**
81 * Display the resource with the course blocks.
82 */
83 function display_course_blocks_start() {
6aac6eef 84
5f5cd33c 85 global $CFG, $USER, $THEME;
d18830fe 86
65634a81 87 require_once($CFG->libdir.'/blocklib.php');
88 require_once($CFG->libdir.'/pagelib.php');
89 require_once($CFG->dirroot.'/course/lib.php'); //required by some blocks
d18830fe 90
65634a81 91 $PAGE = page_create_object(PAGE_COURSE_VIEW, $this->course->id);
92 $this->PAGE = $PAGE;
93 $pageblocks = blocks_setup($PAGE);
0440482f 94
65634a81 95 $blocks_preferred_width = bounded_number(180, blocks_preferred_width($pageblocks[BLOCK_POS_LEFT]), 210);
0440482f 96
65634a81 97 /// Print the page header
0440482f 98
65634a81 99 $edit = optional_param('edit', -1, PARAM_BOOL);
0440482f 100
65634a81 101 if (($edit != -1) and $PAGE->user_allowed_editing()) {
102 $USER->editing = $edit;
103 }
d2b23346 104
65634a81 105 $morenavlinks = array($this->strresources => 'index.php?id='.$this->course->id,
106 $this->resource->name => '');
d2b23346 107
5d1381c2 108 $PAGE->print_header($this->course->shortname.': %fullname%', $morenavlinks, "", "",
ca29e37d 109 update_module_button($this->cm->id, $this->course->id, $this->strresource));
0440482f 110
65634a81 111 echo '<table id="layout-table"><tr>';
0440482f 112
f58fcc82 113 $lt = (empty($THEME->layouttable)) ? array('left', 'middle', 'right') : $THEME->layouttable;
114 foreach ($lt as $column) {
115 $lt1[] = $column;
116 if ($column == 'middle') break;
117 }
118 foreach ($lt1 as $column) {
119 switch ($column) {
120 case 'left':
121 if((blocks_have_content($pageblocks, BLOCK_POS_LEFT) || $PAGE->user_is_editing())) {
122 echo '<td style="width: '.$blocks_preferred_width.'px;" id="left-column">';
123 print_container_start();
124 blocks_print_group($PAGE, $pageblocks, BLOCK_POS_LEFT);
125 print_container_end();
126 echo '</td>';
127 }
128 break;
129
130 case 'middle':
131 echo '<td id="middle-column">';
132 print_container_start(false, 'middle-column-wrap');
133 echo '<div id="resource">';
134 break;
135
136 case 'right':
137 if((blocks_have_content($pageblocks, BLOCK_POS_RIGHT) || $PAGE->user_is_editing())) {
138 echo '<td style="width: '.$blocks_preferred_width.'px;" id="right-column">';
139 print_container_start();
140 blocks_print_group($PAGE, $pageblocks, BLOCK_POS_RIGHT);
141 print_container_end();
142 echo '</td>';
143 }
144 break;
145 }
65634a81 146 }
65634a81 147 }
0440482f 148
149
65634a81 150 /**
151 * Finish displaying the resource with the course blocks
152 */
153 function display_course_blocks_end() {
5f5cd33c 154 global $CFG, $THEME;
0440482f 155
65634a81 156 $PAGE = $this->PAGE;
157 $pageblocks = blocks_setup($PAGE);
158 $blocks_preferred_width = bounded_number(180, blocks_preferred_width($pageblocks[BLOCK_POS_RIGHT]), 210);
0440482f 159
f58fcc82 160 $lt = (empty($THEME->layouttable)) ? array('left', 'middle', 'right') : $THEME->layouttable;
161 foreach ($lt as $column) {
162 if ($column != 'middle') {
163 array_shift($lt);
164 } else if ($column == 'middle') {
165 break;
166 }
167 }
168 foreach ($lt as $column) {
169 switch ($column) {
170 case 'left':
171 if((blocks_have_content($pageblocks, BLOCK_POS_LEFT) || $PAGE->user_is_editing())) {
172 echo '<td style="width: '.$blocks_preferred_width.'px;" id="left-column">';
173 print_container_start();
174 blocks_print_group($PAGE, $pageblocks, BLOCK_POS_LEFT);
175 print_container_end();
176 echo '</td>';
177 }
178 break;
179
180 case 'middle':
181 echo '</div>';
182 print_container_end();
183 echo '</td>';
184 break;
185
186 case 'right':
187 if((blocks_have_content($pageblocks, BLOCK_POS_RIGHT) || $PAGE->user_is_editing())) {
188 echo '<td style="width: '.$blocks_preferred_width.'px;" id="right-column">';
189 print_container_start();
190 blocks_print_group($PAGE, $pageblocks, BLOCK_POS_RIGHT);
191 print_container_end();
192 echo '</td>';
193 }
194 break;
195 }
65634a81 196 }
0440482f 197
65634a81 198 echo '</tr></table>';
0440482f 199
65634a81 200 print_footer($this->course);
201
202 }
0440482f 203
204
65634a81 205 function add_instance($resource) {
c18269c7 206 global $DB;
65634a81 207 // Given an object containing all the necessary data,
7cac0c4b 208 // (defined by the form in mod_form.php) this function
65634a81 209 // will create a new instance and return the id number
210 // of the new instance.
2a439ba7 211
65634a81 212 $resource->timemodified = time();
2a439ba7 213
c18269c7 214 return $DB->insert_record("resource", $resource);
65634a81 215 }
2a439ba7 216
cccb016a 217
65634a81 218 function update_instance($resource) {
c18269c7 219 global $DB;
65634a81 220 // Given an object containing all the necessary data,
7cac0c4b 221 // (defined by the form in mod_form.php) this function
65634a81 222 // will update an existing instance with new data.
cccb016a 223
65634a81 224 $resource->id = $resource->instance;
225 $resource->timemodified = time();
cccb016a 226
c18269c7 227 return $DB->update_record("resource", $resource);
65634a81 228 }
cccb016a 229
230
65634a81 231 function delete_instance($resource) {
c18269c7 232 global $DB;
65634a81 233 // Given an object containing the resource data
234 // this function will permanently delete the instance
235 // and any data that depends on it.
cccb016a 236
65634a81 237 $result = true;
cccb016a 238
c18269c7 239 if (! $DB->delete_records("resource", array("id"=>$resource->id))) {
65634a81 240 $result = false;
241 }
2a439ba7 242
65634a81 243 return $result;
244 }
cccb016a 245
65634a81 246 function setup_elements(&$mform) {
247 //override to add your own options
248 }
2a439ba7 249
65634a81 250 function setup_preprocessing(&$default_values){
251 //override to add your own options
252 }
d18830fe 253
d67bfc32 254 function portfolio_prepare_package_uploaded($exporter) {
3efe78df 255 // @todo penny implement later - see MDL-15758
256
257 }
258
d67bfc32 259 function portfolio_prepare_package_online($exporter, $text=false) {
96a38422 260 $filename = clean_filename($this->cm->name . '.' . 'html');
ffcfd8a7 261 $formatoptions = (object)array('noclean' => true);
3efe78df 262 $format = (($text) ? FORMAT_MOODLE : FORMAT_HTML);
263 $content = format_text($this->resource->alltext, $format, $formatoptions, $this->course->id);
6be1dcae 264 return $exporter->write_new_file($content, $filename, false);
3efe78df 265 }
266
ffcfd8a7 267 function portfolio_get_sha1_online($text=false) {
268 $formatoptions = (object)array('noclean' => true);
269 $format = (($text) ? FORMAT_MOODLE : FORMAT_HTML);
270 $content = format_text($this->resource->alltext, $format, $formatoptions, $this->course->id);
271 return sha1($content);
272 }
273
274 function portfolio_get_sha1_uploaded() {
275 // @todo penny implement later.
276 }
277
d18830fe 278} /// end of class definition
279
280
281
282function resource_add_instance($resource) {
283 global $CFG;
ec81373f 284
1b1d3422 285 $resource->type = clean_param($resource->type, PARAM_SAFEDIR); // Just to be safe
79035d46 286
d18830fe 287 require_once("$CFG->dirroot/mod/resource/type/$resource->type/resource.class.php");
1aef6fb7 288 $resourceclass = "resource_$resource->type";
289 $res = new $resourceclass();
d18830fe 290
291 return $res->add_instance($resource);
292}
293
294function resource_update_instance($resource) {
295 global $CFG;
ec81373f 296
1b1d3422 297 $resource->type = clean_param($resource->type, PARAM_SAFEDIR); // Just to be safe
79035d46 298
d18830fe 299 require_once("$CFG->dirroot/mod/resource/type/$resource->type/resource.class.php");
1aef6fb7 300 $resourceclass = "resource_$resource->type";
301 $res = new $resourceclass();
d18830fe 302
303 return $res->update_instance($resource);
304}
305
306function resource_delete_instance($id) {
c18269c7 307 global $CFG, $DB;
ec81373f 308
c18269c7 309 if (! $resource = $DB->get_record("resource", array("id"=>$id))) {
d18830fe 310 return false;
311 }
79035d46 312
1b1d3422 313 $resource->type = clean_param($resource->type, PARAM_SAFEDIR); // Just to be safe
ec81373f 314
d18830fe 315 require_once("$CFG->dirroot/mod/resource/type/$resource->type/resource.class.php");
1aef6fb7 316 $resourceclass = "resource_$resource->type";
317 $res = new $resourceclass();
d18830fe 318
f0c6abbf 319 return $res->delete_instance($resource);
d18830fe 320}
321
322
2a439ba7 323function resource_user_outline($course, $user, $mod, $resource) {
5f5cd33c 324 global $DB;
325
326 if ($logs = $DB->get_records("log", array('userid'=>$user->id, 'module'=>'resource',
327 'action'=>'view', 'info'=>$resource->id), "time ASC")) {
2a439ba7 328
329 $numviews = count($logs);
330 $lastlog = array_pop($logs);
331
9f741612 332 $result = new object();
2a439ba7 333 $result->info = get_string("numviews", "", $numviews);
334 $result->time = $lastlog->time;
335
336 return $result;
337 }
338 return NULL;
339}
340
341
342function resource_user_complete($course, $user, $mod, $resource) {
5f5cd33c 343 global $CFG, $DB;
2a439ba7 344
5f5cd33c 345 if ($logs = $DB->get_records("log", array('userid'=>$user->id, 'module'=>'resource',
346 'action'=>'view', 'info'=>$resource->id), "time ASC")) {
2a439ba7 347 $numviews = count($logs);
348 $lastlog = array_pop($logs);
349
350 $strmostrecently = get_string("mostrecently");
351 $strnumviews = get_string("numviews", "", $numviews);
352
353 echo "$strnumviews - $strmostrecently ".userdate($lastlog->time);
354
355 } else {
4282d7dd 356 print_string("neverseen", "resource");
2a439ba7 357 }
358}
359
84caf038 360function resource_get_participants($resourceid) {
361//Returns the users with data in one resource
362//(NONE, byt must exists on EVERY mod !!)
363
364 return false;
365}
2a439ba7 366
8dddba42 367function resource_get_coursemodule_info($coursemodule) {
ec81373f 368/// Given a course_module object, this function returns any
8dddba42 369/// "extra" information that may be needed when printing
370/// this activity in a course listing.
371///
372/// See get_array_of_activities() in course/lib.php
373///
5f5cd33c 374 global $CFG, $DB;
9d361034 375
376 $info = NULL;
377
5f5cd33c 378 if ($resource = $DB->get_record("resource", array("id"=>$coursemodule->instance), 'id, popup, reference, type, name')) {
dd97c328 379 $info = new object();
1ea543df 380 $info->name = $resource->name;
85e8239e 381 if (!empty($resource->popup)) {
bfca8b17 382 $info->extra = urlencode("onclick=\"this.target='resource$resource->id'; return ".
839f2456 383 "openpopup('/mod/resource/view.php?inpopup=true&amp;id=".
8dddba42 384 $coursemodule->id.
d18830fe 385 "','resource$resource->id','$resource->popup');\"");
8dddba42 386 }
9d361034 387
f1e0649c 388 require_once($CFG->libdir.'/filelib.php');
9d361034 389
c93dae38 390 $customicon = $CFG->dirroot.'/mod/resource/type/'.$resource->type.'/icon.gif';
85e8239e 391 if ($resource->type == 'file') {
ec81373f 392 $icon = mimeinfo("icon", $resource->reference);
9d361034 393 if ($icon != 'unknown.gif') {
ec81373f 394 $info->icon ="f/$icon";
85e8239e 395 } else {
ec81373f 396 $info->icon ="f/web.gif";
9d361034 397 }
d18830fe 398 } else if ($resource->type == 'directory') {
ec81373f 399 $info->icon ="f/folder.gif";
c93dae38 400 } else if (file_exists($customicon)) {
401 $info->icon ='mod/resource/type/'.$resource->type.'/icon.gif';
9d361034 402 }
8dddba42 403 }
404
9d361034 405 return $info;
8dddba42 406}
ec81373f 407
af65e103 408function resource_redirect_tags($text, $url, $tagtoparse, $keytoparse,$prefix = "" ) {
bf46cd22 409 $valid = 1;
af65e103 410 if ( strpos($url,"?") == FALSE ) {
411 $valid = 1;
412 }
413 if ( $valid ) {
414 $lastpoint = strrpos($url,".");
415 $lastslash = strrpos($url,"/");
416 if ( $lastpoint > $lastslash ) {
417 $root = substr($url,0,$lastslash+1);
418 } else {
419 $root = $url;
420 }
ec81373f 421 if ( $root == "http://" or
af65e103 422 $root == "https://") {
423 $root = $url;
424 }
425 if ( substr($root,strlen($root)-1) == '/' ) {
426 $root = substr($root,0,-1);
427 }
ec81373f 428
af65e103 429 $mainroot = $root;
430 $lastslash = strrpos($mainroot,"/");
431 while ( $lastslash > 9) {
432 $mainroot = substr($mainroot,0,$lastslash);
ec81373f 433
af65e103 434 $lastslash = strrpos($mainroot,"/");
435 }
8dddba42 436
ec81373f 437 $regex = "/<$tagtoparse (.+?)>/is";
438 $count = preg_match_all($regex, $text, $hrefs);
af65e103 439 for ( $i = 0; $i < $count; $i++) {
440 $tag = $hrefs[1][$i];
ec81373f 441
af65e103 442 $poshref = strpos(strtolower($tag),strtolower($keytoparse));
443 $start = $poshref + strlen($keytoparse);
444 $left = substr($tag,0,$start);
445 if ( $tag[$start] == '"' ) {
446 $left .= '"';
447 $start++;
448 }
449 $posspace = strpos($tag," ", $start+1);
450 $right = "";
451 if ( $posspace != FALSE) {
452 $right = substr($tag, $posspace);
453 }
454 $end = strlen($tag)-1;
455 if ( $tag[$end] == '"' ) {
456 $right = '"' . $right;
457 }
458 $finalurl = substr($tag,$start,$end-$start+$diff);
459 // Here, we could have these possible values for $finalurl:
460 // file.ext Add current root dir
461 // http://(domain) don't care
462 // http://(domain)/ don't care
463 // http://(domain)/folder don't care
464 // http://(domain)/folder/ don't care
465 // http://(domain)/folder/file.ext don't care
466 // folder/ Add current root dir
467 // folder/file.ext Add current root dir
468 // /folder/ Add main root dir
469 // /folder/file.ext Add main root dir
470
471 // Special case: If finalurl contains a ?, it won't be parsed
bf46cd22 472 $valid = 1;
af65e103 473
474 if ( strpos($finalurl,"?") == FALSE ) {
475 $valid = 1;
476 }
477 if ( $valid ) {
478 if ( $finalurl[0] == "/" ) {
479 $finalurl = $mainroot . $finalurl;
ec81373f 480 } elseif ( strtolower(substr($finalurl,0,7)) != "http://" and
af65e103 481 strtolower(substr($finalurl,0,8)) != "https://") {
482 if ( $finalurl[0] == "/") {
483 $finalurl = $mainroot . $finalurl;
484 } else {
485 $finalurl = "$root/$finalurl";
486 }
487 }
ec81373f 488
af65e103 489 $text = str_replace($tag,"$left$prefix$finalurl$right",$text);
490 }
491 }
492 }
493 return $text;
494}
8dddba42 495
d18830fe 496function resource_is_url($path) {
427c8ccb 497 if (strpos($path, '://')) { // eg http:// https:// ftp:// etc
d18830fe 498 return true;
499 }
427c8ccb 500 if (strpos($path, '/') === 0) { // Starts with slash
501 return true;
502 }
503 return false;
d18830fe 504}
505
89bfeee0 506function resource_get_types() {
9f741612 507 global $CFG;
508
89bfeee0 509 $types = array();
6da4b261 510
3d30a455 511 $standardresources = array('text','html','file','directory');
6da4b261 512 foreach ($standardresources as $resourcetype) {
89bfeee0 513 $type = new object();
514 $type->modclass = MOD_CLASS_RESOURCE;
d81f018f 515 $type->name = $resourcetype;
89bfeee0 516 $type->type = "resource&amp;type=$resourcetype";
517 $type->typestr = get_string("resourcetype$resourcetype", 'resource');
518 $types[] = $type;
6da4b261 519 }
520
521 /// Drop-in extra resource types
522 $resourcetypes = get_list_of_plugins('mod/resource/type');
523 foreach ($resourcetypes as $resourcetype) {
37147357 524 if (!empty($CFG->{'resource_hide_'.$resourcetype})) { // Not wanted
525 continue;
526 }
89bfeee0 527 if (!in_array($resourcetype, $standardresources)) {
528 $type = new object();
529 $type->modclass = MOD_CLASS_RESOURCE;
d81f018f 530 $type->name = $resourcetype;
89bfeee0 531 $type->type = "resource&amp;type=$resourcetype";
13ca1e06 532 $type->typestr = resource_get_name($resourcetype);
89bfeee0 533 $types[] = $type;
6da4b261 534 }
535 }
89bfeee0 536
537 return $types;
6da4b261 538}
f3221af9 539
540function resource_get_view_actions() {
541 return array('view','view all');
542}
543
544function resource_get_post_actions() {
545 return array();
546}
547
a69be0d8 548function resource_renamefiles($course, $wdir, $oldname, $name) {
5f5cd33c 549 global $CFG, $DB;
a69be0d8 550
551 $status = '<p align=\"center\"><strong>'.get_string('affectedresources', 'resource').':</strong><ul>';
552 $updates = false;
553
554 $old = trim($wdir.'/'.$oldname, '/');
555 $new = trim($wdir.'/'.$name, '/');
556
557 $sql = "SELECT r.id, r.reference, r.name, cm.id AS cmid
5f5cd33c 558 FROM {resource} r, {course_modules} cm, {modules} m
559 WHERE r.course = :courseid
560 AND m.name = 'resource'
561 AND cm.module = m.id
562 AND cm.instance = r.id
563 AND (r.type = 'file' OR r.type = 'directory')
564 AND (r.reference LIKE :old1 OR r.reference = :old2)";
565 $params = array('courseid'=>$course->id, 'old1'=>"{$old}/%", 'old2'=>$old);
566 if ($resources = $DB->get_records_sql($sql, $params)) {
a69be0d8 567 foreach ($resources as $resource) {
568 $r = new object();
569 $r->id = $resource->id;
570 $r->reference = '';
571 if ($resource->reference == $old) {
5f5cd33c 572 $r->reference = $new;
a69be0d8 573 } else {
5f5cd33c 574 $r->reference = preg_replace('|^'.preg_quote($old, '|').'/|', $new.'/', $resource->reference);
a69be0d8 575 }
576 if ($r->reference !== '') {
577 $updates = true;
578 $status .= "<li><a href=\"$CFG->wwwroot/mod/resource/view.php?id=$resource->cmid\" target=\"_blank\">$resource->name</a>: $resource->reference ==> $r->reference</li>";
579 if (!empty($CFG->resource_autofilerename)) {
5f5cd33c 580 if (!$DB->update_record('resource', $r)) {
baa336f0 581 print_error('cannotupdate', 'resource');
a69be0d8 582 }
583 }
584 }
585 }
586 }
587 $status .= '</ul></p>';
588
589 if ($updates) {
590 echo $status;
591 if (empty($CFG->resource_autofilerename)) {
592 notify(get_string('warningdisabledrename', 'resource'));
593 }
594 }
595}
596
597function resource_delete_warning($course, $files) {
5f5cd33c 598 global $CFG, $DB;
a69be0d8 599
600 $found = array();
601
602 foreach($files as $key=>$file) {
603 $files[$key] = trim($file, '/');
604 }
605 $sql = "SELECT r.id, r.reference, r.name, cm.id AS cmid
5f5cd33c 606 FROM {resource} r,
607 {course_modules} cm,
608 {modules} m
609 WHERE r.course = ?
610 AND m.name = 'resource'
611 AND cm.module = m.id
612 AND cm.instance = r.id
613 AND (r.type = 'file' OR r.type = 'directory')";
614 if ($resources = $DB->get_records_sql($sql, array($course->id))) {
a69be0d8 615 foreach ($resources as $resource) {
616 if ($resource->reference == '') {
617 continue; // top shared directory does not prevent anything
618 }
619 if (in_array($resource->reference, $files)) {
620 $found[$resource->id] = $resource;
621 } else {
622 foreach($files as $file) {
623 if (preg_match('|^'.preg_quote($file, '|').'/|', $resource->reference)) {
624 $found[$resource->id] = $resource;
625 }
626 }
627 }
628 }
629 }
630
631 if (!empty($found)) {
632
633 print_simple_box_start("center");
634 echo '<p><strong>'.get_string('affectedresources', 'resource').':</strong><ul>';
635 foreach($found as $resource) {
636 echo "<li><a href=\"$CFG->wwwroot/mod/resource/view.php?id=$resource->cmid\" target=\"_blank\">$resource->name</a>: $resource->reference</li>";
637 }
638 echo '</ul></p>';
639 print_simple_box_end();
640
641 return true;
642 } else {
643 return false;
644 }
645}
646
0b5a80a1 647/**
648 * This function is used by the reset_course_userdata function in moodlelib.
649 * @param $data the data submitted from the reset course.
650 * @return array status array
651 */
652function resource_reset_userdata($data) {
653 return array();
654}
f432bebf 655
656/**
657 * Returns all other caps used in module
658 */
659function resource_get_extra_capabilities() {
660 return array('moodle/site:accessallgroups');
661}
662
3efe78df 663class resource_portfolio_caller extends portfolio_module_caller_base {
664
665 private $resource;
666 private $resourcefile;
667
0d06b6fd 668 public static function expected_callbackargs() {
669 return array(
670 'id' => true,
671 );
672 }
673
674 public function load_data() {
675 global $CFG, $DB;
676 if (!$this->cm = get_coursemodule_from_instance('resource', $this->id)) {
3bb8a2c7 677 throw new portfolio_caller_exception('invalidid');
3efe78df 678 }
679 $this->cm->type = $DB->get_field('resource', 'type', array('id' => $this->cm->instance));
680 $resourceclass = 'resource_'. $this->cm->type;
681 $this->resourcefile = $CFG->dirroot.'/mod/resource/type/'.$this->cm->type.'/resource.class.php';
682 require_once($this->resourcefile);
683 $this->resource= new $resourceclass($this->cm->id);
759204f8 684 if (!is_callable(array($this->resource, 'portfolio_prepare_package')) || !is_callable(array($this->resource, 'portfolio_get_sha1'))) {
5d1381c2 685 debug_print_backtrace();
c5046670 686 throw new portfolio_exception('portfolionotimplemented', 'resource', null, $this->cm->type);
687 }
0d06b6fd 688 $this->supportedformats = array(self::type_to_format($this->cm->type));
689 }
690
691 public static function type_to_format($type) {
ea0de12f 692 // this is kind of yuk... but there's just not good enough OO here
693 $format = PORTFOLIO_FORMAT_FILE;
0d06b6fd 694 switch ($type) {
ea0de12f 695 case 'html':
6be1dcae 696 $format = PORTFOLIO_FORMAT_PLAINHTML;
ea0de12f 697 case 'text':
698 $format = PORTFOLIO_FORMAT_TEXT;
699 case 'file':
e4af1dee 700 // $format = portfolio_format_from_file($file); // change after we switch upload type resources over to new files api.
ea0de12f 701 }
0d06b6fd 702 return $format;
3efe78df 703 }
704
705 public function __wakeup() {
654119b5 706 global $CFG;
707 if (empty($CFG)) {
708 return; // too early yet
709 }
3efe78df 710 require_once($this->resourcefile);
711 $this->resource = unserialize(serialize($this->resource));
712 }
713
714 public function expected_time() {
ea0de12f 715 // @todo penny check filesize if the type is uploadey (not implemented yet)
d8606b20 716 // like this: return portfolio_expected_time_file($this->file); // or whatever
3efe78df 717 return PORTFOLIO_TIME_LOW;
718 }
719
d67bfc32 720 public function prepare_package() {
721 return $this->resource->portfolio_prepare_package($this->exporter);
3efe78df 722 }
723
3efe78df 724 public function check_permissions() {
494e47e4 725 return has_capability('mod/resource:exportresource', get_context_instance(CONTEXT_MODULE, $this->cm->id));
3efe78df 726 }
727
866d543f 728 public static function add_button($resource, $format=null, $return=false) {
494e47e4 729 if (!has_capability('mod/resource:exportresource', get_context_instance(CONTEXT_MODULE, $resource->cm->id))) {
730 return;
731 }
ffcfd8a7 732 if (!is_callable(array($resource, 'portfolio_prepare_package')) || !is_callable(array($resource, 'portfolio_get_sha1'))) {
3efe78df 733 debugging(get_string('portfolionotimplemented', 'resource'));
734 return false;
735 }
759204f8 736 $callersupports = array(self::type_to_format($resource->resource->type));
737 if ($resource->resource->type == 'file') {
e4af1dee 738 // $callersupports = array(portfolio_format_from_file($file);
739 }
0d06b6fd 740 $button = new portfolio_add_button();
741 $button->set_callback_options('resource_portfolio_caller', array('id' => $resource->cm->instance), '/mod/resource/lib.php');
742 $button->set_formats($callersupports);
743 if ($return) {
744 return $button->to_html($format);
745 }
746 $button->render($format);
3efe78df 747 }
ffcfd8a7 748
749 public function get_sha1() {
750 return $this->resource->portfolio_get_sha1();
751 }
752
753 public static function display_name() {
754 return get_string('modulename', 'resource');
755 }
3efe78df 756}
757
18a2a0cb 758/**
759 * @param string $feature FEATURE_xx constant for requested feature
760 * @return mixed True if module supports feature, null if doesn't know
761 */
762function resource_supports($feature) {
763 switch($feature) {
42f103be 764 case FEATURE_GROUPS: return false;
765 case FEATURE_GROUPINGS: return false;
766 case FEATURE_GROUPMEMBERSONLY: return true;
767 case FEATURE_MODEDIT_INTRO_EDITOR: return true;
18a2a0cb 768 case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
42f103be 769 case FEATURE_GRADE_HAS_GRADE: return false;
770 case FEATURE_GRADE_OUTCOMES: return false;
771
18a2a0cb 772 default: return null;
773 }
774}
775
13ca1e06 776/**
777 * Returns the full name of the given resource type. The name can
778 * either be set at the resource type level or at the resource module
779 * level.
780 *
781 * @param string $type shortname (or directory name) of the resource type
782 */
783function resource_get_name($type) {
784 $name = get_string("resourcetype$type", "resource_$type");
785 if (substr($name, 0, 2) === '[[') {
786 $name = get_string("resourcetype$type", 'resource');
787 }
788 return $name;
789}
790
2a439ba7 791?>