-
Notifications
You must be signed in to change notification settings - Fork 465
/
openoffice_presentation.class.php
executable file
·302 lines (273 loc) · 11.6 KB
/
openoffice_presentation.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?php
/* For licensing terms, see /license.txt */
/**
* Defines the OpenofficeDocument class, which is meant as a conversion
* tool from Office presentations (.ppt, .sxi, .odp, .pptx) to
* learning paths.
*
* @package chamilo.learnpath
*
* @author Eric Marguin <eric.marguin@dokeos.com>
* @license GNU/GPL
*/
/**
* Defines the "OpenofficePresentation" child of class "OpenofficeDocument".
*/
require_once 'openoffice_document.class.php';
if (api_get_setting('search_enabled') == 'true') {
require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
}
class OpenofficePresentation extends OpenofficeDocument
{
public $take_slide_name;
public function __construct($take_slide_name = false, $course_code = null, $resource_id = null, $user_id = null)
{
$this->take_slide_name = $take_slide_name;
parent::__construct($course_code, $resource_id, $user_id);
}
public function make_lp($files = [])
{
$_course = api_get_course_info();
$previous = 0;
$i = 0;
if (!is_dir($this->base_work_dir.$this->created_dir)) {
return false;
}
$dir = $this->created_dir;
if (substr($dir, -1, 1) !== '/') {
$dir .= '/';
}
foreach ($files as $file) {
/* '||' is used as separator between fields:
slide name (with accents) || file name (without accents) || all slide text (to be indexed).
*/
list($slide_name, $file_name, $slide_body) = explode('||', $file);
// Filename is utf8 encoded, but when we decode, some chars are not translated (like quote ’).
// so we remove these chars by translating it in htmlentities and the reconvert it in want charset.
$slide_name = api_htmlentities($slide_name, ENT_COMPAT, $this->original_charset);
$slide_name = str_replace('’', '\'', $slide_name);
$slide_name = api_convert_encoding($slide_name, api_get_system_encoding(), $this->original_charset);
$slide_name = api_html_entity_decode($slide_name, ENT_COMPAT, api_get_system_encoding());
if ($this->take_slide_name === true) {
$slide_name = str_replace('_', ' ', $slide_name);
$slide_name = api_ucfirst($slide_name);
} else {
$slide_name = 'slide'.str_repeat('0', 2 - strlen($i)).$i;
}
if (!is_file($this->base_work_dir.$dir.$file_name) or filesize($this->base_work_dir.$dir.$file_name) == 0) {
continue;
}
$i++;
if (substr($file_name, -1, 1) == '/') {
$file_name = substr($file_name, 0, -1);
}
// Add the png to documents.
$document_id = add_document(
$_course,
$dir.urlencode($file_name),
'file',
filesize($this->base_work_dir.$dir.$file_name),
$slide_name
);
api_item_property_update(
$_course,
TOOL_DOCUMENT,
$document_id,
'DocumentAdded',
api_get_user_id(),
0,
0,
null,
null,
api_get_session_id()
);
// Generating the thumbnail.
$image = $this->base_work_dir.$dir.$file_name;
$pattern = '/(\w+)\.png$/';
$replacement = '${1}_thumb.png';
$thumb_name = preg_replace($pattern, $replacement, $file_name);
// Calculate thumbnail size.
$image_size = api_getimagesize($image);
$width = $image_size['width'];
$height = $image_size['height'];
$thumb_width = 300;
$thumb_height = floor($height * ($thumb_width / $width));
$my_new_image = new Image($image);
$my_new_image->resize($thumb_width, $thumb_height);
$my_new_image->send_image($this->base_work_dir.$dir.$thumb_name, -1, 'png');
// Adding the thumbnail to documents.
$document_id_thumb = add_document(
$_course,
$dir.urlencode($thumb_name),
'file',
filesize($this->base_work_dir.$dir.$thumb_name),
$slide_name
);
api_item_property_update(
$_course,
TOOL_THUMBNAIL,
$document_id_thumb,
'DocumentAdded',
api_get_user_id(),
0,
0
);
// Create an html file.
$html_file = $file_name.'.html';
$fp = fopen($this->base_work_dir.$dir.$html_file, 'w+');
if (substr($dir, 0, 1) != '/') {
$dir = '/'.$dir;
}
$slide_src = api_get_path(REL_COURSE_PATH).$_course['path'].'/document'.$dir.utf8_encode($file_name);
$slide_src = str_replace('\/\/', '/', $slide_src);
fwrite(
$fp,
'<html>
<head>
<title>'.$slide_name.'</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="'.api_get_path(WEB_PUBLIC_PATH).'assets/bootstrap/dist/css/bootstrap.min.css" media="screen" rel="stylesheet" type="text/css" />
</head>
<body>
<img class="img-responsive" alt="'.htmlspecialchars($slide_name.'"', ENT_QUOTES).'" src="'.$slide_src.'" />
</body>
</html>'
); // This indentation is to make the generated html files to look well.
fclose($fp);
$document_id = add_document(
$_course,
$dir.urlencode($html_file),
'file',
filesize($this->base_work_dir.$dir.$html_file),
$slide_name
);
if ($document_id) {
// Put the document in item_property update.
api_item_property_update(
$_course,
TOOL_DOCUMENT,
$document_id,
'DocumentAdded',
api_get_user_id(),
0,
0,
null,
null,
api_get_session_id()
);
$previous = $this->add_item(
0,
$previous,
'document',
$document_id,
$slide_name,
''
);
if ($this->first_item == 0) {
$this->first_item = intval($previous);
}
}
// Code for text indexing.
if (api_get_setting('search_enabled') == 'true') {
if (isset($_POST['index_document']) && $_POST['index_document']) {
$di = new ChamiloIndexer();
isset($_POST['language']) ? $lang = Database::escape_string($_POST['language']) : $lang = 'english';
$di->connectDb(null, null, $lang);
$ic_slide = new IndexableChunk();
$ic_slide->addValue('title', $slide_name);
$specific_fields = get_specific_field_list();
$all_specific_terms = '';
foreach ($specific_fields as $specific_field) {
if (isset($_REQUEST[$specific_field['code']])) {
$sterms = trim($_REQUEST[$specific_field['code']]);
$all_specific_terms .= ' '.$sterms;
if (!empty($sterms)) {
$sterms = explode(',', $sterms);
foreach ($sterms as $sterm) {
$ic_slide->addTerm(trim($sterm), $specific_field['code']);
}
}
}
}
$slide_body = $all_specific_terms.' '.$slide_body;
$ic_slide->addValue('content', $slide_body);
/* FIXME: cidReq:lp_id:doc_id al indexar */
// Add a comment to say terms separated by commas.
$courseid = api_get_course_id();
$ic_slide->addCourseId($courseid);
$ic_slide->addToolId(TOOL_LEARNPATH);
$lp_id = $this->lp_id;
$xapian_data = [
SE_COURSE_ID => $courseid,
SE_TOOL_ID => TOOL_LEARNPATH,
SE_DATA => ['lp_id' => $lp_id, 'lp_item' => $previous, 'document_id' => $document_id],
SE_USER => (int) api_get_user_id(),
];
$ic_slide->xapian_data = serialize($xapian_data);
$di->addChunk($ic_slide);
// Index and return search engine document id.
$did = $di->index();
if ($did) {
// Save it to db.
$tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
$sql = 'INSERT INTO %s (id, course_code, tool_id, ref_id_high_level, ref_id_second_level, search_did)
VALUES (NULL , \'%s\', \'%s\', %s, %s, %s)';
$sql = sprintf($sql, $tbl_se_ref, api_get_course_id(), TOOL_LEARNPATH, $lp_id, $previous, $did);
Database::query($sql);
}
}
}
}
}
public function add_command_parameters()
{
if (empty($this->slide_width) || empty($this->slide_height)) {
list($w, $h) = explode('x', api_get_setting('service_ppt2lp', 'size'));
$this->slide_width = (int) $w;
$this->slide_height = (int) $h;
}
return ' -w '.$this->slide_width.' -h '.$this->slide_height.' -d oogie "'
.Security::sanitizeExecParam($this->base_work_dir.'/'.$this->file_path)
.'" "'
.Security::sanitizeExecParam($this->base_work_dir.$this->created_dir.'.html')
.'"';
}
public function set_slide_size($width, $height)
{
$this->slide_width = (int) $width;
$this->slide_height = (int) $height;
}
public function add_docs_to_visio($files = [])
{
$_course = api_get_course_info();
foreach ($files as $file) {
// '||' is used as separator between slide name (with accents) and file name (without accents).
list($slide_name, $file_name) = explode('||', $file);
$slide_name = api_htmlentities($slide_name, ENT_COMPAT, $this->original_charset);
$slide_name = str_replace('’', '\'', $slide_name);
$slide_name = api_convert_encoding($slide_name, api_get_system_encoding(), $this->original_charset);
$slide_name = api_html_entity_decode($slide_name, ENT_COMPAT, api_get_system_encoding());
$did = add_document(
$_course,
$this->created_dir.'/'.urlencode($file_name),
'file',
filesize($this->base_work_dir.$this->created_dir.'/'.$file_name),
$slide_name
);
if ($did) {
api_item_property_update(
$_course,
TOOL_DOCUMENT,
$did,
'DocumentAdded',
api_get_user_id(),
0,
null,
null,
null,
api_get_session_id()
);
}
}
}
}