work.lib.php 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681
  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. * @version $Id: work.lib.php 22357 2009-07-24 17:44:17Z juliomontoya $
  10. * @todo this lib should be convert in a static class and moved to main/inc/lib
  11. */
  12. /**
  13. * Initialization
  14. */
  15. require_once api_get_path(SYS_CODE_PATH).'document/document.inc.php';
  16. require_once api_get_path(LIBRARY_PATH).'fileDisplay.lib.php';
  17. /**
  18. * Displays action links (for admins, authorized groups members and authorized students)
  19. * @param string Current dir
  20. * @param integer Whether to show tool options
  21. * @param integer Whether to show upload form option
  22. * @return void
  23. */
  24. function display_action_links($cur_dir_path, $always_show_tool_options, $always_show_upload_form) {
  25. global $gradebook;
  26. $display_output = '';
  27. $origin = isset($_GET['origin']) ? Security::remove_XSS($_GET['origin']) : '';
  28. $curdirpath = isset($_GET['curdirpath']) ? Security::remove_XSS($_GET['curdirpath']) : empty($curdirpath);
  29. if (strlen($cur_dir_path) > 0 && $cur_dir_path != '/') {
  30. $parent_dir = dirname($cur_dir_path);
  31. $display_output .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&origin='.$origin.'&gradebook='.$gradebook.'&curdirpath='.$parent_dir.'">'.Display::return_icon('back.png', get_lang('BackToWorksList'),'','32').'</a>';
  32. } else {
  33. if ($_GET['display_tool_options'] == 'true' OR $_GET['display_upload_form'] == 'true') {
  34. if ($origin != 'learnpath') {
  35. //$display_output .= '<a href="work.php?gradebook='.$gradebook.'">'.Display::return_icon('back.png', get_lang('BackToWorksList'),'','32').'</a>';
  36. }
  37. }
  38. }
  39. if (!$always_show_tool_options && api_is_allowed_to_edit(null, true) && $origin != 'learnpath') {
  40. if (empty($curdirpath)) {
  41. //$display_output .= '<a href="work.php?">'.Display::return_icon('back.png', get_lang('BackToWorksList'),'','32').'</a>';
  42. }
  43. // Create dir
  44. if ($cur_dir_path == '/') {
  45. $display_output .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;toolgroup='.Security::remove_XSS($_GET['toolgroup']).'&amp;createdir=1&origin='.$origin.'&gradebook='.$gradebook.'">'.Display::return_icon('new_work.png', get_lang('CreateAssignment'),'','32').'</a>';
  46. }
  47. if (empty($curdirpath) || $curdirpath == '.') {
  48. // Options
  49. $display_output .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;display_tool_options=true&amp;origin='.$origin.'&amp;gradebook='.$gradebook.'">'.Display::return_icon('settings.png', get_lang('EditToolOptions'),'','32').'</a>';
  50. }
  51. }
  52. if (!$always_show_upload_form && api_is_allowed_to_session_edit(false, true) && (isset($_GET['curdirpath']) && (!empty($_GET['curdirpath']) && $_GET['curdirpath'] != '.') )) {
  53. $display_output .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;curdirpath='.$cur_dir_path.'&amp;display_upload_form=true&amp;origin='.$origin.'&amp;gradebook='.$gradebook.'">'.Display::return_icon('upload_file.png', get_lang('UploadADocument'),'','32').'</a>';
  54. }
  55. if (api_is_allowed_to_edit(null, true) && $origin != 'learnpath' && api_is_allowed_to_session_edit(false, true)) {
  56. // Delete all files
  57. if (api_get_setting('permanently_remove_deleted_files') == 'true'){
  58. $message = get_lang('ConfirmYourChoiceDeleteAllfiles');
  59. } else {
  60. $message = get_lang('ConfirmYourChoice');
  61. }
  62. if (empty($curdirpath) or $curdirpath != '.') {
  63. //$display_output .= '<a href="#">'.Display::return_icon('delete_na.png', get_lang('Delete'),'','32').'</a>';
  64. } else {
  65. /*$display_output .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;curdirpath='.$cur_dir_path.'&amp;origin='.$origin.'&amp;gradebook='.$gradebook.'&amp;delete=all" onclick="javascript: if(!confirm(\''.addslashes(api_htmlentities($message, ENT_QUOTES)).'\')) return false;">'.
  66. Display::return_icon('delete.png', get_lang('Delete'),'','32').'</a>';*/
  67. }
  68. // make all files visible or invisible
  69. $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  70. $sql_query = "SHOW COLUMNS FROM ".$work_table." LIKE 'accepted'";
  71. $sql_result = Database::query($sql_query);
  72. if ($sql_result) {
  73. $columnStatus = Database::fetch_array($sql_result);
  74. /*
  75. if (empty($curdirpath) || $curdirpath == '.') {
  76. if ($columnStatus['Default'] == 1) {
  77. $display_output .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;origin='.$origin.'&amp;gradebook='.$gradebook.'&amp;make_invisible=all&amp;curdirpath='.$cur_dir_path.'">'.
  78. Display::return_icon('visible.png', get_lang('MakeAllPapersInvisible'),'','32')."</a>\n";
  79. } else {
  80. $display_output .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;origin='.$origin.'&amp;gradebook='.$gradebook.'&amp;make_visible=all&amp;curdirpath='.$cur_dir_path.'">'.
  81. Display::return_icon('invisible.png', get_lang('MakeAllPapersVisible'),'','32')."</a>\n";
  82. }
  83. }*/
  84. }
  85. }
  86. if (api_is_allowed_to_edit(null, true)) {
  87. global $publication;
  88. if (empty($curdirpath) or $curdirpath != '.' or $cur_dir_path != '/') {
  89. if (empty($_GET['list']) or Security::remove_XSS($_GET['list']) == 'with') {
  90. $display_output .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;curdirpath='.$cur_dir_path.'&amp;origin='.$origin.'&amp;gradebook='.$gradebook.'&amp;list=without">'.
  91. Display::return_icon('exercice_uncheck.png', get_lang('ViewUsersWithoutTask'),'','32')."</a>\n";
  92. } else {
  93. $display_output .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;curdirpath='.$cur_dir_path.'&amp;origin='.$origin.'&amp;gradebook='.$gradebook.'&amp;list=with">'.
  94. Display::return_icon('exercice_check.png', get_lang('ViewUsersWithTask'),'','32')."</a>\n";
  95. $_SESSION['token'] = time();
  96. $display_output .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;curdirpath='.$cur_dir_path.'&amp;origin='.$origin.'&amp;gradebook='.$gradebook.'&amp;list=without&amp;action=send_mail&amp;sec_token='.$_SESSION['token'].'">'.
  97. Display::return_icon('mail_send.png', get_lang('ReminderMessage'),'','32')."</a>";
  98. }
  99. }
  100. }
  101. if ($display_output != '') {
  102. echo '<div class="actions">';
  103. echo $display_output;
  104. echo '</div>';
  105. }
  106. }
  107. /**
  108. * Displays all options for this tool.
  109. * These are
  110. * - make all files visible / invisible
  111. * - set the default visibility of uploaded files
  112. *
  113. * @param $uploadvisibledisabled
  114. * @param $origin
  115. * @param $base_work_dir Base working directory (up to '/work')
  116. * @param $cur_dir_path Current subdirectory of 'work/'
  117. * @param $cur_dir_path_url Current subdirectory of 'work/', url-encoded
  118. */
  119. function display_tool_options($uploadvisibledisabled, $origin, $base_work_dir, $cur_dir_path, $cur_dir_path_url) {
  120. global $group_properties, $gradebook;
  121. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  122. $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  123. if (!$is_allowed_to_edit) {
  124. return;
  125. }
  126. echo '<form method="post" action="'.api_get_self().'?origin='.$origin.'&gradebook='.$gradebook.'&display_tool_options=true">';
  127. echo '<div class="row"><div class="form_header">'.get_lang('EditToolOptions').'</div></div>';
  128. display_default_visibility_form($uploadvisibledisabled);
  129. display_studentsdelete_form();
  130. echo '<div class="row">
  131. <div class="formw">
  132. <button type="submit" class="save" name="changeProperties" value="'.get_lang('Ok').'">'.get_lang('Ok').'</button>
  133. </div>
  134. </div>';
  135. echo '</form>';
  136. }
  137. /**
  138. * Displays the form where course admins can specify wether uploaded documents
  139. * are visible or invisible by default.
  140. *
  141. * @param $uploadvisibledisabled
  142. * @param $origin
  143. */
  144. function display_default_visibility_form($uploadvisibledisabled) {
  145. ?>
  146. <div class="row">
  147. <div class="label">
  148. <?php echo get_lang('_default_upload'); ?>
  149. </div>
  150. <div class="formw">
  151. <input class="checkbox" type="radio" name="uploadvisibledisabled" value="0" <?php if ($uploadvisibledisabled == 0) echo 'checked'; ?> />
  152. <?php echo get_lang('_new_visible'); ?><br />
  153. <input class="checkbox" type="radio" name="uploadvisibledisabled" value="1" <?php if ($uploadvisibledisabled == 1) echo 'checked'; ?> />
  154. <?php echo get_lang('_new_unvisible'); ?>
  155. </div>
  156. </div>
  157. <?php
  158. }
  159. /**
  160. * Display a part of the form to edit the settings of the tool
  161. * In this case weither the students are allowed to delete their own publication or not (by default not)
  162. *
  163. * @return html code
  164. * @since Dokeos 1.8.6.2
  165. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  166. */
  167. function display_studentsdelete_form() {
  168. // by default api_get_course_setting returns -1 and the code only expects 0 or 1 so anything tha
  169. // is different than 1 will be converted into 0
  170. $current_course_setting_value = api_get_course_setting('student_delete_own_publication');
  171. if ($current_course_setting_value != 1) {
  172. $current_course_setting_value = 0;
  173. }
  174. ?>
  175. <div class="row">
  176. <div class="label">
  177. <?php echo get_lang('StudentAllowedToDeleteOwnPublication'); ?>
  178. </div>
  179. <div class="formw">
  180. <input class="checkbox" type="radio" name="student_delete_own_publication" value="0" <?php if ($current_course_setting_value == 0) echo 'checked'; ?> />
  181. <?php echo get_lang('No'); ?><br />
  182. <input class="checkbox" type="radio" name="student_delete_own_publication" value="1" <?php if ($current_course_setting_value == 1) echo 'checked'; ?> />
  183. <?php echo get_lang('Yes'); ?>
  184. </div>
  185. </div>
  186. <?php
  187. }
  188. /**
  189. * This function displays the firstname and lastname of the user as a link to the user tool.
  190. *
  191. * @see this is the same function as in the new forum, so this probably has to move to a user library.
  192. *
  193. * @todo move this function to the user library (remove duplicate in dropbox_functions.inc.php)
  194. *
  195. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  196. * @version march 2006
  197. */
  198. function display_user_link_work($user_id, $name = '', $gradebook='') {
  199. global $_otherusers;
  200. $user_id = intval($user_id);
  201. if ($user_id != 0) {
  202. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  203. $sql = "SELECT user_id, firstname, lastname FROM $table_user WHERE user_id='".Database::escape_string($user_id)."'";
  204. $result = Database::query($sql);
  205. $row = Database::fetch_array($result);
  206. if ($name == '') {
  207. return '<a href="../user/userInfo.php?cidReq='.api_get_course_id().'&amp;gradebook='.$gradebook.'&amp;origin=&amp;uInfo='.$row['user_id'].'">'.api_get_person_name($row['firstname'], $row['lastname']).'</a>';
  208. } else {
  209. return '<a href="../user/userInfo.php?cidReq='.api_get_course_id().'&amp;gradebook='.$gradebook.'&amp;origin=&amp;uInfo='.$user_id.'">'.$name.'</a>';
  210. }
  211. } else {
  212. return $name.' ('.get_lang('Anonymous').')';
  213. }
  214. }
  215. /**
  216. * converts 2008-10-06 12:45:00 to timestamp
  217. * @deprecated any calls found
  218. */
  219. function convert_date_to_number($default) {
  220. // 2008-10-12 00:00:00 ---to--> 12345672218 (timestamp)
  221. $parts = split(' ', $default);
  222. list($d_year, $d_month, $d_day) = split('-', $parts[0]);
  223. list($d_hour, $d_minute, $d_second) = split(':', $parts[1]);
  224. return mktime((int)$d_hour, (int)$d_minute, (int)$d_second, (int)$d_month, (int)$d_day, (int)$d_year);
  225. }
  226. /**
  227. * converts 1-9 to 01-09
  228. */
  229. function two_digits($number) {
  230. $number = (int)$number;
  231. return ($number < 10) ? '0'.$number : $number;
  232. }
  233. /**
  234. * converts 2008-10-06 12:45:00 to -> array($data'year'=>2008,$data'month'=>10 etc...)
  235. */
  236. function convert_date_to_array($date, $group) {
  237. $parts = split(' ', $date);
  238. $date_parts = split('-', $parts[0]);
  239. $date_parts_tmp = array();
  240. foreach ($date_parts as $item) {
  241. $date_parts_tmp[] = intval($item);
  242. }
  243. $time_parts = split(':', $parts[1]);
  244. $time_parts_tmp = array();
  245. foreach ($time_parts as $item) {
  246. $time_parts_tmp[] = intval($item);
  247. }
  248. list($data[$group.'[year]'], $data[$group.'[month]'], $data[$group.'[day]']) = $date_parts_tmp;
  249. list($data[$group.'[hour]'], $data[$group.'[minute]']) = $time_parts_tmp;
  250. return $data;
  251. }
  252. /**
  253. * get date from a group of date
  254. */
  255. function get_date_from_group($group) {
  256. return $_POST[$group]['year'].'-'.two_digits($_POST[$group]['month']).'-'.two_digits($_POST[$group]['day']).' '.two_digits($_POST[$group]['hour']).':'.two_digits($_POST[$group]['minute']).':00';
  257. }
  258. /**
  259. * create a group of select from a date
  260. */
  261. function create_group_date_select($prefix = '') {
  262. $minute = range(10, 59);
  263. $d_year = date('Y');
  264. array_unshift($minute, '00', '01', '02', '03', '04', '05', '06', '07', '08', '09');
  265. $group_name[] = FormValidator :: createElement('select', $prefix.'day', '', array_combine(range(1, 31), range(1, 31)));
  266. $group_name[] = FormValidator :: createElement('select', $prefix.'month', '', array_combine(range(1, 12), api_get_months_long()));
  267. $group_name[] = FormValidator :: createElement('select', $prefix.'year', '', array($d_year => $d_year, $d_year + 1 => $d_year + 1));
  268. $group_name[] = FormValidator :: createElement('select', $prefix.'hour', '', array_combine(range(0, 23), range(0, 23)));
  269. $group_name[] = FormValidator :: createElement('select', $prefix.'minute', '', $minute);
  270. return $group_name;
  271. }
  272. /**
  273. * Display the list of student publications, taking into account the user status
  274. *
  275. * @param $currentCourseRepositoryWeb, the web location of the course folder
  276. * @param $link_target_parameter - should there be a target parameter for the links
  277. * @param $dateFormatLong - date format
  278. * @param $origin - typically empty or 'learnpath'
  279. */
  280. function display_student_publications_list($work_dir, $sub_course_dir, $currentCourseRepositoryWeb, $link_target_parameter, $dateFormatLong, $origin, $add_in_where_query = '') {
  281. global $timeNoSecFormat, $dateFormatShort, $gradebook, $_user;
  282. // Database table names
  283. $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  284. $iprop_table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  285. $work_assigment = Database :: get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT);
  286. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  287. $user_id = api_get_user_id();
  288. $publications_list = array();
  289. $sort_params = array();
  290. if (isset($_GET['column'])) {
  291. $sort_params[] = 'column='.Security::remove_XSS($_GET['column']);
  292. }
  293. if (isset($_GET['page_nr'])) {
  294. $sort_params[] = 'page_nr='.Security::remove_XSS($_GET['page_nr']);
  295. }
  296. if (isset($_GET['per_page'])) {
  297. $sort_params[] = 'per_page='.Security::remove_XSS($_GET['per_page']);
  298. }
  299. if (isset($_GET['direction'])) {
  300. $sort_params[] = 'direction='.Security::remove_XSS($_GET['direction']);
  301. }
  302. $sort_params = implode('&amp;', $sort_params);
  303. $my_params = $sort_params;
  304. $origin = Security::remove_XSS($origin);
  305. if (substr($sub_course_dir, -1, 1) != '/' && !empty($sub_course_dir)) {
  306. $sub_course_dir = $sub_course_dir.'/';
  307. }
  308. if ($sub_course_dir == '/') {
  309. $sub_course_dir = '';
  310. }
  311. //condition for the session
  312. $session_id = api_get_session_id();
  313. $condition_session = api_get_session_condition($session_id);
  314. //Get list from database
  315. if ($is_allowed_to_edit) {
  316. $sql_get_publications_list = "SELECT * FROM ".$work_table." " .
  317. "WHERE url LIKE BINARY '$sub_course_dir%' " .
  318. "AND url NOT LIKE BINARY '$sub_course_dir%/%' " .$add_in_where_query.$condition_session.
  319. " ORDER BY sent_date DESC";
  320. $sql_get_publications_num = "SELECT count(*) FROM ".$work_table." " .
  321. "WHERE url LIKE BINARY '$sub_course_dir%' " .
  322. "AND url NOT LIKE BINARY '$sub_course_dir%/%' " .$add_in_where_query.
  323. $condition_session.
  324. " ORDER BY id";
  325. } else {
  326. if (!empty($_SESSION['toolgroup'])) {
  327. $group_query = " WHERE post_group_id = '".intval($_SESSION['toolgroup'])."' "; // set to select only messages posted by the user's group
  328. $subdirs_query = "AND url NOT LIKE BINARY '$sub_course_dir%/%' AND url LIKE BINARY '$sub_course_dir%'";
  329. } else {
  330. $group_query = " WHERE post_group_id = '0' ";
  331. $subdirs_query = "AND url NOT LIKE '$sub_course_dir%/%' AND url LIKE '$sub_course_dir%'";
  332. }
  333. $sql_get_publications_list = "SELECT * FROM $work_table $group_query $subdirs_query ".$add_in_where_query." $condition_session ORDER BY id";
  334. $sql_get_publications_num = "SELECT count(url) FROM ".$work_table." " .
  335. "WHERE url LIKE BINARY '$sub_course_dir%' AND url NOT LIKE BINARY '$sub_course_dir%/%' " .$add_in_where_query.
  336. $condition_session.
  337. " ORDER BY id";
  338. }
  339. //echo $sql_get_publications_list;
  340. $sql_result = Database::query($sql_get_publications_list);
  341. $sql_result_num = Database::query($sql_get_publications_num);
  342. $row = Database::fetch_array($sql_result_num);
  343. $count_files = $row[0];
  344. $table_header[] = array(get_lang('Type'), false, 'style="width:40px"');
  345. $table_header[] = array(get_lang('Title'), true);
  346. if ($count_files != 0) {
  347. $table_header[] = array(get_lang('Authors'), true);
  348. $table_header[] = array(get_lang('Qualification'), true);
  349. }
  350. $table_header[] = array(get_lang('Date'), true, 'style="width:160px"');
  351. if ($origin != 'learnpath') {
  352. $table_header[] = array(get_lang('Actions'), false, 'style="width:90px"');
  353. $table_header[] = array('RealDate', true);
  354. }
  355. // An array with the setting of the columns -> 1: columns that we will show, 0:columns that will be hide
  356. $column_show[] = 1; // type
  357. $column_show[] = 1; // title
  358. if ($count_files != 0) {
  359. $column_show[] = 1; // authors
  360. $column_show[] = 1; // qualification
  361. }
  362. $column_show[] = 1; //date
  363. $column_show[] = 1; // modify
  364. $column_show[] = 0; //real date in correct format
  365. // Here we change the way how the colums are going to be sort
  366. // in this case the the column of LastResent ( 4th element in $column_header) we will be order like the column RealDate
  367. // because in the column RealDate we have the days in a correct format "2008-03-12 10:35:48"
  368. $column_order = array();
  369. $i=0;
  370. foreach($table_header as $item) {
  371. $column_order[$i] = $i;
  372. $i++;
  373. }
  374. if ($count_files != 0) {
  375. if ($sub_course_dir == '/work') {
  376. $column_order[2] = 2;
  377. } else {
  378. //$column_order[2] = 5;
  379. }
  380. } else {
  381. //$column_order[1] = 4;
  382. $column_order[2] = 4;
  383. }
  384. $table_data = array();
  385. $dirs_list = get_subdirs_list($work_dir);
  386. $my_sub_dir = str_replace('work/', '', $sub_course_dir);
  387. // @todo Since "works" cant have sub works this foreach is useless when selecting the list of works
  388. // List of all folders
  389. if (is_array($dirs_list)) {
  390. foreach ($dirs_list as $dir) {
  391. if ($my_sub_dir == '') {
  392. $mydir_temp = '/'.$dir;
  393. } else {
  394. $mydir_temp = '/'.$my_sub_dir.$dir;
  395. }
  396. $sql_select_directory = "SELECT prop.lastedit_date, work.id, author, has_properties, view_properties, description, qualification, weight FROM ".$iprop_table." prop INNER JOIN ".$work_table." work ON (prop.ref=work.id) WHERE ";
  397. if (!empty($_SESSION['toolgroup'])) {
  398. $sql_select_directory .= " work.post_group_id = '".$_SESSION['toolgroup']."' "; // set to select only messages posted by the user's group
  399. } else {
  400. $sql_select_directory .= " work.post_group_id = '0' ";
  401. }
  402. $sql_select_directory .= " AND work.url LIKE BINARY '".$mydir_temp."' AND work.filetype = 'folder' AND prop.tool='work' $condition_session";
  403. $result = Database::query($sql_select_directory);
  404. $row = Database::fetch_array($result);
  405. if (!$row) {
  406. // the folder belongs to another session
  407. continue;
  408. }
  409. $direc_date = $row['lastedit_date']; //directory's date
  410. $author = $row['author']; //directory's author
  411. $view_properties = $row['view_properties'];
  412. $is_assignment = $row['has_properties'];
  413. $id2 = $row['id'];
  414. $mydir = $my_sub_dir.$dir;
  415. if ($is_allowed_to_edit) {
  416. isset($_GET['edit_dir']) ? $clean_edit_dir = Security :: remove_XSS($_GET['edit_dir']) : $clean_edit_dir = '';
  417. // form edit directory
  418. if (isset($clean_edit_dir) && $clean_edit_dir == $mydir) {
  419. if (!empty($row['has_properties'])) {
  420. $sql = Database::query('SELECT * FROM '.$work_assigment.' WHERE id = '."'".$row['has_properties']."'".' LIMIT 1');
  421. $homework = Database::fetch_array($sql);
  422. }
  423. $form_folder = new FormValidator('edit_dir', 'post', api_get_self().'?curdirpath='.$my_sub_dir.'&origin='.$origin.'&gradebook='.$gradebook.'&edit_dir='.$mydir);
  424. $group_name[] = FormValidator :: createElement('text', 'dir_name');
  425. $form_folder -> addGroup($group_name, 'my_group', get_lang('Title'));
  426. $form_folder -> addGroupRule('my_group', get_lang('ThisFieldIsRequired'), 'required');
  427. $defaults = array('my_group[dir_name]' => html_entity_decode($dir), 'description' => api_html_entity_decode($row['description']));
  428. //$form_folder-> addElement('textarea', 'description', get_lang('Description'), array('rows' => 5, 'cols' => 50));
  429. $form_folder->add_html_editor('description', get_lang('Description'), false, false, array('ToolbarSet' => 'work', 'Width' => '100%', 'Height' => '200'));
  430. $qualification_input[] = FormValidator :: createElement('text','qualification');
  431. $form_folder -> addGroup($qualification_input, 'qualification', get_lang('QualificationNumeric'), 'size="10"');
  432. if ($row['weight'] > 0) {
  433. $weight_input[] = FormValidator :: createElement('text', 'weight');
  434. $form_folder -> addGroup($weight_input, 'weight', get_lang('WeightInTheGradebook'), 'size="10"');
  435. }
  436. $there_is_a_end_date = false;
  437. if ($row['view_properties'] == '1') {
  438. if ($homework['expires_on'] != '0000-00-00 00:00:00') {
  439. $homework['expires_on'] = api_get_local_time($homework['expires_on']);
  440. $there_is_a_expire_date = true;
  441. $form_folder -> addGroup(create_group_date_select(), 'expires', get_lang('ExpiresAt'));
  442. }
  443. if ($homework['ends_on'] != '0000-00-00 00:00:00') {
  444. $homework['ends_on'] = api_get_local_time($homework['ends_on']);
  445. $there_is_a_end_date = true;
  446. $form_folder -> addGroup(create_group_date_select(), 'ends', get_lang('EndsAt'));
  447. }
  448. if ($there_is_a_expire_date && $there_is_a_end_date) {
  449. $form_folder -> addRule(array('expires', 'ends'), get_lang('DateExpiredNotBeLessDeadLine'), 'comparedate');
  450. }
  451. } else {
  452. $form_folder -> addElement('html', '<div class="row">
  453. <div class="label">&nbsp;</div>
  454. <div class="formw">
  455. <a href="javascript://" onclick="javascript: return plus();" ><span id="plus">&nbsp;<img style="vertical-align:middle;" src="../img/div_show.gif" alt="" />&nbsp;'.get_lang('AdvancedParameters').'</span></a>
  456. </div>
  457. </div> ');
  458. $form_folder -> addElement('html', '<div id="options" style="display: none;">');
  459. if (empty($default)) {
  460. $default = api_get_local_time();
  461. }
  462. $parts = split(' ', $default);
  463. list($d_year, $d_month, $d_day) = split('-', $parts[0]);
  464. list($d_hour, $d_minute) = split(':', $parts[1]);
  465. if ((int)$row['weight'] == 0) {
  466. $form_folder -> addElement('checkbox', 'make_calification', null, get_lang('MakeQualifiable'), 'onclick="javascript: if(this.checked){document.getElementById(\'option3\').style.display = \'block\';}else{document.getElementById(\'option3\').style.display = \'none\';}"');
  467. $form_folder -> addElement('html', '<div id=\'option3\' style="display:none">');
  468. $weight_input2[] = FormValidator :: createElement('text', 'weight');
  469. $form_folder -> addGroup($weight_input2, 'weight', get_lang('WeightInTheGradebook'), 'size="10"');
  470. $form_folder -> addElement('html', '</div>');
  471. }
  472. if ($homework['expires_on'] == '0000-00-00 00:00:00') {
  473. $homework['expires_on'] = api_get_local_time();
  474. $there_is_a_expire_date = true;
  475. $form_folder -> addElement('checkbox', 'enableExpiryDate',null,get_lang('EnableExpiryDate'), 'onclick="javascript: if(this.checked){document.getElementById(\'option1\').style.display = \'block\';}else{document.getElementById(\'option1\').style.display = \'none\';}"');
  476. $form_folder -> addElement('html', '<div id=\'option1\' style="display:none">');
  477. $form_folder -> addGroup(create_group_date_select(), 'expires', get_lang('ExpiresAt'));
  478. $form_folder -> addElement('html', '</div>');
  479. }
  480. if ($homework['ends_on'] == '0000-00-00 00:00:00') {
  481. $homework['ends_on'] = api_get_local_time();
  482. $there_is_a_end_date = true;
  483. $form_folder -> addElement('checkbox', 'enableEndDate', null, get_lang('EnableEndDate'), 'onclick="javascript: if(this.checked){document.getElementById(\'option2\').style.display = \'block\';}else{document.getElementById(\'option2\').style.display = \'none\';}"');
  484. $form_folder -> addElement('html', '<div id=\'option2\' style="display:none">');
  485. $form_folder -> addGroup(create_group_date_select(), 'ends', get_lang('EndsAt'));
  486. $form_folder -> addElement('html', '</div>');
  487. }
  488. $form_folder -> addRule(array('expires', 'ends'), get_lang('DateExpiredNotBeLessDeadLine'), 'comparedate');
  489. $form_folder -> addElement('html', '</div>');
  490. }
  491. $form_folder -> addElement('style_submit_button', 'submit', get_lang('ModifyDirectory'), 'class="save"');
  492. if ($there_is_a_end_date) {
  493. $end_date_array = convert_date_to_array($homework['ends_on'], 'ends');
  494. $defaults = array_merge($defaults, $end_date_array);
  495. }
  496. if ($there_is_a_expire_date) {
  497. $expires_date_array = convert_date_to_array($homework['expires_on'], 'expires');
  498. $defaults = array_merge($defaults, $expires_date_array);
  499. }
  500. if (!empty($row['qualification'])) {
  501. $defaults = array_merge($defaults, array('qualification[qualification]' => $row['qualification']));
  502. }
  503. if (!empty($row['weight'])) {
  504. $defaults = array_merge($defaults, array('weight[weight]' => $row['weight']));
  505. }
  506. $form_folder -> setDefaults($defaults);
  507. $display_edit_form = true;
  508. if ($form_folder -> validate()) {
  509. $values = $form_folder -> exportValues();
  510. $values = $values['my_group'];
  511. $dir_name = replace_dangerous_char($values['dir_name']);
  512. $dir_name = disable_dangerous_file($dir_name);
  513. $edit_check = false;
  514. if ($dir_name != $dir) {
  515. if (!is_work_exist_by_url('/'.$dir_name)) {
  516. $edit_check = true;
  517. }
  518. } else {
  519. $edit_check = true;
  520. }
  521. if ($edit_check) {
  522. $TABLEAGENDA = Database::get_course_table(TABLE_AGENDA);
  523. if ($there_is_a_end_date || $there_is_a_expire_date) {
  524. if ($row['view_properties'] == '1') {
  525. $sql_add_publication = "UPDATE ".$work_table." SET has_properties = '".$row['has_properties']. "', view_properties=1 where id ='".$row['id']."'";
  526. Database::query($sql_add_publication);
  527. $expires_query = ' SET expires_on = '."'".($there_is_a_expire_date ? api_get_utc_datetime(get_date_from_group('expires')) : '0000-00-00 00:00:00')."'".',';
  528. $ends_query = ' ends_on = '."'".($there_is_a_end_date ? api_get_utc_datetime(get_date_from_group('ends')) : '0000-00-00 00:00:00')."'";
  529. Database::query('UPDATE '.$work_assigment.$expires_query.$ends_query.' WHERE id = '."'".$row['has_properties']."'");
  530. } elseif ($row['view_properties'] == '0') {
  531. if ($_POST['enableExpiryDate'] == '1') {
  532. $expires_query = ' SET expires_on = '."'".($there_is_a_expire_date ? api_get_utc_datetime(get_date_from_group('expires')) : '0000-00-00 00:00:00')."'";
  533. //$ends_query = ' ends_on = '."'".($there_is_a_end_date ? get_date_from_group('ends') : '0000-00-00 00:00:00')."'";
  534. Database::query('UPDATE '.$work_assigment.$expires_query.' WHERE id = '."'".$row['has_properties']."'");
  535. $sql_add_publication = "UPDATE ".$work_table." SET has_properties = '".$row['has_properties']. "', view_properties=1 where id ='".$row['id']."'";
  536. Database::query($sql_add_publication);
  537. }
  538. if ($_POST['enableEndDate'] == '1') {
  539. //$expires_query = ' SET expires_on = '."'".($there_is_a_expire_date ? get_date_from_group('expires') : '0000-00-00 00:00:00')."'".',';
  540. $ends_query = ' SET ends_on = '."'".($there_is_a_end_date ? api_get_utc_datetime(get_date_from_group('ends')) : '0000-00-00 00:00:00')."'";
  541. Database::query('UPDATE '.$work_assigment.$ends_query.' WHERE id = '."'".$row['has_properties']."'");
  542. $sql_add_publication = "UPDATE ".$work_table." SET has_properties = '".$row['has_properties']. "', view_properties=1 where id ='".$row['id']."'";
  543. Database::query($sql_add_publication);
  544. }
  545. }
  546. }
  547. //if($_POST['qualification']['qualification']!='')
  548. Database::query('UPDATE '.$work_table.' SET description = '."'".Database::escape_string($_POST['description'])."'".', qualification = '."'".Database::escape_string($_POST['qualification']['qualification'])."'".',weight = '."'".Database::escape_string($_POST['weight']['weight'])."'".' WHERE id = '."'".$row['id']."'");
  549. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/gradebook_functions.inc.php';
  550. $link_id = is_resource_in_course_gradebook(api_get_course_id(), 3 , $row['id'], api_get_session_id());
  551. if ($link_id !== false) {
  552. Database::query('UPDATE '.Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK).' SET weight = '."'".Database::escape_string((float)$_POST['weight']['weight'])."'".' WHERE id = '.$link_id);
  553. }
  554. //we are changing the current work and we want add them into gradebook
  555. if (isset($_POST['make_calification']) && $_POST['make_calification'] == 1) {
  556. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/gradebookitem.class.php';
  557. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/evaluation.class.php';
  558. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/abstractlink.class.php';
  559. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/gradebook_functions.inc.php';
  560. $resource_name = $_POST['dir_name'];
  561. add_resource_to_course_gradebook(api_get_course_id(), 3, $row['id'], Database::escape_string($resource_name), (float)$_POST['weight']['weight'], (float)$_POST['qualification']['qualification'], Database::escape_string($_POST['description']), time(), 1, api_get_session_id());
  562. }
  563. update_dir_name($mydir, $dir_name);
  564. $mydir = $my_sub_dir.$dir_name;
  565. $dir = $dir_name;
  566. $display_edit_form = false;
  567. // gets calendar_id from student_publication_assigment
  568. $sql = "SELECT add_to_calendar FROM $work_assigment WHERE publication_id ='".$row['id']."'";
  569. $res = Database::query($sql);
  570. $calendar_id = Database::fetch_row($res);
  571. // update from agenda if it exists
  572. if (!empty($calendar_id[0])) {
  573. $sql = "UPDATE ".$TABLEAGENDA."
  574. SET title='".$dir_name."',
  575. content = '".$dir_name."',
  576. end_date='".get_date_from_group('ends')."'
  577. WHERE id='".$calendar_id[0]."'";
  578. Database::query($sql);
  579. }
  580. Display::display_confirmation_message(get_lang('FolderEdited'));
  581. } else {
  582. Display::display_warning_message(get_lang('FileExists'));
  583. }
  584. }
  585. }
  586. }
  587. $action = '';
  588. $row = array();
  589. $class = '';
  590. //$a_count_directory = count_dir($work_dir.'/'.$dir, false);
  591. $cant_files = 0;
  592. $cant_dir = 0;
  593. if (api_is_allowed_to_edit()) {
  594. $sql_document = "SELECT count(*) FROM $work_table WHERE url NOT LIKE '".$sub_course_dir.$dir."/%/%' AND url LIKE '".$sub_course_dir.$dir."/%'";
  595. } else {
  596. // gets admin_course
  597. $table_course_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  598. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  599. $sql = "SELECT course_user.user_id FROM $table_user user, $table_course_user course_user
  600. WHERE course_user.user_id=user.user_id AND course_user.course_code='".api_get_course_id()."' AND course_user.status='1'";
  601. $res = Database::query($sql);
  602. $admin_course = '';
  603. while($row_admin = Database::fetch_row($res)) {
  604. $admin_course .= '\''.$row_admin[0].'\',';
  605. }
  606. $sql_document = "SELECT count(*) FROM $work_table s, $iprop_table p WHERE s.id = p.ref AND p.tool='work' AND s.accepted='1' AND url NOT LIKE '".$sub_course_dir.$dir."/%/%' AND url LIKE '".$sub_course_dir.$dir."/%'";
  607. }
  608. //count documents
  609. $res_document = Database::query($sql_document);
  610. $count_document = Database::fetch_row($res_document);
  611. $cant_files = $count_document[0];
  612. //count directories
  613. $sql_directory = "SELECT count(*) FROM $work_table s WHERE url NOT LIKE '/".$mydir."/%/%' AND url LIKE '/".$mydir."/%'";
  614. $res_directory = Database::query($sql_directory);
  615. $count_directory = Database::fetch_row($res_directory);
  616. $cant_dir = $count_directory[0];
  617. $text_file = get_lang('FilesUpload');
  618. $text_dir = get_lang('Directories');
  619. if ($cant_files == 1) {
  620. $text_file = api_strtolower(get_lang('FileUpload'));
  621. }
  622. if ($cant_dir == 1) {
  623. $text_dir = get_lang('directory');
  624. }
  625. if ($cant_dir != 0) {
  626. $dirtext = ' ('.$cant_dir.' '.$text_dir.')';
  627. } else {
  628. $dirtext = '';
  629. }
  630. $icon = Display::return_icon('work.png', get_lang('Assignment'),array(), 22);
  631. if (!empty($display_edit_form) && isset($clean_edit_dir) && $clean_edit_dir == $mydir) {
  632. $row[] = $icon;
  633. $row[] = '<span class="invisible" style="display:none">'.$dir.'</span>'.$form_folder->toHtml(); // form to edit the directory's name
  634. } else {
  635. $row[] = '<a href="'.api_get_self().'?'.api_get_cidreq().'&origin='.$origin.'&gradebook='.$gradebook.'&curdirpath='.$mydir.'">'.$icon.'</a>';
  636. $add_to_name = '';
  637. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/gradebook_functions.inc.php';
  638. $link_id = is_resource_in_course_gradebook(api_get_course_id(), 3 , $id2 , api_get_session_id());
  639. $count = 0;
  640. if ($link_id !== false) {
  641. $gradebook_data = get_resource_from_course_gradebook($link_id);
  642. $count = $gradebook_data['weight'];
  643. }
  644. if ($count > 0) {
  645. $add_to_name = ' / <span style="color:blue">'.get_lang('IncludedInEvaluation').'</span>';
  646. } else {
  647. $add_to_name = '';
  648. }
  649. $show_as_icon = get_work_id($mydir); //true or false
  650. if ($show_as_icon) {
  651. if (api_is_allowed_to_edit()) {
  652. $zip = '<a href="'.api_get_self().'?cidReq='.api_get_course_id().'&gradebook='.$gradebook.'&action=downloadfolder&path=/'.$mydir.'">
  653. '.Display::return_icon('save_pack.png', get_lang('Save'), array('style' => 'float:right;'), 22).'</a>';
  654. }
  655. $row[] = $zip.'<a href="'.api_get_self().'?'.api_get_cidreq().'&origin='.$origin.'&gradebook='.Security::remove_XSS($_GET['gradebook']).'&curdirpath='.$mydir.'"'.$class.'>'.$dir.'</a>'.$add_to_name.'<br />'.$cant_files.' '.$text_file.$dirtext;
  656. } else {
  657. $row[] = '<a href="'.api_get_self().'?'.api_get_cidreq().'&origin='.$origin.'&gradebook='.$gradebook.'&curdirpath='.$mydir.'"'.$class.'>'.$dir.'</a>'.$add_to_name.'<br />'.$cant_files.' '.$text_file.$dirtext;
  658. }
  659. }
  660. if ($count_files != 0) {
  661. $row[] = '';
  662. }
  663. if ($direc_date != '' && $direc_date != '0000-00-00 00:00:00') {
  664. $direc_date_local = api_get_local_time($direc_date);
  665. $row[] = date_to_str_ago($direc_date_local).'<br /><span class="dropbox_date">'.api_format_date($direc_date_local).'</span>';
  666. } else {
  667. $direc_date_local = '0000-00-00 00:00:00';
  668. $row[] = '';
  669. }
  670. if ($origin != 'learnpath') {
  671. if ($is_allowed_to_edit) {
  672. $action .= '<a href="'.api_get_self().'?cidReq='.api_get_course_id().'&curdirpath='.$my_sub_dir.'&origin='.$origin.'&gradebook='.$gradebook.'&edit_dir='.$mydir.'">'.Display::return_icon('edit.png', get_lang('Modify'),array(), 22).'</a>';
  673. $action .= ' <a href="'.api_get_self().'?'.api_get_cidreq().'&origin='.$origin.'&gradebook='.$gradebook.'&delete_dir='.$mydir.'&delete2='.$id2.'" onclick="javascript:if(!confirm('."'".addslashes(api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES))."'".')) return false;" title="'.get_lang('DirDelete').'" >'.Display::return_icon('delete.png',get_lang('DirDelete'),'',22).'</a>';
  674. $row[] = $action;
  675. } else {
  676. $row[] = '';
  677. }
  678. }
  679. $row[] = $direc_date_local;
  680. $table_data[] = $row;
  681. }
  682. }
  683. //Redefining $my_sub_dir
  684. if (substr($my_sub_dir,strlen($my_sub_dir)-1, strlen($my_sub_dir)) == '/') {
  685. $my_sub_dir = substr($my_sub_dir, 0,strlen($my_sub_dir)-1);
  686. }
  687. if (Database::num_rows($sql_result) > 0) {
  688. while ($work = Database::fetch_object($sql_result)) {
  689. //Get the author ID for that document from the item_property table
  690. $is_author = false;
  691. $author_sql = "SELECT * FROM $iprop_table WHERE tool = 'work' AND ref=".$work->id;
  692. $author_qry = Database::query($author_sql);
  693. $row2 = Database::fetch_array($author_qry);
  694. if (Database::num_rows($author_qry) == 1) {
  695. $is_author = true;
  696. }
  697. //display info depending on the permissions
  698. if ($work->accepted == '1' || $is_allowed_to_edit) {
  699. $row = array();
  700. if ($work->accepted == '0') {
  701. $class = 'class="invisible"';
  702. } else {
  703. $class = '';
  704. }
  705. $qualification_string = '';
  706. $add_string = '';
  707. if (defined('IS_ASSIGNMENT')) {
  708. if($work->qualification == '') {
  709. //$qualification_string = '<b style="color:orange">'.get_lang('NotRevised').'</b>';
  710. $qualification_string = '<b style="color:orange"> - </b>';
  711. } else {
  712. //$qualification_string = '<b style="color:blue">'.get_lang('Qualification').': '.$work->qualification.'</b>';
  713. $qualification_string = '<b style="color:blue">'.$work->qualification.'</b>';
  714. }
  715. if (defined('ASSIGNMENT_EXPIRES') && (ASSIGNMENT_EXPIRES < api_strtotime($work->sent_date))) {
  716. $add_string = ' <b style="color:red">'.get_lang('Expired').'</b>';
  717. }
  718. }
  719. $url = implode('/', array_map('rawurlencode', explode('/', $work->url)));
  720. $row[] = '<a href="download.php?file='.$url.'">'.build_document_icon_tag('file', substr(basename($work->url), 13)).'</a>';
  721. $row[] = '<a href="download.php?file='.$url.'"'.$class.'>'.Display::return_icon('save.png', get_lang('Save'),array('style' => 'float:right;'), 22).' '.$work->title.'</a><br />'.$work->description;
  722. $row[] = display_user_link_work($row2['insert_user_id'], $work->author, $gradebook); // $work->author;
  723. $row[] = $qualification_string;
  724. $work_sent_date_local = api_get_local_time($work->sent_date);
  725. $row[] = date_to_str_ago($work_sent_date_local).$add_string.'<br /><span class="dropbox_date">'.api_format_date($work_sent_date_local).'</span>';
  726. if ($is_allowed_to_edit) {
  727. $action = '';
  728. $action .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&curdirpath='.urlencode($my_sub_dir).'&amp;origin='.$origin.'&gradebook='.$gradebook.'&amp;edit='.$work->id.'&gradebook='.Security::remove_XSS($_GET['gradebook']).'&amp;parent_id='.$work->parent_id.'" title="'.get_lang('Modify').'" >'.Display::return_icon('rate_work.png', get_lang('CorrectAndRate'),array(), 22).'</a>';
  729. $action .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&curdirpath='.urlencode($my_sub_dir).'&amp;origin='.$origin.'&gradebook='.$gradebook.'&amp;move='.$work->id.'" title="'.get_lang('Move').'">'.Display::return_icon('move.png', get_lang('Move'),array(), 22).'</a>';
  730. if ($work->accepted == '1') {
  731. $action .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&curdirpath='.urlencode($my_sub_dir).'&amp;origin='.$origin.'&gradebook='.$gradebook.'&amp;make_invisible='.$work->id.'&amp;'.$sort_params.'" title="'.get_lang('Invisible').'" >'.Display::return_icon('visible.png', get_lang('Invisible'),array(), 22).'</a>';
  732. } else {
  733. $action .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&curdirpath='.urlencode($my_sub_dir).'&amp;origin='.$origin.'&gradebook='.$gradebook.'&amp;make_visible='.$work->id.'&amp;'.$sort_params.'" title="'.get_lang('Visible').'" >'.Display::return_icon('invisible.png', get_lang('Visible'),array(), 22).'</a> ';
  734. }
  735. $action .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&curdirpath='.urlencode($my_sub_dir).'&amp;origin='.$origin.'&gradebook='.$gradebook.'&amp;delete='.$work->id.'" onclick="javascript:if(!confirm('."'".addslashes(api_htmlentities(get_lang('ConfirmYourChoice'),ENT_QUOTES))."'".')) return false;" title="'.get_lang('WorkDelete').'" >'.Display::return_icon('delete.png', get_lang('WorkDelete'),'',22).'</a>';
  736. $row[] = $action;
  737. // the user that is not course admin can only edit/delete own document
  738. } elseif ($row2['insert_user_id'] == $_user['user_id']) {
  739. $action = '';
  740. $action .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&curdirpath='.urlencode($my_sub_dir).'&gradebook='.Security::remove_XSS($_GET['gradebook']).'&amp;origin='.$origin.'&gradebook='.$gradebook.'&amp;edit='.$work->id.'" title="'.get_lang('Modify').'" >'.Display::return_icon('edit.png', get_lang('Modify'),array(), 22).'</a>';
  741. if (api_get_course_setting('student_delete_own_publication') == 1) {
  742. $action .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&curdirpath='.urlencode($my_sub_dir).'&amp;origin='.$origin.'&gradebook='.$gradebook.'&amp;delete='.$work->id.'" onclick="javascript:if(!confirm('."'".addslashes(api_htmlentities(get_lang('ConfirmYourChoice'),ENT_QUOTES))."'".')) return false;" title="'.get_lang('WorkDelete').'" >'.Display::return_icon('delete.png',get_lang('WorkDelete'),'',22).'</a>';
  743. }
  744. $row[] = $action;
  745. } else {
  746. $row[] = ' ';
  747. }
  748. $row[] = $work_sent_date_local;
  749. $table_data[] = $row;
  750. }
  751. }
  752. }
  753. $sorting_options = array();
  754. $sorting_options['column'] = 1;
  755. $paging_options = array();
  756. if (isset($_GET['curdirpath'])) {
  757. $my_params = array ('curdirpath' => Security::remove_XSS($_GET['curdirpath']));
  758. }
  759. if (isset($_GET['edit_dir'])) {
  760. $my_params = array ('edit_dir' => Security::remove_XSS($_GET['edit_dir']));
  761. }
  762. $my_params['origin'] = $origin;
  763. Display::display_sortable_config_table('work', $table_header, $table_data, $sorting_options, $paging_options, $my_params, $column_show, $column_order);
  764. }
  765. /**
  766. * Returns a list of subdirectories found in the given directory.
  767. *
  768. * The list return starts from the given base directory.
  769. * If you require the subdirs of /var/www/ (or /var/www), you will get 'abc/', 'def/', but not '/var/www/abc/'...
  770. * @param string Base dir
  771. * @param integer 0 if we only want dirs from this level, 1 if we want to recurse into subdirs
  772. * @return strings_array The list of subdirs in 'abc/' form, -1 on error, and 0 if none found
  773. * @todo Add a session check to see if subdirs_list doesn't exist yet (cached copy)
  774. */
  775. function get_subdirs_list($basedir = '', $recurse = 0) {
  776. //echo "Looking for subdirs of $basedir";
  777. if (empty($basedir) or !is_dir($basedir)) {
  778. return -1;
  779. }
  780. if (substr($basedir, -1, 1) != '/') {
  781. $basedir = $basedir.'/';
  782. }
  783. $dirs_list = array();
  784. $dh = opendir($basedir);
  785. while ($entry = readdir($dh)) {
  786. $entry = replace_dangerous_char($entry);
  787. $entry = disable_dangerous_file($entry);
  788. if (is_dir($basedir.$entry) && $entry != '..' && $entry != '.') {
  789. $dirs_list[] = $entry;
  790. if ($recurse == 1) {
  791. foreach (get_subdirs_list($basedir.$entry) as $subdir) {
  792. $dirs_list[] = $entry.'/'.$subdir;
  793. }
  794. }
  795. }
  796. }
  797. closedir($dh);
  798. return $dirs_list;
  799. }
  800. /**
  801. * Builds the form thats enables the user to
  802. * select a directory to browse/upload in
  803. * This function has been copied from the document/document.inc.php library
  804. *
  805. * @param array $folders
  806. * @param string $curdirpath
  807. * @param string $group_dir
  808. * @return string html form
  809. */
  810. // TODO: This function is a candidate for removal, it is not used anywhere.
  811. function build_work_directory_selector($folders, $curdirpath, $group_dir = '') {
  812. $form = '<form name="selector" action="'.api_get_self().'?'.api_get_cidreq().'" method="POST">'."\n";
  813. $form .= get_lang('CurrentDirectory').' <select name="curdirpath" onchange="javascript: document.selector.submit();">'."\n";
  814. //group documents cannot be uploaded in the root
  815. if ($group_dir == '') {
  816. $form .= '<option value="/">/ ('.get_lang('Root').')</option>';
  817. if (is_array($folders)) {
  818. foreach ($folders as $folder) {
  819. $selected = ($curdirpath == $folder) ? ' selected="selected"' : '';
  820. $form .= '<option'.$selected.' value="'.$folder.'">'.$folder.'</option>'."\n";
  821. }
  822. }
  823. } else {
  824. foreach ($folders as $folder) {
  825. $selected = ($curdirpath == $folder) ? ' selected="selected"' : '';
  826. $display_folder = substr($folder, strlen($group_dir));
  827. $display_folder = ($display_folder == '') ? '/ ('.get_lang('Root').')' : $display_folder;
  828. $form .= '<option'.$selected.' value="'.$folder.'">'.$display_folder.'</option>'."\n";
  829. }
  830. }
  831. $form .= '</select>'."\n";
  832. $form .= '<noscript><input type="submit" name="change_path" value="'.get_lang('Ok').'" /></noscript>'."\n";
  833. $form .= '</form>';
  834. return $form;
  835. }
  836. /**
  837. * Builds the form thats enables the user to
  838. * move a document from one directory to another
  839. * This function has been copied from the document/document.inc.php library
  840. *
  841. * @param array $folders
  842. * @param string $curdirpath
  843. * @param string $move_file
  844. * @return string html form
  845. */
  846. function build_work_move_to_selector($folders, $curdirpath, $move_file, $group_dir = '') {
  847. $move_file = intval($move_file);
  848. $tbl_work = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  849. $sql = "SELECT title FROM $tbl_work WHERE id ='".$move_file."'";
  850. $result = Database::query($sql);
  851. $title = Database::fetch_row($result);
  852. global $gradebook;
  853. $form = '<form name="move_to" action="'.api_get_self().'?gradebook='.$gradebook.'&curdirpath='.Security::remove_XSS($curdirpath).'" method="POST">';
  854. $form .= '<div class="row"><div class="form_header">'.get_lang('MoveFile').' - '.Security::remove_XSS($title[0]).'</div></div>';
  855. $form .= '<input type="hidden" name="move_file" value="'.$move_file.'" />';
  856. $form .= '<div class="row">
  857. <div class="label">
  858. <span class="form_required">*</span>'.get_lang('Select').'
  859. </div>
  860. <div class="formw">';
  861. $form .= ' <select name="move_to">';
  862. //group documents cannot be uploaded in the root
  863. if ($group_dir == '') {
  864. if ($curdirpath != '/') {
  865. //$form .= '<option value="0">/ ('.get_lang('Root').')</option>';
  866. }
  867. if (is_array($folders)) {
  868. foreach ($folders as $fid => $folder) {
  869. //you cannot move a file to:
  870. //1. current directory
  871. //2. inside the folder you want to move
  872. //3. inside a subfolder of the folder you want to move
  873. if (($curdirpath != $folder) && ($folder != $move_file) && (substr($folder, 0, strlen($move_file) + 1) != $move_file.'/')) {
  874. $form .= '<option value="'.$fid.'">'.$folder.'</option>';
  875. }
  876. }
  877. }
  878. } else {
  879. if ($curdirpath != '/') {
  880. $form .= '<option value="0">/ ('.get_lang('Root').')</option>';
  881. }
  882. foreach ($folders as $fid => $folder) {
  883. if (($curdirpath != $folder) && ($folder != $move_file) && (substr($folder, 0, strlen($move_file) + 1) != $move_file.'/')) {
  884. //cannot copy dir into his own subdir
  885. $display_folder = substr($folder, strlen($group_dir));
  886. $display_folder = ($display_folder == '') ? '/ ('.get_lang('Root').')' : $display_folder;
  887. $form .= '<option value="'.$fid.'">'.$display_folder.'</option>'."\n";
  888. }
  889. }
  890. }
  891. $form .= '</select>';
  892. $form .= ' </div>
  893. </div>';
  894. $form .= '<div class="row">
  895. <div class="label"></div>
  896. <div class="formw">
  897. <button type="submit" class="save" name="move_file_submit">'.get_lang('MoveFile').'</button>
  898. </div>
  899. </div>';
  900. $form .= '</form>';
  901. $form .= '<div style="clear: both; margin-bottom: 10px;"></div>';
  902. return $form;
  903. }
  904. /**
  905. * Checks if the first given directory exists as a subdir of the second given directory
  906. * This function should now be deprecated by Security::check_abs_path()
  907. * @param string Subdir
  908. * @param string Base dir
  909. * @return integer -1 on error, 0 if not subdir, 1 if subdir
  910. */
  911. // TODO: This function is a candidate for removal, it is not used anywhere.
  912. function is_subdir_of($subdir, $basedir) {
  913. if (empty($subdir) or empty($basedir)) {
  914. return -1;
  915. }
  916. if (substr($basedir, -1, 1) != '/') {
  917. $basedir = $basedir.'/';
  918. }
  919. if (substr($subdir, 0, 1) == '/') {
  920. $subdir = substr($subdir, 1);
  921. }
  922. return is_dir($basedir.$subdir) ? 1 : 0;
  923. }
  924. /**
  925. * creates a new directory trying to find a directory name
  926. * that doesn't already exist
  927. * (we could use unique_name() here...)
  928. *
  929. * @author Hugues Peeters <hugues.peeters@claroline.net>
  930. * @author Bert Vanderkimpen
  931. * @author Yannick Warnier <ywarnier@beeznest.org> Adaptation for work tool
  932. * @param string Base work dir (.../work)
  933. * @param string $desiredDirName complete path of the desired name
  934. * @return string actual directory name if it succeeds, boolean false otherwise
  935. */
  936. function create_unexisting_work_directory($base_work_dir, $desired_dir_name) {
  937. $nb = '';
  938. $base_work_dir = (substr($base_work_dir, -1, 1) == '/' ? $base_work_dir : $base_work_dir.'/');
  939. while (file_exists($base_work_dir.$desired_dir_name.$nb)) {
  940. $nb += 1;
  941. }
  942. if (@mkdir($base_work_dir.$desired_dir_name.$nb, api_get_permissions_for_new_directories())) {
  943. return $desired_dir_name.$nb;
  944. } else {
  945. return false;
  946. }
  947. }
  948. /**
  949. * Delete a work-tool directory
  950. * @param string Base "work" directory for this course as /var/www/chamilo/courses/ABCD/work/
  951. * @param string The directory name as the bit after "work/", without trailing slash
  952. * @return integer -1 on error
  953. */
  954. function del_dir($base_work_dir, $dir, $id) {
  955. $id = intval($id);
  956. if (empty($dir) or $dir == '/') {
  957. return -1;
  958. }
  959. $check = Security::check_abs_path($base_work_dir.$dir, $base_work_dir);
  960. if (!$check || !is_dir($base_work_dir.$dir)) {
  961. return -1;
  962. }
  963. $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  964. //Deleting the folder
  965. $url_path = get_work_path($id);
  966. if (!empty($url_path) && $url_path != -1) {
  967. //Deleting all contents inside the folder
  968. $sql = "DELETE FROM $table WHERE url LIKE BINARY 'work/".$dir."/%'";
  969. $res = Database::query($sql);
  970. $sql = "DELETE FROM $table WHERE filetype = 'folder' AND id = $id";
  971. $res = Database::query($sql);
  972. require_once api_get_path(LIBRARY_PATH).'fileManage.lib.php';
  973. $new_dir = $dir.'_DELETED_'.$id;
  974. if (api_get_setting('permanently_remove_deleted_files') == 'true'){
  975. my_delete($base_work_dir.$dir);
  976. } else {
  977. if (file_exists($base_work_dir.$dir)) {
  978. rename($base_work_dir.$dir, $base_work_dir.$new_dir);
  979. }
  980. }
  981. }
  982. }
  983. /**
  984. * Get the path of a document in the student_publication table (path relative to the course directory)
  985. * @param integer Element ID
  986. * @return string Path (or -1 on error)
  987. */
  988. function get_work_path($id) {
  989. $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  990. $sql = 'SELECT url FROM '.$table.' WHERE id='.intval($id);
  991. $res = Database::query($sql);
  992. if (Database::num_rows($res)) {
  993. $row = Database::fetch_array($res);
  994. return $row['url'];
  995. }
  996. return -1;
  997. }
  998. /**
  999. * Update the url of a work in the student_publication table
  1000. * @param integer ID of the work to update
  1001. * @param string Destination directory where the work has been moved (must end with a '/')
  1002. * @return -1 on error, sql query result on success
  1003. */
  1004. function update_work_url($id, $new_path, $parent_id) {
  1005. if (empty($id)) return -1;
  1006. $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  1007. $sql = "SELECT * FROM $table WHERE id=$id";
  1008. $res = Database::query($sql);
  1009. if (Database::num_rows($res) != 1) {
  1010. return -1;
  1011. } else {
  1012. $row = Database::fetch_array($res);
  1013. $filename = basename($row['url']);
  1014. $new_url = $new_path .$filename;
  1015. $sql2 = "UPDATE $table SET url = '$new_url', parent_id = '$parent_id' WHERE id=$id";
  1016. $res2 = Database::query($sql2);
  1017. return $res2;
  1018. }
  1019. }
  1020. /**
  1021. * Update the url of a dir in the student_publication table
  1022. * @param string old path
  1023. * @param string new path
  1024. */
  1025. function update_dir_name($path, $new_name) {
  1026. if (!empty($new_name)) {
  1027. global $base_work_dir;
  1028. include_once api_get_path(LIBRARY_PATH).'fileManage.lib.php';
  1029. include_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
  1030. $path_to_dir = dirname($path);
  1031. if ($path_to_dir == '.') {
  1032. $path_to_dir = '';
  1033. } else {
  1034. $path_to_dir .= '/';
  1035. }
  1036. $new_name = Security::remove_XSS($new_name);
  1037. $new_name = replace_dangerous_char($new_name);
  1038. $new_name = disable_dangerous_file($new_name);
  1039. my_rename($base_work_dir.'/'.$path, $new_name);
  1040. $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  1041. //update all the files in the other directories according with the next query
  1042. $sql = 'SELECT id, url FROM '.$table.' WHERE url LIKE BINARY "work/'.$path.'/%"'; // like binary (Case Sensitive)
  1043. $rs = Database::query($sql);
  1044. $work_len = strlen('work/'.$path);
  1045. while ($work = Database :: fetch_array($rs)) {
  1046. $new_dir = $work['url'];
  1047. $name_with_directory = substr($new_dir, $work_len, strlen($new_dir));
  1048. $sql = 'UPDATE '.$table.' SET url="work/'.$path_to_dir.$new_name.$name_with_directory.'" WHERE id= '.$work['id'];
  1049. Database::query($sql);
  1050. }
  1051. //update all the directory's children according with the next query
  1052. $sql = 'SELECT id, url FROM '.$table.' WHERE url LIKE BINARY "/'.$path.'%"';
  1053. $rs = Database::query($sql);
  1054. $work_len = strlen('/'.$path);
  1055. while ($work = Database :: fetch_array($rs)) {
  1056. $new_dir = $work['url'];
  1057. $name_with_directory = substr($new_dir, $work_len, strlen($new_dir));
  1058. $url = $path_to_dir.$new_name.$name_with_directory;
  1059. $sql = 'UPDATE '.$table.' SET url="/'.$url.'" WHERE id= '.$work['id'];
  1060. Database::query($sql);
  1061. }
  1062. }
  1063. }
  1064. /**
  1065. * Return an array with all the folder's ids that are in the given path
  1066. * @param string Path of the directory
  1067. * @return array The list of ids of all the directories in the path
  1068. * @author Julio Montoya Dokeos
  1069. * @version April 2008
  1070. */
  1071. function get_parent_directories($my_cur_dir_path) {
  1072. $list_id = array();
  1073. if (!empty($my_cur_dir_path)) {
  1074. $list_parents = explode('/', $my_cur_dir_path);
  1075. $dir_acum = '';
  1076. $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  1077. for ($i = 0; $i < count($list_parents) - 1; $i++) {
  1078. $item = Database::escape_string($list_parents[$i]);
  1079. $where_sentence = "url LIKE BINARY '" . $dir_acum . "/" . $item."'";
  1080. $dir_acum .= '/' . $list_parents[$i];
  1081. $sql = "SELECT id FROM ". $work_table . " WHERE ". $where_sentence;
  1082. $result = Database::query($sql);
  1083. $row = Database::fetch_array($result);
  1084. $list_id[] = $row['id'];
  1085. }
  1086. }
  1087. return $list_id;
  1088. }
  1089. /**
  1090. * Transform an all directory structure (only directories) in an array
  1091. * @param string path of the directory
  1092. * @return array the directory structure into an array
  1093. * @author Julio Montoya Dokeos
  1094. * @version April 2008
  1095. */
  1096. function directory_to_array($directory) {
  1097. $array_items = array();
  1098. if ($handle = @opendir($directory)) {
  1099. while (false !== ($file = readdir($handle))) {
  1100. if ($file != '.' && $file != '..') {
  1101. if (is_dir($directory. '/' . $file)) {
  1102. $array_items = array_merge($array_items, directory_to_array($directory. '/' . $file));
  1103. $file = $directory . '/' . $file;
  1104. $array_items[] = preg_replace("/\/\//si", '/', $file);
  1105. }
  1106. }
  1107. }
  1108. closedir($handle);
  1109. }
  1110. return $array_items;
  1111. }
  1112. /**
  1113. * Insert into the DB of the course all the directories
  1114. * @param string path of the /work directory of the course
  1115. * @return -1 on error, sql query result on success
  1116. * @author Julio Montoya Dokeos
  1117. * @version April 2008
  1118. */
  1119. function insert_all_directory_in_course_table($base_work_dir) {
  1120. $dir_to_array = directory_to_array($base_work_dir, true);
  1121. $only_dir = array();
  1122. for ($i = 0; $i < count($dir_to_array); $i++) {
  1123. $only_dir[] = substr($dir_to_array[$i], strlen($base_work_dir), strlen($dir_to_array[$i]));
  1124. }
  1125. for($i = 0; $i < count($only_dir); $i++) {
  1126. global $work_table;
  1127. $sql_insert_all= "INSERT INTO " . $work_table . " SET url = '" . $only_dir[$i] . "', " .
  1128. "title = '',
  1129. description = '',
  1130. author = '',
  1131. active = '0',
  1132. accepted = '1',
  1133. filetype = 'folder',
  1134. post_group_id = '".intval($_GET['toolgroup'])."',
  1135. sent_date = '0000-00-00 00:00:00' ";
  1136. Database::query($sql_insert_all);
  1137. }
  1138. }
  1139. /**
  1140. * This function displays the number of files contained in a directory
  1141. *
  1142. * @param string the path of the directory
  1143. * @param boolean true if we want the total quantity of files include in others child directorys , false only files in the directory
  1144. * @return array the first element is an integer with the number of files in the folder, the second element is the number of directories
  1145. * @author Julio Montoya Dokeos
  1146. * @version April 2008
  1147. */
  1148. function count_dir($path_dir, $recurse) {
  1149. $count = 0;
  1150. $count_dir = 0;
  1151. $d = dir($path_dir);
  1152. while ($entry = $d->Read()) {
  1153. if (!(($entry == '..') || ($entry == '.'))) {
  1154. if (is_dir($path_dir.'/'.$entry)) {
  1155. $count_dir++;
  1156. if ($recurse) {
  1157. $count += count_dir($path_dir . '/' . $entry, $recurse);
  1158. }
  1159. } else {
  1160. $count++;
  1161. }
  1162. }
  1163. }
  1164. $return_array = array();
  1165. $return_array[] = $count;
  1166. $return_array[] = $count_dir;
  1167. return $return_array;
  1168. }
  1169. /**
  1170. * returns all the javascript that is required for easily
  1171. * validation when you create a work
  1172. * this goes into the $htmlHeadXtra[] array
  1173. */
  1174. function to_javascript_work() {
  1175. return '<script>
  1176. function plus() {
  1177. if(document.getElementById(\'options\').style.display == \'none\') {
  1178. document.getElementById(\'options\').style.display = \'block\';
  1179. document.getElementById(\'plus\').innerHTML=\'&nbsp;'.Display::return_icon('div_hide.gif', get_lang('Hide', ''), array('style' => 'vertical-align:middle')).'&nbsp;'.addslashes(get_lang('AdvancedParameters', '')).'\';
  1180. } else {
  1181. document.getElementById(\'options\').style.display = \'none\';
  1182. document.getElementById(\'plus\').innerHTML=\'&nbsp;'.Display::return_icon('div_show.gif', get_lang('Show', ''), array('style' => 'vertical-align:middle')).'&nbsp;'.addslashes(get_lang('AdvancedParameters', '')).'\';
  1183. }
  1184. }
  1185. function updateDocumentTitle(value){
  1186. var temp = value.indexOf("/");
  1187. //linux path
  1188. if(temp!=-1){
  1189. var temp=value.split("/");
  1190. }
  1191. else{
  1192. var temp=value.split("\\\");
  1193. }
  1194. document.getElementById("file_upload").value=temp[temp.length-1];
  1195. }
  1196. function checkDate(month, day, year)
  1197. {
  1198. var monthLength =
  1199. new Array(31,28,31,30,31,30,31,31,30,31,30,31);
  1200. if (!day || !month || !year)
  1201. return false;
  1202. // check for bisestile year
  1203. if (year/4 == parseInt(year/4))
  1204. monthLength[1] = 29;
  1205. if (month < 1 || month > 12)
  1206. return false;
  1207. if (day > monthLength[month-1])
  1208. return false;
  1209. return true;
  1210. }
  1211. function mktime() {
  1212. var no, ma = 0, mb = 0, i = 0, d = new Date(), argv = arguments, argc = argv.length;
  1213. d.setHours(0,0,0); d.setDate(1); d.setMonth(1); d.setYear(1972);
  1214. var dateManip = {
  1215. 0: function(tt){ return d.setHours(tt); },
  1216. 1: function(tt){ return d.setMinutes(tt); },
  1217. 2: function(tt){ set = d.setSeconds(tt); mb = d.getDate() - 1; return set; },
  1218. 3: function(tt){ set = d.setMonth(parseInt(tt)-1); ma = d.getFullYear() - 1972; return set; },
  1219. 4: function(tt){ return d.setDate(tt+mb); },
  1220. 5: function(tt){ return d.setYear(tt+ma); }
  1221. };
  1222. for( i = 0; i < argc; i++ ){
  1223. no = parseInt(argv[i]*1);
  1224. if (isNaN(no)) {
  1225. return false;
  1226. } else {
  1227. // arg is number, lets manipulate date object
  1228. if(!dateManip[i](no)){
  1229. // failed
  1230. return false;
  1231. }
  1232. }
  1233. }
  1234. return Math.floor(d.getTime()/1000);
  1235. }
  1236. function validate(){
  1237. var expires_day = document.form1.expires_day.value;
  1238. var expires_month = document.form1.expires_month.value;
  1239. var expires_year = document.form1.expires_year.value;
  1240. var expires_hour = document.form1.expires_hour.value;
  1241. var expires_minute = document.form1.expires_minute.value;
  1242. var expires_date = mktime(expires_hour,expires_minute,0,expires_month,expires_day,expires_year)
  1243. var ends_day = document.form1.ends_day.value;
  1244. var ends_month = document.form1.ends_month.value;
  1245. var ends_year = document.form1.ends_year.value;
  1246. var ends_hour = document.form1.ends_hour.value;
  1247. var ends_minute = document.form1.ends_minute.value;
  1248. var ends_date = mktime(ends_hour,ends_minute,0,ends_month,ends_day,ends_year)
  1249. var new_dir = document.form1.new_dir.value;
  1250. msg_id1 = document.getElementById("msg_error1");
  1251. msg_id2 = document.getElementById("msg_error2");
  1252. msg_id3 = document.getElementById("msg_error3");
  1253. msg_id4 = document.getElementById("msg_error4");
  1254. msg_id5 = document.getElementById("msg_error_weight");
  1255. if(new_dir==""){
  1256. msg_id1.style.display ="block";
  1257. msg_id1.innerHTML="'.get_lang('FieldRequired', '').'";
  1258. msg_id2.innerHTML="";msg_id3.innerHTML="";msg_id4.innerHTML="";msg_id5.innerHTML="";
  1259. }
  1260. else if(document.form1.type1.checked && document.form1.type2.checked && expires_date > ends_date) {
  1261. msg_id2.style.display ="block";
  1262. msg_id2.innerHTML="'.get_lang('EndDateCannotBeBeforeTheExpireDate', '').'";
  1263. msg_id1.innerHTML="";msg_id3.innerHTML="";msg_id4.innerHTML="";msg_id5.innerHTML="";
  1264. }
  1265. else if (!checkDate(expires_month,expires_day,expires_year))
  1266. {
  1267. msg_id3.style.display ="block";
  1268. msg_id3.innerHTML="'.get_lang('InvalidDate', '').'";
  1269. msg_id1.innerHTML="";msg_id2.innerHTML="";msg_id4.innerHTML="";msg_id5.innerHTML="";
  1270. }
  1271. else if (!checkDate(ends_month,ends_day,ends_year))
  1272. {
  1273. msg_id4.style.display ="block";
  1274. msg_id4.innerHTML="'.get_lang('InvalidDate', '').'";
  1275. msg_id1.innerHTML="";msg_id2.innerHTML="";msg_id3.innerHTML="";msg_id5.innerHTML="";
  1276. }
  1277. else
  1278. {
  1279. if (document.form1.make_calification.checked)
  1280. {
  1281. var weight = document.form1.weight.value;
  1282. if(weight==""){
  1283. msg_id5.style.display ="block";
  1284. msg_id5.innerHTML="'.get_lang('WeightNecessary', '').'";
  1285. msg_id1.innerHTML="";msg_id2.innerHTML="";msg_id3.innerHTML="";msg_id4.innerHTML="";
  1286. return false;
  1287. }
  1288. }
  1289. document.form1.action = "work.php?origin='.api_get_tools_lists($_REQUEST['origin']).'&gradebook='.(empty($_GET['gradebook'])?'':'view').'";
  1290. document.form1.submit();
  1291. }
  1292. }
  1293. </script>';
  1294. }
  1295. /**
  1296. * Gets the id of a student publication with a given path
  1297. * @param string $path
  1298. * @return true if is found / false if not found
  1299. */
  1300. // TODO: The name of this function does not fit with the kind of information it returns. Maybe check_work_id() or is_work_id()?
  1301. function get_work_id($path) {
  1302. $TBL_STUDENT_PUBLICATION = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
  1303. $TBL_PROP_TABLE = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1304. if (api_is_allowed_to_edit()) {
  1305. $sql = "SELECT work.id FROM $TBL_STUDENT_PUBLICATION AS work,$TBL_PROP_TABLE AS props WHERE props.tool='work' AND work.id=props.ref AND work.url LIKE 'work/".$path."%' AND work.filetype='file' AND props.visibility<>'2'";
  1306. } else {
  1307. $sql = "SELECT work.id FROM $TBL_STUDENT_PUBLICATION AS work,$TBL_PROP_TABLE AS props WHERE props.tool='work' AND work.id=props.ref AND work.url LIKE 'work/".$path."%' AND work.filetype='file' AND props.visibility<>'2' AND props.lastedit_user_id='".api_get_user_id()."'";
  1308. }
  1309. $result = Database::query($sql);
  1310. $num_rows = Database::num_rows($result);
  1311. if ($result && $num_rows > 0) {
  1312. return true;
  1313. } else {
  1314. return false;
  1315. }
  1316. }
  1317. /**
  1318. * Get list of users who have not given the task
  1319. * @param int
  1320. * @return array
  1321. * @author cvargas carlos.vargas@beeznest.com
  1322. */
  1323. function get_list_users_without_publication($task_id) {
  1324. $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  1325. $iprop_table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1326. $table_course_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  1327. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  1328. $session_course_rel_user = Database :: get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  1329. //condition for the session
  1330. $session_id = api_get_session_id();
  1331. if ($session_id == 0){
  1332. $sql = "SELECT user_id as id FROM $work_table WHERE parent_id='$task_id'";
  1333. } else {
  1334. $sql = "SELECT user_id as id FROM $work_table WHERE parent_id='$task_id' and session_id='".$session_id."'";
  1335. }
  1336. $result = Database::query($sql);
  1337. $users_with_tasks = array();
  1338. while($row = Database::fetch_array($result)) {
  1339. $users_with_tasks[] = $row['id'];
  1340. }
  1341. if ($session_id == 0){
  1342. $sql_users = "SELECT cu.user_id, u.lastname, u.firstname, u.email FROM $table_course_user AS cu, $table_user AS u WHERE u.status!=1 and cu.course_code='".api_get_course_id()."' AND u.user_id=cu.user_id";
  1343. } else {
  1344. $sql_users = "SELECT cu.id_user, u.lastname, u.firstname, u.email FROM $session_course_rel_user AS cu, $table_user AS u WHERE u.status!=1 and cu.course_code='".api_get_course_id()."' AND u.user_id=cu.id_user and cu.id_session='".$session_id."'";
  1345. }
  1346. $result_users = Database::query($sql_users);
  1347. $users_without_tasks = array();
  1348. while ($row_users = Database::fetch_row($result_users)) {
  1349. if (in_array($row_users[0], $users_with_tasks)) continue;
  1350. $user_id = array_shift($row_users);
  1351. $row_users[3] = $row_users[2];
  1352. $row_users[2] = Display::encrypted_mailto_link($row_users[2], $row_users[2]);
  1353. $users_without_tasks[] = $row_users;
  1354. }
  1355. return $users_without_tasks;
  1356. }
  1357. /**
  1358. * Display list of users who have not given the task
  1359. *
  1360. * @param int task id
  1361. * @return array
  1362. * @author cvargas carlos.vargas@beeznest.com cfasanando, christian.fasanado@beeznest.com
  1363. * @author Julio Montoya <gugli100@gmail.com> Fixes
  1364. */
  1365. function display_list_users_without_publication($task_id) {
  1366. global $origin;
  1367. $table_header[] = array(get_lang('LastName'), true);
  1368. $table_header[] = array(get_lang('FirstName'), true);
  1369. $table_header[] = array(get_lang('Email'), true);
  1370. // table_data
  1371. $table_data = get_list_users_without_publication($task_id);
  1372. $sorting_options = array();
  1373. $sorting_options['column'] = 1;
  1374. $paging_options = array();
  1375. $my_params = array();
  1376. if (isset($_GET['curdirpath'])) {
  1377. $my_params['curdirpath'] = Security::remove_XSS($_GET['curdirpath']);
  1378. }
  1379. if (isset($_GET['edit_dir'])) {
  1380. $my_params['edit_dir'] = Security::remove_XSS($_GET['edit_dir']);
  1381. }
  1382. if (isset($_GET['list'])) {
  1383. $my_params['list'] = Security::remove_XSS($_GET['list']);
  1384. }
  1385. $my_params['origin'] = $origin;
  1386. //$column_show
  1387. $column_show[] = 1;
  1388. $column_show[] = 1;
  1389. $column_show[] = 1;
  1390. Display::display_sortable_config_table('work', $table_header, $table_data, $sorting_options, $paging_options, $my_params, $column_show);
  1391. }
  1392. /**
  1393. * Send reminder to users who have not given the task
  1394. *
  1395. * @param int
  1396. * @return array
  1397. * @author cvargas carlos.vargas@beeznest.com cfasanando, christian.fasanado@beeznest.com
  1398. */
  1399. function send_reminder_users_without_publication($task_id) {
  1400. global $_course, $my_cur_dir_path, $currentUserFirstName, $currentUserLastName, $currentUserEmail;
  1401. $emailsubject = '[' . api_get_setting('siteName') . '] ';
  1402. $sender_name = api_get_person_name($currentUserFirstName, $currentUserLastName, null, PERSON_NAME_EMAIL_ADDRESS);
  1403. $email_admin = $currentUserEmail;
  1404. // The body can be as long as you wish, and any combination of text and variables
  1405. $emailbody_user .= get_lang('ReminderToSubmitPendingTask')."\n".get_lang('CourseName').' : '.$_course['name']."\n";
  1406. $emailbody_user .= get_lang('WorkName').' : '.substr($my_cur_dir_path, 0, -1)."\n\n".get_lang('Teacher').' : '.api_get_person_name($currentUserFirstName, $currentUserLastName)."\n".get_lang('Email').' : '.$currentUserEmail;
  1407. $list_users = get_list_users_without_publication($task_id);
  1408. foreach ($list_users as $user) {
  1409. $name_user = api_get_person_name($user[0], $user[1], null, PERSON_NAME_EMAIL_ADDRESS);
  1410. @api_mail($name_user, $user[3], $emailsubject, $emailbody_user, $sender_name, $email_admin);
  1411. }
  1412. }
  1413. /**
  1414. * Sends an email to the students of a course when a homework is created
  1415. *
  1416. * @param string course_id
  1417. *
  1418. * @author Guillaume Viguier <guillaume.viguier@beeznest.com>
  1419. * @author Julio Montoya <gugli100@gmail.com> Adding session support - 2011
  1420. */
  1421. function send_email_on_homework_creation($course_id) {
  1422. require_once api_get_path(LIBRARY_PATH).'course.lib.php';
  1423. // Get the students of the course
  1424. $session_id = api_get_session_id();
  1425. if (empty($session_id)) {
  1426. $students = CourseManager::get_student_list_from_course_code($course_id);
  1427. } else {
  1428. $students = CourseManager::get_student_list_from_course_code($course_id, true, $session_id);
  1429. }
  1430. $emailsubject = '[' . api_get_setting('siteName') . '] '.get_lang('HomeworkCreated');
  1431. $currentUser = api_get_user_info(api_get_user_id());
  1432. if (!empty($students)) {
  1433. foreach($students as $student) {
  1434. $user_info = api_get_user_info($student["user_id"]);
  1435. if(!empty($user_info["mail"])) {
  1436. $name_user = api_get_person_name($user_info["firstname"], $user_info["lastname"], null, PERSON_NAME_EMAIL_ADDRESS);
  1437. $emailbody = get_lang('Dear')." ".$name_user.",\n\n";
  1438. $emailbody .= get_lang('HomeworkHasBeenCreatedForTheCourse')." ".$course_id.". "."\n\n".get_lang('PleaseCheckHomeworkPage');
  1439. $emailbody .= "\n\n".api_get_person_name($currentUser["firstname"], $currentUser["lastname"]);
  1440. @api_mail($name_user, $user_info["mail"], $emailsubject, $emailbody, api_get_person_name($currentUser["firstname"], $currentUser["lastname"], null, PERSON_NAME_EMAIL_ADDRESS), $currentUser["mail"]);
  1441. }
  1442. }
  1443. }
  1444. }
  1445. function is_work_exist_by_url($url) {
  1446. $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  1447. $url = Database::escape_string($url);
  1448. $sql = "SELECT id FROM $work_table WHERE url='$url'";
  1449. $result = Database::query($sql);
  1450. if (Database::num_rows($result)> 0) {
  1451. $row = Database::fetch_row($result);
  1452. if (empty($row)) {
  1453. return false;
  1454. } else {
  1455. return true;
  1456. }
  1457. } else {
  1458. return false;
  1459. }
  1460. }
  1461. function make_select($name, $values, $checked = '') {
  1462. $output = '<select name="'.$name.'" id="'.$name.'">';
  1463. foreach($values as $key => $value) {
  1464. $output .= '<option value="'.$key.'" '.(($checked==$key) ? 'selected="selected"' : '').'>'.$value.'</option>';
  1465. }
  1466. $output .= '</select>';
  1467. return $output;
  1468. }
  1469. function make_checkbox($name, $checked = '') {
  1470. return '<input type="checkbox" value="1" name="'.$name.'" '.((!empty($checked))?'checked="checked"':'').'/>';
  1471. }
  1472. function draw_date_picker($prefix, $default = '') {
  1473. //$default = 2008-10-01 10:00:00
  1474. if (empty($default)) {
  1475. //$default = date('Y-m-d H:i:s');
  1476. $default = api_get_local_time();
  1477. }
  1478. $parts = split(' ', $default);
  1479. list($d_year, $d_month, $d_day) = split('-', $parts[0]);
  1480. list($d_hour, $d_minute) = split(':', $parts[1]);
  1481. $minute = range(10, 59);
  1482. array_unshift($minute, '00', '01', '02', '03', '04', '05', '06', '07', '08', '09');
  1483. $date_form = make_select($prefix.'_day', array_combine(range(1, 31), range(1, 31)), $d_day);
  1484. $date_form .= make_select($prefix.'_month', array_combine(range(1, 12), api_get_months_long()), $d_month);
  1485. $date_form .= make_select($prefix.'_year', array($d_year => $d_year, $d_year + 1 => $d_year + 1), $d_year).'&nbsp;&nbsp;&nbsp;&nbsp;';
  1486. $date_form .= make_select($prefix.'_hour', array_combine(range(0, 23), range(0, 23)), $d_hour).' : ';
  1487. $date_form .= make_select($prefix.'_minute', $minute, $d_minute);
  1488. return $date_form;
  1489. }
  1490. function get_date_from_select($prefix) {
  1491. return $_POST[$prefix.'_year'].'-'.two_digits($_POST[$prefix.'_month']).'-'.two_digits($_POST[$prefix.'_day']).' '.two_digits($_POST[$prefix.'_hour']).':'.two_digits($_POST[$prefix.'_minute']).':00';
  1492. }