work.lib.php 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849
  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;
  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. $_SESSION['token'] = time();
  76. $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='.$_SESSION['token'].'">'.
  77. Display::return_icon('mail_send.png', get_lang('ReminderMessage'),'','32')."</a>";
  78. }
  79. }
  80. }
  81. if ($display_output != '') {
  82. echo '<div class="actions">';
  83. echo $display_output;
  84. echo '</div>';
  85. }
  86. }
  87. /**
  88. * Displays all options for this tool.
  89. * These are
  90. * - make all files visible / invisible
  91. * - set the default visibility of uploaded files
  92. *
  93. * @param $uploadvisibledisabled
  94. * @param $origin
  95. */
  96. function display_tool_options($uploadvisibledisabled, $origin) {
  97. global $group_properties, $gradebook;
  98. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  99. $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  100. if (!$is_allowed_to_edit) {
  101. return;
  102. }
  103. echo '<form method="post" action="'.api_get_self().'?origin='.$origin.'&gradebook='.$gradebook.'&action=settings">';
  104. echo '<div class="row"><div class="form_header">'.get_lang('EditToolOptions').'</div></div>';
  105. display_default_visibility_form($uploadvisibledisabled);
  106. display_studentsdelete_form();
  107. echo '<div class="row">
  108. <div class="formw">
  109. <button type="submit" class="save" name="changeProperties" value="'.get_lang('Ok').'">'.get_lang('Ok').'</button>
  110. </div>
  111. </div>';
  112. echo '</form>';
  113. }
  114. /**
  115. * Displays the form where course admins can specify wether uploaded documents
  116. * are visible or invisible by default.
  117. *
  118. * @param $uploadvisibledisabled
  119. * @param $origin
  120. */
  121. function display_default_visibility_form($uploadvisibledisabled) {
  122. ?>
  123. <div class="row">
  124. <div class="label">
  125. <?php echo get_lang('_default_upload'); ?>
  126. </div>
  127. <div class="formw">
  128. <input id="uploadvisibledisabled_1" class="checkbox" type="radio" name="uploadvisibledisabled" value="0" <?php if ($uploadvisibledisabled == 0) echo 'checked'; ?> />
  129. <label for="uploadvisibledisabled_1">
  130. <?php echo get_lang('_new_visible'); ?></label>
  131. <div></div>
  132. <input id="uploadvisibledisabled_2" class="checkbox" type="radio" name="uploadvisibledisabled" value="1" <?php if ($uploadvisibledisabled == 1) echo 'checked'; ?> />
  133. <label for="uploadvisibledisabled_2">
  134. <?php echo get_lang('_new_unvisible'); ?>
  135. </label>
  136. </div>
  137. </div>
  138. <?php
  139. }
  140. /**
  141. * Display a part of the form to edit the settings of the tool
  142. * In this case weither the students are allowed to delete their own publication or not (by default not)
  143. *
  144. * @return html code
  145. * @since Dokeos 1.8.6.2
  146. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  147. */
  148. function display_studentsdelete_form() {
  149. // by default api_get_course_setting returns -1 and the code only expects 0 or 1 so anything tha
  150. // is different than 1 will be converted into 0
  151. $current_course_setting_value = api_get_course_setting('student_delete_own_publication');
  152. if ($current_course_setting_value != 1) {
  153. $current_course_setting_value = 0;
  154. }
  155. ?>
  156. <div class="row">
  157. <div class="label"><?php echo get_lang('StudentAllowedToDeleteOwnPublication'); ?></div>
  158. <div class="formw">
  159. <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'; ?> />
  160. <label for="student_delete_own_publication_2">
  161. <?php echo get_lang('Yes'); ?>
  162. </label>
  163. <div></div>
  164. <input id="student_delete_own_publication_1" class="checkbox" type="radio" name="student_delete_own_publication" value="0"
  165. <?php if ($current_course_setting_value == 0) echo 'checked'; ?> />
  166. <label for="student_delete_own_publication_1">
  167. <?php echo get_lang('No'); ?>
  168. </label>
  169. </div>
  170. </div>
  171. <?php
  172. }
  173. /**
  174. * converts 2008-10-06 12:45:00 to timestamp
  175. * @deprecated any calls found
  176. */
  177. function convert_date_to_number($default) {
  178. // 2008-10-12 00:00:00 ---to--> 12345672218 (timestamp)
  179. $parts = split(' ', $default);
  180. list($d_year, $d_month, $d_day) = split('-', $parts[0]);
  181. list($d_hour, $d_minute, $d_second) = split(':', $parts[1]);
  182. return mktime((int)$d_hour, (int)$d_minute, (int)$d_second, (int)$d_month, (int)$d_day, (int)$d_year);
  183. }
  184. /**
  185. * converts 1-9 to 01-09
  186. */
  187. function two_digits($number) {
  188. $number = (int)$number;
  189. return ($number < 10) ? '0'.$number : $number;
  190. }
  191. /**
  192. * converts 2008-10-06 12:45:00 to -> array($data'year'=>2008,$data'month'=>10 etc...)
  193. */
  194. function convert_date_to_array($date, $group) {
  195. $parts = split(' ', $date);
  196. $date_parts = split('-', $parts[0]);
  197. $date_parts_tmp = array();
  198. foreach ($date_parts as $item) {
  199. $date_parts_tmp[] = intval($item);
  200. }
  201. $time_parts = split(':', $parts[1]);
  202. $time_parts_tmp = array();
  203. foreach ($time_parts as $item) {
  204. $time_parts_tmp[] = intval($item);
  205. }
  206. list($data[$group.'[year]'], $data[$group.'[month]'], $data[$group.'[day]']) = $date_parts_tmp;
  207. list($data[$group.'[hour]'], $data[$group.'[minute]']) = $time_parts_tmp;
  208. return $data;
  209. }
  210. /**
  211. * get date from a group of date
  212. */
  213. function get_date_from_group($group) {
  214. 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';
  215. }
  216. /**
  217. * create a group of select from a date
  218. */
  219. function create_group_date_select($prefix = '') {
  220. $minute = range(10, 59);
  221. $d_year = date('Y');
  222. array_unshift($minute, '00', '01', '02', '03', '04', '05', '06', '07', '08', '09');
  223. $group_name[] = FormValidator :: createElement('select', $prefix.'day', '', array_combine(range(1, 31), range(1, 31)));
  224. $group_name[] = FormValidator :: createElement('select', $prefix.'month', '', array_combine(range(1, 12), api_get_months_long()));
  225. $group_name[] = FormValidator :: createElement('select', $prefix.'year', '', array($d_year => $d_year, $d_year + 1 => $d_year + 1));
  226. $group_name[] = FormValidator :: createElement('select', $prefix.'hour', '', array_combine(range(0, 23), range(0, 23)));
  227. $group_name[] = FormValidator :: createElement('select', $prefix.'minute', '', $minute);
  228. return $group_name;
  229. }
  230. function get_work_data_by_path($path) {
  231. $path = Database::escape_string($path);
  232. $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  233. $sql = "SELECT * FROM ".$work_table." WHERE url = '$path'";
  234. $result = Database::query($sql);
  235. $return = array();
  236. if (Database::num_rows($result)) {
  237. $return = Database::fetch_array($result,'ASSOC');
  238. }
  239. return $return;
  240. }
  241. function get_work_data_by_id($id) {
  242. $id = intval($id);
  243. $course_id = api_get_course_int_id();
  244. $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  245. $sql = "SELECT * FROM ".$work_table." WHERE id = $id AND c_id = $course_id";
  246. $result = Database::query($sql);
  247. $return = array();
  248. if (Database::num_rows($result)) {
  249. $return = Database::fetch_array($result,'ASSOC');
  250. }
  251. return $return;
  252. }
  253. function get_work_count_by_student($user_id, $work_id) {
  254. $user_id = intval($user_id);
  255. $work_id = intval($work_id);
  256. $course_id = api_get_course_id();
  257. $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  258. $sql = "SELECT COUNT(*) as count FROM $work_table WHERE parent_id = $work_id AND user_id = $user_id AND active = 1 ";
  259. $result = Database::query($sql);
  260. $return = 0;
  261. if (Database::num_rows($result)) {
  262. $return = Database::fetch_row($result,'ASSOC');
  263. $return = intval($return[0]);
  264. }
  265. return $return;
  266. }
  267. function get_work_assignment_by_id($id) {
  268. $id = intval($id);
  269. $course_id = api_get_course_int_id();
  270. $table = Database :: get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT);
  271. $sql = "SELECT * FROM ".$table." WHERE c_id = $course_id AND publication_id = $id";
  272. $result = Database::query($sql);
  273. $return = array();
  274. if (Database::num_rows($result)) {
  275. $return = Database::fetch_array($result,'ASSOC');
  276. }
  277. return $return;
  278. }
  279. /**
  280. * Display the list of student publications, taking into account the user status
  281. *
  282. * @param $link_target_parameter - should there be a target parameter for the links
  283. * @param $dateFormatLong - date format
  284. * @param $origin - typically empty or 'learnpath'
  285. */
  286. function display_student_publications_list($id, $link_target_parameter, $dateFormatLong, $origin, $add_in_where_query = '') {
  287. global $timeNoSecFormat, $dateFormatShort, $gradebook, $_course;
  288. // Database table names
  289. $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  290. $iprop_table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  291. $work_assigment = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT);
  292. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  293. $user_id = api_get_user_id();
  294. //condition for the session
  295. $session_id = api_get_session_id();
  296. $condition_session = api_get_session_condition($session_id);
  297. $course_id = api_get_course_int_id();
  298. $publications_list = array();
  299. $sort_params = array();
  300. if (isset($_GET['column'])) {
  301. $sort_params[] = 'column='.Security::remove_XSS($_GET['column']);
  302. }
  303. if (isset($_GET['page_nr'])) {
  304. $sort_params[] = 'page_nr='.Security::remove_XSS($_GET['page_nr']);
  305. }
  306. if (isset($_GET['per_page'])) {
  307. $sort_params[] = 'per_page='.Security::remove_XSS($_GET['per_page']);
  308. }
  309. if (isset($_GET['direction'])) {
  310. $sort_params[] = 'direction='.Security::remove_XSS($_GET['direction']);
  311. }
  312. $sort_params = implode('&amp;', $sort_params);
  313. $my_params = $sort_params;
  314. $origin = Security::remove_XSS($origin);
  315. // Getting the work data
  316. $my_folder_data = get_work_data_by_id($id);
  317. $qualification_exists = false;
  318. if (!empty($my_folder_data['qualification']) && intval($my_folder_data['qualification']) > 0) {
  319. $qualification_exists = true;
  320. }
  321. $work_dir = api_get_path(SYS_COURSE_PATH).$_course['path'].'/work';
  322. if (!empty($my_folder_data)) {
  323. $work_dir = api_get_path(SYS_COURSE_PATH).$_course['path'].'/work'.$my_folder_data['url'];
  324. }
  325. if (empty($my_folder_data)) {
  326. $work_in_gradebook_link_id = is_resource_in_course_gradebook(api_get_course_id(), 3 , $id, api_get_session_id());
  327. if ($work_in_gradebook_link_id) {
  328. if ($is_allowed_to_edit)
  329. if (intval($my_folder_data['qualification']) == 0) {
  330. Display::display_warning_message(get_lang('MaxWeightNeedToBeProvided'));
  331. }
  332. }
  333. $contains_file_query = '';
  334. //Get list from database
  335. if ($is_allowed_to_edit) {
  336. $active_condition = ' active IN (0, 1)';
  337. $sql_get_publications_list = "SELECT * FROM $work_table
  338. WHERE c_id = $course_id $add_in_where_query $condition_session AND $active_condition AND
  339. ( parent_id = 0)
  340. $contains_file_query
  341. ORDER BY sent_date DESC";
  342. } else {
  343. if (!empty($_SESSION['toolgroup'])) {
  344. $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
  345. $subdirs_query = "AND parent_id = 0";
  346. } else {
  347. $group_query = " WHERE c_id = $course_id AND post_group_id = '0' ";
  348. $subdirs_query = "AND parent_id = 0";
  349. }
  350. //@todo how we can active or not an assignment?
  351. $active_condition = ' AND active IN (1,0)';
  352. $sql_get_publications_list = "SELECT * FROM $work_table $group_query $subdirs_query $add_in_where_query $active_condition $condition_session ORDER BY title";
  353. }
  354. $work_parents = array();
  355. $sql_result = Database::query($sql_get_publications_list);
  356. if (Database::num_rows($sql_result)) {
  357. while ($work = Database::fetch_object($sql_result)) {
  358. if ($work->parent_id == 0) {
  359. $work_parents[] = $work;
  360. }
  361. }
  362. }
  363. } else {
  364. $parent_id = isset($my_folder_data['id']) ? $my_folder_data['id'] : 0;
  365. if (!empty($_SESSION['toolgroup'])) {
  366. $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
  367. } else {
  368. $group_query = " WHERE c_id = $course_id AND post_group_id = '0' ";
  369. }
  370. $subdirs_query = "AND parent_id = $parent_id ";
  371. if ($is_allowed_to_edit) {
  372. //$subdirs_query .= " AND user_id = ".api_get_user_id()." ";
  373. } else {
  374. $subdirs_query .= " AND user_id = ".api_get_user_id()." ";
  375. }
  376. $active_condition = ' AND active IN (1)';
  377. $sql_get_publications_list = "SELECT * FROM $work_table $group_query $subdirs_query $add_in_where_query $active_condition $condition_session ORDER BY title";
  378. $sql_result = Database::query($sql_get_publications_list);
  379. }
  380. $edit_dir = isset($_GET['edit_dir']) ? $_GET['edit_dir'] : '';
  381. $table_header = array();
  382. $table_has_actions_column = false;
  383. $table_header[] = array(get_lang('Type'), false, 'style="width:40px"');
  384. $table_header[] = array(get_lang('Title'), true);
  385. if (!empty($id)) {
  386. $table_header[] = array(get_lang('FirstName'), true);
  387. $table_header[] = array(get_lang('LastName'), true);
  388. if ($qualification_exists) {
  389. $table_header[] = array(get_lang('Qualification'), true);
  390. }
  391. }
  392. $table_header[] = array(get_lang('Date'), true, 'style="width:180px"');
  393. if ($is_allowed_to_edit) {
  394. $table_header[] = array(get_lang('Actions'), false, 'style="width:90px"');
  395. $table_has_actions_column = true;
  396. }
  397. // the following column name seems both undefined and unused
  398. //$table_header[] = array('RealDate', true);
  399. $table_data = array();
  400. // List of all folders if no id was provided
  401. if (is_array($work_parents)) {
  402. foreach ($work_parents as $work_parent) {
  403. $sql_select_directory = "SELECT title, prop.insert_date, prop.lastedit_date, work.id, author, has_properties, view_properties, description, qualification, weight, allow_text_assignment
  404. FROM ".$iprop_table." prop INNER JOIN ".$work_table." work ON (prop.ref=work.id)
  405. WHERE active IN (0, 1) AND ";
  406. if (!empty($_SESSION['toolgroup'])) {
  407. $sql_select_directory .= " work.post_group_id = '".$_SESSION['toolgroup']."' "; // set to select only messages posted by the user's group
  408. } else {
  409. $sql_select_directory .= " work.post_group_id = '0' ";
  410. }
  411. $sql_select_directory .= " AND prop.c_id = $course_id AND
  412. work.c_id = $course_id AND
  413. work.id = ".$work_parent->id." AND
  414. work.filetype = 'folder' AND
  415. prop.tool='work' $condition_session";
  416. $result = Database::query($sql_select_directory);
  417. $row = Database::fetch_array($result, 'ASSOC');
  418. if (!$row) {
  419. // the folder belongs to another session
  420. continue;
  421. }
  422. $direc_date = $row['lastedit_date']; //directory's date
  423. $author = $row['author']; //directory's author
  424. $view_properties = $row['view_properties'];
  425. $is_assignment = $row['has_properties'];
  426. $id2 = $row['id']; //work id
  427. if ($is_allowed_to_edit) {
  428. // form edit directory
  429. if (!empty($edit_dir) && $edit_dir == $id2) {
  430. if (!empty($row['has_properties'])) {
  431. $sql = Database::query('SELECT * FROM '.$work_assigment.' WHERE c_id = '.$course_id.' AND id = "'.$row['has_properties'].'" LIMIT 1');
  432. $homework = Database::fetch_array($sql);
  433. }
  434. $form_folder = new FormValidator('edit_dir', 'post', api_get_self().'?origin='.$origin.'&gradebook='.$gradebook.'&edit_dir='.$id2);
  435. $group_name[] = FormValidator :: createElement('text', 'dir_name');
  436. $form_folder -> addGroup($group_name, 'my_group', get_lang('Title'));
  437. $form_folder->addElement('hidden', 'work_id', $id2);
  438. $form_folder -> addGroupRule('my_group', get_lang('ThisFieldIsRequired'), 'required');
  439. $defaults = array('my_group[dir_name]' => Security::remove_XSS($row['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) {
  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. }
  753. if ($work->contains_file) {
  754. $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>';
  755. }
  756. if ($work->accepted == '1') {
  757. $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>';
  758. } else {
  759. $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> ';
  760. }
  761. $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>';
  762. $row[] = $action;
  763. // the user that is not course admin can only edit/delete own document
  764. } elseif ($is_author && empty($work->qualificator_id)) {
  765. if (!$table_has_actions_column) {
  766. $table_header[] = array(get_lang('Actions'), false, 'style="width:90px"');
  767. $table_has_actions_column = true;
  768. }
  769. $action = '';
  770. if (api_is_allowed_to_session_edit(false, true)) {
  771. $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>';
  772. } else {
  773. $action .= Display::return_icon('edit_na.png', get_lang('Modify'),array(), 22);
  774. }
  775. if (api_get_course_setting('student_delete_own_publication') == 1) {
  776. $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>';
  777. }
  778. $row[] = $action;
  779. } else {
  780. $row[] = ' ';
  781. }
  782. $row[] = $work_sent_date_local;
  783. $table_data[] = $row;
  784. }
  785. }
  786. }
  787. $sorting_options = array();
  788. $sorting_options['column'] = 1;
  789. // Here we change the way how the colums are going to be sorted
  790. // in this case the the column of LastResent ( 4th element in $column_header) we will be order like the column RealDate
  791. // because in the column RealDate we have the days in a correct format "2008-03-12 10:35:48"
  792. $column_order = array();
  793. $i=0;
  794. foreach($table_header as $item) {
  795. $column_order[$i] = $i;
  796. $i++;
  797. }
  798. if (empty($my_folder_data)) {
  799. $column_order[1] = 5;
  800. } else {
  801. $column_order[2] = 2;
  802. }
  803. // An array with the setting of the columns -> 1: columns that we will show, 0:columns that will be hide
  804. $column_show = array();
  805. $column_show[] = 1; // type 0
  806. $column_show[] = 1; // title 1
  807. if (!empty($my_folder_data)) {
  808. $column_show[] = 1; // 2
  809. $column_show[] = 1; // 3
  810. if ($qualification_exists) {
  811. $column_show[] = 1; // 4
  812. }
  813. }
  814. $column_show[] = 1; //date
  815. if ($table_has_actions_column) {
  816. $column_show[] = 1; // modify
  817. }
  818. $column_show[] = 0; //real date in correct format
  819. $paging_options = array();
  820. if (isset($_GET['curdirpath'])) {
  821. $my_params = array ('curdirpath' => Security::remove_XSS($_GET['curdirpath']));
  822. }
  823. $my_params = array ('id' => isset($_GET['id']) ? $_GET['id'] : null);
  824. if (isset($_GET['edit_dir'])) {
  825. $my_params = array ('edit_dir' => intval($_GET['edit_dir']));
  826. }
  827. $my_params['origin'] = $origin;
  828. Display::display_sortable_config_table('work', $table_header, $table_data, $sorting_options, $paging_options, $my_params, $column_show, $column_order);
  829. }
  830. function get_work_list() {
  831. }
  832. /**
  833. * Returns a list of subdirectories found in the given directory.
  834. *
  835. * The list return starts from the given base directory.
  836. * If you require the subdirs of /var/www/ (or /var/www), you will get 'abc/', 'def/', but not '/var/www/abc/'...
  837. * @param string Base dir
  838. * @param integer 0 if we only want dirs from this level, 1 if we want to recurse into subdirs
  839. * @return strings_array The list of subdirs in 'abc/' form, -1 on error, and 0 if none found
  840. * @todo Add a session check to see if subdirs_list doesn't exist yet (cached copy)
  841. */
  842. function get_subdirs_list($basedir = '', $recurse = 0) {
  843. //echo "Looking for subdirs of $basedir";
  844. if (empty($basedir) or !is_dir($basedir)) {
  845. return -1;
  846. }
  847. if (substr($basedir, -1, 1) != '/') {
  848. $basedir = $basedir.'/';
  849. }
  850. $dirs_list = array();
  851. $dh = opendir($basedir);
  852. while ($entry = readdir($dh)) {
  853. $entry = replace_dangerous_char($entry);
  854. $entry = disable_dangerous_file($entry);
  855. if (is_dir($basedir.$entry) && $entry != '..' && $entry != '.') {
  856. $dirs_list[] = $entry;
  857. if ($recurse == 1) {
  858. foreach (get_subdirs_list($basedir.$entry) as $subdir) {
  859. $dirs_list[] = $entry.'/'.$subdir;
  860. }
  861. }
  862. }
  863. }
  864. closedir($dh);
  865. return $dirs_list;
  866. }
  867. /**
  868. * Builds the form thats enables the user to
  869. * select a directory to browse/upload in
  870. * This function has been copied from the document/document.inc.php library
  871. *
  872. * @param array $folders
  873. * @param string $curdirpath
  874. * @param string $group_dir
  875. * @return string html form
  876. */
  877. // TODO: This function is a candidate for removal, it is not used anywhere.
  878. function build_work_directory_selector($folders, $curdirpath, $group_dir = '') {
  879. $form = '<form name="selector" action="'.api_get_self().'?'.api_get_cidreq().'" method="POST">'."\n";
  880. $form .= get_lang('CurrentDirectory').' <select name="curdirpath" onchange="javascript: document.selector.submit();">'."\n";
  881. //group documents cannot be uploaded in the root
  882. if ($group_dir == '') {
  883. $form .= '<option value="/">/ ('.get_lang('Root').')</option>';
  884. if (is_array($folders)) {
  885. foreach ($folders as $folder) {
  886. $selected = ($curdirpath == $folder) ? ' selected="selected"' : '';
  887. $form .= '<option'.$selected.' value="'.$folder.'">'.$folder.'</option>'."\n";
  888. }
  889. }
  890. } else {
  891. foreach ($folders as $folder) {
  892. $selected = ($curdirpath == $folder) ? ' selected="selected"' : '';
  893. $display_folder = substr($folder, strlen($group_dir));
  894. $display_folder = ($display_folder == '') ? '/ ('.get_lang('Root').')' : $display_folder;
  895. $form .= '<option'.$selected.' value="'.$folder.'">'.$display_folder.'</option>'."\n";
  896. }
  897. }
  898. $form .= '</select>'."\n";
  899. $form .= '<noscript><input type="submit" name="change_path" value="'.get_lang('Ok').'" /></noscript>'."\n";
  900. $form .= '</form>';
  901. return $form;
  902. }
  903. /**
  904. * Builds the form thats enables the user to
  905. * move a document from one directory to another
  906. * This function has been copied from the document/document.inc.php library
  907. *
  908. * @param array $folders
  909. * @param string $curdirpath
  910. * @param string $move_file
  911. * @return string html form
  912. */
  913. function build_work_move_to_selector($folders, $curdirpath, $move_file, $group_dir = '') {
  914. $course_id = api_get_course_int_id();
  915. $move_file = intval($move_file);
  916. $tbl_work = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  917. $sql = "SELECT title FROM $tbl_work WHERE c_id = $course_id AND id ='".$move_file."'";
  918. $result = Database::query($sql);
  919. $title = Database::fetch_row($result);
  920. global $gradebook;
  921. $form = '<form name="move_to_form" action="'.api_get_self().'?gradebook='.$gradebook.'&curdirpath='.Security::remove_XSS($curdirpath).'" method="POST">';
  922. $form .= '<div class="row"><div class="form_header">'.get_lang('MoveFile').' - '.Security::remove_XSS($title[0]).'</div></div>';
  923. $form .= '<input type="hidden" name="item_id" value="'.$move_file.'" />';
  924. $form .= '<input type="hidden" name="action" value="move_to" />';
  925. $form .= '<div class="row">
  926. <div class="label">
  927. <span class="form_required">*</span>'.get_lang('Select').'
  928. </div>
  929. <div class="formw">';
  930. $form .= ' <select name="move_to_id">';
  931. //group documents cannot be uploaded in the root
  932. if ($group_dir == '') {
  933. if ($curdirpath != '/') {
  934. //$form .= '<option value="0">/ ('.get_lang('Root').')</option>';
  935. }
  936. if (is_array($folders)) {
  937. foreach ($folders as $fid => $folder) {
  938. //you cannot move a file to:
  939. //1. current directory
  940. //2. inside the folder you want to move
  941. //3. inside a subfolder of the folder you want to move
  942. if (($curdirpath != $folder) && ($folder != $move_file) && (substr($folder, 0, strlen($move_file) + 1) != $move_file.'/')) {
  943. $form .= '<option value="'.$fid.'">'.$folder.'</option>';
  944. }
  945. }
  946. }
  947. } else {
  948. if ($curdirpath != '/') {
  949. $form .= '<option value="0">/ ('.get_lang('Root').')</option>';
  950. }
  951. foreach ($folders as $fid => $folder) {
  952. if (($curdirpath != $folder) && ($folder != $move_file) && (substr($folder, 0, strlen($move_file) + 1) != $move_file.'/')) {
  953. //cannot copy dir into his own subdir
  954. $display_folder = substr($folder, strlen($group_dir));
  955. $display_folder = ($display_folder == '') ? '/ ('.get_lang('Root').')' : $display_folder;
  956. $form .= '<option value="'.$fid.'">'.$display_folder.'</option>'."\n";
  957. }
  958. }
  959. }
  960. $form .= '</select>';
  961. $form .= ' </div>
  962. </div>';
  963. $form .= '<div class="row">
  964. <div class="label"></div>
  965. <div class="formw">
  966. <button type="submit" class="save" name="move_file_submit">'.get_lang('MoveFile').'</button>
  967. </div>
  968. </div>';
  969. $form .= '</form>';
  970. $form .= '<div style="clear: both; margin-bottom: 10px;"></div>';
  971. return $form;
  972. }
  973. /**
  974. * Checks if the first given directory exists as a subdir of the second given directory
  975. * This function should now be deprecated by Security::check_abs_path()
  976. * @param string Subdir
  977. * @param string Base dir
  978. * @return integer -1 on error, 0 if not subdir, 1 if subdir
  979. */
  980. // TODO: This function is a candidate for removal, it is not used anywhere.
  981. function is_subdir_of($subdir, $basedir) {
  982. if (empty($subdir) or empty($basedir)) {
  983. return -1;
  984. }
  985. if (substr($basedir, -1, 1) != '/') {
  986. $basedir = $basedir.'/';
  987. }
  988. if (substr($subdir, 0, 1) == '/') {
  989. $subdir = substr($subdir, 1);
  990. }
  991. return is_dir($basedir.$subdir) ? 1 : 0;
  992. }
  993. /**
  994. * creates a new directory trying to find a directory name
  995. * that doesn't already exist
  996. * (we could use unique_name() here...)
  997. *
  998. * @author Hugues Peeters <hugues.peeters@claroline.net>
  999. * @author Bert Vanderkimpen
  1000. * @author Yannick Warnier <ywarnier@beeznest.org> Adaptation for work tool
  1001. * @param string Base work dir (.../work)
  1002. * @param string $desiredDirName complete path of the desired name
  1003. * @return string actual directory name if it succeeds, boolean false otherwise
  1004. */
  1005. function create_unexisting_work_directory($base_work_dir, $desired_dir_name) {
  1006. $nb = '';
  1007. $base_work_dir = (substr($base_work_dir, -1, 1) == '/' ? $base_work_dir : $base_work_dir.'/');
  1008. while (file_exists($base_work_dir.$desired_dir_name.$nb)) {
  1009. $nb += 1;
  1010. }
  1011. if (@mkdir($base_work_dir.$desired_dir_name.$nb, api_get_permissions_for_new_directories())) {
  1012. return $desired_dir_name.$nb;
  1013. } else {
  1014. return false;
  1015. }
  1016. }
  1017. /**
  1018. * Delete a work-tool directory
  1019. * @param string Base "work" directory for this course as /var/www/chamilo/courses/ABCD/work/
  1020. * @param string The directory name as the bit after "work/", without trailing slash
  1021. * @return integer -1 on error
  1022. */
  1023. function del_dir($id) {
  1024. global $_course;
  1025. $id = intval($id);
  1026. $work_data = get_work_data_by_id($id);
  1027. if (empty($work_data)) {
  1028. return false;
  1029. }
  1030. $base_work_dir = api_get_path(SYS_COURSE_PATH) .$_course['path'].'/work';
  1031. $work_data_url = $base_work_dir.$work_data['url'];
  1032. $check = Security::check_abs_path($work_data_url.'/', $base_work_dir.'/');
  1033. $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  1034. $course_id = api_get_course_int_id();
  1035. if (!empty($work_data['url'])) {
  1036. //Deleting all contents inside the folder
  1037. //@todo replace to parent_id
  1038. $sql = "UPDATE $table SET active = 2 WHERE c_id = $course_id AND filetype = 'folder' AND id = $id";
  1039. $res = Database::query($sql);
  1040. $sql = "UPDATE $table SET active = 2 WHERE c_id = $course_id AND parent_id = $id";
  1041. $res = Database::query($sql);
  1042. if ($check) {
  1043. require_once api_get_path(LIBRARY_PATH).'fileManage.lib.php';
  1044. $new_dir = $work_data_url.'_DELETED_'.$id;
  1045. if (api_get_setting('permanently_remove_deleted_files') == 'true'){
  1046. my_delete($work_data_url);
  1047. } else {
  1048. if (file_exists($work_data_url)) {
  1049. rename($work_data_url, $new_dir);
  1050. }
  1051. }
  1052. }
  1053. }
  1054. }
  1055. /**
  1056. * Get the path of a document in the student_publication table (path relative to the course directory)
  1057. * @param integer Element ID
  1058. * @return string Path (or -1 on error)
  1059. */
  1060. function get_work_path($id) {
  1061. $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  1062. $course_id = api_get_course_int_id();
  1063. $sql = 'SELECT url FROM '.$table.' WHERE c_id = '.$course_id.' AND id='.intval($id);
  1064. $res = Database::query($sql);
  1065. if (Database::num_rows($res)) {
  1066. $row = Database::fetch_array($res);
  1067. return $row['url'];
  1068. }
  1069. return -1;
  1070. }
  1071. /**
  1072. * Update the url of a work in the student_publication table
  1073. * @param integer ID of the work to update
  1074. * @param string Destination directory where the work has been moved (must end with a '/')
  1075. * @return -1 on error, sql query result on success
  1076. */
  1077. function update_work_url($id, $new_path, $parent_id) {
  1078. if (empty($id)) return -1;
  1079. $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  1080. $course_id = api_get_course_int_id();
  1081. $id = intval($id);
  1082. $parent_id = intval($parent_id);
  1083. $sql = "SELECT * FROM $table WHERE c_id = $course_id AND id = $id";
  1084. $res = Database::query($sql);
  1085. if (Database::num_rows($res) != 1) {
  1086. return -1;
  1087. } else {
  1088. $row = Database::fetch_array($res);
  1089. $filename = basename($row['url']);
  1090. $new_url = $new_path .$filename;
  1091. $sql2 = "UPDATE $table SET url = '$new_url', parent_id = '$parent_id' WHERE c_id = $course_id AND id = $id";
  1092. $res2 = Database::query($sql2);
  1093. return $res2;
  1094. }
  1095. }
  1096. /**
  1097. * Update the url of a dir in the student_publication table
  1098. * @param string old path
  1099. * @param string new path
  1100. */
  1101. function update_dir_name($work_data, $new_name, $title) {
  1102. $course_id = api_get_course_int_id();
  1103. $work_id = intval($work_data['id']);
  1104. $path = $work_data['url'];
  1105. if ($work_data['title'] == $title) {
  1106. return true;
  1107. }
  1108. $title = Database::escape_string($title);
  1109. if (!empty($new_name)) {
  1110. global $base_work_dir;
  1111. $path_to_dir = dirname($path);
  1112. if ($path_to_dir == '.') {
  1113. $path_to_dir = '';
  1114. } else {
  1115. $path_to_dir .= '/';
  1116. }
  1117. $new_name = Security::remove_XSS($new_name);
  1118. $new_name = replace_dangerous_char($new_name);
  1119. $new_name = disable_dangerous_file($new_name);
  1120. my_rename($base_work_dir.'/'.$path, $new_name);
  1121. $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  1122. //update all the files in the other directories according with the next query
  1123. $sql = "SELECT id, url FROM $table WHERE c_id = $course_id AND parent_id = $work_id"; // like binary (Case Sensitive)
  1124. $rs = Database::query($sql);
  1125. $work_len = strlen('work/'.$path);
  1126. while ($work = Database :: fetch_array($rs)) {
  1127. $new_dir = $work['url'];
  1128. $name_with_directory = substr($new_dir, $work_len, strlen($new_dir));
  1129. $sql = 'UPDATE '.$table.' SET url="work/'.$path_to_dir.$new_name.$name_with_directory.'" WHERE c_id = '.$course_id.' AND id= '.$work['id'];
  1130. Database::query($sql);
  1131. }
  1132. $sql = "UPDATE $table SET url= '/".$new_name."' , title = '".$title."' WHERE c_id = $course_id AND id = $work_id";
  1133. Database::query($sql);
  1134. //update all the directory's children according with the next query
  1135. /*
  1136. $sql = 'SELECT id, url FROM '.$table.' WHERE c_id = '.$course_id.' AND url LIKE BINARY "/'.$path.'%"';
  1137. $rs = Database::query($sql);
  1138. $work_len = strlen('/'.$path);
  1139. while ($work = Database :: fetch_array($rs)) {
  1140. $new_dir = $work['url'];
  1141. $name_with_directory = substr($new_dir, $work_len, strlen($new_dir));
  1142. $url = $path_to_dir.$new_name.$name_with_directory;
  1143. $sql = 'UPDATE '.$table.' SET url="/'.$url.'", title = "'.$new_name.'" WHERE c_id = '.$course_id.' AND id= '.$work['id'];
  1144. Database::query($sql);
  1145. }*/
  1146. }
  1147. }
  1148. /**
  1149. * Return an array with all the folder's ids that are in the given path
  1150. * @param string Path of the directory
  1151. * @return array The list of ids of all the directories in the path
  1152. * @author Julio Montoya Dokeos
  1153. * @version April 2008
  1154. */
  1155. function get_parent_directories($id) {
  1156. $course_id = api_get_course_int_id();
  1157. $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  1158. $id = intval($id);
  1159. $sql = "SELECT id FROM $work_table WHERE c_id = $course_id AND parent_id = $id";
  1160. $result = Database::query($sql);
  1161. $list_id = array();
  1162. if (Database::num_rows($result)) {
  1163. while ($row = Database::fetch_array($result)) {
  1164. $list_id[] = $row['id'];
  1165. }
  1166. }
  1167. return $list_id;
  1168. }
  1169. /**
  1170. * Transform an all directory structure (only directories) in an array
  1171. * @param string path of the directory
  1172. * @return array the directory structure into an array
  1173. * @author Julio Montoya Dokeos
  1174. * @version April 2008
  1175. */
  1176. function directory_to_array($directory) {
  1177. $array_items = array();
  1178. if ($handle = @opendir($directory)) {
  1179. while (false !== ($file = readdir($handle))) {
  1180. if ($file != '.' && $file != '..') {
  1181. if (is_dir($directory. '/' . $file)) {
  1182. $array_items = array_merge($array_items, directory_to_array($directory. '/' . $file));
  1183. $file = $directory . '/' . $file;
  1184. $array_items[] = preg_replace("/\/\//si", '/', $file);
  1185. }
  1186. }
  1187. }
  1188. closedir($handle);
  1189. }
  1190. return $array_items;
  1191. }
  1192. /**
  1193. * Insert into the DB of the course all the directories
  1194. * @param string path of the /work directory of the course
  1195. * @return -1 on error, sql query result on success
  1196. * @author Julio Montoya Dokeos
  1197. * @version April 2008
  1198. */
  1199. function insert_all_directory_in_course_table($base_work_dir) {
  1200. $dir_to_array = directory_to_array($base_work_dir, true);
  1201. $only_dir = array();
  1202. for ($i = 0; $i < count($dir_to_array); $i++) {
  1203. $only_dir[] = substr($dir_to_array[$i], strlen($base_work_dir), strlen($dir_to_array[$i]));
  1204. }
  1205. $course_id = api_get_course_int_id();
  1206. for($i = 0; $i < count($only_dir); $i++) {
  1207. global $work_table;
  1208. $sql_insert_all= "INSERT INTO " . $work_table . " SET
  1209. c_id = '$course_id',
  1210. url = '" . $only_dir[$i] . "',
  1211. title = '',
  1212. description = '',
  1213. author = '',
  1214. active = '0',
  1215. accepted = '1',
  1216. filetype = 'folder',
  1217. post_group_id = '".intval($_GET['toolgroup'])."',
  1218. sent_date = '0000-00-00 00:00:00' ";
  1219. Database::query($sql_insert_all);
  1220. }
  1221. }
  1222. /**
  1223. * This function displays the number of files contained in a directory
  1224. *
  1225. * @param string the path of the directory
  1226. * @param boolean true if we want the total quantity of files include in others child directorys , false only files in the directory
  1227. * @return array the first element is an integer with the number of files in the folder, the second element is the number of directories
  1228. * @author Julio Montoya Dokeos
  1229. * @version April 2008
  1230. */
  1231. function count_dir($path_dir, $recurse) {
  1232. $count = 0;
  1233. $count_dir = 0;
  1234. $d = dir($path_dir);
  1235. while ($entry = $d->Read()) {
  1236. if (!(($entry == '..') || ($entry == '.'))) {
  1237. if (is_dir($path_dir.'/'.$entry)) {
  1238. $count_dir++;
  1239. if ($recurse) {
  1240. $count += count_dir($path_dir . '/' . $entry, $recurse);
  1241. }
  1242. } else {
  1243. $count++;
  1244. }
  1245. }
  1246. }
  1247. $return_array = array();
  1248. $return_array[] = $count;
  1249. $return_array[] = $count_dir;
  1250. return $return_array;
  1251. }
  1252. /**
  1253. * returns all the javascript that is required for easily
  1254. * validation when you create a work
  1255. * this goes into the $htmlHeadXtra[] array
  1256. */
  1257. function to_javascript_work() {
  1258. return '<script>
  1259. function plus() {
  1260. if(document.getElementById(\'options\').style.display == \'none\') {
  1261. document.getElementById(\'options\').style.display = \'block\';
  1262. document.getElementById(\'plus\').innerHTML=\'&nbsp;'.Display::return_icon('div_hide.gif', get_lang('Hide', ''), array('style' => 'vertical-align:middle')).'&nbsp;'.addslashes(get_lang('AdvancedParameters', '')).'\';
  1263. } else {
  1264. document.getElementById(\'options\').style.display = \'none\';
  1265. document.getElementById(\'plus\').innerHTML=\'&nbsp;'.Display::return_icon('div_show.gif', get_lang('Show', ''), array('style' => 'vertical-align:middle')).'&nbsp;'.addslashes(get_lang('AdvancedParameters', '')).'\';
  1266. }
  1267. }
  1268. function updateDocumentTitle(value) {
  1269. var temp = value.indexOf("/");
  1270. //linux path
  1271. if(temp!=-1){
  1272. var temp=value.split("/");
  1273. } else {
  1274. var temp=value.split("\\\");
  1275. }
  1276. document.getElementById("file_upload").value=temp[temp.length-1];
  1277. $("#contains_file_id").attr("checked", true);
  1278. }
  1279. function checkDate(month, day, year) {
  1280. var monthLength =
  1281. new Array(31,28,31,30,31,30,31,31,30,31,30,31);
  1282. if (!day || !month || !year)
  1283. return false;
  1284. // check for bisestile year
  1285. if (year/4 == parseInt(year/4))
  1286. monthLength[1] = 29;
  1287. if (month < 1 || month > 12)
  1288. return false;
  1289. if (day > monthLength[month-1])
  1290. return false;
  1291. return true;
  1292. }
  1293. function mktime() {
  1294. var no, ma = 0, mb = 0, i = 0, d = new Date(), argv = arguments, argc = argv.length;
  1295. d.setHours(0,0,0); d.setDate(1); d.setMonth(1); d.setYear(1972);
  1296. var dateManip = {
  1297. 0: function(tt){ return d.setHours(tt); },
  1298. 1: function(tt){ return d.setMinutes(tt); },
  1299. 2: function(tt){ set = d.setSeconds(tt); mb = d.getDate() - 1; return set; },
  1300. 3: function(tt){ set = d.setMonth(parseInt(tt)-1); ma = d.getFullYear() - 1972; return set; },
  1301. 4: function(tt){ return d.setDate(tt+mb); },
  1302. 5: function(tt){ return d.setYear(tt+ma); }
  1303. };
  1304. for( i = 0; i < argc; i++ ){
  1305. no = parseInt(argv[i]*1);
  1306. if (isNaN(no)) {
  1307. return false;
  1308. } else {
  1309. // arg is number, lets manipulate date object
  1310. if(!dateManip[i](no)){
  1311. // failed
  1312. return false;
  1313. }
  1314. }
  1315. }
  1316. return Math.floor(d.getTime()/1000);
  1317. }
  1318. function validate(){
  1319. var expires_day = document.form1.expires_day.value;
  1320. var expires_month = document.form1.expires_month.value;
  1321. var expires_year = document.form1.expires_year.value;
  1322. var expires_hour = document.form1.expires_hour.value;
  1323. var expires_minute = document.form1.expires_minute.value;
  1324. var expires_date = mktime(expires_hour,expires_minute,0,expires_month,expires_day,expires_year)
  1325. var ends_day = document.form1.ends_day.value;
  1326. var ends_month = document.form1.ends_month.value;
  1327. var ends_year = document.form1.ends_year.value;
  1328. var ends_hour = document.form1.ends_hour.value;
  1329. var ends_minute = document.form1.ends_minute.value;
  1330. var ends_date = mktime(ends_hour,ends_minute,0,ends_month,ends_day,ends_year)
  1331. var new_dir = document.form1.new_dir.value;
  1332. msg_id1 = document.getElementById("msg_error1");
  1333. msg_id2 = document.getElementById("msg_error2");
  1334. msg_id3 = document.getElementById("msg_error3");
  1335. msg_id4 = document.getElementById("msg_error4");
  1336. msg_id5 = document.getElementById("msg_error_weight");
  1337. if(new_dir==""){
  1338. msg_id1.style.display ="block";
  1339. msg_id1.innerHTML="'.get_lang('FieldRequired', '').'";
  1340. msg_id2.innerHTML="";msg_id3.innerHTML="";msg_id4.innerHTML="";msg_id5.innerHTML="";
  1341. }
  1342. else if(document.form1.type1.checked && document.form1.type2.checked && expires_date > ends_date) {
  1343. msg_id2.style.display ="block";
  1344. msg_id2.innerHTML="'.get_lang('EndDateCannotBeBeforeTheExpireDate', '').'";
  1345. msg_id1.innerHTML="";msg_id3.innerHTML="";msg_id4.innerHTML="";msg_id5.innerHTML="";
  1346. }
  1347. else if (!checkDate(expires_month,expires_day,expires_year))
  1348. {
  1349. msg_id3.style.display ="block";
  1350. msg_id3.innerHTML="'.get_lang('InvalidDate', '').'";
  1351. msg_id1.innerHTML="";msg_id2.innerHTML="";msg_id4.innerHTML="";msg_id5.innerHTML="";
  1352. }
  1353. else if (!checkDate(ends_month,ends_day,ends_year))
  1354. {
  1355. msg_id4.style.display ="block";
  1356. msg_id4.innerHTML="'.get_lang('InvalidDate', '').'";
  1357. msg_id1.innerHTML="";msg_id2.innerHTML="";msg_id3.innerHTML="";msg_id5.innerHTML="";
  1358. }
  1359. else
  1360. {
  1361. if (document.form1.make_calification.checked)
  1362. {
  1363. var weight = document.form1.weight.value;
  1364. if(weight==""){
  1365. msg_id5.style.display ="block";
  1366. msg_id5.innerHTML="'.get_lang('WeightNecessary', '').'";
  1367. msg_id1.innerHTML="";msg_id2.innerHTML="";msg_id3.innerHTML="";msg_id4.innerHTML="";
  1368. return false;
  1369. }
  1370. }
  1371. document.form1.action = "work.php?origin='.api_get_tools_lists($_REQUEST['origin']).'&gradebook='.(empty($_GET['gradebook'])?'':'view').'";
  1372. document.form1.submit();
  1373. }
  1374. }
  1375. </script>';
  1376. }
  1377. /**
  1378. * Gets the id of a student publication with a given path
  1379. * @param string $path
  1380. * @return true if is found / false if not found
  1381. */
  1382. // TODO: The name of this function does not fit with the kind of information it returns. Maybe check_work_id() or is_work_id()?
  1383. function get_work_id($path) {
  1384. $TBL_STUDENT_PUBLICATION = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
  1385. $TBL_PROP_TABLE = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1386. $course_id = api_get_course_int_id();
  1387. if (api_is_allowed_to_edit()) {
  1388. $sql = "SELECT work.id FROM $TBL_STUDENT_PUBLICATION AS work, $TBL_PROP_TABLE AS props
  1389. 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'";
  1390. } else {
  1391. $sql = "SELECT work.id FROM $TBL_STUDENT_PUBLICATION AS work,$TBL_PROP_TABLE AS props
  1392. 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()."'";
  1393. }
  1394. $result = Database::query($sql);
  1395. $num_rows = Database::num_rows($result);
  1396. if ($result && $num_rows > 0) {
  1397. return true;
  1398. } else {
  1399. return false;
  1400. }
  1401. }
  1402. /**
  1403. * Get list of users who have not given the task
  1404. * @param int
  1405. * @return array
  1406. * @author cvargas
  1407. * @author Julio Montoya <gugli100@gmail.com> Fixing query
  1408. */
  1409. function get_list_users_without_publication($task_id) {
  1410. $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  1411. $iprop_table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1412. $table_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  1413. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  1414. $session_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  1415. //condition for the session
  1416. $session_id = api_get_session_id();
  1417. $course_id = api_get_course_int_id();
  1418. $task_id = intval($task_id);
  1419. if ($session_id == 0) {
  1420. $sql = "SELECT user_id as id FROM $work_table WHERE c_id = $course_id AND parent_id='$task_id'";
  1421. } else {
  1422. $sql = "SELECT user_id as id FROM $work_table WHERE c_id = $course_id AND parent_id='$task_id' and session_id='".$session_id."'";
  1423. }
  1424. $result = Database::query($sql);
  1425. $users_with_tasks = array();
  1426. while($row = Database::fetch_array($result)) {
  1427. $users_with_tasks[] = $row['id'];
  1428. }
  1429. if ($session_id == 0){
  1430. $sql_users = "SELECT cu.user_id, u.lastname, u.firstname, u.email FROM $table_course_user AS cu, $table_user AS u
  1431. WHERE u.status!=1 and cu.course_code='".api_get_course_id()."' AND u.user_id=cu.user_id";
  1432. } else {
  1433. $sql_users = "SELECT cu.id_user, u.lastname, u.firstname, u.email FROM $session_course_rel_user AS cu, $table_user AS u
  1434. 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."'";
  1435. }
  1436. $group_id = api_get_group_id();
  1437. //just in case
  1438. if (empty($group_id)) {
  1439. $group_id = isset($_SESSION['toolgroup']) ? $_SESSION['toolgroup'] : 0;
  1440. }
  1441. if ($group_id) {
  1442. $group_user_list = GroupManager::get_subscribed_users($group_id);
  1443. $new_group_user_list = array();
  1444. if (!empty($group_user_list)) {
  1445. foreach($group_user_list as $group_user) {
  1446. $new_group_user_list[] = $group_user['user_id'];
  1447. }
  1448. }
  1449. }
  1450. $result_users = Database::query($sql_users);
  1451. $users_without_tasks = array();
  1452. while ($row_users = Database::fetch_row($result_users)) {
  1453. if (in_array($row_users[0], $users_with_tasks)) continue;
  1454. if ($group_id) {
  1455. if (!in_array($row_users[0], $new_group_user_list)) continue;
  1456. }
  1457. $user_id = array_shift($row_users);
  1458. $row_users[3] = $row_users[2];
  1459. $row_users[2] = Display::encrypted_mailto_link($row_users[2], $row_users[2]);
  1460. $users_without_tasks[] = $row_users;
  1461. }
  1462. return $users_without_tasks;
  1463. }
  1464. /**
  1465. * Display list of users who have not given the task
  1466. *
  1467. * @param int task id
  1468. * @return array
  1469. * @author cvargas carlos.vargas@beeznest.com cfasanando, christian.fasanado@beeznest.com
  1470. * @author Julio Montoya <gugli100@gmail.com> Fixes
  1471. */
  1472. function display_list_users_without_publication($task_id) {
  1473. global $origin;
  1474. $table_header[] = array(get_lang('LastName'), true);
  1475. $table_header[] = array(get_lang('FirstName'), true);
  1476. $table_header[] = array(get_lang('Email'), true);
  1477. // table_data
  1478. $table_data = get_list_users_without_publication($task_id);
  1479. $sorting_options = array();
  1480. $sorting_options['column'] = 1;
  1481. $paging_options = array();
  1482. $my_params = array();
  1483. if (isset($_GET['curdirpath'])) {
  1484. $my_params['curdirpath'] = Security::remove_XSS($_GET['curdirpath']);
  1485. }
  1486. if (isset($_GET['edit_dir'])) {
  1487. $my_params['edit_dir'] = Security::remove_XSS($_GET['edit_dir']);
  1488. }
  1489. if (isset($_GET['list'])) {
  1490. $my_params['list'] = Security::remove_XSS($_GET['list']);
  1491. }
  1492. $my_params['origin'] = $origin;
  1493. //$column_show
  1494. $column_show[] = 1;
  1495. $column_show[] = 1;
  1496. $column_show[] = 1;
  1497. Display::display_sortable_config_table('work', $table_header, $table_data, $sorting_options, $paging_options, $my_params, $column_show);
  1498. }
  1499. /**
  1500. * Send reminder to users who have not given the task
  1501. *
  1502. * @param int
  1503. * @return array
  1504. * @author cvargas carlos.vargas@beeznest.com cfasanando, christian.fasanado@beeznest.com
  1505. */
  1506. function send_reminder_users_without_publication($task_data) {
  1507. global $_course, $currentUserFirstName, $currentUserLastName, $currentUserEmail;
  1508. $task_id = $task_data['id'];
  1509. $task_title = !empty($task_data['title']) ? $task_data['title'] : basename($task_data['url']);
  1510. $emailsubject = '[' . api_get_setting('siteName') . '] ';
  1511. $sender_name = api_get_person_name($currentUserFirstName, $currentUserLastName, null, PERSON_NAME_EMAIL_ADDRESS);
  1512. $email_admin = $currentUserEmail;
  1513. // The body can be as long as you wish, and any combination of text and variables
  1514. $emailbody_user .= get_lang('ReminderToSubmitPendingTask')."\n".get_lang('CourseName').' : '.$_course['name']."\n";
  1515. $emailbody_user .= get_lang('WorkName').' : '.$task_title."\n\n".get_lang('Teacher').' : '.api_get_person_name($currentUserFirstName, $currentUserLastName)."\n".get_lang('Email').' : '.$currentUserEmail;
  1516. $list_users = get_list_users_without_publication($task_id);
  1517. foreach ($list_users as $user) {
  1518. $name_user = api_get_person_name($user[0], $user[1], null, PERSON_NAME_EMAIL_ADDRESS);
  1519. @api_mail($name_user, $user[3], $emailsubject, $emailbody_user, $sender_name, $email_admin);
  1520. }
  1521. }
  1522. /**
  1523. * Sends an email to the students of a course when a homework is created
  1524. *
  1525. * @param string course_id
  1526. *
  1527. * @author Guillaume Viguier <guillaume.viguier@beeznest.com>
  1528. * @author Julio Montoya <gugli100@gmail.com> Adding session support - 2011
  1529. */
  1530. function send_email_on_homework_creation($course_id) {
  1531. // Get the students of the course
  1532. $session_id = api_get_session_id();
  1533. if (empty($session_id)) {
  1534. $students = CourseManager::get_student_list_from_course_code($course_id);
  1535. } else {
  1536. $students = CourseManager::get_student_list_from_course_code($course_id, true, $session_id);
  1537. }
  1538. $emailsubject = '[' . api_get_setting('siteName') . '] '.get_lang('HomeworkCreated');
  1539. $currentUser = api_get_user_info(api_get_user_id());
  1540. if (!empty($students)) {
  1541. foreach($students as $student) {
  1542. $user_info = api_get_user_info($student["user_id"]);
  1543. if(!empty($user_info["mail"])) {
  1544. $name_user = api_get_person_name($user_info["firstname"], $user_info["lastname"], null, PERSON_NAME_EMAIL_ADDRESS);
  1545. $emailbody = get_lang('Dear')." ".$name_user.",\n\n";
  1546. $emailbody .= get_lang('HomeworkHasBeenCreatedForTheCourse')." ".$course_id.". "."\n\n".get_lang('PleaseCheckHomeworkPage');
  1547. $emailbody .= "\n\n".api_get_person_name($currentUser["firstname"], $currentUser["lastname"]);
  1548. @api_mail($name_user, $user_info["mail"], $emailsubject, $emailbody, api_get_person_name($currentUser["firstname"], $currentUser["lastname"], null, PERSON_NAME_EMAIL_ADDRESS), $currentUser["mail"]);
  1549. }
  1550. }
  1551. }
  1552. }
  1553. function is_work_exist_by_url($url) {
  1554. $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  1555. $url = Database::escape_string($url);
  1556. $sql = "SELECT id FROM $work_table WHERE url='$url'";
  1557. $result = Database::query($sql);
  1558. if (Database::num_rows($result)> 0) {
  1559. $row = Database::fetch_row($result);
  1560. if (empty($row)) {
  1561. return false;
  1562. } else {
  1563. return true;
  1564. }
  1565. } else {
  1566. return false;
  1567. }
  1568. }
  1569. function make_select($name, $values, $checked = '') {
  1570. $output = '<select name="'.$name.'" id="'.$name.'">';
  1571. foreach($values as $key => $value) {
  1572. $output .= '<option value="'.$key.'" '.(($checked==$key) ? 'selected="selected"' : '').'>'.$value.'</option>';
  1573. }
  1574. $output .= '</select>';
  1575. return $output;
  1576. }
  1577. function make_checkbox($name, $checked = '', $label = null) {
  1578. $check = '<input id ="'.$name.'" type="checkbox" value="1" name="'.$name.'" '.((!empty($checked))?'checked="checked"':'').'/>';
  1579. if (!empty($label)) {
  1580. $check .="<label for ='$name'>$label</label>";
  1581. }
  1582. return $check;
  1583. }
  1584. function draw_date_picker($prefix, $default = '') {
  1585. if (empty($default)) {
  1586. $default = api_get_local_time();
  1587. }
  1588. $parts = explode(' ', $default);
  1589. list($d_year, $d_month, $d_day) = explode('-', $parts[0]);
  1590. list($d_hour, $d_minute) = explode(':', $parts[1]);
  1591. $minute = range(10, 59);
  1592. array_unshift($minute, '00', '01', '02', '03', '04', '05', '06', '07', '08', '09');
  1593. $date_form = make_select($prefix.'_day', array_combine(range(1, 31), range(1, 31)), $d_day);
  1594. $date_form .= make_select($prefix.'_month', array_combine(range(1, 12), api_get_months_long()), $d_month);
  1595. $date_form .= make_select($prefix.'_year', array($d_year => $d_year, $d_year + 1 => $d_year + 1), $d_year).'&nbsp;&nbsp;&nbsp;&nbsp;';
  1596. $date_form .= make_select($prefix.'_hour', array_combine(range(0, 23), range(0, 23)), $d_hour).' : ';
  1597. $date_form .= make_select($prefix.'_minute', $minute, $d_minute);
  1598. return $date_form;
  1599. }
  1600. function get_date_from_select($prefix) {
  1601. 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';
  1602. }