create_document.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  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. $_SESSION['whereami'] = 'document/create';
  16. $this_section = SECTION_COURSES;
  17. $htmlHeadXtra[] = '
  18. <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. if (isset($_REQUEST['certificate']) && $_REQUEST['certificate'] == 'true') {
  138. $is_certificate_mode = true;
  139. }
  140. if ($is_certificate_mode) {
  141. $nameTools = get_lang('CreateCertificate');
  142. } else {
  143. $nameTools = get_lang('CreateDocument');
  144. }
  145. /* Constants and variables */
  146. $doc_table = Database::get_course_table(TABLE_DOCUMENT);
  147. $course_id = api_get_course_int_id();
  148. $document_data = DocumentManager::get_document_data_by_id($_REQUEST['id'], api_get_course_id(), true);
  149. if (empty($document_data)) {
  150. if (api_is_in_group()) {
  151. $group_properties = GroupManager::get_group_properties(api_get_group_id());
  152. $document_id = DocumentManager::get_document_id(api_get_course_info(), $group_properties['directory']);
  153. $document_data = DocumentManager::get_document_data_by_id($document_id, api_get_course_id());
  154. $dir = $document_data['path'];
  155. $folder_id = $document_data['id'];
  156. } else {
  157. $dir = '/';
  158. $folder_id = 0;
  159. }
  160. } else {
  161. $folder_id = $document_data['id'];
  162. $dir = $document_data['path'];
  163. }
  164. /* MAIN CODE */
  165. // Please, do not modify this dirname formatting
  166. if (strstr($dir, '..')) {
  167. $dir = '/';
  168. }
  169. if ($dir[0] == '.') {
  170. $dir = substr($dir, 1);
  171. }
  172. if ($dir[0] != '/') {
  173. $dir = '/'.$dir;
  174. }
  175. if ($dir[strlen($dir) - 1] != '/') {
  176. $dir .= '/';
  177. }
  178. if ($is_certificate_mode) {
  179. $document_id = DocumentManager::get_document_id(api_get_course_info(), '/certificates');
  180. $document_data = DocumentManager::get_document_data_by_id($document_id, api_get_course_id(), true);
  181. $folder_id = $document_data['id'];
  182. $dir = '/certificates/';
  183. }
  184. // Configuration for the FCKEDITOR
  185. $doc_tree = explode('/', $dir);
  186. $count_dir = count($doc_tree) - 2; // "2" because at the begin and end there are 2 "/"
  187. if (api_is_in_group()) {
  188. $group_properties = GroupManager::get_group_properties(api_get_group_id());
  189. // Level correction for group documents.
  190. if (!empty($group_properties['directory'])) {
  191. $count_dir = $count_dir > 0 ? $count_dir - 1 : 0;
  192. }
  193. }
  194. $relative_url = '';
  195. for ($i = 0; $i < ($count_dir); $i++) {
  196. $relative_url .= '../';
  197. }
  198. // We do this in order to avoid the condition in html_editor.php ==> if ($this -> fck_editor->Config['CreateDocumentWebDir']=='' || $this -> fck_editor->Config['CreateDocumentDir']== '')
  199. if ($relative_url == '') {
  200. $relative_url = '/';
  201. }
  202. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  203. $html_editor_config = array(
  204. 'ToolbarSet' => ($is_allowed_to_edit ? 'Documents' : 'DocumentsStudent'),
  205. 'Width' => '100%',
  206. 'Height' => '500',
  207. 'FullPage' => true,
  208. 'InDocument' => true,
  209. 'CreateDocumentDir' => $relative_url,
  210. 'CreateDocumentWebDir' => (empty($group_properties['directory']))
  211. ? api_get_path(WEB_COURSE_PATH).$_course['path'].'/document/'
  212. : api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document'.$group_properties['directory'].'/',
  213. 'BaseHref' => api_get_path(WEB_COURSE_PATH).$_course['path'].'/document'.$dir
  214. );
  215. if ($is_certificate_mode) {
  216. $html_editor_config['CreateDocumentDir'] = api_get_path(WEB_COURSE_PATH).$_course['path'].'/document/';
  217. $html_editor_config['CreateDocumentWebDir'] = api_get_path(WEB_COURSE_PATH).$_course['path'].'/document/';
  218. $html_editor_config['BaseHref'] = api_get_path(WEB_COURSE_PATH).$_course['path'].'/document'.$dir;
  219. }
  220. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document'.$dir;
  221. if (!is_dir($filepath)) {
  222. $filepath = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/';
  223. $dir = '/';
  224. }
  225. $to_group_id = 0;
  226. if (!$is_certificate_mode) {
  227. $req_gid = null;
  228. if (api_is_in_group()) {
  229. $req_gid = '&amp;gidReq='.api_get_group_id();
  230. $interbreadcrumb[] = array(
  231. "url" => "../group/group_space.php?gidReq=".api_get_group_id(),
  232. "name" => get_lang('GroupSpace')
  233. );
  234. $noPHP_SELF = true;
  235. $to_group_id = api_get_group_id();
  236. $path = explode('/', $dir);
  237. if ('/'.$path[1] != $group_properties['directory']) {
  238. api_not_allowed(true);
  239. }
  240. }
  241. $interbreadcrumb[] = array(
  242. "url" => "./document.php?curdirpath=".urlencode($dir).$req_gid,
  243. "name" => get_lang('Documents')
  244. );
  245. } else {
  246. $interbreadcrumb[] = array('url' => '../gradebook/'.$_SESSION['gradebook_dest'], 'name' => get_lang('Gradebook'));
  247. }
  248. if (!$is_allowed_in_course) {
  249. api_not_allowed(true);
  250. }
  251. if (!($is_allowed_to_edit || $_SESSION['group_member_with_upload_rights'] || is_my_shared_folder(
  252. $_user['user_id'],
  253. Security::remove_XSS($dir),
  254. api_get_session_id()
  255. ))
  256. ) {
  257. api_not_allowed(true);
  258. }
  259. /* Header */
  260. event_access_tool(TOOL_DOCUMENT);
  261. $display_dir = $dir;
  262. if (isset ($group_properties)) {
  263. $display_dir = explode('/', $dir);
  264. unset ($display_dir[0]);
  265. unset ($display_dir[1]);
  266. $display_dir = implode('/', $display_dir);
  267. }
  268. $select_cat = isset($_GET['selectcat']) ? intval($_GET['selectcat']) : null;
  269. // Create a new form
  270. $form = new FormValidator('create_document', 'post', api_get_self().'?'.api_get_cidreq().'&dir='.Security::remove_XSS(
  271. urlencode($dir)
  272. ).'&selectcat='.$select_cat, null, array('class' => 'form-vertical'));
  273. // form title
  274. $form->addElement('header', $nameTools);
  275. if ($is_certificate_mode) { //added condition for certicate in gradebook
  276. $form->addElement('hidden', 'certificate', 'true', array('id' => 'certificate'));
  277. if (isset($_GET['selectcat'])) {
  278. $form->addElement('hidden', 'selectcat', $select_cat);
  279. }
  280. }
  281. // Hidden element with current directory
  282. $form->addElement('hidden', 'id');
  283. $defaults = array();
  284. $defaults['id'] = $folder_id;
  285. // Filename
  286. $form->addElement('hidden', 'title_edited', 'false', 'id="title_edited"');
  287. /**
  288. * Check if a document width the choosen filename allready exists
  289. */
  290. function document_exists($filename)
  291. {
  292. global $filepath;
  293. $filename = addslashes(trim($filename));
  294. $filename = Security::remove_XSS($filename);
  295. $filename = replace_dangerous_char($filename);
  296. $filename = FileManager::disable_dangerous_file($filename);
  297. return !file_exists($filepath.$filename.'.html');
  298. }
  299. // Add group to the form
  300. if ($is_certificate_mode) {
  301. $form->addElement('text', 'title', get_lang('CertificateName'), 'class="span4" id="document_title"');
  302. } else {
  303. $form->addElement('text', 'title', get_lang('Title'), 'class="span4" id="document_title"');
  304. }
  305. // Show read-only box only in groups
  306. if (!empty($_SESSION['_gid'])) {
  307. $group[] = $form->createElement('checkbox', 'readonly', '', get_lang('ReadOnly'));
  308. }
  309. $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  310. $form->addRule('title', get_lang('FileExists'), 'callback', 'document_exists');
  311. $current_session_id = api_get_session_id();
  312. $form->add_html_editor('content', '', false, false, $html_editor_config);
  313. // Comment-field
  314. $folders = DocumentManager::get_all_document_folders($_course, $to_group_id, $is_allowed_to_edit);
  315. // If we are not in the certificates creation, display a folder chooser for the
  316. // new document created
  317. if (!$is_certificate_mode && !is_my_shared_folder($_user['user_id'], $dir, $current_session_id)) {
  318. $folders = DocumentManager::get_all_document_folders($_course, $to_group_id, $is_allowed_to_edit);
  319. $parent_select = $form->addElement('select', 'curdirpath', array(null, get_lang('DestinationDirectory')));
  320. // Following two conditions copied from document.inc.php::build_directory_selector()
  321. $folder_titles = array();
  322. if (is_array($folders)) {
  323. $escaped_folders = array();
  324. foreach ($folders as $key => & $val) {
  325. //Hide some folders
  326. if ($val == '/HotPotatoes_files' || $val == '/certificates' || basename($val) == 'css') {
  327. continue;
  328. }
  329. //Admin setting for Hide/Show the folders of all users
  330. if (api_get_setting('show_users_folders') == 'false' && (strstr($val, '/shared_folder') || strstr(
  331. $val,
  332. 'shared_folder_session_'
  333. ))
  334. ) {
  335. continue;
  336. }
  337. //Admin setting for Hide/Show Default folders to all users
  338. if (api_get_setting(
  339. 'show_default_folders'
  340. ) == 'false' && ($val == '/images' || $val == '/flash' || $val == '/audio' || $val == '/video' || strstr(
  341. $val,
  342. '/images/gallery'
  343. ) || $val == '/video/flv')
  344. ) {
  345. continue;
  346. }
  347. //Admin setting for Hide/Show chat history folder
  348. if (api_get_setting('show_chat_folder') == 'false' && $val == '/chat_files') {
  349. continue;
  350. }
  351. $escaped_folders[$key] = Database::escape_string($val);
  352. }
  353. $folder_sql = implode("','", $escaped_folders);
  354. $sql = "SELECT * FROM $doc_table WHERE c_id = $course_id AND filetype='folder' AND path IN ('".$folder_sql."')";
  355. $res = Database::query($sql);
  356. $folder_titles = array();
  357. while ($obj = Database::fetch_object($res)) {
  358. $folder_titles[$obj->path] = $obj->title;
  359. }
  360. }
  361. if (empty($group_dir)) {
  362. $parent_select->addOption(get_lang('HomeDirectory'), '/');
  363. if (is_array($folders)) {
  364. foreach ($folders as & $folder) {
  365. //Hide some folders
  366. if ($folder == '/HotPotatoes_files' || $folder == '/certificates' || basename($folder) == 'css') {
  367. continue;
  368. }
  369. //Admin setting for Hide/Show the folders of all users
  370. if (api_get_setting('show_users_folders') == 'false' && (strstr($folder, '/shared_folder') || strstr(
  371. $folder,
  372. 'shared_folder_session_'
  373. ))
  374. ) {
  375. continue;
  376. }
  377. //Admin setting for Hide/Show Default folders to all users
  378. if (api_get_setting(
  379. 'show_default_folders'
  380. ) == 'false' && ($folder == '/images' || $folder == '/flash' || $folder == '/audio' || $folder == '/video' || strstr(
  381. $folder,
  382. '/images/gallery'
  383. ) || $folder == '/video/flv')
  384. ) {
  385. continue;
  386. }
  387. //Admin setting for Hide/Show chat history folder
  388. if (api_get_setting('show_chat_folder') == 'false' && $folder == '/chat_files') {
  389. continue;
  390. }
  391. $selected = (substr($dir, 0, -1) == $folder) ? ' selected="selected"' : '';
  392. $path_parts = explode('/', $folder);
  393. $folder_titles[$folder] = Text::cut($folder_titles[$folder], 80);
  394. $space_counter = count($path_parts) - 2;
  395. if ($space_counter > 0) {
  396. $label = str_repeat('&nbsp;&nbsp;&nbsp;', $space_counter).' &mdash; '.$folder_titles[$folder];
  397. } else {
  398. $label = ' &mdash; '.$folder_titles[$folder];
  399. }
  400. $parent_select->addOption($label, $folder);
  401. if ($selected != '') {
  402. $parent_select->setSelected($folder);
  403. }
  404. }
  405. }
  406. } else {
  407. foreach ($folders as & $folder) {
  408. $selected = (substr($dir, 0, -1) == $folder) ? ' selected="selected"' : '';
  409. $label = $folder_titles[$folder];
  410. if ($folder == $group_dir) {
  411. $label = '/ ('.get_lang('HomeDirectory').')';
  412. } else {
  413. $path_parts = explode('/', str_replace($group_dir, '', $folder));
  414. $label = Text::cut($label, 80);
  415. $label = str_repeat('&nbsp;&nbsp;&nbsp;', count($path_parts) - 2).' &mdash; '.$label;
  416. }
  417. $parent_select->addOption($label, $folder);
  418. if ($selected != '') {
  419. $parent_select->setSelected($folder);
  420. }
  421. }
  422. }
  423. }
  424. if ($is_certificate_mode) {
  425. $form->addElement('style_submit_button', 'submit', get_lang('CreateCertificate'), 'class="save"');
  426. } else {
  427. $form->addElement('style_submit_button', 'submit', get_lang('CreateDoc'), 'class="save"');
  428. }
  429. $form->setDefaults($defaults);
  430. // If form validates -> save the new document
  431. if ($form->validate()) {
  432. $values = $form->exportValues();
  433. $readonly = isset($values['readonly']) ? 1 : 0;
  434. $values['title'] = trim($values['title']);
  435. if (!empty($values['curdirpath'])) {
  436. $dir = $values['curdirpath'];
  437. }
  438. if ($dir[strlen($dir) - 1] != '/') {
  439. $dir .= '/';
  440. }
  441. //Setting the filename
  442. $filename = $values['title'];
  443. $filename = addslashes(trim($filename));
  444. $filename = Security::remove_XSS($filename);
  445. $filename = replace_dangerous_char($filename);
  446. $filename = FileManager::disable_dangerous_file($filename);
  447. //Setting the title
  448. $title = $values['title'];
  449. //Setting the extension
  450. $extension = 'html';
  451. $content = Security::remove_XSS($values['content'], COURSEMANAGERLOWSECURITY);
  452. if (strpos($content, '/css/frames.css') === false) {
  453. $content = str_replace(
  454. '</head>',
  455. '<style> body{margin:10px;}</style><link rel="stylesheet" href="./css/frames.css" type="text/css" /></head>',
  456. $content
  457. );
  458. }
  459. if ($fp = @fopen($filepath.$filename.'.'.$extension, 'w')) {
  460. $content = str_replace(api_get_path(WEB_COURSE_PATH), $_configuration['url_append'].'/courses/', $content);
  461. // change the path of mp3 to absolute
  462. // first regexp deals with ../../../ urls
  463. // Disabled by Ivan Tcholakov.
  464. //$content = preg_replace("|(flashvars=\"file=)(\.+/)+|","$1".api_get_path(REL_COURSE_PATH).$_course['path'].'/document/',$content);
  465. //second regexp deals with audio/ urls
  466. // Disabled by Ivan Tcholakov.
  467. //$content = preg_replace("|(flashvars=\"file=)([^/]+)/|","$1".api_get_path(REL_COURSE_PATH).$_course['path'].'/document/$2/',$content);
  468. fputs($fp, $content);
  469. fclose($fp);
  470. chmod($filepath.$filename.'.'.$extension, api_get_permissions_for_new_files());
  471. if (!is_dir($filepath.'css')) {
  472. mkdir($filepath.'css', api_get_permissions_for_new_directories());
  473. $doc_id = FileManager::add_document($_course, $dir.'css', 'folder', 0, 'css');
  474. api_item_property_update(
  475. $_course,
  476. TOOL_DOCUMENT,
  477. $doc_id,
  478. 'FolderCreated',
  479. $_user['user_id'],
  480. null,
  481. null,
  482. null,
  483. null,
  484. $current_session_id
  485. );
  486. api_item_property_update(
  487. $_course,
  488. TOOL_DOCUMENT,
  489. $doc_id,
  490. 'invisible',
  491. $_user['user_id'],
  492. null,
  493. null,
  494. null,
  495. null,
  496. $current_session_id
  497. );
  498. }
  499. if (!is_file($filepath.'css/frames.css')) {
  500. // Make a copy of the current css for the new document
  501. copy(
  502. api_get_path(SYS_CODE_PATH).'css/'.api_get_setting('stylesheets').'/frames.css',
  503. $filepath.'css/frames.css'
  504. );
  505. $doc_id = FileManager::add_document(
  506. $_course,
  507. $dir.'css/frames.css',
  508. 'file',
  509. filesize($filepath.'css/frames.css'),
  510. 'frames.css'
  511. );
  512. api_item_property_update(
  513. $_course,
  514. TOOL_DOCUMENT,
  515. $doc_id,
  516. 'DocumentAdded',
  517. $_user['user_id'],
  518. null,
  519. null,
  520. null,
  521. null,
  522. $current_session_id
  523. );
  524. api_item_property_update(
  525. $_course,
  526. TOOL_DOCUMENT,
  527. $doc_id,
  528. 'invisible',
  529. $_user['user_id'],
  530. null,
  531. null,
  532. null,
  533. null,
  534. $current_session_id
  535. );
  536. }
  537. $file_size = filesize($filepath.$filename.'.'.$extension);
  538. $save_file_path = $dir.$filename.'.'.$extension;
  539. $document_id = FileManager::add_document(
  540. $_course,
  541. $save_file_path,
  542. 'file',
  543. $file_size,
  544. $title,
  545. null,
  546. $readonly
  547. );
  548. if ($document_id) {
  549. api_item_property_update(
  550. $_course,
  551. TOOL_DOCUMENT,
  552. $document_id,
  553. 'DocumentAdded',
  554. $_user['user_id'],
  555. $to_group_id,
  556. null,
  557. null,
  558. null,
  559. $current_session_id
  560. );
  561. // Update parent folders
  562. FileManager::item_property_update_on_folder($_course, $dir, $_user['user_id']);
  563. $new_comment = isset($_POST['comment']) ? trim($_POST['comment']) : '';
  564. $new_comment = Database::escape_string($new_comment);
  565. $new_title = isset($_POST['title']) ? trim($_POST['title']) : '';
  566. $new_title = htmlspecialchars($new_title);
  567. $new_title = Database::escape_string($new_title);
  568. if ($new_comment || $new_title) {
  569. $ct = '';
  570. if ($new_comment) {
  571. $ct .= ", comment='$new_comment'";
  572. }
  573. if ($new_title) {
  574. $ct .= ", title='$new_title'";
  575. }
  576. Database::query(
  577. "UPDATE $doc_table SET".substr($ct, 1)." WHERE c_id = $course_id AND id = '$document_id'"
  578. );
  579. }
  580. $dir = substr($dir, 0, -1);
  581. $selectcat = '';
  582. if (isset($_REQUEST['selectcat'])) {
  583. $selectcat = "&selectcat=".Security::remove_XSS($_REQUEST['selectcat']);
  584. }
  585. $certificate_condition = '';
  586. if ($is_certificate_mode) {
  587. $df = DocumentManager::get_default_certificate_id($_course['code']);
  588. if (!isset($df)) {
  589. DocumentManager::attach_gradebook_certificate($_course['code'], $document_id);
  590. }
  591. $certificate_condition = '&certificate=true';
  592. }
  593. header('Location: document.php?'.api_get_cidreq().'&id='.$folder_id.$selectcat.$certificate_condition);
  594. exit();
  595. } else {
  596. Display :: display_header($nameTools, 'Doc');
  597. Display :: display_error_message(get_lang('Impossible'));
  598. Display :: display_footer();
  599. }
  600. } else {
  601. Display :: display_header($nameTools, 'Doc');
  602. Display :: display_error_message(get_lang('Impossible'));
  603. Display :: display_footer();
  604. }
  605. } else {
  606. // Interbreadcrumb for the current directory root path
  607. // Copied from document.php
  608. $dir_array = explode('/', $dir);
  609. $array_len = count($dir_array);
  610. // Interbreadcrumb for the current directory root path
  611. if (empty($document_data['parents'])) {
  612. $interbreadcrumb[] = array('url' => '#', 'name' => $document_data['title']);
  613. } else {
  614. foreach ($document_data['parents'] as $document_sub_data) {
  615. $interbreadcrumb[] = array(
  616. 'url' => $document_sub_data['document_url'],
  617. 'name' => $document_sub_data['title']
  618. );
  619. }
  620. }
  621. Display :: display_header($nameTools, "Doc");
  622. //api_display_tool_title($nameTools);
  623. // actions
  624. echo '<div class="actions">';
  625. // link back to the documents overview
  626. if ($is_certificate_mode) {
  627. echo '<a href="document.php?certificate=true&id='.$folder_id.'&selectcat='.Security::remove_XSS(
  628. $_GET['selectcat']
  629. ).'">'.Display::return_icon(
  630. 'back.png',
  631. get_lang('Back').' '.get_lang('To').' '.get_lang('CertificateOverview'),
  632. '',
  633. ICON_SIZE_MEDIUM
  634. ).'</a>';
  635. } else {
  636. echo '<a href="document.php?curdirpath='.Security::remove_XSS($dir).'">'.Display::return_icon(
  637. 'back.png',
  638. get_lang('Back').' '.get_lang('To').' '.get_lang('DocumentsOverview'),
  639. '',
  640. ICON_SIZE_MEDIUM
  641. ).'</a>';
  642. }
  643. echo '</div>';
  644. if ($is_certificate_mode) {
  645. $all_information_by_create_certificate = DocumentManager::get_all_info_to_certificate(
  646. api_get_user_id(),
  647. api_get_course_id()
  648. );
  649. $str_info = '';
  650. foreach ($all_information_by_create_certificate[0] as $info_value) {
  651. $str_info .= $info_value.'<br/>';
  652. }
  653. $create_certificate = get_lang('CreateCertificateWithTags');
  654. Display::display_normal_message($create_certificate.': <br /><br/>'.$str_info, false);
  655. }
  656. // HTML-editor
  657. echo '<div class="row-fluid" style="overflow:hidden">
  658. <div id="template_col" class="span2" style="width:162px">
  659. <div id="frmModel" style="overflow: visible;"></div>
  660. </div>
  661. <div id="hide_bar_template"></div>
  662. <div id="doc_form" class="span9">
  663. '.$form->return_form().'
  664. </div>
  665. </div>';
  666. Display :: display_footer();
  667. }