work.lib.php 57 KB

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