work.lib.php 76 KB

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