openoffice_presentation.class.php 11 KB

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