dropbox_class.inc.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. <?php
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2006 Dokeos S.A.
  6. Copyright (c) 2006 Ghent University (UGent)
  7. Copyright (c) various contributors
  8. For a full list of contributors, see "credits.txt".
  9. The full license can be read in "license.txt".
  10. This program is free software; you can redistribute it and/or
  11. modify it under the terms of the GNU General Public License
  12. as published by the Free Software Foundation; either version 2
  13. of the License, or (at your option) any later version.
  14. See the GNU General Public License for more details.
  15. Contact address: Dokeos, 44 rue des palais, B-1030 Brussels, Belgium
  16. Mail: info@dokeos.com
  17. ==============================================================================
  18. */
  19. /**
  20. * Dropbox module for Dokeos
  21. * Classes for the dropbox module.
  22. *
  23. * 3 classes are defined:
  24. * - Dropbox_Work:
  25. * . id
  26. * . uploader_id => who sent it // RH: Mailing: or mailing pseudo_id
  27. * . uploaderName
  28. * . filename => name of file stored on the server
  29. * . filesize // RH: Mailing: zero for sent zip
  30. * . title => name of file returned to user. This is the original name of the file
  31. * except when the original name contained spaces. In that case the spaces
  32. * will be replaced by _
  33. * . description
  34. * . author
  35. * . upload_date => date when file was first sent
  36. * . last_upload_date=> date when file was last sent
  37. * . isOldWork => has the work already been uploaded before
  38. *
  39. * . feedback_date => date of most recent feedback // RH: Feedback
  40. * . feedback => feedback text (or HTML?) // RH: Feedback
  41. *
  42. * - Dropbox_SentWork extends Dropbox_Work
  43. * . recipients => array of ["id"]["name"] lists the recipients of the work
  44. * // RH: Mailing: or mailing pseudo_id
  45. * ["feedback_date"]["feedback"] // RH: Feedback
  46. * - Dropbox_Person:
  47. * . userId
  48. * . receivedWork => array of Dropbox_Work objects
  49. * . sentWork => array of Dropbox_SentWork objects
  50. * . isCourseTutor
  51. * . isCourseAdmin
  52. * . _orderBy =>private property used for determining the field by which the works have to be ordered
  53. *
  54. * @version 1.30
  55. * @copyright 2004
  56. * @author Jan Bols <jan@ivpv.UGent.be>
  57. * with contributions by Ren� Haentjens <rene.haentjens@UGent.be> (see RH)
  58. * @package dokeos.dropbox
  59. **/
  60. class Dropbox_Work {
  61. var $id;
  62. var $uploader_id;
  63. var $uploaderName;
  64. var $filename;
  65. var $filesize;
  66. var $title;
  67. var $description;
  68. var $author;
  69. var $upload_date;
  70. var $last_upload_date;
  71. var $isOldWork;
  72. var $feedback_date, $feedback; // RH: Feedback
  73. /**
  74. * Constructor calls private functions to create a new work or retreive an existing work from DB
  75. * depending on the number of parameters
  76. *
  77. * @param unknown_type $arg1
  78. * @param unknown_type $arg2
  79. * @param unknown_type $arg3
  80. * @param unknown_type $arg4
  81. * @param unknown_type $arg5
  82. * @param unknown_type $arg6
  83. * @return Dropbox_Work
  84. */
  85. function Dropbox_Work ($arg1, $arg2=null, $arg3=null, $arg4=null, $arg5=null, $arg6=null) {
  86. if (func_num_args()>1) {
  87. $this->_createNewWork($arg1, $arg2, $arg3, $arg4, $arg5, $arg6);
  88. } else {
  89. $this->_createExistingWork($arg1);
  90. }
  91. }
  92. /**
  93. * private function creating a new work object
  94. *
  95. * @param unknown_type $uploader_id
  96. * @param unknown_type $title
  97. * @param unknown_type $description
  98. * @param unknown_type $author
  99. * @param unknown_type $filename
  100. * @param unknown_type $filesize
  101. *
  102. * @todo $author was originally a field but this has now been replaced by the first and lastname of the uploader (to prevent anonymous uploads)
  103. * As a consequence this parameter can be removed
  104. */
  105. function _createNewWork ($uploader_id, $title, $description, $author, $filename, $filesize) {
  106. global $_user;
  107. // Do some sanity checks
  108. settype($uploader_id, 'integer') or die(dropbox_lang("generalError")." (code 201)"); //set $uploader_id to correct type
  109. //if (! isCourseMember($uploader_id)) die(); //uploader must be coursemember to be able to upload
  110. //-->this check is done when submitting data so it isn't checked here
  111. // Fill in the properties
  112. $this->uploader_id = $uploader_id;
  113. $this->uploaderName = getUserNameFromId($this->uploader_id);
  114. $this->filename = $filename;
  115. $this->filesize = $filesize;
  116. $this->title = $title;
  117. $this->description = $description;
  118. $this->author = $_user['firstName'].' '.$_user['lastName'];
  119. $this->last_upload_date = date("Y-m-d H:i:s",time());
  120. // Check if object exists already. If it does, the old object is used
  121. // with updated information (authors, descriptio, upload_date)
  122. $this->isOldWork = FALSE;
  123. $sql="SELECT id, upload_date
  124. FROM ".dropbox_cnf("tbl_file")."
  125. WHERE filename = '".addslashes($this->filename)."'";
  126. $result = api_sql_query($sql,__FILE__,__LINE__);
  127. $res = mysql_fetch_array($result);
  128. if ($res != FALSE) {
  129. $this->isOldWork = TRUE;
  130. }
  131. // insert or update the dropbox_file table and set the id property
  132. if ($this->isOldWork) {
  133. $this->id = $res["id"];
  134. $this->upload_date = $res["upload_date"];
  135. $sql = "UPDATE ".dropbox_cnf("tbl_file")."
  136. SET filesize = '".addslashes($this->filesize)."'
  137. , title = '".addslashes($this->title)."'
  138. , description = '".addslashes($this->description)."'
  139. , author = '".addslashes($this->author)."'
  140. , last_upload_date = '".addslashes($this->last_upload_date)."'
  141. WHERE id='".addslashes($this->id)."'";
  142. $result = api_sql_query($sql,__FILE__,__LINE__);
  143. } else {
  144. $this->upload_date = $this->last_upload_date;
  145. $sql="INSERT INTO ".dropbox_cnf("tbl_file")."
  146. (uploader_id, filename, filesize, title, description, author, upload_date, last_upload_date, session_id)
  147. VALUES ('".addslashes($this->uploader_id)."'
  148. , '".addslashes($this->filename)."'
  149. , '".addslashes($this->filesize)."'
  150. , '".addslashes($this->title)."'
  151. , '".addslashes($this->description)."'
  152. , '".addslashes($this->author)."'
  153. , '".addslashes($this->upload_date)."'
  154. , '".addslashes($this->last_upload_date)."'
  155. , ".intval($_SESSION['id_session'])."
  156. )";
  157. $result = api_sql_query($sql,__FILE__,__LINE__);
  158. $this->id = mysql_insert_id(); //get automatically inserted id
  159. }
  160. // insert entries into person table
  161. $sql="INSERT INTO ".dropbox_cnf("tbl_person")."
  162. (file_id, user_id)
  163. VALUES ('".addslashes($this->id)."'
  164. , '".addslashes($this->uploader_id)."'
  165. )";
  166. $result = api_sql_query($sql); //if work already exists no error is generated
  167. }
  168. /**
  169. * private function creating existing object by retreiving info from db
  170. *
  171. * @param unknown_type $id
  172. */
  173. function _createExistingWork ($id) {
  174. global $_user; // RH: Feedback
  175. // Do some sanity checks
  176. settype($id, 'integer') or die(dropbox_lang("generalError")." (code 205)"); //set $id to correct type
  177. // get the data from DB
  178. $sql="SELECT uploader_id, filename, filesize, title, description, author, upload_date, last_upload_date, cat_id
  179. FROM ".dropbox_cnf("tbl_file")."
  180. WHERE id='".addslashes($id)."'";
  181. $result = api_sql_query($sql,__FILE__,__LINE__);
  182. $res = mysql_fetch_array($result,MYSQL_ASSOC);
  183. // Check if uploader is still in Dokeos system
  184. $uploader_id = stripslashes($res["uploader_id"]);
  185. $uploaderName = getUserNameFromId($uploader_id);
  186. if ($uploaderName == FALSE) {
  187. //deleted user
  188. $this->uploader_id = -1;
  189. $this->uploaderName = dropbox_lang("anonymous", "noDLTT");
  190. } else {
  191. $this->uploader_id = $uploader_id;
  192. $this->uploaderName = $uploaderName;
  193. }
  194. // Fill in properties
  195. $this->id = $id;
  196. $this->filename = stripslashes($res["filename"]);
  197. $this->filesize = stripslashes($res["filesize"]);
  198. $this->title = stripslashes($res["title"]);
  199. $this->description = stripslashes($res["description"]);
  200. $this->author = stripslashes($res["author"]);
  201. $this->upload_date = stripslashes($res["upload_date"]);
  202. $this->last_upload_date = stripslashes($res["last_upload_date"]);
  203. $this->category = $res['cat_id'];
  204. // Getting the feedback on the work.
  205. if ($_GET['action']=='viewfeedback' AND $this->id==$_GET['id']) {
  206. $feedback2=array();
  207. $sql_feedback = "SELECT * FROM ".dropbox_cnf("tbl_feedback")." WHERE file_id='".$id."' ORDER BY feedback_id ASC";
  208. $result = api_sql_query($sql_feedback, __FILE__, __LINE__);
  209. while ($row_feedback=Database::fetch_array($result)) {
  210. $feedback2[]=$row_feedback;
  211. }
  212. $this->feedback2=$feedback2;
  213. }
  214. /*
  215. // RH: Feedback
  216. $result = api_sql_query("SELECT feedback_date, feedback, cat_id FROM ".
  217. dropbox_cnf("tbl_post")." WHERE dest_user_id='".$_user['user_id'].
  218. "' AND file_id='".$id."'",__FILE__,__LINE__);
  219. if ($res = mysql_fetch_array($result))
  220. {
  221. $this->feedback_date = $res["feedback_date"];
  222. $this->feedback = $res["feedback"];
  223. $this->category = $res['cat_id'];
  224. } // do not fail if there is no recipient = current user...*/
  225. }
  226. }
  227. class Dropbox_SentWork extends Dropbox_Work
  228. {
  229. var $recipients; //array of ["id"]["name"] arrays
  230. /**
  231. * Constructor calls private functions to create a new work or retreive an existing work from DB
  232. * depending on the number of parameters
  233. *
  234. * @param unknown_type $arg1
  235. * @param unknown_type $arg2
  236. * @param unknown_type $arg3
  237. * @param unknown_type $arg4
  238. * @param unknown_type $arg5
  239. * @param unknown_type $arg6
  240. * @param unknown_type $arg7
  241. * @return Dropbox_SentWork
  242. */
  243. function Dropbox_SentWork ($arg1, $arg2=null, $arg3=null, $arg4=null, $arg5=null, $arg6=null, $arg7=null) {
  244. if (func_num_args()>1) {
  245. $this->_createNewSentWork ($arg1, $arg2, $arg3, $arg4, $arg5, $arg6, $arg7);
  246. } else {
  247. $this->_createExistingSentWork ($arg1);
  248. }
  249. }
  250. /**
  251. * private function creating a new SentWork object
  252. *
  253. * @param unknown_type $uploader_id
  254. * @param unknown_type $title
  255. * @param unknown_type $description
  256. * @param unknown_type $author
  257. * @param unknown_type $filename
  258. * @param unknown_type $filesize
  259. * @param unknown_type $recipient_ids
  260. */
  261. function _createNewSentWork ($uploader_id, $title, $description, $author, $filename, $filesize, $recipient_ids) {
  262. // Call constructor of Dropbox_Work object
  263. $this->Dropbox_Work($uploader_id, $title, $description, $author, $filename, $filesize);
  264. // Do sanity checks on recipient_ids array & property fillin
  265. // The sanity check for ex-coursemembers is already done in base constructor
  266. settype($uploader_id, 'integer') or die(dropbox_lang("generalError")." (code 208)"); //set $uploader_id to correct type
  267. $justSubmit = FALSE; // RH: mailing zip-file or just upload
  268. if ( is_int($recipient_ids)) {
  269. $justSubmit = TRUE; $recipient_ids = array($recipient_ids + $this->id);
  270. } elseif ( count($recipient_ids) == 0) { // RH: Just Upload
  271. $justSubmit = TRUE; $recipient_ids = array($uploader_id);
  272. }
  273. if (! is_array($recipient_ids) || count($recipient_ids) == 0) {
  274. die(dropbox_lang("generalError")." (code 209)");
  275. }
  276. foreach ($recipient_ids as $rec) {
  277. if (empty($rec)) die(dropbox_lang("generalError")." (code 210)");
  278. //if (!isCourseMember($rec)) die(); //cannot sent document to someone outside of course
  279. //this check is done when validating submitted data
  280. $this->recipients[] = array("id"=>$rec, "name"=>getUserNameFromId($rec));
  281. }
  282. // insert data in dropbox_post and dropbox_person table for each recipient
  283. foreach ($this->recipients as $rec) {
  284. $sql="INSERT INTO ".dropbox_cnf("tbl_post")."
  285. (file_id, dest_user_id, session_id)
  286. VALUES ('".addslashes($this->id)."', '".addslashes($rec["id"])."', ".intval($_SESSION['id_session']).")";
  287. $result = api_sql_query($sql); //if work already exists no error is generated
  288. //insert entries into person table
  289. $sql="INSERT INTO ".dropbox_cnf("tbl_person")."
  290. (file_id, user_id)
  291. VALUES ('".addslashes($this->id)."'
  292. , '".addslashes($rec["id"])."'
  293. )";
  294. // RH: do not add recipient in person table if mailing zip or just upload
  295. if (!$justSubmit) {
  296. $result = api_sql_query($sql); //if work already exists no error is generated
  297. }
  298. //update item_property (previously last_tooledit) table for each recipient
  299. global $_course, $dropbox_cnf;
  300. if (($ownerid = $this->uploader_id) > $dropbox_cnf["mailingIdBase"]) {
  301. $ownerid = getUserOwningThisMailing($ownerid);
  302. }
  303. if (($recipid = $rec["id"]) > $dropbox_cnf["mailingIdBase"]) {
  304. $recipid = $ownerid; // mailing file recipient = mailing id, not a person
  305. }
  306. api_item_property_update($_course, TOOL_DROPBOX, $this->id, "DropboxFileAdded", $ownerid, NULL, $recipid) ;
  307. }
  308. }
  309. /**
  310. * private function creating existing object by retreiving info from db
  311. *
  312. * @param unknown_type $id
  313. */
  314. function _createExistingSentWork ($id) {
  315. // Call constructor of Dropbox_Work object
  316. $this->Dropbox_Work($id);
  317. // Do sanity check. The sanity check for ex-coursemembers is already done in base constructor
  318. settype($id, 'integer') or die(dropbox_lang("generalError")." (code 211)"); //set $id to correct type
  319. //Fill in recipients array/
  320. $this->recipients = array(); // RH: Feedback: added to SELECT
  321. $sql="SELECT dest_user_id, feedback_date, feedback
  322. FROM ".dropbox_cnf("tbl_post")."
  323. WHERE file_id='".addslashes($id)."'";
  324. $result = api_sql_query($sql,__FILE__,__LINE__);
  325. while ($res = Database::fetch_array($result)) {
  326. // check for deleted users
  327. $dest_user_id = $res["dest_user_id"];
  328. $recipientName = getUserNameFromId($dest_user_id);
  329. //$this->category=$res['cat_id'];
  330. if ($recipientName == FALSE) {
  331. $this->recipients[] = array("id"=>-1, "name"=> dropbox_lang("anonymous", "noDLTT"));
  332. } else {
  333. $this->recipients[] = array("id"=>$dest_user_id, "name"=>$recipientName, "user_id"=>$dest_user_id,
  334. "feedback_date"=>$res["feedback_date"], "feedback"=>$res["feedback"]); // RH: Feedback
  335. }
  336. }
  337. }
  338. }
  339. class Dropbox_Person
  340. {
  341. var $receivedWork; //array of Dropbox_Work objects
  342. var $sentWork; //array of Dropbox_SentWork objects
  343. var $userId = 0;
  344. var $isCourseAdmin = FALSE;
  345. var $isCourseTutor = FALSE;
  346. var $_orderBy = ''; //private property that determines by which field
  347. //the receivedWork and the sentWork arrays are sorted
  348. /**
  349. * Constructor for recreating the Dropbox_Person object
  350. *
  351. * @param unknown_type $userId
  352. * @param unknown_type $isCourseAdmin
  353. * @param unknown_type $isCourseTutor
  354. * @return Dropbox_Person
  355. */
  356. function Dropbox_Person ($userId, $isCourseAdmin, $isCourseTutor) {
  357. // Fill in properties
  358. $this->userId = $userId;
  359. $this->isCourseAdmin = $isCourseAdmin;
  360. $this->isCourseTutor = $isCourseTutor;
  361. $this->receivedWork = array();
  362. $this->sentWork = array();
  363. //Note: perhaps include an ex coursemember check to delete old files
  364. $post_tbl = Database::get_course_table(TABLE_DROPBOX_POST);
  365. $person_tbl = Database::get_course_table(TABLE_DROPBOX_PERSON);
  366. $file_tbl = Database::get_course_table(TABLE_DROPBOX_FILE);
  367. // find all entries where this person is the recipient
  368. $sql = "SELECT r.file_id, r.cat_id
  369. FROM $post_tbl r, $person_tbl p
  370. WHERE r.dest_user_id = '".addslashes($this->userId)."'
  371. AND r.dest_user_id = p.user_id
  372. AND r.file_id = p.file_id";
  373. if (intval($_SESSION['id_session']>0)) {
  374. $sql .= " AND r.session_id = ".intval($_SESSION['id_session']);
  375. }
  376. $result = api_sql_query($sql,__FILE__,__LINE__);
  377. while ($res = Database::fetch_array($result)) {
  378. $temp = new Dropbox_Work($res["file_id"]);
  379. $temp -> category = $res['cat_id'];
  380. $this->receivedWork[] = $temp;
  381. }
  382. // find all entries where this person is the sender/uploader
  383. $sql = "SELECT f.id
  384. FROM $file_tbl f, $person_tbl p
  385. WHERE f.uploader_id = '".addslashes($this->userId)."'
  386. AND f.uploader_id = p.user_id
  387. AND f.id = p.file_id";
  388. if(intval($_SESSION['id_session']>0)) {
  389. $sql .= " AND f.session_id = ".intval($_SESSION['id_session']);
  390. }
  391. $result =api_sql_query($sql,__FILE__,__LINE__);
  392. while ($res = Database::fetch_array($result)) {
  393. $this->sentWork[] = new Dropbox_SentWork($res["id"]);
  394. }
  395. }
  396. /**
  397. * This private method is used by the usort function in the
  398. * orderSentWork and orderReceivedWork methods.
  399. * It compares 2 work-objects by 1 of the properties of that object, dictated by the
  400. * private property _orderBy
  401. *
  402. * @param unknown_type $a
  403. * @param unknown_type $b
  404. * @return -1, 0 or 1 dependent of the result of the comparison.
  405. */
  406. function _cmpWork ($a, $b) {
  407. $sort = $this->_orderBy;
  408. $aval = $a->$sort;
  409. $bval = $b->$sort;
  410. if ($sort == 'recipients') { //the recipients property is an array so we do the comparison based on the first item of the recipients array
  411. $aval = $aval[0]['name'];
  412. $bval = $bval[0]['name'];
  413. }
  414. if ($sort == 'filesize') { //filesize is not a string, so we use other comparison technique
  415. return $aval<$bval ? -1 : 1;
  416. } else {
  417. return strcasecmp($aval, $bval);
  418. }
  419. }
  420. /**
  421. * method that sorts the objects in the sentWork array, dependent on the $sort parameter.
  422. * $sort can be lastDate, firstDate, title, size, ...
  423. *
  424. * @param unknown_type $sort
  425. */
  426. function orderSentWork($sort) {
  427. /*
  428. */
  429. switch($sort) {
  430. case 'lastDate':
  431. $this->_orderBy = 'last_upload_date';
  432. break;
  433. case 'firstDate':
  434. $this->_orderBy = 'upload_date';
  435. break;
  436. case 'title':
  437. $this->_orderBy = 'title';
  438. break;
  439. case 'size':
  440. $this->_orderBy = 'filesize';
  441. break;
  442. case 'author':
  443. $this->_orderBy = 'author';
  444. break;
  445. case 'recipient':
  446. $this->_orderBy = 'recipients';
  447. break;
  448. default:
  449. $this->_orderBy = 'last_upload_date';
  450. } // switch
  451. usort($this->sentWork, array($this,"_cmpWork")); //this calls the _cmpWork method
  452. }
  453. /**
  454. * method that sorts the objects in the receivedWork array, dependent on the $sort parameter.
  455. * $sort can be lastDate, firstDate, title, size, ...
  456. * @param unknown_type $sort
  457. */
  458. function orderReceivedWork($sort) {
  459. switch($sort) {
  460. case 'lastDate':
  461. $this->_orderBy = 'last_upload_date';
  462. break;
  463. case 'firstDate':
  464. $this->_orderBy = 'upload_date';
  465. break;
  466. case 'title':
  467. $this->_orderBy = 'title';
  468. break;
  469. case 'size':
  470. $this->_orderBy = 'filesize';
  471. break;
  472. case 'author':
  473. $this->_orderBy = 'author';
  474. break;
  475. case 'sender':
  476. $this->_orderBy = 'uploaderName';
  477. break;
  478. default:
  479. $this->_orderBy = 'last_upload_date';
  480. } // switch
  481. usort($this->receivedWork, array($this,"_cmpWork")); //this calls the _cmpWork method
  482. }
  483. /**
  484. * Deletes all the received work of this person
  485. *
  486. */
  487. function deleteAllReceivedWork () {
  488. //delete entries in person table concerning received works
  489. foreach ($this->receivedWork as $w) {
  490. api_sql_query("DELETE FROM ".dropbox_cnf("tbl_person")." WHERE user_id='".$this->userId."' AND file_id='".$w->id."'",__FILE__,__LINE__);
  491. }
  492. removeUnusedFiles(); //check for unused files
  493. }
  494. /**
  495. * Deletes all the received categories and work of this person
  496. */
  497. function deleteReceivedWorkFolder($id) {
  498. $sql = "DELETE FROM ".dropbox_cnf("tbl_file")." where cat_id = '".$id."' ";
  499. if(!api_sql_query($sql)) return false;
  500. $sql = "DELETE FROM ".dropbox_cnf("tbl_category")." where cat_id = '".$id."' ";
  501. if(!api_sql_query($sql)) return false;
  502. $sql = "DELETE FROM ".dropbox_cnf("tbl_post")." where cat_id = '".$id."' ";
  503. if(!api_sql_query($sql)) return false;
  504. return true;
  505. }
  506. /**
  507. * Deletes a received dropbox file of this person with id=$id
  508. *
  509. * @param integer $id
  510. */
  511. function deleteReceivedWork ($id) {
  512. //id check
  513. $found = false;
  514. foreach($this->receivedWork as $w) {
  515. if ($w->id == $id) {
  516. $found = true;
  517. break;
  518. }
  519. }
  520. if (! $found) {
  521. if(!$this->deleteReceivedWorkFolder($id)) {
  522. die(dropbox_lang("generalError")." (code 216)");
  523. }
  524. }
  525. //delete entries in person table concerning received works
  526. api_sql_query("DELETE FROM ".dropbox_cnf("tbl_person")." WHERE user_id='".$this->userId."' AND file_id='".$id."'",__FILE__,__LINE__);
  527. removeUnusedFiles(); //check for unused files
  528. }
  529. /**
  530. * Deletes all the sent dropbox files of this person
  531. */
  532. function deleteAllSentWork () {
  533. //delete entries in person table concerning sent works
  534. foreach ($this->sentWork as $w) {
  535. api_sql_query("DELETE FROM ".dropbox_cnf("tbl_person")." WHERE user_id='".$this->userId."' AND file_id='".$w->id."'",__FILE__,__LINE__);
  536. removeMoreIfMailing($w->id); // RH: Mailing: see init1
  537. }
  538. removeUnusedFiles(); //check for unused files
  539. }
  540. /**
  541. * Deletes a sent dropbox file of this person with id=$id
  542. *
  543. * @param unknown_type $id
  544. */
  545. function deleteSentWork ($id) {
  546. //index check
  547. $found = false;
  548. foreach($this->sentWork as $w) {
  549. if ($w->id == $id) {
  550. $found = true;
  551. break;
  552. }
  553. }
  554. if (!$found) {
  555. if(!$this->deleteReceivedWorkFolder($id)) {
  556. die(dropbox_lang("generalError")." (code 219)");
  557. }
  558. }
  559. //$file_id = $this->sentWork[$index]->id; // RH: Mailing
  560. //delete entries in person table concerning sent works
  561. api_sql_query("DELETE FROM ".dropbox_cnf("tbl_person")." WHERE user_id='".$this->userId."' AND file_id='".$id."'",__FILE__,__LINE__);
  562. removeMoreIfMailing($id); // RH: Mailing: see init1
  563. removeUnusedFiles(); //check for unused files
  564. }
  565. /**
  566. * Updates feedback for received work of this person with id=$id
  567. *
  568. * @param unknown_type $id
  569. * @param unknown_type $text
  570. */
  571. function updateFeedback($id, $text) {
  572. global $_course, $dropbox_cnf;
  573. //id check
  574. $found = false; $wi = -1;
  575. foreach($this->receivedWork as $w) {
  576. $wi++;
  577. if ($w->id == $id){
  578. $found = true;
  579. break;
  580. } // foreach (... as $wi -> $w) gives error 221! (no idea why...)
  581. }
  582. if (! $found) {
  583. die(dropbox_lang("generalError")." (code 221)");
  584. }
  585. $feedback_date = date("Y-m-d H:i:s",time());
  586. $this->receivedWork[$wi]->feedback_date = $feedback_date;
  587. $this->receivedWork[$wi]->feedback = $text;
  588. api_sql_query("UPDATE ".dropbox_cnf("tbl_post")." SET feedback_date='".
  589. addslashes($feedback_date)."', feedback='".addslashes($text).
  590. "' WHERE dest_user_id='".$this->userId."' AND file_id='".$id."'",__FILE__,__LINE__);
  591. //update item_property (previously last_tooledit) table
  592. if (($ownerid = $this->receivedWork[$wi]->uploader_id) > $dropbox_cnf["mailingIdBase"]) {
  593. $ownerid = getUserOwningThisMailing($ownerid);
  594. }
  595. api_item_property_update($_course, TOOL_DROPBOX, $this->receivedWork[$wi]->id, "DropboxFileUpdated", $this->userId, NULL, $ownerid) ;
  596. }
  597. /**
  598. * Filter the received work
  599. * @param string $type
  600. * @param string $value
  601. */
  602. function filter_received_work($type,$value) {
  603. global $dropbox_cnf;
  604. $new_received_work = array();
  605. foreach ($this->receivedWork as $index => $work) {
  606. switch ($type) {
  607. case 'uploader_id':
  608. if ($work->uploader_id == $value ||
  609. ($work->uploader_id > $dropbox_cnf["mailingIdBase"] &&
  610. getUserOwningThisMailing($work->uploader_id) == $value)) {
  611. $new_received_work[] = $work;
  612. }
  613. break;
  614. default:
  615. $new_received_work[] = $work;
  616. }
  617. }
  618. $this->receivedWork = $new_received_work;
  619. }
  620. }