MDL-33552 - portfolio api E_STRICT fixes
[moodle.git] / lib / portfolio / formats.php
CommitLineData
87fcac8d 1<?php
93dd2725
RW
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
87fcac8d 17/**
87fcac8d 18 * This file contains all the class definitions of the export formats.
18cdcdbf 19 *
87fcac8d 20 * They are implemented in php classes rather than just a simpler hash
21 * Because it provides an easy way to do subtyping using php inheritance.
93dd2725
RW
22 *
23 * @package core_portfolio
24 * @copyright 2008 Penny Leach <penny@catalyst.net.nz>,
25 * Martin Dougiamas <http://dougiamas.com>
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
87fcac8d 27 */
28
0972665f
PS
29defined('MOODLE_INTERNAL') || die();
30
87fcac8d 31/**
93dd2725 32 * Base class to inherit from.
18cdcdbf 33 *
93dd2725
RW
34 * Do not use this anywhere in supported_formats
35 *
36 * @package core_portfolio
37 * @category portfolio
38 * @copyright 2008 Penny Leach <penny@catalyst.net.nz>,
39 * Martin Dougiamas <http://dougiamas.com>
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 *
8192522e
PL
42 */
43abstract class portfolio_format {
086831a6
PL
44
45 /**
93dd2725
RW
46 * Array of mimetypes this format supports
47 *
48 * @throws coding_exception
086831a6 49 */
7c109ea3
PS
50 public static function mimetypes() {
51 throw new coding_exception('mimetypes() method needs to be overridden in each subclass of portfolio_format');
52 }
6be1dcae 53
086831a6 54 /**
93dd2725
RW
55 * For multipart formats, eg html with attachments,
56 * we need to have a directory to place associated files from
57 * inside the zip file. This is the name of that directory
58 *
59 * @throws coding_exception
086831a6 60 */
7c109ea3
PS
61 public static function get_file_directory() {
62 throw new coding_exception('get_file_directory() method needs to be overridden in each subclass of portfolio_format');
63 }
6be1dcae 64
086831a6 65 /**
93dd2725 66 * Given a file, return a snippet of markup in whatever format
086831a6 67 * to link to that file.
18cdcdbf 68 * Usually involves the path given by get_file_directory.
93dd2725 69 * This is not supported in subclasses of portfolio_format_file
59dd457e 70 * since they're all just single files.
18cdcdbf 71 * @see get_file_directory
8192522e 72 *
93dd2725
RW
73 * @param stored_file $file file information object
74 * @param array $options array of options to pass. can contain:
8192522e 75 * attributes => hash of existing html attributes (eg title, height, width, etc)
8192522e 76 *
93dd2725 77 * @throws coding_exception
086831a6 78 */
7c109ea3
PS
79 public static function file_output($file, $options=null) {
80 throw new coding_exception('file_output() method needs to be overridden in each subclass of portfolio_format');
81 }
8192522e 82
93dd2725
RW
83 /**
84 * Create portfolio tag
85 *
86 * @param stored_file $file file information object
87 * @param string $path file path
88 * @param array $attributes portfolio attributes
89 * @return string
90 */
8192522e
PL
91 public static function make_tag($file, $path, $attributes) {
92 $srcattr = 'href';
93 $tag = 'a';
94 $content = $file->get_filename();
95 if (in_array($file->get_mimetype(), portfolio_format_image::mimetypes())) {
96 $srcattr = 'src';
97 $tag = 'img';
98 $content = '';
99 }
100
101 $attributes[$srcattr] = $path; // this will override anything we might have been passed (which is good)
102 $dom = new DomDocument();
103 $elem = null;
104 if ($content) {
105 $elem = $dom->createElement($tag, $content);
106 } else {
107 $elem = $dom->createElement($tag);
108 }
109
110 foreach ($attributes as $key => $value) {
111 $elem->setAttribute($key, $value);
112 }
113 $dom->appendChild($elem);
114 return $dom->saveXML($elem);
59dd457e
PL
115 }
116
117 /**
93dd2725
RW
118 * Whether this format conflicts with the given format.
119 * This is used for the case where an export location
59dd457e 120 * "generally" supports something like FORMAT_PLAINHTML
93dd2725
RW
121 * but then in a specific export case, must add attachments,
122 * which means that FORMAT_RICHHTML is supported in that case,
59dd457e
PL
123 * which implies removing support for FORMAT_PLAINHTML.
124 * Note that conflicts don't have to be bi-directional
125 * (eg FORMAT_PLAINHTML conflicts with FORMAT_RICHHTML
126 * but not the other way around) and things within the class hierarchy
127 * are resolved automatically anyway.
59dd457e
PL
128 * This is really just between subclasses of format_rich
129 * and subclasses of format_file.
130 *
131 * @param string $format one of the FORMAT_XX constants
93dd2725 132 * @return bool
59dd457e
PL
133 */
134 public static function conflicts($format) {
135 return false;
6be1dcae 136 }
87fcac8d 137}
138
8192522e 139/**
93dd2725
RW
140 * The most basic type - pretty much everything is a subtype
141 *
142 * @package core_portfolio
143 * @category portfolio
144 * @copyright 2009 Penny Leach <penny@catalyst.net.nz>, Martin Dougiamas
145 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
146 */
8192522e
PL
147class portfolio_format_file extends portfolio_format {
148
93dd2725
RW
149 /**
150 * Array of mimetypes this format supports
151 *
152 * @return array
153 */
8192522e
PL
154 public static function mimetypes() {
155 return array();
156 }
157
93dd2725
RW
158 /**
159 * For multipart formats, eg html with attachments,
160 * we need to have a directory to place associated files from
161 * inside the zip file. This is the name of that directory
162 *
163 * @return bool
164 */
8192522e 165 public static function get_file_directory() {
82e8d268 166 return false;
8192522e
PL
167 }
168
93dd2725
RW
169 /**
170 * Given a file, return a snippet of markup in whatever format
171 * to link to that file.
18cdcdbf 172 * Usually involves the path given by get_file_directory.
93dd2725
RW
173 * This is not supported in subclasses of portfolio_format_file
174 * since they're all just single files.
18cdcdbf 175 * @see get_file_directory
93dd2725
RW
176 *
177 * @param stored_file $file informations object
178 * @param array $options array of options to pass. can contain:
179 * attributes => hash of existing html attributes (eg title, height, width, etc)
180 */
8192522e
PL
181 public static function file_output($file, $options=null) {
182 throw new portfolio_exception('fileoutputnotsupported', 'portfolio');
183 }
184}
185
87fcac8d 186/**
93dd2725
RW
187 * Image format, subtype of file.
188 *
189 * @package core_portfolio
190 * @category portfolio
191 * @copyright 2009 Penny Leach
192 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
193 */
87fcac8d 194class portfolio_format_image extends portfolio_format_file {
086831a6 195 /**
93dd2725
RW
196 * Return all mimetypes that use image.gif (eg all images)
197 *
198 * @return string
086831a6 199 */
87fcac8d 200 public static function mimetypes() {
559276b1 201 return file_get_typegroup('type', 'image');
87fcac8d 202 }
50938e30 203
93dd2725
RW
204 /**
205 * Whether this format conflicts with the given format.
206 * This is used for the case where an export location
207 * "generally" supports something like FORMAT_PLAINHTML
208 * but then in a specific export case, must add attachments,
209 * which means that FORMAT_RICHHTML is supported in that case,
210 * which implies removing support for FORMAT_PLAINHTML.
211 * Note that conflicts don't have to be bi-directional
212 * (eg FORMAT_PLAINHTML conflicts with FORMAT_RICHHTML
213 * but not the other way around) and things within the class hierarchy
214 * are resolved automatically anyway.
215 * This is really just between subclasses of format_rich
216 * and subclasses of format_file.
217 *
218 * @param string $format one of the FORMAT_XX constants
219 * @return bool
220 */
50938e30
PL
221 public static function conflicts($format) {
222 return ($format == PORTFOLIO_FORMAT_RICHHTML
223 || $format == PORTFOLIO_FORMAT_PLAINHTML);
224 }
87fcac8d 225}
226
227/**
93dd2725 228 * HTML format
18cdcdbf 229 *
93dd2725
RW
230 * Could be used for an external cms or something in case we want to be really specific.
231 *
232 * @package core_portfolio
233 * @category portfolio
234 * @copyright 2008 Penny Leach
235 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
236 */
6be1dcae 237class portfolio_format_plainhtml extends portfolio_format_file {
59dd457e 238
93dd2725
RW
239 /**
240 * Return html mimetype
241 *
242 * @return array
243 */
87fcac8d 244 public static function mimetypes() {
245 return array('text/html');
246 }
59dd457e 247
93dd2725
RW
248 /**
249 * Whether this format conflicts with the given format.
250 * This is used for the case where an export location
251 * "generally" supports something like FORMAT_PLAINHTML
252 * but then in a specific export case, must add attachments,
253 * which means that FORMAT_RICHHTML is supported in that case,
254 * which implies removing support for FORMAT_PLAINHTML.
255 * Note that conflicts don't have to be bi-directional
256 * (eg FORMAT_PLAINHTML conflicts with FORMAT_RICHHTML
257 * but not the other way around) and things within the class hierarchy
258 * are resolved automatically anyway.
259 * This is really just between subclasses of format_rich
260 * and subclasses of format_file.
261 *
262 * @param string $format one of the FORMAT_XX constants
263 * @return bool
264 */
59dd457e 265 public static function conflicts($format) {
549c1ae6
PL
266 return ($format == PORTFOLIO_FORMAT_RICHHTML
267 || $format == PORTFOLIO_FORMAT_FILE);
59dd457e 268 }
87fcac8d 269}
270
271/**
93dd2725 272 * Video format
18cdcdbf 273 *
93dd2725
RW
274 * For portfolio plugins that support videos specifically
275 *
276 * @package core_portfolio
277 * @category portfolio
278 * @copyright 2008 Penny Leach
279 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
280 */
87fcac8d 281class portfolio_format_video extends portfolio_format_file {
93dd2725
RW
282
283 /**
284 * Return video mimetypes
285 *
286 * @return array
287 */
87fcac8d 288 public static function mimetypes() {
559276b1 289 return file_get_typegroup('type', 'video');
87fcac8d 290 }
291}
292
293/**
93dd2725 294 * Class for plain text format.
18cdcdbf 295 *
93dd2725
RW
296 * Not sure why we would need this yet,
297 * but since resource module wants to export it... we can
298 *
299 * @package core_portfolio
300 * @category portfolio
301 * @copyright 2008 Penny Leach
302 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
303 */
87fcac8d 304class portfolio_format_text extends portfolio_format_file {
93dd2725
RW
305
306 /**
307 * Return plain text mimetypes
308 *
309 * @return array
310 */
87fcac8d 311 public static function mimetypes() {
312 return array('text/plain');
313 }
59dd457e 314
93dd2725
RW
315 /**
316 * Whether this format conflicts with the given format.
317 * This is used for the case where an export location
318 * "generally" supports something like FORMAT_PLAINHTML
319 * but then in a specific export case, must add attachments,
320 * which means that FORMAT_RICHHTML is supported in that case,
321 * which implies removing support for FORMAT_PLAINHTML.
322 * Note that conflicts don't have to be bi-directional
323 * (eg FORMAT_PLAINHTML conflicts with FORMAT_RICHHTML
324 * but not the other way around) and things within the class hierarchy
325 * are resolved automatically anyway.
326 * This is really just between subclasses of format_rich
327 * and subclasses of format_file.
328 *
329 * @param string $format one of the FORMAT_XX constants
330 * @return bool
331 */
59dd457e
PL
332 public static function conflicts($format ) {
333 return ($format == PORTFOLIO_FORMAT_PLAINHTML
334 || $format == PORTFOLIO_FORMAT_RICHHTML);
335 }
87fcac8d 336}
337
086831a6 338/**
93dd2725 339 * Base class for rich formats.
18cdcdbf 340 *
93dd2725
RW
341 * These are multipart - eg things with attachments
342 *
343 * @package core_portfolio
344 * @category portfolio
345 * @copyright 2009 Penny Leach
346 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
086831a6 347 */
8192522e 348abstract class portfolio_format_rich extends portfolio_format {
59dd457e 349
93dd2725
RW
350 /**
351 * Return rich text mimetypes
352 *
353 * @return array
354 */
8192522e
PL
355 public static function mimetypes() {
356 return array();
59dd457e
PL
357 }
358
6be1dcae 359}
360
086831a6 361/**
93dd2725 362 * Richhtml - html with attachments.
18cdcdbf 363 *
93dd2725 364 * The most commonly used rich format
086831a6 365 * eg inline images
93dd2725
RW
366 *
367 * @package core_portfolio
368 * @category portfolio
369 * @copyright 2009 Penny Leach
370 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
086831a6 371 */
6be1dcae 372class portfolio_format_richhtml extends portfolio_format_rich {
93dd2725
RW
373
374 /**
375 * For multipart formats, eg html with attachments,
376 * we need to have a directory to place associated files from
377 * inside the zip file. this is the name of that directory
378 *
379 * @return string
380 */
6be1dcae 381 public static function get_file_directory() {
8192522e 382 return 'site_files/';
6be1dcae 383 }
93dd2725
RW
384
385 /**
386 * Given a file, return a snippet of markup in whatever format
387 * to link to that file.
18cdcdbf 388 * Usually involves the path given by get_file_directory.
93dd2725
RW
389 * This is not supported in subclasses of portfolio_format_file
390 * since they're all just single files.
18cdcdbf 391 * @see get_file_directory
93dd2725
RW
392 *
393 * @param stored_file $file information for existing file
394 * @param array $options array of options to pass. can contain:
395 * attributes => hash of existing html attributes (eg title, height, width, etc)
396 * @return string
397 */
8192522e
PL
398 public static function file_output($file, $options=null) {
399 $path = self::get_file_directory() . $file->get_filename();
400 $attributes = array();
401 if (!empty($options['attributes']) && is_array($options['attributes'])) {
402 $attributes = $options['attributes'];
6be1dcae 403 }
8192522e 404 return self::make_tag($file, $path, $attributes);
6be1dcae 405 }
93dd2725
RW
406
407 /**
408 * Whether this format conflicts with the given format.
409 * This is used for the case where an export location
410 * "generally" supports something like FORMAT_PLAINHTML
411 * but then in a specific export case, must add attachments,
412 * which means that FORMAT_RICHHTML is supported in that case,
413 * which implies removing support for FORMAT_PLAINHTML.
414 * Note that conflicts don't have to be bi-directional
415 * (eg FORMAT_PLAINHTML conflicts with FORMAT_RICHHTML
416 * but not the other way around) and things within the class hierarchy
417 * are resolved automatically anyway.
418 * This is really just between subclasses of format_rich
419 * and subclasses of format_file.
420 *
421 * @todo MDL-31305 - revisit the conflict with file, since we zip here
422 * @param string $format one of the FORMAT_XX constants
423 * @return bool
424 */
59dd457e
PL
425 public static function conflicts($format) { // TODO revisit the conflict with file, since we zip here
426 return ($format == PORTFOLIO_FORMAT_PLAINHTML || $format == PORTFOLIO_FORMAT_FILE);
427 }
428
6be1dcae 429}
430
93dd2725
RW
431/**
432 * Class used for leap2a format
433 *
434 * @package core_portfolio
435 * @category portfolio
436 * @copyright 2009 Penny Leach
437 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
438 */
59dd457e
PL
439class portfolio_format_leap2a extends portfolio_format_rich {
440
93dd2725
RW
441 /**
442 * For multipart formats, eg html with attachments,
443 * we need to have a directory to place associated files from
444 * inside the zip file. this is the name of that directory
445 *
446 * @return string
447 */
59dd457e
PL
448 public static function get_file_directory() {
449 return 'files/';
450 }
451
93dd2725
RW
452 /**
453 * Return the file prefix
454 *
455 * @return string
456 */
ebb7e782
PL
457 public static function file_id_prefix() {
458 return 'storedfile';
459 }
460
59dd457e 461 /**
93dd2725 462 * Return the link to a file
59dd457e 463 *
93dd2725
RW
464 * @param stored_file $file information for existing file
465 * @param array $options array of options to pass. can contain:
466 * attributes => hash of existing html attributes (eg title, height, width, etc)
467 * @return string
59dd457e 468 */
8192522e 469 public static function file_output($file, $options=null) {
59dd457e 470 $id = '';
8192522e
PL
471 if (!is_array($options)) {
472 $options = array();
473 }
ebb7e782
PL
474 if (!array_key_exists('entry', $options)) {
475 $options['entry'] = true;
476 }
8192522e 477 if (!empty($options['entry'])) {
ebb7e782 478 $path = 'portfolio:' . self::file_id_prefix() . $file->get_id();
59dd457e 479 } else {
8192522e
PL
480 $path = self::get_file_directory() . $file->get_filename();
481 }
482 $attributes = array();
483 if (!empty($options['attributes']) && is_array($options['attributes'])) {
484 $attributes = $options['attributes'];
59dd457e 485 }
8192522e
PL
486 $attributes['rel'] = 'enclosure';
487 return self::make_tag($file, $path, $attributes);
59dd457e
PL
488 }
489
93dd2725
RW
490 /**
491 * Generate portfolio_format_leap2a
492 *
493 * @param stdclass $user user information object
494 * @return portfolio_format_leap2a_writer
495 */
59dd457e
PL
496 public static function leap2a_writer(stdclass $user=null) {
497 global $CFG;
498 if (empty($user)) {
499 global $USER;
500 $user = $USER;
501 }
502 require_once($CFG->libdir . '/portfolio/formats/leap2a/lib.php');
503 return new portfolio_format_leap2a_writer($user);
504 }
505
93dd2725
RW
506 /**
507 * Return the manifest name
508 *
509 * @return string
510 */
59dd457e
PL
511 public static function manifest_name() {
512 return 'leap2a.xml';
513 }
514}
6be1dcae 515
516
93dd2725
RW
517// later.... a moodle plugin might support this.
518// it's commented out in portfolio_supported_formats so cannot currently be used.
59dd457e 519//class portfolio_format_mbkp extends portfolio_format_rich {}
a9ec9031
DP
520
521/**
93dd2725 522 * 'PDF format', subtype of file.
18cdcdbf 523 *
93dd2725
RW
524 * For portfolio plugins that support PDFs specifically.
525 *
526 * @package core_portfolio
527 * @category portfolio
528 * @copyright 2009 Dan Poltawski
529 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
530 */
a9ec9031 531class portfolio_format_pdf extends portfolio_format_file {
93dd2725
RW
532
533 /**
534 * Return pdf mimetypes
535 *
536 * @return array
537 */
a9ec9031
DP
538 public static function mimetypes() {
539 return array('application/pdf');
540 }
541}
542
543/**
93dd2725 544 * 'Document format', subtype of file.
18cdcdbf 545 *
93dd2725
RW
546 * For portfolio plugins that support documents specifically.
547 *
548 * @package core_portfolio
549 * @category portfolio
550 * @copyright 2009 Dan Poltawski
551 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
552 */
a9ec9031 553class portfolio_format_document extends portfolio_format_file {
93dd2725
RW
554
555 /**
556 * Return documents mimetypes
557 *
558 * @return array of documents mimetypes
559 */
a9ec9031 560 public static function mimetypes() {
559276b1 561 return file_get_typegroup('type', 'document');
a9ec9031
DP
562 }
563}
564
565/**
93dd2725 566 * 'Spreadsheet format', subtype of file.
18cdcdbf 567 *
93dd2725
RW
568 * For portfolio plugins that support spreadsheets specifically.
569 *
570 * @package core_portfolio
571 * @category portfolio
572 * @copyright 2009 Dan Poltawski
573 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
574 */
a9ec9031 575class portfolio_format_spreadsheet extends portfolio_format_file {
93dd2725
RW
576
577 /**
578 * Return spreadsheet spreadsheet mimetypes
579 *
580 * @return array of documents mimetypes
581 */
a9ec9031 582 public static function mimetypes() {
559276b1 583 return file_get_typegroup('type', 'spreadsheet');
a9ec9031
DP
584 }
585}
586
587/**
93dd2725 588 * 'Presentation format', subtype of file.
18cdcdbf 589 *
93dd2725
RW
590 * For portfolio plugins that support presentation specifically.
591 *
592 * @package core_portfolio
593 * @category portfolio
594 * @copyright 2009 Dan Poltawski
595 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
596 */
a9ec9031 597class portfolio_format_presentation extends portfolio_format_file {
93dd2725
RW
598
599 /**
600 * Return presentation documents mimetypes
601 *
602 * @return array presentation document mimetypes
603 */
a9ec9031 604 public static function mimetypes() {
559276b1 605 return file_get_typegroup('type', 'presentation');
a9ec9031
DP
606 }
607}