dropbox_functions.inc.php 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541
  1. <?php //$id: $
  2. /* For licensing terms, see /dokeos_license.txt */
  3. /**
  4. * This file contains additional dropbox functions. Initially there were some
  5. * functions in the init files also but I have moved them over
  6. * to one file -- Patrick Cool <patrick.cool@UGent.be>, Ghent University
  7. */
  8. /**
  9. * This function is a wrapper function for the multiple actions feature.
  10. * @return Mixed If there is a problem, return a string message, otherwise nothing
  11. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  12. * @version march 2006
  13. */
  14. function handle_multiple_actions()
  15. {
  16. global $_user, $is_courseAdmin, $is_courseTutor;
  17. // STEP 1: are we performing the actions on the received or on the sent files?
  18. if($_POST['action']=='delete_received' || $_POST['action']=='download_received')
  19. {
  20. $part = 'received';
  21. }
  22. elseif($_POST['action']=='delete_sent' || $_POST['action']=='download_sent')
  23. {
  24. $part = 'sent';
  25. }
  26. // STEP 2: at least one file has to be selected. If not we return an error message
  27. foreach ($_POST as $key=>$value)
  28. {
  29. if (strstr($value,$part.'_') AND $key!='view_received_category' AND $key!='view_sent_category')
  30. {
  31. $checked_files=true;
  32. $checked_file_ids[]=intval(substr($value,strrpos($value,'_')));
  33. }
  34. }
  35. $checked_file_ids = $_POST['id'];
  36. if (!is_array($checked_file_ids) || count($checked_file_ids)==0)
  37. {
  38. return get_lang('CheckAtLeastOneFile');
  39. }
  40. // STEP 3A: deleting
  41. if ($_POST['action']=='delete_received' || $_POST['action']=='delete_sent')
  42. {
  43. $dropboxfile=new Dropbox_Person( $_user['user_id'], $is_courseAdmin, $is_courseTutor);
  44. foreach ($checked_file_ids as $key=>$value)
  45. {
  46. if ($_GET['view']=='received')
  47. {
  48. $dropboxfile->deleteReceivedWork($value);
  49. $message=get_lang('ReceivedFileDeleted');
  50. }
  51. if ($_GET['view']=='sent' OR empty($_GET['view']))
  52. {
  53. $dropboxfile->deleteSentWork($value);
  54. $message=get_lang('SentFileDeleted');
  55. }
  56. }
  57. return $message;
  58. }
  59. // STEP 3B: giving comment
  60. if ($_POST['actions']=='comment')
  61. {
  62. // This has not been implemented.
  63. // The idea was that it would be possible to write the same feedback for the selected documents.
  64. }
  65. // STEP 3C: moving
  66. if (strstr($_POST['action'], 'move_'))
  67. {
  68. // check move_received_n or move_sent_n command
  69. if (strstr($_POST['action'],'received')){
  70. $part = 'received';
  71. $to_cat_id = str_replace('move_received_','',$_POST['action']);
  72. }
  73. else {
  74. $part = 'sent';
  75. $to_cat_id = str_replace('move_sent_','',$_POST['action']);
  76. }
  77. foreach ($checked_file_ids as $key=>$value)
  78. {
  79. var_dump($value);
  80. var_dump($to_cat_id);
  81. var_dump($part);
  82. store_move($value, $to_cat_id, $part);
  83. }
  84. return get_lang('FilesMoved');
  85. }
  86. // STEP 3D: downloading
  87. if ($_POST['action']=='download_sent' || $_POST['action']=='download_received')
  88. {
  89. zip_download($checked_file_ids);
  90. }
  91. }
  92. /**
  93. * This function deletes a dropbox category
  94. *
  95. * @todo give the user the possibility what needs to be done with the files in this category: move them to the root, download them as a zip, delete them
  96. *
  97. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  98. * @version march 2006
  99. */
  100. function delete_category($action, $id)
  101. {
  102. global $dropbox_cnf;
  103. global $_user, $is_courseAdmin, $is_courseTutor;
  104. // an additional check that might not be necessary
  105. if ($action=='deletereceivedcategory')
  106. {
  107. $sentreceived='received';
  108. $entries_table=$dropbox_cnf['tbl_post'];
  109. $id_field='file_id';
  110. $return_message = get_lang('ReceivedCatgoryDeleted');
  111. }
  112. elseif ($action=='deletesentcategory')
  113. {
  114. $sentreceived='sent';
  115. $entries_table=$dropbox_cnf['tbl_file'];
  116. $id_field='id';
  117. $return_message = get_lang('SentCatgoryDeleted');
  118. }
  119. else
  120. {
  121. return get_lang('Error');
  122. }
  123. // step 1: delete the category
  124. $sql="DELETE FROM ".$dropbox_cnf['tbl_category']." WHERE cat_id='".Database::escape_string($id)."' AND $sentreceived='1'";
  125. $result=Database::query($sql,__FILE__,__LINE__);
  126. // step 2: delete all the documents in this category
  127. $sql="SELECT * FROM ".$entries_table." WHERE cat_id='".Database::escape_string($id)."'";
  128. $result=Database::query($sql,__FILE__,__LINE__);
  129. while ($row=Database::fetch_array($result))
  130. {
  131. $dropboxfile=new Dropbox_Person( $_user['user_id'], $is_courseAdmin, $is_courseTutor);
  132. if ($action=='deletereceivedcategory')
  133. {
  134. $dropboxfile->deleteReceivedWork($row[$id_field]);
  135. }
  136. if ($action=='deletesentcategory')
  137. {
  138. $dropboxfile->deleteSentWork($row[$id_field]);
  139. }
  140. }
  141. return $return_message;
  142. }
  143. /**
  144. * Displays the form to move one individual file to a category
  145. *
  146. * @return html code of the form that appears in a dokeos message box.
  147. *
  148. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  149. * @version march 2006
  150. */
  151. function display_move_form($part, $id, $target=array())
  152. {
  153. echo '<div class="row"><div class="form_header">'.get_lang('MoveFileTo').'</div></div>';
  154. echo '<form name="form1" method="post" action="'.api_get_self().'?view_received_category='.$_GET['view_received_category'].'&view_sent_category='.$_GET['view_sent_category'].'&view='.$_GET['view'].'">';
  155. echo '<input type="hidden" name="id" value="'.Security::remove_XSS($id).'">';
  156. echo '<input type="hidden" name="part" value="'.Security::remove_XSS($part).'">';
  157. echo '
  158. <div class="row">
  159. <div class="label">
  160. <span class="form_required">*</span> '.get_lang('MoveFileTo').'
  161. </div>
  162. <div class="formw">';
  163. echo '<select name="move_target">';
  164. echo '<option value="0">'.get_lang('Root').'</option>';
  165. foreach ($target as $key=>$category)
  166. {
  167. echo '<option value="'.$category['cat_id'].'">'.$category['cat_name'].'</option>';
  168. }
  169. echo '</select>';
  170. echo ' </div>
  171. </div>';
  172. echo '
  173. <div class="row">
  174. <div class="label">
  175. </div>
  176. <div class="formw">
  177. <button class="next" type="submit" name="do_move" value="'.get_lang('Ok').'">'.get_lang('MoveFile').'</button>
  178. </div>
  179. </div>
  180. ';
  181. echo '</form>';
  182. echo '<div style="clear: both;"></div>';
  183. }
  184. /**
  185. * This function moves a file to a different category
  186. *
  187. * @param $id the id of the file we are moving
  188. * @param $target the id of the folder we are moving to
  189. * @param $part are we moving a received file or a sent file?
  190. *
  191. * @return language string
  192. *
  193. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  194. * @version march 2006
  195. */
  196. function store_move($id, $target, $part)
  197. {
  198. global $_user;
  199. global $dropbox_cnf;
  200. if ((isset($id) AND $id<>'') AND (isset($target) AND $target<>'') AND (isset($part) AND $part<>''))
  201. {
  202. if ($part=='received')
  203. {
  204. $sql="UPDATE ".$dropbox_cnf["tbl_post"]." SET cat_id='".Database::escape_string($target)."'
  205. WHERE dest_user_id='".Database::escape_string($_user['user_id'])."'
  206. AND file_id='".Database::escape_string($id)."'
  207. ";
  208. Database::query($sql,__FILE__,__LINE__);
  209. $return_message=get_lang('ReceivedFileMoved');
  210. }
  211. if ($part=='sent')
  212. {
  213. $sql="UPDATE ".$dropbox_cnf["tbl_file"]." SET cat_id='".Database::escape_string($target)."'
  214. WHERE uploader_id='".Database::escape_string($_user['user_id'])."'
  215. AND id='".Database::escape_string($id)."'
  216. ";
  217. Database::query($sql,__FILE__,__LINE__);
  218. $return_message=get_lang('SentFileMoved');
  219. }
  220. }
  221. else
  222. {
  223. $return_message=get_lang('NotMovedError');
  224. }
  225. return $return_message;
  226. }
  227. /**
  228. * This functions displays all teh possible actions that can be performed on multiple files. This is the dropdown list that
  229. * appears below the sortable table of the sent / or received files.
  230. *
  231. * @return html value for the dropdown list
  232. *
  233. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  234. * @version march 2006
  235. */
  236. function display_action_options($part, $categories, $current_category=0)
  237. {
  238. echo '<select name="actions">';
  239. echo '<option value="download">'.get_lang('Download').'</option>';
  240. echo '<option value="delete">'.get_lang('Delete').'</option>';
  241. if(is_array($categories))
  242. {
  243. echo '<optgroup label="'.get_lang('MoveTo').'">';
  244. if ($current_category<>0)
  245. {
  246. echo '<option value="move_0">'.get_lang('Root').'</a>';
  247. }
  248. foreach ($categories as $key=>$value)
  249. {
  250. if( $current_category<>$value['cat_id'])
  251. {
  252. echo '<option value="move_'.$value['cat_id'].'">'.$value['cat_name'].'</option>';
  253. }
  254. }
  255. echo '</optgroup>';
  256. }
  257. echo '</select>';
  258. echo '<input type="submit" name="do_actions_'.Security::remove_XSS($part).'" value="'.get_lang('Ok').'" />';
  259. }
  260. /**
  261. * this function returns the html code that displays the checkboxes next to the files so that
  262. * multiple actions on one file are possible.
  263. *
  264. * @param $id the unique id of the file
  265. * @param $part are we dealing with a sent or with a received file?
  266. *
  267. * @return html code
  268. *
  269. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  270. * @version march 2006
  271. */
  272. function display_file_checkbox($id, $part)
  273. {
  274. if (isset($_GET['selectall']))
  275. {
  276. $checked='checked';
  277. }
  278. $return_value='<input type="checkbox" name="'.Security::remove_XSS($part).'_'.Security::remove_XSS($id).'" value="'.Security::remove_XSS($id).'" '.$checked.' />';
  279. return $return_value;
  280. }
  281. /**
  282. * This function retrieves all the dropbox categories and returns them as an array
  283. *
  284. * @param $filter default '', when we need only the categories of the sent or the received part.
  285. *
  286. * @return array
  287. *
  288. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  289. * @version march 2006
  290. */
  291. function get_dropbox_categories($filter='')
  292. {
  293. global $_user;
  294. global $dropbox_cnf;
  295. $return_array=array();
  296. $session_id = api_get_session_id();
  297. $condition_session = api_get_session_condition($session_id);
  298. $sql="SELECT * FROM ".$dropbox_cnf['tbl_category']." WHERE user_id='".$_user['user_id']."' $condition_session";
  299. $result=Database::query($sql,__FILE__,__LINE__);
  300. while ($row=Database::fetch_array($result))
  301. {
  302. if(($filter=='sent' AND $row['sent']==1) OR ($filter=='received' AND $row['received']==1) OR $filter=='')
  303. {
  304. $return_array[$row['cat_id']]=$row;
  305. }
  306. }
  307. return $return_array;
  308. }
  309. /**
  310. * This functions stores a new dropboxcategory
  311. *
  312. * @var it might not seem very elegant if you create a category in sent and in received with the same name that you get two entries in the
  313. * dropbox_category table but it is the easiest solution. You get
  314. * cat_name | received | sent | user_id
  315. * test | 1 | 0 | 237
  316. * test | 0 | 1 | 237
  317. * more elegant would be
  318. * test | 1 | 1 | 237
  319. *
  320. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  321. * @version march 2006
  322. */
  323. function store_addcategory()
  324. {
  325. global $_user;
  326. global $dropbox_cnf;
  327. // check if the target is valid
  328. if ($_POST['target']=='sent')
  329. {
  330. $sent=1;
  331. $received=0;
  332. }
  333. elseif ($_POST['target']=='received')
  334. {
  335. $sent=0;
  336. $received=1;
  337. }
  338. else
  339. {
  340. return get_lang('Error');
  341. }
  342. // check if the category name is valid
  343. if ($_POST['category_name']=='')
  344. {
  345. return array('type' => 'error', 'message'=>get_lang('ErrorPleaseGiveCategoryName'));
  346. }
  347. if (!$_POST['edit_id'])
  348. {
  349. $session_id = api_get_session_id();
  350. // step 3a, we check if the category doesn't already exist
  351. $sql="SELECT * FROM ".$dropbox_cnf['tbl_category']." WHERE user_id='".$_user['user_id']."' AND cat_name='".Database::escape_string(Security::remove_XSS($_POST['category_name']))."' AND received='".$received."' AND sent='$sent' AND session_id='$session_id'";
  352. $result=Database::query($sql,__FILE__,__LINE__);
  353. // step 3b, we add the category if it does not exist yet.
  354. if (Database::num_rows($result)==0)
  355. {
  356. $sql="INSERT INTO ".$dropbox_cnf['tbl_category']." (cat_name, received, sent, user_id, session_id)
  357. VALUES ('".Database::escape_string(Security::remove_XSS($_POST['category_name']))."', '".Database::escape_string($received)."', '".Database::escape_string($sent)."', '".Database::escape_string($_user['user_id'])."',$session_id)";
  358. Database::query($sql,__FILE__,__LINE__);
  359. return array('type' => 'confirmation', 'message'=>get_lang('CategoryStored'));
  360. }
  361. else
  362. {
  363. return array('type' => 'error', 'message'=>get_lang('CategoryAlreadyExistsEditIt'));
  364. }
  365. }
  366. else
  367. {
  368. $sql="UPDATE ".$dropbox_cnf['tbl_category']." SET cat_name='".Database::escape_string(Security::remove_XSS($_POST['category_name']))."', received='".Database::escape_string($received)."' , sent='".Database::escape_string($sent)."'
  369. WHERE user_id='".Database::escape_string($_user['user_id'])."'
  370. AND cat_id='".Database::escape_string(Security::remove_XSS($_POST['edit_id']))."'";
  371. Database::query($sql,__FILE__,__LINE__);
  372. return array('type' => 'confirmation', 'message'=>get_lang('CategoryModified'));
  373. }
  374. }
  375. /**
  376. * This function displays the form to add a new category.
  377. *
  378. * @param $category_name this parameter is the name of the category (used when no section is selected)
  379. * @param $id this is the id of the category we are editing.
  380. *
  381. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  382. * @version march 2006
  383. */
  384. function display_addcategory_form($category_name='', $id='',$action)
  385. {
  386. global $dropbox_cnf;
  387. $title=get_lang('AddNewCategory');
  388. if (isset($id) AND $id<>'')
  389. {
  390. // retrieve the category we are editing
  391. $sql="SELECT * FROM ".$dropbox_cnf['tbl_category']." WHERE cat_id='".Database::escape_string($id)."'";
  392. $result=Database::query($sql,__FILE__,__LINE__);
  393. $row=Database::fetch_array($result);
  394. if ($category_name=='') // after an edit with an error we do not want to return to the original name but the name we already modified. (happens when createinrecievedfiles AND createinsentfiles are not checked)
  395. {
  396. $category_name=$row['cat_name'];
  397. }
  398. if ($row['received']=='1')
  399. {
  400. $target='received';
  401. }
  402. if ($row['sent']=='1')
  403. {
  404. $target='sent';
  405. }
  406. $title=get_lang('EditCategory');
  407. }
  408. if ($action=='addreceivedcategory') {
  409. $target='received';
  410. }
  411. if ($action=='addsentcategory') {
  412. $target='sent';
  413. }
  414. if ($action=='editcategory') {
  415. $text=get_lang('ModifyCategory');
  416. $class='save';
  417. } else if ($action=='addreceivedcategory' or $action=='addsentcategory') {
  418. $text=get_lang('CreateCategory');
  419. $class='add';
  420. }
  421. echo "<form name=\"add_new_category\" method=\"post\" action=\"".api_get_self()."?view=".Security::remove_XSS($_GET['view'])."\">\n";
  422. if (isset($id) AND $id<>'')
  423. {
  424. echo '<input name="edit_id" type="hidden" value="'.Security::remove_XSS($id).'">';
  425. }
  426. echo '<input name="action" type="hidden" value="'.Security::remove_XSS($action).'">';
  427. echo '<input name="target" type="hidden" value="'.$target.'">';
  428. echo '<div class="row"><div class="form_header">'.$title.'</div></div>';
  429. echo ' <div class="row">
  430. <div class="label">
  431. <span class="form_required">*</span> '.get_lang('CategoryName').'
  432. </div>
  433. <div class="formw">';
  434. if ($_POST AND empty($_POST['category_name']))
  435. {
  436. echo '<span class="form_error">'.get_lang('ThisFieldIsRequired').'. '.get_lang('ErrorPleaseGiveCategoryName').'<span><br />';
  437. }
  438. if ($_POST AND !empty($_POST['category_name']))
  439. {
  440. echo '<span class="form_error">'.get_lang('CategoryAlreadyExistsEditIt').'<span><br />';
  441. }
  442. echo ' <input type="text" name="category_name" value="'.Security::remove_XSS($category_name).'" />
  443. </div>
  444. </div>';
  445. echo ' <div class="row">
  446. <div class="label">
  447. </div>
  448. <div class="formw">
  449. <button class="'.$class.'" type="submit" name="StoreCategory">'.$text.'</button>
  450. </div>
  451. </div>';
  452. echo ' <div class="row">
  453. <div class="label">
  454. </div>
  455. <div class="formw">
  456. <span class="form_required">*</span> <small>'.get_lang('ThisFieldIsRequired').'</small>
  457. </div>
  458. </div>';
  459. echo '</form>';
  460. echo '<div style="clear: both;"></div>';
  461. }
  462. /**
  463. * this function displays the form to upload a new item to the dropbox.
  464. *
  465. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  466. * @version march 2006
  467. */
  468. function display_add_form()
  469. {
  470. global $_user, $is_courseAdmin, $is_courseTutor, $course_info, $origin, $dropbox_unid;
  471. $token = Security::get_token();
  472. $dropbox_person = new Dropbox_Person( $_user['user_id'], $is_courseAdmin, $is_courseTutor);
  473. ?>
  474. <form method="post" action="index.php?view_received_category=<?php echo Security::remove_XSS($_GET['view_received_category']); ?>&view_sent_category=<?php echo Security::remove_XSS($_GET['view_sent_category']); ?>&view=<?php echo Security::remove_XSS($_GET['view']); ?>&<?php echo "origin=$origin"."&".api_get_cidreq(); ?>" enctype="multipart/form-data" onsubmit="return checkForm(this)">
  475. <div class="row"><div class="form_header"><?php echo get_lang('UploadNewFile'); ?></div></div>
  476. <div class="row">
  477. <div class="label">
  478. <span class="form_required">*</span><?php echo dropbox_lang("uploadFile")?>:
  479. </div>
  480. <div class="formw">
  481. <input type="hidden" name="MAX_FILE_SIZE" value='<?php echo dropbox_cnf("maxFilesize")?>' />
  482. <input type="file" name="file" size="20" <?php if (dropbox_cnf("allowOverwrite")) echo 'onChange="checkfile(this.value)"'; ?> />
  483. <input type="hidden" name="dropbox_unid" value="<?php echo $dropbox_unid ?>" />
  484. <input type="hidden" name="sec_token" value="<?php echo $token ?>" />
  485. <?php
  486. if ($origin=='learnpath')
  487. {
  488. echo '<input type="hidden" name="origin" value="learnpath" />';
  489. }
  490. ?>
  491. </div>
  492. </div>
  493. <?php
  494. if (dropbox_cnf("allowOverwrite"))
  495. {
  496. ?>
  497. <div class="row">
  498. <div class="label">
  499. </div>
  500. <div class="formw">
  501. <input type="checkbox" name="cb_overwrite" id="cb_overwrite" value="true" /><?php echo dropbox_lang("overwriteFile")?>
  502. </div>
  503. </div>
  504. <?php
  505. }
  506. ?>
  507. <div class="row">
  508. <div class="label">
  509. <?php echo dropbox_lang("sendTo")?>
  510. </div>
  511. <div class="formw">
  512. <?php
  513. //list of all users in this course and all virtual courses combined with it
  514. if(isset($_SESSION['id_session'])){
  515. $complete_user_list_for_dropbox = array();
  516. if(api_get_setting('dropbox_allow_student_to_student')=='true' || $_user['status'] != STUDENT)
  517. {
  518. $complete_user_list_for_dropbox = CourseManager :: get_user_list_from_course_code($course_info['code'],true,$_SESSION['id_session']);
  519. }
  520. $complete_user_list2 = CourseManager :: get_coach_list_from_course_code($course_info['code'],$_SESSION['id_session']);
  521. $complete_user_list_for_dropbox = array_merge($complete_user_list_for_dropbox,$complete_user_list2);
  522. }
  523. else{
  524. if(api_get_setting('dropbox_allow_student_to_student')=='true' || $_user['status'] != STUDENT)
  525. {
  526. $complete_user_list_for_dropbox = CourseManager :: get_user_list_from_course_code($course_info['code'],true,$_SESSION['id_session']);
  527. }
  528. else
  529. {
  530. $complete_user_list_for_dropbox = CourseManager :: get_teacher_list_from_course_code($course_info['code']);
  531. }
  532. }
  533. foreach ($complete_user_list_for_dropbox as $k => $e) {
  534. $complete_user_list_for_dropbox[$k] = $e + array('lastcommafirst' => api_get_person_name($e['firstname'], $e['lastname']));
  535. }
  536. $complete_user_list_for_dropbox = TableSort::sort_table($complete_user_list_for_dropbox, 'lastcommafirst');
  537. ?>
  538. <select name="recipients[]" size="
  539. <?php
  540. if ( $dropbox_person -> isCourseTutor || $dropbox_person -> isCourseAdmin)
  541. {
  542. echo 10;
  543. }
  544. else
  545. {
  546. echo 6;
  547. }
  548. ?>" multiple style="width: 350px;">
  549. <?php
  550. /*
  551. Create the options inside the select box:
  552. List all selected users their user id as value and a name string as display
  553. */
  554. $current_user_id = '';
  555. foreach ($complete_user_list_for_dropbox as $current_user)
  556. {
  557. if ( ($dropbox_person -> isCourseTutor
  558. || $dropbox_person -> isCourseAdmin
  559. || dropbox_cnf("allowStudentToStudent") // RH: also if option is set
  560. || $current_user['status']!=5 // always allow teachers
  561. || $current_user['tutor_id']==1 // always allow tutors
  562. ) && $current_user['user_id'] != $_user['user_id'] ) // don't include yourself
  563. {
  564. if ($current_user['user_id'] == $current_user_id) continue;
  565. $full_name = $current_user['lastcommafirst'];
  566. $current_user_id = $current_user['user_id'];
  567. echo '<option value="user_' . $current_user_id . '">' . $full_name . '</option>';
  568. }
  569. }
  570. /*
  571. * Show groups
  572. */
  573. if ( ($dropbox_person -> isCourseTutor || $dropbox_person -> isCourseAdmin)
  574. && dropbox_cnf("allowGroup") || dropbox_cnf("allowStudentToStudent"))
  575. {
  576. $complete_group_list_for_dropbox = GroupManager::get_group_list(null,dropbox_cnf("courseId"));
  577. if (count($complete_group_list_for_dropbox) > 0)
  578. {
  579. foreach ($complete_group_list_for_dropbox as $current_group)
  580. {
  581. if ($current_group['number_of_members'] > 0)
  582. {
  583. echo '<option value="group_'.$current_group['id'].'">G: '.$current_group['name'].' - '.$current_group['number_of_members'].' '.get_lang('Users').'</option>';
  584. }
  585. }
  586. }
  587. }
  588. if ( ($dropbox_person -> isCourseTutor || $dropbox_person -> isCourseAdmin) && dropbox_cnf("allowMailing")) // RH: Mailing starting point
  589. {
  590. // echo '<option value="mailing">'.dropbox_lang("mailingInSelect").'</option>';
  591. }
  592. if ( dropbox_cnf("allowJustUpload")) // RH
  593. {
  594. //echo '<option value="upload">'.dropbox_lang("justUploadInSelect").'</option>';
  595. echo '<option value="user_'.$_user['user_id'].'">'.dropbox_lang("justUploadInSelect").'</option>';
  596. }
  597. echo '</select>
  598. </div>
  599. </div>';
  600. echo '
  601. <div class="row">
  602. <div class="label">
  603. </div>
  604. <div class="formw">
  605. <button type="Submit" class="upload" name="submitWork">'.dropbox_lang("upload", "noDLTT").'</button>
  606. </div>
  607. </div>
  608. ';
  609. echo "</form>";
  610. }
  611. /**
  612. * returns username or false if user isn't registered anymore
  613. * @todo check if this function is still necessary. There might be a library function for this.
  614. */
  615. function getUserNameFromId ( $id) // RH: Mailing: return 'Mailing ' + id
  616. {
  617. $mailingId = $id - dropbox_cnf("mailingIdBase");
  618. if ( $mailingId > 0)
  619. {
  620. return dropbox_lang("mailingAsUsername", "noDLTT") . $mailingId;
  621. }
  622. $id = intval($id);
  623. $sql = "SELECT ".(api_is_western_name_order() ? "CONCAT(firstname,' ', lastname)" : "CONCAT(lastname,' ', firstname)")." AS name
  624. FROM " . dropbox_cnf("tbl_user") . "
  625. WHERE user_id='$id'";
  626. $result = Database::query($sql,__FILE__,__LINE__);
  627. $res = Database::fetch_array( $result);
  628. if ( $res == FALSE) return FALSE;
  629. return stripslashes( $res["name"]);
  630. }
  631. /**
  632. * returns loginname or false if user isn't registered anymore
  633. * @todo check if this function is still necessary. There might be a library function for this.
  634. */
  635. function getLoginFromId ( $id)
  636. {
  637. $id = intval($id);
  638. $sql = "SELECT username
  639. FROM " . dropbox_cnf("tbl_user") . "
  640. WHERE user_id='$id'";
  641. $result =Database::query($sql,__FILE__,__LINE__);
  642. $res = Database::fetch_array( $result);
  643. if ( $res == FALSE) return FALSE;
  644. return stripslashes( $res["username"]);
  645. }
  646. /**
  647. * @return boolean indicating if user with user_id=$user_id is a course member
  648. * @todo eliminate global
  649. * @todo check if this function is still necessary. There might be a library function for this.
  650. */
  651. function isCourseMember( $user_id)
  652. {
  653. global $_course;
  654. $course_code = $_course['sysCode'];
  655. $is_course_member = CourseManager::is_user_subscribed_in_course($user_id, $course_code,true);
  656. return $is_course_member;
  657. }
  658. /**
  659. * Checks if there are files in the dropbox_file table that aren't used anymore in dropbox_person table.
  660. * If there are, all entries concerning the file are deleted from the db + the file is deleted from the server
  661. */
  662. function removeUnusedFiles( )
  663. {
  664. // select all files that aren't referenced anymore
  665. $sql = "SELECT DISTINCT f.id, f.filename
  666. FROM " . dropbox_cnf("tbl_file") . " f
  667. LEFT JOIN " . dropbox_cnf("tbl_person") . " p ON f.id = p.file_id
  668. WHERE p.user_id IS NULL";
  669. $result = Database::query($sql,__FILE__,__LINE__);
  670. while ( $res = Database::fetch_array( $result))
  671. {
  672. //delete the selected files from the post and file tables
  673. $sql = "DELETE FROM " . dropbox_cnf("tbl_post") . " WHERE file_id='" . $res['id'] . "'";
  674. $result1 = Database::query($sql,__FILE__,__LINE__);
  675. $sql = "DELETE FROM " . dropbox_cnf("tbl_file") . " WHERE id='" . $res['id'] . "'";
  676. $result1 = Database::query($sql,__FILE__,__LINE__);
  677. //delete file from server
  678. @unlink( dropbox_cnf("sysPath") . "/" . $res["filename"]);
  679. }
  680. }
  681. /**
  682. * RH: Mailing (2 new functions)
  683. *
  684. * Mailing zip-file is posted to (dest_user_id = ) mailing pseudo_id
  685. * and is only visible to its uploader (user_id).
  686. *
  687. * Mailing content files have uploader_id == mailing pseudo_id, a normal recipient,
  688. * and are visible initially to recipient and pseudo_id.
  689. *
  690. * @author René Haentjens, Ghent University
  691. *
  692. * @todo check if this function is still necessary.
  693. */
  694. function getUserOwningThisMailing($mailingPseudoId, $owner = 0, $or_die = '')
  695. {
  696. $mailingPseudoId = intval($mailingPseudoId);
  697. $sql = "SELECT f.uploader_id
  698. FROM " . dropbox_cnf("tbl_file") . " f
  699. LEFT JOIN " . dropbox_cnf("tbl_post") . " p ON f.id = p.file_id
  700. WHERE p.dest_user_id = '" . $mailingPseudoId . "'";
  701. $result = Database::query($sql,__FILE__,__LINE__);
  702. if (!($res = Database::fetch_array($result)))
  703. die(dropbox_lang("generalError")." (code 901)");
  704. if ($owner == 0) return $res['uploader_id'];
  705. if ($res['uploader_id'] == $owner) return TRUE;
  706. die(dropbox_lang("generalError")." (code ".$or_die.")");
  707. }
  708. /**
  709. * @author Ren� Haentjens, Ghent University
  710. * @todo check if this function is still necessary.
  711. */
  712. function removeMoreIfMailing($file_id)
  713. {
  714. // when deleting a mailing zip-file (posted to mailingPseudoId):
  715. // 1. the detail window is no longer reachable, so
  716. // for all content files, delete mailingPseudoId from person-table
  717. // 2. finding the owner (getUserOwningThisMailing) is no longer possible, so
  718. // for all content files, replace mailingPseudoId by owner as uploader
  719. $file_id = intval($file_id);
  720. $sql = "SELECT p.dest_user_id
  721. FROM " . dropbox_cnf("tbl_post") . " p
  722. WHERE p.file_id = '" . $file_id . "'";
  723. $result = Database::query($sql,__FILE__,__LINE__);
  724. if ( $res = Database::fetch_array( $result))
  725. {
  726. $mailingPseudoId = $res['dest_user_id'];
  727. if ( $mailingPseudoId > dropbox_cnf("mailingIdBase"))
  728. {
  729. $sql = "DELETE FROM " . dropbox_cnf("tbl_person") . " WHERE user_id='" . $mailingPseudoId . "'";
  730. $result1 = Database::query($sql,__FILE__,__LINE__);
  731. $sql = "UPDATE " . dropbox_cnf("tbl_file") .
  732. " SET uploader_id='" . api_get_user_id() . "' WHERE uploader_id='" . $mailingPseudoId . "'";
  733. $result1 = Database::query($sql,__FILE__,__LINE__);
  734. }
  735. }
  736. }
  737. /**
  738. * The dropbox has a deviant naming scheme for language files so it needs an additional language function
  739. *
  740. * @todo check if this function is still necessary.
  741. *
  742. * @author René Haentjens, Ghent University
  743. */
  744. function dropbox_lang($variable, $notrans = 'DLTT')
  745. {
  746. global $charset;
  747. return (api_get_setting('server_type') == 'test' ?
  748. get_lang('dropbox_lang["'.$variable.'"]', $notrans) :
  749. api_html_entity_decode(api_to_system_encoding(str_replace("\\'", "'", $GLOBALS['dropbox_lang'][$variable]), null, true), ENT_QUOTES, $charset));
  750. }
  751. /**
  752. * Function that finds a given config setting
  753. *
  754. * @author René Haentjens, Ghent University
  755. */
  756. function dropbox_cnf($variable)
  757. {
  758. return $GLOBALS['dropbox_cnf'][$variable];
  759. }
  760. /**
  761. *
  762. */
  763. function store_add_dropbox()
  764. {
  765. global $dropbox_cnf;
  766. global $_user;
  767. global $_course;
  768. // ----------------------------------------------------------
  769. // Validating the form data
  770. // ----------------------------------------------------------
  771. // the author is
  772. /*
  773. if (!isset( $_POST['authors']))
  774. {
  775. return get_lang('AuthorFieldCannotBeEmpty');
  776. }
  777. */
  778. // there are no recipients selected
  779. if ( !isset( $_POST['recipients']) || count( $_POST['recipients']) <= 0)
  780. {
  781. return get_lang('YouMustSelectAtLeastOneDestinee');
  782. }
  783. // Check if all the recipients are valid
  784. else
  785. {
  786. $thisIsAMailing = FALSE; // RH: Mailing selected as destination
  787. $thisIsJustUpload = FALSE; // RH
  788. foreach( $_POST['recipients'] as $rec)
  789. {
  790. if ( $rec == 'mailing')
  791. {
  792. $thisIsAMailing = TRUE;
  793. }
  794. elseif ( $rec == 'upload')
  795. {
  796. $thisIsJustUpload = TRUE;
  797. }
  798. elseif (strpos($rec, 'user_') === 0 && !isCourseMember(substr($rec, strlen('user_') ) ))
  799. {
  800. return get_lang('InvalideUserDetected');
  801. }
  802. elseif (strpos($rec, 'group_') !== 0 && strpos($rec, 'user_') !== 0)
  803. {
  804. return get_lang('InvalideGroupDetected');
  805. }
  806. }
  807. }
  808. // we are doing a mailing but an additional recipient is selected
  809. if ( $thisIsAMailing && ( count($_POST['recipients']) != 1))
  810. {
  811. return get_lang('MailingSelectNoOther');
  812. }
  813. // we are doing a just upload but an additional recipient is selected.
  814. // note: why can't this be valid? It is like sending a document to yourself AND to a different person (I do this quite often with my e-mails)
  815. if ( $thisIsJustUpload && ( count($_POST['recipients']) != 1))
  816. {
  817. return get_lang('mailingJustUploadSelectNoOther');
  818. }
  819. if ( empty( $_FILES['file']['name']))
  820. {
  821. $error = TRUE;
  822. return get_lang('NoFileSpecified');
  823. }
  824. // ----------------------------------------------------------
  825. // are we overwriting a previous file or sending a new one
  826. // ----------------------------------------------------------
  827. $dropbox_overwrite = false;
  828. if ( isset($_POST['cb_overwrite']) && $_POST['cb_overwrite']==true)
  829. {
  830. $dropbox_overwrite = true;
  831. }
  832. // ----------------------------------------------------------
  833. // doing the upload
  834. // ----------------------------------------------------------
  835. $dropbox_filename = $_FILES['file']['name'];
  836. $dropbox_filesize = $_FILES['file']['size'];
  837. $dropbox_filetype = $_FILES['file']['type'];
  838. $dropbox_filetmpname = $_FILES['file']['tmp_name'];
  839. // check if the filesize does not exceed the allowed size.
  840. if ( $dropbox_filesize <= 0 || $dropbox_filesize > $dropbox_cnf["maxFilesize"])
  841. {
  842. return get_lang('DropboxFileTooBig');
  843. }
  844. // check if the file is actually uploaded
  845. if ( !is_uploaded_file( $dropbox_filetmpname)) // check user fraud : no clean error msg.
  846. {
  847. return get_lang('TheFileIsNotUploaded');
  848. }
  849. // Try to add an extension to the file if it hasn't got one
  850. $dropbox_filename = add_ext_on_mime( $dropbox_filename,$dropbox_filetype);
  851. // Replace dangerous characters
  852. $dropbox_filename = replace_dangerous_char( $dropbox_filename);
  853. // Transform any .php file in .phps fo security
  854. $dropbox_filename = php2phps ( $dropbox_filename);
  855. //filter extension
  856. if(!filter_extension($dropbox_filename))
  857. {
  858. return get_lang('UplUnableToSaveFileFilteredExtension');
  859. }
  860. // set title
  861. $dropbox_title = $dropbox_filename;
  862. // set author
  863. if ( $_POST['authors'] == '')
  864. {
  865. $_POST['authors'] = getUserNameFromId( $_user['user_id']);
  866. }
  867. // note: I think we could better migrate everything from here on to separate functions: store_new_dropbox, store_new_mailing, store_just_upload
  868. if ($dropbox_overwrite) // RH: Mailing: adapted
  869. {
  870. $dropbox_person = new Dropbox_Person( $_user['user_id'], api_is_course_admin(), api_is_course_tutor());
  871. foreach($dropbox_person->sentWork as $w)
  872. {
  873. if ($w->title == $dropbox_filename)
  874. {
  875. if ( ($w->recipients[0]['id'] > dropbox_cnf("mailingIdBase")) xor $thisIsAMailing)
  876. {
  877. return get_lang('MailingNonMailingError');
  878. }
  879. if ( ($w->recipients[0]['id'] == $_user['user_id']) xor $thisIsJustUpload)
  880. {
  881. return get_lang('MailingJustUploadSelectNoOther');
  882. }
  883. $dropbox_filename = $w->filename;
  884. $found = true; // note: do we still need this?
  885. break;
  886. }
  887. }
  888. }
  889. else // rename file to login_filename_uniqueId format
  890. {
  891. $dropbox_filename = getLoginFromId( $_user['user_id']) . "_" . $dropbox_filename . "_".uniqid('');
  892. }
  893. // creating the array that contains all the users who will receive the file
  894. $new_work_recipients = array();
  895. foreach ($_POST["recipients"] as $rec)
  896. {
  897. if (strpos($rec, 'user_') === 0)
  898. {
  899. $new_work_recipients[] = substr($rec, strlen('user_') );
  900. }
  901. elseif (strpos($rec, 'group_') === 0 )
  902. {
  903. $userList = GroupManager::get_subscribed_users(substr($rec, strlen('group_') ));
  904. foreach ($userList as $usr)
  905. {
  906. if (! in_array($usr['user_id'], $new_work_recipients) && $usr['user_id'] != $_user['user_id'])
  907. {
  908. $new_work_recipients[] = $usr['user_id'];
  909. }
  910. }
  911. }
  912. }
  913. @move_uploaded_file( $dropbox_filetmpname, dropbox_cnf("sysPath") . '/' . $dropbox_filename);
  914. $b_send_mail = api_get_course_setting('email_alert_on_new_doc_dropbox');
  915. if($b_send_mail)
  916. {
  917. foreach($new_work_recipients as $recipient_id)
  918. {
  919. include_once(api_get_path(LIBRARY_PATH) . 'usermanager.lib.php');
  920. $recipent_temp=UserManager :: get_user_info_by_id($recipient_id);
  921. api_mail(api_get_person_name($recipent_temp['firstname'].' '.$recipent_temp['lastname'], null, PERSON_NAME_EMAIL_ADDRESS), $recipent_temp['email'],
  922. get_lang('NewDropboxFileUploaded'),
  923. get_lang('NewDropboxFileUploadedContent').' '.api_get_path(WEB_CODE_PATH).'dropbox/index.php?cidReq='.$_course['sysCode']."\n\n".api_get_person_name($_user['firstName'], $_user['lastName'], null, PERSON_NAME_EMAIL_ADDRESS)."\n". get_lang('Email') ." : ".$_user['mail'], api_get_person_name($_user['firstName'], $_user['lastName'], null, PERSON_NAME_EMAIL_ADDRESS), $_user['mail']);
  924. //get_lang('NewDropboxFileUploadedContent').' '.api_get_path(WEB_CODE_PATH).'dropbox/index.php?cidReq='.$_course['sysCode']."\n\n".api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'), null, PERSON_NAME_EMAIL_ADDRESS)."\n". get_lang('Manager'). " ".api_get_setting('siteName')."\n" .get_lang('Email') ." : ".api_get_setting('emailAdministrator'), api_get_person_name(api_get_setting('administratorName')." ".api_get_setting('administratorSurname'), null, PERSON_NAME_EMAIL_ADDRESS), api_get_setting('emailAdministrator'));
  925. }
  926. }
  927. new Dropbox_SentWork( $_user['user_id'], $dropbox_title, $_POST['description'], strip_tags($_POST['authors']), $dropbox_filename, $dropbox_filesize, $new_work_recipients);
  928. Security::clear_token();
  929. return get_lang('FileUploadSucces');
  930. }
  931. /**
  932. * This function displays the firstname and lastname of the user as a link to the user tool.
  933. *
  934. * @see this is the same function as in the new forum, so this probably has to move to a user library.
  935. *
  936. * @todo move this function to the user library
  937. *
  938. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  939. * @version march 2006
  940. */
  941. function display_user_link($user_id, $name='')
  942. {
  943. global $_otherusers;
  944. if ($user_id<>0)
  945. {
  946. if ($name=='')
  947. {
  948. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  949. $sql="SELECT * FROM $table_user WHERE user_id='".Database::escape_string($user_id)."'";
  950. $result=Database::query($sql,__FILE__,__LINE__);
  951. $row=Database::fetch_array($result);
  952. return "<a href=\"../user/userInfo.php?uInfo=".$row['user_id']."\">".api_get_person_name($row['firstname'], $row['lastname'])."</a>";
  953. }
  954. else
  955. {
  956. $user_id = intval($user_id);
  957. return "<a href=\"../user/userInfo.php?uInfo=".$user_id."\">".Security::remove_XSS($name)."</a>";
  958. }
  959. }
  960. else
  961. {
  962. return $name.' ('.get_lang('Anonymous').')';
  963. }
  964. }
  965. /**
  966. * this function transforms the array containing all the feedback into something visually attractive.
  967. *
  968. * @param an array containing all the feedback about the given message.
  969. *
  970. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  971. * @version march 2006
  972. */
  973. function feedback($array)
  974. {
  975. foreach ($array as $key=>$value)
  976. {
  977. $output.=format_feedback($value);
  978. }
  979. $output.=feedback_form();
  980. return $output;
  981. }
  982. /**
  983. * This function returns the html code to display the feedback messages on a given dropbox file
  984. * @param $feedback_array an array that contains all the feedback messages about the given document.
  985. * @return html code
  986. * @todo add the form for adding new comment (if the other party has not deleted it yet).
  987. *
  988. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  989. * @version march 2006
  990. */
  991. function format_feedback($feedback)
  992. {
  993. $output.=display_user_link($feedback['author_user_id']);
  994. $output.='&nbsp;&nbsp;['.$feedback['feedback_date'].']<br />';
  995. $output.='<div style="padding-top:6px">'.nl2br($feedback['feedback']).'</div><hr size="1" noshade/><br />';
  996. return $output;
  997. }
  998. /**
  999. * this function returns the code for the form for adding a new feedback message to a dropbox file.
  1000. * @return html code
  1001. *
  1002. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1003. * @version march 2006
  1004. */
  1005. function feedback_form()
  1006. {
  1007. global $dropbox_cnf;
  1008. $return = get_lang('AddNewFeedback').'<br />';
  1009. // we now check if the other users have not delete this document yet. If this is the case then it is useless to see the
  1010. // add feedback since the other users will never get to see the feedback.
  1011. $sql="SELECT * FROM ".$dropbox_cnf["tbl_person"]." WHERE file_id='".Database::escape_string($_GET['id'])."'";
  1012. $result=Database::query($sql,__LINE__, __FILE__);
  1013. $number_users_who_see_file=Database::num_rows($result);
  1014. if ($number_users_who_see_file>1)
  1015. {
  1016. $return .= '<textarea name="feedback" style="width: 80%; height: 80px;"></textarea><br /><button type="submit" class="add" name="store_feedback" value="'.get_lang('Ok').'"
  1017. onclick="document.form_tablename.attributes.action.value = document.location;">'.get_lang('AddComment').'</button>';
  1018. }
  1019. else
  1020. {
  1021. $return .= get_lang('AllUsersHaveDeletedTheFileAndWillNotSeeFeedback');
  1022. }
  1023. return $return;
  1024. }
  1025. /**
  1026. * @return a language string (depending on the success or failure.
  1027. *
  1028. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1029. * @version march 2006
  1030. */
  1031. function store_feedback()
  1032. {
  1033. global $dropbox_cnf;
  1034. global $_user;
  1035. if (!is_numeric($_GET['id']))
  1036. {
  1037. return get_lang('FeedbackError');
  1038. }
  1039. if ($_POST['feedback']=='')
  1040. {
  1041. return get_lang('PleaseTypeText');
  1042. }
  1043. else
  1044. {
  1045. $sql="INSERT INTO ".$dropbox_cnf['tbl_feedback']." (file_id, author_user_id, feedback, feedback_date) VALUES
  1046. ('".Database::escape_string($_GET['id'])."','".Database::escape_string($_user['user_id'])."','".Database::escape_string($_POST['feedback'])."',NOW())";
  1047. Database::query($sql,__FILE__,__LINE__);
  1048. return get_lang('DropboxFeedbackStored');
  1049. }
  1050. }
  1051. /**
  1052. * This function downloads all the files of the inputarray into one zip
  1053. * @param $array an array containing all the ids of the files that have to be downloaded.
  1054. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1055. * @todo consider removing the check if the user has received or sent this file (zip download of a folder already sufficiently checks for this).
  1056. * @todo integrate some cleanup function that removes zip files that are older than 2 days
  1057. *
  1058. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1059. * @version march 2006
  1060. */
  1061. function zip_download ($array)
  1062. {
  1063. global $_course;
  1064. global $dropbox_cnf;
  1065. global $_user;
  1066. global $files;
  1067. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  1068. // zip library for creation of the zipfile
  1069. include(api_get_path(LIBRARY_PATH)."/pclzip/pclzip.lib.php");
  1070. // place to temporarily stash the zipfiles
  1071. $temp_zip_dir = api_get_path(SYS_COURSE_PATH).$_course['path']."/temp/";
  1072. // create the directory if it does not exist yet.
  1073. if(!is_dir($temp_zip_dir))
  1074. {
  1075. mkdir($temp_zip_dir);
  1076. }
  1077. cleanup_temp_dropbox();
  1078. $files='';
  1079. // note: we also have to add the check if the user has received or sent this file. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  1080. $sql="SELECT distinct file.filename, file.title, file.author, file.description
  1081. FROM ".$dropbox_cnf["tbl_file"]." file, ".$dropbox_cnf["tbl_person"]." person
  1082. WHERE file.id IN (".implode(', ',$array).")
  1083. AND file.id=person.file_id
  1084. AND person.user_id='".$_user['user_id']."'";
  1085. $result=Database::query($sql,__FILE__,__LINE__);
  1086. while ($row=Database::fetch_array($result))
  1087. {
  1088. $files[$row['filename']]=array('filename'=>$row['filename'],'title'=>$row['title'], 'author'=>$row['author'], 'description'=>$row['description']);
  1089. }
  1090. //$alternative is a variable that uses an alternative method to create the zip
  1091. // because the renaming of the files inside the zip causes error on php5 (unexpected end of archive)
  1092. $alternative=true;
  1093. if ($alternative)
  1094. {
  1095. zip_download_alternative($files);
  1096. exit;
  1097. }
  1098. // create the zip file
  1099. $name = 'dropboxdownload-'.$_user['user_id'].'-'.mktime().'.zip';
  1100. $temp_zip_file=$temp_zip_dir.'/'.$name;
  1101. $zip_folder=new PclZip($temp_zip_file);
  1102. foreach ($files as $key=>$value)
  1103. {
  1104. // met hernoemen van de files in de zip
  1105. $zip_folder->add(api_get_path(SYS_COURSE_PATH).$_course['path']."/dropbox/".$value['filename'],PCLZIP_OPT_REMOVE_PATH, api_get_path(SYS_COURSE_PATH).$_course['path']."/dropbox", PCLZIP_CB_PRE_ADD, 'my_pre_add_callback');
  1106. // zonder hernoemen van de files in de zip
  1107. //$zip_folder->add(api_get_path(SYS_COURSE_PATH).$_course['path']."/dropbox/".$value['filename'],PCLZIP_OPT_REMOVE_PATH, api_get_path(SYS_COURSE_PATH).$_course['path']."/dropbox");
  1108. }
  1109. // create the overview file
  1110. $overview_file_content=generate_html_overview($files, array('filename'), array('title'));
  1111. $overview_file=$temp_zip_dir.'/overview.html';
  1112. $handle=fopen($overview_file,'w');
  1113. fwrite($handle,$overview_file_content);
  1114. // send the zip file
  1115. DocumentManager::file_send_for_download($temp_zip_file,true,$name);
  1116. exit;
  1117. }
  1118. /**
  1119. * This is a callback function to decrypt the files in the zip file to their normal filename (as stored in the database)
  1120. * @param $p_event a variable of PCLZip
  1121. * @param $p_header a variable of PCLZip
  1122. *
  1123. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1124. * @version march 2006
  1125. */
  1126. function my_pre_add_callback($p_event, &$p_header)
  1127. {
  1128. global $files;
  1129. $p_header['stored_filename']=$files[$p_header['stored_filename']]['title'];
  1130. return 1;
  1131. }
  1132. /**
  1133. * This function is an alternative zip download. It was added because PCLZip causes problems on PHP5 when using PCLZIP_CB_PRE_ADD and a callback function to rename
  1134. * the files inside the zip file (dropbox scrambles the files to prevent
  1135. * @todo consider using a htaccess that denies direct access to the file but only allows the php file to access it. This would remove the scrambling requirement
  1136. * but it would require additional checks to see if the filename of the uploaded file is not used yet.
  1137. * @param $files is an associative array that contains the files that the user wants to download (check to see if the user is allowed to download these files already
  1138. * happened so the array is clean!!. The key is the filename on the filesystem. The value is an array that contains both the filename on the filesystem and
  1139. * the original filename (that will be used in the zip file)
  1140. * @todo when we copy the files there might be two files with the same name. We need a function that (recursively) checks this and changes the name
  1141. *
  1142. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1143. * @version march 2006
  1144. */
  1145. function zip_download_alternative($files)
  1146. {
  1147. global $_course;
  1148. global $_user;
  1149. $temp_zip_dir = api_get_path(SYS_COURSE_PATH).$_course['path']."/temp/";
  1150. // Step 2: we copy all the original dropbox files to the temp folder and change their name into the original name
  1151. foreach ($files as $key=>$value)
  1152. {
  1153. $value['title']=check_file_name(api_strtolower($value['title']));
  1154. $files[$value['filename']]['title']=$value['title'];
  1155. copy(api_get_path(SYS_COURSE_PATH).$_course['path']."/dropbox/".$value['filename'], api_get_path(SYS_COURSE_PATH).$_course['path']."/temp/".$value['title']);
  1156. }
  1157. // Step 3: create the zip file and add all the files to it
  1158. $temp_zip_file=$temp_zip_dir.'/dropboxdownload-'.$_user['user_id'].'-'.mktime().'.zip';
  1159. $zip_folder=new PclZip($temp_zip_file);
  1160. foreach ($files as $key=>$value)
  1161. {
  1162. $zip_folder->add(api_get_path(SYS_COURSE_PATH).$_course['path']."/temp/".$value['title'],PCLZIP_OPT_REMOVE_PATH, api_get_path(SYS_COURSE_PATH).$_course['path']."/temp");
  1163. }
  1164. // Step 1: create the overview file and add it to the zip
  1165. $overview_file_content=generate_html_overview($files, array('filename'), array('title'));
  1166. $overview_file=$temp_zip_dir.'overview'.replace_dangerous_char(api_is_western_name_order() ? $_user['firstname'].$_user['lastname'] : $_user['lastname'].$_user['firstname'], 'strict').'.html';
  1167. $handle=fopen($overview_file,'w');
  1168. fwrite($handle,$overview_file_content);
  1169. // todo: find a different solution for this because even 2 seconds is no guarantee.
  1170. sleep(2);
  1171. // Step 4: we add the overview file
  1172. $zip_folder->add($overview_file,PCLZIP_OPT_REMOVE_PATH, api_get_path(SYS_COURSE_PATH).$_course['path']."/temp");
  1173. // Step 5: send the file for download;
  1174. DocumentManager::file_send_for_download($temp_zip_file,true);
  1175. // Step 6: remove the files in the temp dir
  1176. foreach ($files as $key=>$value)
  1177. {
  1178. unlink(api_get_path(SYS_COURSE_PATH).$_course['path']."/temp/".$value['title']);
  1179. }
  1180. //unlink($overview_file);
  1181. exit;
  1182. }
  1183. /**
  1184. * @desc This function checks if the real filename of the dropbox files doesn't already exist in the temp folder. If this is the case then
  1185. * it will generate a different filename;
  1186. *
  1187. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1188. * @version march 2006
  1189. */
  1190. function check_file_name($file_name_2_check, $counter=0)
  1191. {
  1192. global $_course;
  1193. $new_file_name=$file_name_2_check;
  1194. if ($counter<>0)
  1195. {
  1196. $new_file_name=$counter.$new_file_name;
  1197. }
  1198. if (!file_exists(api_get_path(SYS_COURSE_PATH).$_course['path']."/temp/".$new_file_name))
  1199. {
  1200. return $new_file_name;
  1201. }
  1202. else
  1203. {
  1204. $counter++;
  1205. $new_file_name=check_file_name($file_name_2_check,$counter);
  1206. return $new_file_name;
  1207. }
  1208. }
  1209. /**
  1210. * @desc Cleans the temp zip files that were created when users download several files or a whole folder at once.
  1211. * T
  1212. * @return true
  1213. * @todo
  1214. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1215. * @version march 2006
  1216. */
  1217. function cleanup_temp_dropbox()
  1218. {
  1219. global $_course;
  1220. $handle=opendir(api_get_path(SYS_COURSE_PATH).$_course['path']."/temp");
  1221. while (false !== ($file = readdir($handle)))
  1222. {
  1223. if ($file<>'.' OR $file<>'..')
  1224. {
  1225. $name=str_replace('.zip', '',$file);
  1226. $name_part=explode('-',$name);
  1227. $timestamp_of_file=$name_part[count($name_part)-1];
  1228. // if it is a dropboxdownloadfile and the file is older than one day then we delete it
  1229. if (strstr($file, 'dropboxdownload') AND $timestamp_of_file<(mktime()-86400))
  1230. {
  1231. unlink(api_get_path(SYS_COURSE_PATH).$_course['path']."/temp/".$file);
  1232. }
  1233. }
  1234. }
  1235. closedir($handle);
  1236. return true;
  1237. }
  1238. /**
  1239. * @desc generates the contents of a html file that gives an overview of all the files in the zip file.
  1240. * This is to know the information of the files that are inside the zip file (who send it, the comment, ...)
  1241. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1242. * @version march 2006
  1243. */
  1244. function generate_html_overview($files, $dont_show_columns=array(), $make_link=array())
  1245. {
  1246. $return="<html>\n<head>\n\t<title>".get_lang('OverviewOfFilesInThisZip')."</title>\n</head>";
  1247. $return.="\n\n<body>\n<table border=\"1px\">";
  1248. $counter=0;
  1249. foreach ($files as $key=>$value)
  1250. {
  1251. // We add the header
  1252. if ($counter==0)
  1253. {
  1254. $columns_array=array_keys($value);
  1255. $return.="\n<tr>";
  1256. foreach ($columns_array AS $columns_array_key=>$columns_array_value)
  1257. {
  1258. if (!in_array($columns_array_value,$dont_show_columns))
  1259. {
  1260. $return.="\n\t<th>".$columns_array_value."</th>";
  1261. }
  1262. $column[]=$columns_array_value;
  1263. }
  1264. $return.="</tr><n";
  1265. }
  1266. $counter++;
  1267. // We add the content
  1268. $return.="\n<tr>";
  1269. foreach ($column AS $column_key=>$column_value)
  1270. {
  1271. if (!in_array($column_value,$dont_show_columns))
  1272. {
  1273. $return.="\n\t<td>";
  1274. if (in_array($column_value, $make_link))
  1275. {
  1276. $return.='<a href="'.$value[$column_value].'">'.$value[$column_value].'</a>';
  1277. }
  1278. else
  1279. {
  1280. $return.=$value[$column_value];
  1281. }
  1282. $return.="</td>";
  1283. }
  1284. }
  1285. $return.="</tr><n";
  1286. }
  1287. $return.="\n</table>\n\n</body>";
  1288. $return.="\n</html>";
  1289. return $return;
  1290. }
  1291. /**
  1292. * @desc This function retrieves the number of feedback messages on every document. This function might become obsolete when
  1293. * the feedback becomes user individual.
  1294. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1295. * @version march 2006
  1296. */
  1297. function get_total_number_feedback($file_id='')
  1298. {
  1299. global $dropbox_cnf;
  1300. $sql="SELECT COUNT(feedback_id) AS total, file_id FROM ".$dropbox_cnf['tbl_feedback']." GROUP BY file_id";
  1301. $result=Database::query($sql, __FILE__, __LINE__);
  1302. while ($row=Database::fetch_array($result))
  1303. {
  1304. $return[$row['file_id']]=$row['total'];
  1305. }
  1306. return $return;
  1307. }
  1308. /**
  1309. * @desc this function checks if the key exists. If this is the case it returns the value, if not it returns 0
  1310. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1311. * @version march 2006
  1312. */
  1313. function check_number_feedback($key, $array)
  1314. {
  1315. if (is_array($array))
  1316. {
  1317. if (key_exists($key,$array))
  1318. {
  1319. return $array[$key];
  1320. }
  1321. else
  1322. {
  1323. return 0;
  1324. }
  1325. }
  1326. else
  1327. {
  1328. return 0;
  1329. }
  1330. }
  1331. /**
  1332. * Get the last access to a given tool of a given user
  1333. * @param $tool string the tool constant
  1334. * @param $course_code the course_id
  1335. * @param $user_id the id of the user
  1336. * @return string last tool access date
  1337. *
  1338. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1339. * @version march 2006
  1340. *
  1341. * @todo consider moving this function to a more appropriate place.
  1342. */
  1343. function get_last_tool_access($tool, $course_code='', $user_id='')
  1344. {
  1345. global $_course, $_user;
  1346. // The default values of the parameters
  1347. if ($course_code=='')
  1348. {
  1349. $course_code=$_course['id'];
  1350. }
  1351. if ($user_id=='')
  1352. {
  1353. $user_id=$_user['user_id'];
  1354. }
  1355. // the table where the last tool access is stored (=track_e_lastaccess)
  1356. $table_last_access=Database::get_statistic_table('track_e_lastaccess');
  1357. $sql="SELECT access_date FROM $table_last_access WHERE access_user_id='".Database::escape_string($user_id)."'
  1358. AND access_cours_code='".Database::escape_string($course_code)."'
  1359. AND access_tool='".Database::escape_string($tool)."'
  1360. ORDER BY access_date DESC
  1361. LIMIT 1";
  1362. $result=Database::query($sql,__FILE__,__LINE__);
  1363. $row=Database::fetch_array($result);
  1364. return $row['access_date'];
  1365. }