work.lib.php 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833
  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'),'',ICON_SIZE_MEDIUM).'</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'),'',ICON_SIZE_MEDIUM).'</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'),'',ICON_SIZE_MEDIUM).'</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'),'',ICON_SIZE_MEDIUM).'</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'),'',ICON_SIZE_MEDIUM)."</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'),'',ICON_SIZE_MEDIUM)."</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'),'',ICON_SIZE_MEDIUM)."</a>";
  72. } else {
  73. $display_output .= Display::return_icon('mail_send_na.png', get_lang('ReminderMessage'),'',ICON_SIZE_MEDIUM);
  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 '<legend>'.get_lang('EditToolOptions').'</legend>';
  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. <input id="uploadvisibledisabled_2" class="checkbox" type="radio" name="uploadvisibledisabled" value="1" <?php if ($uploadvisibledisabled == 1) echo 'checked'; ?> />
  129. <label for="uploadvisibledisabled_2">
  130. <?php echo get_lang('_new_unvisible'); ?>
  131. </label>
  132. </div>
  133. </div>
  134. <?php
  135. }
  136. /**
  137. * Display a part of the form to edit the settings of the tool
  138. * In this case weither the students are allowed to delete their own publication or not (by default not)
  139. *
  140. * @return html code
  141. * @since Dokeos 1.8.6.2
  142. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  143. */
  144. function display_studentsdelete_form() {
  145. // by default api_get_course_setting returns -1 and the code only expects 0 or 1 so anything tha
  146. // is different than 1 will be converted into 0
  147. $current_course_setting_value = api_get_course_setting('student_delete_own_publication');
  148. if ($current_course_setting_value != 1) {
  149. $current_course_setting_value = 0;
  150. }
  151. ?>
  152. <div class="row">
  153. <div class="label"><?php echo get_lang('StudentAllowedToDeleteOwnPublication'); ?></div>
  154. <div class="formw">
  155. <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'; ?> />
  156. <label for="student_delete_own_publication_2">
  157. <?php echo get_lang('Yes'); ?>
  158. </label>
  159. <div></div>
  160. <input id="student_delete_own_publication_1" class="checkbox" type="radio" name="student_delete_own_publication" value="0"
  161. <?php if ($current_course_setting_value == 0) echo 'checked'; ?> />
  162. <label for="student_delete_own_publication_1">
  163. <?php echo get_lang('No'); ?>
  164. </label>
  165. </div>
  166. </div>
  167. <?php
  168. }
  169. /**
  170. * converts 2008-10-06 12:45:00 to timestamp
  171. * @deprecated any calls found
  172. */
  173. function convert_date_to_number($default) {
  174. // 2008-10-12 00:00:00 ---to--> 12345672218 (timestamp)
  175. $parts = split(' ', $default);
  176. list($d_year, $d_month, $d_day) = split('-', $parts[0]);
  177. list($d_hour, $d_minute, $d_second) = split(':', $parts[1]);
  178. return mktime((int)$d_hour, (int)$d_minute, (int)$d_second, (int)$d_month, (int)$d_day, (int)$d_year);
  179. }
  180. /**
  181. * converts 1-9 to 01-09
  182. */
  183. function two_digits($number) {
  184. $number = (int)$number;
  185. return ($number < 10) ? '0'.$number : $number;
  186. }
  187. /**
  188. * converts 2008-10-06 12:45:00 to -> array($data'year'=>2008,$data'month'=>10 etc...)
  189. */
  190. function convert_date_to_array($date, $group) {
  191. $parts = split(' ', $date);
  192. $date_parts = split('-', $parts[0]);
  193. $date_parts_tmp = array();
  194. foreach ($date_parts as $item) {
  195. $date_parts_tmp[] = intval($item);
  196. }
  197. $time_parts = split(':', $parts[1]);
  198. $time_parts_tmp = array();
  199. foreach ($time_parts as $item) {
  200. $time_parts_tmp[] = intval($item);
  201. }
  202. list($data[$group.'[year]'], $data[$group.'[month]'], $data[$group.'[day]']) = $date_parts_tmp;
  203. list($data[$group.'[hour]'], $data[$group.'[minute]']) = $time_parts_tmp;
  204. return $data;
  205. }
  206. /**
  207. * get date from a group of date
  208. */
  209. function get_date_from_group($group) {
  210. 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';
  211. }
  212. /**
  213. * create a group of select from a date
  214. */
  215. function create_group_date_select($prefix = '') {
  216. $minute = range(10, 59);
  217. $d_year = date('Y');
  218. array_unshift($minute, '00', '01', '02', '03', '04', '05', '06', '07', '08', '09');
  219. $group_name[] = FormValidator :: createElement('select', $prefix.'day', '', array_combine(range(1, 31), range(1, 31)));
  220. $group_name[] = FormValidator :: createElement('select', $prefix.'month', '', array_combine(range(1, 12), api_get_months_long()));
  221. $group_name[] = FormValidator :: createElement('select', $prefix.'year', '', array($d_year => $d_year, $d_year + 1 => $d_year + 1));
  222. $group_name[] = FormValidator :: createElement('select', $prefix.'hour', '', array_combine(range(0, 23), range(0, 23)));
  223. $group_name[] = FormValidator :: createElement('select', $prefix.'minute', '', $minute);
  224. return $group_name;
  225. }
  226. function get_work_data_by_path($path) {
  227. $path = Database::escape_string($path);
  228. $course_id = api_get_course_int_id();
  229. $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  230. $sql = "SELECT * FROM ".$work_table." WHERE url = '$path' AND c_id = $course_id ";
  231. $result = Database::query($sql);
  232. $return = array();
  233. if (Database::num_rows($result)) {
  234. $return = Database::fetch_array($result,'ASSOC');
  235. }
  236. return $return;
  237. }
  238. function get_work_data_by_id($id) {
  239. $id = intval($id);
  240. $course_id = api_get_course_int_id();
  241. $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  242. $sql = "SELECT * FROM $work_table WHERE id = $id AND c_id = $course_id";
  243. $result = Database::query($sql);
  244. $return = array();
  245. if (Database::num_rows($result)) {
  246. $return = Database::fetch_array($result,'ASSOC');
  247. }
  248. return $return;
  249. }
  250. function get_work_count_by_student($user_id, $work_id) {
  251. $user_id = intval($user_id);
  252. $work_id = intval($work_id);
  253. $course_id = api_get_course_int_id();
  254. $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  255. $sql = "SELECT COUNT(*) as count FROM $work_table
  256. WHERE c_id = $course_id AND parent_id = $work_id AND user_id = $user_id AND active = 1 ";
  257. $result = Database::query($sql);
  258. $return = 0;
  259. if (Database::num_rows($result)) {
  260. $return = Database::fetch_row($result,'ASSOC');
  261. $return = intval($return[0]);
  262. }
  263. return $return;
  264. }
  265. function get_work_assignment_by_id($id) {
  266. $id = intval($id);
  267. $course_id = api_get_course_int_id();
  268. $table = Database :: get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT);
  269. $sql = "SELECT * FROM $table WHERE c_id = $course_id AND publication_id = $id";
  270. $result = Database::query($sql);
  271. $return = array();
  272. if (Database::num_rows($result)) {
  273. $return = Database::fetch_array($result,'ASSOC');
  274. }
  275. return $return;
  276. }
  277. /**
  278. * Display the list of student publications, taking into account the user status
  279. *
  280. * @param $link_target_parameter - should there be a target parameter for the links
  281. * @param $dateFormatLong - date format
  282. * @param $origin - typically empty or 'learnpath'
  283. */
  284. function display_student_publications_list($id, $link_target_parameter, $dateFormatLong, $origin, $add_in_where_query = '') {
  285. global $timeNoSecFormat, $dateFormatShort, $gradebook, $_course;
  286. // Database table names
  287. $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  288. $iprop_table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  289. $work_assigment = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT);
  290. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  291. $user_id = api_get_user_id();
  292. $session_id = api_get_session_id();
  293. $condition_session = api_get_session_condition($session_id);
  294. $course_id = api_get_course_int_id();
  295. $group_id = api_get_group_id();
  296. $sort_params = array();
  297. if (isset($_GET['column'])) {
  298. $sort_params[] = 'column='.Security::remove_XSS($_GET['column']);
  299. }
  300. if (isset($_GET['page_nr'])) {
  301. $sort_params[] = 'page_nr='.Security::remove_XSS($_GET['page_nr']);
  302. }
  303. if (isset($_GET['per_page'])) {
  304. $sort_params[] = 'per_page='.Security::remove_XSS($_GET['per_page']);
  305. }
  306. if (isset($_GET['direction'])) {
  307. $sort_params[] = 'direction='.Security::remove_XSS($_GET['direction']);
  308. }
  309. $sort_params = implode('&amp;', $sort_params);
  310. $my_params = $sort_params;
  311. $origin = Security::remove_XSS($origin);
  312. // Getting the work data
  313. $my_folder_data = get_work_data_by_id($id);
  314. $qualification_exists = false;
  315. if (!empty($my_folder_data['qualification']) && intval($my_folder_data['qualification']) > 0) {
  316. $qualification_exists = true;
  317. }
  318. $work_dir = api_get_path(SYS_COURSE_PATH).$_course['path'].'/work';
  319. if (!empty($my_folder_data)) {
  320. $work_dir = api_get_path(SYS_COURSE_PATH).$_course['path'].'/work'.$my_folder_data['url'];
  321. }
  322. if (empty($my_folder_data)) {
  323. $work_in_gradebook_link_id = is_resource_in_course_gradebook(api_get_course_id(), 3 , $id, api_get_session_id());
  324. if ($work_in_gradebook_link_id) {
  325. if ($is_allowed_to_edit)
  326. if (intval($my_folder_data['qualification']) == 0) {
  327. Display::display_warning_message(get_lang('MaxWeightNeedToBeProvided'));
  328. }
  329. }
  330. $contains_file_query = '';
  331. //Get list from database
  332. if ($is_allowed_to_edit) {
  333. $active_condition = ' active IN (0, 1)';
  334. $sql_get_publications_list = "SELECT * FROM $work_table
  335. WHERE c_id = $course_id $add_in_where_query $condition_session AND $active_condition AND
  336. ( parent_id = 0)
  337. $contains_file_query
  338. ORDER BY sent_date DESC";
  339. } else {
  340. if (!empty($group_id)) {
  341. $group_query = " WHERE c_id = $course_id AND post_group_id = '".$group_id."' "; // set to select only messages posted by the user's group
  342. $subdirs_query = "AND parent_id = 0";
  343. } else {
  344. $group_query = " WHERE c_id = $course_id AND post_group_id = '0' ";
  345. $subdirs_query = "AND parent_id = 0";
  346. }
  347. //@todo how we can active or not an assignment?
  348. $active_condition = ' AND active IN (1,0)';
  349. $sql_get_publications_list = "SELECT * FROM $work_table $group_query $subdirs_query $add_in_where_query $active_condition $condition_session ORDER BY title";
  350. }
  351. $work_parents = array();
  352. $sql_result = Database::query($sql_get_publications_list);
  353. if (Database::num_rows($sql_result)) {
  354. while ($work = Database::fetch_object($sql_result)) {
  355. if ($work->parent_id == 0) {
  356. $work_parents[] = $work;
  357. }
  358. }
  359. }
  360. } else {
  361. $parent_id = isset($my_folder_data['id']) ? $my_folder_data['id'] : 0;
  362. if (!empty($group_id)) {
  363. $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
  364. } else {
  365. $group_query = " WHERE c_id = $course_id AND post_group_id = '0' ";
  366. }
  367. $subdirs_query = "AND parent_id = $parent_id ";
  368. if ($is_allowed_to_edit) {
  369. //$subdirs_query .= " AND user_id = ".api_get_user_id()." ";
  370. } else {
  371. $subdirs_query .= " AND user_id = ".api_get_user_id()." ";
  372. }
  373. $active_condition = ' AND active IN (1)';
  374. $sql_get_publications_list = "SELECT * FROM $work_table $group_query $subdirs_query $add_in_where_query $active_condition $condition_session ORDER BY title";
  375. $sql_result = Database::query($sql_get_publications_list);
  376. }
  377. $edit_dir = isset($_GET['edit_dir']) ? $_GET['edit_dir'] : '';
  378. $table_header = array();
  379. $table_has_actions_column = false;
  380. $table_header[] = array(get_lang('Type'), false, 'style="width:40px"');
  381. $table_header[] = array(get_lang('Title'), true);
  382. if (!empty($id)) {
  383. $table_header[] = array(get_lang('FirstName'), true);
  384. $table_header[] = array(get_lang('LastName'), true);
  385. if ($qualification_exists) {
  386. $table_header[] = array(get_lang('Qualification'), true);
  387. }
  388. }
  389. $table_header[] = array(get_lang('Date'), true, 'style="width:200px"');
  390. if ($is_allowed_to_edit) {
  391. $table_header[] = array(get_lang('Actions'), false, 'style="width:90px"', array('class'=>'td_actions'));
  392. $table_has_actions_column = true;
  393. }
  394. // the following column name seems both undefined and unused
  395. //$table_header[] = array('RealDate', true);
  396. $table_data = array();
  397. // List of all folders if no id was provided
  398. $group_id = api_get_group_id();
  399. if (is_array($work_parents)) {
  400. foreach ($work_parents as $work_parent) {
  401. $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
  402. FROM ".$iprop_table." prop INNER JOIN ".$work_table." work ON (prop.ref=work.id AND prop.c_id = $course_id )
  403. WHERE active IN (0, 1) AND ";
  404. if (!empty($group_id)) {
  405. $sql_select_directory .= " work.post_group_id = '".$group_id."' "; // set to select only messages posted by the user's group
  406. } else {
  407. $sql_select_directory .= " work.post_group_id = '0' ";
  408. }
  409. $sql_select_directory .= " AND
  410. work.c_id = $course_id AND
  411. work.id = ".$work_parent->id." AND
  412. work.filetype = 'folder' AND
  413. prop.tool='work' $condition_session";
  414. $result = Database::query($sql_select_directory);
  415. $row = Database::fetch_array($result, 'ASSOC');
  416. if (!$row) {
  417. // the folder belongs to another session
  418. continue;
  419. }
  420. $direc_date = $row['lastedit_date']; //directory's date
  421. $author = $row['author']; //directory's author
  422. $view_properties = $row['view_properties'];
  423. $is_assignment = $row['has_properties'];
  424. $id2 = $row['id']; //work id
  425. if ($is_allowed_to_edit) {
  426. // form edit directory
  427. if (!empty($edit_dir) && $edit_dir == $id2) {
  428. if (!empty($row['has_properties'])) {
  429. $sql = Database::query('SELECT * FROM '.$work_assigment.' WHERE c_id = '.$course_id.' AND id = "'.$row['has_properties'].'" LIMIT 1');
  430. $homework = Database::fetch_array($sql);
  431. }
  432. $form_folder = new FormValidator('edit_dir', 'post', api_get_self().'?origin='.$origin.'&gradebook='.$gradebook.'&edit_dir='.$id2);
  433. $form_folder->addElement('text', 'dir_name', get_lang('Title'));
  434. $form_folder->addElement('hidden', 'work_id', $id2);
  435. $form_folder -> addRule('dir_name', get_lang('ThisFieldIsRequired'), 'required');
  436. $my_title = !empty($row['title']) ? $row['title'] : basename($row['url']);
  437. $defaults = array('dir_name' => Security::remove_XSS($my_title), 'description' => Security::remove_XSS($row['description']));
  438. $form_folder->add_html_editor('description', get_lang('Description'), false, false, array('ToolbarSet' => 'work', 'Width' => '80%', 'Height' => '200'));
  439. $there_is_a_end_date = false;
  440. $form_folder -> addElement('html', '<div class="row">
  441. <div class="label">&nbsp;</div>
  442. <div class="formw">
  443. <a href="javascript://" onclick="javascript: return plus();" >
  444. <span id="plus">&nbsp;<img style="vertical-align:middle;" src="../img/div_show.gif" alt="" />
  445. &nbsp;'.get_lang('AdvancedParameters').'
  446. </span>
  447. </a>
  448. </div>
  449. </div> ');
  450. $form_folder->addElement('html', '<div id="options" style="display: none;">');
  451. if (empty($default)) {
  452. $default = api_get_local_time();
  453. }
  454. $parts = explode(' ', $default);
  455. list($d_year, $d_month, $d_day) = explode('-', $parts[0]);
  456. list($d_hour, $d_minute) = explode(':', $parts[1]);
  457. $qualification_input[] = FormValidator :: createElement('text','qualification');
  458. $form_folder -> addGroup($qualification_input, 'qualification', get_lang('QualificationNumeric'), 'size="10"');
  459. if ((int)$row['weight'] == 0) {
  460. $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\';}"');
  461. $form_folder -> addElement('html', '<div id=\'option3\' style="display:none">');
  462. $weight_input2[] = FormValidator :: createElement('text', 'weight');
  463. $form_folder -> addGroup($weight_input2, 'weight', get_lang('WeightInTheGradebook'), 'size="10"');
  464. $form_folder -> addElement('html', '</div>');
  465. } else {
  466. $weight_input[] = FormValidator :: createElement('text', 'weight');
  467. $form_folder -> addGroup($weight_input, 'weight', get_lang('WeightInTheGradebook'), 'size="10"');
  468. }
  469. if ($homework['expires_on'] != '0000-00-00 00:00:00') {
  470. $homework['expires_on'] = api_get_local_time($homework['expires_on']);
  471. $there_is_a_expire_date = true;
  472. $defaults['enableExpiryDate'] = true;
  473. $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\';}"');
  474. $form_folder -> addElement('html', '<div id=\'option1\' style="display:block">');
  475. $form_folder -> addGroup(create_group_date_select(), 'expires', get_lang('ExpiresAt'));
  476. $form_folder -> addElement('html', '</div>');
  477. } else {
  478. $homework['expires_on'] = api_get_local_time();
  479. $expires_date_array = convert_date_to_array(api_get_local_time(), 'expires');
  480. $defaults = array_merge($defaults, $expires_date_array);
  481. $there_is_a_expire_date = false;
  482. $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\';}"');
  483. $form_folder -> addElement('html', '<div id=\'option1\' style="display:none">');
  484. $form_folder -> addGroup(create_group_date_select(), 'expires', get_lang('ExpiresAt'));
  485. $form_folder -> addElement('html', '</div>');
  486. }
  487. if ($homework['ends_on'] != '0000-00-00 00:00:00') {
  488. $homework['ends_on'] = api_get_local_time($homework['ends_on']);
  489. $there_is_a_end_date = true;
  490. $defaults['enableEndDate'] = true;
  491. $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\';}"');
  492. $form_folder -> addElement('html', '<div id=\'option2\' style="display:block">');
  493. $form_folder -> addGroup(create_group_date_select(), 'ends', get_lang('EndsAt'));
  494. $form_folder -> addElement('html', '</div>');
  495. $form_folder -> addRule(array('expires', 'ends'), get_lang('DateExpiredNotBeLessDeadLine'), 'comparedate');
  496. } else {
  497. $homework['ends_on'] = api_get_local_time();
  498. $expires_date_array = convert_date_to_array(api_get_local_time(), 'ends');
  499. $defaults = array_merge($defaults, $expires_date_array);
  500. $there_is_a_end_date = false;
  501. $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\';}"');
  502. $form_folder -> addElement('html', '<div id=\'option2\' style="display:none">');
  503. $form_folder -> addGroup(create_group_date_select(), 'ends', get_lang('EndsAt'));
  504. $form_folder -> addElement('html', '</div>');
  505. $form_folder -> addRule(array('expires', 'ends'), get_lang('DateExpiredNotBeLessDeadLine'), 'comparedate');
  506. }
  507. if ($there_is_a_expire_date && $there_is_a_end_date) {
  508. $form_folder -> addRule(array('expires', 'ends'), get_lang('DateExpiredNotBeLessDeadLine'), 'comparedate');
  509. }
  510. $form_folder -> addElement('checkbox', 'allow_text_assignment', null, get_lang('AllowTextAssignments'));
  511. $form_folder -> addElement('html', '</div>');
  512. $form_folder -> addElement('style_submit_button', 'submit', get_lang('ModifyDirectory'), 'class="save"');
  513. if ($there_is_a_end_date) {
  514. $end_date_array = convert_date_to_array($homework['ends_on'], 'ends');
  515. $defaults = array_merge($defaults, $end_date_array);
  516. }
  517. if ($there_is_a_expire_date) {
  518. $expires_date_array = convert_date_to_array($homework['expires_on'], 'expires');
  519. $defaults = array_merge($defaults, $expires_date_array);
  520. }
  521. if (!empty($row['qualification'])) {
  522. $defaults = array_merge($defaults, array('qualification[qualification]' => $row['qualification']));
  523. }
  524. if (!empty($row['weight'])) {
  525. $defaults = array_merge($defaults, array('weight[weight]' => $row['weight']));
  526. }
  527. $defaults['allow_text_assignment'] = $row['allow_text_assignment'];
  528. $form_folder -> setDefaults($defaults);
  529. $display_edit_form = true;
  530. if ($form_folder->validate()) {
  531. if ($_POST['enableExpiryDate'] == '1') {
  532. $there_is_a_expire_date = true;
  533. } else {
  534. $there_is_a_expire_date = false;
  535. }
  536. if ($_POST['enableEndDate'] == '1') {
  537. $there_is_a_end_date = true;
  538. } else {
  539. $there_is_a_end_date = false;
  540. }
  541. $values = $form_folder->exportValues();
  542. $work_id = $values['work_id'];
  543. //$values = $values['my_group'];
  544. $dir_name = replace_dangerous_char($values['dir_name']);
  545. $dir_name = disable_dangerous_file($dir_name);
  546. $edit_check = false;
  547. $work_data = get_work_data_by_id($work_id);
  548. if (!empty($work_data)) {
  549. $edit_check = true;
  550. } else {
  551. $edit_check = true;
  552. }
  553. if ($edit_check) {
  554. $TABLEAGENDA = Database::get_course_table(TABLE_AGENDA);
  555. $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')."'";
  556. Database::query('UPDATE '.$work_assigment.$expires_query.' WHERE c_id = '.$course_id.' AND id = '."'".$row['has_properties']."'");
  557. $sql_add_publication = "UPDATE ".$work_table." SET has_properties = '".$row['has_properties']. "', view_properties=1 WHERE c_id = $course_id AND id ='".$row['id']."'";
  558. Database::query($sql_add_publication);
  559. $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')."'";
  560. Database::query('UPDATE '.$work_assigment.$ends_query.' WHERE c_id = '.$course_id.' AND id = '."'".$row['has_properties']."'");
  561. $sql_add_publication = "UPDATE ".$work_table." SET has_properties = '".$row['has_properties']. "', view_properties=1 WHERE c_id = $course_id AND id ='".$row['id']."'";
  562. Database::query($sql_add_publication);
  563. $sql = 'UPDATE '.$work_table.' SET
  564. allow_text_assignment = '."'".intval($_POST['allow_text_assignment'])."'".' ,
  565. title = '."'".Database::escape_string($_POST['dir_name'])."'".',
  566. description = '."'".Database::escape_string($_POST['description'])."'".',
  567. qualification = '."'".Database::escape_string($_POST['qualification']['qualification'])."'".',
  568. weight = '."'".Database::escape_string($_POST['weight']['weight'])."'".'
  569. WHERE c_id = '.$course_id.' AND id = '.$row['id'];
  570. Database::query($sql);
  571. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/gradebook_functions.inc.php';
  572. $link_id = is_resource_in_course_gradebook(api_get_course_id(), 3 , $row['id'], api_get_session_id());
  573. if ($link_id !== false) {
  574. Database::query('UPDATE '.Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK).' SET weight = '."'".Database::escape_string((float)$_POST['weight']['weight'])."'".'
  575. WHERE c_id = '.$course_id.' AND id = '.$link_id);
  576. }
  577. //we are changing the current work and we want add them into gradebook
  578. if (isset($_POST['make_calification']) && $_POST['make_calification'] == 1) {
  579. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/gradebookitem.class.php';
  580. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/evaluation.class.php';
  581. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/abstractlink.class.php';
  582. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/gradebook_functions.inc.php';
  583. $resource_name = $_POST['dir_name'];
  584. 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());
  585. }
  586. update_dir_name($work_data, $dir_name, $values['dir_name']);
  587. $dir = $dir_name;
  588. $display_edit_form = false;
  589. // gets calendar_id from student_publication_assigment
  590. $sql = "SELECT add_to_calendar FROM $work_assigment WHERE c_id = $course_id AND publication_id ='".$row['id']."'";
  591. $res = Database::query($sql);
  592. $calendar_id = Database::fetch_row($res);
  593. $dir_name = sprintf(get_lang('HandingOverOfTaskX'), $dir_name);
  594. $end_date = $row['insert_date'];
  595. if ($_POST['enableExpiryDate'] == '1') {
  596. $end_date = Database::escape_string(api_get_utc_datetime(get_date_from_group('expires')));
  597. }
  598. // update from agenda if it exists
  599. if (!empty($calendar_id[0])) {
  600. $sql = "UPDATE ".$TABLEAGENDA."
  601. SET title='".$values['dir_name']."',
  602. content = '".Database::escape_string($_POST['description'])."',
  603. start_date = '".$end_date."',
  604. end_date = '".$end_date."'
  605. WHERE c_id = $course_id AND id='".$calendar_id[0]."'";
  606. Database::query($sql);
  607. }
  608. Display::display_confirmation_message(get_lang('FolderEdited'));
  609. } else {
  610. Display::display_warning_message(get_lang('FileExists'));
  611. }
  612. }
  613. }
  614. }
  615. $work_data = get_work_data_by_id($work_parent->id);
  616. $action = '';
  617. $row = array();
  618. $class = '';
  619. //$a_count_directory = count_dir($work_dir.'/'.$dir, false);
  620. $cant_files = 0;
  621. $cant_dir = 0;
  622. $course_id = api_get_course_int_id();
  623. $session_id = api_get_session_id();
  624. if (api_is_allowed_to_edit()) {
  625. $sql_document = "SELECT count(*) FROM $work_table WHERE c_id = $course_id AND parent_id = ".$work_data['id']." AND active IN (0, 1) ";
  626. } else {
  627. $sql_document = "SELECT count(*) FROM $work_table s, $iprop_table p
  628. WHERE s.c_id = $course_id AND
  629. p.c_id = $course_id AND
  630. s.id = p.ref AND
  631. p.tool='work' AND
  632. s.accepted='1' AND
  633. user_id = ".api_get_user_id()." AND
  634. parent_id = ".$work_data['id']." AND
  635. active = 1 AND
  636. parent_id = ".$work_parent->id."";
  637. }
  638. //count documents
  639. $res_document = Database::query($sql_document);
  640. $count_document = Database::fetch_row($res_document);
  641. $cant_files = $count_document[0];
  642. $text_file = get_lang('FilesUpload');
  643. if ($cant_files == 1) {
  644. $text_file = api_strtolower(get_lang('FileUpload'));
  645. }
  646. $icon = Display::return_icon('work.png', get_lang('Assignment'), array(), ICON_SIZE_SMALL);
  647. if (!empty($display_edit_form) && !empty($edit_dir) && $edit_dir == $id2) {
  648. $row[] = $icon;
  649. $row[] = '<span class="invisible" style="display:none">'.$dir.'</span>'.$form_folder->toHtml(); // form to edit the directory's name
  650. } else {
  651. $row[] = '<a href="'.api_get_self().'?'.api_get_cidreq().'&origin='.$origin.'&gradebook='.$gradebook.'">'.$icon.'</a>';
  652. $add_to_name = '';
  653. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/gradebook_functions.inc.php';
  654. $link_id = is_resource_in_course_gradebook(api_get_course_id(), 3 , $id2 , api_get_session_id());
  655. $count = 0;
  656. if ($link_id !== false) {
  657. $gradebook_data = get_resource_from_course_gradebook($link_id);
  658. $count = $gradebook_data['weight'];
  659. }
  660. if ($count > 0) {
  661. $add_to_name = ' / <span style="color:blue">'.get_lang('IncludedInEvaluation').'</span>';
  662. } else {
  663. $add_to_name = '';
  664. }
  665. $work_title = !empty($work_data['title']) ? $work_data['title'] : basename($work_data['url']);
  666. //Work name
  667. //if (api_is_allowed_to_edit()) {
  668. if ($cant_files > 0 ) {
  669. $zip = '<a href="downloadfolder.inc.php?id='.$work_data['id'].'">'.Display::return_icon('save_pack.png', get_lang('Save'), array('style' => 'float:right;'), ICON_SIZE_SMALL).'</a>';
  670. }
  671. //}
  672. $url = $zip.'<a href="'.api_get_self().'?'.api_get_cidreq().'&origin='.$origin.'&gradebook='.Security::remove_XSS($_GET['gradebook']).'&id='.$work_data['id'].'"'.$class.'>'.
  673. $work_title.'</a>'.
  674. $add_to_name.'<br />'.$cant_files.' '.$text_file.$dirtext;
  675. $row[] = $url;
  676. }
  677. if ($count_files != 0) {
  678. $row[] = '';
  679. }
  680. if ($direc_date != '' && $direc_date != '0000-00-00 00:00:00') {
  681. $direc_date_local = api_get_local_time($direc_date);
  682. $row[] = date_to_str_ago($direc_date_local).'<br /><span class="dropbox_date">'.api_format_date($direc_date_local).'</span>';
  683. } else {
  684. $direc_date_local = '0000-00-00 00:00:00';
  685. $row[] = '';
  686. }
  687. if ($origin != 'learnpath') {
  688. if ($is_allowed_to_edit) {
  689. $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(), ICON_SIZE_SMALL).'</a>';
  690. $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'),'',ICON_SIZE_SMALL).'</a>';
  691. $row[] = $action;
  692. } else {
  693. $row[] = '';
  694. }
  695. }
  696. $row[] = $direc_date_local;
  697. $row[] = $work_data['title'];
  698. $table_data[] = $row;
  699. }
  700. }
  701. //If an id was loaded we loop through the documents
  702. $my_assignment = get_work_assignment_by_id($id);
  703. if (Database::num_rows($sql_result) > 0) {
  704. while ($work = Database::fetch_object($sql_result)) {
  705. //Get the author ID for that document from the item_property table
  706. $is_author = false;
  707. $item_property_data = api_get_item_property_info(api_get_course_int_id(), 'work', $work->id, api_get_session_id());
  708. if (!$is_allowed_to_edit && $item_property_data['insert_user_id'] == api_get_user_id()) {
  709. $is_author = true;
  710. }
  711. $user_info = api_get_user_info($item_property_data['insert_user_id']);
  712. //display info depending on the permissions
  713. if ($is_author && $work->accepted == '1' || $is_allowed_to_edit || CourseManager::is_course_teacher($item_property_data['insert_user_id'], $_course['code'])) {
  714. $row = array();
  715. if ($work->accepted == '0') {
  716. $class = 'class="invisible"';
  717. } else {
  718. $class = '';
  719. }
  720. $qualification_string = '';
  721. if ($qualification_exists) {
  722. if ($work->qualification == '') {
  723. $qualification_string = '<b style="color:orange"> - </b>';
  724. } else {
  725. $qualification_string = '<b style="color:blue">'.$work->qualification.'</b>';
  726. }
  727. }
  728. $add_string = '';
  729. $time_expires = api_strtotime($my_assignment['expires_on'], 'UTC');
  730. if (!empty($my_assignment['expires_on']) && $my_assignment['expires_on'] != '0000-00-00 00:00:00' && $time_expires < api_strtotime($work->sent_date, 'UTC')) {
  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;'), ICON_SIZE_SMALL).' '.$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(), ICON_SIZE_SMALL).'</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(), ICON_SIZE_SMALL).'</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(), ICON_SIZE_SMALL).'</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(), ICON_SIZE_SMALL).'</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(), ICON_SIZE_SMALL).'</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'),'',ICON_SIZE_SMALL).'</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(), ICON_SIZE_SMALL).'</a>';
  775. } else {
  776. $action .= Display::return_icon('edit_na.png', get_lang('Modify'),array(), ICON_SIZE_SMALL);
  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'),'',ICON_SIZE_SMALL).'</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. $origin = isset($_REQUEST['origin']) && !empty($_REQUEST['origin']) ? api_get_tools_lists($_REQUEST['origin']) : '';
  1246. $js = '<script>
  1247. function plus() {
  1248. if(document.getElementById(\'options\').style.display == \'none\') {
  1249. document.getElementById(\'options\').style.display = \'block\';
  1250. document.getElementById(\'plus\').innerHTML=\'&nbsp;'.Display::return_icon('div_hide.gif', get_lang('Hide', ''), array('style' => 'vertical-align:middle')).'&nbsp;'.addslashes(get_lang('AdvancedParameters', '')).'\';
  1251. } else {
  1252. document.getElementById(\'options\').style.display = \'none\';
  1253. document.getElementById(\'plus\').innerHTML=\'&nbsp;'.Display::return_icon('div_show.gif', get_lang('Show', ''), array('style' => 'vertical-align:middle')).'&nbsp;'.addslashes(get_lang('AdvancedParameters', '')).'\';
  1254. }
  1255. }
  1256. function updateDocumentTitle(value) {
  1257. var temp = value.indexOf("/");
  1258. //linux path
  1259. if(temp!=-1){
  1260. var temp=value.split("/");
  1261. } else {
  1262. var temp=value.split("\\\");
  1263. }
  1264. document.getElementById("file_upload").value=temp[temp.length-1];
  1265. $("#contains_file_id").attr("checked", true);
  1266. }
  1267. function checkDate(month, day, year) {
  1268. var monthLength =
  1269. new Array(31,28,31,30,31,30,31,31,30,31,30,31);
  1270. if (!day || !month || !year)
  1271. return false;
  1272. // check for bisestile year
  1273. if (year/4 == parseInt(year/4))
  1274. monthLength[1] = 29;
  1275. if (month < 1 || month > 12)
  1276. return false;
  1277. if (day > monthLength[month-1])
  1278. return false;
  1279. return true;
  1280. }
  1281. function mktime() {
  1282. var no, ma = 0, mb = 0, i = 0, d = new Date(), argv = arguments, argc = argv.length;
  1283. d.setHours(0,0,0); d.setDate(1); d.setMonth(1); d.setYear(1972);
  1284. var dateManip = {
  1285. 0: function(tt){ return d.setHours(tt); },
  1286. 1: function(tt){ return d.setMinutes(tt); },
  1287. 2: function(tt){ set = d.setSeconds(tt); mb = d.getDate() - 1; return set; },
  1288. 3: function(tt){ set = d.setMonth(parseInt(tt)-1); ma = d.getFullYear() - 1972; return set; },
  1289. 4: function(tt){ return d.setDate(tt+mb); },
  1290. 5: function(tt){ return d.setYear(tt+ma); }
  1291. };
  1292. for( i = 0; i < argc; i++ ){
  1293. no = parseInt(argv[i]*1);
  1294. if (isNaN(no)) {
  1295. return false;
  1296. } else {
  1297. // arg is number, lets manipulate date object
  1298. if(!dateManip[i](no)){
  1299. // failed
  1300. return false;
  1301. }
  1302. }
  1303. }
  1304. return Math.floor(d.getTime()/1000);
  1305. }
  1306. function validate() {
  1307. var expires_day = document.form1.expires_day.value;
  1308. var expires_month = document.form1.expires_month.value;
  1309. var expires_year = document.form1.expires_year.value;
  1310. var expires_hour = document.form1.expires_hour.value;
  1311. var expires_minute = document.form1.expires_minute.value;
  1312. var expires_date = mktime(expires_hour,expires_minute,0,expires_month,expires_day,expires_year)
  1313. var ends_day = document.form1.ends_day.value;
  1314. var ends_month = document.form1.ends_month.value;
  1315. var ends_year = document.form1.ends_year.value;
  1316. var ends_hour = document.form1.ends_hour.value;
  1317. var ends_minute = document.form1.ends_minute.value;
  1318. var ends_date = mktime(ends_hour,ends_minute,0,ends_month,ends_day,ends_year);
  1319. var new_dir = document.form1.new_dir.value;
  1320. msg_id1 = document.getElementById("msg_error1");
  1321. msg_id2 = document.getElementById("msg_error2");
  1322. msg_id3 = document.getElementById("msg_error3");
  1323. msg_id4 = document.getElementById("msg_error4");
  1324. msg_id5 = document.getElementById("msg_error_weight");
  1325. if(new_dir=="") {
  1326. msg_id1.style.display ="block";
  1327. msg_id1.innerHTML="'.get_lang('FieldRequired', '').'";
  1328. msg_id2.innerHTML="";msg_id3.innerHTML="";msg_id4.innerHTML="";msg_id5.innerHTML="";
  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. } else if (!checkDate(expires_month,expires_day,expires_year)) {
  1334. msg_id3.style.display ="block";
  1335. msg_id3.innerHTML="'.get_lang('InvalidDate', '').'";
  1336. msg_id1.innerHTML="";msg_id2.innerHTML="";msg_id4.innerHTML="";msg_id5.innerHTML="";
  1337. } else if (!checkDate(ends_month,ends_day,ends_year)) {
  1338. msg_id4.style.display ="block";
  1339. msg_id4.innerHTML="'.get_lang('InvalidDate', '').'";
  1340. msg_id1.innerHTML="";msg_id2.innerHTML="";msg_id3.innerHTML="";msg_id5.innerHTML="";
  1341. } else {
  1342. if (document.form1.make_calification.checked) {
  1343. var weight = document.form1.weight.value;
  1344. if(weight=="") {
  1345. msg_id5.style.display ="block";
  1346. msg_id5.innerHTML="'.get_lang('WeightNecessary', '').'";
  1347. msg_id1.innerHTML="";msg_id2.innerHTML="";msg_id3.innerHTML="";msg_id4.innerHTML="";
  1348. return false;
  1349. }
  1350. }
  1351. document.form1.action = "work.php?'. api_get_cidreq().'&origin='.$origin.'&gradebook='.(empty($_GET['gradebook'])?'':'view').'";
  1352. document.form1.submit();
  1353. }
  1354. }
  1355. </script>';
  1356. return $js;
  1357. }
  1358. /**
  1359. * Gets the id of a student publication with a given path
  1360. * @param string $path
  1361. * @return true if is found / false if not found
  1362. */
  1363. // TODO: The name of this function does not fit with the kind of information it returns. Maybe check_work_id() or is_work_id()?
  1364. function get_work_id($path) {
  1365. $TBL_STUDENT_PUBLICATION = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
  1366. $TBL_PROP_TABLE = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1367. $course_id = api_get_course_int_id();
  1368. if (api_is_allowed_to_edit()) {
  1369. $sql = "SELECT work.id FROM $TBL_STUDENT_PUBLICATION AS work, $TBL_PROP_TABLE AS props
  1370. 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'";
  1371. } else {
  1372. $sql = "SELECT work.id FROM $TBL_STUDENT_PUBLICATION AS work,$TBL_PROP_TABLE AS props
  1373. 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()."'";
  1374. }
  1375. $result = Database::query($sql);
  1376. $num_rows = Database::num_rows($result);
  1377. if ($result && $num_rows > 0) {
  1378. return true;
  1379. } else {
  1380. return false;
  1381. }
  1382. }
  1383. /**
  1384. * Get list of users who have not given the task
  1385. * @param int
  1386. * @return array
  1387. * @author cvargas
  1388. * @author Julio Montoya <gugli100@gmail.com> Fixing query
  1389. */
  1390. function get_list_users_without_publication($task_id) {
  1391. $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  1392. $table_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  1393. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  1394. $session_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  1395. //condition for the session
  1396. $session_id = api_get_session_id();
  1397. $course_id = api_get_course_int_id();
  1398. $task_id = intval($task_id);
  1399. if ($session_id == 0) {
  1400. $sql = "SELECT user_id as id FROM $work_table WHERE c_id = $course_id AND parent_id='$task_id'";
  1401. } else {
  1402. $sql = "SELECT user_id as id FROM $work_table WHERE c_id = $course_id AND parent_id='$task_id' and session_id='".$session_id."'";
  1403. }
  1404. $result = Database::query($sql);
  1405. $users_with_tasks = array();
  1406. while($row = Database::fetch_array($result)) {
  1407. $users_with_tasks[] = $row['id'];
  1408. }
  1409. if ($session_id == 0){
  1410. $sql_users = "SELECT cu.user_id, u.lastname, u.firstname, u.email FROM $table_course_user AS cu, $table_user AS u
  1411. WHERE u.status!=1 and cu.course_code='".api_get_course_id()."' AND u.user_id=cu.user_id";
  1412. } else {
  1413. $sql_users = "SELECT cu.id_user, u.lastname, u.firstname, u.email FROM $session_course_rel_user AS cu, $table_user AS u
  1414. 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."'";
  1415. }
  1416. $group_id = api_get_group_id();
  1417. $new_group_user_list = array();
  1418. if ($group_id) {
  1419. $group_user_list = GroupManager::get_subscribed_users($group_id);
  1420. if (!empty($group_user_list)) {
  1421. foreach($group_user_list as $group_user) {
  1422. $new_group_user_list[] = $group_user['user_id'];
  1423. }
  1424. }
  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. if ($group_id && !in_array($row_users[0], $new_group_user_list)) {
  1431. continue;
  1432. }
  1433. //$user_id = array_shift($row_users);
  1434. $row_users[0] = $row_users[1];
  1435. $row_users[1] = $row_users[2];
  1436. $row_users[2] = Display::encrypted_mailto_link($row_users[3]);
  1437. $users_without_tasks[] = $row_users;
  1438. }
  1439. return $users_without_tasks;
  1440. }
  1441. /**
  1442. * Display list of users who have not given the task
  1443. *
  1444. * @param int task id
  1445. * @return array
  1446. * @author cvargas carlos.vargas@beeznest.com cfasanando, christian.fasanado@beeznest.com
  1447. * @author Julio Montoya <gugli100@gmail.com> Fixes
  1448. */
  1449. function display_list_users_without_publication($task_id) {
  1450. global $origin;
  1451. $table_header[] = array(get_lang('LastName'), true);
  1452. $table_header[] = array(get_lang('FirstName'), true);
  1453. $table_header[] = array(get_lang('Email'), true);
  1454. // table_data
  1455. $table_data = get_list_users_without_publication($task_id);
  1456. $sorting_options = array();
  1457. $sorting_options['column'] = 1;
  1458. $paging_options = array();
  1459. $my_params = array();
  1460. if (isset($_GET['curdirpath'])) {
  1461. $my_params['curdirpath'] = Security::remove_XSS($_GET['curdirpath']);
  1462. }
  1463. if (isset($_GET['edit_dir'])) {
  1464. $my_params['edit_dir'] = Security::remove_XSS($_GET['edit_dir']);
  1465. }
  1466. if (isset($_GET['list'])) {
  1467. $my_params['list'] = Security::remove_XSS($_GET['list']);
  1468. }
  1469. $my_params['origin'] = $origin;
  1470. //$column_show
  1471. $column_show[] = 1;
  1472. $column_show[] = 1;
  1473. $column_show[] = 1;
  1474. Display::display_sortable_config_table('work', $table_header, $table_data, $sorting_options, $paging_options, $my_params, $column_show);
  1475. }
  1476. /**
  1477. * Send reminder to users who have not given the task
  1478. *
  1479. * @param int
  1480. * @return array
  1481. * @author cvargas carlos.vargas@beeznest.com cfasanando, christian.fasanado@beeznest.com
  1482. */
  1483. function send_reminder_users_without_publication($task_data) {
  1484. global $_course;
  1485. $sender_name = api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'), null, PERSON_NAME_EMAIL_ADDRESS);
  1486. $task_id = $task_data['id'];
  1487. $task_title = !empty($task_data['title']) ? $task_data['title'] : basename($task_data['url']);
  1488. $subject = '[' . api_get_setting('siteName') . '] ';
  1489. // The body can be as long as you wish, and any combination of text and variables
  1490. $content = get_lang('ReminderToSubmitPendingTask')."\n".get_lang('CourseName').' : '.$_course['name']."\n";
  1491. $content .= get_lang('WorkName').' : '.$task_title."\n";
  1492. $list_users = get_list_users_without_publication($task_id);
  1493. $mails_sent_to = array();
  1494. foreach ($list_users as $user) {
  1495. $name_user = api_get_person_name($user[1], $user[0], null, PERSON_NAME_EMAIL_ADDRESS);
  1496. $dear_line = get_lang('Dear')." ".api_get_person_name($user[1], $user[0]) .", \n\n";
  1497. $body = $dear_line.$content;
  1498. api_mail($name_user, $user[3], $subject, $body, $sender_name, $email_admin);
  1499. $mails_sent_to[] = $name_user;
  1500. }
  1501. return $mails_sent_to;
  1502. }
  1503. /**
  1504. * Sends an email to the students of a course when a homework is created
  1505. *
  1506. * @param string course_id
  1507. *
  1508. * @author Guillaume Viguier <guillaume.viguier@beeznest.com>
  1509. * @author Julio Montoya <gugli100@gmail.com> Adding session support - 2011
  1510. */
  1511. function send_email_on_homework_creation($course_id) {
  1512. // Get the students of the course
  1513. $session_id = api_get_session_id();
  1514. if (empty($session_id)) {
  1515. $students = CourseManager::get_student_list_from_course_code($course_id);
  1516. } else {
  1517. $students = CourseManager::get_student_list_from_course_code($course_id, true, $session_id);
  1518. }
  1519. $emailsubject = '[' . api_get_setting('siteName') . '] '.get_lang('HomeworkCreated');
  1520. $currentUser = api_get_user_info(api_get_user_id());
  1521. if (!empty($students)) {
  1522. foreach($students as $student) {
  1523. $user_info = api_get_user_info($student["user_id"]);
  1524. if(!empty($user_info["mail"])) {
  1525. $name_user = api_get_person_name($user_info["firstname"], $user_info["lastname"], null, PERSON_NAME_EMAIL_ADDRESS);
  1526. $emailbody = get_lang('Dear')." ".$name_user.",\n\n";
  1527. $emailbody .= get_lang('HomeworkHasBeenCreatedForTheCourse')." ".$course_id.". "."\n\n".get_lang('PleaseCheckHomeworkPage');
  1528. $emailbody .= "\n\n".api_get_person_name($currentUser["firstname"], $currentUser["lastname"]);
  1529. @api_mail($name_user, $user_info["mail"], $emailsubject, $emailbody, api_get_person_name($currentUser["firstname"], $currentUser["lastname"], null, PERSON_NAME_EMAIL_ADDRESS), $currentUser["mail"]);
  1530. }
  1531. }
  1532. }
  1533. }
  1534. function is_work_exist_by_url($url) {
  1535. $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  1536. $url = Database::escape_string($url);
  1537. $sql = "SELECT id FROM $work_table WHERE url='$url'";
  1538. $result = Database::query($sql);
  1539. if (Database::num_rows($result)> 0) {
  1540. $row = Database::fetch_row($result);
  1541. if (empty($row)) {
  1542. return false;
  1543. } else {
  1544. return true;
  1545. }
  1546. } else {
  1547. return false;
  1548. }
  1549. }
  1550. function make_select($name, $values, $checked = '') {
  1551. $output = '<select name="'.$name.'" id="'.$name.'">';
  1552. foreach($values as $key => $value) {
  1553. $output .= '<option value="'.$key.'" '.(($checked==$key) ? 'selected="selected"' : '').'>'.$value.'</option>';
  1554. }
  1555. $output .= '</select>';
  1556. return $output;
  1557. }
  1558. function make_checkbox($name, $checked = '', $label = null) {
  1559. $check = '<input id ="'.$name.'" type="checkbox" value="1" name="'.$name.'" '.((!empty($checked))?'checked="checked"':'').'/>';
  1560. if (!empty($label)) {
  1561. $check .="<label for ='$name'>$label</label>";
  1562. }
  1563. return $check;
  1564. }
  1565. function draw_date_picker($prefix, $default = '') {
  1566. if (empty($default)) {
  1567. $default = api_get_local_time();
  1568. }
  1569. $parts = explode(' ', $default);
  1570. list($d_year, $d_month, $d_day) = explode('-', $parts[0]);
  1571. list($d_hour, $d_minute) = explode(':', $parts[1]);
  1572. $minute = range(10, 59);
  1573. array_unshift($minute, '00', '01', '02', '03', '04', '05', '06', '07', '08', '09');
  1574. $date_form = make_select($prefix.'_day', array_combine(range(1, 31), range(1, 31)), $d_day);
  1575. $date_form .= make_select($prefix.'_month', array_combine(range(1, 12), api_get_months_long()), $d_month);
  1576. $date_form .= make_select($prefix.'_year', array($d_year => $d_year, $d_year + 1 => $d_year + 1), $d_year).'&nbsp;&nbsp;&nbsp;&nbsp;';
  1577. $date_form .= make_select($prefix.'_hour', array_combine(range(0, 23), range(0, 23)), $d_hour).' : ';
  1578. $date_form .= make_select($prefix.'_minute', $minute, $d_minute);
  1579. return $date_form;
  1580. }
  1581. function get_date_from_select($prefix) {
  1582. 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';
  1583. }