create_document.php 24 KB

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