work.lib.php 73 KB

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