work.php 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.work
  5. * @author Thomas, Hugues, Christophe - original version
  6. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University - ability for course admins to specify wether uploaded documents are visible or invisible by default.
  7. * @author Roan Embrechts, code refactoring and virtual course support
  8. * @author Frederic Vauthier, directories management
  9. * @author Julio Montoya <gugli100@gmail.com> BeezNest 2011 LOTS of bug fixes
  10. *
  11. * @todo refactor more code into functions, use quickforms, coding standards, ... jm
  12. */
  13. /**
  14. * STUDENT PUBLICATIONS MODULE
  15. *
  16. * Note: for a more advanced module, see the dropbox tool.
  17. * This one is easier with less options.
  18. * This tool is better used for publishing things,
  19. * sending in assignments is better in the dropbox.
  20. *
  21. * GOALS
  22. * *****
  23. * Allow student to quickly send documents immediately
  24. * visible on the course website.
  25. *
  26. * The script does 5 things:
  27. *
  28. * 1. Upload documents
  29. * 2. Give them a name
  30. * 3. Modify data about documents
  31. * 4. Delete link to documents and simultaneously remove them
  32. * 5. Show documents list to students and visitors
  33. *
  34. * On the long run, the idea is to allow sending realvideo . Which means only
  35. * establish a correspondence between RealServer Content Path and the user's
  36. * documents path.
  37. *
  38. * All documents are sent to the address /$_configuration['root_sys']/$currentCourseID/document/
  39. * where $currentCourseID is the web directory for the course and $_configuration['root_sys']
  40. * usually /var/www/html
  41. *
  42. * Modified by Patrick Cool, february 2004:
  43. * Allow course managers to specify wether newly uploaded documents should
  44. * be visible or unvisible by default
  45. * This is ideal for reviewing the uploaded documents before the document
  46. * is available for everyone.
  47. *
  48. * note: maybe the form to change the behaviour should go into the course
  49. * properties page?
  50. * note 2: maybe a new field should be created in the course table for
  51. * this behaviour.
  52. *
  53. * We now use the show_score field since this is not used.
  54. *
  55. */
  56. /* INIT SECTION */
  57. $language_file = array('exercice', 'work', 'document', 'admin');
  58. require_once '../inc/global.inc.php';
  59. // @todo why is this needed?
  60. //session
  61. if (isset ($_GET['id_session'])) {
  62. $_SESSION['id_session'] = Database::escape_string($_GET['id_session']);
  63. }
  64. isset($_SESSION['id_session']) ? $id_session = $_SESSION['id_session'] : $id_session = null;
  65. // Including necessary files
  66. require_once 'work.lib.php';
  67. require_once api_get_path(LIBRARY_PATH).'course.lib.php';
  68. require_once api_get_path(LIBRARY_PATH).'security.lib.php';
  69. require_once api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php';
  70. require_once api_get_path(LIBRARY_PATH).'document.lib.php';
  71. require_once api_get_path(LIBRARY_PATH).'groupmanager.lib.php';
  72. require_once api_get_path(LIBRARY_PATH).'usermanager.lib.php';
  73. require_once api_get_path(LIBRARY_PATH).'mail.lib.inc.php';
  74. require_once api_get_path(LIBRARY_PATH).'fckeditor/fckeditor.php';
  75. // Section (for the tabs)
  76. $this_section = SECTION_COURSES;
  77. $ctok = $_SESSION['sec_token'];
  78. $stok = Security::get_token();
  79. $work_id = isset($_GET['id']) ? intval($_GET['id']) : null;
  80. $my_folder_data = get_work_data_by_id($work_id);
  81. $curdirpath = '';
  82. $is_special = false;
  83. if (!empty($my_folder_data)) {
  84. if ($my_folder_data['filetype'] == 'folder') {
  85. $curdirpath = $my_folder_data['url'];
  86. if (!empty($my_folder_data['has_properties'])) {
  87. $is_special = true;
  88. }
  89. }
  90. }
  91. $htmlHeadXtra[] = to_javascript_work();
  92. $htmlHeadXtra[] = '<script type="text/javascript">
  93. function setFocus(){
  94. $("#work_title").focus();
  95. }
  96. $(document).ready(function () {
  97. setFocus();
  98. });
  99. </script>';
  100. // Table definitions
  101. $main_course_table = Database :: get_main_table(TABLE_MAIN_COURSE);
  102. $work_table = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
  103. $iprop_table = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  104. $TSTDPUBASG = Database :: get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT);
  105. $table_course_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  106. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  107. $table_session = Database :: get_main_table(TABLE_MAIN_SESSION);
  108. $table_session_course = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE);
  109. $table_session_course_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  110. /* Constants and variables */
  111. $tool_name = get_lang('StudentPublications');
  112. $user_id = api_get_user_id();
  113. $course_code = api_get_course_id();
  114. $session_id = api_get_session_id();
  115. $is_course_member = CourseManager::is_user_subscribed_in_real_or_linked_course($user_id, $course_code,$session_id);
  116. $is_course_member = $is_course_member || api_is_platform_admin();
  117. $currentCourseRepositorySys = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/';
  118. $currentCourseRepositoryWeb = api_get_path(WEB_COURSE_PATH) . $_course['path'] . '/';
  119. $currentUserFirstName = $_user['firstName'];
  120. $currentUserLastName = $_user['lastName'];
  121. $currentUserEmail = $_user['mail'];
  122. $authors = isset($_POST['authors']) ? Database::escape_string($_POST['authors']) : '';
  123. $delete = isset($_REQUEST['delete']) ? Database::escape_string($_REQUEST['delete']) : '';
  124. $description = isset($_REQUEST['description']) ? Database::escape_string($_REQUEST['description']) : '';
  125. $display_tool_options = isset($_REQUEST['display_tool_options']) ? $_REQUEST['display_tool_options'] : '';
  126. $display_upload_form = isset($_REQUEST['display_upload_form']) ? $_REQUEST['display_upload_form'] : '';
  127. $edit = isset($_REQUEST['edit']) ? Database::escape_string($_REQUEST['edit']) : '';
  128. $parent_id = isset($_REQUEST['parent_id']) ? Database::escape_string($_REQUEST['parent_id']) : '';
  129. $make_invisible = isset($_REQUEST['make_invisible']) ? Database::escape_string($_REQUEST['make_invisible']) : '';
  130. $make_visible = isset($_REQUEST['make_visible']) ? Database::escape_string($_REQUEST['make_visible']) : '';
  131. $origin = isset($_REQUEST['origin']) ? Security::remove_XSS($_REQUEST['origin']) : '';
  132. $submitGroupWorkUrl = isset($_REQUEST['submitGroupWorkUrl']) ? Security::remove_XSS($_REQUEST['submitGroupWorkUrl']) : '';
  133. $title = isset($_REQUEST['title']) ? Database::escape_string($_REQUEST['title']) : '';
  134. $uploadvisibledisabled = isset($_REQUEST['uploadvisibledisabled']) ? Database::escape_string($_REQUEST['uploadvisibledisabled']) : '';
  135. $id = isset($_REQUEST['id']) ? strval(intval($_REQUEST['id'])) : '';
  136. // get data for publication assignment
  137. $has_expired = false;
  138. $has_ended = false;
  139. //directories management
  140. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  141. $course_dir = $sys_course_path . $_course['path'];
  142. $base_work_dir = $course_dir . '/work';
  143. $cur_dir_path = '';
  144. if (isset($curdirpath) && $curdirpath != '') {
  145. //now using common security approach with security lib
  146. $in_course = Security :: check_abs_path($base_work_dir.$curdirpath, $base_work_dir);
  147. if (!$in_course) {
  148. $curdirpath = "/";
  149. }
  150. }
  151. if ($curdirpath == '.') {
  152. $curdirpath = '/';
  153. }
  154. /* Configuration settings */
  155. $link_target_parameter = ""; //or e.g. "target=\"_blank\"";
  156. $always_show_tool_options = false;
  157. $always_show_upload_form = false;
  158. if ($always_show_tool_options) {
  159. $display_tool_options = true;
  160. }
  161. if ($always_show_upload_form) {
  162. $display_upload_form = true;
  163. }
  164. $display_list_users_without_publication = isset($_GET['list']) && Security::remove_XSS($_GET['list']) == 'without';
  165. if (isset($_GET['action']) && $_GET['action'] == 'send_mail') {
  166. if ($_GET['sec_token'] == $_SESSION['token']) {
  167. send_reminder_users_without_publication($my_folder_data);
  168. unset($_SESSION['token']);
  169. }
  170. }
  171. api_protect_course_script(true);
  172. /* More init stuff */
  173. if (isset ($_POST['cancelForm']) && !empty ($_POST['cancelForm'])) {
  174. header('Location: ' . api_get_self() . '?origin='.$origin.'&amp;gradebook='.$gradebook);
  175. exit ();
  176. }
  177. if (!empty($_POST['submitWork']) || !empty($submitGroupWorkUrl)) {
  178. // These libraries are only used for upload purpose, so we only include them when necessary.
  179. require_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
  180. require_once api_get_path(LIBRARY_PATH).'fileDisplay.lib.php'; // need format_url function
  181. }
  182. // If the POST's size exceeds 8M (default value in php.ini) the $_POST array is emptied
  183. // If that case happens, we set $submitWork to 1 to allow displaying of the error message
  184. // The redirection with header() is needed to avoid apache to show an error page on the next request
  185. if ($_SERVER['REQUEST_METHOD'] == 'POST' && !sizeof($_POST)) {
  186. if (strstr($_SERVER['REQUEST_URI'], '?')) {
  187. header('Location: ' . $_SERVER['REQUEST_URI'] . '&submitWork=1');
  188. exit ();
  189. } else {
  190. header('Location: ' . $_SERVER['REQUEST_URI'] . '?submitWork=1');
  191. exit ();
  192. }
  193. }
  194. //toolgroup comes from group. the but of tis variable is to limit post to the group of the student
  195. //if (!api_is_course_admin()) {
  196. if (!empty ($_GET['toolgroup'])) {
  197. $toolgroup = Database::escape_string($_GET['toolgroup']);
  198. api_session_register('toolgroup');
  199. }
  200. //}
  201. //download of an completed folder
  202. if (isset($_GET['action']) && $_GET['action'] == 'downloadfolder') {
  203. require 'downloadfolder.inc.php';
  204. }
  205. /* Header */
  206. if (!empty($_GET['gradebook']) && $_GET['gradebook'] == 'view') {
  207. $_SESSION['gradebook'] = Security::remove_XSS($_GET['gradebook']);
  208. $gradebook = $_SESSION['gradebook'];
  209. } elseif (empty($_GET['gradebook'])) {
  210. unset($_SESSION['gradebook']);
  211. $gradebook = '';
  212. }
  213. if (!empty($gradebook) && $gradebook == 'view') {
  214. $interbreadcrumb[] = array ('url' => '../gradebook/' . $_SESSION['gradebook_dest'],'name' => get_lang('ToolGradebook'));
  215. }
  216. if (!empty($_SESSION['toolgroup'])) {
  217. $_clean['toolgroup'] = (int)$_SESSION['toolgroup'];
  218. $group_properties = GroupManager :: get_group_properties($_clean['toolgroup']);
  219. $interbreadcrumb[] = array ('url' => '../group/group.php', 'name' => get_lang('Groups'));
  220. $interbreadcrumb[] = array ('url' => '../group/group_space.php?gidReq='.$_SESSION['toolgroup'], 'name' => get_lang('GroupSpace').' '.$group_properties['name']);
  221. $url_dir ='';
  222. $interbreadcrumb[] = array ('url' =>'#','name' => get_lang('StudentPublications'));
  223. $dir_array = explode('/', $curdirpath);
  224. $array_len = count($dir_array);
  225. $dir_acum = '';
  226. for ($i = 0; $i < $array_len; $i++) {
  227. $url_dir = 'work.php?&curdirpath=' . $dir_acum . $dir_array[$i];
  228. $interbreadcrumb[] = array ('url' => $url_dir,'name' => $dir_array[$i]);
  229. $dir_acum .= $dir_array[$i] . '/';
  230. }
  231. if ($display_upload_form) {
  232. $interbreadcrumb[] = array ('url' => 'work.php','name' => get_lang('UploadADocument'));
  233. }
  234. if ($display_tool_options) {
  235. $interbreadcrumb[] = array (
  236. 'url' => 'work.php',
  237. 'name' => get_lang('EditToolOptions'));
  238. }
  239. if ($_GET['createdir'] == 1) {
  240. $interbreadcrumb[] = array ('url' => 'work.php','name' => get_lang('CreateAssignment'));
  241. }
  242. Display :: display_header(null);
  243. } else {
  244. if (isset($origin) && $origin != 'learnpath') {
  245. $url_dir = '';
  246. if (isset($_GET['id']) && !empty($_GET['id']) || $display_upload_form || $display_tool_options || $_GET['createdir'] == 1) {
  247. $interbreadcrumb[] = array ('url' => 'work.php', 'name' => get_lang('StudentPublications'));
  248. } else {
  249. $interbreadcrumb[] = array ('url' => '#', 'name' => get_lang('StudentPublications'));
  250. }
  251. $dir_array = explode('/', $curdirpath);
  252. $array_len = count($dir_array);
  253. $dir_acum = '';
  254. for ($i = 0; $i < $array_len; $i++) {
  255. $url_dir = 'work.php?gradebook='.$gradebook.'&amp;curdirpath=' . $dir_acum . $dir_array[$i];
  256. if (isset($_REQUEST['curdirpath']) && $_REQUEST['curdirpath'] != '.' || $display_upload_form || $display_tool_options || $_GET['createdir'] == 1) {
  257. $interbreadcrumb[] = array ('url' => $url_dir ,'name' => $dir_array[$i]);
  258. } else {
  259. $interbreadcrumb[] = array ('url' => '#','name' => $dir_array[$i]);
  260. }
  261. $dir_acum .= $dir_array[$i] . '/';
  262. }
  263. if ($display_upload_form) {
  264. $interbreadcrumb[] = array ('url' => '#', 'name' => get_lang('UploadADocument'));
  265. }
  266. if ($display_tool_options) {
  267. $interbreadcrumb[] = array ('url' => '#', 'name' => get_lang('EditToolOptions'));
  268. }
  269. if ($_GET['createdir'] == 1) {
  270. $interbreadcrumb[] = array ('url' => '#','name' => get_lang('CreateAssignment'));
  271. }
  272. Display :: display_header(null);
  273. } else {
  274. //we are in the learnpath tool
  275. require api_get_path(INCLUDE_PATH).'reduced_header.inc.php';
  276. }
  277. }
  278. //stats
  279. event_access_tool(TOOL_STUDENTPUBLICATION);
  280. $is_allowed_to_edit = api_is_allowed_to_edit(); //has to come after display_tool_view_option();
  281. /* MAIN CODE */
  282. if (!empty ($_POST['changeProperties'])) {
  283. // changing the tool setting: default visibility of an uploaded document
  284. $query = "UPDATE " . $main_course_table . " SET show_score='" . $uploadvisibledisabled . "' WHERE code='" . $_course['sysCode'] . "'";
  285. Database::query($query);
  286. // changing the tool setting: is a student allowed to delete his/her own document
  287. // database table definition
  288. $table_course_setting = Database :: get_course_table(TOOL_COURSE_SETTING);
  289. // counting the number of occurrences of this setting (if 0 => add, if 1 => update)
  290. $query = "SELECT * FROM " . $table_course_setting . " WHERE variable = 'student_delete_own_publication'";
  291. $result = Database::query($query);
  292. $number_of_setting = Database::num_rows($result);
  293. if ($number_of_setting == 1) {
  294. $query = "UPDATE " . $table_course_setting . " SET value='" . Database::escape_string($_POST['student_delete_own_publication']) . "' WHERE variable='student_delete_own_publication'";
  295. Database::query($query);
  296. } else {
  297. $query = "INSERT INTO " . $table_course_setting . " (variable, value, category) VALUES ('student_delete_own_publication','" . Database::escape_string($_POST['student_delete_own_publication']) . "','work')";
  298. Database::query($query);
  299. }
  300. $_course['show_score'] = $uploadvisibledisabled;
  301. } else {
  302. $query = "SELECT * FROM " . $main_course_table . " WHERE code=\"" . $_course['sysCode'] . "\"";
  303. $result = Database::query($query);
  304. $row = Database::fetch_array($result);
  305. $uploadvisibledisabled = $row["show_score"];
  306. }
  307. // introduction section
  308. if ($origin == 'learnpath') {
  309. echo '<div style="height:15px">&nbsp;</div>';
  310. }
  311. Display :: display_introduction_section(TOOL_STUDENTPUBLICATION);
  312. /* EDIT COMMAND WORK COMMAND */
  313. $qualification_number = 0;
  314. if (!empty($edit)) {
  315. if (api_get_session_id() != 0 && !api_is_allowed_to_session_edit(false, true)) {
  316. api_not_allowed();
  317. }
  318. $sql = "SELECT * FROM " . $work_table . " WHERE id='" . $edit . "'";
  319. $result = Database::query($sql);
  320. if (!empty($result)) {
  321. $row = Database::fetch_array($result);
  322. $workTitle = $row['title'];
  323. $workAuthor = $row['author'];
  324. $workDescription = $row['description'];
  325. $workUrl = $row['url'];
  326. $qualification_number = $row['qualification'];
  327. }
  328. }
  329. /* MAKE INVISIBLE WORK COMMAND */
  330. if (!empty($make_invisible)) {
  331. if (api_get_session_id() != 0 && !api_is_allowed_to_session_edit(false, true)) {
  332. api_not_allowed();
  333. }
  334. if (isset($make_invisible) && $make_invisible == 'all') {
  335. $sql = "ALTER TABLE " . $work_table . "
  336. CHANGE accepted accepted TINYINT(1) DEFAULT '0'";
  337. Database::query($sql);
  338. $sql = "UPDATE " . $work_table . " SET accepted = 0";
  339. Database::query($sql);
  340. Display::display_confirmation_message(get_lang('AllFilesInvisible'));
  341. } else {
  342. $sql = "UPDATE " . $work_table . " SET accepted = 0
  343. WHERE id = '" . $make_invisible . "'";
  344. Database::query($sql);
  345. Display::display_confirmation_message(get_lang('FileInvisible'));
  346. }
  347. }
  348. /* MAKE VISIBLE WORK COMMAND */
  349. if (!empty($make_visible)) {
  350. if (api_get_session_id() != 0 && !api_is_allowed_to_session_edit(false, true)) {
  351. api_not_allowed();
  352. }
  353. if (isset($make_visible) && $make_visible == 'all') {
  354. $sql = "ALTER TABLE " . $work_table . " CHANGE accepted accepted TINYINT(1) DEFAULT '1'";
  355. Database::query($sql);
  356. $sql = "UPDATE " . $work_table . " SET accepted = 1";
  357. Database::query($sql);
  358. Display::display_confirmation_message(get_lang('AllFilesVisible'));
  359. } else {
  360. $sql = "UPDATE " . $work_table . " SET accepted = 1
  361. WHERE id = '" . $make_visible . "'";
  362. Database::query($sql);
  363. Display::display_confirmation_message(get_lang('FileVisible'));
  364. }
  365. }
  366. /* Create dir command */
  367. if (!empty($_REQUEST['new_dir'])) {
  368. if (api_get_session_id() != 0 && !api_is_allowed_to_session_edit(false, true)) {
  369. api_not_allowed();
  370. }
  371. $fexpire = get_date_from_select('expires');
  372. $fend = get_date_from_select('ends');
  373. require_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
  374. $directory = Security::remove_XSS($_POST['new_dir']);
  375. $directory = replace_dangerous_char($directory);
  376. $directory = disable_dangerous_file($directory);
  377. $dir_name = $curdirpath.$directory;
  378. $created_dir = create_unexisting_work_directory($base_work_dir, $dir_name);
  379. // we insert here the directory in the table $work_table
  380. $dir_name_sql = '';
  381. if ($ctok == $_POST['sec_token']) {
  382. if (!empty($created_dir)) {
  383. if ($curdirpath == '/') {
  384. $dir_name_sql = $created_dir;
  385. } else {
  386. $dir_name_sql = '/'.$created_dir;
  387. }
  388. // Insert into agenda
  389. $agenda_id = 0;
  390. $end_date = '';
  391. if (isset($_POST['add_to_calendar']) && $_POST['add_to_calendar'] == 1) {
  392. require_once api_get_path(SYS_CODE_PATH).'calendar/agenda.inc.php';
  393. require_once api_get_path(SYS_CODE_PATH).'resourcelinker/resourcelinker.inc.php';
  394. $course = isset($course_info) ? $course_info : null;
  395. $date = time();
  396. $title = sprintf(get_lang('HandingOverOfTaskX'), $_POST['new_dir']);
  397. if (!empty($_POST['type1'])) {
  398. $end_date = get_date_from_select('expires');
  399. }
  400. $description = isset($_POST['description']) ? $_POST['description'] : '';
  401. $content = '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;curdirpath='.api_substr($dir_name_sql, 1).'" >'.$_POST['new_dir'].'</a>'.$description;
  402. $agenda_id = agenda_add_item($course, $title, $content, $date, $end_date, array('GROUP:'.$toolgroup), 0);
  403. }
  404. $sql_add_publication = "INSERT INTO " . $work_table . " SET
  405. url = '".Database::escape_string($dir_name_sql)."',
  406. title = '".Database::escape_string($_POST['new_dir'])."',
  407. description = '".Database::escape_string($_POST['description'])."',
  408. author = '',
  409. active = '0',
  410. accepted = '1',
  411. filetype = 'folder',
  412. post_group_id = '".$toolgroup."',
  413. sent_date = '".api_get_utc_datetime()."',
  414. qualification = '".(($_POST['qualification_value']!='') ? Database::escape_string($_POST['qualification_value']) : '') ."',
  415. parent_id = '',
  416. qualificator_id = '',
  417. date_of_qualification = '0000-00-00 00:00:00',
  418. weight = '".Database::escape_string($_POST['weight'])."',
  419. session_id = '".intval($id_session)."',
  420. user_id = '".$user_id."'";
  421. Database::query($sql_add_publication);
  422. // add the directory
  423. $id = Database::insert_id();
  424. //Folder created
  425. api_item_property_update($_course, 'work', $id, 'DirectoryCreated', $user_id);
  426. Display :: display_confirmation_message(get_lang('DirectoryCreated'), false);
  427. //Database :: escape_string($_REQUEST['make_visible']);
  428. //if($_POST['type1']==1)
  429. //$insert_limite
  430. // insert into student_publication_assignment
  431. //return something like this: 2008-02-45 00:00:00
  432. if (!empty($_POST['type1']) || !empty($_POST['type2'])) {
  433. $enable_calification = isset($_POST['enable_calification']) ? (int)$_POST['enable_calification'] : null;
  434. $sql_add_homework = "INSERT INTO $TSTDPUBASG SET
  435. expires_on = '".((isset($_POST['type1']) && $_POST['type1']==1) ? api_get_utc_datetime(get_date_from_select('expires')) : '0000-00-00 00:00:00'). "',
  436. ends_on = '".((isset($_POST['type2']) && $_POST['type2']==1) ? api_get_utc_datetime(get_date_from_select('ends')) : '0000-00-00 00:00:00')."',
  437. add_to_calendar = '$agenda_id',
  438. enable_qualification = '".$enable_calification."',
  439. publication_id = '".$id."'";
  440. Database::query($sql_add_homework);
  441. $sql_add_publication = "UPDATE ".$work_table." SET "."has_properties = ".Database::insert_id().", view_properties = 1 ".' where id = '.$id;
  442. Database::query($sql_add_publication);
  443. } else {
  444. $sql_add_homework = "INSERT INTO $TSTDPUBASG SET
  445. expires_on = '0000-00-00 00:00:00',
  446. ends_on = '0000-00-00 00:00:00',
  447. add_to_calendar = '$agenda_id',
  448. enable_qualification = '".(isset($_POST['enable_calification'])?(int)$_POST['enable_calification']:'')."',
  449. publication_id = '".$id."'";
  450. Database::query($sql_add_homework);
  451. $sql_add_publication = "UPDATE ".$work_table." SET "."has_properties = ".Database::insert_id().", view_properties = 0 ".' where id = '.$id;
  452. Database::query($sql_add_publication);
  453. }
  454. if (isset($_POST['make_calification']) && $_POST['make_calification'] == 1) {
  455. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/gradebookitem.class.php';
  456. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/evaluation.class.php';
  457. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/abstractlink.class.php';
  458. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/gradebook_functions.inc.php';
  459. //$resource_name = (empty($_POST['qualification_name'])) ? $_POST['new_dir'] : $_POST['qualification_name'];
  460. $resource_name = $_POST['new_dir'];
  461. add_resource_to_course_gradebook(api_get_course_id(), 3, $id, $resource_name, $_POST['weight'], $_POST['qualification_value'], $_POST['description'], time(), 1, api_get_session_id());
  462. }
  463. // end features
  464. if (api_get_course_setting('email_alert_students_on_new_homework') == 1) {
  465. send_email_on_homework_creation(api_get_course_id());
  466. }
  467. } else {
  468. Display :: display_error_message(get_lang('CannotCreateDir'));
  469. }
  470. }
  471. }
  472. /* Delete dir command */
  473. if (!empty($_REQUEST['delete_dir'])) {
  474. if (api_get_session_id() != 0 && !api_is_allowed_to_session_edit(false, true)) {
  475. api_not_allowed();
  476. }
  477. $delete_directory = $_REQUEST['delete_dir'];
  478. $id = $_REQUEST['delete2'];
  479. del_dir($base_work_dir . '/', $delete_directory, $id);
  480. Display :: display_confirmation_message(get_lang('DirDeleted') . ': '.$delete_directory);
  481. }
  482. if (!empty($_REQUEST['delete2'])) {
  483. if (api_get_session_id() != 0 && !api_is_allowed_to_session_edit(false, true)) {
  484. api_not_allowed();
  485. }
  486. $delete_2 = intval($_REQUEST['delete2']);
  487. // gets calendar_id from student_publication_assigment
  488. $sql = "SELECT add_to_calendar FROM $TSTDPUBASG WHERE publication_id ='$delete_2'";
  489. $res = Database::query($sql);
  490. $calendar_id = Database::fetch_row($res);
  491. // delete from agenda if it exists
  492. if (!empty($calendar_id[0])) {
  493. $t_agenda = Database::get_course_table(TABLE_AGENDA);
  494. $sql = "DELETE FROM $t_agenda WHERE id ='".$calendar_id[0]."'";
  495. Database::query($sql);
  496. }
  497. $sql2 = "DELETE FROM $TSTDPUBASG WHERE publication_id ='$delete_2'";
  498. $result2 = Database::query($sql2);
  499. $link_id = is_resource_in_course_gradebook(api_get_course_id(), 3 , $delete_2, api_get_session_id());
  500. if ($link_id !== false) {
  501. remove_resource_from_course_gradebook($link_id);
  502. }
  503. }
  504. /* Move file form request */
  505. if (!empty ($_REQUEST['move'])) {
  506. $folders = array();
  507. $session_id = api_get_session_id();
  508. $session_id == 0 ? $withsession = " AND session_id = 0 " : $withsession = " AND session_id='".$session_id."'";
  509. $sql = "SELECT id, url FROM $work_table WHERE url LIKE '/%' AND post_group_id = '".(empty($_SESSION['toolgroup'])?0:intval($_SESSION['toolgroup']))."'".$withsession;
  510. $res = Database::query($sql);
  511. while($folder = Database::fetch_array($res)) {
  512. $folders[$folder['id']] = substr($folder['url'], 1, strlen($folder['url']) - 1);
  513. }
  514. echo build_work_move_to_selector($folders, $curdirpath, $_REQUEST['move']);
  515. }
  516. /* Move file command */
  517. if (isset ($_POST['move_to']) && isset ($_POST['move_file'])) {
  518. require_once api_get_path(LIBRARY_PATH).'fileManage.lib.php';
  519. $move_to = $_POST['move_to'];
  520. $move_to_path = get_work_path($move_to);
  521. if ($move_to_path==-1) {
  522. $move_to_path = '/';
  523. } elseif (substr($move_to_path, -1, 1) != '/') {
  524. $move_to_path = $move_to_path .'/';
  525. }
  526. //security fix: make sure they can't move files that are not in the document table
  527. $move_file_id = $_POST['move_file'];
  528. if ($path = get_work_path($move_file_id)) {
  529. //Display::display_normal_message('We want to move '.$_POST['move_file'].' to '.$_POST['move_to']);
  530. if (move($course_dir . '/' . $path, $base_work_dir . $move_to_path)) {
  531. //update db
  532. update_work_url($move_file_id, 'work' . $move_to_path, $move_to);
  533. // update all the parents in the table item propery
  534. $list_id = get_parent_directories($move_to_path);
  535. for ($i = 0; $i < count($list_id); $i++) {
  536. api_item_property_update($_course, 'work', $list_id[$i], 'FolderUpdated', $user_id);
  537. }
  538. Display :: display_confirmation_message(get_lang('DirMv'));
  539. } else {
  540. Display :: display_error_message(get_lang('Impossible'));
  541. }
  542. } else {
  543. Display :: display_error_message(get_lang('Impossible'));
  544. }
  545. }
  546. /* COMMANDS SECTION (reserved for others - check they're authors each time) */
  547. else {
  548. $iprop_table = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  549. $user_id = api_get_user_id();
  550. /* DELETE WORK COMMAND */
  551. if ($delete) {
  552. if (api_get_session_id() != 0 && !api_is_allowed_to_session_edit(false, true)) {
  553. api_not_allowed();
  554. }
  555. if ($delete == 'all' && api_is_allowed_to_edit(null, true)) {
  556. $path = $currentCourseRepositorySys;
  557. $t_agenda = Database::get_course_table(TABLE_AGENDA);
  558. $sql = "SELECT id, url, filetype FROM ".$work_table." WHERE session_id = ".api_get_session_id().' ORDER BY url DESC'; // do not change the "order by", otherwise the work assignments will not be renamed
  559. $result = Database::query($sql);
  560. while($row = Database::fetch_array($result)) {
  561. $url = $row['url'];
  562. //Deleting works
  563. $delete_query = "DELETE FROM ".$work_table." WHERE id = ".$row['id'];
  564. Database::query($delete_query);
  565. //Deleting agenda calendar for that work assignment
  566. $sql_agenda = "SELECT add_to_calendar FROM ".$TSTDPUBASG." WHERE publication_id = ".$row['id'];
  567. $rs_agenda = Database::query($sql_agenda);
  568. while ($row_agenda = Database::fetch_array($rs_agenda)) {
  569. if (!empty($row_agenda['add_to_calendar'])) {
  570. $delete_agenda = "DELETE FROM ".$t_agenda." WHERE id = ".$row_agenda['add_to_calendar'];
  571. Database::query($delete_agenda);
  572. }
  573. }
  574. //Deleting the work assignment
  575. $delete_query = "DELETE FROM ".$TSTDPUBASG. " WHERE publication_id = ".$row['id'];
  576. Database::query($delete_query);
  577. if ($row['filetype'] == 'folder') {
  578. $url = 'work'.$url;
  579. }
  580. if (api_get_setting('permanently_remove_deleted_files') == 'true') {
  581. if (file_exists($path.$url)) {
  582. rmdirr($path.$url);
  583. }
  584. } else {
  585. if ($row['filetype'] == 'folder') {
  586. $new_file = $path.'work/DELETED_'.basename($url);
  587. } else {
  588. $new_file = $path.dirname($url).'/DELETED_'.basename($url);
  589. }
  590. if (file_exists($path.$url)) {
  591. rename($path.$url, $new_file);
  592. }
  593. }
  594. }
  595. /*
  596. $sql_agenda = "SELECT add_to_calendar FROM ".$TSTDPUBASG." WHERE add_to_calendar <> 0";
  597. $rs_agenda = Database::query($sql_agenda);
  598. $t_agenda = Database::get_course_table(TABLE_AGENDA);
  599. while ($row_agenda=Database::fetch_array($rs_agenda)) {
  600. $deleteagenda = "DELETE FROM ".$t_agenda." WHERE id='".$row_agenda['add_to_calendar']."'";
  601. $rsdeleteagenda = Database::query($deleteagenda);
  602. }
  603. $result2 = Database::query($queryString2);
  604. $result3 = Database::query($queryString3);
  605. $d = dir($path);
  606. if (api_get_setting('permanently_remove_deleted_files') == 'true') {
  607. while (false !== $entry = $d->read()) {
  608. if ($entry == '.' || $entry == '..') continue;
  609. rmdirr($path.$entry);
  610. }
  611. } else {
  612. while (false !== $entry = $d->read()) {
  613. if ($entry == '.' || $entry == '..' || substr($entry, 0, 8) == 'DELETED_') continue;
  614. $new_file = 'DELETED_'.$entry;
  615. rename($path.$entry, $path.$new_file);
  616. }
  617. }*/
  618. } else {
  619. $file_deleted = false;
  620. //Get the author ID for that document from the item_property table
  621. $author_sql = "SELECT * FROM $iprop_table WHERE tool = 'work' AND insert_user_id='$user_id' AND ref=" .Database::escape_string($delete);
  622. $author_qry = Database::query($author_sql);
  623. if ((Database :: num_rows($author_qry) == 1 AND api_get_course_setting('student_delete_own_publication') == 1) || api_is_allowed_to_edit(null,true)) {
  624. //we found the current user is the author
  625. $queryString1 = "SELECT url FROM " . $work_table . " WHERE id = '$delete'";
  626. $result1 = Database::query($queryString1);
  627. $row = Database::fetch_array($result1);
  628. if (Database::num_rows($result1) > 0) {
  629. $queryString2 = "DELETE FROM " . $work_table . " WHERE id='$delete'";
  630. $queryString3 = "DELETE FROM " . $TSTDPUBASG . " WHERE publication_id='$delete'";
  631. $result2 = Database::query($queryString2);
  632. $result3 = Database::query($queryString3);
  633. api_item_property_update($_course, 'work', $delete, 'DocumentDeleted', $user_id);
  634. $work = $row['url'];
  635. if (!empty($work)) {
  636. if (api_get_setting('permanently_remove_deleted_files') == 'true') {
  637. my_delete($currentCourseRepositorySys.'/'.$work);
  638. Display::display_confirmation_message(get_lang('TheDocumentHasBeenDeleted'));
  639. $file_deleted = true;
  640. } else {
  641. require_once api_get_path(LIBRARY_PATH).'fileManage.lib.php';
  642. $extension = pathinfo($work, PATHINFO_EXTENSION);
  643. $basename_file = basename($work, '.'.$extension);
  644. $new_dir = $work.'_DELETED_'.$delete.'.'.$extension;
  645. rename($currentCourseRepositorySys.'/'.$work, $currentCourseRepositorySys.'/'.$new_dir);
  646. Display::display_confirmation_message(get_lang('TheDocumentHasBeenDeleted'));
  647. $file_deleted = true;
  648. }
  649. }
  650. }
  651. if (!$file_deleted) {
  652. Display::display_error_message(get_lang('YouAreNotAllowedToDeleteThisDocument'));
  653. }
  654. } else {
  655. Display::display_error_message(get_lang('YouAreNotAllowedToDeleteThisDocument'));
  656. }
  657. }
  658. }
  659. /* EDIT COMMAND WORK COMMAND */
  660. if ($edit) {
  661. if (api_get_session_id() != 0 && !api_is_allowed_to_session_edit(false, true)) {
  662. api_not_allowed();
  663. }
  664. //Get the author ID for that document from the item_property table
  665. $author_sql = "SELECT * FROM $iprop_table WHERE tool = 'work' AND insert_user_id='$user_id' AND ref=" . $edit;
  666. $author_qry = Database::query($author_sql);
  667. if (Database :: num_rows($author_qry) == 1) {
  668. //we found the current user is the author
  669. $sql = "SELECT * FROM " . $work_table . " WHERE id='" . $edit . "'";
  670. $result = Database::query($sql);
  671. if ($result) {
  672. $row = Database::fetch_array($result);
  673. $workTitle = $row['title'];
  674. $workAuthor = $row['author'];
  675. $workDescription = $row['description'];
  676. $workUrl = $row['url'];
  677. $qualification_number = $row['qualification'];
  678. }
  679. }
  680. }
  681. }
  682. /* FORM SUBMIT PROCEDURE */
  683. $error_message = '';
  684. $user_id = api_get_user_id();
  685. if (isset($_POST['sec_token']) && $ctok == $_POST['sec_token']) { //check the token inserted into the form
  686. if (!empty($_POST['submitWork']) && !empty($is_course_member)) {
  687. if (!empty($_FILES['file']['size'])) {
  688. $updir = $currentCourseRepositorySys . 'work/'; //directory path to upload
  689. // Try to add an extension to the file if it has'nt one
  690. $new_file_name = add_ext_on_mime(stripslashes($_FILES['file']['name']), $_FILES['file']['type']);
  691. // Replace dangerous characters
  692. $new_file_name = replace_dangerous_char($new_file_name, 'strict');
  693. // Transform any .php file in .phps fo security
  694. $new_file_name = php2phps($new_file_name);
  695. $filesize = filesize($_FILES['file']['tmp_name']);
  696. if (empty($filesize)) {
  697. Display :: display_error_message(get_lang('UplUploadFailedSizeIsZero'));
  698. $succeed = false;
  699. } elseif (!filter_extension($new_file_name)) {
  700. //filter extension
  701. Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  702. $succeed = false;
  703. } else {
  704. if (!$title) {
  705. $title = $_FILES['file']['name'];
  706. }
  707. $authors = api_get_person_name($currentUserFirstName, $currentUserLastName);
  708. // Compose a unique file name to avoid any conflict
  709. $new_file_name = api_get_unique_id();
  710. if (isset ($_SESSION['toolgroup'])) {
  711. $post_group_id = $_SESSION['toolgroup'];
  712. } else {
  713. $post_group_id = '0';
  714. }
  715. //if we come from the group tools the groupid will be saved in $work_table
  716. @move_uploaded_file($_FILES['file']['tmp_name'], $updir.$curdirpath.'/'.$new_file_name);
  717. $url = 'work'.$curdirpath.'/'.$new_file_name;
  718. $result = Database::query("SHOW FIELDS FROM " . $work_table . " LIKE 'sent_date'");
  719. if (!Database::num_rows($result)) {
  720. Database::query("ALTER TABLE " . $work_table . " ADD sent_date DATETIME NOT NULL");
  721. }
  722. $active = '1';
  723. $sql_add_publication = "INSERT INTO " . $work_table . " SET
  724. url = '" . $url . "',
  725. title = '" . Database::escape_string($title) . "',
  726. description = '" . Database::escape_string($description) . "',
  727. author = '" . Database::escape_string($authors) . "',
  728. active = '" . $active . "',
  729. accepted = '1',
  730. post_group_id = '".$post_group_id."',
  731. sent_date = '".api_get_utc_datetime()."',
  732. parent_id = '".$work_id."' ,
  733. session_id = '".intval($id_session)."' ,
  734. user_id = '".$user_id."'";
  735. Database::query($sql_add_publication);
  736. $Id = Database::insert_id();
  737. api_item_property_update($_course, 'work', $Id, 'DocumentAdded', $user_id);
  738. $succeed = true;
  739. // update all the parents in the table item propery
  740. $list_id = get_parent_directories($my_cur_dir_path);
  741. for ($i = 0; $i < count($list_id); $i++) {
  742. api_item_property_update($_course, 'work', $list_id[$i], 'FolderUpdated', $user_id);
  743. }
  744. }
  745. } elseif ($newWorkUrl) {
  746. if (isset ($_SESSION['toolgroup'])) {
  747. $post_group_id = $_SESSION['toolgroup'];
  748. } else {
  749. $post_group_id = '0';
  750. }
  751. // SPECIAL CASE ! For a work coming from another area (i.e. groups)
  752. $url = str_replace('../../' . $_course['path'] . '/', '', $newWorkUrl);
  753. if (!$title) {
  754. $title = basename($workUrl);
  755. }
  756. $result = Database::query("SHOW FIELDS FROM " . $work_table . " LIKE 'sent_date'");
  757. if (!Database::num_rows($result)) {
  758. Database::query("ALTER TABLE " . $work_table . " ADD sent_date DATETIME NOT NULL");
  759. }
  760. $sql = "INSERT INTO " . $work_table . "
  761. SET url = '" . $url . "',
  762. title = '" . Database::escape_string($title) . "',
  763. description = '" . Database::escape_string($description) . "',
  764. author = '" . Database::escape_string($authors) . "',
  765. post_group_id = '".$post_group_id."',
  766. sent_date = '".api_get_utc_datetime()."',
  767. session_id = '".intval($id_session)."',
  768. user_id = '".$user_id."'";
  769. Database::query($sql);
  770. $insertId = Database::insert_id();
  771. api_item_property_update($_course, 'work', $insertId, 'DocumentAdded', $user_id);
  772. $succeed = true;
  773. /*// update all the parents in the table item propery
  774. $list_id=get_parent_directories($my_cur_dir_path);
  775. for ($i = 0; $i < count($list_id); $i++) {
  776. api_item_property_update($_course, 'work', $list_id[$i], 'FolderUpdated', $user_id);
  777. }*/
  778. } else {
  779. /*
  780. * SPECIAL CASE ! For a work edited
  781. */
  782. //Get the author ID for that document from the item_property table
  783. $is_author = false;
  784. $item_to_edit_id = intval($_POST['item_to_edit']);
  785. $item_to_edit_data = api_get_item_property_info(api_get_course_int_id(), 'work', $item_to_edit_id);
  786. if ($is_allowed_to_edit) {
  787. $is_author = true;
  788. } else {
  789. if ($item_to_edit_data['insert_user_id'] == api_get_user_id()) {
  790. $is_author = true;
  791. }
  792. }
  793. if ($is_author) {
  794. if (!$title) {
  795. $title = basename($newWorkUrl);
  796. }
  797. if ($is_allowed_to_edit && ($_POST['qualification'] !='' )) {
  798. $add_to_update = ', qualificator_id ='."'".api_get_user_id()."',";
  799. $add_to_update .= ' qualification ='."'".Database::escape_string($_POST['qualification'])."',";
  800. $add_to_update .= ' date_of_qualification ='."'".api_get_utc_datetime()."'";
  801. }
  802. if ((int)$_POST['qualification'] > (int)$_POST['qualification_over']) {
  803. Display::display_error_message(get_lang('QualificationMustNotBeMoreThanQualificationOver'));
  804. } else {
  805. $sql = "UPDATE " . $work_table . "
  806. SET title = '" . Database::escape_string($title) . "',
  807. description = '" . Database::escape_string($description) . "'
  808. ".$add_to_update."
  809. WHERE id = $item_to_edit_id";
  810. Database::query($sql);
  811. }
  812. api_item_property_update($_course, 'work', $item_to_edit_id, 'DocumentUpdated', $user_id);
  813. $succeed = true;
  814. } else {
  815. $error_message = get_lang('IsNotPosibleSaveTheDocument');
  816. }
  817. }
  818. }
  819. }
  820. if (!empty($_POST['submitWork']) && !empty($succeed) && !$id) {
  821. //last value is to check this is not "just" an edit
  822. //YW Tis part serve to send a e-mail to the tutors when a new file is sent
  823. $send = api_get_course_setting('email_alert_manager_on_new_doc');
  824. if ($send > 0) {
  825. // Lets predefine some variables. Be sure to change the from address!
  826. $emailto = array ();
  827. if (empty ($id_session)) {
  828. $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';
  829. $res_resp = Database::query($sql_resp);
  830. while ($row_email = Database :: fetch_array($res_resp)) {
  831. if (!empty ($row_email['myemail'])) {
  832. $emailto[$row_email['myemail']] = $row_email['myemail'];
  833. }
  834. }
  835. } else {
  836. // coachs of the session
  837. $sql_resp = 'SELECT user.email as myemail
  838. FROM ' . $table_session . ' session
  839. INNER JOIN ' . $table_user . ' user
  840. ON user.user_id = session.id_coach
  841. WHERE session.id = ' . intval($id_session);
  842. $res_resp = Database::query($sql_resp);
  843. while ($row_email = Database :: fetch_array($res_resp)) {
  844. if (!empty ($row_email['myemail'])) {
  845. $emailto[$row_email['myemail']] = $row_email['myemail'];
  846. }
  847. }
  848. //coach of the course
  849. $sql_resp = 'SELECT user.email as myemail
  850. FROM ' . $table_session_course_user . ' scu
  851. INNER JOIN ' . $table_user . ' user
  852. ON user.user_id = scu.id_user AND scu.status=2
  853. WHERE scu.id_session = ' . intval($id_session);
  854. $res_resp = Database::query($sql_resp);
  855. while ($row_email = Database :: fetch_array($res_resp)) {
  856. if (!empty ($row_email['myemail'])) {
  857. $emailto[$row_email['myemail']] = $row_email['myemail'];
  858. }
  859. }
  860. }
  861. if (count($emailto) > 0) {
  862. $emailto = implode(',', $emailto);
  863. $emailfromaddr = api_get_setting('emailAdministrator');
  864. $emailfromname = api_get_setting('siteName');
  865. $emailsubject = "[" . api_get_setting('siteName') . "] ";
  866. $sender_name = api_get_setting('administratorName').' '.api_get_setting('administratorSurname');
  867. $email_admin = api_get_setting('emailAdministrator');
  868. // The body can be as long as you wish, and any combination of text and variables
  869. $emailbody = get_lang('SendMailBody')."\n".get_lang('CourseName')." : ".$_course['name']."\n";
  870. $emailbody .= get_lang('WorkName')." : ".substr($my_cur_dir_path, 0, -1)."\n";
  871. $emailbody .= get_lang('UserName')." : ".$currentUserFirstName .' '.$currentUserLastName ."\n";
  872. $emailbody .= get_lang('DateSent')." : ".api_format_date(api_get_local_time())."\n";
  873. $emailbody .= get_lang('FileName')." : ".$title."\n\n".get_lang('DownloadLink')."\n";
  874. $emailbody .= api_get_path(WEB_CODE_PATH)."work/work.php?".api_get_cidreq()."&amp;curdirpath=".$my_cur_dir_path."\n\n" . api_get_setting('administratorName') . " " . api_get_setting('administratorSurname') . "\n" . get_lang('Manager') . " " . api_get_setting('siteName') . "\n" . get_lang('Email') . " : " . api_get_setting('emailAdministrator');
  875. // Here we are forming one large header line
  876. // Every header must be followed by a \n except the last
  877. @api_mail('', $emailto, $emailsubject, $emailbody, $sender_name,$email_admin);
  878. $emailbody_user = get_lang('Dear')." ".$currentUserFirstName .' '.$currentUserLastName ."\n";
  879. $emailbody_user .= get_lang('MessageConfirmSendingOfTask')."\n".get_lang('CourseName')." : ".$_course['name']."\n";
  880. $emailbody_user .= get_lang('WorkName')." : ".substr($my_cur_dir_path, 0, -1)."\n";
  881. $emailbody_user .= get_lang('DateSent')." : ".api_format_date(api_get_local_time())."\n";
  882. $emailbody_user .= get_lang('FileName')." : ".$title."\n\n".api_get_setting('administratorName')." ".api_get_setting('administratorSurname') . "\n" . get_lang('Manager') . " " . api_get_setting('siteName') . "\n" . get_lang('Email') . " : " . api_get_setting('emailAdministrator');;
  883. //Mail to user
  884. @api_mail('', $currentUserEmail, $emailsubject, $emailbody_user, $sender_name,$email_admin);
  885. }
  886. }
  887. $message = get_lang('DocAdd');
  888. if ($uploadvisibledisabled && !$is_allowed_to_edit) {
  889. //$message .= "<br />" . get_lang('_doc_unvisible') . "<br />";
  890. }
  891. //stats
  892. if (!$Id) {
  893. $Id = $insertId;
  894. }
  895. event_upload($Id);
  896. $submit_success_message = $message . "<br />\n";
  897. Display :: display_confirmation_message($submit_success_message, false);
  898. }
  899. /* Display links to upload form and tool options */
  900. if ($is_special) {
  901. $homework = get_work_assignment_by_id($my_folder_data['id']);
  902. $has_expired = $has_ended = false;
  903. $has_expiry_date = false;
  904. if ($homework['expires_on'] != '0000-00-00 00:00:00' || $homework['ends_on'] != '0000-00-00 00:00:00') {
  905. $time_now = time();
  906. if (!empty($homework['expires_on']) && $homework['expires_on'] != '0000-00-00 00:00:00') {
  907. $time_expires = api_strtotime($homework['expires_on']);
  908. $difference = $time_expires - $time_now;
  909. if ($difference < 0) {
  910. $has_expired = true;
  911. $has_expiry_date = true;
  912. }
  913. }
  914. if (!empty($homework['ends_on']) && $homework['ends_on'] != '0000-00-00 00:00:00') {
  915. $time_ends = api_strtotime($homework['ends_on']);
  916. $difference2 = $time_ends - $time_now;
  917. if ($difference2 < 0) {
  918. $has_ended = true;
  919. }
  920. }
  921. if (empty($homework['expires_on']) || $homework['expires_on'] == '0000-00-00 00:00:00') {
  922. $has_expiry_date = false;
  923. }
  924. $ends_on = api_convert_and_format_date($homework['ends_on']);
  925. $expires_on = api_convert_and_format_date($homework['expires_on']);
  926. if ($has_ended) {
  927. display_action_links($work_id, $curdirpath, $always_show_tool_options, true);
  928. Display :: display_error_message(get_lang('EndDateAlreadyPassed').' '.$ends_on);
  929. } elseif ($has_expired) {
  930. display_action_links($work_id,$curdirpath, $always_show_tool_options, $always_show_upload_form);
  931. Display :: display_warning_message(get_lang('ExpiryDateAlreadyPassed').' '.$expires_on);
  932. } else {
  933. display_action_links($work_id,$curdirpath, $always_show_tool_options, $always_show_upload_form);
  934. if ($has_expiry_date) {
  935. Display :: display_normal_message(get_lang('ExpiryDateToSendWorkIs').' '.$expires_on);
  936. }
  937. }
  938. } else {
  939. display_action_links($work_id,$curdirpath, $always_show_tool_options, $always_show_upload_form);
  940. }
  941. } else {
  942. display_action_links($work_id, $curdirpath, $always_show_tool_options, $always_show_upload_form);
  943. }
  944. /* Display form to upload document */
  945. if ($is_course_member) {
  946. if (($display_upload_form || $edit) && !$has_ended) {
  947. if (api_get_session_id() != 0 && !api_is_allowed_to_session_edit(false, true)) {
  948. api_not_allowed();
  949. }
  950. if ($edit) {
  951. //Get the author ID for that document from the item_property table
  952. $is_author = false;
  953. $author_sql = "SELECT * FROM $iprop_table WHERE tool = 'work' AND insert_user_id='$user_id' AND ref=" . $edit;
  954. $author_qry = Database::query($author_sql);
  955. if (Database :: num_rows($author_qry) == 1) {
  956. $is_author = true;
  957. }
  958. }
  959. //require_once api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php';
  960. require_once api_get_path(LIBRARY_PATH).'fileDisplay.lib.php';
  961. $form = new FormValidator('form', 'POST', api_get_self() . "?id=".$work_id."curdirpath=" . rtrim(Security :: remove_XSS($curdirpath),'/') . "&gradebook=".Security::remove_XSS($_GET['gradebook'])."&origin=$origin", '', 'enctype="multipart/form-data"');
  962. // form title
  963. if ($edit) {
  964. $form_title = get_lang('EditMedia');
  965. } else {
  966. $form_title = get_lang('UploadADocument');
  967. }
  968. $form->addElement('header', '', $form_title);
  969. if (!empty ($error_message)) {
  970. Display :: display_error_message($error_message);
  971. }
  972. $show_progress_bar = false;
  973. if ($submitGroupWorkUrl) {
  974. // For user comming from group space to publish his work
  975. $realUrl = str_replace($_configuration['root_sys'], api_get_path(WEB_PATH), str_replace("\\", '/', realpath($submitGroupWorkUrl)));
  976. $form->addElement('hidden', 'newWorkUrl', $submitGroupWorkUrl);
  977. $text_document = & $form->addElement('text', 'document', get_lang('Document'));
  978. $defaults['document'] = '<a href="' . format_url($submitGroupWorkUrl) . '">' . $realUrl . '</a>';
  979. $text_document->freeze();
  980. } elseif ($edit && ($is_allowed_to_edit or $is_author)) {
  981. $workUrl = $currentCourseRepositoryWeb . $workUrl;
  982. //$form->addElement('hidden', 'id', $edit);
  983. $html = '<div class="row">
  984. <div class="label">' . get_lang("Document") . '
  985. </div>
  986. <div class="formw">
  987. <a href="' . $workUrl . '">' . get_lang("ClickHereToDownloadTheFile") . '</a>
  988. </div>
  989. </div>';
  990. $form->addElement('html', $html);
  991. } else {
  992. // else standard upload option
  993. $form->addElement('file', 'file', get_lang('UploadADocument'), 'size="40" onchange="updateDocumentTitle(this.value)"');
  994. $show_progress_bar = true;
  995. }
  996. $form->addElement('hidden', 'id', $work_id);
  997. $titleWork = $form->addElement('text', 'title', get_lang('TitleWork'), 'id="file_upload" style="width: 350px;"');
  998. $defaults['title'] = $edit ? stripslashes($workTitle) : stripslashes($title);
  999. //Removed to avoid incoherences
  1000. //$titleAuthors = $form->addElement('text', 'authors', get_lang("Authors"), 'style="width: 350px;"');
  1001. //if (empty ($authors)) {
  1002. $authors = api_get_person_name($_user['firstName'], $_user['lastName']);
  1003. //}
  1004. //$defaults["authors"] = ($edit ? stripslashes($workAuthor) : stripslashes($authors));
  1005. $titleAuthors = $form->addElement('textarea', 'description', get_lang("Description"), 'style="width: 350px; height: 60px;"');
  1006. $defaults["description"] = ($edit ? stripslashes($workDescription) : stripslashes($description));
  1007. if ($is_allowed_to_edit && !empty($edit)) {
  1008. // Get qualification from parent_id that'll allow the validation qualification over
  1009. $sql = "SELECT qualification FROM $work_table WHERE id='$work_id'";
  1010. $result = Database::query($sql);
  1011. $row = Database::fetch_array($result);
  1012. $qualification_over = $row['qualification'];
  1013. $form->addElement('text', 'qualification', get_lang('Qualification'), 'size="10"');
  1014. $form->addElement('html', '<div class="row"><div class="formw">'.get_lang('QualificationNumeric').'&nbsp;:&nbsp;'.$qualification_over.'</div></div>');
  1015. $form->addElement('hidden', 'qualification_over', $qualification_over);
  1016. }
  1017. $defaults['qualification'] = $qualification_number;//($edit ? stripslashes($qualification_number) : stripslashes($qualification_number));
  1018. $form->addElement('hidden', 'active', 1);
  1019. $form->addElement('hidden', 'accepted', 1);
  1020. $form->addElement('hidden', 'item_to_edit', $edit);
  1021. $form->addElement('hidden', 'sec_token', $stok);
  1022. if (isset($_GET['edit'])) {
  1023. $text = get_lang('UpdateWork');
  1024. $class = 'save';
  1025. } else {
  1026. $text = get_lang('SendWork');
  1027. $class = 'upload';
  1028. }
  1029. // fix the Ok button when we see the tool in the learn path
  1030. if ($origin == 'learnpath') {
  1031. $form->addElement('html', '<div style="margin-left:137px">');
  1032. $form->addElement('style_submit_button', 'submitWork', $text, array('class="'.$class.'"', 'value="submitWork"'));
  1033. $form->addElement('html', '</div>');
  1034. } else {
  1035. //$form->addElement('submit','submitWork', get_lang('SendFile'));
  1036. $form->addElement('style_submit_button', 'submitWork', $text, array('class="'.$class.'"', 'value="submitWork"'));
  1037. }
  1038. if (!empty($_POST['submitWork']) || $edit) {
  1039. $form->addElement('style_submit_button', 'cancelForm', get_lang('Cancel'), 'class="cancel"');
  1040. }
  1041. if ($show_progress_bar) {
  1042. $form->add_real_progress_bar('uploadWork', 'file');
  1043. }
  1044. $form->setDefaults($defaults);
  1045. //$form->addRule('file', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
  1046. $form->display();
  1047. }
  1048. //show them the form for the directory name
  1049. if (isset($_REQUEST['createdir']) && $is_allowed_to_edit) {
  1050. //create the form that asks for the directory name
  1051. $new_folder_text = '<form name="form1" method="POST">';
  1052. $new_folder_text .= '<div class="row"><div class="form_header">'.get_lang('CreateAssignment').'</div></div>';
  1053. $new_folder_text .= '<input type="hidden" name="curdirpath" value="' . Security :: remove_XSS($curdirpath) . '"/>';
  1054. $new_folder_text .= '<input type="hidden" name="sec_token" value="'.$stok.'" />';
  1055. $new_folder_text .= '<div class="row">
  1056. <div class="label">
  1057. <span class="form_required">*</span> '.get_lang('AssignmentName').'
  1058. </div>
  1059. <div class="formw">
  1060. <div id="msg_error1" style="display:none;color:red"></div>
  1061. <input type="text" id="work_title" name="new_dir" onfocus="document.getElementById(\'msg_error1\').style.display=\'none\';"/>
  1062. </div>
  1063. </div>';
  1064. //new additional fields inside the "if condition" just to agroup
  1065. $new_folder_text .= '<div class="row">
  1066. <div class="label">
  1067. '.get_lang('Description').'
  1068. </div>
  1069. <div class="formw">';
  1070. $oFCKeditor = new FCKeditor('description') ;
  1071. $oFCKeditor->ToolbarSet = 'work';
  1072. $oFCKeditor->Width = '100%';
  1073. $oFCKeditor->Height = '200';
  1074. $oFCKeditor->Value = isset($message) ? $message : null;
  1075. $return = $oFCKeditor->CreateHtml();
  1076. $new_folder_text .= $return;
  1077. $new_folder_text .= '</div>
  1078. </div>';
  1079. // Advanced parameters
  1080. $addtext = '<div id="options" style="display: none;">';
  1081. $addtext .= '<div style="padding:10px">';
  1082. $addtext .= '<b>'.get_lang('QualificationOfAssignment').'</b>';
  1083. $addtext .= '<table cellspacing="0" cellpading="0" border="0"><tr>';
  1084. $addtext .= '<td colspan="2">&nbsp;&nbsp;'.get_lang('QualificationNumeric').'&nbsp;';
  1085. $addtext .= '<input type="text" name="qualification_value" value="" size="5"/></td><tr><td colspan="2">';
  1086. $addtext .= '<input type="checkbox" value="1" name="make_calification" onclick="javascript: if(this.checked){document.getElementById(\'option1\').style.display=\'block\';}else{document.getElementById(\'option1\').style.display=\'none\';}"/>'.get_lang('MakeQualifiable').'</td></tr><tr>';
  1087. $addtext .= '<td colspan="2"><div id="option1" style="display:none">';
  1088. $addtext .= '<div id="msg_error_weight" style="display:none;color:red"></div>';
  1089. $addtext .= '&nbsp;&nbsp;'.get_lang('WeightInTheGradebook').'&nbsp;';
  1090. $addtext .= '<input type="text" name="weight" value="" size="5" onfocus="document.getElementById(\'msg_error_weight\').style.display=\'none\';"/></div></td></tr>';
  1091. $addtext .= '</tr></table>';
  1092. $addtext .= '<br />';
  1093. $addtext .= '<b>'.get_lang('DatesAvailables').'</b><br />';
  1094. $addtext .= '<input type="checkbox" value="1" name="type1" onclick="javascript: if(this.checked){document.getElementById(\'option2\').style.display=\'block\';}else{document.getElementById(\'option2\').style.display=\'none\';}"/>'.get_lang('EnableExpiryDate').'';
  1095. $addtext .= '&nbsp;&nbsp;&nbsp;<span id="msg_error2" style="display:none;color:red"></span>';
  1096. $addtext .= '&nbsp;&nbsp;&nbsp;<span id="msg_error3" style="display:none;color:red"></span>';
  1097. $addtext .= '<div id="option2" style="padding:4px;display:none">&nbsp;&nbsp;';
  1098. $addtext .= draw_date_picker('expires').'</div>';
  1099. $addtext .= '<br /><input type="checkbox" value="1" name="type2" onclick="javascript: if(this.checked){document.getElementById(\'option3\').style.display=\'block\';}else{document.getElementById(\'option3\').style.display=\'none\';}"/>'.get_lang('EnableEndDate').'';
  1100. $addtext .= '<div id="option3" style="padding:4px;display:none">';
  1101. $addtext .= '&nbsp;&nbsp;&nbsp;<div id="msg_error4" style="display:none;color:red"></div>';
  1102. $addtext .= draw_date_picker('ends').'<br />';
  1103. $addtext .= '</div>';
  1104. $addtext .= '<br /><br /><b>'.get_lang('Agenda').'</b><br />';
  1105. $addtext .= '&nbsp;&nbsp;'.make_checkbox('add_to_calendar').get_lang('AddToCalendar').'</div>';
  1106. $addtext .= '</div>';
  1107. $new_folder_text .= '<div class="row">
  1108. <div class="label">
  1109. </div>
  1110. <div class="formw"><a href="javascript: void(0);" onclick="javascript: return plus();"><span id="plus">'.Display::return_icon('div_show.gif',get_lang('AdvancedParameters'), array('style' => 'vertical-align:center')).' '.get_lang('AdvancedParameters').'</span></a><br />
  1111. '.$addtext.'
  1112. </div>
  1113. </div>';
  1114. $new_folder_text .= '<div class="row">
  1115. <div class="label">
  1116. </div>
  1117. <div class="formw">
  1118. <button type="button" class="add" name="create_dir" onClick="javascript: validate();" value="' . addslashes(get_lang('CreateDirectory')) . '"/>' . addslashes(get_lang('ButtonCreateAssignment')) . '</button>
  1119. </div>
  1120. </div>';
  1121. $new_folder_text .= '</form>';
  1122. //show the form
  1123. echo $new_folder_text;
  1124. }
  1125. } else {
  1126. //the user is not registered in this course
  1127. echo '<p style="font-weight:bold">' . get_lang('MustBeRegisteredUser') . '</p>';
  1128. }
  1129. /* Display of tool options */
  1130. if ($display_tool_options) {
  1131. display_tool_options($uploadvisibledisabled, $origin, $base_work_dir, $curdirpath, $cur_dir_path_url);
  1132. }
  1133. /* Display list of student publications */
  1134. if ($curdirpath == '/') {
  1135. $my_cur_dir_path = '';
  1136. } else {
  1137. $my_cur_dir_path = $curdirpath;
  1138. }
  1139. //If no upload form is showed and if NO tooloptions
  1140. if (!$display_upload_form && !$display_tool_options) {
  1141. $add_query = '';
  1142. //Getting if I'm a teacher
  1143. $sql = "SELECT user.firstname, user.lastname FROM $table_user user, $table_course_user course_user
  1144. WHERE course_user.user_id=user.user_id AND course_user.course_code='".api_get_course_id()."' AND course_user.status='1'";
  1145. $res = Database::query($sql);
  1146. $admin_course = '';
  1147. while ($row = Database::fetch_row($res)) {
  1148. $admin_course .='\''.api_get_person_name($row[0], $row[1]).'\',';
  1149. }
  1150. //If I'm student & I'm in a special work and check the work setting: "New documents are visible for all users"
  1151. if (!$is_allowed_to_edit && $is_special && $uploadvisibledisabled == 1) {
  1152. $add_query = ' AND author IN('.$admin_course.'\''.api_get_person_name($_user['firstName'], $_user['lastName']).'\')';
  1153. }
  1154. if ($is_allowed_to_edit && $is_special) {
  1155. if (!empty($_REQUEST['filter'])) {
  1156. switch($_REQUEST['filter']) {
  1157. case 1:
  1158. $add_query = ' AND qualification = '."''";
  1159. break;
  1160. case 2:
  1161. $add_query = ' AND qualification != '."''";
  1162. break;
  1163. case 3:
  1164. $add_query = ' AND sent_date < '."'".$homework['expires_on']."'";
  1165. break;
  1166. default:
  1167. $add_query = '';
  1168. }
  1169. }
  1170. $cidreq = isset($_GET['cidreq']) ? Security::remove_XSS($_GET['cidreq']) : '';
  1171. $curdirpath = isset($_REQUEST['curdirpath']) ? Security::remove_XSS($_REQUEST['curdirpath']) : '';
  1172. $filter = isset($_REQUEST['filter']) ? (int)$_REQUEST['filter'] : '';
  1173. if ($origin != 'learnpath') {
  1174. $form_filter = '<form method="post" action="'.api_get_self().'?cidReq='.$cidreq.'&id='.$work_id.'&curdirpath='.$curdirpath.'&gradebook='.$gradebook.'">';
  1175. $form_filter .= make_select('filter', array(0 => get_lang('SelectAFilter'), 1 => get_lang('FilterByNotRevised'), 2 => get_lang('FilterByRevised'), 3 => get_lang('FilterByNotExpired')), $filter).'&nbsp&nbsp';
  1176. $form_filter .= '<button type="submit" class="save" value="'.get_lang('FilterAssignments').'">'.get_lang('FilterAssignments').'</button></form>';
  1177. echo $form_filter;
  1178. }
  1179. }
  1180. if (!empty($my_folder_data['description'])) {
  1181. echo '<p><div><strong>'.get_lang('Description').':</strong><p>'.Security::remove_XSS($my_folder_data['description'], STUDENT).'</p></div></p>';
  1182. }
  1183. if ($display_list_users_without_publication) {
  1184. display_list_users_without_publication($my_folder_data['id']);
  1185. } else {
  1186. //$base_work_dir . '/' . $my_cur_dir_path, 'work/' . $my_cur_dir_path,
  1187. display_student_publications_list($work_id, $link_target_parameter, $dateFormatLong, $origin, $add_query);
  1188. }
  1189. }
  1190. if ($origin != 'learnpath') {
  1191. //we are not in the learning path tool
  1192. Display :: display_footer();
  1193. }