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