work.lib.php 92 KB

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