work.lib.php 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198
  1. <?php
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004-2008 Dokeos SPRL
  6. For a full list of contributors, see "credits.txt".
  7. The full license can be read in "license.txt".
  8. This program is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU General Public License
  10. as published by the Free Software Foundation; either version 2
  11. of the License, or (at your option) any later version.
  12. See the GNU General Public License for more details.
  13. Contact address: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium
  14. Mail: info@dokeos.com
  15. ==============================================================================
  16. */
  17. /**
  18. * @package dokeos.studentpublications
  19. * @author Thomas, Hugues, Christophe - original version
  20. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University - ability for course admins to specify wether uploaded documents are visible or invisible by default.
  21. * @author Roan Embrechts, code refactoring and virtual course support
  22. * @author Frederic Vauthier, directories management
  23. * @version $Id: $
  24. */
  25. /**
  26. * Displays action links (for admins, authorized groups members and authorized students)
  27. * @param string Current dir
  28. * @param integer Whether to show tool options
  29. * @param integer Whether to show upload form option
  30. * @return void
  31. */
  32. require_once('../document/document.inc.php');
  33. require_once('../inc/lib/fileDisplay.lib.php');
  34. function display_action_links($cur_dir_path, $always_show_tool_options, $always_show_upload_form)
  35. {
  36. $display_output = "";
  37. if(strlen($cur_dir_path) > 0 && $cur_dir_path != '/')
  38. {
  39. $parent_dir = dirname($cur_dir_path);
  40. $display_output .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&origin='.Security::remove_XSS($_GET['origin']).'&curdirpath='.$parent_dir.'">'.Display::return_icon('folder_up.gif').' '.get_lang('Up').'</a>&nbsp;&nbsp;';
  41. }
  42. if (! $always_show_upload_form )
  43. {
  44. $display_output .= "&nbsp;&nbsp;<a href=\"".api_get_self()."?".api_get_cidreq()."&curdirpath=".$cur_dir_path."&amp;display_upload_form=true&amp;origin=".Security::remove_XSS($_GET['origin'])."\">".Display::return_icon('submit_file.gif')." ". get_lang("UploadADocument") .'</a>&nbsp;&nbsp;&nbsp;&nbsp;';
  45. }
  46. if (! $always_show_tool_options && api_is_allowed_to_edit(false,true) )
  47. {
  48. // Create dir
  49. $display_output .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;curdirpath='.$cur_dir_path.'&amp;createdir=1&origin='.Security::remove_XSS($_GET['origin']).'"><img src="../img/folder_new.gif" border="0"alt ="'.get_lang('CreateDir').'" /> '.get_lang('CreateDir').' </a>&nbsp;&nbsp;';
  50. if(api_is_allowed_to_edit()) // the coach can't edit options of the tool
  51. // Options
  52. $display_output .= "<a href=\"".api_get_self()."?".api_get_cidreq()."&curdirpath=".$cur_dir_path."&amp;origin=".Security::remove_XSS($_GET['origin'])."&amp;display_tool_options=true&amp;origin=".Security::remove_XSS($_GET['origin'])."\">".Display::return_icon('acces_tool.gif').' ' . get_lang("EditToolOptions") . "</a>&nbsp;&nbsp;";
  53. }
  54. if ($display_output != "")
  55. {
  56. echo $display_output;
  57. }
  58. }
  59. /**
  60. * Displays all options for this tool.
  61. * These are
  62. * - make all files visible / invisible
  63. * - set the default visibility of uploaded files
  64. *
  65. * @param $uploadvisibledisabled
  66. * @param $origin
  67. * @param $base_work_dir Base working directory (up to '/work')
  68. * @param $cur_dir_path Current subdirectory of 'work/'
  69. * @param $cur_dir_path_url Current subdirectory of 'work/', url-encoded
  70. */
  71. function display_tool_options($uploadvisibledisabled, $origin,$base_work_dir,$cur_dir_path,$cur_dir_path_url)
  72. {
  73. global $charset, $group_properties;
  74. $is_allowed_to_edit = api_is_allowed_to_edit(false,true);
  75. $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  76. if (! $is_allowed_to_edit) return;
  77. echo '<form method="post" action="'.api_get_self().'?origin='.$origin.'&display_tool_options=true">';
  78. echo "<br/><table class=\"data_table\">\n",
  79. "<tr><th>&nbsp;</th><th>".get_lang("Modify")."</th></tr><tr class=\"row_even\">\n",
  80. "<td align=\"right\">",
  81. get_lang('AllFiles')." : </td>",
  82. "<td ><a href=\"".api_get_self()."?".api_get_cidreq()."&amp;curdirpath=".$cur_dir_path."&amp;origin=$origin&amp;delete=all&amp;display_tool_options=true\" ",
  83. "onclick=\"javascript:if(!confirm('".addslashes(htmlentities(get_lang('ConfirmYourChoice'),ENT_QUOTES,$charset))."')) return false;\">",
  84. "<img src=\"../img/delete.gif\" border=\"0\" alt=\"".get_lang('Delete')."\" />",
  85. "</a>",
  86. "&nbsp;";
  87. $sql_query = "SHOW COLUMNS FROM ".$work_table." LIKE 'accepted'";
  88. $sql_result = api_sql_query($sql_query,__FILE__,__LINE__);
  89. if ($sql_result)
  90. {
  91. $columnStatus = mysql_fetch_array($sql_result);
  92. if ($columnStatus['Default'] == 1)
  93. {
  94. echo "<a href=\"".api_get_self()."?".api_get_cidreq()."&curdirpath=".$cur_dir_path."&amp;origin=$origin&make_invisible=all&display_tool_options=true\">",
  95. "<img src=\"../img/visible.gif\" border=\"0\" alt=\"".get_lang('Invisible')."\" />",
  96. "</a>\n";
  97. }
  98. else
  99. {
  100. echo "<a href=\"".api_get_self()."?".api_get_cidreq()."&amp;curdirpath=".$cur_dir_path."&amp;origin=$origin&amp;make_visible=all&amp;display_tool_options=true\">",
  101. "<img src=\"../img/invisible.gif\" border=\"0\" alt=\"".get_lang('Visible')."\">",
  102. "</a>\n";
  103. }
  104. }
  105. echo "</td></tr>";
  106. display_default_visibility_form($uploadvisibledisabled);
  107. echo '</table>';
  108. echo '<div>'.get_lang("ValidateChanges").' : <input type="submit" name="changeProperties" value="'.get_lang("Ok").'" /></div></form>';
  109. /*
  110. echo "<br/><table cellpadding=\"5\" cellspacing=\"2\" border=\"0\">\n";
  111. //==============================================================================
  112. // Display directories list
  113. //==============================================================================
  114. //$folders = DocumentManager::get_all_document_folders($_course,$to_group_id,$is_allowed_to_edit || $group_member_with_upload_rights);
  115. if($cur_dir_path=='/'){$my_cur_dir_path='';}else{$my_cur_dir_path=$cur_dir_path;}
  116. $folders = get_subdirs_list($base_work_dir,1);
  117. echo '<div id="folderselector">';
  118. echo(build_work_directory_selector($folders,$cur_dir_path,''));
  119. echo '</div>';
  120. echo '</td></tr><tr><td>';
  121. if ($cur_dir_path!= '/' && $cur_dir_path!=$group_properties['directory'])
  122. {
  123. echo '<a href="'.api_get_self().'?'.api_get_cidreq().'&curdirpath='.urlencode((dirname($cur_dir_path)=='\\')?'/':dirname($cur_dir_path)).'">'.
  124. '<img src="../img/parent.gif" border="0" align="absbottom" hspace="5" alt="" />'.
  125. get_lang("Up").'</a>&nbsp;'."\n";
  126. }
  127. echo '<!-- create directory -->' .
  128. '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;curdirpath='.$cur_dir_path.'&amp;createdir=1"><img src="../img/folder_new.gif" border="0"alt ="'.get_lang('CreateDir').'" /></a>'.
  129. '<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;curdirpath='.$cur_dir_path.'&amp;createdir=1">'.get_lang('CreateDir').'</a>&nbsp;'."\n";
  130. echo "</td></tr></table>";
  131. */
  132. }
  133. /**
  134. * Displays the form where course admins can specify wether uploaded documents
  135. * are visible or invisible by default.
  136. *
  137. * @param $uploadvisibledisabled
  138. * @param $origin
  139. */
  140. function display_default_visibility_form($uploadvisibledisabled)
  141. {
  142. ?>
  143. <tr class="row_odd"><td align="right">
  144. <strong><?php echo get_lang("_default_upload"); ?></strong></td>
  145. <td><input class="checkbox" type="radio" name="uploadvisibledisabled" value="0"
  146. <?php if($uploadvisibledisabled==0) echo "checked"; ?> />
  147. <?php echo get_lang("_new_visible");?><br />
  148. <input class="checkbox" type="radio" name="uploadvisibledisabled" value="1"
  149. <?php if($uploadvisibledisabled==1) echo "checked"; ?> />
  150. <?php echo get_lang("_new_unvisible"); ?><br />
  151. </td></tr>
  152. <?php
  153. }
  154. /**
  155. * This function displays the firstname and lastname of the user as a link to the user tool.
  156. *
  157. * @see this is the same function as in the new forum, so this probably has to move to a user library.
  158. *
  159. * @todo move this function to the user library
  160. *
  161. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  162. * @version march 2006
  163. */
  164. function display_user_link($user_id, $name='')
  165. {
  166. global $_otherusers;
  167. if ($user_id<>0)
  168. {
  169. if ($name=='')
  170. {
  171. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  172. $sql="SELECT * FROM $table_user WHERE user_id='".Database::escape_string($user_id)."'";
  173. $result=api_sql_query($sql,__FILE__,__LINE__);
  174. $row=mysql_fetch_array($result);
  175. return "<a href=\"../user/userInfo.php?uInfo=".$row['user_id']."\">".$row['firstname']." ".$row['lastname']."</a>";
  176. }
  177. else
  178. {
  179. return "<a href=\"../user/userInfo.php?uInfo=".$user_id."\">".$name."</a>";
  180. }
  181. }
  182. else
  183. {
  184. return $name.' ('.get_lang('Anonymous').')';
  185. }
  186. }
  187. function convert_date_to_number($default){
  188. // 2008-10-12 00:00:00 ---to--> 12345672218 (timestamp)
  189. $parts = split(' ',$default);
  190. list($d_year,$d_month,$d_day) = split('-',$parts[0]);
  191. list($d_hour,$d_minute,$d_second) = split(':',$parts[1]);
  192. return mktime($d_hour, $d_minute, $d_second, $d_month, $d_day, $d_year);
  193. }
  194. /*
  195. * converts 1-9 to 01-09
  196. */
  197. function two_digits($number){
  198. $number = (int)$number;
  199. return ($number < 10) ? '0'.$number : $number;
  200. }
  201. /*
  202. * converts 2008-10-06 12:45:00 to -> array($data'year'=>2008,$data'month'=>10 etc...)
  203. */
  204. function convert_date_to_array($date,$group){
  205. $parts = split(' ',$date);
  206. list($data[$group.'[year]'],$data[$group.'[month]'],$data[$group.'[day]']) = split('-',$parts[0]);
  207. list($data[$group.'[hour]'],$data[$group.'[minute]']) = split(':',$parts[1]);
  208. return $data;
  209. }
  210. function get_date_from_group($group){
  211. 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';
  212. }
  213. function create_group_date_select($prefix=''){
  214. $minute = range(10,59);
  215. $d_year=date('Y');
  216. array_unshift($minute,'00','01','02','03','04','05','06','07','08','09');
  217. $group_name[] = FormValidator :: createElement('select',$prefix.'year','',array($d_year=>$d_year,$d_year+1=>$d_year+1));
  218. $group_name[] = FormValidator :: createElement('select',$prefix.'month','',array_combine(range(1,12),array('Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre')));
  219. $group_name[] = FormValidator :: createElement('select',$prefix.'day','',array_combine(range(1,31),range(1,31)));
  220. $group_name[] = FormValidator :: createElement('select',$prefix.'hour','',array_combine(range(1,24),range(1,24)));
  221. $group_name[] = FormValidator :: createElement('select',$prefix.'minute','',$minute);
  222. return $group_name;
  223. }
  224. /**
  225. * Display the list of student publications, taking into account the user status
  226. *
  227. * @param $currentCourseRepositoryWeb, the web location of the course folder
  228. * @param $link_target_parameter - should there be a target parameter for the links
  229. * @param $dateFormatLong - date format
  230. * @param $origin - typically empty or 'learnpath'
  231. */
  232. function display_student_publications_list($work_dir,$sub_course_dir,$currentCourseRepositoryWeb, $link_target_parameter, $dateFormatLong, $origin,$add_in_where_query='')
  233. {
  234. global $charset;
  235. // Database table names
  236. $work_table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  237. $iprop_table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  238. $is_allowed_to_edit = api_is_allowed_to_edit(false,true);
  239. $user_id = api_get_user_id();
  240. $publications_list = array();
  241. $sort_params = array();
  242. if( isset($_GET['column']))
  243. {
  244. $sort_params[] = 'column='.Security::remove_XSS($_GET['column']);
  245. }
  246. if( isset($_GET['page_nr']))
  247. {
  248. $sort_params[] = 'page_nr='.Security::remove_XSS($_GET['page_nr']);
  249. }
  250. if( isset($_GET['per_page']))
  251. {
  252. $sort_params[] = 'per_page='.Security::remove_XSS($_GET['per_page']);
  253. }
  254. if( isset($_GET['direction']))
  255. {
  256. $sort_params[] = 'direction='.Security::remove_XSS($_GET['direction']);
  257. }
  258. $sort_params = implode('&amp;',$sort_params);
  259. $origin=Security::remove_XSS($origin);
  260. if(substr($sub_course_dir,-1,1)!='/' && !empty($sub_course_dir))
  261. {
  262. $sub_course_dir = $sub_course_dir.'/';
  263. }
  264. if($sub_course_dir == '/')
  265. {
  266. $sub_course_dir='';
  267. }
  268. $session_condition = intval($_SESSION['id_session'])!=0 ?"AND session_id IN (0,".intval($_SESSION['id_session']).")" : "";
  269. //Get list from database
  270. if($is_allowed_to_edit)
  271. {
  272. $sql_get_publications_list = "SELECT * " .
  273. "FROM ".$work_table." " .
  274. "WHERE url LIKE BINARY '$sub_course_dir%' " .
  275. "AND url NOT LIKE BINARY '$sub_course_dir%/%' " .$add_in_where_query.
  276. $session_condition.
  277. "ORDER BY id";
  278. $sql_get_publications_num = "SELECT count(*) " .
  279. "FROM ".$work_table." " .
  280. "WHERE url LIKE BINARY '$sub_course_dir%' " .$add_in_where_query.
  281. "AND url NOT LIKE BINARY '$sub_course_dir%/%' " .
  282. $session_condition.
  283. "ORDER BY id";
  284. }
  285. else
  286. {
  287. if (!empty($_SESSION['toolgroup']))
  288. {
  289. $group_query = " WHERE post_group_id = '".$_SESSION['toolgroup']."' "; // set to select only messages posted by the user's group
  290. $subdirs_query = "AND url NOT LIKE BINARY '$sub_course_dir%/%' AND url LIKE BINARY '$sub_course_dir%'";
  291. }
  292. else
  293. {
  294. $group_query = '';
  295. $subdirs_query = "WHERE url NOT LIKE '$sub_course_dir%/%' AND url LIKE '$sub_course_dir%'";
  296. }
  297. $sql_get_publications_list = "SELECT * FROM $work_table $group_query $subdirs_query ".$add_in_where_query." AND session_id IN (0,".intval($_SESSION['id_session']).") ORDER BY id";
  298. $sql_get_publications_num = "SELECT count(url) " .
  299. "FROM ".$work_table." " .
  300. "WHERE url LIKE BINARY '$sub_course_dir%' " .
  301. "AND url NOT LIKE BINARY '$sub_course_dir%/%' " .$add_in_where_query.
  302. $session_condition.
  303. "ORDER BY id";
  304. }
  305. $sql_result = api_sql_query($sql_get_publications_list,__FILE__,__LINE__);
  306. $sql_result_num = api_sql_query($sql_get_publications_num,__FILE__,__LINE__);
  307. $row=Database::fetch_array($sql_result_num);
  308. $count_files=$row[0];
  309. $table_header[] = array(get_lang('Type'),true,'style="width:40px"');
  310. $table_header[] = array(get_lang('Title'),true);
  311. if ($count_files!=0)
  312. {
  313. $table_header[] = array(get_lang('Authors'),true);
  314. }
  315. $table_header[] = array(get_lang('Date'),true);
  316. if( $is_allowed_to_edit)
  317. {
  318. $table_header[] = array(get_lang('Modify'),true);
  319. }
  320. $table_header[] = array('RealDate',false);
  321. // An array with the setting of the columns -> 1: columns that we will show, 0:columns that will be hide
  322. $column_show[]=1; // type
  323. $column_show[]=1; // title
  324. if ($count_files!=0)
  325. {
  326. $column_show[]=1; // authors
  327. }
  328. $column_show[]=1; //date
  329. if( $is_allowed_to_edit)
  330. {
  331. $column_show[]=1; //modify
  332. }
  333. $column_show[]=0; //real date in correct format
  334. // Here we change the way how the colums are going to be sort
  335. // in this case the the column of LastResent ( 4th element in $column_header) we will be order like the column RealDate
  336. // because in the column RealDate we have the days in a correct format "2008-03-12 10:35:48"
  337. $column_order[]=1; //type
  338. $column_order[]=2; // title
  339. if ($count_files!=0)
  340. {
  341. $column_order[]=3; //authors
  342. }
  343. $column_order[]=6; // date
  344. if( $is_allowed_to_edit)
  345. {
  346. $column_order[]=5;
  347. }
  348. $column_order[]=6;
  349. $table_data = array();
  350. $dirs_list = get_subdirs_list($work_dir);
  351. $my_sub_dir = str_replace('work/','',$sub_course_dir);
  352. // List of all folders
  353. foreach($dirs_list as $dir)
  354. {
  355. if ($my_sub_dir=='')
  356. {
  357. $mydir_temp = '/'.$dir;
  358. }
  359. else
  360. {
  361. $mydir_temp = '/'.$my_sub_dir.$dir;
  362. }
  363. // select the directory's date
  364. /*$sql_select_directory= "SELECT sent_date FROM ".$work_table." WHERE " .
  365. "url LIKE BINARY '".$mydir_temp."' AND filetype = 'folder'";
  366. */
  367. $session_condition = intval($_SESSION['id_session'])!=0 ?"AND work.session_id IN (0,".intval($_SESSION['id_session']).")" : "";
  368. $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 " .
  369. "work.url LIKE BINARY '".$mydir_temp."' AND work.filetype = 'folder' AND prop.tool='work' $session_condition";
  370. $result=api_sql_query($sql_select_directory,__FILE__,__LINE__);
  371. $row=Database::fetch_array($result);
  372. if(!$row) // the folder belongs to another session
  373. continue;
  374. $direc_date= $row['lastedit_date']; //directory's date
  375. $author= $row['author']; //directory's author
  376. $folder_session_id = $row['session_id'];
  377. $view_properties=$row['view_properties'];
  378. $is_assignment = $row['has_properties'];
  379. $id2=$row['id'];
  380. $mydir = $my_sub_dir.$dir;
  381. if ($is_allowed_to_edit)
  382. {
  383. $clean_edit_dir=Security :: remove_XSS(Database::escape_string($_GET['edit_dir']));
  384. // form edit directory
  385. if(isset($clean_edit_dir) && $clean_edit_dir==$mydir)
  386. {
  387. if(!empty($row['has_properties'])){
  388. $sql = api_sql_query('SELECT * FROM '.Database :: get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT).' WHERE id = '."'".$row['has_properties']."'".' LIMIT 1',__FILE__,__LINE__);
  389. $homework = mysql_fetch_array($sql);
  390. }
  391. $form_folder = new FormValidator('edit_dir', 'post', api_get_self().'?curdirpath='.$my_sub_dir.'&origin='.$origin.'&edit_dir='.$mydir);
  392. //echo $row['view_properties'];
  393. //echo $row['has_properties'];
  394. $group_name[] = FormValidator :: createElement('text','dir_name');
  395. $form_folder-> addElement('textarea','description',get_lang('Description'),array('rows'=>5,'cols'=>50));
  396. $qualification_input[] = FormValidator :: createElement('text','qualification');
  397. $form_folder -> addGroup($qualification_input,'qualification',get_lang('Qualification'),'size="10"');
  398. if($row['view_properties']=='1'){
  399. if($homework['expires_on']!='0000-00-00 00:00:00'){
  400. $there_is_a_expire_date = true;
  401. $form_folder -> addGroup(create_group_date_select(),'expires',get_lang('Expires_At'));
  402. }
  403. if($homework['ends_on']!='0000-00-00 00:00:00'){
  404. $there_is_a_end_date = true;
  405. $form_folder -> addGroup(create_group_date_select(),'ends',get_lang('Ends_At'));
  406. }
  407. $form_folder -> addRule (array('expires','ends'), get_lang('DateExpiredNotBeLessDeadLine'), 'comparedate');
  408. }
  409. else
  410. {
  411. //$form_folder -> addElement('checkbox', 'enableRandom', null, get_lang('MakeRandom'),'1');
  412. $form_folder -> addElement('html','<div class="row">
  413. <div class="label">&nbsp;</div>
  414. <div class="formw">
  415. <a href="javascript://" onclick="if(document.getElementById(\'options\').style.display == \'none\'){document.getElementById(\'options\').style.display = \'block\';}else{document.getElementById(\'options\').style.display = \'none\';}"><img src="../img/add_na.gif" alt="" />'.get_lang('AdvancedParameters').'</a>
  416. </div>
  417. </div> ');
  418. $form_folder -> addElement('html','<div id="options" style="display: none;">');
  419. if(empty($default)) $default = date('Y-m-d 12:00:00');
  420. $parts = split(' ',$default);
  421. list($d_year,$d_month,$d_day) = split('-',$parts[0]);
  422. list($d_hour,$d_minute) = split(':',$parts[1]);
  423. if($homework['expires_on']='0000-00-00 00:00:00'){
  424. $homework['expires_on']=date("Y-m-d h:i:s");
  425. $there_is_a_expire_date = true;
  426. $form_folder -> addElement('checkbox', 'enableRandomExpires', null, get_lang('EnableExpireDate'),'1');
  427. $form_folder -> addGroup(create_group_date_select(),'expires',get_lang('Expires_At'));
  428. }
  429. if($homework['ends_on']='0000-00-00 00:00:00'){
  430. $homework['ends_on']=date("Y-m-d h:i:s");
  431. $there_is_a_end_date = true;
  432. $form_folder -> addElement('checkbox', 'enableRandomEnds', null, get_lang('EnableEnd'),'1');
  433. $form_folder -> addGroup(create_group_date_select(),'ends',get_lang('Ends_At'));
  434. }
  435. //$form_folder -> addRule ('expires', get_lang('Dateinconsistent'), 'errordate');
  436. //$form_folder -> addRule ('ends', get_lang('Dateinconsistent'), 'errordate');
  437. $form_folder -> addRule (array('expires','ends'), get_lang('DateExpiredNotBeLessDeadLine'), 'comparedate');
  438. $form_folder -> addElement('html','</div>');
  439. }
  440. $group_name[] = FormValidator :: createElement('submit','submit_edit_dir',get_lang('Ok'));
  441. $form_folder -> addGroup($group_name,'my_group');
  442. $form_folder -> addGroupRule('my_group',get_lang('ThisFieldIsRequired'),'required');
  443. $defaults = array('my_group[dir_name]'=>$dir,'description'=>$row['description']);
  444. if($there_is_a_end_date == true)
  445. $defaults = array_merge($defaults,convert_date_to_array($homework['ends_on'],'ends'));
  446. if($there_is_a_expire_date == true)
  447. $defaults = array_merge($defaults,convert_date_to_array($homework['expires_on'],'expires'));
  448. if(!empty($row['qualification']))
  449. $defaults = array_merge($defaults,array('qualification[qualification]'=>$row['qualification']));
  450. $form_folder -> setDefaults($defaults);
  451. $display_edit_form=true;
  452. if($form_folder -> validate())
  453. {
  454. if($there_is_a_end_date == true || $there_is_a_expire_date == true)
  455. {
  456. if($row['view_properties']=='1')
  457. {
  458. $sql_add_publication = "UPDATE ".Database :: get_course_table(TABLE_STUDENT_PUBLICATION)." SET has_properties = '".$row['has_properties']. "', view_properties=1 where id ='".$row['id']."'";
  459. api_sql_query($sql_add_publication, __FILE__, __LINE__);
  460. $expires_query= ' SET expires_on = '."'".(($there_is_a_expire_date == true)?get_date_from_group('expires'):'0000-00-00 00:00:00')."'".',';
  461. $ends_query = ' ends_on = '."'".(($there_is_a_end_date == true) ? get_date_from_group('ends') : '0000-00-00 00:00:00')."'";
  462. api_sql_query('UPDATE '.Database :: get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT).$expires_query.$ends_query.' WHERE id = '."'".$row['has_properties']."'",__FILE__,__LINE__);
  463. }
  464. else if($row['view_properties']=='0')
  465. {
  466. if ($_POST['enableRandomExpires']=='1')
  467. {
  468. $expires_query= ' SET expires_on = '."'".(($there_is_a_expire_date == true)?get_date_from_group('expires'):'0000-00-00 00:00:00')."'";
  469. //$ends_query = ' ends_on = '."'".(($there_is_a_end_date == true) ? get_date_from_group('ends') : '0000-00-00 00:00:00')."'";
  470. api_sql_query('UPDATE '.Database :: get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT).$expires_query.' WHERE id = '."'".$row['has_properties']."'",__FILE__,__LINE__);
  471. $sql_add_publication = "UPDATE ".Database :: get_course_table(TABLE_STUDENT_PUBLICATION)." SET has_properties = '".$row['has_properties']. "', view_properties=1 where id ='".$row['id']."'";
  472. api_sql_query($sql_add_publication, __FILE__, __LINE__);
  473. }
  474. if ($_POST['enableRandomEnds']=='1')
  475. {
  476. //$expires_query= ' SET expires_on = '."'".(($there_is_a_expire_date == true)?get_date_from_group('expires'):'0000-00-00 00:00:00')."'".',';
  477. $ends_query = ' SET ends_on = '."'".(($there_is_a_end_date == true) ? get_date_from_group('ends') : '0000-00-00 00:00:00')."'";
  478. api_sql_query('UPDATE '.Database :: get_course_table(TABLE_STUDENT_PUBLICATION_ASSIGNMENT).$ends_query.' WHERE id = '."'".$row['has_properties']."'",__FILE__,__LINE__);
  479. $sql_add_publication = "UPDATE ".Database :: get_course_table(TABLE_STUDENT_PUBLICATION)." SET has_properties = '".$row['has_properties']. "', view_properties=1 where id ='".$row['id']."'";
  480. api_sql_query($sql_add_publication, __FILE__, __LINE__);
  481. }
  482. }
  483. }
  484. //if($_POST['qualification']['qualification']!='')
  485. api_sql_query('UPDATE '.Database :: get_course_table(TABLE_STUDENT_PUBLICATION).' SET description = '."'".Database::escape_string($_POST['description'])."'".', qualification = '."'".Database::escape_string($_POST['qualification']['qualification'])."'".' WHERE id = '."'".$row['id']."'",__FILE__,__LINE__);
  486. $values = $form_folder -> exportValues();
  487. $values = $values['my_group'];
  488. update_dir_name($mydir,$values['dir_name']);
  489. $mydir = $my_sub_dir.$values['dir_name'];
  490. $dir = $values['dir_name'];
  491. $display_edit_form=false;
  492. }
  493. }
  494. }
  495. $action = '';
  496. $row = array();
  497. $class = '';
  498. $row[] = '<img src="../img/folder_document.gif" border="0" hspace="5" align="middle" alt="'.get_lang('Folder').'" />'; //image
  499. $a_count_directory=count_dir($work_dir.'/'.$dir,false);
  500. $cant_files=$a_count_directory[0];
  501. $cant_dir=$a_count_directory[1];
  502. $text_file=get_lang('FilesUpload');
  503. $text_dir=get_lang('Directories');
  504. if ($cant_files==1)
  505. {
  506. $text_file=strtolower(get_lang('FileUpload'));
  507. }
  508. if ($cant_dir==1)
  509. {
  510. $text_dir=get_lang('directory');
  511. }
  512. if ($cant_dir!=0)
  513. {
  514. $dirtext=' ('.$cant_dir.' '.$text_dir.')';
  515. }
  516. else
  517. {
  518. $dirtext='';
  519. }
  520. if($display_edit_form && isset($clean_edit_dir) && $clean_edit_dir==$mydir)
  521. {
  522. $row[] = '<span class="invisible" style="display:none">'.$dir.'</span>'.$form_folder->toHtml(); // form to edit the directory's name
  523. }
  524. else
  525. {
  526. $add_to_name = '';
  527. if($view_properties==1) $add_to_name = ' / <span style="color:blue">'.get_lang('Assignment').'</span>';
  528. $row[] = '<a href="'.api_get_self().'?'.api_get_cidreq().'&origin='.$origin.'&curdirpath='.$mydir.'"'.$class.'>'.$dir.'</a>'.$add_to_name.'<br>'.$cant_files.' '.$text_file.$dirtext;
  529. }
  530. if ($count_files!=0)
  531. {
  532. $row[] = "";
  533. }
  534. if ($direc_date!='' && $direc_date!='0000-00-00 00:00:00')
  535. {
  536. $row[]= date_to_str_ago($direc_date).'<br><span class="dropbox_date">'.$direc_date.'</span>';
  537. }
  538. else
  539. {
  540. $row[]='';
  541. }
  542. if( $is_allowed_to_edit && !(api_is_course_coach() && $folder_session_id!=$_SESSION['id_session']))
  543. {
  544. $action .= '<a href="'.api_get_self().'?cidReq='.api_get_course_id().
  545. '&curdirpath='.$my_sub_dir.'&origin='.$origin.'&edit_dir='.$mydir.'"><img src="../img/edit.gif" alt="'.get_lang('Modify').'"></a>';
  546. $action .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&origin='.$origin.'&delete_dir='.$mydir.'&delete2='.$id2.'" onclick="javascript:if(!confirm('."'".addslashes(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').'"></a>';
  547. $row[] = $action;
  548. }
  549. else
  550. {
  551. $row[] = "";
  552. }
  553. $table_data[] = $row;
  554. }
  555. while( $work = mysql_fetch_object($sql_result))
  556. {
  557. //Get the author ID for that document from the item_property table
  558. $is_author = false;
  559. $author_sql = "SELECT * FROM $iprop_table WHERE tool = 'work' AND insert_user_id='$user_id' AND ref=".$work->id;
  560. $author_qry = api_sql_query($author_sql,__FILE__,__LINE__);
  561. if(Database::num_rows($author_qry)==1)
  562. {
  563. $is_author = true;
  564. }
  565. //display info depending on the permissions
  566. if( $work->accepted == '1' || $is_allowed_to_edit)
  567. {
  568. $row = array();
  569. if($work->accepted == '0')
  570. {
  571. $class='class="invisible"';
  572. }
  573. else
  574. {
  575. $class='';
  576. }
  577. if(true || defined('IS_ASSIGNMENT')):
  578. $add_string = '';
  579. if($work->qualificator_id==0)
  580. $qualification_string = ' / <b style="color:orange">'.get_lang('NotRevised').'<b>';
  581. else
  582. $qualification_string = ' / <b style="color:blue">'.get_lang('Qualification').': '.$work->qualification.'<b>';
  583. if(defined('ASSIGNMENT_EXPIRES') && (ASSIGNMENT_EXPIRES < convert_date_to_number($work->sent_date))){
  584. $add_string = ' <b style="color:red">'.get_lang('Expired').'</b>';
  585. }
  586. endif;
  587. $url = implode("/", array_map("rawurlencode", explode("/", $work->url)));
  588. $row[]= build_document_icon_tag('file',$work->url);
  589. $row[]= '<a href="'.$currentCourseRepositoryWeb.$url.'"'.$class.'><img src="../img/filesave.gif" style="float:right;" alt="'.get_lang('Save').'" />'.$work->title.'</a><br />'.$work->description;
  590. $row[]= display_user_link($user_id,$work->author).$qualification_string;// $work->author;
  591. $row[]= date_to_str_ago($work->sent_date).$add_string.'<br><span class="dropbox_date">'.$work->sent_date.'</span>';
  592. if( $is_allowed_to_edit && !(api_is_course_coach() && $work->session_id!=$_SESSION['id_session']))
  593. {
  594. $action = '';
  595. $action .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&curdirpath='.urlencode($my_sub_dir).'&amp;origin='.$origin.'&amp;edit='.$work->id.'" title="'.get_lang('Modify').'" ><img src="../img/edit.gif" alt="'.get_lang('Modify').'"></a>';
  596. $action .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&curdirpath='.urlencode($my_sub_dir).'&amp;origin='.$origin.'&amp;delete='.$work->id.'" onclick="javascript:if(!confirm('."'".addslashes(htmlentities(get_lang('ConfirmYourChoice'),ENT_QUOTES,$charset))."'".')) return false;" title="'.get_lang('WorkDelete').'" ><img src="../img/delete.gif" alt="'.get_lang('WorkDelete').'"></a>';
  597. $action .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&curdirpath='.urlencode($my_sub_dir).'&amp;origin='.$origin.'&amp;move='.$work->id.'" title="'.get_lang('Move').'"><img src="../img/deplacer_fichier.gif" border="0" title="'.get_lang('Move').'" alt="" /></a>';
  598. if($work->accepted == '1')
  599. {
  600. $action .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&curdirpath='.urlencode($my_sub_dir).'&amp;origin='.$origin.'&amp;make_invisible='.$work->id.'&amp;'.$sort_params.'" title="'.get_lang('Invisible').'" ><img src="../img/visible.gif" alt="'.get_lang('Invisible').'"></a>';
  601. }
  602. else
  603. {
  604. $action .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&curdirpath='.urlencode($my_sub_dir).'&amp;origin='.$origin.'&amp;make_visible='.$work->id.'&amp;'.$sort_params.'" title="'.get_lang('Visible').'" ><img src="../img/invisible.gif" alt="'.get_lang('Visible').'"></a>';
  605. }
  606. $row[] = $action;
  607. }
  608. elseif($is_author)
  609. {
  610. $action = '';
  611. $action .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&curdirpath='.urlencode($my_sub_dir).'&amp;origin='.$origin.'&amp;edit='.$work->id.'" title="'.get_lang('Modify').'" ><img src="../img/edit.gif" alt="'.get_lang('Modify').'"></a>';
  612. $action .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&curdirpath='.urlencode($my_sub_dir).'&amp;origin='.$origin.'&amp;delete='.$work->id.'" onclick="javascript:if(!confirm('."'".addslashes(htmlentities(get_lang('ConfirmYourChoice'),ENT_QUOTES,$charset))."'".')) return false;" title="'.get_lang('WorkDelete').'" ><img src="../img/delete.gif" alt="'.get_lang('WorkDelete').'"></a>';
  613. $row[] = $action;
  614. }
  615. else
  616. {
  617. $row[] = " ";
  618. }
  619. $table_data[] = $row;
  620. }
  621. }
  622. $sorting_options=array();
  623. $sorting_options['column']=1;
  624. $paging_options=array();
  625. Display::display_sortable_config_table($table_header,$table_data,$sorting_options, $paging_options,NULL,$column_show,$column_order);
  626. }
  627. /**
  628. * Returns a list of subdirectories found in the given directory.
  629. *
  630. * The list return starts from the given base directory.
  631. * If you require the subdirs of /var/www/ (or /var/www), you will get 'abc/', 'def/', but not '/var/www/abc/'...
  632. * @param string Base dir
  633. * @param integer 0 if we only want dirs from this level, 1 if we want to recurse into subdirs
  634. * @return strings_array The list of subdirs in 'abc/' form, -1 on error, and 0 if none found
  635. * @todo Add a session check to see if subdirs_list doesn't exist yet (cached copy)
  636. */
  637. function get_subdirs_list($basedir='',$recurse=0){
  638. //echo "Looking for subdirs of $basedir";
  639. if(empty($basedir) or !is_dir($basedir)){return -1;}
  640. if(substr($basedir,-1,1)!='/'){$basedir = $basedir.'/';}
  641. $dirs_list = array();
  642. $dh = opendir($basedir);
  643. while($entry = readdir($dh)){
  644. if(is_dir($basedir.$entry) && $entry!='..' && $entry!='.'){
  645. $dirs_list[] = $entry;
  646. if($recurse==1){
  647. foreach(get_subdirs_list($basedir.$entry) as $subdir){
  648. $dirs_list[] = $entry.'/'.$subdir;
  649. }
  650. }
  651. }
  652. }
  653. closedir($dh);
  654. return $dirs_list;
  655. }
  656. /**
  657. * Builds the form thats enables the user to
  658. * select a directory to browse/upload in
  659. * This function has been copied from the document/document.inc.php library
  660. *
  661. * @param array $folders
  662. * @param string $curdirpath
  663. * @param string $group_dir
  664. * @return string html form
  665. */
  666. function build_work_directory_selector($folders,$curdirpath,$group_dir='')
  667. {
  668. $form = '<form name="selector" action="'.api_get_self().'?'.api_get_cidreq().'" method="POST">'."\n";
  669. $form .= get_lang('CurrentDirectory').' <select name="curdirpath" onchange="javascript:document.selector.submit()">'."\n";
  670. //group documents cannot be uploaded in the root
  671. if($group_dir=='')
  672. {
  673. $form .= '<option value="/">/ ('.get_lang('Root').')</option>';
  674. if(is_array($folders))
  675. {
  676. foreach ($folders as $folder)
  677. {
  678. $selected = ($curdirpath==$folder)?' selected="selected"':'';
  679. $form .= '<option'.$selected.' value="'.$folder.'">'.$folder.'</option>'."\n";
  680. }
  681. }
  682. }
  683. else
  684. {
  685. foreach ($folders as $folder)
  686. {
  687. $selected = ($curdirpath==$folder)?' selected="selected"':'';
  688. $display_folder = substr($folder,strlen($group_dir));
  689. $display_folder = ($display_folder == '')?'/ ('.get_lang('Root').')':$display_folder;
  690. $form .= '<option'.$selected.' value="'.$folder.'">'.$display_folder.'</option>'."\n";
  691. }
  692. }
  693. $form .= '</select>'."\n";
  694. $form .= '<noscript><input type="submit" name="change_path" value="'.get_lang('Ok').'" /></noscript>'."\n";
  695. $form .= '</form>';
  696. return $form;
  697. }
  698. /**
  699. * Builds the form thats enables the user to
  700. * move a document from one directory to another
  701. * This function has been copied from the document/document.inc.php library
  702. *
  703. * @param array $folders
  704. * @param string $curdirpath
  705. * @param string $move_file
  706. * @return string html form
  707. */
  708. function build_work_move_to_selector($folders,$curdirpath,$move_file,$group_dir='')
  709. {
  710. $form = '<form name="move_to" action="'.api_get_self().'" method="POST">'."\n";
  711. $form .= '<input type="hidden" name="move_file" value="'.$move_file.'" />'."\n";
  712. $form .= get_lang('MoveTo').' <select name="move_to">'."\n";
  713. //group documents cannot be uploaded in the root
  714. if($group_dir=='')
  715. {
  716. if($curdirpath!='/')
  717. {
  718. $form .= '<option value="/">/ ('.get_lang('Root').')</option>';
  719. }
  720. if(is_array($folders))
  721. {
  722. foreach ($folders as $folder)
  723. {
  724. //you cannot move a file to:
  725. //1. current directory
  726. //2. inside the folder you want to move
  727. //3. inside a subfolder of the folder you want to move
  728. if(($curdirpath!=$folder) && ($folder!=$move_file) && (substr($folder,0,strlen($move_file)+1) != $move_file.'/'))
  729. {
  730. $form .= '<option value="'.$folder.'">'.$folder.'</option>'."\n";
  731. }
  732. }
  733. }
  734. }
  735. else
  736. {
  737. if($curdirpath!='/')
  738. {
  739. $form .= '<option value="/">/ ('.get_lang('Root').')</option>';
  740. }
  741. foreach ($folders as $folder)
  742. {
  743. if(($curdirpath!=$folder) && ($folder!=$move_file) && (substr($folder,0,strlen($move_file)+1) != $move_file.'/'))//cannot copy dir into his own subdir
  744. {
  745. $display_folder = substr($folder,strlen($group_dir));
  746. $display_folder = ($display_folder == '')?'/ ('.get_lang('Root').')':$display_folder;
  747. $form .= '<option value="'.$folder.'">'.$display_folder.'</option>'."\n";
  748. }
  749. }
  750. }
  751. $form .= '</select>'."\n";
  752. $form .= '<input type="submit" name="move_file_submit" value="'.get_lang('Ok').'" />'."\n";
  753. $form .= '</form>';
  754. return $form;
  755. }
  756. /**
  757. * Checks if the first given directory exists as a subdir of the second given directory
  758. * This function should now be deprecated by Security::check_abs_path()
  759. * @param string Subdir
  760. * @param string Base dir
  761. * @return integer -1 on error, 0 if not subdir, 1 if subdir
  762. */
  763. function is_subdir_of($subdir,$basedir){
  764. if(empty($subdir) or empty($basedir)){return -1;}
  765. if(substr($basedir,-1,1)!='/'){$basedir=$basedir.'/';}
  766. if(substr($subdir,0,1)=='/'){$subdir = substr($subdir,1);}
  767. if(is_dir($basedir.$subdir)){
  768. return 1;
  769. }else{
  770. return 0;
  771. }
  772. }
  773. /**
  774. * creates a new directory trying to find a directory name
  775. * that doesn't already exist
  776. * (we could use unique_name() here...)
  777. *
  778. * @author Hugues Peeters <hugues.peeters@claroline.net>
  779. * @author Bert Vanderkimpen
  780. * @author Yannick Warnier <ywarnier@beeznest.org> Adaptation for work tool
  781. * @param string Base work dir (.../work)
  782. * @param string $desiredDirName complete path of the desired name
  783. * @return string actual directory name if it succeeds,
  784. * boolean false otherwise
  785. */
  786. function create_unexisting_work_directory($base_work_dir,$desired_dir_name)
  787. {
  788. $nb = '';
  789. $base_work_dir = (substr($base_work_dir,-1,1)=='/'?$base_work_dir:$base_work_dir.'/');
  790. while ( file_exists($base_work_dir.$desired_dir_name.$nb) )
  791. {
  792. $nb += 1;
  793. }
  794. //echo "creating ".$base_work_dir.$desired_dir_name.$nb."#...";
  795. $perm = api_get_setting('permissions_for_new_directories');
  796. $perm = octdec(!empty($perm)?$perm:'0770');
  797. if ( mkdir($base_work_dir.$desired_dir_name.$nb, $perm))
  798. {
  799. chmod($base_work_dir.$desired_dir_name.$nb, $perm);
  800. return $desired_dir_name.$nb;
  801. }
  802. else
  803. {
  804. return false;
  805. }
  806. }
  807. /**
  808. * Delete a work-tool directory
  809. * @param string Base "work" directory for this course as /var/www/dokeos/courses/ABCD/work/
  810. * @param string The directory name as the bit after "work/", without trailing slash
  811. * @return integer -1 on error
  812. */
  813. function del_dir($base_work_dir,$dir)
  814. {
  815. if(empty($dir) or $dir=='/'){return -1;}//not authorized
  816. //escape hacks
  817. /*
  818. $dir = str_replace('../','',$dir);
  819. $dir = str_replace('..','',$dir);
  820. $dir = str_replace('./','',$dir);
  821. $dir = str_replace('.','',$dir);
  822. */
  823. $check = Security::check_abs_path($base_work_dir.$dir,$base_work_dir);
  824. if (!$check || !is_dir($base_work_dir.$dir)) return -1;
  825. $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  826. $sql = "DELETE FROM $table WHERE url LIKE BINARY 'work/".$dir."/%'";
  827. if(api_is_course_coach())
  828. {
  829. $sql .= ' AND session_id='.intval($_SESSION['id_session']);
  830. }
  831. $res = api_sql_query($sql,__FILE__,__LINE__);
  832. //delete from DB the directories
  833. $sql = "DELETE FROM $table WHERE filetype = 'folder' AND url LIKE BINARY '/".$dir."%'";
  834. if(api_is_course_coach())
  835. {
  836. $sql .= ' AND session_id='.intval($_SESSION['id_session']);
  837. }
  838. $res = api_sql_query($sql,__FILE__,__LINE__);
  839. if(Database::affected_rows()>0)
  840. {
  841. require_once(api_get_path(LIBRARY_PATH).'/fileManage.lib.php');
  842. my_delete($base_work_dir.$dir);
  843. }
  844. }
  845. /**
  846. * Get the path of a document in the student_publication table (path relative to the course directory)
  847. * @param integer Element ID
  848. * @return string Path (or -1 on error)
  849. */
  850. function get_work_path($id){
  851. $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  852. $sql = "SELECT * FROM $table WHERE id=$id";
  853. $res = api_sql_query($sql);
  854. if(Database::num_rows($res)!=1){
  855. return -1;
  856. }else{
  857. $row = Database::fetch_array($res);
  858. return $row['url'];
  859. }
  860. }
  861. /**
  862. * Update the url of a work in the student_publication table
  863. * @param integer ID of the work to update
  864. * @param string Destination directory where the work has been moved (must end with a '/')
  865. * @return -1 on error, sql query result on success
  866. */
  867. function update_work_url($id,$new_path)
  868. {
  869. if(empty($id)) return -1;
  870. $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  871. $sql = "SELECT * FROM $table WHERE id=$id";
  872. $res = api_sql_query($sql);
  873. if(Database::num_rows($res)!=1){
  874. return -1;
  875. }else{
  876. $row = Database::fetch_array($res);
  877. $filename = basename($row['url']);
  878. $new_url = $new_path.$filename;
  879. $sql2 = "UPDATE $table SET url = '$new_url' WHERE id=$id";
  880. $res2 = api_sql_query($sql2);
  881. return $res2;
  882. }
  883. }
  884. /**
  885. * Update the url of a dir in the student_publication table
  886. * @param string old path
  887. * @param string new path
  888. */
  889. function update_dir_name($path, $new_name)
  890. {
  891. global $base_work_dir;
  892. include_once(api_get_path(LIBRARY_PATH) . "/fileManage.lib.php");
  893. include_once(api_get_path(LIBRARY_PATH) . "/fileUpload.lib.php");
  894. $path_to_dir = dirname($path);
  895. if($path_to_dir=='.')
  896. {
  897. $path_to_dir = '';
  898. }
  899. else
  900. {
  901. $path_to_dir .= '/';
  902. }
  903. my_rename($base_work_dir.'/'.$path,$new_name);
  904. $table = Database::get_course_table(TABLE_STUDENT_PUBLICATION);
  905. //update all the files in the other directories according with the next query
  906. $sql = 'SELECT id, url FROM '.$table.' WHERE url LIKE BINARY "work/'.$path.'/%"'; // like binary (Case Sensitive)
  907. $rs = api_sql_query($sql, __FILE__, __LINE__);
  908. $work_len=strlen('work/'.$path);
  909. while($work = Database :: fetch_array($rs))
  910. {
  911. $new_dir=$work['url'];
  912. $name_with_directory=substr($new_dir,$work_len,strlen($new_dir));
  913. $sql = 'UPDATE '.$table.' SET url="work/'.$path_to_dir.$new_name.$name_with_directory.'" WHERE id= '.$work['id'];
  914. api_sql_query($sql, __FILE__, __LINE__);
  915. }
  916. //update all the directory's children according with the next query
  917. $sql = 'SELECT id, url FROM '.$table.' WHERE url LIKE BINARY "/'.$path.'%"';
  918. $rs = api_sql_query($sql, __FILE__, __LINE__);
  919. $work_len=strlen('/'.$path);
  920. while($work = Database :: fetch_array($rs))
  921. {
  922. $new_dir=$work['url'];
  923. $name_with_directory=substr($new_dir,$work_len,strlen($new_dir));
  924. $sql = 'UPDATE '.$table.' SET url="/'.$path_to_dir.$new_name.$name_with_directory.'" WHERE id= '.$work['id'];
  925. api_sql_query($sql, __FILE__, __LINE__);
  926. }
  927. }
  928. /**
  929. * Return an array with all the folder's ids that are in the given path
  930. * @param string Path of the directory
  931. * @return array The list of ids of all the directories in the path
  932. * @author Julio Montoya Dokeos
  933. * @version April 2008
  934. */
  935. function get_parent_directories($my_cur_dir_path)
  936. {
  937. $list_parents = explode('/', $my_cur_dir_path);
  938. $dir_acum = '';
  939. global $work_table;
  940. $list_id=array();
  941. for ($i = 0; $i < count($list_parents) - 1; $i++)
  942. {
  943. $where_sentence = "url LIKE BINARY '" . $dir_acum . "/" . $list_parents[$i]."'";
  944. $dir_acum .= '/' . $list_parents[$i];
  945. $sql = "SELECT id FROM ". $work_table . " WHERE ". $where_sentence;
  946. $result = api_sql_query($sql, __FILE__, __LINE__);
  947. $row= Database::fetch_array($result);
  948. $list_id[]=$row['id'];
  949. }
  950. return $list_id;
  951. }
  952. /**
  953. * Transform an all directory structure (only directories) in an array
  954. * @param string path of the directory
  955. * @return array the directory structure into an array
  956. * @author Julio Montoya Dokeos
  957. * @version April 2008
  958. */
  959. function directory_to_array($directory)
  960. {
  961. $array_items = array();
  962. if ($handle = opendir($directory))
  963. {
  964. while (false !== ($file = readdir($handle)))
  965. {
  966. if ($file != "." && $file != "..")
  967. {
  968. if (is_dir($directory. "/" . $file))
  969. {
  970. $array_items = array_merge($array_items, directory_to_array($directory. "/" . $file));
  971. $file = $directory . "/" . $file;
  972. $array_items[] = preg_replace("/\/\//si", "/", $file);
  973. }
  974. }
  975. }
  976. closedir($handle);
  977. }
  978. return $array_items;
  979. }
  980. /**
  981. * Insert into the DB of the course all the directories
  982. * @param string path of the /work directory of the course
  983. * @return -1 on error, sql query result on success
  984. * @author Julio Montoya Dokeos
  985. * @version April 2008
  986. */
  987. function insert_all_directory_in_course_table($base_work_dir)
  988. {
  989. $dir_to_array =directory_to_array($base_work_dir,true);
  990. $only_dir=array();
  991. for($i=0;$i<count($dir_to_array);$i++)
  992. {
  993. $only_dir[]=substr($dir_to_array[$i],strlen($base_work_dir), strlen($dir_to_array[$i]));
  994. }
  995. /*
  996. echo "<pre>";
  997. print_r($only_dir);
  998. echo "<pre>";
  999. */
  1000. for($i=0;$i<count($only_dir);$i++)
  1001. {
  1002. global $work_table;
  1003. $sql_insert_all= "INSERT INTO " . $work_table . " SET url = '" . $only_dir[$i] . "', " .
  1004. "title = '',
  1005. description = '',
  1006. author = '',
  1007. active = '0',
  1008. accepted = '1',
  1009. filetype = 'folder',
  1010. post_group_id = '0',
  1011. sent_date = '0000-00-00 00:00:00' ";
  1012. //api_sql_query($sql_insert_all, __FILE__, __LINE__);
  1013. }
  1014. }
  1015. /**
  1016. * This function displays the number of files contained in a directory
  1017. *
  1018. * @param string the path of the directory
  1019. * @param boolean true if we want the total quantity of files include in others child directorys , false only files in the directory
  1020. * @return array the first element is an integer with the number of files in the folder, the second element is the number of directories
  1021. * @author Julio Montoya Dokeos
  1022. * @version April 2008
  1023. */
  1024. function count_dir($path_dir, $recurse)
  1025. {
  1026. $count = 0;
  1027. $count_dir= 0;
  1028. $d = dir($path_dir);
  1029. while ($entry = $d->Read())
  1030. {
  1031. if (!(($entry == "..") || ($entry == ".")))
  1032. {
  1033. if (is_dir($path_dir.'/'.$entry))
  1034. {
  1035. $count_dir++;
  1036. if ($recurse)
  1037. {
  1038. $count += count_dir($path_dir . '/' . $entry, $recurse);
  1039. }
  1040. }
  1041. else
  1042. {
  1043. $count++;
  1044. }
  1045. }
  1046. }
  1047. $return_array=array();
  1048. $return_array[]=$count;
  1049. $return_array[]=$count_dir;
  1050. return $return_array;
  1051. }
  1052. ?>