openoffice_presentation.class.php 10 KB

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