create_document.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This file allows creating new html documents with an online WYSIWYG html editor.
  5. *
  6. * @package chamilo.document
  7. */
  8. /**
  9. * Code
  10. */
  11. /* INIT SECTION */
  12. // Name of the language file that needs to be included
  13. $language_file = array('document', 'gradebook');
  14. require_once '../inc/global.inc.php';
  15. api_protect_course_script();
  16. $_SESSION['whereami'] = 'document/create';
  17. $this_section = SECTION_COURSES;
  18. $htmlHeadXtra[] = '<script>
  19. var hide_bar = function() {
  20. $("#template_col").hide();
  21. $("#doc_form").removeClass("span9");
  22. $("#doc_form").addClass("span11");
  23. $("#hide_bar_template").css({"background-image" : \'url("../img/hide2.png")\'})
  24. }
  25. $(document).ready(function() {
  26. if ($(window).width() <= 785 ) {
  27. hide_bar();
  28. }
  29. $("#hide_bar_template").toggle(
  30. function() {
  31. hide_bar();
  32. },
  33. function() {
  34. $("#template_col").show();
  35. $("#doc_form").removeClass("span11");
  36. $("#doc_form").addClass("span9");
  37. $(this).css("background-image", \'url("../img/hide0.png")\');
  38. }
  39. );
  40. });
  41. function InnerDialogLoaded() {
  42. /*
  43. var B=new window.frames[0].FCKToolbarButton(\'Templates\',window.frames[0].FCKLang.Templates);
  44. return B.ClickFrame();
  45. */
  46. var isIE = (navigator.appVersion.indexOf(\'MSIE\') != -1) ? true : false ;
  47. var EditorFrame = null ;
  48. if ( !isIE ) {
  49. EditorFrame = window.frames[0] ;
  50. } else {
  51. // For this dynamic page window.frames[0] enumerates frames in a different order in IE.
  52. // We need a sure method to locate the frame that contains the online editor.
  53. for ( var i = 0, n = window.frames.length ; i < n ; i++ ) {
  54. if ( window.frames[i].location.toString().indexOf(\'InstanceName=content\') != -1 ) {
  55. EditorFrame = window.frames[i] ;
  56. }
  57. }
  58. }
  59. if ( !EditorFrame ) {
  60. return null ;
  61. }
  62. var B = new EditorFrame.FCKToolbarButton(\'Templates\', EditorFrame.FCKLang.Templates);
  63. return B.ClickFrame();
  64. };
  65. var temp=false;
  66. var temp2=false;
  67. var load_default_template = '.((isset($_POST['submit']) || empty($_SERVER['QUERY_STRING'])) ? 'false' : 'true').';
  68. function FCKeditor_OnComplete( editorInstance ) {
  69. editorInstance.Events.AttachEvent( \'OnSelectionChange\', check_for_title ) ;
  70. document.getElementById(\'frmModel\').innerHTML = "<iframe style=\'height: 525px; width: 180px;\' scrolling=\'no\' frameborder=\'0\' src=\''.api_get_path(
  71. WEB_LIBRARY_PATH
  72. ).'fckeditor/editor/fckdialogframe.html \'>";
  73. }
  74. function check_for_title() {
  75. if (temp) {
  76. // This functions shows that you can interact directly with the editor area
  77. // DOM. In this way you have the freedom to do anything you want with it.
  78. // Get the editor instance that we want to interact with.
  79. var oEditor = FCKeditorAPI.GetInstance(\'content\') ;
  80. // Get the Editor Area DOM (Document object).
  81. var oDOM = oEditor.EditorDocument ;
  82. var iLength ;
  83. var contentText ;
  84. var contentTextArray;
  85. var bestandsnaamNieuw = "";
  86. var bestandsnaamOud = "";
  87. // The are two diffent ways to get the text (without HTML markups).
  88. // It is browser specific.
  89. if( document.all ) // If Internet Explorer.
  90. {
  91. contentText = oDOM.body.innerText ;
  92. }
  93. else // If Gecko.
  94. {
  95. var r = oDOM.createRange() ;
  96. r.selectNodeContents( oDOM.body ) ;
  97. contentText = r.toString() ;
  98. }
  99. var index=contentText.indexOf("/*<![CDATA");
  100. contentText=contentText.substr(0,index);
  101. // Compose title if there is none
  102. contentTextArray = contentText.split(\' \') ;
  103. var x=0;
  104. for(x=0; (x<5 && x<contentTextArray.length); x++)
  105. {
  106. if(x < 4)
  107. {
  108. bestandsnaamNieuw += contentTextArray[x] + \' \';
  109. }
  110. else
  111. {
  112. bestandsnaamNieuw += contentTextArray[x];
  113. }
  114. }
  115. }
  116. temp=true;
  117. }
  118. function trim(s) {
  119. while(s.substring(0,1) == \' \') {
  120. s = s.substring(1,s.length);
  121. }
  122. while(s.substring(s.length-1,s.length) == \' \') {
  123. s = s.substring(0,s.length-1);
  124. }
  125. return s;
  126. }
  127. function setFocus() {
  128. $("#document_title").focus();
  129. }
  130. $(window).load(function () {
  131. setFocus();
  132. });
  133. </script>';
  134. require_once api_get_path(SYS_CODE_PATH).'document/document.inc.php';
  135. //I'm in the certification module?
  136. $is_certificate_mode = false;
  137. $_course = api_get_course_info();
  138. if (isset($_REQUEST['certificate']) && $_REQUEST['certificate'] == 'true') {
  139. $is_certificate_mode = true;
  140. }
  141. if ($is_certificate_mode) {
  142. $nameTools = get_lang('CreateCertificate');
  143. } else {
  144. $nameTools = get_lang('CreateDocument');
  145. }
  146. /* Constants and variables */
  147. $doc_table = Database::get_course_table(TABLE_DOCUMENT);
  148. $course_id = api_get_course_int_id();
  149. $document_data = DocumentManager::get_document_data_by_id($_REQUEST['id'], api_get_course_id(), true);
  150. if (empty($document_data)) {
  151. if (api_is_in_group()) {
  152. $group_properties = GroupManager::get_group_properties(api_get_group_id());
  153. $document_id = DocumentManager::get_document_id(api_get_course_info(), $group_properties['directory']);
  154. $document_data = DocumentManager::get_document_data_by_id($document_id, api_get_course_id());
  155. $dir = $document_data['path'];
  156. $folder_id = $document_data['id'];
  157. } else {
  158. $dir = '/';
  159. $folder_id = 0;
  160. }
  161. } else {
  162. $folder_id = $document_data['id'];
  163. $dir = $document_data['path'];
  164. }
  165. /* MAIN CODE */
  166. // Please, do not modify this dirname formatting
  167. if (strstr($dir, '..')) {
  168. $dir = '/';
  169. }
  170. if ($dir[0] == '.') {
  171. $dir = substr($dir, 1);
  172. }
  173. if ($dir[0] != '/') {
  174. $dir = '/'.$dir;
  175. }
  176. if ($dir[strlen($dir) - 1] != '/') {
  177. $dir .= '/';
  178. }
  179. if ($is_certificate_mode) {
  180. $document_id = DocumentManager::get_document_id(api_get_course_info(), '/certificates');
  181. $document_data = DocumentManager::get_document_data_by_id($document_id, api_get_course_id(), true);
  182. $folder_id = $document_data['id'];
  183. $dir = '/certificates/';
  184. }
  185. // Configuration for the FCKEDITOR
  186. $doc_tree = explode('/', $dir);
  187. $count_dir = count($doc_tree) - 2; // "2" because at the begin and end there are 2 "/"
  188. if (api_is_in_group()) {
  189. $group_properties = GroupManager::get_group_properties(api_get_group_id());
  190. // Level correction for group documents.
  191. if (!empty($group_properties['directory'])) {
  192. $count_dir = $count_dir > 0 ? $count_dir - 1 : 0;
  193. }
  194. }
  195. $relative_url = '';
  196. for ($i = 0; $i < ($count_dir); $i++) {
  197. $relative_url .= '../';
  198. }
  199. // We do this in order to avoid the condition in html_editor.php ==> if ($this -> fck_editor->Config['CreateDocumentWebDir']=='' || $this -> fck_editor->Config['CreateDocumentDir']== '')
  200. if ($relative_url == '') {
  201. $relative_url = '/';
  202. }
  203. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  204. $html_editor_config = array(
  205. 'ToolbarSet' => ($is_allowed_to_edit ? 'Documents' : 'DocumentsStudent'),
  206. 'Width' => '100%',
  207. 'Height' => '500',
  208. 'FullPage' => true,
  209. 'InDocument' => true,
  210. 'CreateDocumentDir' => $relative_url,
  211. 'CreateDocumentWebDir' => (empty($group_properties['directory']))
  212. ? api_get_path(WEB_COURSE_PATH).$_course['path'].'/document/'
  213. : api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document'.$group_properties['directory'].'/',
  214. 'BaseHref' => api_get_path(WEB_COURSE_PATH).$_course['path'].'/document'.$dir
  215. );
  216. if ($is_certificate_mode) {
  217. $html_editor_config['CreateDocumentDir'] = api_get_path(WEB_COURSE_PATH).$_course['path'].'/document/';
  218. $html_editor_config['CreateDocumentWebDir'] = api_get_path(WEB_COURSE_PATH).$_course['path'].'/document/';
  219. $html_editor_config['BaseHref'] = api_get_path(WEB_COURSE_PATH).$_course['path'].'/document'.$dir;
  220. }
  221. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$dir;
  222. if (!is_dir($filepath)) {
  223. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/';
  224. $dir = '/';
  225. }
  226. $to_group_id = 0;
  227. if (!$is_certificate_mode) {
  228. $req_gid = null;
  229. if (api_is_in_group()) {
  230. $req_gid = '&amp;gidReq='.api_get_group_id();
  231. $interbreadcrumb[] = array(
  232. "url" => "../group/group_space.php?gidReq=".api_get_group_id(),
  233. "name" => get_lang('GroupSpace')
  234. );
  235. $noPHP_SELF = true;
  236. $to_group_id = api_get_group_id();
  237. $path = explode('/', $dir);
  238. if ('/'.$path[1] != $group_properties['directory']) {
  239. api_not_allowed(true);
  240. }
  241. }
  242. $interbreadcrumb[] = array(
  243. "url" => "./document.php?curdirpath=".urlencode($dir).$req_gid,
  244. "name" => get_lang('Documents')
  245. );
  246. } else {
  247. $interbreadcrumb[] = array('url' => api_get_path(WEB_CODE_PATH).'gradebook/'.$_SESSION['gradebook_dest'], 'name' => get_lang('Gradebook'));
  248. }
  249. if (!($is_allowed_to_edit || $_SESSION['group_member_with_upload_rights'] || is_my_shared_folder(
  250. $_user['user_id'],
  251. Security::remove_XSS($dir),
  252. api_get_session_id()
  253. ))
  254. ) {
  255. api_not_allowed(true);
  256. }
  257. event_access_tool(TOOL_DOCUMENT);
  258. $display_dir = $dir;
  259. if (isset ($group_properties)) {
  260. $display_dir = explode('/', $dir);
  261. unset ($display_dir[0]);
  262. unset ($display_dir[1]);
  263. $display_dir = implode('/', $display_dir);
  264. }
  265. $select_cat = isset($_GET['selectcat']) ? intval($_GET['selectcat']) : null;
  266. // Create a new form
  267. $form = new FormValidator('create_document', 'post', api_get_self().'?'.api_get_cidreq().'&dir='.Security::remove_XSS(
  268. urlencode($dir)
  269. ).'&selectcat='.$select_cat, null, array('class' => 'form-vertical'));
  270. // form title
  271. $form->addElement('header', $nameTools);
  272. if ($is_certificate_mode) { //added condition for certicate in gradebook
  273. $form->addElement('hidden', 'certificate', 'true', array('id' => 'certificate'));
  274. if (isset($_GET['selectcat'])) {
  275. $form->addElement('hidden', 'selectcat', $select_cat);
  276. }
  277. }
  278. // Hidden element with current directory
  279. $form->addElement('hidden', 'id');
  280. $defaults = array();
  281. $defaults['id'] = $folder_id;
  282. // Filename
  283. $form->addElement('hidden', 'title_edited', 'false', 'id="title_edited"');
  284. /**
  285. * Check if a document width the choosen filename allready exists
  286. */
  287. function document_exists($filename)
  288. {
  289. global $filepath;
  290. $filename = addslashes(trim($filename));
  291. $filename = Security::remove_XSS($filename);
  292. $filename = api_replace_dangerous_char($filename);
  293. $filename = FileManager::disable_dangerous_file($filename);
  294. return !file_exists($filepath.$filename.'.html');
  295. }
  296. // Add group to the form
  297. if ($is_certificate_mode) {
  298. $form->addElement('text', 'title', get_lang('CertificateName'), 'class="span4" id="document_title"');
  299. } else {
  300. $form->addElement('text', 'title', get_lang('Title'), 'class="span4" id="document_title"');
  301. }
  302. // Show read-only box only in groups
  303. if (!empty($_SESSION['_gid'])) {
  304. $group[] = $form->createElement('checkbox', 'readonly', '', get_lang('ReadOnly'));
  305. }
  306. $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  307. $form->addRule('title', get_lang('FileExists'), 'callback', 'document_exists');
  308. $current_session_id = api_get_session_id();
  309. $form->add_html_editor('content', '', false, false, $html_editor_config);
  310. // Comment-field
  311. $folders = DocumentManager::get_all_document_folders($_course, $to_group_id, $is_allowed_to_edit);
  312. // If we are not in the certificates creation, display a folder chooser for the
  313. // new document created
  314. if (!$is_certificate_mode && !is_my_shared_folder($_user['user_id'], $dir, $current_session_id)) {
  315. $folders = DocumentManager::get_all_document_folders($_course, $to_group_id, $is_allowed_to_edit);
  316. $parent_select = $form->addElement('select', 'curdirpath', array(null, get_lang('DestinationDirectory')));
  317. // Following two conditions copied from document.inc.php::build_directory_selector()
  318. $folder_titles = array();
  319. if (is_array($folders)) {
  320. $escaped_folders = array();
  321. foreach ($folders as $key => & $val) {
  322. //Hide some folders
  323. if ($val == '/HotPotatoes_files' || $val == '/certificates' || basename($val) == 'css') {
  324. continue;
  325. }
  326. //Admin setting for Hide/Show the folders of all users
  327. if (api_get_setting('show_users_folders') == 'false' && (strstr($val, '/shared_folder') || strstr(
  328. $val,
  329. 'shared_folder_session_'
  330. ))
  331. ) {
  332. continue;
  333. }
  334. //Admin setting for Hide/Show Default folders to all users
  335. if (api_get_setting(
  336. 'show_default_folders'
  337. ) == 'false' && ($val == '/images' || $val == '/flash' || $val == '/audio' || $val == '/video' || strstr(
  338. $val,
  339. '/images/gallery'
  340. ) || $val == '/video/flv')
  341. ) {
  342. continue;
  343. }
  344. //Admin setting for Hide/Show chat history folder
  345. if (api_get_setting('show_chat_folder') == 'false' && $val == '/chat_files') {
  346. continue;
  347. }
  348. $escaped_folders[$key] = Database::escape_string($val);
  349. }
  350. $folder_sql = implode("','", $escaped_folders);
  351. $sql = "SELECT * FROM $doc_table WHERE c_id = $course_id AND filetype='folder' AND path IN ('".$folder_sql."')";
  352. $res = Database::query($sql);
  353. $folder_titles = array();
  354. while ($obj = Database::fetch_object($res)) {
  355. $folder_titles[$obj->path] = $obj->title;
  356. }
  357. }
  358. if (empty($group_dir)) {
  359. $parent_select->addOption(get_lang('HomeDirectory'), '/');
  360. if (is_array($folders)) {
  361. foreach ($folders as & $folder) {
  362. //Hide some folders
  363. if ($folder == '/HotPotatoes_files' || $folder == '/certificates' || basename($folder) == 'css') {
  364. continue;
  365. }
  366. //Admin setting for Hide/Show the folders of all users
  367. if (api_get_setting('show_users_folders') == 'false' && (strstr($folder, '/shared_folder') || strstr(
  368. $folder,
  369. 'shared_folder_session_'
  370. ))
  371. ) {
  372. continue;
  373. }
  374. //Admin setting for Hide/Show Default folders to all users
  375. if (api_get_setting(
  376. 'show_default_folders'
  377. ) == 'false' && ($folder == '/images' || $folder == '/flash' || $folder == '/audio' || $folder == '/video' || strstr(
  378. $folder,
  379. '/images/gallery'
  380. ) || $folder == '/video/flv')
  381. ) {
  382. continue;
  383. }
  384. //Admin setting for Hide/Show chat history folder
  385. if (api_get_setting('show_chat_folder') == 'false' && $folder == '/chat_files') {
  386. continue;
  387. }
  388. $selected = (substr($dir, 0, -1) == $folder) ? ' selected="selected"' : '';
  389. $path_parts = explode('/', $folder);
  390. $folder_titles[$folder] = Text::cut($folder_titles[$folder], 80);
  391. $space_counter = count($path_parts) - 2;
  392. if ($space_counter > 0) {
  393. $label = str_repeat('&nbsp;&nbsp;&nbsp;', $space_counter).' &mdash; '.$folder_titles[$folder];
  394. } else {
  395. $label = ' &mdash; '.$folder_titles[$folder];
  396. }
  397. $parent_select->addOption($label, $folder);
  398. if ($selected != '') {
  399. $parent_select->setSelected($folder);
  400. }
  401. }
  402. }
  403. } else {
  404. foreach ($folders as & $folder) {
  405. $selected = (substr($dir, 0, -1) == $folder) ? ' selected="selected"' : '';
  406. $label = $folder_titles[$folder];
  407. if ($folder == $group_dir) {
  408. $label = '/ ('.get_lang('HomeDirectory').')';
  409. } else {
  410. $path_parts = explode('/', str_replace($group_dir, '', $folder));
  411. $label = Text::cut($label, 80);
  412. $label = str_repeat('&nbsp;&nbsp;&nbsp;', count($path_parts) - 2).' &mdash; '.$label;
  413. }
  414. $parent_select->addOption($label, $folder);
  415. if ($selected != '') {
  416. $parent_select->setSelected($folder);
  417. }
  418. }
  419. }
  420. }
  421. if ($is_certificate_mode) {
  422. $form->addElement('style_submit_button', 'submit', get_lang('CreateCertificate'), 'class="save"');
  423. } else {
  424. $form->addElement('style_submit_button', 'submit', get_lang('CreateDoc'), 'class="save"');
  425. }
  426. $form->setDefaults($defaults);
  427. // If form validates -> save the new document
  428. if ($form->validate()) {
  429. $values = $form->exportValues();
  430. $readonly = isset($values['readonly']) ? 1 : 0;
  431. $values['title'] = trim($values['title']);
  432. if (!empty($values['curdirpath'])) {
  433. $dir = $values['curdirpath'];
  434. }
  435. if ($dir[strlen($dir) - 1] != '/') {
  436. $dir .= '/';
  437. }
  438. //Setting the filename
  439. $filename = $values['title'];
  440. $filename = addslashes(trim($filename));
  441. $filename = Security::remove_XSS($filename);
  442. $filename = api_replace_dangerous_char($filename);
  443. $filename = FileManager::disable_dangerous_file($filename);
  444. //Setting the title
  445. $title = $values['title'];
  446. //Setting the extension
  447. $extension = 'html';
  448. $content = Security::remove_XSS($values['content'], COURSEMANAGERLOWSECURITY);
  449. if (strpos($content, '/css/frames.css') === false) {
  450. $content = str_replace(
  451. '</head>',
  452. '<style> body{margin:10px;}</style><link rel="stylesheet" href="./css/frames.css" type="text/css" /></head>',
  453. $content
  454. );
  455. }
  456. if ($fp = @fopen($filepath.$filename.'.'.$extension, 'w')) {
  457. $content = str_replace(api_get_path(WEB_COURSE_PATH), $_configuration['url_append'].'/courses/', $content);
  458. // change the path of mp3 to absolute
  459. // first regexp deals with ../../../ urls
  460. // Disabled by Ivan Tcholakov.
  461. //$content = preg_replace("|(flashvars=\"file=)(\.+/)+|","$1".api_get_path(REL_COURSE_PATH).$_course['path'].'/document/',$content);
  462. //second regexp deals with audio/ urls
  463. // Disabled by Ivan Tcholakov.
  464. //$content = preg_replace("|(flashvars=\"file=)([^/]+)/|","$1".api_get_path(REL_COURSE_PATH).$_course['path'].'/document/$2/',$content);
  465. fputs($fp, $content);
  466. fclose($fp);
  467. chmod($filepath.$filename.'.'.$extension, api_get_permissions_for_new_files());
  468. if (!is_dir($filepath.'css')) {
  469. mkdir($filepath.'css', api_get_permissions_for_new_directories());
  470. $doc_id = FileManager::add_document($_course, $dir.'css', 'folder', 0, 'css');
  471. api_item_property_update(
  472. $_course,
  473. TOOL_DOCUMENT,
  474. $doc_id,
  475. 'FolderCreated',
  476. $_user['user_id'],
  477. null,
  478. null,
  479. null,
  480. null,
  481. $current_session_id
  482. );
  483. api_item_property_update(
  484. $_course,
  485. TOOL_DOCUMENT,
  486. $doc_id,
  487. 'invisible',
  488. $_user['user_id'],
  489. null,
  490. null,
  491. null,
  492. null,
  493. $current_session_id
  494. );
  495. }
  496. if (!is_file($filepath.'css/frames.css')) {
  497. // Make a copy of the current css for the new document
  498. copy(
  499. api_get_path(SYS_CODE_PATH).'css/'.api_get_setting('stylesheets').'/frames.css',
  500. $filepath.'css/frames.css'
  501. );
  502. $doc_id = FileManager::add_document(
  503. $_course,
  504. $dir.'css/frames.css',
  505. 'file',
  506. filesize($filepath.'css/frames.css'),
  507. 'frames.css'
  508. );
  509. api_item_property_update(
  510. $_course,
  511. TOOL_DOCUMENT,
  512. $doc_id,
  513. 'DocumentAdded',
  514. $_user['user_id'],
  515. null,
  516. null,
  517. null,
  518. null,
  519. $current_session_id
  520. );
  521. api_item_property_update(
  522. $_course,
  523. TOOL_DOCUMENT,
  524. $doc_id,
  525. 'invisible',
  526. $_user['user_id'],
  527. null,
  528. null,
  529. null,
  530. null,
  531. $current_session_id
  532. );
  533. }
  534. $file_size = filesize($filepath.$filename.'.'.$extension);
  535. $save_file_path = $dir.$filename.'.'.$extension;
  536. $document_id = FileManager::add_document(
  537. $_course,
  538. $save_file_path,
  539. 'file',
  540. $file_size,
  541. $title,
  542. null,
  543. $readonly
  544. );
  545. if ($document_id) {
  546. api_item_property_update(
  547. $_course,
  548. TOOL_DOCUMENT,
  549. $document_id,
  550. 'DocumentAdded',
  551. $_user['user_id'],
  552. $to_group_id,
  553. null,
  554. null,
  555. null,
  556. $current_session_id
  557. );
  558. // Update parent folders
  559. FileManager::item_property_update_on_folder($_course, $dir, $_user['user_id']);
  560. $new_comment = isset($_POST['comment']) ? trim($_POST['comment']) : '';
  561. $new_comment = Database::escape_string($new_comment);
  562. $new_title = isset($_POST['title']) ? trim($_POST['title']) : '';
  563. $new_title = htmlspecialchars($new_title);
  564. $new_title = Database::escape_string($new_title);
  565. if ($new_comment || $new_title) {
  566. $ct = '';
  567. if ($new_comment) {
  568. $ct .= ", comment='$new_comment'";
  569. }
  570. if ($new_title) {
  571. $ct .= ", title='$new_title'";
  572. }
  573. Database::query(
  574. "UPDATE $doc_table SET".substr($ct, 1)." WHERE c_id = $course_id AND id = '$document_id'"
  575. );
  576. }
  577. $dir = substr($dir, 0, -1);
  578. $selectcat = '';
  579. if (isset($_REQUEST['selectcat'])) {
  580. $selectcat = "&selectcat=".Security::remove_XSS($_REQUEST['selectcat']);
  581. }
  582. $certificate_condition = '';
  583. if ($is_certificate_mode) {
  584. $df = DocumentManager::get_default_certificate_id($_course['code']);
  585. if (!isset($df)) {
  586. DocumentManager::attach_gradebook_certificate($_course['code'], $document_id);
  587. }
  588. $certificate_condition = '&certificate=true';
  589. }
  590. header('Location: document.php?'.api_get_cidreq().'&id='.$folder_id.$selectcat.$certificate_condition);
  591. exit();
  592. } else {
  593. Display :: display_header($nameTools, 'Doc');
  594. Display :: display_error_message(get_lang('Impossible'));
  595. Display :: display_footer();
  596. }
  597. } else {
  598. Display :: display_header($nameTools, 'Doc');
  599. Display :: display_error_message(get_lang('Impossible'));
  600. Display :: display_footer();
  601. }
  602. } else {
  603. // Interbreadcrumb for the current directory root path
  604. // Copied from document.php
  605. $dir_array = explode('/', $dir);
  606. $array_len = count($dir_array);
  607. // Interbreadcrumb for the current directory root path
  608. if (empty($document_data['parents'])) {
  609. $interbreadcrumb[] = array('url' => '#', 'name' => $document_data['title']);
  610. } else {
  611. foreach ($document_data['parents'] as $document_sub_data) {
  612. $interbreadcrumb[] = array(
  613. 'url' => $document_sub_data['document_url'],
  614. 'name' => $document_sub_data['title']
  615. );
  616. }
  617. }
  618. Display :: display_header($nameTools, "Doc");
  619. // actions
  620. echo '<div class="actions">';
  621. // link back to the documents overview
  622. if ($is_certificate_mode) {
  623. echo '<a href="document.php?certificate=true&id='.$folder_id.'&selectcat='.Security::remove_XSS(
  624. $_GET['selectcat']
  625. ).'">'.Display::return_icon(
  626. 'back.png',
  627. get_lang('Back').' '.get_lang('To').' '.get_lang('CertificateOverview'),
  628. '',
  629. ICON_SIZE_MEDIUM
  630. ).'</a>';
  631. } else {
  632. echo '<a href="document.php?curdirpath='.Security::remove_XSS($dir).'">'.Display::return_icon(
  633. 'back.png',
  634. get_lang('Back').' '.get_lang('To').' '.get_lang('DocumentsOverview'),
  635. '',
  636. ICON_SIZE_MEDIUM
  637. ).'</a>';
  638. }
  639. echo '</div>';
  640. if ($is_certificate_mode) {
  641. $all_information_by_create_certificate = DocumentManager::get_all_info_to_certificate(
  642. api_get_user_id(),
  643. api_get_course_id()
  644. );
  645. $str_info = '';
  646. foreach ($all_information_by_create_certificate[0] as $info_value) {
  647. $str_info .= $info_value.'<br/>';
  648. }
  649. $create_certificate = get_lang('CreateCertificateWithTags');
  650. Display::display_normal_message($create_certificate.': <br /><br/>'.$str_info, false);
  651. }
  652. // HTML-editor
  653. echo '<div class="row-fluid" style="overflow:hidden">
  654. <div id="template_col" class="span2" style="width:162px">
  655. <div id="frmModel" style="overflow: visible;"></div>
  656. </div>
  657. <div id="hide_bar_template"></div>
  658. <div id="doc_form" class="span9">
  659. '.$form->return_form().'
  660. </div>
  661. </div>';
  662. Display :: display_footer();
  663. }