work.php 32 KB

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