work.lib.php 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  1. <?php //$Id: work.lib.php 22357 2009-07-24 17:44:17Z juliomontoya $
  2. /* For licensing terms, see /dokeos_license.txt */
  3. /**
  4. * @package dokeos.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. * @version $Id: work.lib.php 22357 2009-07-24 17:44:17Z juliomontoya $
  10. */
  11. /**
  12. * Displays action links (for admins, authorized groups members and authorized students)
  13. * @param string Current dir
  14. * @param integer Whether to show tool options
  15. * @param integer Whether to show upload form option
  16. * @return void
  17. */
  18. require_once '../document/document.inc.php';
  19. require_once '../inc/lib/fileDisplay.lib.php';
  20. function display_action_links($cur_dir_path, $always_show_tool_options, $always_show_upload_form)
  21. {
  22. global $gradebook;
  23. $display_output = "";
  24. $origin = isset($_GET['origin'])?Security::remove_XSS($_GET['origin']):'';
  25. $origin = api_get_tools_lists($origin);
  26. if (strlen($cur_dir_path) > 0 && $cur_dir_path != '/') {
  27. $parent_dir = dirname($cur_dir_path);
  28. $display_output .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&origin='.$origin.'&gradebook='.$gradebook.'&curdirpath='.$parent_dir.'">'.Display::return_icon('folder_up.gif', get_lang('Up')).' '.get_lang('Up').'</a>';
  29. }
  30. echo '<div class="actions">';
  31. if ($_GET['display_tool_options'] == 'true' OR $_GET['display_upload_form'] == 'true' )
  32. {
  33. if ($origin!='learnpath') {
  34. echo '<a href="work.php?gradebook='.$gradebook.'">'.Display::return_icon('back.png',get_lang('BackToWorksList')).' '.get_lang('BackToWorksList').'</a>';
  35. }
  36. }
  37. if (! $always_show_tool_options && api_is_allowed_to_edit() && $origin != 'learnpath') {
  38. // Create dir
  39. $display_output .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;toolgroup='.Security::remove_XSS($_GET['toolgroup']).'&amp;curdirpath='.$cur_dir_path.'&amp;createdir=1&origin='.$origin.'&gradebook='.$gradebook.'">'.Display::return_icon('folder_new.gif', get_lang('CreateAssignment')).' '.get_lang('CreateAssignment').' </a>';
  40. // Options
  41. $display_output .= "<a href=\"".api_get_self()."?".api_get_cidreq()."&curdirpath=".$cur_dir_path."&amp;origin=".$origin."&amp;display_tool_options=true&amp;origin=".$origin."&amp;gradebook=".$gradebook."\">".Display::return_icon('acces_tool.gif', get_lang("EditToolOptions")).' ' . get_lang("EditToolOptions") . "</a>";
  42. }
  43. if (! $always_show_upload_form ) {
  44. $display_output .= "<a href=\"".api_get_self()."?".api_get_cidreq()."&curdirpath=".$cur_dir_path."&amp;display_upload_form=true&amp;origin=".$origin."&amp;gradebook=".$gradebook."\">".Display::return_icon('submit_file.gif', get_lang("UploadADocument"))." ". get_lang("UploadADocument") .'</a>';
  45. }
  46. if (api_is_allowed_to_edit() && $origin != 'learnpath')
  47. {
  48. // delete all files
  49. $display_output .= "<a href=\"".api_get_self()."?".api_get_cidreq()."&amp;curdirpath=".$cur_dir_path."&amp;origin=$origin&amp;gradebook=$gradebook&amp;delete=all\" ".
  50. "onclick=\"javascript:if(!confirm('".addslashes(api_htmlentities(get_lang('ConfirmYourChoice'),ENT_QUOTES,$charset))."')) return false;\">".
  51. Display::return_icon('delete.gif', get_lang('Delete')).' '.get_lang('DeleteAllFiles')."</a>";
  52. // make all files visible or invisible
  53. $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  54. $sql_query = "SHOW COLUMNS FROM ".$work_table." LIKE 'accepted'";
  55. $sql_result = api_sql_query($sql_query,__FILE__,__LINE__);
  56. if ($sql_result) {
  57. $columnStatus = Database::fetch_array($sql_result);
  58. if ($columnStatus['Default'] == 1) {
  59. $display_output .= "<a href=\"".api_get_self()."?".api_get_cidreq()."&curdirpath=".$cur_dir_path."&amp;origin=$origin&amp;gradebook=$gradebook&amp;make_invisible=all\">".
  60. Display::return_icon('visible.gif', get_lang('MakeAllPapersInvisible')).' '.get_lang('MakeAllPapersInvisible').
  61. "</a>\n";
  62. } else {
  63. $display_output .= "<a href=\"".api_get_self()."?".api_get_cidreq()."&amp;curdirpath=".$cur_dir_path."&amp;origin=$origin&amp;gradebook=$gradebook&amp;make_visible=all\">".
  64. Display::return_icon('invisible.gif', get_lang('MakeAllPapersVisible')).' '.get_lang('MakeAllPapersVisible').
  65. "</a>\n";
  66. }
  67. }
  68. }
  69. if ($display_output != "")
  70. {
  71. echo $display_output;
  72. }
  73. echo '</div>';
  74. }
  75. /**
  76. * Displays all options for this tool.
  77. * These are
  78. * - make all files visible / invisible
  79. * - set the default visibility of uploaded files
  80. *
  81. * @param $uploadvisibledisabled
  82. * @param $origin
  83. * @param $base_work_dir Base working directory (up to '/work')
  84. * @param $cur_dir_path Current subdirectory of 'work/'
  85. * @param $cur_dir_path_url Current subdirectory of 'work/', url-encoded
  86. */
  87. function display_tool_options($uploadvisibledisabled, $origin,$base_work_dir,$cur_dir_path,$cur_dir_path_url) {
  88. global $charset, $group_properties,$gradebook;
  89. $is_allowed_to_edit = api_is_allowed_to_edit();
  90. $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  91. if (! $is_allowed_to_edit) {
  92. return;
  93. }
  94. echo '<form method="post" action="'.api_get_self().'?origin='.$origin.'&gradebook='.$gradebook.'&display_tool_options=true">';
  95. echo '<div class="row"><div class="form_header">'.get_lang('EditToolOptions').'</div></div>';
  96. display_default_visibility_form($uploadvisibledisabled);
  97. echo '<div class="row">
  98. <div class="formw">
  99. <button type="submit" class="save" name="changeProperties" value="'.get_lang('Ok').'">'.get_lang('Ok').'</button>
  100. </div>
  101. </div>';
  102. echo '</form>';
  103. }
  104. /**
  105. * Displays the form where course admins can specify wether uploaded documents
  106. * are visible or invisible by default.
  107. *
  108. * @param $uploadvisibledisabled
  109. * @param $origin
  110. */
  111. function display_default_visibility_form($uploadvisibledisabled) {
  112. ?>
  113. <div class="row">
  114. <div class="label">
  115. <?php echo get_lang("_default_upload"); ?>
  116. </div>
  117. <div class="formw">
  118. <input class="checkbox" type="radio" name="uploadvisibledisabled" value="0"
  119. <?php if($uploadvisibledisabled==0) echo "checked"; ?> />
  120. <?php echo get_lang("_new_visible");?><br />
  121. <input class="checkbox" type="radio" name="uploadvisibledisabled" value="1"
  122. <?php if($uploadvisibledisabled==1) echo "checked"; ?> />
  123. <?php echo get_lang("_new_unvisible"); ?>
  124. </div>
  125. </div>
  126. <?php
  127. }
  128. /**
  129. * This function displays the firstname and lastname of the user as a link to the user tool.
  130. *
  131. * @see this is the same function as in the new forum, so this probably has to move to a user library.
  132. *
  133. * @todo move this function to the user library
  134. *
  135. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  136. * @version march 2006
  137. */
  138. function display_user_link($user_id, $name='')
  139. {
  140. global $_otherusers;
  141. if ($user_id<>0) {
  142. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  143. $sql="SELECT * FROM $table_user WHERE user_id='".Database::escape_string($user_id)."'";
  144. $result=api_sql_query($sql,__FILE__,__LINE__);
  145. $row=Database::fetch_array($result);
  146. if ($name=='') {
  147. return "<a href=\"../user/userInfo.php?cidReq=".api_get_course_id()."&gradebook=$gradebook&origin=&uInfo=".$row['user_id']."\">".api_get_person_name($row['firstname'], $row['lastname'])."</a>";
  148. } else {
  149. return "<a href=\"../user/userInfo.php?cidReq=".api_get_course_id()."&gradebook=$gradebook&origin=&uInfo=".$user_id."\">".$name."</a>";
  150. }
  151. } else {
  152. return $name.' ('.get_lang('Anonymous').')';
  153. }
  154. }
  155. /**
  156. * converts 2008-10-06 12:45:00 to timestamp
  157. */
  158. function convert_date_to_number($default)
  159. {
  160. // 2008-10-12 00:00:00 ---to--> 12345672218 (timestamp)
  161. $parts = split(' ',$default);
  162. list($d_year,$d_month,$d_day) = split('-',$parts[0]);
  163. list($d_hour,$d_minute,$d_second) = split(':',$parts[1]);
  164. return mktime($d_hour, $d_minute, $d_second, $d_month, $d_day, $d_year);
  165. }
  166. /**
  167. * converts 1-9 to 01-09
  168. */
  169. function two_digits($number)
  170. {
  171. $number = (int)$number;
  172. return ($number < 10) ? '0'.$number : $number;
  173. }
  174. /**
  175. * converts 2008-10-06 12:45:00 to -> array($data'year'=>2008,$data'month'=>10 etc...)
  176. */
  177. function convert_date_to_array($date,$group)
  178. {
  179. $parts = split(' ',$date);
  180. list($data[$group.'[year]'],$data[$group.'[month]'],$data[$group.'[day]']) = split('-',$parts[0]);
  181. list($data[$group.'[hour]'],$data[$group.'[minute]']) = split(':',$parts[1]);
  182. return $data;
  183. }
  184. /**
  185. * get date from a group of date
  186. */
  187. function get_date_from_group($group) {
  188. 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';
  189. }
  190. /**
  191. * create a group of select from a date
  192. */
  193. function create_group_date_select($prefix='') {
  194. $minute = range(10,59);
  195. $d_year=date('Y');
  196. array_unshift($minute,'00','01','02','03','04','05','06','07','08','09');
  197. $group_name[] = FormValidator :: createElement('select',$prefix.'day','',array_combine(range(1,31),range(1,31)));
  198. $group_name[] = FormValidator :: createElement('select',$prefix.'month','',array_combine(range(1,12),array(get_lang('JanuaryLong'),get_lang('FebruaryLong'),get_lang('MarchLong'),get_lang('AprilLong'),get_lang('MayLong'),get_lang('JuneLong'),get_lang('JulyLong'),get_lang('AugustLong'),get_lang('SeptemberLong'),get_lang('OctoberLong'),get_lang('NovemberLong'),get_lang('DecemberLong'))));
  199. $group_name[] = FormValidator :: createElement('select',$prefix.'year','',array($d_year=>$d_year,$d_year+1=>$d_year+1));
  200. $group_name[] = FormValidator :: createElement('select',$prefix.'hour','',array_combine(range(0,23),range(0,23)));
  201. $group_name[] = FormValidator :: createElement('select',$prefix.'minute','',$minute);
  202. return $group_name;
  203. }
  204. /**
  205. * Display the list of student publications, taking into account the user status
  206. *
  207. * @param $currentCourseRepositoryWeb, the web location of the course folder
  208. * @param $link_target_parameter - should there be a target parameter for the links
  209. * @param $dateFormatLong - date format
  210. * @param $origin - typically empty or 'learnpath'
  211. */
  212. function display_student_publications_list($work_dir,$sub_course_dir,$currentCourseRepositoryWeb, $link_target_parameter, $dateFormatLong, $origin,$add_in_where_query='')
  213. {
  214. global $charset,$timeNoSecFormat,$dateFormatShort,$gradebook;
  215. // Database table names
  216. $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  217. $iprop_table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  218. $work_assigment = Database :: get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT);
  219. $is_allowed_to_edit = api_is_allowed_to_edit();
  220. $user_id = api_get_user_id();
  221. $publications_list = array();
  222. $sort_params = array();
  223. if( isset($_GET['column'])) {
  224. $sort_params[] = 'column='.Security::remove_XSS($_GET['column']);
  225. }
  226. if( isset($_GET['page_nr'])) {
  227. $sort_params[] = 'page_nr='.Security::remove_XSS($_GET['page_nr']);
  228. }
  229. if( isset($_GET['per_page'])) {
  230. $sort_params[] = 'per_page='.Security::remove_XSS($_GET['per_page']);
  231. }
  232. if( isset($_GET['direction'])) {
  233. $sort_params[] = 'direction='.Security::remove_XSS($_GET['direction']);
  234. }
  235. $sort_params = implode('&amp;',$sort_params);
  236. $my_params=$sort_params;
  237. $origin=Security::remove_XSS($origin);
  238. if(substr($sub_course_dir,-1,1)!='/' && !empty($sub_course_dir)) {
  239. $sub_course_dir = $sub_course_dir.'/';
  240. }
  241. if($sub_course_dir == '/') {
  242. $sub_course_dir='';
  243. }
  244. isset($_SESSION['id_session'])?$id_session=$_SESSION['id_session']:$id_session=null;
  245. $session_condition = intval($id_session)!=0 ?"AND session_id IN (0,".intval($id_session).")" : "";
  246. //Get list from database
  247. if($is_allowed_to_edit) {
  248. $sql_get_publications_list = "SELECT * " .
  249. "FROM ".$work_table." " .
  250. "WHERE url LIKE BINARY '$sub_course_dir%' " .
  251. "AND url NOT LIKE BINARY '$sub_course_dir%/%' " .$add_in_where_query.
  252. $session_condition.
  253. "ORDER BY sent_date DESC";
  254. $sql_get_publications_num = "SELECT count(*) " .
  255. "FROM ".$work_table." " .
  256. "WHERE url LIKE BINARY '$sub_course_dir%' " .
  257. "AND url NOT LIKE BINARY '$sub_course_dir%/%' " .$add_in_where_query.
  258. $session_condition.
  259. "ORDER BY id";
  260. } else {
  261. if (!empty($_SESSION['toolgroup'])) {
  262. $group_query = " WHERE post_group_id = '".$_SESSION['toolgroup']."' "; // set to select only messages posted by the user's group
  263. $subdirs_query = "AND url NOT LIKE BINARY '$sub_course_dir%/%' AND url LIKE BINARY '$sub_course_dir%'";
  264. } else {
  265. $group_query = " WHERE post_group_id = '0' ";
  266. $subdirs_query = "AND url NOT LIKE '$sub_course_dir%/%' AND url LIKE '$sub_course_dir%'";
  267. }
  268. $sql_get_publications_list = "SELECT * FROM $work_table $group_query $subdirs_query ".$add_in_where_query." AND session_id IN (0,".intval($id_session).") ORDER BY id";
  269. $sql_get_publications_num = "SELECT count(url) " .
  270. "FROM ".$work_table." " .
  271. "WHERE url LIKE BINARY '$sub_course_dir%' " .
  272. "AND url NOT LIKE BINARY '$sub_course_dir%/%' " .$add_in_where_query.
  273. $session_condition.
  274. "ORDER BY id";
  275. }
  276. $sql_result = api_sql_query($sql_get_publications_list,__FILE__,__LINE__);
  277. $sql_result_num = api_sql_query($sql_get_publications_num,__FILE__,__LINE__);
  278. $row=Database::fetch_array($sql_result_num);
  279. $count_files=$row[0];
  280. $table_header[] = array(get_lang('Type'),true,'style="width:40px"');
  281. $table_header[] = array(get_lang('Title'),true);
  282. if ($count_files!=0) {
  283. $table_header[] = array(get_lang('Authors'),true);
  284. }
  285. $table_header[] = array(get_lang('Date'),true);
  286. if ($origin != 'learnpath') {
  287. if ($is_allowed_to_edit) {
  288. $table_header[] = array(get_lang('Modify'),true);
  289. }
  290. $table_header[] = array('RealDate',false);
  291. }
  292. // An array with the setting of the columns -> 1: columns that we will show, 0:columns that will be hide
  293. $column_show[]=1; // type
  294. $column_show[]=1; // title
  295. if ($count_files!=0) {
  296. $column_show[]=1; // authors
  297. }
  298. $column_show[]=1; //date
  299. if( $is_allowed_to_edit && $origin != 'learnpath') {
  300. $column_show[]=1; //modify
  301. }
  302. $column_show[]=0; //real date in correct format
  303. // Here we change the way how the colums are going to be sort
  304. // in this case the the column of LastResent ( 4th element in $column_header) we will be order like the column RealDate
  305. // because in the column RealDate we have the days in a correct format "2008-03-12 10:35:48"
  306. $column_order[]=1; //type
  307. $column_order[]=2; // title
  308. if ($count_files!=0) {
  309. $column_order[]=3; //authors
  310. }
  311. $column_order[]=6; // date
  312. if( $is_allowed_to_edit) {
  313. $column_order[]=5;
  314. }
  315. $column_order[]=6;
  316. $table_data = array();
  317. $dirs_list = get_subdirs_list($work_dir);
  318. $my_sub_dir = str_replace('work/','',$sub_course_dir);
  319. // List of all folders
  320. foreach($dirs_list as $dir) {
  321. if ($my_sub_dir=='') {
  322. $mydir_temp = '/'.$dir;
  323. }else {
  324. $mydir_temp = '/'.$my_sub_dir.$dir;
  325. }
  326. $session_condition = intval($id_session)!=0 ?"AND work.session_id IN (0,".intval($id_session).")" : "";
  327. $sql_select_directory= "SELECT prop.lastedit_date, id, author, has_properties, view_properties, description, qualification,id FROM ".$iprop_table." prop INNER JOIN ".$work_table." work ON (prop.ref=work.id) WHERE ";
  328. if (!empty($_SESSION['toolgroup'])) {
  329. $sql_select_directory.=" work.post_group_id = '".$_SESSION['toolgroup']."' "; // set to select only messages posted by the user's group
  330. } else {
  331. $sql_select_directory.=" work.post_group_id = '0' ";
  332. }
  333. $sql_select_directory.=" AND work.url LIKE BINARY '".$mydir_temp."' AND work.filetype = 'folder' AND prop.tool='work' $session_condition";
  334. $result=api_sql_query($sql_select_directory,__FILE__,__LINE__);
  335. $row=Database::fetch_array($result);
  336. if(!$row) {
  337. // the folder belongs to another session
  338. continue;
  339. }
  340. $direc_date= $row['lastedit_date']; //directory's date
  341. $author= $row['author']; //directory's author
  342. $view_properties=$row['view_properties'];
  343. $is_assignment = $row['has_properties'];
  344. $id2=$row['id'];
  345. $mydir = $my_sub_dir.$dir;
  346. if ($is_allowed_to_edit) {
  347. isset($_GET['edit_dir'])?$clean_edit_dir=Security :: remove_XSS(Database::escape_string($_GET['edit_dir'])):$clean_edit_dir='';
  348. // form edit directory
  349. if(isset($clean_edit_dir) && $clean_edit_dir==$mydir) {
  350. if(!empty($row['has_properties'])) {
  351. $sql = api_sql_query('SELECT * FROM '.$work_assigment.' WHERE id = '."'".$row['has_properties']."'".' LIMIT 1',__FILE__,__LINE__);
  352. $homework = Database::fetch_array($sql);
  353. }
  354. $form_folder = new FormValidator('edit_dir', 'post', api_get_self().'?curdirpath='.$my_sub_dir.'&origin='.$origin.'&gradebook='.$gradebook.'&edit_dir='.$mydir);
  355. $group_name[] = FormValidator :: createElement('text','dir_name');
  356. $form_folder -> addGroup($group_name,'my_group',get_lang('Title'));
  357. $form_folder -> addGroupRule('my_group',get_lang('ThisFieldIsRequired'),'required');
  358. $defaults = array('my_group[dir_name]'=>html_entity_decode($dir),'description'=>html_entity_decode($row['description']));
  359. $form_folder-> addElement('textarea','description',get_lang('Description'),array('rows'=>5,'cols'=>50));
  360. $qualification_input[] = FormValidator :: createElement('text','qualification');
  361. $form_folder -> addGroup($qualification_input,'qualification',get_lang('QualificationNumberOver'),'size="10"');
  362. $there_is_a_end_date =false;
  363. if($row['view_properties']=='1') {
  364. if($homework['expires_on']!='0000-00-00 00:00:00'){
  365. $there_is_a_expire_date = true;
  366. $form_folder -> addGroup(create_group_date_select(),'expires',get_lang('ExpiresAt'));
  367. }
  368. if($homework['ends_on']!='0000-00-00 00:00:00') {
  369. $there_is_a_end_date = true;
  370. $form_folder -> addGroup(create_group_date_select(),'ends',get_lang('EndsAt'));
  371. }
  372. $form_folder -> addRule(array('expires','ends'), get_lang('DateExpiredNotBeLessDeadLine'), 'comparedate');
  373. } else {
  374. $form_folder -> addElement('html','<div class="row">
  375. <div class="label">&nbsp;</div>
  376. <div class="formw">
  377. <a href="javascript://" onclick=" return plus();" ><span id="plus">&nbsp;<img style="vertical-align:middle;" src="../img/div_show.gif" alt="" />&nbsp;'.get_lang('AdvancedParameters').'</span></a>
  378. </div>
  379. </div> ');
  380. $form_folder -> addElement('html','<div id="options" style="display: none;">');
  381. if(empty($default)) {
  382. $default = date('Y-m-d 12:00:00');
  383. }
  384. $parts = split(' ',$default);
  385. list($d_year,$d_month,$d_day) = split('-',$parts[0]);
  386. list($d_hour,$d_minute) = split(':',$parts[1]);
  387. if($homework['expires_on']='0000-00-00 00:00:00') {
  388. $homework['expires_on']=date("Y-m-d H:i:s");
  389. $there_is_a_expire_date = true;
  390. $form_folder -> addElement('checkbox', 'enableExpiryDate',null,get_lang('EnableExpiryDate'),'onclick="javascript:if(this.checked==true){document.getElementById(\'option1\').style.display = \'block\';}else{document.getElementById(\'option1\').style.display = \'none\';}"');
  391. $form_folder -> addElement('html','<div id=\'option1\' style="display:none">');
  392. $form_folder -> addGroup(create_group_date_select(),'expires',get_lang('ExpiresAt'));
  393. $form_folder -> addElement('html','</div>');
  394. }
  395. if($homework['ends_on']='0000-00-00 00:00:00') {
  396. $homework['ends_on']=date("Y-m-d H:i:s");
  397. $there_is_a_end_date = true;
  398. $form_folder -> addElement('checkbox', 'enableEndDate', null, get_lang('EnableEndDate'),'onclick="javascript:if(this.checked==true){document.getElementById(\'option2\').style.display = \'block\';}else{document.getElementById(\'option2\').style.display = \'none\';}"');
  399. $form_folder -> addElement('html','<div id=\'option2\' style="display:none">');
  400. $form_folder -> addGroup(create_group_date_select(),'ends',get_lang('EndsAt'));
  401. $form_folder -> addElement('html','</div>');
  402. }
  403. $form_folder -> addRule (array('expires','ends'), get_lang('DateExpiredNotBeLessDeadLine'), 'comparedate');
  404. $form_folder -> addElement('html','</div>');
  405. }
  406. $form_folder -> addElement('style_submit_button','submit',get_lang('ModifyDirectory'),'class="save"');
  407. if($there_is_a_end_date == true) {
  408. $defaults = array_merge($defaults,convert_date_to_array($homework['ends_on'],'ends'));
  409. }
  410. if($there_is_a_expire_date == true) {
  411. $defaults = array_merge($defaults,convert_date_to_array($homework['expires_on'],'expires'));
  412. }
  413. if(!empty($row['qualification'])) {
  414. $defaults = array_merge($defaults,array('qualification[qualification]'=>$row['qualification']));
  415. }
  416. $form_folder -> setDefaults($defaults);
  417. $display_edit_form=true;
  418. if($form_folder -> validate()) {
  419. $TABLEAGENDA = Database::get_course_table(TABLE_AGENDA);
  420. if($there_is_a_end_date == true || $there_is_a_expire_date == true) {
  421. if($row['view_properties']=='1') {
  422. $sql_add_publication = "UPDATE ".$work_table." SET has_properties = '".$row['has_properties']. "', view_properties=1 where id ='".$row['id']."'";
  423. api_sql_query($sql_add_publication, __FILE__, __LINE__);
  424. $expires_query= ' SET expires_on = '."'".(($there_is_a_expire_date == true)?get_date_from_group('expires'):'0000-00-00 00:00:00')."'".',';
  425. $ends_query = ' ends_on = '."'".(($there_is_a_end_date == true) ? get_date_from_group('ends') : '0000-00-00 00:00:00')."'";
  426. api_sql_query('UPDATE '.$work_assigment.$expires_query.$ends_query.' WHERE id = '."'".$row['has_properties']."'",__FILE__,__LINE__);
  427. } else if($row['view_properties']=='0') {
  428. if ($_POST['enableExpiryDate']=='1') {
  429. $expires_query= ' SET expires_on = '."'".(($there_is_a_expire_date == true)?get_date_from_group('expires'):'0000-00-00 00:00:00')."'";
  430. //$ends_query = ' ends_on = '."'".(($there_is_a_end_date == true) ? get_date_from_group('ends') : '0000-00-00 00:00:00')."'";
  431. api_sql_query('UPDATE '.$work_assigment.$expires_query.' WHERE id = '."'".$row['has_properties']."'",__FILE__,__LINE__);
  432. $sql_add_publication = "UPDATE ".$work_table." SET has_properties = '".$row['has_properties']. "', view_properties=1 where id ='".$row['id']."'";
  433. api_sql_query($sql_add_publication, __FILE__, __LINE__);
  434. }
  435. if ($_POST['enableEndDate']=='1') {
  436. //$expires_query= ' SET expires_on = '."'".(($there_is_a_expire_date == true)?get_date_from_group('expires'):'0000-00-00 00:00:00')."'".',';
  437. $ends_query = ' SET ends_on = '."'".(($there_is_a_end_date == true) ? get_date_from_group('ends') : '0000-00-00 00:00:00')."'";
  438. api_sql_query('UPDATE '.$work_assigment.$ends_query.' WHERE id = '."'".$row['has_properties']."'",__FILE__,__LINE__);
  439. $sql_add_publication = "UPDATE ".$work_table." SET has_properties = '".$row['has_properties']. "', view_properties=1 where id ='".$row['id']."'";
  440. api_sql_query($sql_add_publication, __FILE__, __LINE__);
  441. }
  442. }
  443. }
  444. //if($_POST['qualification']['qualification']!='')
  445. api_sql_query('UPDATE '.$work_table.' SET description = '."'".Database::escape_string(Security::remove_XSS($_POST['description']))."'".', qualification = '."'".Database::escape_string($_POST['qualification']['qualification'])."'".' WHERE id = '."'".$row['id']."'",__FILE__,__LINE__);
  446. //api_sql_query('UPDATE '.Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK).' SET weight = '."'".Database::escape_string($_POST['qualification']['qualification'])."'".' WHERE course_code = '."'".api_get_course_id()."'".' AND ref_id = '."'".$row['id']."'".'',__FILE__,__LINE__);
  447. Display::display_confirmation_message(get_lang('FolderEdited'));
  448. $values = $form_folder -> exportValues();
  449. $values = $values['my_group'];
  450. $dir_name = disable_dangerous_file($values['dir_name']);
  451. $dir_name = replace_accents($values['dir_name']);
  452. $dir_name = replace_dangerous_char($values['dir_name']);
  453. update_dir_name($mydir,$dir_name);
  454. $mydir = $my_sub_dir.$dir_name;
  455. $dir = $dir_name;
  456. $display_edit_form=false;
  457. // gets calendar_id from student_publication_assigment
  458. $sql = "SELECT add_to_calendar FROM $work_assigment WHERE publication_id ='".$row['id']."'";
  459. $res = api_sql_query($sql,__FILE__,__LINE__);
  460. $calendar_id = Database::fetch_row($res);
  461. // update from agenda if it exists
  462. if (!empty($calendar_id[0])) {
  463. $sql = "UPDATE ".$TABLEAGENDA."
  464. SET title='".$dir_name."',
  465. content = '".$dir_name."',
  466. end_date='".get_date_from_group('ends')."'
  467. WHERE id='".$calendar_id[0]."'";
  468. api_sql_query($sql,__FILE__,__LINE__);
  469. }
  470. }
  471. }
  472. }
  473. $action = '';
  474. $row = array();
  475. $class = '';
  476. $row[] = '<img src="../img/folder_document.gif" border="0" hspace="5" align="middle" alt="'.get_lang('Assignment').'" title="'.get_lang('Assignment').'" />'; //image
  477. //$a_count_directory=count_dir($work_dir.'/'.$dir,false);
  478. $cant_files=0;
  479. $cant_dir = 0;
  480. if(api_is_allowed_to_edit()) {
  481. $sql_document = "SELECT count(*) FROM $work_table WHERE url NOT LIKE '".$sub_course_dir.$dir."/%/%' AND url LIKE '".$sub_course_dir.$dir."/%'";
  482. } else {
  483. // gets admin_course
  484. $table_course_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  485. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  486. $sql = "SELECT course_user.user_id FROM $table_user user, $table_course_user course_user
  487. WHERE course_user.user_id=user.user_id AND course_user.course_code='".api_get_course_id()."' AND course_user.status='1'";
  488. $res = api_sql_query($sql,__FILE__,__LINE__);
  489. $admin_course = '';
  490. while($row_admin = Database::fetch_row($res)) {
  491. $admin_course .='\''.$row_admin[0].'\',';
  492. }
  493. $sql_document = "SELECT count(*) FROM $work_table s, $iprop_table p WHERE s.id = p.ref AND p.tool='work' AND lastedit_user_id IN(".$admin_course.'\''.api_get_user_id().'\''.") AND s.accepted='1' AND url NOT LIKE '".$sub_course_dir.$dir."/%/%' AND url LIKE '".$sub_course_dir.$dir."/%'";
  494. }
  495. //count documents
  496. $res_document = api_sql_query($sql_document,__FILE__,__LINE__);
  497. $count_document = Database::fetch_row($res_document);
  498. $cant_files = $count_document[0];
  499. //count directories
  500. $sql_directory = "SELECT count(*) FROM $work_table s WHERE url NOT LIKE '/".$mydir."/%/%' AND url LIKE '/".$mydir."/%'";
  501. $res_directory = api_sql_query($sql_directory,__FILE__,__LINE__);
  502. $count_directory = Database::fetch_row($res_directory);
  503. $cant_dir = $count_directory[0];
  504. $text_file=get_lang('FilesUpload');
  505. $text_dir=get_lang('Directories');
  506. if ($cant_files==1) {
  507. $text_file=api_strtolower(get_lang('FileUpload'));
  508. }
  509. if ($cant_dir==1) {
  510. $text_dir=get_lang('directory');
  511. }
  512. if ($cant_dir!=0) {
  513. $dirtext=' ('.$cant_dir.' '.$text_dir.')';
  514. } else {
  515. $dirtext='';
  516. }
  517. if (!empty($display_edit_form) && isset($clean_edit_dir) && $clean_edit_dir==$mydir) {
  518. $row[] = '<span class="invisible" style="display:none">'.$dir.'</span>'.$form_folder->toHtml(); // form to edit the directory's name
  519. } else {
  520. $tbl_gradebook_link = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  521. $add_to_name = '';
  522. $sql = "SELECT weight FROM ". $tbl_gradebook_link ." WHERE type='3' AND ref_id= '".$id2."'";
  523. $result=api_sql_query($sql, __FILE__, __LINE__);
  524. $count = Database::num_rows($result);
  525. if($count>0) {
  526. $add_to_name = ' / <span style="color:blue">'.get_lang('Assignment').'</span>';
  527. } else {
  528. $add_to_name = '';
  529. }
  530. $show_as_icon = get_work_id($mydir); //true or false
  531. if ($show_as_icon){
  532. if (is_allowed_to_edit()) {
  533. $zip='<a href="'.api_get_self().'?cidReq='.api_get_course_id().'&gradebook='.$gradebook.'&action=downloadfolder&path=/'.$mydir.'"><img src="../img/zip_save.gif" style="float:right;" alt="'.get_lang('Save').'" title="'.get_lang('Save').'" width="17" height="17"/></a>';
  534. }
  535. $row[] = $zip.'<a href="'.api_get_self().'?'.api_get_cidreq().'&origin='.$origin.'&gradebook='.Security::remove_XSS($_GET['gradebook']).'&curdirpath='.$mydir.'"'.$class.'>'.$dir.'</a>'.$add_to_name.'<br>'.$cant_files.' '.$text_file.$dirtext;
  536. } else {
  537. $row[] = '<a href="'.api_get_self().'?'.api_get_cidreq().'&origin='.$origin.'&gradebook='.$gradebook.'&curdirpath='.$mydir.'"'.$class.'>'.$dir.'</a>'.$add_to_name.'<br>'.$cant_files.' '.$text_file.$dirtext;
  538. }
  539. }
  540. if ($count_files!=0) {
  541. $row[] = "";
  542. }
  543. if ($direc_date!='' && $direc_date!='0000-00-00 00:00:00') {
  544. $my_direc_date = api_ucfirst(format_locale_date($dateFormatShort,strtotime($direc_date))).'&nbsp;&nbsp;&nbsp;&nbsp;';
  545. $my_direc_date .= ucfirst(strftime($timeNoSecFormat,strtotime($direc_date)));
  546. $row[]= date_to_str_ago($direc_date).'<br /><span class="dropbox_date">'.$my_direc_date.'</span>';
  547. } else {
  548. $row[]='';
  549. }
  550. if ($origin != 'learnpath') {
  551. if( $is_allowed_to_edit) {
  552. $action .= '<a href="'.api_get_self().'?cidReq='.api_get_course_id().
  553. '&curdirpath='.$my_sub_dir.'&origin='.$origin.'&gradebook='.$gradebook.'&edit_dir='.$mydir.'"><img src="../img/edit.gif" alt="'.get_lang('Modify').'" title="'.get_lang('Modify').'"></a>';
  554. $action .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&origin='.$origin.'&gradebook='.$gradebook.'&delete_dir='.$mydir.'&delete2='.$id2.'" onclick="javascript:if(!confirm('."'".addslashes(api_htmlentities(get_lang('ConfirmYourChoice'),ENT_QUOTES,$charset))."'".')) return false;" title="'.get_lang('DirDelete').'" ><img src="'.api_get_path(WEB_IMG_PATH).'delete.gif" alt="'.get_lang('DirDelete').'" title="'.get_lang('DirDelete').'"></a>';
  555. $row[] = $action;
  556. } else {
  557. $row[] = "";
  558. }
  559. }
  560. $table_data[] = $row;
  561. }
  562. while( $work = Database::fetch_object($sql_result)) {
  563. //Get the author ID for that document from the item_property table
  564. $is_author = false;
  565. $author_sql = "SELECT * FROM $iprop_table WHERE tool = 'work' AND ref=".$work->id;
  566. $author_qry = api_sql_query($author_sql,__FILE__,__LINE__);
  567. $row2=Database::fetch_array($author_qry);
  568. if(Database::num_rows($author_qry)==1) {
  569. $is_author = true;
  570. }
  571. //display info depending on the permissions
  572. if( $work->accepted == '1' || $is_allowed_to_edit) {
  573. $row = array();
  574. if($work->accepted == '0')
  575. {
  576. $class='class="invisible"';
  577. } else {
  578. $class='';
  579. }
  580. $qualification_string = '';
  581. $add_string = '';
  582. if(defined('IS_ASSIGNMENT')):
  583. if($work->qualification=='') {
  584. $qualification_string = ' / <b style="color:orange">'.get_lang('NotRevised').'<b>';
  585. } else {
  586. $qualification_string = ' / <b style="color:blue">'.get_lang('Qualification').': '.$work->qualification.'<b>';
  587. }
  588. if(defined('ASSIGNMENT_EXPIRES') && (ASSIGNMENT_EXPIRES < convert_date_to_number($work->sent_date))) {
  589. $add_string = ' <b style="color:red">'.get_lang('Expired').'</b>';
  590. }
  591. endif;
  592. $url = implode("/", array_map("rawurlencode", explode("/", $work->url)));
  593. //$full_file_name = 'download.php?file='.$realname;
  594. $row[]= build_document_icon_tag('file',$work->url);
  595. $row[]= '<a href="download.php?file='.$url.'"'.$class.'><img src="../img/filesave.gif" style="float:right;" alt="'.get_lang('Save').'" title="'.get_lang('Save').'" />'.$work->title.'</a><br />'.$work->description;
  596. $row[]= display_user_link($row2['insert_user_id'],$work->author).$qualification_string;// $work->author;
  597. $sent_date = api_ucfirst(format_locale_date($dateFormatShort,strtotime($work->sent_date))).'&nbsp;&nbsp;&nbsp;&nbsp;';
  598. $sent_date .= ucfirst(strftime($timeNoSecFormat,strtotime($work->sent_date)));
  599. $row[]= date_to_str_ago($work->sent_date).$add_string.'<br><span class="dropbox_date">'.$sent_date.'</span>';
  600. if( $is_allowed_to_edit) {
  601. $action = '';
  602. $action .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&curdirpath='.urlencode($my_sub_dir).'&amp;origin='.$origin.'&gradebook='.$gradebook.'&amp;edit='.$work->id.'&gradebook='.Security::remove_XSS($_GET['gradebook']).'&amp;parent_id='.$work->parent_id.'" title="'.get_lang('Modify').'" ><img src="../img/edit.gif" alt="'.get_lang('Modify').'" title="'.get_lang('Modify').'"></a>';
  603. $action .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&curdirpath='.urlencode($my_sub_dir).'&amp;origin='.$origin.'&gradebook='.$gradebook.'&amp;delete='.$work->id.'" onclick="javascript:if(!confirm('."'".addslashes(api_htmlentities(get_lang('ConfirmYourChoice'),ENT_QUOTES,$charset))."'".')) return false;" title="'.get_lang('WorkDelete').'" ><img src="../img/delete.gif" alt="'.get_lang('WorkDelete').'" title="'.get_lang('WorkDelete').'"></a>';
  604. $action .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&curdirpath='.urlencode($my_sub_dir).'&amp;origin='.$origin.'&gradebook='.$gradebook.'&amp;move='.$work->id.'" title="'.get_lang('Move').'"><img src="../img/deplacer_fichier.gif" border="0" title="'.get_lang('Move').'" alt="'.get_lang('Move').'" /></a>';
  605. if($work->accepted == '1') {
  606. $action .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&curdirpath='.urlencode($my_sub_dir).'&amp;origin='.$origin.'&gradebook='.$gradebook.'&amp;make_invisible='.$work->id.'&amp;'.$sort_params.'" title="'.get_lang('Invisible').'" ><img src="../img/visible.gif" alt="'.get_lang('Invisible').'" title="'.get_lang('Invisible').'"></a>';
  607. } else {
  608. $action .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&curdirpath='.urlencode($my_sub_dir).'&amp;origin='.$origin.'&gradebook='.$gradebook.'&amp;make_visible='.$work->id.'&amp;'.$sort_params.'" title="'.get_lang('Visible').'" ><img src="../img/invisible.gif" alt="'.get_lang('Visible').'" title="'.get_lang('Visible').'"></a>';
  609. }
  610. $row[] = $action;
  611. } elseif($is_author) {
  612. $action = '';
  613. $action .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&curdirpath='.urlencode($my_sub_dir).'&gradebook='.Security::remove_XSS($_GET['gradebook']).'&amp;origin='.$origin.'&gradebook='.$gradebook.'&amp;edit='.$work->id.'" title="'.get_lang('Modify').'" ><img src="../img/edit.gif" alt="'.get_lang('Modify').'" title="'.get_lang('Modify').'"></a>';
  614. $action .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&curdirpath='.urlencode($my_sub_dir).'&amp;origin='.$origin.'&gradebook='.$gradebook.'&amp;delete='.$work->id.'" onclick="javascript:if(!confirm('."'".addslashes(api_htmlentities(get_lang('ConfirmYourChoice'),ENT_QUOTES,$charset))."'".')) return false;" title="'.get_lang('WorkDelete').'" ><img src="../img/delete.gif" alt="'.get_lang('WorkDelete').'" title="'.get_lang('WorkDelete').'" ></a>';
  615. $row[] = $action;
  616. } else {
  617. $row[] = " ";
  618. }
  619. $table_data[] = $row;
  620. }
  621. }
  622. $sorting_options=array();
  623. $sorting_options['column']=1;
  624. $paging_options=array();
  625. if (isset($_GET['curdirpath'])) {
  626. $my_params = array ('curdirpath' => Security::remove_XSS($_GET['curdirpath']));
  627. }
  628. if (isset($_GET['edit_dir'])) {
  629. $my_params = array ('edit_dir' => Security::remove_XSS($_GET['edit_dir']));
  630. }
  631. $my_params['origin'] = $origin;
  632. Display::display_sortable_config_table($table_header,$table_data,$sorting_options, $paging_options,$my_params,$column_show,$column_order);
  633. }
  634. /**
  635. * Returns a list of subdirectories found in the given directory.
  636. *
  637. * The list return starts from the given base directory.
  638. * If you require the subdirs of /var/www/ (or /var/www), you will get 'abc/', 'def/', but not '/var/www/abc/'...
  639. * @param string Base dir
  640. * @param integer 0 if we only want dirs from this level, 1 if we want to recurse into subdirs
  641. * @return strings_array The list of subdirs in 'abc/' form, -1 on error, and 0 if none found
  642. * @todo Add a session check to see if subdirs_list doesn't exist yet (cached copy)
  643. */
  644. function get_subdirs_list($basedir='',$recurse=0){
  645. //echo "Looking for subdirs of $basedir";
  646. if(empty($basedir) or !is_dir($basedir)) {
  647. return -1;
  648. }
  649. if(substr($basedir,-1,1)!='/') {
  650. $basedir = $basedir.'/';
  651. }
  652. $dirs_list = array();
  653. $dh = opendir($basedir);
  654. while($entry = readdir($dh)) {
  655. $entry = replace_accents($entry);
  656. $entry = disable_dangerous_file($entry);
  657. $entry = replace_dangerous_char($entry);
  658. if(is_dir($basedir.$entry) && $entry!='..' && $entry!='.') {
  659. $dirs_list[] = $entry;
  660. if($recurse==1) {
  661. foreach(get_subdirs_list($basedir.$entry) as $subdir) {
  662. $dirs_list[] = $entry.'/'.$subdir;
  663. }
  664. }
  665. }
  666. }
  667. closedir($dh);
  668. return $dirs_list;
  669. }
  670. /**
  671. * Builds the form thats enables the user to
  672. * select a directory to browse/upload in
  673. * This function has been copied from the document/document.inc.php library
  674. *
  675. * @param array $folders
  676. * @param string $curdirpath
  677. * @param string $group_dir
  678. * @return string html form
  679. */
  680. function build_work_directory_selector($folders,$curdirpath,$group_dir='')
  681. {
  682. $form = '<form name="selector" action="'.api_get_self().'?'.api_get_cidreq().'" method="POST">'."\n";
  683. $form .= get_lang('CurrentDirectory').' <select name="curdirpath" onchange="javascript:document.selector.submit()">'."\n";
  684. //group documents cannot be uploaded in the root
  685. if ($group_dir=='') {
  686. $form .= '<option value="/">/ ('.get_lang('Root').')</option>';
  687. if (is_array($folders)) {
  688. foreach ($folders as $folder) {
  689. $selected = ($curdirpath==$folder)?' selected="selected"':'';
  690. $form .= '<option'.$selected.' value="'.$folder.'">'.$folder.'</option>'."\n";
  691. }
  692. }
  693. } else {
  694. foreach ($folders as $folder) {
  695. $selected = ($curdirpath==$folder)?' selected="selected"':'';
  696. $display_folder = substr($folder,strlen($group_dir));
  697. $display_folder = ($display_folder == '')?'/ ('.get_lang('Root').')':$display_folder;
  698. $form .= '<option'.$selected.' value="'.$folder.'">'.$display_folder.'</option>'."\n";
  699. }
  700. }
  701. $form .= '</select>'."\n";
  702. $form .= '<noscript><input type="submit" name="change_path" value="'.get_lang('Ok').'" /></noscript>'."\n";
  703. $form .= '</form>';
  704. return $form;
  705. }
  706. /**
  707. * Builds the form thats enables the user to
  708. * move a document from one directory to another
  709. * This function has been copied from the document/document.inc.php library
  710. *
  711. * @param array $folders
  712. * @param string $curdirpath
  713. * @param string $move_file
  714. * @return string html form
  715. */
  716. function build_work_move_to_selector($folders,$curdirpath,$move_file,$group_dir='')
  717. {
  718. //gets file title
  719. $tbl_work = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  720. $sql = "SELECT title FROM $tbl_work WHERE id ='".(int)$move_file."'";
  721. $result = api_sql_query($sql,__FILE__,__LINE__);
  722. $title = Database::fetch_row($result);
  723. global $gradebook;
  724. $form = '<form name="move_to" action="'.api_get_self().'?gradebook='.$gradebook.'" method="POST">'."\n";
  725. $form .= '<div class="row"><div class="form_header">'.get_lang('MoveFile').'</div></div>';
  726. $form .= '<input type="hidden" name="move_file" value="'.$move_file.'" />'."\n";
  727. $form .= '<div class="row">
  728. <div class="label">
  729. <span class="form_required">*</span>'.sprintf(get_lang('MoveXTo'),$title[0]).'
  730. </div>
  731. <div class="formw">';
  732. $form .= ' <select name="move_to">'."\n";
  733. //group documents cannot be uploaded in the root
  734. if($group_dir=='') {
  735. if($curdirpath!='/') {
  736. $form .= '<option value="/">/ ('.get_lang('Root').')</option>';
  737. }
  738. if (is_array($folders)) {
  739. foreach ($folders as $folder) {
  740. //you cannot move a file to:
  741. //1. current directory
  742. //2. inside the folder you want to move
  743. //3. inside a subfolder of the folder you want to move
  744. if(($curdirpath!=$folder) && ($folder!=$move_file) && (substr($folder,0,strlen($move_file)+1) != $move_file.'/')) {
  745. $form .= '<option value="'.$folder.'">'.$folder.'</option>'."\n";
  746. }
  747. }
  748. }
  749. } else {
  750. if ($curdirpath!='/') {
  751. $form .= '<option value="/">/ ('.get_lang('Root').')</option>';
  752. }
  753. foreach ($folders as $folder) {
  754. if (($curdirpath!=$folder) && ($folder!=$move_file) && (substr($folder,0,strlen($move_file)+1) != $move_file.'/')) {
  755. //cannot copy dir into his own subdir
  756. $display_folder = substr($folder,strlen($group_dir));
  757. $display_folder = ($display_folder == '')?'/ ('.get_lang('Root').')':$display_folder;
  758. $form .= '<option value="'.$folder.'">'.$display_folder.'</option>'."\n";
  759. }
  760. }
  761. }
  762. $form .= '</select>'."\n";
  763. $form .= ' </div>
  764. </div>';
  765. $form .= '<div class="row">
  766. <div class="label">
  767. </div>
  768. <div class="formw">
  769. <button type="submit" class="save" name="move_file_submit">'.get_lang('MoveFile').'</button>
  770. </div>
  771. </div>';
  772. $form .= '</form>';
  773. $form .= '<div style="clear: both; margin-bottom: 10px;"></div>';
  774. return $form;
  775. }
  776. /**
  777. * Checks if the first given directory exists as a subdir of the second given directory
  778. * This function should now be deprecated by Security::check_abs_path()
  779. * @param string Subdir
  780. * @param string Base dir
  781. * @return integer -1 on error, 0 if not subdir, 1 if subdir
  782. */
  783. function is_subdir_of($subdir,$basedir)
  784. {
  785. if(empty($subdir) or empty($basedir)) {
  786. return -1;
  787. }
  788. if(substr($basedir,-1,1)!='/') {
  789. $basedir=$basedir.'/';
  790. }
  791. if(substr($subdir,0,1)=='/') {
  792. $subdir = substr($subdir,1);
  793. }
  794. if(is_dir($basedir.$subdir)) {
  795. return 1;
  796. } else {
  797. return 0;
  798. }
  799. }
  800. /**
  801. * creates a new directory trying to find a directory name
  802. * that doesn't already exist
  803. * (we could use unique_name() here...)
  804. *
  805. * @author Hugues Peeters <hugues.peeters@claroline.net>
  806. * @author Bert Vanderkimpen
  807. * @author Yannick Warnier <ywarnier@beeznest.org> Adaptation for work tool
  808. * @param string Base work dir (.../work)
  809. * @param string $desiredDirName complete path of the desired name
  810. * @return string actual directory name if it succeeds,
  811. * boolean false otherwise
  812. */
  813. function create_unexisting_work_directory($base_work_dir,$desired_dir_name)
  814. {
  815. $nb = '';
  816. $base_work_dir = (substr($base_work_dir,-1,1)=='/'?$base_work_dir:$base_work_dir.'/');
  817. while ( file_exists($base_work_dir.$desired_dir_name.$nb) ) {
  818. $nb += 1;
  819. }
  820. //echo "creating ".$base_work_dir.$desired_dir_name.$nb."#...";
  821. $perm = api_get_setting('permissions_for_new_directories');
  822. $perm = octdec(!empty($perm)?$perm:'0770');
  823. if ( mkdir($base_work_dir.$desired_dir_name.$nb, $perm)) {
  824. chmod($base_work_dir.$desired_dir_name.$nb, $perm);
  825. return $desired_dir_name.$nb;
  826. } else {
  827. return false;
  828. }
  829. }
  830. /**
  831. * Delete a work-tool directory
  832. * @param string Base "work" directory for this course as /var/www/dokeos/courses/ABCD/work/
  833. * @param string The directory name as the bit after "work/", without trailing slash
  834. * @return integer -1 on error
  835. */
  836. function del_dir($base_work_dir,$dir,$id) {
  837. if(empty($dir) or $dir=='/') {
  838. return -1;
  839. }
  840. $check = Security::check_abs_path($base_work_dir.$dir,$base_work_dir);
  841. if (!$check || !is_dir($base_work_dir.$dir)) {
  842. return -1;
  843. }
  844. $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  845. $sql = "DELETE FROM $table WHERE url LIKE BINARY 'work/".$dir."/%'";
  846. $res = api_sql_query($sql,__FILE__,__LINE__);
  847. //delete from DB the directories
  848. $sql = "DELETE FROM $table WHERE filetype = 'folder' AND url LIKE BINARY '/".$dir."%'";
  849. $res = api_sql_query($sql,__FILE__,__LINE__);
  850. require_once(api_get_path(LIBRARY_PATH).'/fileManage.lib.php');
  851. //my_delete($base_work_dir.$dir);
  852. $new_dir= $dir.'_DELETED_'.$id;
  853. rename($base_work_dir.$dir, $base_work_dir.$new_dir);
  854. }
  855. /**
  856. * Get the path of a document in the student_publication table (path relative to the course directory)
  857. * @param integer Element ID
  858. * @return string Path (or -1 on error)
  859. */
  860. function get_work_path($id)
  861. {
  862. $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  863. $sql = "SELECT * FROM $table WHERE id=$id";
  864. $res = api_sql_query($sql);
  865. if(Database::num_rows($res)!=1) {
  866. return -1;
  867. } else {
  868. $row = Database::fetch_array($res);
  869. return $row['url'];
  870. }
  871. }
  872. /**
  873. * Update the url of a work in the student_publication table
  874. * @param integer ID of the work to update
  875. * @param string Destination directory where the work has been moved (must end with a '/')
  876. * @return -1 on error, sql query result on success
  877. */
  878. function update_work_url($id,$new_path)
  879. {
  880. if(empty($id)) return -1;
  881. $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  882. $sql = "SELECT * FROM $table WHERE id=$id";
  883. $res = api_sql_query($sql);
  884. if(Database::num_rows($res)!=1) {
  885. return -1;
  886. } else {
  887. $row = Database::fetch_array($res);
  888. $filename = basename($row['url']);
  889. $new_url = $new_path.$filename;
  890. $sql2 = "UPDATE $table SET url = '$new_url' WHERE id=$id";
  891. $res2 = api_sql_query($sql2);
  892. return $res2;
  893. }
  894. }
  895. /**
  896. * Update the url of a dir in the student_publication table
  897. * @param string old path
  898. * @param string new path
  899. */
  900. function update_dir_name($path, $new_name) {
  901. if (!empty($new_name)){
  902. global $base_work_dir;
  903. include_once(api_get_path(LIBRARY_PATH) . "/fileManage.lib.php");
  904. include_once(api_get_path(LIBRARY_PATH) . "/fileUpload.lib.php");
  905. $path_to_dir = dirname($path);
  906. if($path_to_dir=='.') {
  907. $path_to_dir = '';
  908. } else {
  909. $path_to_dir .= '/';
  910. }
  911. $new_name_filter=Security::remove_XSS($new_name);
  912. $new_name=replace_accents($new_name_filter);
  913. $new_name=disable_dangerous_file($new_name_filter);
  914. //$new_name=replace_dangerous_char($new_name);
  915. my_rename($base_work_dir.'/'.$path,$new_name);
  916. $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  917. //update all the files in the other directories according with the next query
  918. $sql = 'SELECT id, url FROM '.$table.' WHERE url LIKE BINARY "work/'.$path.'/%"'; // like binary (Case Sensitive)
  919. $rs = api_sql_query($sql, __FILE__, __LINE__);
  920. $work_len=strlen('work/'.$path);
  921. while($work = Database :: fetch_array($rs)) {
  922. $new_dir=$work['url'];
  923. $name_with_directory=substr($new_dir,$work_len,strlen($new_dir));
  924. $sql = 'UPDATE '.$table.' SET url="work/'.$path_to_dir.$new_name.$name_with_directory.'" WHERE id= '.$work['id'];
  925. api_sql_query($sql, __FILE__, __LINE__);
  926. }
  927. //update all the directory's children according with the next query
  928. $sql = 'SELECT id, url FROM '.$table.' WHERE url LIKE BINARY "/'.$path.'%"';
  929. $rs = api_sql_query($sql, __FILE__, __LINE__);
  930. $work_len=strlen('/'.$path);
  931. while($work = Database :: fetch_array($rs)) {
  932. $new_dir=$work['url'];
  933. $name_with_directory=substr($new_dir,$work_len,strlen($new_dir));
  934. $url=$path_to_dir.$new_name.$name_with_directory;
  935. $sql = 'UPDATE '.$table.' SET url="/'.$url.'" WHERE id= '.$work['id'];
  936. api_sql_query($sql, __FILE__, __LINE__);
  937. }
  938. }
  939. }
  940. /**
  941. * Return an array with all the folder's ids that are in the given path
  942. * @param string Path of the directory
  943. * @return array The list of ids of all the directories in the path
  944. * @author Julio Montoya Dokeos
  945. * @version April 2008
  946. */
  947. function get_parent_directories($my_cur_dir_path) {
  948. $list_id = array();
  949. if (!empty($my_cur_dir_path)) {
  950. $list_parents = explode('/', $my_cur_dir_path);
  951. $dir_acum = '';
  952. global $work_table;
  953. for ($i = 0; $i < count($list_parents) - 1; $i++) {
  954. $item = Database::escape_string($list_parents[$i]);
  955. $where_sentence = "url LIKE BINARY '" . $dir_acum . "/" . $item."'";
  956. $dir_acum .= '/' . $list_parents[$i];
  957. $sql = "SELECT id FROM ". $work_table . " WHERE ". $where_sentence;
  958. $result = api_sql_query($sql, __FILE__, __LINE__);
  959. $row= Database::fetch_array($result);
  960. $list_id[]=$row['id'];
  961. }
  962. }
  963. return $list_id;
  964. }
  965. /**
  966. * Transform an all directory structure (only directories) in an array
  967. * @param string path of the directory
  968. * @return array the directory structure into an array
  969. * @author Julio Montoya Dokeos
  970. * @version April 2008
  971. */
  972. function directory_to_array($directory)
  973. {
  974. $array_items = array();
  975. if ($handle = opendir($directory)) {
  976. while (false !== ($file = readdir($handle))) {
  977. if ($file != "." && $file != "..") {
  978. if (is_dir($directory. "/" . $file)) {
  979. $array_items = array_merge($array_items, directory_to_array($directory. "/" . $file));
  980. $file = $directory . "/" . $file;
  981. $array_items[] = preg_replace("/\/\//si", "/", $file);
  982. }
  983. }
  984. }
  985. closedir($handle);
  986. }
  987. return $array_items;
  988. }
  989. /**
  990. * Insert into the DB of the course all the directories
  991. * @param string path of the /work directory of the course
  992. * @return -1 on error, sql query result on success
  993. * @author Julio Montoya Dokeos
  994. * @version April 2008
  995. */
  996. function insert_all_directory_in_course_table($base_work_dir) {
  997. $dir_to_array =directory_to_array($base_work_dir,true);
  998. $only_dir=array();
  999. for($i=0;$i<count($dir_to_array);$i++) {
  1000. $only_dir[]=substr($dir_to_array[$i],strlen($base_work_dir), strlen($dir_to_array[$i]));
  1001. }
  1002. for($i=0;$i<count($only_dir);$i++) {
  1003. global $work_table;
  1004. $sql_insert_all= "INSERT INTO " . $work_table . " SET url = '" . $only_dir[$i] . "', " .
  1005. "title = '',
  1006. description = '',
  1007. author = '',
  1008. active = '0',
  1009. accepted = '1',
  1010. filetype = 'folder',
  1011. post_group_id = '".intval($_GET['toolgroup'])."',
  1012. sent_date = '0000-00-00 00:00:00' ";
  1013. api_sql_query($sql_insert_all, __FILE__, __LINE__);
  1014. }
  1015. }
  1016. /**
  1017. * This function displays the number of files contained in a directory
  1018. *
  1019. * @param string the path of the directory
  1020. * @param boolean true if we want the total quantity of files include in others child directorys , false only files in the directory
  1021. * @return array the first element is an integer with the number of files in the folder, the second element is the number of directories
  1022. * @author Julio Montoya Dokeos
  1023. * @version April 2008
  1024. */
  1025. function count_dir($path_dir, $recurse)
  1026. {
  1027. $count = 0;
  1028. $count_dir= 0;
  1029. $d = dir($path_dir);
  1030. while ($entry = $d->Read()) {
  1031. if (!(($entry == "..") || ($entry == "."))) {
  1032. if (is_dir($path_dir.'/'.$entry)) {
  1033. $count_dir++;
  1034. if ($recurse) {
  1035. $count += count_dir($path_dir . '/' . $entry, $recurse);
  1036. }
  1037. } else {
  1038. $count++;
  1039. }
  1040. }
  1041. }
  1042. $return_array=array();
  1043. $return_array[]=$count;
  1044. $return_array[]=$count_dir;
  1045. return $return_array;
  1046. }
  1047. /**
  1048. * returns all the javascript that is required for easily
  1049. * validation when you create a work
  1050. * this goes into the $htmlHeadXtra[] array
  1051. */
  1052. function to_javascript_work() {
  1053. return '<script>
  1054. function plus() {
  1055. if(document.getElementById(\'options\').style.display == \'none\') {
  1056. document.getElementById(\'options\').style.display = \'block\';
  1057. document.getElementById(\'plus\').innerHTML=\'&nbsp;'.Display::return_icon('div_hide.gif',get_lang('Hide', ''),array('style'=>'vertical-align:middle')).'&nbsp;'.addslashes(get_lang('AdvancedParameters', '')).'\';
  1058. } else {
  1059. document.getElementById(\'options\').style.display = \'none\';
  1060. document.getElementById(\'plus\').innerHTML=\'&nbsp;'.Display::return_icon('div_show.gif',get_lang('Show', ''),array('style'=>'vertical-align:middle')).'&nbsp;'.addslashes(get_lang('AdvancedParameters', '')).'\';
  1061. }
  1062. }
  1063. function updateDocumentTitle(value){
  1064. var temp = value.indexOf("/");
  1065. //linux path
  1066. if(temp!=-1){
  1067. var temp=value.split("/");
  1068. }
  1069. else{
  1070. var temp=value.split("\\\");
  1071. }
  1072. document.getElementById("file_upload").value=temp[temp.length-1];
  1073. }
  1074. function checkDate(month, day, year)
  1075. {
  1076. var monthLength =
  1077. new Array(31,28,31,30,31,30,31,31,30,31,30,31);
  1078. if (!day || !month || !year)
  1079. return false;
  1080. // check for bisestile year
  1081. if (year/4 == parseInt(year/4))
  1082. monthLength[1] = 29;
  1083. if (month < 1 || month > 12)
  1084. return false;
  1085. if (day > monthLength[month-1])
  1086. return false;
  1087. return true;
  1088. }
  1089. function mktime() {
  1090. var no, ma = 0, mb = 0, i = 0, d = new Date(), argv = arguments, argc = argv.length;
  1091. d.setHours(0,0,0); d.setDate(1); d.setMonth(1); d.setYear(1972);
  1092. var dateManip = {
  1093. 0: function(tt){ return d.setHours(tt); },
  1094. 1: function(tt){ return d.setMinutes(tt); },
  1095. 2: function(tt){ set = d.setSeconds(tt); mb = d.getDate() - 1; return set; },
  1096. 3: function(tt){ set = d.setMonth(parseInt(tt)-1); ma = d.getFullYear() - 1972; return set; },
  1097. 4: function(tt){ return d.setDate(tt+mb); },
  1098. 5: function(tt){ return d.setYear(tt+ma); }
  1099. };
  1100. for( i = 0; i < argc; i++ ){
  1101. no = parseInt(argv[i]*1);
  1102. if (isNaN(no)) {
  1103. return false;
  1104. } else {
  1105. // arg is number, lets manipulate date object
  1106. if(!dateManip[i](no)){
  1107. // failed
  1108. return false;
  1109. }
  1110. }
  1111. }
  1112. return Math.floor(d.getTime()/1000);
  1113. }
  1114. function validate(){
  1115. var expires_day = document.form1.expires_day.value;
  1116. var expires_month = document.form1.expires_month.value;
  1117. var expires_year = document.form1.expires_year.value;
  1118. var expires_hour = document.form1.expires_hour.value;
  1119. var expires_minute = document.form1.expires_minute.value;
  1120. var expires_date = mktime(expires_hour,expires_minute,0,expires_month,expires_day,expires_year)
  1121. var ends_day = document.form1.ends_day.value;
  1122. var ends_month = document.form1.ends_month.value;
  1123. var ends_year = document.form1.ends_year.value;
  1124. var ends_hour = document.form1.ends_hour.value;
  1125. var ends_minute = document.form1.ends_minute.value;
  1126. var ends_date = mktime(ends_hour,ends_minute,0,ends_month,ends_day,ends_year)
  1127. var new_dir = document.form1.new_dir.value;
  1128. msg_id1 = document.getElementById("msg_error1");
  1129. msg_id2 = document.getElementById("msg_error2");
  1130. msg_id3 = document.getElementById("msg_error3");
  1131. msg_id4 = document.getElementById("msg_error4");
  1132. msg_id5 = document.getElementById("msg_error_weight");
  1133. if(new_dir==""){
  1134. msg_id1.style.display ="block";
  1135. msg_id1.innerHTML="'.get_lang('FieldRequired', '').'";
  1136. msg_id2.innerHTML="";msg_id3.innerHTML="";msg_id4.innerHTML="";msg_id5.innerHTML="";
  1137. }
  1138. else if(document.form1.type1.checked==true && document.form1.type2.checked==true && expires_date > ends_date) {
  1139. msg_id2.style.display ="block";
  1140. msg_id2.innerHTML="'.get_lang('EndDateCannotBeBeforeTheExpireDate', '').'";
  1141. msg_id1.innerHTML="";msg_id3.innerHTML="";msg_id4.innerHTML="";msg_id5.innerHTML="";
  1142. }
  1143. else if (checkDate(expires_month,expires_day,expires_year) == false)
  1144. {
  1145. msg_id3.style.display ="block";
  1146. msg_id3.innerHTML="'.get_lang('InvalidDate', '').'";
  1147. msg_id1.innerHTML="";msg_id2.innerHTML="";msg_id4.innerHTML="";msg_id5.innerHTML="";
  1148. }
  1149. else if (checkDate(ends_month,ends_day,ends_year) == false)
  1150. {
  1151. msg_id4.style.display ="block";
  1152. msg_id4.innerHTML="'.get_lang('InvalidDate', '').'";
  1153. msg_id1.innerHTML="";msg_id2.innerHTML="";msg_id3.innerHTML="";msg_id5.innerHTML="";
  1154. }
  1155. else
  1156. {
  1157. if (document.form1.make_calification.checked == true)
  1158. {
  1159. var weight = document.form1.weight.value;
  1160. if(weight==""){
  1161. msg_id5.style.display ="block";
  1162. msg_id5.innerHTML="'.get_lang('WeightNecessary', '').'";
  1163. msg_id1.innerHTML="";msg_id2.innerHTML="";msg_id3.innerHTML="";msg_id4.innerHTML="";
  1164. return false;
  1165. }
  1166. }
  1167. document.form1.action = "work.php?origin='.api_get_tools_lists($_REQUEST['origin']).'&gradebook='.(empty($_GET['gradebook'])?'':'view').'";
  1168. document.form1.submit();
  1169. }
  1170. }
  1171. </script>';
  1172. }
  1173. /**
  1174. * Gets the id of a student publication with a given path
  1175. * @param string $path
  1176. * @return true if is found / false if not found
  1177. */
  1178. function get_work_id($path) {
  1179. $TBL_STUDENT_PUBLICATION = Database :: get_course_table(TABLE_STUDENT_PUBLICATION);
  1180. $TBL_PROP_TABLE = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1181. if (is_allowed_to_edit()) {
  1182. $sql = "SELECT id FROM $TBL_STUDENT_PUBLICATION AS work,$TBL_PROP_TABLE AS props WHERE props.tool='work' AND work.id=props.ref AND work.url LIKE 'work/".$path."%' AND work.filetype='file' AND props.visibility<>'2'";
  1183. } else {
  1184. $sql = "SELECT id FROM $TBL_STUDENT_PUBLICATION AS work,$TBL_PROP_TABLE AS props WHERE props.tool='work' AND work.id=props.ref AND work.url LIKE 'work/".$path."%' AND work.filetype='file' AND props.visibility<>'2' AND props.lastedit_user_id='".api_get_user_id()."'";
  1185. }
  1186. $result = api_sql_query($sql, __FILE__, __LINE__);
  1187. $num_rows = Database::num_rows($result);
  1188. if ($result && $num_rows > 0) {
  1189. return true;
  1190. } else {
  1191. return false;
  1192. }
  1193. }