work.lib.php 75 KB

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