work.php 57 KB

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