portfolio: MDL-21030 added helper function to add multiple attachments
[moodle.git] / lib / portfolio / formats.php
CommitLineData
87fcac8d 1<?php
2/**
3 * Moodle - Modular Object-Oriented Dynamic Learning Environment
4 * http://moodle.org
5 * Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 * @package moodle
21 * @subpackage portfolio
22 * @author Penny Leach <penny@catalyst.net.nz>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL
24 * @copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com
25 *
26 * This file contains all the class definitions of the export formats.
27 * They are implemented in php classes rather than just a simpler hash
28 * Because it provides an easy way to do subtyping using php inheritance.
29 */
30
31/**
32* the most basic type - pretty much everything is a subtype
33*/
34class portfolio_format_file {
086831a6
PL
35
36 /**
37 * array of mimetypes this format supports
38 */
87fcac8d 39 public static function mimetypes() {
40 return array(null);
41 }
6be1dcae 42
086831a6
PL
43 /**
44 * for multipart formats, eg html with attachments,
45 * we need to have a directory to place associated files in
46 * inside the zip file. this is the name of that directory
47 */
6be1dcae 48 public static function get_file_directory() {
49 return null;
50 }
51
086831a6
PL
52 /**
53 * given a file, return a snippet of markup in whatever format
54 * to link to that file.
55 * usually involves the path given by {@link get_file_directory}
59dd457e
PL
56 * this is not supported in subclasses of portfolio_format_file
57 * since they're all just single files.
086831a6 58 */
6be1dcae 59 public static function file_output($file) {
59dd457e
PL
60 throw new portfolio_exception('fileoutputnotsupported', 'portfolio');
61 }
62
63 /**
64 * whether this format conflicts with the given format
65 * this is used for the case where an export location
66 * "generally" supports something like FORMAT_PLAINHTML
67 * but then in a specific export case, must add attachments
68 * which means that FORMAT_RICHHTML is supported in that case
69 * which implies removing support for FORMAT_PLAINHTML.
70 * Note that conflicts don't have to be bi-directional
71 * (eg FORMAT_PLAINHTML conflicts with FORMAT_RICHHTML
72 * but not the other way around) and things within the class hierarchy
73 * are resolved automatically anyway.
74 *
75 * This is really just between subclasses of format_rich
76 * and subclasses of format_file.
77 *
78 * @param string $format one of the FORMAT_XX constants
79 *
80 * @return boolean
81 */
82 public static function conflicts($format) {
83 return false;
6be1dcae 84 }
87fcac8d 85}
86
87/**
88* image format, subtype of file.
89*/
90class portfolio_format_image extends portfolio_format_file {
086831a6
PL
91 /**
92 * return all mimetypes that use image.gif (eg all images)
93 */
87fcac8d 94 public static function mimetypes() {
95 return mimeinfo_from_icon('type', 'image.gif', true);
96 }
97}
98
99/**
100* html format - could be used for an external cms or something
101*
102* in case we want to be really specific.
103*/
6be1dcae 104class portfolio_format_plainhtml extends portfolio_format_file {
59dd457e 105
87fcac8d 106 public static function mimetypes() {
107 return array('text/html');
108 }
59dd457e
PL
109
110 public static function conflicts($format) {
111 return ($format == PORTFOLIO_FORMAT_RICHHTML);
112 }
87fcac8d 113}
114
115/**
116* video format, subtype of file.
117*
086831a6 118* for portfolio plugins that support videos specifically
87fcac8d 119*/
120class portfolio_format_video extends portfolio_format_file {
121 public static function mimetypes() {
d02b207a 122 return array_merge(
086831a6
PL
123 mimeinfo_from_icon('type', 'video.gif', true),
124 mimeinfo_from_icon('type', 'avi.gif', true)
125 );
87fcac8d 126 }
127}
128
129/**
130* class for plain text format.. not sure why we would need this yet
131* but since resource module wants to export it... we can
132*/
133class portfolio_format_text extends portfolio_format_file {
134 public static function mimetypes() {
135 return array('text/plain');
136 }
59dd457e
PL
137
138 public static function conflicts($format ) {
139 return ($format == PORTFOLIO_FORMAT_PLAINHTML
140 || $format == PORTFOLIO_FORMAT_RICHHTML);
141 }
87fcac8d 142}
143
086831a6
PL
144/**
145 * base class for rich formats.
146 * these are multipart - eg things with attachments
147 */
59dd457e 148abstract class portfolio_format_rich {
6be1dcae 149 public static function mimetypes() {
150 return array(null);
151 }
59dd457e
PL
152
153 public static function conflicts($format) {
154 return false;
155 }
156
157 /**
158 * given a file, return a snippet of markup in whatever format
159 * to link to that file.
160 * usually involves the path given by {@link get_file_directory}
161 * this is not supported in subclasses of portfolio_format_file
162 * since they're all just single files.
163 *
164 * @param stored_file $file the file to link to
165 * @param mixed $extras any extra arguments
166 */
167 public static abstract function file_output($file, $extras=null);
168
6be1dcae 169}
170
086831a6
PL
171/**
172 * most commonly used rich format - richhtml - html with attachments
173 * eg inline images
174 */
6be1dcae 175class portfolio_format_richhtml extends portfolio_format_rich {
176 public static function get_file_directory() {
177 return 'site_files';
178 }
59dd457e 179 public static function file_output($file, $extras=null) {
6be1dcae 180 $path = self::get_file_directory() . '/' . $file->get_filename();
181 if (in_array($file->get_mimetype(), portfolio_format_image::mimetypes())) {
182 return '<img src="' . $path . '" alt="' . $file->get_filename() . '" />';
183 }
184 return '<a href="' . $path . '">' . $file->get_filename() . '</a>';
185 }
59dd457e
PL
186 public static function conflicts($format) { // TODO revisit the conflict with file, since we zip here
187 return ($format == PORTFOLIO_FORMAT_PLAINHTML || $format == PORTFOLIO_FORMAT_FILE);
188 }
189
6be1dcae 190}
191
59dd457e
PL
192class portfolio_format_leap2a extends portfolio_format_rich {
193
194 public static function get_file_directory() {
195 return 'files/';
196 }
197
198 /**
199 * return the link to a file
200 *
201 * @param stored_file $file
202 * @param boolean $entry whether the file is a LEAP2A entry or just a bundled file (default false)
203 */
204 public static function file_output($file, $entry=false) {
205 $id = '';
206 if ($entry) {
207 $id = 'portfolio:file' . $file->get_id;
208 } else {
209 $id = self::get_file_directory() . '/' . $file->get_filename();
210 }
211 return '<a rel="enclosure" href="' . $id . '">' . $file->get_filename() . '</a>';
212 }
213
214 public static function leap2a_writer(stdclass $user=null) {
215 global $CFG;
216 if (empty($user)) {
217 global $USER;
218 $user = $USER;
219 }
220 require_once($CFG->libdir . '/portfolio/formats/leap2a/lib.php');
221 return new portfolio_format_leap2a_writer($user);
222 }
223
224 public static function manifest_name() {
225 return 'leap2a.xml';
226 }
227}
6be1dcae 228
229
87fcac8d 230/**
231* later.... a moodle plugin might support this.
232* it's commented out in portfolio_supported_formats so cannot currently be used.
233*/
59dd457e 234//class portfolio_format_mbkp extends portfolio_format_rich {}
a9ec9031
DP
235
236/**
237* 'PDF format', subtype of file.
238*
239* for portfolio plugins that support PDFs specifically
240*/
241class portfolio_format_pdf extends portfolio_format_file {
242 public static function mimetypes() {
243 return array('application/pdf');
244 }
245}
246
247/**
248* 'Document format', subtype of file.
249*
250* for portfolio plugins that support documents specifically
251*/
252class portfolio_format_document extends portfolio_format_file {
253 public static function mimetypes() {
254 return array_merge(
255 array('text/plain', 'text/rtf'),
256 mimeinfo_from_icon('type', 'word.gif', true),
257 mimeinfo_from_icon('type', 'docx.gif', true),
258 mimeinfo_from_icon('type', 'odt.gif', true)
259 );
260 }
261}
262
263/**
264* 'Spreadsheet format', subtype of file.
265*
266* for portfolio plugins that support spreadsheets specifically
267*/
268class portfolio_format_spreadsheet extends portfolio_format_file {
269 public static function mimetypes() {
270 return array_merge(
271 mimeinfo_from_icon('type', 'excel.gif', true),
272 mimeinfo_from_icon('type', 'xlsm.gif', true),
273 mimeinfo_from_icon('type', 'ods.gif', true)
274 );
275 }
276}
277
278/**
279* 'Presentation format', subtype of file.
280*
281* for portfolio plugins that support presentation specifically
282*/
283class portfolio_format_presentation extends portfolio_format_file {
284 public static function mimetypes() {
285 return mimeinfo_from_icon('type', 'powerpoint.gif', true);
286 }
287}