work.php 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  1. <?php
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004-2008 Dokeos S.P.R.L
  6. For a full list of contributors, see "credits.txt".
  7. The full license can be read in "license.txt".
  8. This program is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU General Public License
  10. as published by the Free Software Foundation; either version 2
  11. of the License, or (at your option) any later version.
  12. See the GNU General Public License for more details.
  13. Contact address: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium
  14. Mail: info@dokeos.com
  15. ==============================================================================
  16. */
  17. /**
  18. * @package dokeos.studentpublications
  19. * @author Thomas, Hugues, Christophe - original version
  20. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University - ability for course admins to specify wether uploaded documents are visible or invisible by default.
  21. * @author Roan Embrechts, code refactoring and virtual course support
  22. * @author Frederic Vauthier, directories management
  23. * @version $Id: work.php 14854 2008-04-11 15:42:13Z juliomontoya $
  24. *
  25. * @todo refactor more code into functions, use quickforms, coding standards, ...
  26. */
  27. /**
  28. ==============================================================================
  29. * STUDENT PUBLICATIONS MODULE
  30. *
  31. * Note: for a more advanced module, see the dropbox tool.
  32. * This one is easier with less options.
  33. * This tool is better used for publishing things,
  34. * sending in assignments is better in the dropbox.
  35. *
  36. * GOALS
  37. * *****
  38. * Allow student to quickly send documents immediately
  39. * visible on the course website.
  40. *
  41. * The script does 5 things:
  42. *
  43. * 1. Upload documents
  44. * 2. Give them a name
  45. * 3. Modify data about documents
  46. * 4. Delete link to documents and simultaneously remove them
  47. * 5. Show documents list to students and visitors
  48. *
  49. * On the long run, the idea is to allow sending realvideo . Which means only
  50. * establish a correspondence between RealServer Content Path and the user's
  51. * documents path.
  52. *
  53. * All documents are sent to the address /$_configuration['root_sys']/$currentCourseID/document/
  54. * where $currentCourseID is the web directory for the course and $_configuration['root_sys']
  55. * usually /var/www/html
  56. *
  57. * Modified by Patrick Cool, february 2004:
  58. * Allow course managers to specify wether newly uploaded documents should
  59. * be visible or unvisible by default
  60. * This is ideal for reviewing the uploaded documents before the document
  61. * is available for everyone.
  62. *
  63. * note: maybe the form to change the behaviour should go into the course
  64. * properties page?
  65. * note 2: maybe a new field should be created in the course table for
  66. * this behaviour.
  67. *
  68. * We now use the show_score field since this is not used.
  69. *
  70. ==============================================================================
  71. */
  72. /*
  73. ==============================================================================
  74. INIT SECTION
  75. ==============================================================================
  76. */
  77. // name of the language file that needs to be included
  78. $language_file = array (
  79. 'work',
  80. 'document',
  81. 'admin'
  82. );
  83. // Section (for the tabs)
  84. $this_section = SECTION_COURSES;
  85. // @todo why is this needed?
  86. //session
  87. if (isset ($_GET['id_session'])) {
  88. $_SESSION['id_session'] = Database::escape_string($_GET['id_session']);
  89. }
  90. $htmlHeadXtra[] = '<script>
  91. function updateDocumentTitle(value){
  92. var temp = value.indexOf("/");
  93. //linux path
  94. if(temp!=-1){
  95. var temp=value.split("/");
  96. }
  97. else{
  98. var temp=value.split("\\\");
  99. }
  100. document.getElementById("file_upload").value=temp[temp.length-1];
  101. }
  102. </script>
  103. ';
  104. /*
  105. -----------------------------------------------------------
  106. Including necessary files
  107. -----------------------------------------------------------
  108. */
  109. require ('../inc/global.inc.php');
  110. require_once (api_get_path(LIBRARY_PATH) . "course.lib.php");
  111. require_once (api_get_path(LIBRARY_PATH) . "debug.lib.inc.php");
  112. require_once (api_get_path(LIBRARY_PATH) . "events.lib.inc.php");
  113. require_once (api_get_path(LIBRARY_PATH) . "security.lib.php");
  114. require_once(api_get_path(LIBRARY_PATH) . "formvalidator/FormValidator.class.php");
  115. require_once ('work.lib.php');
  116. /*
  117. -----------------------------------------------------------
  118. Table definitions
  119. -----------------------------------------------------------
  120. */
  121. $main_course_table = Database :: get_main_table(TABLE_MAIN_COURSE);
  122. $work_table = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
  123. $iprop_table = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  124. /*
  125. -----------------------------------------------------------
  126. Constants and variables
  127. -----------------------------------------------------------
  128. */
  129. $tool_name = get_lang('StudentPublications');
  130. $user_id = api_get_user_id();
  131. $course_code = $_course['sysCode'];
  132. $is_course_member = $is_courseMember || api_is_platform_admin();
  133. $currentCourseRepositorySys = api_get_path(SYS_COURSE_PATH) . $_course["path"] . "/";
  134. $currentCourseRepositoryWeb = api_get_path(WEB_COURSE_PATH) . $_course["path"] . "/";
  135. $currentUserFirstName = $_user['firstName'];
  136. $currentUserLastName = $_user['lastName'];
  137. $authors = Database :: escape_string($_POST['authors']);
  138. $delete = Database :: escape_string($_REQUEST['delete']);
  139. $description = Database :: escape_string($_REQUEST['description']);
  140. $display_tool_options = $_REQUEST['display_tool_options'];
  141. $display_upload_form = $_REQUEST['display_upload_form'];
  142. $edit = Database :: escape_string($_REQUEST['edit']);
  143. $make_invisible = Database :: escape_string($_REQUEST['make_invisible']);
  144. $make_visible = Database :: escape_string($_REQUEST['make_visible']);
  145. $origin = Security :: remove_XSS($_REQUEST['origin']);
  146. $submitGroupWorkUrl = Security :: remove_XSS($_REQUEST['submitGroupWorkUrl']);
  147. $title = Database :: escape_string($_REQUEST['title']);
  148. $uploadvisibledisabled = Database :: escape_string($_REQUEST['uploadvisibledisabled']);
  149. $id = strval(intval($_REQUEST['id']));
  150. //directories management
  151. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  152. $course_dir = $sys_course_path . $_course['path'];
  153. $base_work_dir = $course_dir . '/work';
  154. $http_www = api_get_path('WEB_COURSE_PATH') . $_course['path'] . '/work';
  155. $cur_dir_path = '';
  156. if (isset ($_GET['curdirpath']) && $_GET['curdirpath'] != '') {
  157. //$cur_dir_path = preg_replace('#[\.]+/#','',$_GET['curdirpath']); //escape '..' hack attempts
  158. //now using common security approach with security lib
  159. $in_course = Security :: check_abs_path($base_work_dir . '/' . $_GET['curdirpath'], $base_work_dir);
  160. if (!$in_course) {
  161. $cur_dir_path = "/";
  162. } else {
  163. $cur_dir_path = $_GET['curdirpath'];
  164. }
  165. }
  166. elseif (isset ($_POST['curdirpath']) && $_POST['curdirpath'] != '') {
  167. //$cur_dir_path = preg_replace('#[\.]+/#','/',$_POST['curdirpath']); //escape '..' hack attempts
  168. //now using common security approach with security lib
  169. $in_course = Security :: check_abs_path($base_work_dir . '/' . $_POST['curdirpath'], $base_work_dir);
  170. if (!$in_course) {
  171. $cur_dir_path = "/";
  172. } else {
  173. $cur_dir_path = $_POST['curdirpath'];
  174. }
  175. } else {
  176. $cur_dir_path = '/';
  177. }
  178. if ($cur_dir_path == '.') {
  179. $cur_dir_path = '/';
  180. }
  181. $cur_dir_path_url = urlencode($cur_dir_path);
  182. //prepare a form of path that can easily be added at the end of any url ending with "work/"
  183. $my_cur_dir_path = $cur_dir_path;
  184. if ($my_cur_dir_path == '/')
  185. {
  186. $my_cur_dir_path = '';
  187. }
  188. elseif (substr($my_cur_dir_path, -1, 1) != '/')
  189. {
  190. $my_cur_dir_path = $my_cur_dir_path . '/';
  191. }
  192. /*
  193. -----------------------------------------------------------
  194. Configuration settings
  195. -----------------------------------------------------------
  196. */
  197. $link_target_parameter = ""; //or e.g. "target=\"_blank\"";
  198. $always_show_tool_options = false;
  199. $always_show_upload_form = false;
  200. if ($always_show_tool_options) {
  201. $display_tool_options = true;
  202. }
  203. if ($always_show_upload_form) {
  204. $display_upload_form = true;
  205. }
  206. api_protect_course_script(true);
  207. /*
  208. -----------------------------------------------------------
  209. More init stuff
  210. -----------------------------------------------------------
  211. */
  212. if (isset ($_POST['cancelForm']) && !empty ($_POST['cancelForm'])) {
  213. header('Location: ' . api_get_self() . "?origin=$origin");
  214. exit ();
  215. }
  216. if ($_POST['submitWork'] || $submitGroupWorkUrl) {
  217. // these libraries are only used for upload purpose
  218. // so we only include them when necessary
  219. include_once (api_get_path(INCLUDE_PATH) . "lib/fileUpload.lib.php");
  220. include_once (api_get_path(INCLUDE_PATH) . "lib/fileDisplay.lib.php"); // need format_url function
  221. }
  222. // If the POST's size exceeds 8M (default value in php.ini) the $_POST array is emptied
  223. // If that case happens, we set $submitWork to 1 to allow displaying of the error message
  224. // The redirection with header() is needed to avoid apache to show an error page on the next request
  225. if ($_SERVER['REQUEST_METHOD'] == 'POST' && !sizeof($_POST)) {
  226. if (strstr($_SERVER['REQUEST_URI'], '?')) {
  227. header('Location: ' . $_SERVER['REQUEST_URI'] . '&submitWork=1');
  228. exit ();
  229. } else {
  230. header('Location: ' . $_SERVER['REQUEST_URI'] . '?submitWork=1');
  231. exit ();
  232. }
  233. }
  234. //toolgroup comes from group. the but of tis variable is to limit post to the group of the student
  235. if (!api_is_course_admin()) {
  236. if (!empty ($_GET['toolgroup'])) {
  237. $toolgroup = Database::escape_string($_GET['toolgroup']);
  238. api_session_register('toolgroup');
  239. }
  240. }
  241. /*
  242. -----------------------------------------------------------
  243. Header
  244. -----------------------------------------------------------
  245. */
  246. if ($origin != 'learnpath') {
  247. $interbreadcrumb[] = array (
  248. 'url' => $url_dir,
  249. 'name' => get_lang('StudentPublications'));
  250. //if (!$display_tool_options && !$display_upload_form)
  251. //{
  252. //------interbreadcrumb for the current directory root path
  253. $dir_array = explode("/", $cur_dir_path);
  254. $array_len = count($dir_array);
  255. if ($array_len > 0)
  256. {
  257. $url_dir = 'work.php?&curdirpath=/';
  258. $interbreadcrumb[] = array (
  259. 'url' => $url_dir,
  260. 'name' => get_lang('HomeDirectory'));
  261. }
  262. $dir_acum = '';
  263. for ($i = 0; $i < $array_len; $i++) {
  264. $url_dir = 'work.php?&curdirpath=' . $dir_acum . $dir_array[$i];
  265. $interbreadcrumb[] = array (
  266. 'url' => $url_dir,
  267. 'name' => $dir_array[$i]
  268. );
  269. $dir_acum .= $dir_array[$i] . '/';
  270. }
  271. // }
  272. if ($display_upload_form) {
  273. //$tool_name = get_lang("UploadADocument");
  274. //$interbreadcrumb[] = array ("url" => "work.php", "name" => get_lang('StudentPublications'));
  275. $interbreadcrumb[] = array (
  276. "url" => "work.php",
  277. "name" => get_lang('UploadADocument'));
  278. }
  279. if ($display_tool_options) {
  280. //$tool_name = get_lang("EditToolOptions");
  281. //$interbreadcrumb[] = array ("url" => "work.php", "name" => get_lang('StudentPublications'));
  282. $interbreadcrumb[] = array (
  283. "url" => "work.php",
  284. "name" => get_lang('EditToolOptions'));
  285. }
  286. //--------------------------------------------------
  287. Display :: display_header(null);
  288. }
  289. else
  290. {
  291. //we are in the learnpath tool
  292. include api_get_path(INCLUDE_PATH) . 'reduced_header.inc.php';
  293. }
  294. //stats
  295. event_access_tool(TOOL_STUDENTPUBLICATION);
  296. $is_allowed_to_edit = api_is_allowed_to_edit(); //has to come after display_tool_view_option();
  297. //api_display_tool_title($tool_name);
  298. /*
  299. ==============================================================================
  300. MAIN CODE
  301. ==============================================================================
  302. */
  303. if (isset ($_POST['changeProperties']))
  304. {
  305. $query = "UPDATE " . $main_course_table . " SET show_score='" . $uploadvisibledisabled . "' WHERE code='" . $_course['sysCode'] . "'";
  306. api_sql_query($query, __FILE__, __LINE__);
  307. $_course['show_score'] = $uploadvisibledisabled;
  308. }
  309. else
  310. {
  311. $query = "SELECT * FROM " . $main_course_table . " WHERE code=\"" . $_course['sysCode'] . "\"";
  312. $result = api_sql_query($query, __FILE__, __LINE__);
  313. $row = mysql_fetch_array($result);
  314. $uploadvisibledisabled = $row["show_score"];
  315. }
  316. /*
  317. -----------------------------------------------------------
  318. Introduction section
  319. -----------------------------------------------------------
  320. */
  321. Display :: display_introduction_section(TOOL_STUDENTPUBLICATION);
  322. /*
  323. -----------------------------------------------------------
  324. COMMANDS SECTION (reserved for course administrator)
  325. -----------------------------------------------------------
  326. */
  327. if (api_is_allowed_to_edit())
  328. {
  329. /*-------------------------------------------
  330. DELETE WORK COMMAND
  331. -----------------------------------------*/
  332. if ($delete)
  333. {
  334. if ($delete == "all")
  335. {
  336. $queryString1 = "SELECT url FROM " . $work_table . "";
  337. $queryString2 = "DELETE FROM " . $work_table . "";
  338. }
  339. else
  340. {
  341. $queryString1 = "SELECT url FROM " . $work_table . " WHERE id = '$delete'";
  342. $queryString2 = "DELETE FROM " . $work_table . " WHERE id='$delete'";
  343. }
  344. $result1 = api_sql_query($queryString1, __FILE__, __LINE__);
  345. $result2 = api_sql_query($queryString2, __FILE__, __LINE__);
  346. if ($result1)
  347. {
  348. while ($thisUrl = Database::fetch_array($result1)) {
  349. // check the url really points to a file in the work area
  350. // (some work links can come from groups area...)
  351. //if (substr (dirname($thisUrl['url']), -4) == "work")
  352. if (strstr($thisUrl['url'], "work/$my_cur_dir_path") !== false)
  353. {
  354. @ unlink($currentCourseRepositorySys . $thisUrl['url']);
  355. }
  356. }
  357. }
  358. }
  359. /*-------------------------------------------
  360. EDIT COMMAND WORK COMMAND
  361. -----------------------------------------*/
  362. if ($edit)
  363. {
  364. $sql = "SELECT * FROM " . $work_table . " WHERE id='" . $edit . "'";
  365. $result = api_sql_query($sql, __FILE__, __LINE__);
  366. if ($result)
  367. {
  368. $row = mysql_fetch_array($result);
  369. $workTitle = $row['title'];
  370. $workAuthor = $row['author'];
  371. $workDescription = $row['description'];
  372. $workUrl = $row['url'];
  373. }
  374. }
  375. /*-------------------------------------------
  376. MAKE INVISIBLE WORK COMMAND
  377. -----------------------------------------*/
  378. if ($make_invisible)
  379. {
  380. if ($make_invisible == "all")
  381. {
  382. $sql = "ALTER TABLE " . $work_table . "
  383. CHANGE accepted accepted TINYINT(1) DEFAULT '0'";
  384. api_sql_query($sql, __FILE__, __LINE__);
  385. $sql = "UPDATE " . $work_table . "
  386. SET accepted = 0";
  387. api_sql_query($sql, __FILE__, __LINE__);
  388. }
  389. else
  390. {
  391. $sql = "UPDATE " . $work_table . "
  392. SET accepted = 0
  393. WHERE id = '" . $make_invisible . "'";
  394. api_sql_query($sql, __FILE__, __LINE__);
  395. }
  396. }
  397. /*-------------------------------------------
  398. MAKE VISIBLE WORK COMMAND
  399. -----------------------------------------*/
  400. if ($make_visible)
  401. {
  402. if ($make_visible == "all")
  403. {
  404. $sql = "ALTER TABLE " . $work_table . "
  405. CHANGE accepted accepted TINYINT(1) DEFAULT '1'";
  406. api_sql_query($sql, __FILE__, __LINE__);
  407. $sql = "UPDATE " . $work_table . "
  408. SET accepted = 1";
  409. api_sql_query($sql, __FILE__, __LINE__);
  410. }
  411. else
  412. {
  413. $sql = "UPDATE " . $work_table . "
  414. SET accepted = 1
  415. WHERE id = '" . $make_visible . "'";
  416. api_sql_query($sql, __FILE__, __LINE__);
  417. }
  418. // update all the parents in the table item propery
  419. $list_id=get_parent_directories($my_cur_dir_path);
  420. for ($i = 0; $i < count($list_id); $i++)
  421. {
  422. api_item_property_update($_course, 'work', $list_id[$i], get_lang('FolderUpdated'), $user_id);
  423. }
  424. }
  425. /*--------------------
  426. * Create dir command
  427. ---------------------*/
  428. if (!empty ($_REQUEST['create_dir']) && !empty ($_REQUEST['new_dir']))
  429. {
  430. //create the directory
  431. //needed for directory creation
  432. include_once (api_get_path(LIBRARY_PATH) . "fileUpload.lib.php");
  433. $added_slash = (substr($cur_dir_path, -1, 1) == '/') ? '' : '/';
  434. $dir_name = $cur_dir_path . $added_slash . replace_dangerous_char($_POST['new_dir']);
  435. $created_dir = create_unexisting_work_directory($base_work_dir, $dir_name);
  436. // we insert here the directory in the table $work_table
  437. $dir_name_sql='';
  438. if ($created_dir)
  439. {
  440. if ($cur_dir_path=='/')
  441. {
  442. $dir_name_sql = $created_dir;
  443. }
  444. else
  445. {
  446. $dir_name_sql = '/'.$created_dir;
  447. }
  448. $sql_add_publication = "INSERT INTO " . $work_table . " SET " .
  449. "url = '" . $dir_name_sql . "',
  450. title = '',
  451. description = '',
  452. author = '',
  453. active = '0',
  454. accepted = '1',
  455. filetype = 'folder',
  456. post_group_id = '0',
  457. sent_date = NOW()";
  458. api_sql_query($sql_add_publication, __FILE__, __LINE__);
  459. // add the directory
  460. $id = mysql_insert_id();
  461. //Folder created
  462. api_item_property_update($_course, 'work', $id, get_lang('DirCr'), $user_id);
  463. // update all the parents in the table item propery
  464. $list_id=get_parent_directories($my_cur_dir_path);
  465. for ($i = 0; $i < count($list_id); $i++)
  466. {
  467. api_item_property_update($_course, 'work', $list_id[$i], get_lang('FolderUpdated'), $user_id);
  468. }
  469. //-------------------------------------------------------------------------------
  470. Display :: display_normal_message('<span title="' . $created_dir . '">' . get_lang('DirCr') . '</span>', false);
  471. //uncomment if you want to enter the created dir
  472. //$curdirpath = $created_dir;
  473. //$curdirpathurl = urlencode($curdirpath);
  474. }
  475. else
  476. {
  477. Display :: display_error_message(get_lang('CannotCreateDir'));
  478. }
  479. }
  480. /* -------------------
  481. * Delete dir command
  482. --------------------*/
  483. if (!empty ($_REQUEST['delete_dir']))
  484. {
  485. $delete_directory=$_REQUEST['delete_dir'];
  486. del_dir($base_work_dir . '/', $delete_directory);
  487. Display :: display_normal_message($delete_directory . ' ' . get_lang('DirDeleted'));
  488. }
  489. /* ----------------------
  490. * Move file form request
  491. ----------------------- */
  492. if (!empty ($_REQUEST['move']))
  493. {
  494. $folders = get_subdirs_list($base_work_dir, 1);
  495. Display :: display_normal_message(build_work_move_to_selector($folders, $cur_dir_path, $_REQUEST['move']), false);
  496. }
  497. /* ------------------
  498. * Move file command
  499. ------------------- */
  500. if (isset ($_POST['move_to']) && isset ($_POST['move_file']))
  501. {
  502. include_once (api_get_path(LIBRARY_PATH) . "/fileManage.lib.php");
  503. $move_to = $_POST['move_to'];
  504. if ($move_to == '/' or empty ($move_to)) {
  505. $move_to = '';
  506. }
  507. elseif (substr($move_to, -1, 1) != '/') {
  508. $move_to = $move_to . '/';
  509. }
  510. //security fix: make sure they can't move files that are not in the document table
  511. if ($path = get_work_path($_POST['move_file']))
  512. {
  513. //echo "got path $path";
  514. //Display::display_normal_message('We want to move '.$_POST['move_file'].' to '.$_POST['move_to']);
  515. if (move($course_dir . '/' . $path, $base_work_dir . '/' . $move_to)) {
  516. //update db
  517. update_work_url($_POST['move_file'], 'work/' . $move_to);
  518. //set the current path
  519. $cur_dir_path = $move_to;
  520. $cur_dir_path_url = urlencode($move_to);
  521. // update all the parents in the table item propery
  522. $list_id=get_parent_directories($cur_dir_path);
  523. for ($i = 0; $i < count($list_id); $i++)
  524. {
  525. api_item_property_update($_course, 'work', $list_id[$i], get_lang('FolderUpdated'), $user_id);
  526. }
  527. Display :: display_normal_message(get_lang('DirMv'));
  528. }
  529. else
  530. {
  531. Display :: display_error_message(get_lang('Impossible'));
  532. }
  533. }
  534. else
  535. {
  536. Display :: display_error_message(get_lang('Impossible'));
  537. }
  538. }
  539. }
  540. /*
  541. -----------------------------------------------------------
  542. COMMANDS SECTION (reserved for others - check they're authors each time)
  543. -----------------------------------------------------------
  544. */
  545. else
  546. {
  547. $iprop_table = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  548. $user_id = api_get_user_id();
  549. /*-------------------------------------------
  550. DELETE WORK COMMAND
  551. -----------------------------------------*/
  552. if ($delete)
  553. {
  554. if ($delete == "all")
  555. {
  556. /*not authorized to this user */
  557. }
  558. else
  559. {
  560. //Get the author ID for that document from the item_property table
  561. $author_sql = "SELECT * FROM $iprop_table WHERE tool = 'work' AND insert_user_id='$user_id' AND ref=" . mysql_real_escape_string($delete);
  562. $author_qry = api_sql_query($author_sql, __FILE__, __LINE__);
  563. if (Database :: num_rows($author_qry) == 1)
  564. {
  565. //we found the current user is the author
  566. $queryString1 = "SELECT url FROM " . $work_table . " WHERE id = '$delete'";
  567. $queryString2 = "DELETE FROM " . $work_table . " WHERE id='$delete'";
  568. $result1 = api_sql_query($queryString1, __FILE__, __LINE__);
  569. $result2 = api_sql_query($queryString2, __FILE__, __LINE__);
  570. if ($result1)
  571. {
  572. api_item_property_update($_course, 'work', $delete, get_lang('DocumentDeleted'), $user_id);
  573. while ($thisUrl = mysql_fetch_array($result1))
  574. {
  575. // check the url really points to a file in the work area
  576. // (some work links can come from groups area...)
  577. if (substr(dirname($thisUrl['url']), -4) == "work")
  578. {
  579. @ unlink($currentCourseRepositorySys . "work/" . $thisWork);
  580. }
  581. }
  582. }
  583. }
  584. }
  585. }
  586. /*-------------------------------------------
  587. EDIT COMMAND WORK COMMAND
  588. -----------------------------------------*/
  589. if ($edit)
  590. {
  591. //Get the author ID for that document from the item_property table
  592. $author_sql = "SELECT * FROM $iprop_table WHERE tool = 'work' AND insert_user_id='$user_id' AND ref=" . $edit;
  593. $author_qry = api_sql_query($author_sql, __FILE__, __LINE__);
  594. if (Database :: num_rows($author_qry) == 1)
  595. {
  596. //we found the current user is the author
  597. $sql = "SELECT * FROM " . $work_table . " WHERE id='" . $edit . "'";
  598. $result = api_sql_query($sql, __FILE__, __LINE__);
  599. if ($result)
  600. {
  601. $row = mysql_fetch_array($result);
  602. $workTitle = $row['title'];
  603. $workAuthor = $row['author'];
  604. $workDescription = $row['description'];
  605. $workUrl = $row['url'];
  606. }
  607. }
  608. }
  609. }
  610. /*
  611. ==============================================================================
  612. FORM SUBMIT PROCEDURE
  613. ==============================================================================
  614. */
  615. $error_message = "";
  616. $check = Security :: check_token('post'); //check the token inserted into the form
  617. if ($_POST['submitWork'] && $is_course_member && $check)
  618. {
  619. if ($_FILES['file']['size'])
  620. {
  621. $updir = $currentCourseRepositorySys . 'work/'; //directory path to upload
  622. // Try to add an extension to the file if it has'nt one
  623. $new_file_name = add_ext_on_mime(stripslashes($_FILES['file']['name']), $_FILES['file']['type']);
  624. // Replace dangerous characters
  625. $new_file_name = replace_dangerous_char($new_file_name, 'strict');
  626. // Transform any .php file in .phps fo security
  627. $new_file_name = php2phps($new_file_name);
  628. //filter extension
  629. if (!filter_extension($new_file_name))
  630. {
  631. Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  632. $succeed = false;
  633. }
  634. else
  635. {
  636. if (!$title)
  637. {
  638. $title = $_FILES['file']['name'];
  639. }
  640. if (!$authors)
  641. {
  642. $authors = $currentUserFirstName . " " . $currentUserLastName;
  643. }
  644. // compose a unique file name to avoid any conflict
  645. $new_file_name = uniqid('') . $new_file_name;
  646. if (isset ($_SESSION['toolgroup']))
  647. {
  648. $post_group_id = $_SESSION['toolgroup'];
  649. }
  650. else
  651. {
  652. $post_group_id = '0';
  653. }
  654. //if we come from the group tools the groupid will be saved in $work_table
  655. @move_uploaded_file($_FILES['file']['tmp_name'], $updir . $my_cur_dir_path . $new_file_name);
  656. $url = "work/" . $my_cur_dir_path . $new_file_name;
  657. $result = api_sql_query("SHOW FIELDS FROM " . $work_table . " LIKE 'sent_date'", __FILE__, __LINE__);
  658. if (!mysql_num_rows($result))
  659. {
  660. api_sql_query("ALTER TABLE " . $work_table . " ADD sent_date DATETIME NOT NULL");
  661. }
  662. $current_date = date('Y-m-d H:i:s');
  663. $sql_add_publication = "INSERT INTO " . $work_table . " SET " .
  664. "url = '" . $url . "',
  665. title = '" . $title . "',
  666. description = '" . $description . "',
  667. author = '" . $authors . "',
  668. active = '" . $active . "',
  669. accepted = '" . (!$uploadvisibledisabled) . "',
  670. post_group_id = '" . $post_group_id . "',
  671. sent_date = ' ".$current_date ."' ";
  672. api_sql_query($sql_add_publication, __FILE__, __LINE__);
  673. $Id = mysql_insert_id();
  674. api_item_property_update($_course, 'work', $Id, get_lang('DocumentAdded'), $user_id);
  675. $succeed = true;
  676. // update all the parents in the table item propery
  677. $list_id=get_parent_directories($my_cur_dir_path);
  678. for ($i = 0; $i < count($list_id); $i++)
  679. {
  680. api_item_property_update($_course, 'work', $list_id[$i], get_lang('FolderUpdated'), $user_id);
  681. }
  682. }
  683. }
  684. elseif ($newWorkUrl)
  685. {
  686. /*
  687. * SPECIAL CASE ! For a work coming from another area (i.e. groups)
  688. */
  689. $url = str_replace('../../' . $_course['path'] . '/', '', $newWorkUrl);
  690. if (!$title)
  691. {
  692. $title = basename($workUrl);
  693. }
  694. $result = api_sql_query("SHOW FIELDS FROM " . $work_table . " LIKE 'sent_date'", __FILE__, __LINE__);
  695. if (!Database::num_rows($result)) {
  696. api_sql_query("ALTER TABLE " . $work_table . " ADD sent_date DATETIME NOT NULL");
  697. }
  698. $sql = "INSERT INTO " . $work_table . "
  699. SET url = '" . $url . "',
  700. title = '" . $title . "',
  701. description = '" . $description . "',
  702. author = '" . $authors . "',
  703. sent_date = NOW()";
  704. api_sql_query($sql, __FILE__, __LINE__);
  705. $insertId = Database::insert_id();
  706. api_item_property_update($_course, 'work', $insertId, get_lang('DocumentAdded'), $user_id);
  707. $succeed = true;
  708. // update all the parents in the table item propery
  709. $list_id=get_parent_directories($my_cur_dir_path);
  710. for ($i = 0; $i < count($list_id); $i++)
  711. {
  712. api_item_property_update($_course, 'work', $list_id[$i], get_lang('FolderUpdated'), $user_id);
  713. }
  714. }
  715. /*
  716. * SPECIAL CASE ! For a work edited
  717. */
  718. else
  719. {
  720. //Get the author ID for that document from the item_property table
  721. $is_author = false;
  722. $author_sql = "SELECT * FROM $iprop_table WHERE tool = 'work' AND insert_user_id='$user_id' AND ref=" . mysql_real_escape_string($id);
  723. $author_qry = api_sql_query($author_sql, __FILE__, __LINE__);
  724. if (Database :: num_rows($author_qry) == 1) {
  725. $is_author = true;
  726. }
  727. if ($id && ($is_allowed_to_edit or $is_author))
  728. {
  729. if (!$title)
  730. {
  731. $title = basename($newWorkUrl);
  732. }
  733. $sql = "UPDATE " . $work_table . "
  734. SET title = '" . $title . "',
  735. description = '" . $description . "',
  736. author = '" . $authors . "'
  737. WHERE id = '" . $id . "'";
  738. api_sql_query($sql, __FILE__, __LINE__);
  739. $insertId = $id;
  740. api_item_property_update($_course, 'work', $insertId, get_lang('DocumentUpdated'), $user_id);
  741. $succeed = true;
  742. }
  743. else
  744. {
  745. $error_message = get_lang('TooBig');
  746. }
  747. }
  748. Security :: clear_token(); //clear the token to prevent re-executing the request with back button
  749. }
  750. if ($_POST['submitWork'] && $succeed && !$id) //last value is to check this is not "just" an edit
  751. {
  752. //YW Tis part serve to send a e-mail to the tutors when a new file is sent
  753. $send = api_get_course_setting('email_alert_manager_on_new_doc');
  754. if ($send > 0)
  755. {
  756. // Lets predefine some variables. Be sure to change the from address!
  757. $table_course_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  758. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  759. $table_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  760. $table_session_course = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE);
  761. $emailto = array ();
  762. if (empty ($_SESSION['id_session'])) {
  763. $sql_resp = 'SELECT u.email as myemail FROM ' . $table_course_user . ' cu, ' . $table_user . ' u WHERE cu.course_code = ' . "'" . api_get_course_id() . "'" . ' AND cu.status = 1 AND u.user_id = cu.user_id';
  764. $res_resp = api_sql_query($sql_resp, __FILE__, __LINE__);
  765. while ($row_email = Database :: fetch_array($res_resp)) {
  766. if (!empty ($row_email['myemail'])) {
  767. $emailto[$row_email['myemail']] = $row_email['myemail'];
  768. }
  769. }
  770. } else {
  771. // coachs of the session
  772. $sql_resp = 'SELECT user.email as myemail
  773. FROM ' . $table_session . ' session
  774. INNER JOIN ' . $table_user . ' user
  775. ON user.user_id = session.id_coach
  776. WHERE session.id = ' . intval($_SESSION['id_session']);
  777. $res_resp = api_sql_query($sql_resp, __FILE__, __LINE__);
  778. while ($row_email = Database :: fetch_array($res_resp)) {
  779. if (!empty ($row_email['myemail'])) {
  780. $emailto[$row_email['myemail']] = $row_email['myemail'];
  781. }
  782. }
  783. //coach of the course
  784. $sql_resp = 'SELECT user.email as myemail
  785. FROM ' . $table_session_course . ' session_course
  786. INNER JOIN ' . $table_user . ' user
  787. ON user.user_id = session_course.id_coach
  788. WHERE session_course.id_session = ' . intval($_SESSION['id_session']);
  789. $res_resp = api_sql_query($sql_resp, __FILE__, __LINE__);
  790. while ($row_email = Database :: fetch_array($res_resp)) {
  791. if (!empty ($row_email['myemail'])) {
  792. $emailto[$row_email['myemail']] = $row_email['myemail'];
  793. }
  794. }
  795. }
  796. if (count($emailto) > 0)
  797. {
  798. $emailto = implode(',', $emailto);
  799. $emailfromaddr = get_setting('emailAdministrator');
  800. $emailfromname = get_setting('siteName');
  801. $emailsubject = "[" . get_setting('siteName') . "] ";
  802. // The body can be as long as you wish, and any combination of text and variables
  803. //$emailbody=get_lang('SendMailBody').' '.api_get_path(WEB_CODE_PATH)."work/work.php?".api_get_cidreq()." ($title)\n\n".get_setting('administratorName')." ".get_setting('administratorSurname')."\n". get_lang('Manager'). " ".get_setting('siteName')."\nT. ".get_setting('administratorTelephone')."\n" .get_lang('Email') ." : ".get_setting('emailAdministrator');
  804. $emailbody = get_lang('SendMailBody').' '.api_get_path(WEB_CODE_PATH)."work/work.php?".api_get_cidreq()."&amp;curdirpath=".$my_cur_dir_path." (" . stripslashes($title) . ")\n\n" . get_setting('administratorName') . " " . get_setting('administratorSurname') . "\n" . get_lang('Manager') . " " . get_setting('siteName') . "\n" . get_lang('Email') . " : " . get_setting('emailAdministrator');
  805. // Here we are forming one large header line
  806. // Every header must be followed by a \n except the last
  807. $emailheaders = "From: " . get_setting('administratorName') . " " . get_setting('administratorSurname') . " <" . get_setting('emailAdministrator') . ">\n";
  808. $emailheaders .= "Reply-To: " . get_setting('emailAdministrator');
  809. // Because I predefined all of my variables, this api_send_mail() function looks nice and clean hmm?
  810. @ api_send_mail($emailto, $emailsubject, $emailbody, $emailheaders);
  811. }
  812. }
  813. $message = get_lang('DocAdd');
  814. if ($uploadvisibledisabled && !$is_allowed_to_edit) {
  815. $message .= "<br />" . get_lang('_doc_unvisible') . "<br />";
  816. }
  817. //stats
  818. if (!$Id) {
  819. $Id = $insertId;
  820. }
  821. event_upload($Id);
  822. $submit_success_message = $message . "<br />\n";
  823. Display :: display_normal_message($submit_success_message, false);
  824. }
  825. //{
  826. /*=======================================
  827. Display links to upload form and tool options
  828. =======================================
  829. */
  830. display_action_links($cur_dir_path, $always_show_tool_options, $always_show_upload_form);
  831. /*=======================================
  832. Display form to upload document
  833. =======================================*/
  834. if ($is_course_member)
  835. {
  836. if ($display_upload_form || $edit)
  837. {
  838. $token = Security :: get_token(); //generate token to be used to check validity of request
  839. if ($edit)
  840. {
  841. //Get the author ID for that document from the item_property table
  842. $is_author = false;
  843. $author_sql = "SELECT * FROM $iprop_table WHERE tool = 'work' AND insert_user_id='$user_id' AND ref=" . $edit;
  844. $author_qry = api_sql_query($author_sql, __FILE__, __LINE__);
  845. if (Database :: num_rows($author_qry) == 1) {
  846. $is_author = true;
  847. }
  848. }
  849. require_once (api_get_path(LIBRARY_PATH) . 'formvalidator/FormValidator.class.php');
  850. require_once (api_get_path(LIBRARY_PATH) . 'fileDisplay.lib.php');
  851. $form = new FormValidator('form', 'POST', api_get_self() . "?curdirpath=" . Security :: remove_XSS($cur_dir_path) . "&origin=$origin", '', 'enctype="multipart/form-data"');
  852. if (!empty ($error_message))
  853. Display :: display_error_message($error_message);
  854. if ($submitGroupWorkUrl) // For user comming from group space to publish his work
  855. {
  856. $realUrl = str_replace($_configuration['root_sys'], $_configuration['root_web'], str_replace("\\", "/", realpath($submitGroupWorkUrl)));
  857. $form->addElement('hidden', 'newWorkUrl', $submitGroupWorkUrl);
  858. $text_document = & $form->addElement('text', 'document', get_lang("Document"));
  859. $defaults["document"] = '<a href="' . format_url($submitGroupWorkUrl) . '">' . $realUrl . '</a>';
  860. $text_document->freeze();
  861. }
  862. elseif ($edit && ($is_allowed_to_edit or $is_author))
  863. {
  864. $workUrl = $currentCourseRepositoryWeb . $workUrl;
  865. $form->addElement('hidden', 'id', $edit);
  866. $html = '<div class="row">
  867. <div class="label">' . get_lang("Document") . '
  868. </div>
  869. <div class="formw">
  870. <a href="' . $workUrl . '">' . $workUrl . '</a>
  871. </div>
  872. </div>';
  873. $form->addElement('html', $html);
  874. }
  875. else // else standard upload option
  876. {
  877. $form->addElement('file', 'file', get_lang('DownloadFile'), 'size="40" onchange="updateDocumentTitle(this.value)"');
  878. }
  879. $titleWork = $form->addElement('text', 'title', get_lang("TitleWork"), 'id="file_upload" style="width: 350px;"');
  880. $defaults["title"] = ($edit ? stripslashes($workTitle) : stripslashes($title));
  881. $titleAuthors = $form->addElement('text', 'authors', get_lang("Authors"), 'style="width: 350px;"');
  882. if (empty ($authors))
  883. {
  884. $authors = $_user['firstName'] . " " . $_user['lastName'];
  885. }
  886. $defaults["authors"] = ($edit ? stripslashes($workAuthor) : stripslashes($authors));
  887. $titleAuthors = $form->addElement('textarea', 'description', get_lang("Description"), 'style="width: 350px; height: 60px;"');
  888. $defaults["description"] = ($edit ? stripslashes($workDescription) : stripslashes($description));
  889. $form->addElement('hidden', 'active', 1);
  890. $form->addElement('hidden', 'accepted', 1);
  891. $form->addElement('hidden', 'sec_token', $token);
  892. // fix the Ok button when we see the tool in the learn path
  893. if ($origin== 'learnpath')
  894. {
  895. $form->addElement('html', '<div style="margin-left:137px">');
  896. $form->addElement('submit', 'submitWork', get_lang('Ok'));
  897. $form->addElement('html', '</div>');
  898. }
  899. else
  900. {
  901. $form->addElement('submit', 'submitWork', get_lang('Ok'));
  902. }
  903. if ($_POST['submitWork'] || $edit)
  904. {
  905. $form->addElement('submit', 'cancelForm', get_lang('Cancel'));
  906. }
  907. $form->add_real_progress_bar('uploadWork', 'DownloadFile');
  908. $form->setDefaults($defaults);
  909. echo '<br /><br />';
  910. $form->display();
  911. }
  912. //show them the form for the directory name
  913. if (isset ($_REQUEST['createdir']) && $is_allowed_to_edit)
  914. {
  915. //create the form that asks for the directory name
  916. $new_folder_text = '<br /><br /><form action="' . api_get_self() . '?origin='.$origin.'" method="POST">';
  917. $new_folder_text .= '<input type="hidden" name="curdirpath" value="' . Security :: remove_XSS($cur_dir_path) . '"/>';
  918. $new_folder_text .= get_lang('NewDir') . ' ';
  919. $new_folder_text .= '<input type="text" name="new_dir"/>';
  920. $new_folder_text .= '<input type="submit" name="create_dir" value="' . get_lang('Ok') . '"/>';
  921. $new_folder_text .= '</form>';
  922. //show the form
  923. echo $new_folder_text;
  924. }
  925. }
  926. else
  927. {
  928. //the user is not registered in this course
  929. echo "<p style=\"font-weight:bold\">" . get_lang("MustBeRegisteredUser") . "</p>";
  930. }
  931. /*
  932. ==============================================================================
  933. Display of tool options
  934. ==============================================================================
  935. */
  936. if ($display_tool_options)
  937. {
  938. display_tool_options($uploadvisibledisabled, $origin, $base_work_dir, $cur_dir_path, $cur_dir_path_url);
  939. }
  940. /*
  941. ==============================================================================
  942. Display list of student publications
  943. ==============================================================================
  944. */
  945. if ($cur_dir_path == '/')
  946. {
  947. $my_cur_dir_path = '';
  948. }
  949. else
  950. {
  951. $my_cur_dir_path = $cur_dir_path;
  952. }
  953. if (!$display_upload_form && !$display_tool_options) {
  954. display_student_publications_list($base_work_dir . '/' . $my_cur_dir_path, 'work/' . $my_cur_dir_path, $currentCourseRepositoryWeb, $link_target_parameter, $dateFormatLong, $origin);
  955. }
  956. /*
  957. ==============================================================================
  958. Footer
  959. ==============================================================================
  960. */
  961. if ($origin != 'learnpath')
  962. {
  963. //we are not in the learning path tool
  964. Display :: display_footer();
  965. }
  966. ?>