openoffice_presentation.class.php 11 KB

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