weekly release 2.3dev (blame stronk7 for 0202 mistake)
[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 *
0972665f 20 * @package core
87fcac8d 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
0972665f
PS
31defined('MOODLE_INTERNAL') || die();
32
87fcac8d 33/**
8192522e
PL
34 * base class to inherit from
35 * do not use this anywhere in supported_formats
36 */
37abstract class portfolio_format {
086831a6
PL
38
39 /**
40 * array of mimetypes this format supports
41 */
7c109ea3
PS
42 public static function mimetypes() {
43 throw new coding_exception('mimetypes() method needs to be overridden in each subclass of portfolio_format');
44 }
6be1dcae 45
086831a6
PL
46 /**
47 * for multipart formats, eg html with attachments,
48 * we need to have a directory to place associated files in
49 * inside the zip file. this is the name of that directory
50 */
7c109ea3
PS
51 public static function get_file_directory() {
52 throw new coding_exception('get_file_directory() method needs to be overridden in each subclass of portfolio_format');
53 }
6be1dcae 54
086831a6
PL
55 /**
56 * given a file, return a snippet of markup in whatever format
57 * to link to that file.
58 * usually involves the path given by {@link get_file_directory}
59dd457e
PL
59 * this is not supported in subclasses of portfolio_format_file
60 * since they're all just single files.
8192522e
PL
61 *
62 * @param stored_file $file
63 * @param array $options array of options to pass. can contain:
64 * attributes => hash of existing html attributes (eg title, height, width, etc)
65 * and whatever the sub class adds into this list
66 *
67 * @return string some html or xml or whatever
086831a6 68 */
7c109ea3
PS
69 public static function file_output($file, $options=null) {
70 throw new coding_exception('file_output() method needs to be overridden in each subclass of portfolio_format');
71 }
8192522e
PL
72
73 public static function make_tag($file, $path, $attributes) {
74 $srcattr = 'href';
75 $tag = 'a';
76 $content = $file->get_filename();
77 if (in_array($file->get_mimetype(), portfolio_format_image::mimetypes())) {
78 $srcattr = 'src';
79 $tag = 'img';
80 $content = '';
81 }
82
83 $attributes[$srcattr] = $path; // this will override anything we might have been passed (which is good)
84 $dom = new DomDocument();
85 $elem = null;
86 if ($content) {
87 $elem = $dom->createElement($tag, $content);
88 } else {
89 $elem = $dom->createElement($tag);
90 }
91
92 foreach ($attributes as $key => $value) {
93 $elem->setAttribute($key, $value);
94 }
95 $dom->appendChild($elem);
96 return $dom->saveXML($elem);
59dd457e
PL
97 }
98
99 /**
100 * whether this format conflicts with the given format
101 * this is used for the case where an export location
102 * "generally" supports something like FORMAT_PLAINHTML
103 * but then in a specific export case, must add attachments
104 * which means that FORMAT_RICHHTML is supported in that case
105 * which implies removing support for FORMAT_PLAINHTML.
106 * Note that conflicts don't have to be bi-directional
107 * (eg FORMAT_PLAINHTML conflicts with FORMAT_RICHHTML
108 * but not the other way around) and things within the class hierarchy
109 * are resolved automatically anyway.
110 *
111 * This is really just between subclasses of format_rich
112 * and subclasses of format_file.
113 *
114 * @param string $format one of the FORMAT_XX constants
115 *
116 * @return boolean
117 */
118 public static function conflicts($format) {
119 return false;
6be1dcae 120 }
87fcac8d 121}
122
8192522e
PL
123/**
124* the most basic type - pretty much everything is a subtype
125*/
126class portfolio_format_file extends portfolio_format {
127
128 public static function mimetypes() {
129 return array();
130 }
131
132 public static function get_file_directory() {
82e8d268 133 return false;
8192522e
PL
134 }
135
136 public static function file_output($file, $options=null) {
137 throw new portfolio_exception('fileoutputnotsupported', 'portfolio');
138 }
139}
140
87fcac8d 141/**
142* image format, subtype of file.
143*/
144class portfolio_format_image extends portfolio_format_file {
086831a6
PL
145 /**
146 * return all mimetypes that use image.gif (eg all images)
147 */
87fcac8d 148 public static function mimetypes() {
ede72522 149 return mimeinfo_from_icon('type', 'image', true);
87fcac8d 150 }
50938e30
PL
151
152 public static function conflicts($format) {
153 return ($format == PORTFOLIO_FORMAT_RICHHTML
154 || $format == PORTFOLIO_FORMAT_PLAINHTML);
155 }
87fcac8d 156}
157
158/**
159* html format - could be used for an external cms or something
160*
161* in case we want to be really specific.
162*/
6be1dcae 163class portfolio_format_plainhtml extends portfolio_format_file {
59dd457e 164
87fcac8d 165 public static function mimetypes() {
166 return array('text/html');
167 }
59dd457e
PL
168
169 public static function conflicts($format) {
549c1ae6
PL
170 return ($format == PORTFOLIO_FORMAT_RICHHTML
171 || $format == PORTFOLIO_FORMAT_FILE);
59dd457e 172 }
87fcac8d 173}
174
175/**
176* video format, subtype of file.
177*
086831a6 178* for portfolio plugins that support videos specifically
87fcac8d 179*/
180class portfolio_format_video extends portfolio_format_file {
181 public static function mimetypes() {
d02b207a 182 return array_merge(
ede72522
PS
183 mimeinfo_from_icon('type', 'video', true),
184 mimeinfo_from_icon('type', 'avi', true)
086831a6 185 );
87fcac8d 186 }
187}
188
189/**
190* class for plain text format.. not sure why we would need this yet
191* but since resource module wants to export it... we can
192*/
193class portfolio_format_text extends portfolio_format_file {
194 public static function mimetypes() {
195 return array('text/plain');
196 }
59dd457e
PL
197
198 public static function conflicts($format ) {
199 return ($format == PORTFOLIO_FORMAT_PLAINHTML
200 || $format == PORTFOLIO_FORMAT_RICHHTML);
201 }
87fcac8d 202}
203
086831a6
PL
204/**
205 * base class for rich formats.
206 * these are multipart - eg things with attachments
207 */
8192522e 208abstract class portfolio_format_rich extends portfolio_format {
59dd457e 209
8192522e
PL
210 public static function mimetypes() {
211 return array();
59dd457e
PL
212 }
213
6be1dcae 214}
215
086831a6
PL
216/**
217 * most commonly used rich format - richhtml - html with attachments
218 * eg inline images
219 */
6be1dcae 220class portfolio_format_richhtml extends portfolio_format_rich {
221 public static function get_file_directory() {
8192522e 222 return 'site_files/';
6be1dcae 223 }
8192522e
PL
224 public static function file_output($file, $options=null) {
225 $path = self::get_file_directory() . $file->get_filename();
226 $attributes = array();
227 if (!empty($options['attributes']) && is_array($options['attributes'])) {
228 $attributes = $options['attributes'];
6be1dcae 229 }
8192522e 230 return self::make_tag($file, $path, $attributes);
6be1dcae 231 }
59dd457e
PL
232 public static function conflicts($format) { // TODO revisit the conflict with file, since we zip here
233 return ($format == PORTFOLIO_FORMAT_PLAINHTML || $format == PORTFOLIO_FORMAT_FILE);
234 }
235
6be1dcae 236}
237
59dd457e
PL
238class portfolio_format_leap2a extends portfolio_format_rich {
239
240 public static function get_file_directory() {
241 return 'files/';
242 }
243
ebb7e782
PL
244 public static function file_id_prefix() {
245 return 'storedfile';
246 }
247
59dd457e
PL
248 /**
249 * return the link to a file
250 *
251 * @param stored_file $file
8192522e 252 * @param array $options can contain the same as normal, with the addition of:
ebb7e782 253 * entry => whether the file is a LEAP2A entry or just a bundled file (default true)
59dd457e 254 */
8192522e 255 public static function file_output($file, $options=null) {
59dd457e 256 $id = '';
8192522e
PL
257 if (!is_array($options)) {
258 $options = array();
259 }
ebb7e782
PL
260 if (!array_key_exists('entry', $options)) {
261 $options['entry'] = true;
262 }
8192522e 263 if (!empty($options['entry'])) {
ebb7e782 264 $path = 'portfolio:' . self::file_id_prefix() . $file->get_id();
59dd457e 265 } else {
8192522e
PL
266 $path = self::get_file_directory() . $file->get_filename();
267 }
268 $attributes = array();
269 if (!empty($options['attributes']) && is_array($options['attributes'])) {
270 $attributes = $options['attributes'];
59dd457e 271 }
8192522e
PL
272 $attributes['rel'] = 'enclosure';
273 return self::make_tag($file, $path, $attributes);
59dd457e
PL
274 }
275
276 public static function leap2a_writer(stdclass $user=null) {
277 global $CFG;
278 if (empty($user)) {
279 global $USER;
280 $user = $USER;
281 }
282 require_once($CFG->libdir . '/portfolio/formats/leap2a/lib.php');
283 return new portfolio_format_leap2a_writer($user);
284 }
285
286 public static function manifest_name() {
287 return 'leap2a.xml';
288 }
289}
6be1dcae 290
291
87fcac8d 292/**
293* later.... a moodle plugin might support this.
294* it's commented out in portfolio_supported_formats so cannot currently be used.
295*/
59dd457e 296//class portfolio_format_mbkp extends portfolio_format_rich {}
a9ec9031
DP
297
298/**
299* 'PDF format', subtype of file.
300*
301* for portfolio plugins that support PDFs specifically
302*/
303class portfolio_format_pdf extends portfolio_format_file {
304 public static function mimetypes() {
305 return array('application/pdf');
306 }
307}
308
309/**
310* 'Document format', subtype of file.
311*
312* for portfolio plugins that support documents specifically
313*/
314class portfolio_format_document extends portfolio_format_file {
315 public static function mimetypes() {
316 return array_merge(
317 array('text/plain', 'text/rtf'),
ede72522
PS
318 mimeinfo_from_icon('type', 'word', true),
319 mimeinfo_from_icon('type', 'docx', true),
320 mimeinfo_from_icon('type', 'odt', true)
a9ec9031
DP
321 );
322 }
323}
324
325/**
326* 'Spreadsheet format', subtype of file.
327*
328* for portfolio plugins that support spreadsheets specifically
329*/
330class portfolio_format_spreadsheet extends portfolio_format_file {
331 public static function mimetypes() {
332 return array_merge(
ede72522
PS
333 mimeinfo_from_icon('type', 'excel', true),
334 mimeinfo_from_icon('type', 'xlsm', true),
335 mimeinfo_from_icon('type', 'ods', true)
a9ec9031
DP
336 );
337 }
338}
339
340/**
341* 'Presentation format', subtype of file.
342*
343* for portfolio plugins that support presentation specifically
344*/
345class portfolio_format_presentation extends portfolio_format_file {
346 public static function mimetypes() {
ede72522 347 return mimeinfo_from_icon('type', 'powerpoint', true);
a9ec9031
DP
348 }
349}