work.php 29 KB

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