PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/main/newscorm/openoffice_presentation.class.php

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