openoffice_presentation.class.php 11 KB

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