work.lib.php 75 KB

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