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