openoffice_presentation.class.php 11 KB

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