work.lib.php 97 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475
  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(SYS_CODE_PATH).'gradebook/lib/gradebook_functions.inc.php';
  17. /**
  18. * Displays action links (for admins, authorized groups members and authorized students)
  19. * @param string Current dir
  20. * @param integer Whether to show tool options
  21. * @param integer Whether to show upload form option
  22. * @return void
  23. */
  24. function display_action_links($id, $cur_dir_path, $show_tool_options, $display_upload_link, $action)
  25. {
  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($id)) {
  34. $display_output .= '<a href="'.api_get_self().'?'.api_get_cidreq(
  35. ).'&origin='.$origin.'&gradebook='.$gradebook.'&id='.$my_back_id.'">'.Display::return_icon(
  36. 'back.png',
  37. get_lang('BackToWorksList'),
  38. '',
  39. ICON_SIZE_MEDIUM
  40. ).'</a>';
  41. }
  42. if ($show_tool_options && api_is_allowed_to_edit(null, true) && $origin != 'learnpath') {
  43. // Create dir
  44. if (empty($id)) {
  45. $display_output .= '<a href="'.api_get_self().'?'.api_get_cidreq(
  46. ).'&amp;action=create_dir&origin='.$origin.'&gradebook='.$gradebook.'">';
  47. $display_output .= Display::return_icon(
  48. 'new_work.png',
  49. get_lang('CreateAssignment'),
  50. '',
  51. ICON_SIZE_MEDIUM
  52. ).'</a>';
  53. }
  54. if (empty($id)) {
  55. // Options
  56. $display_output .= '<a href="'.api_get_self().'?'.api_get_cidreq(
  57. ).'&amp;action=settings&amp;origin='.$origin.'&amp;gradebook='.$gradebook.'">';
  58. $display_output .= Display::return_icon(
  59. 'settings.png',
  60. get_lang('EditToolOptions'),
  61. '',
  62. ICON_SIZE_MEDIUM
  63. ).'</a>';
  64. }
  65. }
  66. if ($display_upload_link && api_is_allowed_to_session_edit(false, true) && !empty($id)) {
  67. $display_output .= '<a href="'.api_get_self().'?'.api_get_cidreq(
  68. ).'&id='.$id.'&action=upload_form&origin='.$origin.'&gradebook='.$gradebook.'">';
  69. $display_output .= Display::return_icon(
  70. 'upload_file.png',
  71. get_lang('UploadADocument'),
  72. '',
  73. ICON_SIZE_MEDIUM
  74. ).'</a>';
  75. }
  76. if (api_is_allowed_to_edit(null, true) && $origin != 'learnpath' && api_is_allowed_to_session_edit(false, true)) {
  77. // Delete all files
  78. if (api_get_setting('permanently_remove_deleted_files') == 'true') {
  79. $message = get_lang('ConfirmYourChoiceDeleteAllfiles');
  80. } else {
  81. $message = get_lang('ConfirmYourChoice');
  82. }
  83. }
  84. if (api_is_allowed_to_edit(null, true)) {
  85. global $token;
  86. if (!empty($id)) {
  87. if (empty($_GET['list']) or Security::remove_XSS($_GET['list']) == 'with') {
  88. $display_output .= '<a href="'.api_get_self().'?'.api_get_cidreq(
  89. ).'&amp;id='.$id.'&amp;curdirpath='.$cur_dir_path.'&amp;origin='.$origin.'&amp;gradebook='.$gradebook.'&amp;list=without">'.
  90. Display::return_icon(
  91. 'exercice_uncheck.png',
  92. get_lang('ViewUsersWithoutTask'),
  93. '',
  94. ICON_SIZE_MEDIUM
  95. )."</a>\n";
  96. $count = get_count_work($id);
  97. if ($count > 0) {
  98. $display_output .= '<a href="downloadfolder.inc.php?id='.$id.'">'.Display::return_icon(
  99. 'save_pack.png',
  100. get_lang('Save'),
  101. array('style' => 'float:right;'),
  102. ICON_SIZE_MEDIUM
  103. ).'</a>';
  104. }
  105. } else {
  106. $display_output .= '<a href="'.api_get_self().'?'.api_get_cidreq(
  107. ).'&amp;id='.$id.'&amp;curdirpath='.$cur_dir_path.'&amp;origin='.$origin.'&amp;gradebook='.$gradebook.'&amp;list=with">'.
  108. Display::return_icon(
  109. 'exercice_check.png',
  110. get_lang('ViewUsersWithTask'),
  111. '',
  112. ICON_SIZE_MEDIUM
  113. )."</a>\n";
  114. $display_output .= '<a href="downloadfolder.inc.php?id='.$id.'">'.Display::return_icon(
  115. 'save_pack.png',
  116. get_lang('Save'),
  117. array('style' => 'float:right;'),
  118. ICON_SIZE_MEDIUM
  119. ).'</a>';
  120. if (!isset($_GET['action']) || (isset($_GET['action']) && $_GET['action'] != 'send_mail')) {
  121. $display_output .= '<a href="'.api_get_self().'?'.api_get_cidreq(
  122. ).'&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.'">'.
  123. Display::return_icon('mail_send.png', get_lang('ReminderMessage'), '', ICON_SIZE_MEDIUM)."</a>";
  124. } else {
  125. $display_output .= Display::return_icon(
  126. 'mail_send_na.png',
  127. get_lang('ReminderMessage'),
  128. '',
  129. ICON_SIZE_MEDIUM
  130. );
  131. }
  132. }
  133. }
  134. }
  135. if ($display_output != '') {
  136. echo '<div class="actions">';
  137. echo $display_output;
  138. echo '</div>';
  139. }
  140. }
  141. /**
  142. * Displays all options for this tool.
  143. * These are
  144. * - make all files visible / invisible
  145. * - set the default visibility of uploaded files
  146. *
  147. * @param $uploadvisibledisabled
  148. * @param $origin
  149. */
  150. function display_tool_options($uploadvisibledisabled, $origin)
  151. {
  152. global $gradebook;
  153. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  154. if (!$is_allowed_to_edit) {
  155. return;
  156. }
  157. echo '<form class="form-horizontal" method="post" action="'.api_get_self(
  158. ).'?origin='.$origin.'&gradebook='.$gradebook.'&action=settings">';
  159. echo '<legend>'.get_lang('EditToolOptions').'</legend>';
  160. display_default_visibility_form($uploadvisibledisabled);
  161. display_studentsdelete_form();
  162. echo '<div class="row">
  163. <div class="formw">
  164. <button type="submit" class="save" name="changeProperties" value="'.get_lang('Ok').'">'.get_lang(
  165. 'Ok'
  166. ).'</button>
  167. </div>
  168. </div>';
  169. echo '</form>';
  170. }
  171. /**
  172. * Displays the form where course admins can specify wether uploaded documents
  173. * are visible or invisible by default.
  174. *
  175. * @param $uploadvisibledisabled
  176. * @param $origin
  177. */
  178. function display_default_visibility_form($uploadvisibledisabled)
  179. {
  180. ?>
  181. <div class="control-group">
  182. <label class="control-label">
  183. <?php echo get_lang('DefaultUpload'); ?>
  184. </label>
  185. <div class="controls">
  186. <label class="radio" for="uploadvisibledisabled_1">
  187. <input id="uploadvisibledisabled_1" class="checkbox" type="radio" name="uploadvisibledisabled"
  188. value="0" <?php if ($uploadvisibledisabled == 0) {
  189. echo 'checked';
  190. } ?> />
  191. <?php echo get_lang('NewVisible'); ?>
  192. </label>
  193. <label class="radio" for="uploadvisibledisabled_2">
  194. <input id="uploadvisibledisabled_2" class="checkbox" type="radio" name="uploadvisibledisabled"
  195. value="1" <?php if ($uploadvisibledisabled == 1) {
  196. echo 'checked';
  197. } ?> />
  198. <?php echo get_lang('NewUnvisible'); ?>
  199. </label>
  200. </div>
  201. </div>
  202. <?php
  203. }
  204. /**
  205. * Display a part of the form to edit the settings of the tool
  206. * In this case weither the students are allowed to delete their own publication or not (by default not)
  207. *
  208. * @return html code
  209. * @since Dokeos 1.8.6.2
  210. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  211. */
  212. function display_studentsdelete_form()
  213. {
  214. // by default api_get_course_setting returns -1 and the code only expects 0 or 1 so anything tha
  215. // is different than 1 will be converted into 0
  216. $current_course_setting_value = api_get_course_setting('student_delete_own_publication');
  217. if ($current_course_setting_value != 1) {
  218. $current_course_setting_value = 0;
  219. }
  220. ?>
  221. <div class="control-group">
  222. <label class="control-label"><?php echo get_lang('StudentAllowedToDeleteOwnPublication'); ?></label>
  223. <div class="controls">
  224. <label class="radio" for="student_delete_own_publication_2">
  225. <input id="student_delete_own_publication_2" class="checkbox" type="radio"
  226. name="student_delete_own_publication"
  227. value="1" <?php if ($current_course_setting_value == 1) {
  228. echo 'checked';
  229. } ?> />
  230. <?php echo get_lang('Yes'); ?>
  231. </label>
  232. <label class="radio" for="student_delete_own_publication_1">
  233. <input id="student_delete_own_publication_1" class="checkbox" type="radio"
  234. name="student_delete_own_publication" value="0"
  235. <?php if ($current_course_setting_value == 0) {
  236. echo 'checked';
  237. } ?> />
  238. <?php echo get_lang('No'); ?>
  239. </label>
  240. </div>
  241. </div>
  242. <?php
  243. }
  244. /**
  245. * converts 2008-10-06 12:45:00 to timestamp
  246. * @deprecated any calls found
  247. */
  248. function convert_date_to_number($default)
  249. {
  250. // 2008-10-12 00:00:00 ---to--> 12345672218 (timestamp)
  251. $parts = split(' ', $default);
  252. list($d_year, $d_month, $d_day) = split('-', $parts[0]);
  253. list($d_hour, $d_minute, $d_second) = split(':', $parts[1]);
  254. return mktime((int)$d_hour, (int)$d_minute, (int)$d_second, (int)$d_month, (int)$d_day, (int)$d_year);
  255. }
  256. /**
  257. * converts 1-9 to 01-09
  258. */
  259. function two_digits($number)
  260. {
  261. $number = (int)$number;
  262. return ($number < 10) ? '0'.$number : $number;
  263. }
  264. /**
  265. * converts 2008-10-06 12:45:00 to -> array($data'year'=>2008,$data'month'=>10 etc...)
  266. */
  267. function convert_date_to_array($date, $group)
  268. {
  269. $parts = split(' ', $date);
  270. $date_parts = split('-', $parts[0]);
  271. $date_parts_tmp = array();
  272. foreach ($date_parts as $item) {
  273. $date_parts_tmp[] = intval($item);
  274. }
  275. $time_parts = split(':', $parts[1]);
  276. $time_parts_tmp = array();
  277. foreach ($time_parts as $item) {
  278. $time_parts_tmp[] = intval($item);
  279. }
  280. list($data[$group.'[year]'], $data[$group.'[month]'], $data[$group.'[day]']) = $date_parts_tmp;
  281. list($data[$group.'[hour]'], $data[$group.'[minute]']) = $time_parts_tmp;
  282. return $data;
  283. }
  284. /**
  285. * get date from a group of date
  286. */
  287. function get_date_from_group($group)
  288. {
  289. return $_POST[$group]['year'].'-'.two_digits($_POST[$group]['month']).'-'.two_digits(
  290. $_POST[$group]['day']
  291. ).' '.two_digits($_POST[$group]['hour']).':'.two_digits($_POST[$group]['minute']).':00';
  292. }
  293. /**
  294. * create a group of select from a date
  295. */
  296. function create_group_date_select($prefix = '')
  297. {
  298. $minute = range(10, 59);
  299. $d_year = date('Y');
  300. array_unshift($minute, '00', '01', '02', '03', '04', '05', '06', '07', '08', '09');
  301. $group_name[] = FormValidator :: createElement(
  302. 'select',
  303. $prefix.'day',
  304. '',
  305. array_combine(range(1, 31), range(1, 31))
  306. );
  307. $group_name[] = FormValidator :: createElement(
  308. 'select',
  309. $prefix.'month',
  310. '',
  311. array_combine(range(1, 12), api_get_months_long())
  312. );
  313. $group_name[] = FormValidator :: createElement(
  314. 'select',
  315. $prefix.'year',
  316. '',
  317. array($d_year => $d_year, $d_year + 1 => $d_year + 1)
  318. );
  319. $group_name[] = FormValidator :: createElement(
  320. 'select',
  321. $prefix.'hour',
  322. '',
  323. array_combine(range(0, 23), range(0, 23))
  324. );
  325. $group_name[] = FormValidator :: createElement('select', $prefix.'minute', '', $minute);
  326. return $group_name;
  327. }
  328. function get_work_data_by_path($path)
  329. {
  330. $path = Database::escape_string($path);
  331. $course_id = api_get_course_int_id();
  332. $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  333. $sql = "SELECT * FROM ".$work_table." WHERE url = '$path' AND c_id = $course_id ";
  334. $result = Database::query($sql);
  335. $return = array();
  336. if (Database::num_rows($result)) {
  337. $return = Database::fetch_array($result, 'ASSOC');
  338. }
  339. return $return;
  340. }
  341. function get_work_data_by_id($id)
  342. {
  343. $id = intval($id);
  344. $course_id = api_get_course_int_id();
  345. $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  346. $sql = "SELECT * FROM $work_table WHERE id = $id AND c_id = $course_id";
  347. $result = Database::query($sql);
  348. $return = array();
  349. if (Database::num_rows($result)) {
  350. $return = Database::fetch_array($result, 'ASSOC');
  351. }
  352. return $return;
  353. }
  354. function get_work_count_by_student($user_id, $work_id)
  355. {
  356. $user_id = intval($user_id);
  357. $work_id = intval($work_id);
  358. $course_id = api_get_course_int_id();
  359. $session_id = api_get_session_id();
  360. $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  361. $sql = "SELECT COUNT(*) as count FROM $work_table
  362. WHERE c_id = $course_id AND parent_id = $work_id AND user_id = $user_id AND active = 1 AND session_id = $session_id ";
  363. $result = Database::query($sql);
  364. $return = 0;
  365. if (Database::num_rows($result)) {
  366. $return = Database::fetch_row($result, 'ASSOC');
  367. $return = intval($return[0]);
  368. }
  369. return $return;
  370. }
  371. function get_work_assignment_by_id($id)
  372. {
  373. $id = intval($id);
  374. $course_id = api_get_course_int_id();
  375. $table = Database :: get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT);
  376. $sql = "SELECT * FROM $table WHERE c_id = $course_id AND publication_id = $id";
  377. $result = Database::query($sql);
  378. $return = array();
  379. if (Database::num_rows($result)) {
  380. $return = Database::fetch_array($result, 'ASSOC');
  381. }
  382. return $return;
  383. }
  384. /**
  385. * Display the list of student publications, taking into account the user status
  386. *
  387. * @param $link_target_parameter - should there be a target parameter for the links
  388. * @param $dateFormatLong - date format
  389. * @param $origin - typically empty or 'learnpath'
  390. */
  391. function display_student_publications_list(
  392. $id,
  393. $link_target_parameter,
  394. $dateFormatLong,
  395. $origin,
  396. $add_in_where_query = ''
  397. ) {
  398. global $timeNoSecFormat, $dateFormatShort, $gradebook, $_course;
  399. // Database table names
  400. $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  401. $iprop_table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  402. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  403. $work_assigment = Database::get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT);
  404. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  405. $user_id = api_get_user_id();
  406. $session_id = api_get_session_id();
  407. $condition_session = api_get_session_condition($session_id);
  408. $course_id = api_get_course_int_id();
  409. $group_id = api_get_group_id();
  410. $course_info = api_get_course_info(api_get_course_id());
  411. $sort_params = array();
  412. if (isset($_GET['column'])) {
  413. $sort_params[] = 'column='.Security::remove_XSS($_GET['column']);
  414. }
  415. if (isset($_GET['page_nr'])) {
  416. $sort_params[] = 'page_nr='.Security::remove_XSS($_GET['page_nr']);
  417. }
  418. if (isset($_GET['per_page'])) {
  419. $sort_params[] = 'per_page='.Security::remove_XSS($_GET['per_page']);
  420. }
  421. if (isset($_GET['direction'])) {
  422. $sort_params[] = 'direction='.Security::remove_XSS($_GET['direction']);
  423. }
  424. $sort_params = implode('&amp;', $sort_params);
  425. $my_params = $sort_params;
  426. $origin = Security::remove_XSS($origin);
  427. // Getting the work data
  428. $my_folder_data = get_work_data_by_id($id);
  429. $qualification_exists = false;
  430. if (!empty($my_folder_data['qualification']) && intval($my_folder_data['qualification']) > 0) {
  431. $qualification_exists = true;
  432. }
  433. $work_dir = api_get_path(SYS_COURSE_PATH).$_course['path'].'/work';
  434. if (!empty($my_folder_data)) {
  435. $work_dir = api_get_path(SYS_COURSE_PATH).$_course['path'].'/work'.$my_folder_data['url'];
  436. }
  437. if (empty($my_folder_data)) {
  438. $link_info = is_resource_in_course_gradebook(api_get_course_id(), 3, $id, api_get_session_id());
  439. $work_in_gradebook_link_id = $link_info['id'];
  440. if ($work_in_gradebook_link_id) {
  441. if ($is_allowed_to_edit) {
  442. if (intval($my_folder_data['qualification']) == 0) {
  443. Display::display_warning_message(get_lang('MaxWeightNeedToBeProvided'));
  444. }
  445. }
  446. }
  447. $contains_file_query = '';
  448. //Get list from database
  449. if ($is_allowed_to_edit) {
  450. $active_condition = ' active IN (0, 1)';
  451. $sql_get_publications_list = "SELECT * FROM $work_table ".
  452. " WHERE c_id = $course_id $add_in_where_query $condition_session AND $active_condition AND ".
  453. " ( parent_id = 0) ".
  454. " $contains_file_query ".
  455. " ORDER BY sent_date DESC";
  456. } else {
  457. if (!empty($group_id)) {
  458. $group_query = " WHERE c_id = $course_id AND post_group_id = '".$group_id."' "; // set to select only messages posted by the user's group
  459. $subdirs_query = "AND parent_id = 0";
  460. } else {
  461. $group_query = " WHERE c_id = $course_id AND post_group_id = '0' ";
  462. $subdirs_query = "AND parent_id = 0";
  463. }
  464. //@todo how we can active or not an assignment?
  465. $active_condition = ' AND active IN (1,0)';
  466. $sql_get_publications_list = "SELECT * FROM $work_table $group_query $subdirs_query $add_in_where_query $active_condition $condition_session ORDER BY title";
  467. }
  468. $work_parents = array();
  469. $sql_result = Database::query($sql_get_publications_list);
  470. if (Database::num_rows($sql_result)) {
  471. while ($work = Database::fetch_object($sql_result)) {
  472. if ($work->parent_id == 0) {
  473. $work_parents[] = $work;
  474. }
  475. }
  476. }
  477. }
  478. $edit_dir = isset($_GET['edit_dir']) ? $_GET['edit_dir'] : '';
  479. $table_header = array();
  480. $table_has_actions_column = false;
  481. $table_header[] = array(get_lang('Type'), false, 'style="width:40px"');
  482. $table_header[] = array(get_lang('Title'), true);
  483. if (!empty($id)) {
  484. $table_header[] = array(get_lang('FirstName'), true);
  485. $table_header[] = array(get_lang('LastName'), true);
  486. if ($qualification_exists) {
  487. $table_header[] = array(get_lang('Qualification'), true);
  488. }
  489. }
  490. $table_header[] = array(get_lang('Date'), true, 'style="width:200px"');
  491. if ($is_allowed_to_edit) {
  492. $table_header[] = array(get_lang('Actions'), false, 'style="width:90px"', array('class' => 'td_actions'));
  493. $table_has_actions_column = true;
  494. }
  495. // the following column name seems both undefined and unused
  496. //$table_header[] = array('RealDate', true);
  497. $table_data = array();
  498. // List of all folders if no id was provided
  499. $group_id = api_get_group_id();
  500. if (is_array($work_parents)) {
  501. foreach ($work_parents as $work_parent) {
  502. $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 ".
  503. " FROM ".$iprop_table." prop INNER JOIN ".$work_table." work ON (prop.ref=work.id AND prop.c_id = $course_id ) ".
  504. " WHERE active IN (0, 1) AND ";
  505. if (!empty($group_id)) {
  506. $sql_select_directory .= " work.post_group_id = '".$group_id."' "; // set to select only messages posted by the user's group
  507. } else {
  508. $sql_select_directory .= " work.post_group_id = '0' ";
  509. }
  510. $sql_select_directory .= " AND ".
  511. " work.c_id = $course_id AND ".
  512. " work.id = ".$work_parent->id." AND ".
  513. " work.filetype = 'folder' AND ".
  514. " prop.tool='work' $condition_session";
  515. $result = Database::query($sql_select_directory);
  516. $row = Database::fetch_array($result, 'ASSOC');
  517. if (!$row) {
  518. // the folder belongs to another session
  519. continue;
  520. }
  521. $direc_date = $row['lastedit_date']; //directory's date
  522. $author = $row['author']; //directory's author
  523. $view_properties = $row['view_properties'];
  524. $is_assignment = $row['has_properties'];
  525. $id2 = $row['id']; //work id
  526. $locked = api_resource_is_locked_by_gradebook($id2, LINK_STUDENTPUBLICATION);
  527. if ($is_allowed_to_edit && $locked == false) {
  528. // form edit directory
  529. if (!empty($edit_dir) && $edit_dir == $id2) {
  530. if (!empty($row['has_properties'])) {
  531. $sql = Database::query(
  532. 'SELECT * FROM '.$work_assigment.' WHERE c_id = '.$course_id.' AND id = "'.$row['has_properties'].'" LIMIT 1'
  533. );
  534. $homework = Database::fetch_array($sql);
  535. }
  536. $form_folder = new FormValidator('edit_dir', 'post', api_get_self(
  537. ).'?origin='.$origin.'&gradebook='.$gradebook.'&edit_dir='.$id2);
  538. $form_folder->addElement('text', 'dir_name', get_lang('Title'));
  539. $form_folder->addElement('hidden', 'work_id', $id2);
  540. $form_folder->addRule('dir_name', get_lang('ThisFieldIsRequired'), 'required');
  541. $my_title = !empty($row['title']) ? $row['title'] : basename($row['url']);
  542. $defaults = array(
  543. 'dir_name' => Security::remove_XSS($my_title),
  544. 'description' => Security::remove_XSS($row['description'])
  545. );
  546. $form_folder->add_html_editor(
  547. 'description',
  548. get_lang('Description'),
  549. false,
  550. false,
  551. array('ToolbarSet' => 'work', 'Width' => '80%', 'Height' => '200')
  552. );
  553. $there_is_a_end_date = false;
  554. $form_folder->addElement(
  555. 'advanced_settings',
  556. '<a href="javascript://" onclick="javascript: return plus();" >
  557. <span id="plus">&nbsp;<img style="vertical-align:middle;" src="../img/div_show.gif" alt="" />
  558. &nbsp;'.get_lang('AdvancedParameters').'
  559. </span>
  560. </a>'
  561. );
  562. $form_folder->addElement('html', '<div id="options" style="display: none;">');
  563. if (empty($default)) {
  564. $default = api_get_local_time();
  565. }
  566. $parts = explode(' ', $default);
  567. list($d_year, $d_month, $d_day) = explode('-', $parts[0]);
  568. list($d_hour, $d_minute) = explode(':', $parts[1]);
  569. $qualification_input[] = FormValidator :: createElement('text', 'qualification');
  570. $form_folder->addGroup($qualification_input, 'qualification', get_lang('QualificationNumeric'));
  571. if (Gradebook::is_active()) {
  572. $link_info = is_resource_in_course_gradebook(
  573. api_get_course_id(),
  574. LINK_STUDENTPUBLICATION,
  575. $id2
  576. );
  577. $form_folder->addElement(
  578. 'checkbox',
  579. 'make_calification',
  580. null,
  581. get_lang('MakeQualifiable'),
  582. 'onclick="javascript: if(this.checked){document.getElementById(\'option3\').style.display = \'block\';}else{document.getElementById(\'option3\').style.display = \'none\';}"'
  583. );
  584. if (!empty($link_info)) {
  585. $form_folder->addElement('html', '<div id=\'option3\' style="display:block">');
  586. } else {
  587. $form_folder->addElement('html', '<div id=\'option3\' style="display:none">');
  588. }
  589. //Loading gradebook select
  590. load_gradebook_select_in_tool($form_folder);
  591. $weight_input2[] = FormValidator :: createElement('text', 'weight');
  592. $form_folder->addGroup($weight_input2, 'weight', get_lang('WeightInTheGradebook'), 'size="10"');
  593. $form_folder->addElement('html', '</div>');
  594. $defaults['weight[weight]'] = $link_info['weight'];
  595. if (!empty($link_info)) {
  596. $defaults['category_id'] = $link_info['category_id'];
  597. $defaults['make_calification'] = 1;
  598. }
  599. } else {
  600. $defaults['category_id'] = '';
  601. }
  602. if ($homework['expires_on'] != '0000-00-00 00:00:00') {
  603. $homework['expires_on'] = api_get_local_time($homework['expires_on']);
  604. $there_is_a_expire_date = true;
  605. $defaults['enableExpiryDate'] = true;
  606. $form_folder->addElement(
  607. 'checkbox',
  608. 'enableExpiryDate',
  609. null,
  610. get_lang('EnableExpiryDate'),
  611. 'onclick="javascript: if(this.checked){document.getElementById(\'option1\').style.display = \'block\';}else{document.getElementById(\'option1\').style.display = \'none\';}"'
  612. );
  613. $form_folder->addElement('html', '<div id=\'option1\' style="display:block">');
  614. $form_folder->addGroup(create_group_date_select(), 'expires', get_lang('ExpiresAt'));
  615. $form_folder->addElement('html', '</div>');
  616. } else {
  617. $homework['expires_on'] = api_get_local_time();
  618. $expires_date_array = convert_date_to_array(api_get_local_time(), 'expires');
  619. $defaults = array_merge($defaults, $expires_date_array);
  620. $there_is_a_expire_date = false;
  621. $form_folder->addElement(
  622. 'checkbox',
  623. 'enableExpiryDate',
  624. null,
  625. get_lang('EnableExpiryDate'),
  626. 'onclick="javascript: if(this.checked){document.getElementById(\'option1\').style.display = \'block\';}else{document.getElementById(\'option1\').style.display = \'none\';}"'
  627. );
  628. $form_folder->addElement('html', '<div id=\'option1\' style="display:none">');
  629. $form_folder->addGroup(create_group_date_select(), 'expires', get_lang('ExpiresAt'));
  630. $form_folder->addElement('html', '</div>');
  631. }
  632. if ($homework['ends_on'] != '0000-00-00 00:00:00') {
  633. $homework['ends_on'] = api_get_local_time($homework['ends_on']);
  634. $there_is_a_end_date = true;
  635. $defaults['enableEndDate'] = true;
  636. $form_folder->addElement(
  637. 'checkbox',
  638. 'enableEndDate',
  639. null,
  640. get_lang('EnableEndDate'),
  641. 'onclick="javascript: if(this.checked){document.getElementById(\'option2\').style.display = \'block\';}else{document.getElementById(\'option2\').style.display = \'none\';}"'
  642. );
  643. $form_folder->addElement('html', '<div id=\'option2\' style="display:block">');
  644. $form_folder->addGroup(create_group_date_select(), 'ends', get_lang('EndsAt'));
  645. $form_folder->addElement('html', '</div>');
  646. $form_folder->addRule(
  647. array('expires', 'ends'),
  648. get_lang('DateExpiredNotBeLessDeadLine'),
  649. 'comparedate'
  650. );
  651. } else {
  652. $homework['ends_on'] = api_get_local_time();
  653. $expires_date_array = convert_date_to_array(api_get_local_time(), 'ends');
  654. $defaults = array_merge($defaults, $expires_date_array);
  655. $there_is_a_end_date = false;
  656. $form_folder->addElement(
  657. 'checkbox',
  658. 'enableEndDate',
  659. null,
  660. get_lang('EnableEndDate'),
  661. 'onclick="javascript: if(this.checked){document.getElementById(\'option2\').style.display = \'block\';}else{document.getElementById(\'option2\').style.display = \'none\';}"'
  662. );
  663. $form_folder->addElement('html', '<div id=\'option2\' style="display:none">');
  664. $form_folder->addGroup(create_group_date_select(), 'ends', get_lang('EndsAt'));
  665. $form_folder->addElement('html', '</div>');
  666. $form_folder->addRule(
  667. array('expires', 'ends'),
  668. get_lang('DateExpiredNotBeLessDeadLine'),
  669. 'comparedate'
  670. );
  671. }
  672. if ($there_is_a_expire_date && $there_is_a_end_date) {
  673. $form_folder->addRule(
  674. array('expires', 'ends'),
  675. get_lang('DateExpiredNotBeLessDeadLine'),
  676. 'comparedate'
  677. );
  678. }
  679. $form_folder->addElement(
  680. 'checkbox',
  681. 'allow_text_assignment',
  682. null,
  683. get_lang('AllowTextAssignments')
  684. );
  685. $form_folder->addElement('html', '</div>');
  686. $form_folder->addElement(
  687. 'style_submit_button',
  688. 'submit',
  689. get_lang('ModifyDirectory'),
  690. 'class="save"'
  691. );
  692. if ($there_is_a_end_date) {
  693. $end_date_array = convert_date_to_array($homework['ends_on'], 'ends');
  694. $defaults = array_merge($defaults, $end_date_array);
  695. }
  696. if ($there_is_a_expire_date) {
  697. $expires_date_array = convert_date_to_array($homework['expires_on'], 'expires');
  698. $defaults = array_merge($defaults, $expires_date_array);
  699. }
  700. if (!empty($row['qualification'])) {
  701. $defaults = array_merge(
  702. $defaults,
  703. array('qualification[qualification]' => $row['qualification'])
  704. );
  705. }
  706. $defaults['allow_text_assignment'] = $row['allow_text_assignment'];
  707. $form_folder->setDefaults($defaults);
  708. $display_edit_form = true;
  709. if ($form_folder->validate()) {
  710. if ($_POST['enableExpiryDate'] == '1') {
  711. $there_is_a_expire_date = true;
  712. } else {
  713. $there_is_a_expire_date = false;
  714. }
  715. if ($_POST['enableEndDate'] == '1') {
  716. $there_is_a_end_date = true;
  717. } else {
  718. $there_is_a_end_date = false;
  719. }
  720. $values = $form_folder->exportValues();
  721. $work_id = $values['work_id'];
  722. $dir_name = replace_dangerous_char($values['dir_name']);
  723. $dir_name = FileManager::disable_dangerous_file($dir_name);
  724. $edit_check = false;
  725. $work_data = get_work_data_by_id($work_id);
  726. if (!empty($work_data)) {
  727. $edit_check = true;
  728. } else {
  729. $edit_check = true;
  730. }
  731. if ($edit_check) {
  732. $TABLEAGENDA = Database::get_course_table(TABLE_AGENDA);
  733. $expires_query = ' SET expires_on = '."'".($there_is_a_expire_date ? api_get_utc_datetime(
  734. get_date_from_group('expires')
  735. ) : '0000-00-00 00:00:00')."'";
  736. Database::query(
  737. 'UPDATE '.$work_assigment.$expires_query.' WHERE c_id = '.$course_id.' AND id = '."'".$row['has_properties']."'"
  738. );
  739. $sql_add_publication = "UPDATE ".$work_table." SET has_properties = '".$row['has_properties']."', view_properties=1 WHERE c_id = $course_id AND id ='".$row['id']."'";
  740. Database::query($sql_add_publication);
  741. $ends_query = ' SET ends_on = '."'".($there_is_a_end_date ? api_get_utc_datetime(
  742. get_date_from_group('ends')
  743. ) : '0000-00-00 00:00:00')."'";
  744. Database::query(
  745. 'UPDATE '.$work_assigment.$ends_query.' WHERE c_id = '.$course_id.' AND id = '."'".$row['has_properties']."'"
  746. );
  747. $sql_add_publication = "UPDATE ".$work_table." SET has_properties = '".$row['has_properties']."', view_properties=1 WHERE c_id = $course_id AND id ='".$row['id']."'";
  748. Database::query($sql_add_publication);
  749. $qualification_value = isset($_POST['qualification']['qualification']) && !empty($_POST['qualification']['qualification']) ? intval(
  750. $_POST['qualification']['qualification']
  751. ) : 0;
  752. $enable_qualification = !empty($qualification_value) ? 1 : 0;
  753. $sql_add_publication = "UPDATE ".$work_assigment." SET enable_qualification = '".$enable_qualification."' WHERE c_id = $course_id AND publication_id ='".$row['id']."'";
  754. Database::query($sql_add_publication);
  755. $sql = 'UPDATE '.$work_table.' SET
  756. allow_text_assignment = '."'".intval(
  757. $_POST['allow_text_assignment']
  758. )."'".' ,
  759. title = '."'".Database::escape_string($_POST['dir_name'])."'".',
  760. description = '."'".Database::escape_string($_POST['description'])."'".',
  761. qualification = '."'".Database::escape_string(
  762. $_POST['qualification']['qualification']
  763. )."'".',
  764. weight = '."'".Database::escape_string($_POST['weight']['weight'])."'".'
  765. WHERE c_id = '.$course_id.' AND id = '.$row['id'];
  766. Database::query($sql);
  767. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/gradebook_functions.inc.php';
  768. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/gradebookitem.class.php';
  769. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/evaluation.class.php';
  770. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/be/abstractlink.class.php';
  771. $link_info = is_resource_in_course_gradebook(
  772. api_get_course_id(),
  773. LINK_STUDENTPUBLICATION,
  774. $row['id'],
  775. api_get_session_id()
  776. );
  777. $link_id = null;
  778. if (!empty($link_info)) {
  779. $link_id = $link_info['id'];
  780. }
  781. if (isset($_POST['make_calification']) && $_POST['make_calification'] == 1 && !empty($_POST['category_id'])) {
  782. if (empty($link_id)) {
  783. add_resource_to_course_gradebook(
  784. $_POST['category_id'],
  785. api_get_course_id(),
  786. LINK_STUDENTPUBLICATION,
  787. $row['id'],
  788. $_POST['dir_name'],
  789. (float)$_POST['weight']['weight'],
  790. (float)$_POST['qualification']['qualification'],
  791. $_POST['description'],
  792. 1,
  793. api_get_session_id(),
  794. $link_id
  795. );
  796. } else {
  797. update_resource_from_course_gradebook(
  798. $link_id,
  799. api_get_course_id(),
  800. $_POST['weight']['weight']
  801. );
  802. }
  803. } else {
  804. //Delete everything of the gradebook
  805. remove_resource_from_course_gradebook($link_id);
  806. }
  807. update_dir_name($work_data, $dir_name, $values['dir_name']);
  808. $dir = $dir_name;
  809. $display_edit_form = false;
  810. // gets calendar_id from student_publication_assigment
  811. $sql = "SELECT add_to_calendar FROM $work_assigment WHERE c_id = $course_id AND publication_id ='".$row['id']."'";
  812. $res = Database::query($sql);
  813. $calendar_id = Database::fetch_row($res);
  814. $dir_name = sprintf(get_lang('HandingOverOfTaskX'), $dir_name);
  815. $end_date = $row['insert_date'];
  816. if ($_POST['enableExpiryDate'] == '1') {
  817. $end_date = Database::escape_string(
  818. api_get_utc_datetime(get_date_from_group('expires'))
  819. );
  820. }
  821. // update from agenda if it exists
  822. if (!empty($calendar_id[0])) {
  823. $sql = "UPDATE ".$TABLEAGENDA."
  824. SET title='".$values['dir_name']."',
  825. content = '".Database::escape_string($_POST['description'])."',
  826. start_date = '".$end_date."',
  827. end_date = '".$end_date."'
  828. WHERE c_id = $course_id AND id='".$calendar_id[0]."'";
  829. Database::query($sql);
  830. }
  831. Display::display_confirmation_message(get_lang('FolderEdited'));
  832. } else {
  833. Display::display_warning_message(get_lang('FileExists'));
  834. }
  835. }
  836. }
  837. }
  838. $work_data = get_work_data_by_id($work_parent->id);
  839. $action = '';
  840. $row = array();
  841. $class = '';
  842. $cant_files = 0;
  843. $course_id = api_get_course_int_id();
  844. $session_id = api_get_session_id();
  845. if (api_is_allowed_to_edit()) {
  846. $sql_document = "SELECT count(*) FROM $work_table w INNER JOIN $user_table u ON w.user_id = u.user_id WHERE w.c_id = $course_id AND w.parent_id = ".$work_data['id']." AND w.active IN (0, 1)";
  847. } else {
  848. $user_filter = "user_id = ".api_get_user_id()." AND ";
  849. if ($course_info['show_score'] == 0) {
  850. $user_filter = null;
  851. }
  852. $sql_document = "SELECT count(*) FROM $work_table s, $iprop_table p
  853. WHERE s.c_id = $course_id AND
  854. p.c_id = $course_id AND
  855. s.id = p.ref AND
  856. p.tool='work' AND
  857. s.accepted='1' AND
  858. $user_filter
  859. parent_id = ".$work_data['id']." AND
  860. active = 1 AND
  861. parent_id = ".$work_parent->id."";
  862. }
  863. //count documents
  864. $res_document = Database::query($sql_document);
  865. $count_document = Database::fetch_row($res_document);
  866. $cant_files = $count_document[0];
  867. $text_file = get_lang('FilesUpload');
  868. if ($cant_files == 1) {
  869. $text_file = api_strtolower(get_lang('FileUpload'));
  870. }
  871. $icon = Display::return_icon('work.png', get_lang('Assignment'), array(), ICON_SIZE_SMALL);
  872. if (!empty($display_edit_form) && !empty($edit_dir) && $edit_dir == $id2) {
  873. $row[] = $icon;
  874. $row[] = '<span class="invisible" style="display:none">'.$dir.'</span>'.$form_folder->toHtml(
  875. ); // form to edit the directory's name
  876. } else {
  877. $row[] = '<a href="'.api_get_self().'?'.api_get_cidreq(
  878. ).'&origin='.$origin.'&gradebook='.$gradebook.'">'.$icon.'</a>';
  879. $add_to_name = '';
  880. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/gradebook_functions.inc.php';
  881. $link_info = is_resource_in_course_gradebook(api_get_course_id(), 3, $id2, api_get_session_id());
  882. $link_id = $link_info['id'];
  883. $count = 0;
  884. if ($link_info !== false) {
  885. $gradebook_data = get_resource_from_course_gradebook($link_id);
  886. $count = $gradebook_data['weight'];
  887. }
  888. if ($count > 0) {
  889. $add_to_name = Display::label(get_lang('IncludedInEvaluation'), 'info');
  890. } else {
  891. $add_to_name = '';
  892. }
  893. $work_title = !empty($work_data['title']) ? $work_data['title'] : basename($work_data['url']);
  894. //Work name
  895. //if (api_is_allowed_to_edit()) {
  896. if ($cant_files > 0) {
  897. $zip = '<a href="downloadfolder.inc.php?id='.$work_data['id'].'">'.Display::return_icon(
  898. 'save_pack.png',
  899. get_lang('Save'),
  900. array('style' => 'float:right;'),
  901. ICON_SIZE_SMALL
  902. ).'</a>';
  903. }
  904. //}
  905. $url = $zip.'<a href="'.api_get_self().'?'.api_get_cidreq(
  906. ).'&origin='.$origin.'&gradebook='.Security::remove_XSS(
  907. $_GET['gradebook']
  908. ).'&id='.$work_data['id'].'"'.$class.'>'.
  909. $work_title.'</a> '.$add_to_name.'<br />'.$cant_files.' '.$text_file.$dirtext;
  910. $row[] = $url;
  911. }
  912. if ($count_files != 0) {
  913. $row[] = '';
  914. }
  915. if ($direc_date != '' && $direc_date != '0000-00-00 00:00:00') {
  916. $direc_date_local = api_get_local_time($direc_date);
  917. $row[] = date_to_str_ago($direc_date_local).'<br /><span class="dropbox_date">'.api_format_date(
  918. $direc_date_local
  919. ).'</span>';
  920. } else {
  921. $direc_date_local = '0000-00-00 00:00:00';
  922. $row[] = '';
  923. }
  924. if ($origin != 'learnpath') {
  925. if ($is_allowed_to_edit) {
  926. if (api_resource_is_locked_by_gradebook($id2, LINK_STUDENTPUBLICATION)) {
  927. $action .= Display::return_icon('edit_na.png', get_lang('Edit'), array(), ICON_SIZE_SMALL);
  928. $action .= Display::return_icon('delete_na.png', get_lang('Delete'), array(), ICON_SIZE_SMALL);
  929. } else {
  930. $action .= '<a href="'.api_get_self().'?cidReq='.api_get_course_id(
  931. ).'&origin='.$origin.'&gradebook='.$gradebook.'&edit_dir='.$id2.'">'.Display::return_icon(
  932. 'edit.png',
  933. get_lang('Modify'),
  934. array(),
  935. ICON_SIZE_SMALL
  936. ).'</a>';
  937. $action .= ' <a href="'.api_get_self().'?'.api_get_cidreq(
  938. ).'&origin='.$origin.'&gradebook='.$gradebook.'&delete_dir='.$id2.'" onclick="javascript:if(!confirm('."'".addslashes(
  939. api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES)
  940. )."'".')) return false;" title="'.get_lang('DirDelete').'" >'.Display::return_icon(
  941. 'delete.png',
  942. get_lang('DirDelete'),
  943. '',
  944. ICON_SIZE_SMALL
  945. ).'</a>';
  946. }
  947. $row[] = $action;
  948. } else {
  949. $row[] = '';
  950. }
  951. }
  952. $row[] = $direc_date_local;
  953. $row[] = $work_data['title'];
  954. $table_data[] = $row;
  955. }
  956. }
  957. $sorting_options = array();
  958. $sorting_options['column'] = 1;
  959. // Here we change the way how the colums are going to be sorted
  960. // in this case the the column of LastResent ( 4th element in $column_header) we will be order like the column RealDate
  961. // because in the column RealDate we have the days in a correct format "2008-03-12 10:35:48"
  962. $column_order = array();
  963. $i = 0;
  964. foreach ($table_header as $item) {
  965. $column_order[$i] = $i;
  966. $i++;
  967. }
  968. if (empty($my_folder_data)) {
  969. $column_order[1] = 5;
  970. } else {
  971. $column_order[2] = 2;
  972. }
  973. // An array with the setting of the columns -> 1: columns that we will show, 0:columns that will be hide
  974. $column_show = array();
  975. $column_show[] = 1; // type 0
  976. $column_show[] = 1; // title 1
  977. if (!empty($my_folder_data)) {
  978. $column_show[] = 1; // 2
  979. $column_show[] = 1; // 3
  980. if ($qualification_exists) {
  981. $column_show[] = 1; // 4
  982. }
  983. }
  984. $column_show[] = 1; //date
  985. if ($table_has_actions_column) {
  986. $column_show[] = 1; // modify
  987. }
  988. $column_show[] = 0; //real date in correct format
  989. $paging_options = array();
  990. if (isset($_GET['curdirpath'])) {
  991. $my_params = array('curdirpath' => Security::remove_XSS($_GET['curdirpath']));
  992. }
  993. $my_params = array('id' => isset($_GET['id']) ? $_GET['id'] : null);
  994. if (isset($_GET['edit_dir'])) {
  995. $my_params = array('edit_dir' => intval($_GET['edit_dir']));
  996. }
  997. $my_params['origin'] = $origin;
  998. Display::display_sortable_config_table(
  999. 'work',
  1000. $table_header,
  1001. $table_data,
  1002. $sorting_options,
  1003. $paging_options,
  1004. $my_params,
  1005. $column_show,
  1006. $column_order
  1007. );
  1008. }
  1009. /**
  1010. * Returns a list of subdirectories found in the given directory.
  1011. *
  1012. * The list return starts from the given base directory.
  1013. * If you require the subdirs of /var/www/ (or /var/www), you will get 'abc/', 'def/', but not '/var/www/abc/'...
  1014. * @param string Base dir
  1015. * @param integer 0 if we only want dirs from this level, 1 if we want to recurse into subdirs
  1016. * @return strings_array The list of subdirs in 'abc/' form, -1 on error, and 0 if none found
  1017. * @todo Add a session check to see if subdirs_list doesn't exist yet (cached copy)
  1018. */
  1019. function get_subdirs_list($basedir = '', $recurse = 0)
  1020. {
  1021. //echo "Looking for subdirs of $basedir";
  1022. if (empty($basedir) or !is_dir($basedir)) {
  1023. return -1;
  1024. }
  1025. if (substr($basedir, -1, 1) != '/') {
  1026. $basedir = $basedir.'/';
  1027. }
  1028. $dirs_list = array();
  1029. $dh = opendir($basedir);
  1030. while ($entry = readdir($dh)) {
  1031. $entry = replace_dangerous_char($entry);
  1032. $entry = FileManager::disable_dangerous_file($entry);
  1033. if (is_dir($basedir.$entry) && $entry != '..' && $entry != '.') {
  1034. $dirs_list[] = $entry;
  1035. if ($recurse == 1) {
  1036. foreach (get_subdirs_list($basedir.$entry) as $subdir) {
  1037. $dirs_list[] = $entry.'/'.$subdir;
  1038. }
  1039. }
  1040. }
  1041. }
  1042. closedir($dh);
  1043. return $dirs_list;
  1044. }
  1045. /**
  1046. * Builds the form thats enables the user to
  1047. * select a directory to browse/upload in
  1048. * This function has been copied from the document/document.inc.php library
  1049. *
  1050. * @param array $folders
  1051. * @param string $curdirpath
  1052. * @param string $group_dir
  1053. * @return string html form
  1054. */
  1055. // TODO: This function is a candidate for removal, it is not used anywhere.
  1056. function build_work_directory_selector($folders, $curdirpath, $group_dir = '')
  1057. {
  1058. $form = '<form name="selector" action="'.api_get_self().'?'.api_get_cidreq().'" method="POST">'."\n";
  1059. $form .= get_lang(
  1060. 'CurrentDirectory'
  1061. ).' <select name="curdirpath" onchange="javascript: document.selector.submit();">'."\n";
  1062. //group documents cannot be uploaded in the root
  1063. if ($group_dir == '') {
  1064. $form .= '<option value="/">/ ('.get_lang('Root').')</option>';
  1065. if (is_array($folders)) {
  1066. foreach ($folders as $folder) {
  1067. $selected = ($curdirpath == $folder) ? ' selected="selected"' : '';
  1068. $form .= '<option'.$selected.' value="'.$folder.'">'.$folder.'</option>'."\n";
  1069. }
  1070. }
  1071. } else {
  1072. foreach ($folders as $folder) {
  1073. $selected = ($curdirpath == $folder) ? ' selected="selected"' : '';
  1074. $display_folder = substr($folder, strlen($group_dir));
  1075. $display_folder = ($display_folder == '') ? '/ ('.get_lang('Root').')' : $display_folder;
  1076. $form .= '<option'.$selected.' value="'.$folder.'">'.$display_folder.'</option>'."\n";
  1077. }
  1078. }
  1079. $form .= '</select>'."\n";
  1080. $form .= '<noscript><input type="submit" name="change_path" value="'.get_lang('Ok').'" /></noscript>'."\n";
  1081. $form .= '</form>';
  1082. return $form;
  1083. }
  1084. /**
  1085. * Builds the form thats enables the user to
  1086. * move a document from one directory to another
  1087. * This function has been copied from the document/document.inc.php library
  1088. *
  1089. * @param array $folders
  1090. * @param string $curdirpath
  1091. * @param string $move_file
  1092. * @return string html form
  1093. */
  1094. function build_work_move_to_selector($folders, $curdirpath, $move_file, $group_dir = '')
  1095. {
  1096. $course_id = api_get_course_int_id();
  1097. $move_file = intval($move_file);
  1098. $tbl_work = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  1099. $sql = "SELECT title FROM $tbl_work WHERE c_id = $course_id AND id ='".$move_file."'";
  1100. $result = Database::query($sql);
  1101. $title = Database::fetch_row($result);
  1102. global $gradebook;
  1103. //@todo use formvalidator please!
  1104. $form = '<form class="form-horizontal" name="move_to_form" action="'.api_get_self(
  1105. ).'?gradebook='.$gradebook.'&curdirpath='.Security::remove_XSS($curdirpath).'" method="POST">';
  1106. $form .= '<legend>'.get_lang('MoveFile').' - '.Security::remove_XSS($title[0]).'</legend>';
  1107. $form .= '<input type="hidden" name="item_id" value="'.$move_file.'" />';
  1108. $form .= '<input type="hidden" name="action" value="move_to" />';
  1109. $form .= '<div class="control-group">
  1110. <label>
  1111. <span class="form_required">*</span>'.get_lang('Select').'
  1112. </label>
  1113. <div class="controls">';
  1114. $form .= ' <select name="move_to_id">';
  1115. //group documents cannot be uploaded in the root
  1116. if ($group_dir == '') {
  1117. if ($curdirpath != '/') {
  1118. //$form .= '<option value="0">/ ('.get_lang('Root').')</option>';
  1119. }
  1120. if (is_array($folders)) {
  1121. foreach ($folders as $fid => $folder) {
  1122. //you cannot move a file to:
  1123. //1. current directory
  1124. //2. inside the folder you want to move
  1125. //3. inside a subfolder of the folder you want to move
  1126. if (($curdirpath != $folder) && ($folder != $move_file) && (substr(
  1127. $folder,
  1128. 0,
  1129. strlen($move_file) + 1
  1130. ) != $move_file.'/')
  1131. ) {
  1132. $form .= '<option value="'.$fid.'">'.$folder.'</option>';
  1133. }
  1134. }
  1135. }
  1136. } else {
  1137. if ($curdirpath != '/') {
  1138. $form .= '<option value="0">/ ('.get_lang('Root').')</option>';
  1139. }
  1140. foreach ($folders as $fid => $folder) {
  1141. if (($curdirpath != $folder) && ($folder != $move_file) && (substr(
  1142. $folder,
  1143. 0,
  1144. strlen($move_file) + 1
  1145. ) != $move_file.'/')
  1146. ) {
  1147. //cannot copy dir into his own subdir
  1148. $display_folder = substr($folder, strlen($group_dir));
  1149. $display_folder = ($display_folder == '') ? '/ ('.get_lang('Root').')' : $display_folder;
  1150. $form .= '<option value="'.$fid.'">'.$display_folder.'</option>'."\n";
  1151. }
  1152. }
  1153. }
  1154. $form .= '</select>';
  1155. $form .= ' </div>
  1156. </div>';
  1157. $form .= '<div class="control-group">
  1158. <div class="controls">
  1159. <button type="submit" class="save" name="move_file_submit">'.get_lang('MoveFile').'</button>
  1160. </div>
  1161. </div>';
  1162. $form .= '</form>';
  1163. $form .= '<div style="clear: both; margin-bottom: 10px;"></div>';
  1164. return $form;
  1165. }
  1166. /**
  1167. * Checks if the first given directory exists as a subdir of the second given directory
  1168. * This function should now be deprecated by Security::check_abs_path()
  1169. * @param string Subdir
  1170. * @param string Base dir
  1171. * @return integer -1 on error, 0 if not subdir, 1 if subdir
  1172. */
  1173. // TODO: This function is a candidate for removal, it is not used anywhere.
  1174. function is_subdir_of($subdir, $basedir)
  1175. {
  1176. if (empty($subdir) or empty($basedir)) {
  1177. return -1;
  1178. }
  1179. if (substr($basedir, -1, 1) != '/') {
  1180. $basedir = $basedir.'/';
  1181. }
  1182. if (substr($subdir, 0, 1) == '/') {
  1183. $subdir = substr($subdir, 1);
  1184. }
  1185. return is_dir($basedir.$subdir) ? 1 : 0;
  1186. }
  1187. /**
  1188. * creates a new directory trying to find a directory name
  1189. * that doesn't already exist
  1190. * (we could use unique_name() here...)
  1191. *
  1192. * @author Hugues Peeters <hugues.peeters@claroline.net>
  1193. * @author Bert Vanderkimpen
  1194. * @author Yannick Warnier <ywarnier@beeznest.org> Adaptation for work tool
  1195. * @param string Base work dir (.../work)
  1196. * @param string $desiredDirName complete path of the desired name
  1197. * @return string actual directory name if it succeeds, boolean false otherwise
  1198. */
  1199. function create_unexisting_work_directory($base_work_dir, $desired_dir_name)
  1200. {
  1201. $nb = '';
  1202. $base_work_dir = (substr($base_work_dir, -1, 1) == '/' ? $base_work_dir : $base_work_dir.'/');
  1203. while (file_exists($base_work_dir.$desired_dir_name.$nb)) {
  1204. $nb += 1;
  1205. }
  1206. if (@mkdir($base_work_dir.$desired_dir_name.$nb, api_get_permissions_for_new_directories())) {
  1207. return $desired_dir_name.$nb;
  1208. } else {
  1209. return false;
  1210. }
  1211. }
  1212. /**
  1213. * Delete a work-tool directory
  1214. * @param string Base "work" directory for this course as /var/www/chamilo/courses/ABCD/work/
  1215. * @param string The directory name as the bit after "work/", without trailing slash
  1216. * @return integer -1 on error
  1217. */
  1218. function del_dir($id)
  1219. {$_course = api_get_course_info();
  1220. $id = intval($id);
  1221. $work_data = get_work_data_by_id($id);
  1222. if (empty($work_data)) {
  1223. return false;
  1224. }
  1225. $base_work_dir = api_get_path(SYS_COURSE_PATH).$_course['path'].'/work';
  1226. $work_data_url = $base_work_dir.$work_data['url'];
  1227. $check = Security::check_abs_path($work_data_url.'/', $base_work_dir.'/');
  1228. $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  1229. $course_id = api_get_course_int_id();
  1230. if (!empty($work_data['url'])) {
  1231. //Deleting all contents inside the folder
  1232. //@todo replace to parent_id
  1233. $sql = "UPDATE $table SET active = 2 WHERE c_id = $course_id AND filetype = 'folder' AND id = $id";
  1234. $res = Database::query($sql);
  1235. $sql = "UPDATE $table SET active = 2 WHERE c_id = $course_id AND parent_id = $id";
  1236. $res = Database::query($sql);
  1237. if ($check) {
  1238. $new_dir = $work_data_url.'_DELETED_'.$id;
  1239. if (api_get_setting('permanently_remove_deleted_files') == 'true') {
  1240. FileManager::my_delete($work_data_url);
  1241. } else {
  1242. if (file_exists($work_data_url)) {
  1243. rename($work_data_url, $new_dir);
  1244. }
  1245. }
  1246. }
  1247. }
  1248. }
  1249. /**
  1250. * Get the path of a document in the student_publication table (path relative to the course directory)
  1251. * @param integer Element ID
  1252. * @return string Path (or -1 on error)
  1253. */
  1254. function get_work_path($id)
  1255. {
  1256. $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  1257. $course_id = api_get_course_int_id();
  1258. $sql = 'SELECT url FROM '.$table.' WHERE c_id = '.$course_id.' AND id='.intval($id);
  1259. $res = Database::query($sql);
  1260. if (Database::num_rows($res)) {
  1261. $row = Database::fetch_array($res);
  1262. return $row['url'];
  1263. }
  1264. return -1;
  1265. }
  1266. /**
  1267. * Update the url of a work in the student_publication table
  1268. * @param integer ID of the work to update
  1269. * @param string Destination directory where the work has been moved (must end with a '/')
  1270. * @return -1 on error, sql query result on success
  1271. */
  1272. function update_work_url($id, $new_path, $parent_id)
  1273. {
  1274. if (empty($id)) {
  1275. return -1;
  1276. }
  1277. $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  1278. $course_id = api_get_course_int_id();
  1279. $id = intval($id);
  1280. $parent_id = intval($parent_id);
  1281. $sql = "SELECT * FROM $table WHERE c_id = $course_id AND id = $id";
  1282. $res = Database::query($sql);
  1283. if (Database::num_rows($res) != 1) {
  1284. return -1;
  1285. } else {
  1286. $row = Database::fetch_array($res);
  1287. $filename = basename($row['url']);
  1288. $new_url = $new_path.$filename;
  1289. $sql2 = "UPDATE $table SET url = '$new_url', parent_id = '$parent_id' WHERE c_id = $course_id AND id = $id";
  1290. $res2 = Database::query($sql2);
  1291. return $res2;
  1292. }
  1293. }
  1294. /**
  1295. * Update the url of a dir in the student_publication table
  1296. * @param string old path
  1297. * @param string new path
  1298. */
  1299. function update_dir_name($work_data, $new_name, $title)
  1300. {
  1301. $course_id = api_get_course_int_id();
  1302. $work_id = intval($work_data['id']);
  1303. $path = $work_data['url'];
  1304. if ($work_data['title'] == $title) {
  1305. return true;
  1306. }
  1307. $title = Database::escape_string($title);
  1308. if (!empty($new_name)) {
  1309. global $base_work_dir;
  1310. $new_name = Security::remove_XSS($new_name);
  1311. $new_name = replace_dangerous_char($new_name);
  1312. $new_name = FileManager::disable_dangerous_file($new_name);
  1313. FileManager::my_rename($base_work_dir.'/'.$path, $new_name);
  1314. $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  1315. //update all the files in the other directories according with the next query
  1316. $sql = "SELECT id, url FROM $table WHERE c_id = $course_id AND parent_id = $work_id"; // like binary (Case Sensitive)
  1317. $rs = Database::query($sql);
  1318. $work_len = strlen('work/'.$path);
  1319. while ($work = Database :: fetch_array($rs)) {
  1320. $new_dir = $work['url'];
  1321. $name_with_directory = substr($new_dir, $work_len, strlen($new_dir));
  1322. $name = Database::escape_string('work/'.$new_name.'/'.$name_with_directory);
  1323. $sql = 'UPDATE '.$table.' SET url= "'.$name.'" WHERE c_id = '.$course_id.' AND id= '.$work['id'];
  1324. Database::query($sql);
  1325. }
  1326. $sql = "UPDATE $table SET url= '/".$new_name."' , title = '".$title."' WHERE c_id = $course_id AND id = $work_id";
  1327. Database::query($sql);
  1328. }
  1329. }
  1330. /**
  1331. * Return an array with all the folder's ids that are in the given path
  1332. * @param string Path of the directory
  1333. * @return array The list of ids of all the directories in the path
  1334. * @author Julio Montoya Dokeos
  1335. * @version April 2008
  1336. */
  1337. function get_parent_directories($id)
  1338. {
  1339. $course_id = api_get_course_int_id();
  1340. $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  1341. $id = intval($id);
  1342. $sql = "SELECT id FROM $work_table WHERE c_id = $course_id AND parent_id = $id";
  1343. $result = Database::query($sql);
  1344. $list_id = array();
  1345. if (Database::num_rows($result)) {
  1346. while ($row = Database::fetch_array($result)) {
  1347. $list_id[] = $row['id'];
  1348. }
  1349. }
  1350. return $list_id;
  1351. }
  1352. /**
  1353. * Transform an all directory structure (only directories) in an array
  1354. * @param string path of the directory
  1355. * @return array the directory structure into an array
  1356. * @author Julio Montoya Dokeos
  1357. * @version April 2008
  1358. */
  1359. function directory_to_array($directory)
  1360. {
  1361. $array_items = array();
  1362. if ($handle = @opendir($directory)) {
  1363. while (false !== ($file = readdir($handle))) {
  1364. if ($file != '.' && $file != '..') {
  1365. if (is_dir($directory.'/'.$file)) {
  1366. $array_items = array_merge($array_items, directory_to_array($directory.'/'.$file));
  1367. $file = $directory.'/'.$file;
  1368. $array_items[] = preg_replace("/\/\//si", '/', $file);
  1369. }
  1370. }
  1371. }
  1372. closedir($handle);
  1373. }
  1374. return $array_items;
  1375. }
  1376. /**
  1377. * Insert into the DB of the course all the directories
  1378. * @param string path of the /work directory of the course
  1379. * @return -1 on error, sql query result on success
  1380. * @author Julio Montoya Dokeos
  1381. * @version April 2008
  1382. */
  1383. function insert_all_directory_in_course_table($base_work_dir)
  1384. {
  1385. $dir_to_array = directory_to_array($base_work_dir, true);
  1386. $only_dir = array();
  1387. for ($i = 0; $i < count($dir_to_array); $i++) {
  1388. $only_dir[] = substr($dir_to_array[$i], strlen($base_work_dir), strlen($dir_to_array[$i]));
  1389. }
  1390. $course_id = api_get_course_int_id();
  1391. $group_id = api_get_group_id();
  1392. for ($i = 0; $i < count($only_dir); $i++) {
  1393. global $work_table;
  1394. $sql_insert_all = "INSERT INTO ".$work_table." SET
  1395. c_id = '$course_id',
  1396. url = '".$only_dir[$i]."',
  1397. title = '',
  1398. description = '',
  1399. author = '',
  1400. active = '0',
  1401. accepted = '1',
  1402. filetype = 'folder',
  1403. post_group_id = '".$group_id."',
  1404. sent_date = '0000-00-00 00:00:00' ";
  1405. Database::query($sql_insert_all);
  1406. }
  1407. }
  1408. /**
  1409. * This function displays the number of files contained in a directory
  1410. *
  1411. * @param string the path of the directory
  1412. * @param boolean true if we want the total quantity of files include in others child directorys , false only files in the directory
  1413. * @return array the first element is an integer with the number of files in the folder, the second element is the number of directories
  1414. * @author Julio Montoya Dokeos
  1415. * @version April 2008
  1416. */
  1417. function count_dir($path_dir, $recurse)
  1418. {
  1419. $count = 0;
  1420. $count_dir = 0;
  1421. $d = dir($path_dir);
  1422. while ($entry = $d->Read()) {
  1423. if (!(($entry == '..') || ($entry == '.'))) {
  1424. if (is_dir($path_dir.'/'.$entry)) {
  1425. $count_dir++;
  1426. if ($recurse) {
  1427. $count += count_dir($path_dir.'/'.$entry, $recurse);
  1428. }
  1429. } else {
  1430. $count++;
  1431. }
  1432. }
  1433. }
  1434. $return_array = array();
  1435. $return_array[] = $count;
  1436. $return_array[] = $count_dir;
  1437. return $return_array;
  1438. }
  1439. /**
  1440. * returns all the javascript that is required for easily
  1441. * validation when you create a work
  1442. * this goes into the $htmlHeadXtra[] array
  1443. */
  1444. function to_javascript_work()
  1445. {
  1446. $origin = isset($_REQUEST['origin']) && !empty($_REQUEST['origin']) ? api_get_tools_lists($_REQUEST['origin']) : '';
  1447. $js = '<script>
  1448. function plus() {
  1449. if(document.getElementById(\'options\').style.display == \'none\') {
  1450. document.getElementById(\'options\').style.display = \'block\';
  1451. document.getElementById(\'plus\').innerHTML=\'&nbsp;'.Display::return_icon(
  1452. 'div_hide.gif',
  1453. get_lang('Hide', ''),
  1454. array('style' => 'vertical-align:middle')
  1455. ).'&nbsp;'.addslashes(get_lang('AdvancedParameters', '')).'\';
  1456. } else {
  1457. document.getElementById(\'options\').style.display = \'none\';
  1458. document.getElementById(\'plus\').innerHTML=\'&nbsp;'.Display::return_icon(
  1459. 'div_show.gif',
  1460. get_lang('Show', ''),
  1461. array('style' => 'vertical-align:middle')
  1462. ).'&nbsp;'.addslashes(get_lang('AdvancedParameters', '')).'\';
  1463. }
  1464. }
  1465. function updateDocumentTitle(value) {
  1466. var temp = value.indexOf("/");
  1467. //linux path
  1468. if(temp!=-1){
  1469. var temp=value.split("/");
  1470. } else {
  1471. var temp=value.split("\\\");
  1472. }
  1473. document.getElementById("file_upload").value=temp[temp.length-1];
  1474. $("#contains_file_id").attr("value", 1);
  1475. }
  1476. function checkDate(month, day, year) {
  1477. var monthLength =
  1478. new Array(31,28,31,30,31,30,31,31,30,31,30,31);
  1479. if (!day || !month || !year)
  1480. return false;
  1481. // check for bisestile year
  1482. if (year/4 == parseInt(year/4))
  1483. monthLength[1] = 29;
  1484. if (month < 1 || month > 12)
  1485. return false;
  1486. if (day > monthLength[month-1])
  1487. return false;
  1488. return true;
  1489. }
  1490. function mktime() {
  1491. var no, ma = 0, mb = 0, i = 0, d = new Date(), argv = arguments, argc = argv.length;
  1492. d.setHours(0,0,0); d.setDate(1); d.setMonth(1); d.setYear(1972);
  1493. var dateManip = {
  1494. 0: function(tt){ return d.setHours(tt); },
  1495. 1: function(tt){ return d.setMinutes(tt); },
  1496. 2: function(tt){ set = d.setSeconds(tt); mb = d.getDate() - 1; return set; },
  1497. 3: function(tt){ set = d.setMonth(parseInt(tt)-1); ma = d.getFullYear() - 1972; return set; },
  1498. 4: function(tt){ return d.setDate(tt+mb); },
  1499. 5: function(tt){ return d.setYear(tt+ma); }
  1500. };
  1501. for( i = 0; i < argc; i++ ){
  1502. no = parseInt(argv[i]*1);
  1503. if (isNaN(no)) {
  1504. return false;
  1505. } else {
  1506. // arg is number, lets manipulate date object
  1507. if(!dateManip[i](no)){
  1508. // failed
  1509. return false;
  1510. }
  1511. }
  1512. }
  1513. return Math.floor(d.getTime()/1000);
  1514. }
  1515. function validate() {
  1516. var expires_day = document.form1.expires_day.value;
  1517. var expires_month = document.form1.expires_month.value;
  1518. var expires_year = document.form1.expires_year.value;
  1519. var expires_hour = document.form1.expires_hour.value;
  1520. var expires_minute = document.form1.expires_minute.value;
  1521. var expires_date = mktime(expires_hour,expires_minute,0,expires_month,expires_day,expires_year)
  1522. var ends_day = document.form1.ends_day.value;
  1523. var ends_month = document.form1.ends_month.value;
  1524. var ends_year = document.form1.ends_year.value;
  1525. var ends_hour = document.form1.ends_hour.value;
  1526. var ends_minute = document.form1.ends_minute.value;
  1527. var ends_date = mktime(ends_hour,ends_minute,0,ends_month,ends_day,ends_year);
  1528. var new_dir = document.form1.new_dir.value;
  1529. msg_id1 = document.getElementById("msg_error1");
  1530. msg_id2 = document.getElementById("msg_error2");
  1531. msg_id3 = document.getElementById("msg_error3");
  1532. msg_id4 = document.getElementById("msg_error4");
  1533. msg_id5 = document.getElementById("msg_error_weight");
  1534. if(new_dir=="") {
  1535. msg_id1.style.display ="block";
  1536. msg_id1.innerHTML="'.get_lang('FieldRequired', '').'";
  1537. msg_id2.innerHTML="";msg_id3.innerHTML="";msg_id4.innerHTML="";msg_id5.innerHTML="";
  1538. } else if(document.form1.type1.checked && document.form1.type2.checked && expires_date > ends_date) {
  1539. msg_id2.style.display ="block";
  1540. msg_id2.innerHTML="'.get_lang('EndDateCannotBeBeforeTheExpireDate', '').'";
  1541. msg_id1.innerHTML="";msg_id3.innerHTML="";msg_id4.innerHTML="";msg_id5.innerHTML="";
  1542. } else if (!checkDate(expires_month,expires_day,expires_year)) {
  1543. msg_id3.style.display ="block";
  1544. msg_id3.innerHTML="'.get_lang('InvalidDate', '').'";
  1545. msg_id1.innerHTML="";msg_id2.innerHTML="";msg_id4.innerHTML="";msg_id5.innerHTML="";
  1546. } else if (!checkDate(ends_month,ends_day,ends_year)) {
  1547. msg_id4.style.display ="block";
  1548. msg_id4.innerHTML="'.get_lang('InvalidDate', '').'";
  1549. msg_id1.innerHTML="";msg_id2.innerHTML="";msg_id3.innerHTML="";msg_id5.innerHTML="";
  1550. } else {
  1551. if (document.form1.make_calification.checked) {
  1552. var weight = document.form1.weight.value;
  1553. if(weight=="") {
  1554. msg_id5.style.display ="block";
  1555. msg_id5.innerHTML="'.get_lang('WeightNecessary', '').'";
  1556. msg_id1.innerHTML="";msg_id2.innerHTML="";msg_id3.innerHTML="";msg_id4.innerHTML="";
  1557. return false;
  1558. }
  1559. }
  1560. document.form1.action = "work.php?'.api_get_cidreq(
  1561. ).'&origin='.$origin.'&gradebook='.(empty($_GET['gradebook']) ? '' : 'view').'";
  1562. document.form1.submit();
  1563. }
  1564. }
  1565. </script>';
  1566. return $js;
  1567. }
  1568. /**
  1569. * Gets the id of a student publication with a given path
  1570. * @param string $path
  1571. * @return true if is found / false if not found
  1572. */
  1573. // TODO: The name of this function does not fit with the kind of information it returns. Maybe check_work_id() or is_work_id()?
  1574. function get_work_id($path)
  1575. {
  1576. $TBL_STUDENT_PUBLICATION = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
  1577. $TBL_PROP_TABLE = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1578. $course_id = api_get_course_int_id();
  1579. if (api_is_allowed_to_edit()) {
  1580. $sql = "SELECT work.id FROM $TBL_STUDENT_PUBLICATION AS work, $TBL_PROP_TABLE AS props
  1581. 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'";
  1582. } else {
  1583. $sql = "SELECT work.id FROM $TBL_STUDENT_PUBLICATION AS work,$TBL_PROP_TABLE AS props
  1584. 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(
  1585. )."'";
  1586. }
  1587. $result = Database::query($sql);
  1588. $num_rows = Database::num_rows($result);
  1589. if ($result && $num_rows > 0) {
  1590. return true;
  1591. } else {
  1592. return false;
  1593. }
  1594. }
  1595. function get_count_work($work_id)
  1596. {
  1597. $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  1598. $iprop_table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1599. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  1600. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  1601. $session_id = api_get_session_id();
  1602. $condition_session = api_get_session_condition($session_id);
  1603. $course_id = api_get_course_int_id();
  1604. $group_id = api_get_group_id();
  1605. $course_info = api_get_course_info(api_get_course_id());
  1606. $work_id = intval($work_id);
  1607. if (!empty($group_id)) {
  1608. $extra_conditions = " work.post_group_id = '".intval(
  1609. $group_id
  1610. )."' "; // set to select only messages posted by the user's group
  1611. } else {
  1612. $extra_conditions = " work.post_group_id = '0' ";
  1613. }
  1614. if ($is_allowed_to_edit) {
  1615. $extra_conditions .= ' AND work.active IN (0, 1) ';
  1616. } else {
  1617. $extra_conditions .= ' AND work.active = 1 AND accepted = 1';
  1618. if (isset($course_info['show_score']) && $course_info['show_score'] == 1) {
  1619. $extra_conditions .= " AND work.user_id = ".api_get_user_id()." ";
  1620. } else {
  1621. $extra_conditions .= '';
  1622. }
  1623. }
  1624. $extra_conditions .= " AND parent_id = ".$work_id." ";
  1625. $sql = "SELECT count(*) as count ".
  1626. " FROM ".$iprop_table." prop INNER JOIN ".$work_table." work ".
  1627. " ON (prop.ref=work.id AND prop.c_id = $course_id ".
  1628. " AND prop.tool='work' AND work.active = 1 ".
  1629. " AND prop.visibility <> 2 AND work.c_id = $course_id ) ".
  1630. " INNER JOIN $user_table u ON (work.user_id = u.user_id) ".
  1631. " WHERE $extra_conditions $condition_session ";
  1632. $result = Database::query($sql);
  1633. $users_with_work = 0;
  1634. if (Database::num_rows($result)) {
  1635. $result = Database::fetch_array($result);
  1636. $users_with_work = $result['count'];
  1637. }
  1638. return $users_with_work;
  1639. }
  1640. function get_work_user_list($start, $limit, $column, $direction, $work_id, $where_condition)
  1641. {
  1642. $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  1643. $iprop_table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1644. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  1645. $session_id = api_get_session_id();
  1646. $course_id = api_get_course_int_id();
  1647. $group_id = api_get_group_id();
  1648. $course_info = api_get_course_info(api_get_course_id());
  1649. $work_id = intval($work_id);
  1650. $column = empty($column) ? : Database::escape_string($column);
  1651. $start = intval($start);
  1652. $limit = intval($limit);
  1653. if (!in_array($direction, array('asc', 'desc'))) {
  1654. $direction = 'desc';
  1655. }
  1656. $work_data = get_work_data_by_id($work_id);
  1657. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  1658. $condition_session = api_get_session_condition($session_id);
  1659. $locked = api_resource_is_locked_by_gradebook($work_id, LINK_STUDENTPUBLICATION);
  1660. if (!empty($work_data)) {
  1661. if (!empty($group_id)) {
  1662. $extra_conditions = " work.post_group_id = '".intval(
  1663. $group_id
  1664. )."' "; // set to select only messages posted by the user's group
  1665. } else {
  1666. $extra_conditions = " work.post_group_id = '0' ";
  1667. }
  1668. if ($is_allowed_to_edit) {
  1669. $extra_conditions .= ' AND work.active IN (0, 1) ';
  1670. } else {
  1671. $extra_conditions .= ' AND work.active IN (1) ';
  1672. if (isset($course_info['show_score']) && $course_info['show_score'] == 1) {
  1673. $extra_conditions .= " AND u.user_id = ".api_get_user_id()." ";
  1674. } else {
  1675. $extra_conditions .= '';
  1676. }
  1677. }
  1678. $extra_conditions .= " AND parent_id = ".$work_id." ";
  1679. $select = 'DISTINCT
  1680. work.id as id,
  1681. title as title,
  1682. description,
  1683. url,
  1684. sent_date,
  1685. contains_file,
  1686. has_properties,
  1687. view_properties,
  1688. qualification,
  1689. weight,
  1690. allow_text_assignment,
  1691. u.firstname,
  1692. u.lastname,
  1693. u.username,
  1694. parent_id,
  1695. accepted,
  1696. qualificator_id';
  1697. $user_condition = "INNER JOIN $user_table u ON (work.user_id = u.user_id) ";
  1698. $work_condition = "$iprop_table prop INNER JOIN $work_table work ON (prop.ref = work.id AND prop.c_id = $course_id AND work.c_id = $course_id ) ";
  1699. $work_assignment = get_work_assignment_by_id($work_id);
  1700. $sql = "SELECT $select
  1701. FROM $work_condition $user_condition
  1702. WHERE $extra_conditions $where_condition $condition_session ";
  1703. $sql .= " ORDER BY $column $direction ";
  1704. $sql .= " LIMIT $start, $limit";
  1705. $result = Database::query($sql);
  1706. $works = array();
  1707. while ($work = Database::fetch_array($result, 'ASSOC')) {
  1708. $item_id = $work['id'];
  1709. //Get the author ID for that document from the item_property table
  1710. $is_author = false;
  1711. $can_read = false;
  1712. $item_property_data = api_get_item_property_info(
  1713. api_get_course_int_id(),
  1714. 'work',
  1715. $item_id,
  1716. api_get_session_id()
  1717. );
  1718. if (!$is_allowed_to_edit && $item_property_data['insert_user_id'] == api_get_user_id()) {
  1719. $is_author = true;
  1720. }
  1721. if ($course_info['show_score'] == 0) {
  1722. $can_read = true;
  1723. }
  1724. if ($work['accepted'] == '0') {
  1725. $class = 'invisible';
  1726. } else {
  1727. $class = '';
  1728. }
  1729. $qualification_exists = false;
  1730. if (!empty($work_data['qualification']) && intval($work_data['qualification']) > 0) {
  1731. $qualification_exists = true;
  1732. }
  1733. $qualification_string = '';
  1734. if ($qualification_exists) {
  1735. if ($work['qualification'] == '') {
  1736. $qualification_string = Display::label('-');
  1737. } else {
  1738. $qualification_string = Display::label($work['qualification'], 'info');
  1739. }
  1740. }
  1741. $add_string = '';
  1742. $time_expires = api_strtotime($work_assignment['expires_on'], 'UTC');
  1743. if (!empty($work_assignment['expires_on']) && $work_assignment['expires_on'] != '0000-00-00 00:00:00' && $time_expires && ($time_expires < api_strtotime(
  1744. $work['sent_date'],
  1745. 'UTC'
  1746. ))
  1747. ) {
  1748. $add_string = Display::label(get_lang('Expired'), 'important');
  1749. }
  1750. if (($can_read && $work['accepted'] == '1') || ($is_author && $work['accepted'] == '1') || $is_allowed_to_edit) {
  1751. //Firstname, lastname, username
  1752. $work['firstname'] = Display::div($work['firstname'], array('class' => $class));
  1753. $work['lastname'] = Display::div($work['lastname'], array('class' => $class));
  1754. $work['username'] = Display::div($work['username'], array('class' => $class));
  1755. $work['title'] = Display::div($work['title'], array('class' => $class));
  1756. //Type
  1757. $work['type'] = build_document_icon_tag('file', $work['title']);
  1758. //File name
  1759. $link_to_download = null;
  1760. if ($work['contains_file']) {
  1761. $link_to_download = '<a href="download.php?id='.$item_id.'">'.Display::return_icon(
  1762. 'save.png',
  1763. get_lang('Save'),
  1764. array(),
  1765. ICON_SIZE_SMALL
  1766. ).'</a> ';
  1767. } else {
  1768. $link_to_download = '<a href="view.php?id='.$item_id.'">'.Display::return_icon(
  1769. 'default.png',
  1770. get_lang('View'),
  1771. array(),
  1772. ICON_SIZE_SMALL
  1773. ).'</a> ';
  1774. }
  1775. $send_to = Portfolio::share('work', $work['id'], array('style' => 'white-space:nowrap;'));
  1776. $work['qualification'] = $qualification_string;
  1777. //Date
  1778. $work_date = api_convert_and_format_date($work['sent_date']);
  1779. $work['sent_date'] = date_to_str_ago(
  1780. api_get_local_time($work['sent_date'])
  1781. ).' '.$add_string.'<br />'.$work_date;
  1782. //Actions
  1783. $url = api_get_path(WEB_CODE_PATH).'work/work.php?'.api_get_cidreq().'&id='.$work_id;
  1784. $action = '';
  1785. if ($is_allowed_to_edit) {
  1786. if ($locked) {
  1787. if ($qualification_exists) {
  1788. $action .= Display::return_icon(
  1789. 'rate_work_na.png',
  1790. get_lang('CorrectAndRate'),
  1791. array(),
  1792. ICON_SIZE_SMALL
  1793. );
  1794. } else {
  1795. $action .= Display::return_icon(
  1796. 'edit_na.png',
  1797. get_lang('Comment'),
  1798. array(),
  1799. ICON_SIZE_SMALL
  1800. );
  1801. }
  1802. } else {
  1803. if ($qualification_exists) {
  1804. $action .= '<a href="'.$url.'&amp;action=edit&item_id='.$item_id.'&amp;parent_id='.$work['parent_id'].'" title="'.get_lang(
  1805. 'Modify'
  1806. ).'" >'.
  1807. Display::return_icon(
  1808. 'rate_work.png',
  1809. get_lang('CorrectAndRate'),
  1810. array(),
  1811. ICON_SIZE_SMALL
  1812. ).'</a>';
  1813. } else {
  1814. $action .= '<a href="'.$url.'&amp;action=edit&item_id='.$item_id.'&gradebook='.Security::remove_XSS(
  1815. $_GET['gradebook']
  1816. ).'&amp;parent_id='.$work['parent_id'].'" title="'.get_lang('Modify').'" >'.
  1817. Display::return_icon('edit.png', get_lang('Comment'), array(), ICON_SIZE_SMALL).'</a>';
  1818. }
  1819. }
  1820. if ($work['contains_file']) {
  1821. if ($locked) {
  1822. $action .= Display::return_icon('move_na.png', get_lang('Move'), array(), ICON_SIZE_SMALL);
  1823. } else {
  1824. $action .= '<a href="'.$url.'&amp;action=move&item_id='.$item_id.'" title="'.get_lang(
  1825. 'Move'
  1826. ).'">'.Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_SMALL).'</a>';
  1827. }
  1828. }
  1829. if ($work['accepted'] == '1') {
  1830. $action .= '<a href="'.$url.'&amp;action=make_invisible&item_id='.$item_id.'&amp;'.$sort_params.'" title="'.get_lang(
  1831. 'Invisible'
  1832. ).'" >'.Display::return_icon(
  1833. 'visible.png',
  1834. get_lang('Invisible'),
  1835. array(),
  1836. ICON_SIZE_SMALL
  1837. ).'</a>';
  1838. } else {
  1839. $action .= '<a href="'.$url.'&amp;action=make_visible&item_id='.$item_id.'&amp;'.$sort_params.'" title="'.get_lang(
  1840. 'Visible'
  1841. ).'" >'.Display::return_icon(
  1842. 'invisible.png',
  1843. get_lang('Visible'),
  1844. array(),
  1845. ICON_SIZE_SMALL
  1846. ).'</a> ';
  1847. }
  1848. if ($locked) {
  1849. $action .= Display::return_icon('delete_na.png', get_lang('Delete'), '', ICON_SIZE_SMALL);
  1850. } else {
  1851. $action .= '<a href="'.$url.'&amp;action=delete&amp;item_id='.$item_id.'" onclick="javascript:if(!confirm('."'".addslashes(
  1852. api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES)
  1853. )."'".')) return false;" title="'.get_lang('Delete').'" >'.Display::return_icon(
  1854. 'delete.png',
  1855. get_lang('Delete'),
  1856. '',
  1857. ICON_SIZE_SMALL
  1858. ).'</a>';
  1859. }
  1860. } elseif ($is_author && (empty($work['qualificator_id']) || $work['qualificator_id'] == 0)) {
  1861. if (api_is_allowed_to_session_edit(false, true)) {
  1862. $action .= '<a href="'.$url.'&amp;action=edit&item_id='.$item_id.'" title="'.get_lang(
  1863. 'Modify'
  1864. ).'" >'.Display::return_icon('edit.png', get_lang('Modify'), array(), ICON_SIZE_SMALL).'</a>';
  1865. } else {
  1866. $action .= Display::return_icon('edit_na.png', get_lang('Modify'), array(), ICON_SIZE_SMALL);
  1867. }
  1868. if (api_get_course_setting('student_delete_own_publication') == 1) {
  1869. $action .= '<a href="'.$url.'&amp;action=delete&amp;item_id='.$item_id.'" onclick="javascript:if(!confirm('."'".addslashes(
  1870. api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES)
  1871. )."'".')) return false;" title="'.get_lang('Delete').'" >'.Display::return_icon(
  1872. 'delete.png',
  1873. get_lang('Delete'),
  1874. '',
  1875. ICON_SIZE_SMALL
  1876. ).'</a>';
  1877. }
  1878. } else {
  1879. $action .= Display::return_icon('edit_na.png', get_lang('Modify'), array(), ICON_SIZE_SMALL);
  1880. }
  1881. //Status
  1882. if (empty($work['qualificator_id'])) {
  1883. $qualificator_id = Display::label(get_lang('NotRevised'), 'warning');
  1884. } else {
  1885. $qualificator_id = Display::label(get_lang('Revised'), 'success');
  1886. }
  1887. $work['qualificator_id'] = $qualificator_id;
  1888. $work['actions'] = $send_to.$link_to_download.$action;
  1889. $works[] = $work;
  1890. }
  1891. }
  1892. return $works;
  1893. }
  1894. }
  1895. /**
  1896. * Send reminder to users who have not given the task
  1897. *
  1898. * @param int
  1899. * @return array
  1900. * @author cvargas carlos.vargas@beeznest.com cfasanando, christian.fasanado@beeznest.com
  1901. */
  1902. function send_reminder_users_without_publication($task_data)
  1903. {
  1904. $_course = api_get_course_info();
  1905. $sender_name = api_get_person_name(
  1906. api_get_setting('administratorName'),
  1907. api_get_setting('administratorSurname'),
  1908. null,
  1909. PERSON_NAME_EMAIL_ADDRESS
  1910. );
  1911. $task_id = $task_data['id'];
  1912. $task_title = !empty($task_data['title']) ? $task_data['title'] : basename($task_data['url']);
  1913. $subject = '['.api_get_setting('siteName').'] ';
  1914. // The body can be as long as you wish, and any combination of text and variables
  1915. $content = get_lang('ReminderToSubmitPendingTask')."\n".get_lang('CourseName').' : '.$_course['name']."\n";
  1916. $content .= get_lang('WorkName').' : '.$task_title."\n";
  1917. $list_users = get_list_users_without_publication($task_id);
  1918. $mails_sent_to = array();
  1919. foreach ($list_users as $user) {
  1920. $name_user = api_get_person_name($user[1], $user[0], null, PERSON_NAME_EMAIL_ADDRESS);
  1921. $dear_line = get_lang('Dear')." ".api_get_person_name($user[1], $user[0]).", \n\n";
  1922. $body = $dear_line.$content;
  1923. api_mail($name_user, $user[3], $subject, $body, $sender_name, $email_admin);
  1924. $mails_sent_to[] = $name_user;
  1925. }
  1926. return $mails_sent_to;
  1927. }
  1928. /**
  1929. * Sends an email to the students of a course when a homework is created
  1930. *
  1931. * @param int course_id
  1932. *
  1933. * @author Guillaume Viguier <guillaume.viguier@beeznest.com>
  1934. * @author Julio Montoya <gugli100@gmail.com> Adding session support - 2011
  1935. */
  1936. function send_email_on_homework_creation($course_id)
  1937. {
  1938. // Get the students of the course
  1939. $session_id = api_get_session_id();
  1940. if (empty($session_id)) {
  1941. $students = CourseManager::get_student_list_from_course_code($course_id);
  1942. } else {
  1943. $students = CourseManager::get_student_list_from_course_code($course_id, true, $session_id);
  1944. }
  1945. $courseInfo = api_get_course_info_by_id($course_id);
  1946. $emailsubject = '['.api_get_setting('siteName').'] '.get_lang('HomeworkCreated');
  1947. $currentUser = api_get_user_info(api_get_user_id());
  1948. if (!empty($students)) {
  1949. foreach ($students as $student) {
  1950. $user_info = api_get_user_info($student["user_id"]);
  1951. if (!empty($user_info["mail"])) {
  1952. $name_user = api_get_person_name(
  1953. $user_info["firstname"],
  1954. $user_info["lastname"],
  1955. null,
  1956. PERSON_NAME_EMAIL_ADDRESS
  1957. );
  1958. $emailbody = get_lang('Dear')." ".$name_user.",\n\n";
  1959. $emailbody .= get_lang('HomeworkHasBeenCreatedForTheCourse')." ".$courseInfo['title'].". "."\n\n".get_lang(
  1960. 'PleaseCheckHomeworkPage'
  1961. );
  1962. $emailbody .= "\n\n".api_get_person_name($currentUser["firstname"], $currentUser["lastname"]);
  1963. @api_mail(
  1964. $name_user,
  1965. $user_info["mail"],
  1966. $emailsubject,
  1967. $emailbody,
  1968. api_get_person_name(
  1969. $currentUser["firstname"],
  1970. $currentUser["lastname"],
  1971. null,
  1972. PERSON_NAME_EMAIL_ADDRESS
  1973. ),
  1974. $currentUser["mail"]
  1975. );
  1976. }
  1977. }
  1978. }
  1979. }
  1980. function is_work_exist_by_url($url)
  1981. {
  1982. $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  1983. $url = Database::escape_string($url);
  1984. $sql = "SELECT id FROM $work_table WHERE url='$url'";
  1985. $result = Database::query($sql);
  1986. if (Database::num_rows($result) > 0) {
  1987. $row = Database::fetch_row($result);
  1988. if (empty($row)) {
  1989. return false;
  1990. } else {
  1991. return true;
  1992. }
  1993. } else {
  1994. return false;
  1995. }
  1996. }
  1997. function make_select($name, $values, $checked = '')
  1998. {
  1999. $output = '<select name="'.$name.'" id="'.$name.'">';
  2000. foreach ($values as $key => $value) {
  2001. $output .= '<option value="'.$key.'" '.(($checked == $key) ? 'selected="selected"' : '').'>'.$value.'</option>';
  2002. }
  2003. $output .= '</select>';
  2004. return $output;
  2005. }
  2006. function make_checkbox($name, $checked = '', $label = null)
  2007. {
  2008. $check = '<input id ="'.$name.'" type="checkbox" value="1" name="'.$name.'" '.((!empty($checked)) ? 'checked="checked"' : '').'/>';
  2009. if (!empty($label)) {
  2010. $check .= "<label for ='$name'>$label</label>";
  2011. }
  2012. return $check;
  2013. }
  2014. function draw_date_picker($prefix, $default = '')
  2015. {
  2016. if (empty($default)) {
  2017. $default = api_get_local_time();
  2018. }
  2019. $parts = explode(' ', $default);
  2020. list($d_year, $d_month, $d_day) = explode('-', $parts[0]);
  2021. list($d_hour, $d_minute) = explode(':', $parts[1]);
  2022. $minute = range(10, 59);
  2023. array_unshift($minute, '00', '01', '02', '03', '04', '05', '06', '07', '08', '09');
  2024. $date_form = make_select($prefix.'_day', array_combine(range(1, 31), range(1, 31)), $d_day);
  2025. $date_form .= make_select($prefix.'_month', array_combine(range(1, 12), api_get_months_long()), $d_month);
  2026. $date_form .= make_select(
  2027. $prefix.'_year',
  2028. array($d_year => $d_year, $d_year + 1 => $d_year + 1),
  2029. $d_year
  2030. ).'&nbsp;&nbsp;&nbsp;&nbsp;';
  2031. $date_form .= make_select($prefix.'_hour', array_combine(range(0, 23), range(0, 23)), $d_hour).' : ';
  2032. $date_form .= make_select($prefix.'_minute', $minute, $d_minute);
  2033. return $date_form;
  2034. }
  2035. function get_date_from_select($prefix)
  2036. {
  2037. return $_POST[$prefix.'_year'].'-'.two_digits($_POST[$prefix.'_month']).'-'.two_digits(
  2038. $_POST[$prefix.'_day']
  2039. ).' '.two_digits($_POST[$prefix.'_hour']).':'.two_digits($_POST[$prefix.'_minute']).':00';
  2040. }
  2041. /* Check if a user is the author of the item */
  2042. function user_is_author($item_id, $user_id = null)
  2043. {
  2044. if (empty($item_id)) {
  2045. return false;
  2046. }
  2047. if (empty($user_id)) {
  2048. $user_id = api_get_user_id();
  2049. }
  2050. $is_author = false;
  2051. $item_to_edit_data = api_get_item_property_info(api_get_course_int_id(), 'work', $item_id, api_get_session_id());
  2052. $is_allowed_to_edit = api_is_allowed_to_edit();
  2053. if ($is_allowed_to_edit) {
  2054. $is_author = true;
  2055. } else {
  2056. if ($item_to_edit_data['insert_user_id'] == $user_id) {
  2057. $is_author = true;
  2058. }
  2059. }
  2060. if (!$is_author) {
  2061. //api_not_allowed();
  2062. return false;
  2063. }
  2064. return $is_author;
  2065. }
  2066. /**
  2067. * Get list of users who have not given the task
  2068. * @param int
  2069. * @return array
  2070. * @author cvargas
  2071. * @author Julio Montoya <gugli100@gmail.com> Fixing query
  2072. */
  2073. function get_list_users_without_publication($task_id)
  2074. {
  2075. $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  2076. $table_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  2077. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  2078. $session_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  2079. //condition for the session
  2080. $session_id = api_get_session_id();
  2081. $course_id = api_get_course_int_id();
  2082. $task_id = intval($task_id);
  2083. if ($session_id == 0) {
  2084. $sql = "SELECT user_id as id FROM $work_table WHERE c_id = $course_id AND parent_id='$task_id'";
  2085. } else {
  2086. $sql = "SELECT user_id as id FROM $work_table WHERE c_id = $course_id AND parent_id='$task_id' and session_id='".$session_id."'";
  2087. }
  2088. $result = Database::query($sql);
  2089. $users_with_tasks = array();
  2090. while ($row = Database::fetch_array($result)) {
  2091. $users_with_tasks[] = $row['id'];
  2092. }
  2093. if ($session_id == 0) {
  2094. $sql_users = "SELECT cu.user_id, u.lastname, u.firstname, u.email FROM $table_course_user AS cu, $table_user AS u
  2095. WHERE u.status!=1 and cu.c_id='".api_get_course_int_id()."' AND u.user_id=cu.user_id";
  2096. } else {
  2097. $sql_users = "SELECT cu.id_user, u.lastname, u.firstname, u.email FROM $session_course_rel_user AS cu, $table_user AS u
  2098. WHERE u.status!=1 and cu.c_id='".api_get_course_int_id()."' AND u.user_id=cu.id_user and cu.id_session='".$session_id."'";
  2099. }
  2100. $group_id = api_get_group_id();
  2101. $new_group_user_list = array();
  2102. if ($group_id) {
  2103. $group_user_list = GroupManager::get_subscribed_users($group_id);
  2104. if (!empty($group_user_list)) {
  2105. foreach ($group_user_list as $group_user) {
  2106. $new_group_user_list[] = $group_user['user_id'];
  2107. }
  2108. }
  2109. }
  2110. $result_users = Database::query($sql_users);
  2111. $users_without_tasks = array();
  2112. while ($row_users = Database::fetch_row($result_users)) {
  2113. if (in_array($row_users[0], $users_with_tasks)) {
  2114. continue;
  2115. }
  2116. if ($group_id && !in_array($row_users[0], $new_group_user_list)) {
  2117. continue;
  2118. }
  2119. //$user_id = array_shift($row_users);
  2120. $row_users[0] = $row_users[1];
  2121. $row_users[1] = $row_users[2];
  2122. $row_users[2] = Display::encrypted_mailto_link($row_users[3]);
  2123. $users_without_tasks[] = $row_users;
  2124. }
  2125. return $users_without_tasks;
  2126. }
  2127. /**
  2128. * Display list of users who have not given the task
  2129. *
  2130. * @param int task id
  2131. * @return array
  2132. * @author cvargas carlos.vargas@beeznest.com cfasanando, christian.fasanado@beeznest.com
  2133. * @author Julio Montoya <gugli100@gmail.com> Fixes
  2134. */
  2135. function display_list_users_without_publication($task_id)
  2136. {
  2137. global $origin;
  2138. $table_header[] = array(get_lang('LastName'), true);
  2139. $table_header[] = array(get_lang('FirstName'), true);
  2140. $table_header[] = array(get_lang('Email'), true);
  2141. // table_data
  2142. $table_data = get_list_users_without_publication($task_id);
  2143. $sorting_options = array();
  2144. $sorting_options['column'] = 1;
  2145. $paging_options = array();
  2146. $my_params = array();
  2147. if (isset($_GET['curdirpath'])) {
  2148. $my_params['curdirpath'] = Security::remove_XSS($_GET['curdirpath']);
  2149. }
  2150. if (isset($_GET['edit_dir'])) {
  2151. $my_params['edit_dir'] = Security::remove_XSS($_GET['edit_dir']);
  2152. }
  2153. if (isset($_GET['list'])) {
  2154. $my_params['list'] = Security::remove_XSS($_GET['list']);
  2155. }
  2156. $my_params['origin'] = $origin;
  2157. $my_params['id'] = intval($_GET['id']);
  2158. //$column_show
  2159. $column_show[] = 1;
  2160. $column_show[] = 1;
  2161. $column_show[] = 1;
  2162. Display::display_sortable_config_table(
  2163. 'work',
  2164. $table_header,
  2165. $table_data,
  2166. $sorting_options,
  2167. $paging_options,
  2168. $my_params,
  2169. $column_show
  2170. );
  2171. }