dropbox_class.inc.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Dropbox module for Chamilo
  5. * Classes for the dropbox module.
  6. *
  7. * 3 classes have been defined:
  8. * - Dropbox_Work:
  9. * . id
  10. * . uploader_id => who sent it
  11. * . uploaderName
  12. * . filename => name of file stored on the server
  13. * . filesize
  14. * . title => name of file returned to user. This is the original name of the file
  15. * except when the original name contained spaces. In that case the spaces
  16. * will be replaced by _
  17. * . description
  18. * . author
  19. * . upload_date => date when file was first sent
  20. * . last_upload_date => date when file was last sent
  21. * . isOldWork => has the work already been uploaded before
  22. *
  23. * . feedback_date => date of most recent feedback
  24. * . feedback => feedback text (or HTML?)
  25. *
  26. * - Dropbox_SentWork extends Dropbox_Work
  27. * . recipients => array of ["id"]["name"] lists the recipients of the work
  28. *
  29. * - Dropbox_Person:
  30. * . userId
  31. * . receivedWork => array of Dropbox_Work objects
  32. * . sentWork => array of Dropbox_SentWork objects
  33. * . isCourseTutor
  34. * . isCourseAdmin
  35. * . _orderBy => private property used for determining the field by which the works have to be ordered
  36. *
  37. * @version 1.30
  38. * @copyright 2004
  39. * @author Jan Bols <jan@ivpv.UGent.be>
  40. * with contributions by René Haentjens <rene.haentjens@UGent.be>
  41. * @package chamilo.dropbox
  42. */
  43. class Dropbox_Work
  44. {
  45. public $id;
  46. public $uploader_id;
  47. public $uploaderName;
  48. public $filename;
  49. public $filesize;
  50. public $title;
  51. public $description;
  52. public $author;
  53. public $upload_date;
  54. public $last_upload_date;
  55. public $isOldWork;
  56. public $feedback_date, $feedback;
  57. /**
  58. * Constructor calls private functions to create a new work or retreive an existing work from DB
  59. * depending on the number of parameters
  60. *
  61. * @param unknown_type $arg1
  62. * @param unknown_type $arg2
  63. * @param unknown_type $arg3
  64. * @param unknown_type $arg4
  65. * @param unknown_type $arg5
  66. * @param unknown_type $arg6
  67. * @return Dropbox_Work
  68. */
  69. public function __construct($arg1, $arg2 = null, $arg3 = null, $arg4 = null, $arg5 = null, $arg6 = null)
  70. {
  71. if (func_num_args() > 1) {
  72. $this->_createNewWork($arg1, $arg2, $arg3, $arg4, $arg5, $arg6);
  73. } else {
  74. $this->_createExistingWork($arg1);
  75. }
  76. }
  77. /**
  78. * private function creating a new work object
  79. *
  80. * @param int $uploader_id
  81. * @param string $title
  82. * @param string $description
  83. * @param string $author
  84. * @param string $filename
  85. * @param int $filesize
  86. *
  87. * @todo $author was originally a field but this has now been replaced by the first and lastname of the uploader (to prevent anonymous uploads)
  88. * As a consequence this parameter can be removed
  89. */
  90. public function _createNewWork($uploader_id, $title, $description, $author, $filename, $filesize)
  91. {
  92. $dropbox_cnf = getDropboxConf();
  93. // Fill in the properties
  94. $this->uploader_id = intval($uploader_id);
  95. $this->uploaderName = getUserNameFromId($this->uploader_id);
  96. $this->filename = $filename;
  97. $this->filesize = $filesize;
  98. $this->title = $title;
  99. $this->description = $description;
  100. $this->author = $author;
  101. $this->last_upload_date = api_get_utc_datetime();
  102. $course_id = api_get_course_int_id();
  103. // Check if object exists already. If it does, the old object is used
  104. // with updated information (authors, description, upload_date)
  105. $this->isOldWork = false;
  106. $sql = "SELECT id, upload_date FROM ".$dropbox_cnf['tbl_file']."
  107. WHERE c_id = $course_id AND filename = '".Database::escape_string($this->filename)."'";
  108. $result = Database::query($sql);
  109. $res = Database::fetch_array($result);
  110. if ($res) {
  111. $this->isOldWork = true;
  112. }
  113. // Insert or update the dropbox_file table and set the id property
  114. if ($this->isOldWork) {
  115. $this->id = $res['id'];
  116. $this->upload_date = $res['upload_date'];
  117. $params = [
  118. 'filesize' => $this->filesize,
  119. 'title' => $this->title,
  120. 'description' => $this->description,
  121. 'author' => $this->author,
  122. 'last_upload_date' => $this->last_upload_date,
  123. 'session_id' => api_get_session_id(),
  124. ];
  125. Database::update(
  126. $dropbox_cnf['tbl_file'],
  127. $params,
  128. ['c_id = ? AND id = ?' => [$course_id, $this->id]]
  129. );
  130. } else {
  131. $this->upload_date = $this->last_upload_date;
  132. $params = [
  133. 'c_id' => $course_id,
  134. 'uploader_id' => $this->uploader_id,
  135. 'filename' => $this->filename,
  136. 'filesize' => $this->filesize,
  137. 'title' => $this->title,
  138. 'description' => $this->description,
  139. 'author' => $this->author,
  140. 'upload_date' => $this->upload_date,
  141. 'last_upload_date' => $this->last_upload_date,
  142. 'session_id' => api_get_session_id(),
  143. 'cat_id' => 0
  144. ];
  145. $this->id = Database::insert($dropbox_cnf['tbl_file'], $params);
  146. if ($this->id) {
  147. $sql = "UPDATE ".$dropbox_cnf['tbl_file']." SET id = iid WHERE iid = {$this->id}";
  148. Database::query($sql);
  149. }
  150. }
  151. $sql = "SELECT count(file_id) as count
  152. FROM ".$dropbox_cnf['tbl_person']."
  153. WHERE c_id = $course_id AND file_id = ".intval($this->id)." AND user_id = ".$this->uploader_id;
  154. $result = Database::query($sql);
  155. $row = Database::fetch_array($result);
  156. if ($row['count'] == 0) {
  157. // Insert entries into person table
  158. $sql = "INSERT INTO ".$dropbox_cnf['tbl_person']." (c_id, file_id, user_id)
  159. VALUES ($course_id, ".intval($this->id)." , ".intval($this->uploader_id).")";
  160. Database::query($sql);
  161. }
  162. }
  163. /**
  164. * private function creating existing object by retreiving info from db
  165. *
  166. * @param int $id
  167. */
  168. public function _createExistingWork($id)
  169. {
  170. $course_id = api_get_course_int_id();
  171. $dropbox_cnf = getDropboxConf();
  172. $action = isset($_GET['action']) ? $_GET['action'] : null;
  173. // Do some sanity checks
  174. $id = intval($id);
  175. // Get the data from DB
  176. $sql = "SELECT uploader_id, filename, filesize, title, description, author, upload_date, last_upload_date, cat_id
  177. FROM ".$dropbox_cnf['tbl_file']."
  178. WHERE c_id = $course_id AND id = ".$id."";
  179. $result = Database::query($sql);
  180. $res = Database::fetch_array($result, 'ASSOC');
  181. // Check if uploader is still in Chamilo system
  182. $uploader_id = stripslashes($res['uploader_id']);
  183. $uploaderName = getUserNameFromId($uploader_id);
  184. if (!$uploaderName) {
  185. //deleted user
  186. $this->uploader_id = -1;
  187. $this->uploaderName = get_lang('Unknown', '');
  188. } else {
  189. $this->uploader_id = $uploader_id;
  190. $this->uploaderName = $uploaderName;
  191. }
  192. // Fill in properties
  193. $this->id = $id;
  194. $this->filename = stripslashes($res['filename']);
  195. $this->filesize = stripslashes($res['filesize']);
  196. $this->title = stripslashes($res['title']);
  197. $this->description = stripslashes($res['description']);
  198. $this->author = stripslashes($res['author']);
  199. $this->upload_date = stripslashes($res['upload_date']);
  200. $this->last_upload_date = stripslashes($res['last_upload_date']);
  201. $this->category = $res['cat_id'];
  202. // Getting the feedback on the work.
  203. if ($action == 'viewfeedback' AND $this->id == $_GET['id']) {
  204. $feedback2 = array();
  205. $sql_feedback = "SELECT * FROM ".$dropbox_cnf['tbl_feedback']."
  206. WHERE c_id = $course_id AND file_id='".$id."' ORDER BY feedback_id ASC";
  207. $result = Database::query($sql_feedback);
  208. while ($row_feedback = Database::fetch_array($result)) {
  209. $row_feedback['feedback'] = Security::remove_XSS($row_feedback['feedback']);
  210. $feedback2[] = $row_feedback;
  211. }
  212. $this->feedback2= $feedback2;
  213. }
  214. }
  215. }
  216. class Dropbox_SentWork extends Dropbox_Work
  217. {
  218. public $recipients; //array of ['id']['name'] arrays
  219. /**
  220. * Constructor calls private functions to create a new work or retreive an existing work from DB
  221. * depending on the number of parameters
  222. *
  223. * @param unknown_type $arg1
  224. * @param unknown_type $arg2
  225. * @param unknown_type $arg3
  226. * @param unknown_type $arg4
  227. * @param unknown_type $arg5
  228. * @param unknown_type $arg6
  229. * @param unknown_type $arg7
  230. * @return Dropbox_SentWork
  231. */
  232. public function __construct($arg1, $arg2 = null, $arg3 = null, $arg4 = null, $arg5 = null, $arg6 = null, $arg7 = null)
  233. {
  234. if (func_num_args() > 1) {
  235. $this->_createNewSentWork($arg1, $arg2, $arg3, $arg4, $arg5, $arg6, $arg7);
  236. } else {
  237. $this->_createExistingSentWork($arg1);
  238. }
  239. }
  240. /**
  241. * private function creating a new SentWork object
  242. *
  243. * @param int $uploader_id
  244. * @param string $title
  245. * @param string $description
  246. * @param string $author
  247. * @param string $filename
  248. * @param int $filesize
  249. * @param array $recipient_ids
  250. */
  251. public function _createNewSentWork($uploader_id, $title, $description, $author, $filename, $filesize, $recipient_ids)
  252. {
  253. $dropbox_cnf = getDropboxConf();
  254. $_course = api_get_course_info();
  255. // Call constructor of Dropbox_Work object
  256. parent::__construct(
  257. $uploader_id,
  258. $title,
  259. $description,
  260. $author,
  261. $filename,
  262. $filesize
  263. );
  264. $course_id = api_get_course_int_id();
  265. // Do sanity checks on recipient_ids array & property fillin
  266. // The sanity check for ex-coursemembers is already done in base constructor
  267. settype($uploader_id, 'integer') or die(get_lang('GeneralError').' (code 208)'); // Set $uploader_id to correct type
  268. $justSubmit = false;
  269. if ( is_int($recipient_ids)) {
  270. $justSubmit = true;
  271. $recipient_ids = array($recipient_ids + $this->id);
  272. } elseif ( count($recipient_ids) == 0) {
  273. $justSubmit = true;
  274. $recipient_ids = array($uploader_id);
  275. }
  276. if (! is_array($recipient_ids) || count($recipient_ids) == 0) {
  277. die(get_lang('GeneralError').' (code 209)');
  278. }
  279. foreach ($recipient_ids as $rec) {
  280. if (empty($rec)) die(get_lang('GeneralError').' (code 210)');
  281. //if (!isCourseMember($rec)) die(); //cannot sent document to someone outside of course
  282. //this check is done when validating submitted data
  283. $this->recipients[] = array('id' => $rec, 'name' => getUserNameFromId($rec));
  284. }
  285. $table_post = $dropbox_cnf['tbl_post'];
  286. $table_person = $dropbox_cnf['tbl_person'];
  287. $session_id = api_get_session_id();
  288. $uploader_id = $this->uploader_id;
  289. $user = api_get_user_id();
  290. $now = api_get_utc_datetime();
  291. // Insert data in dropbox_post and dropbox_person table for each recipient
  292. foreach ($this->recipients as $rec) {
  293. $file_id = (int)$this->id;
  294. $user_id = (int)$rec['id'];
  295. $sql = "INSERT INTO $table_post (c_id, file_id, dest_user_id, session_id, feedback_date, cat_id)
  296. VALUES ($course_id, $file_id, $user_id, $session_id, '$now', 0)";
  297. Database::query($sql);
  298. // If work already exists no error is generated
  299. /**
  300. * Poster is already added when work is created - not so good to split logic
  301. */
  302. if ($user_id != $user) {
  303. // Insert entries into person table
  304. $sql = "INSERT INTO $table_person (c_id, file_id, user_id)
  305. VALUES ($course_id, $file_id, $user_id)";
  306. // Do not add recipient in person table if mailing zip or just upload.
  307. if (!$justSubmit) {
  308. Database::query($sql); // If work already exists no error is generated
  309. }
  310. }
  311. // Update item_property table for each recipient
  312. if (($ownerid = $this->uploader_id) > $dropbox_cnf['mailingIdBase']) {
  313. $ownerid = getUserOwningThisMailing($ownerid);
  314. }
  315. if (($recipid = $rec["id"]) > $dropbox_cnf['mailingIdBase']) {
  316. $recipid = $ownerid; // mailing file recipient = mailing id, not a person
  317. }
  318. api_item_property_update(
  319. $_course,
  320. TOOL_DROPBOX,
  321. $this->id,
  322. 'DropboxFileAdded',
  323. $ownerid,
  324. null,
  325. $recipid
  326. );
  327. }
  328. }
  329. /**
  330. * private function creating existing object by retreiving info from db
  331. *
  332. * @param unknown_type $id
  333. */
  334. public function _createExistingSentWork($id)
  335. {
  336. $dropbox_cnf = getDropboxConf();
  337. $id = intval($id);
  338. $course_id = api_get_course_int_id();
  339. // Call constructor of Dropbox_Work object
  340. parent::__construct($id);
  341. // Fill in recipients array
  342. $this->recipients = array();
  343. $sql = "SELECT dest_user_id, feedback_date, feedback
  344. FROM ".$dropbox_cnf['tbl_post']."
  345. WHERE c_id = $course_id AND file_id = ".intval($id)."";
  346. $result = Database::query($sql);
  347. while ($res = Database::fetch_array($result, 'ASSOC')) {
  348. // Check for deleted users
  349. $dest_user_id = $res['dest_user_id'];
  350. $user_info = api_get_user_info($dest_user_id);
  351. //$this->category = $res['cat_id'];
  352. if (!$user_info) {
  353. $this->recipients[] = array('id' => -1, 'name' => get_lang('Unknown', ''));
  354. } else {
  355. $this->recipients[] = array(
  356. 'id' => $dest_user_id,
  357. 'name' => $user_info['complete_name'],
  358. 'user_id' => $dest_user_id,
  359. 'feedback_date' => $res['feedback_date'],
  360. 'feedback' => $res['feedback']
  361. );
  362. }
  363. }
  364. }
  365. }
  366. class Dropbox_Person
  367. {
  368. // The receivedWork and the sentWork arrays are sorted.
  369. public $receivedWork; // an array of Dropbox_Work objects
  370. public $sentWork; // an array of Dropbox_SentWork objects
  371. public $userId = 0;
  372. public $isCourseAdmin = false;
  373. public $isCourseTutor = false;
  374. public $_orderBy = ''; // private property that determines by which field
  375. /**
  376. * Constructor for recreating the Dropbox_Person object
  377. *
  378. * @param int $userId
  379. * @param bool $isCourseAdmin
  380. * @param bool $isCourseTutor
  381. * @return Dropbox_Person
  382. */
  383. public function __construct($userId, $isCourseAdmin, $isCourseTutor)
  384. {
  385. $course_id = api_get_course_int_id();
  386. // Fill in properties
  387. $this->userId = $userId;
  388. $this->isCourseAdmin = $isCourseAdmin;
  389. $this->isCourseTutor = $isCourseTutor;
  390. $this->receivedWork = array();
  391. $this->sentWork = array();
  392. // Note: perhaps include an ex coursemember check to delete old files
  393. $session_id = api_get_session_id();
  394. $condition_session = api_get_session_condition($session_id);
  395. $post_tbl = Database::get_course_table(TABLE_DROPBOX_POST);
  396. $person_tbl = Database::get_course_table(TABLE_DROPBOX_PERSON);
  397. $file_tbl = Database::get_course_table(TABLE_DROPBOX_FILE);
  398. // Find all entries where this person is the recipient
  399. $sql = "SELECT DISTINCT r.file_id, r.cat_id
  400. FROM $post_tbl r
  401. INNER JOIN $person_tbl p
  402. ON (r.file_id = p.file_id AND r.c_id = $course_id AND p.c_id = $course_id )
  403. WHERE
  404. p.user_id = ".intval($this->userId)." AND
  405. r.dest_user_id = ".intval($this->userId)." $condition_session ";
  406. $result = Database::query($sql);
  407. while ($res = Database::fetch_array($result)) {
  408. $temp = new Dropbox_Work($res['file_id']);
  409. $temp->category = $res['cat_id'];
  410. $this->receivedWork[] = $temp;
  411. }
  412. // Find all entries where this person is the sender/uploader
  413. $sql = "SELECT DISTINCT f.id
  414. FROM $file_tbl f
  415. INNER JOIN $person_tbl p
  416. ON (f.id = p.file_id AND f.c_id = $course_id AND p.c_id = $course_id)
  417. WHERE
  418. f.uploader_id = ".intval($this->userId)." AND
  419. p.user_id = ".intval($this->userId)."
  420. $condition_session
  421. ";
  422. $result = Database::query($sql);
  423. while ($res = Database::fetch_array($result)) {
  424. $this->sentWork[] = new Dropbox_SentWork($res['id']);
  425. }
  426. }
  427. /**
  428. * This private method is used by the usort function in the
  429. * orderSentWork and orderReceivedWork methods.
  430. * It compares 2 work-objects by 1 of the properties of that object, dictated by the
  431. * private property _orderBy
  432. *
  433. * @param unknown_type $a
  434. * @param unknown_type $b
  435. * @return -1, 0 or 1 dependent of the result of the comparison.
  436. */
  437. function _cmpWork($a, $b)
  438. {
  439. $sort = $this->_orderBy;
  440. $aval = $a->$sort;
  441. $bval = $b->$sort;
  442. if ($sort == 'recipients') {
  443. // The recipients property is an array so we do the comparison based
  444. // on the first item of the recipients array
  445. $aval = $aval[0]['name'];
  446. $bval = $bval[0]['name'];
  447. }
  448. if ($sort == 'filesize') { // Filesize is not a string, so we use other comparison technique
  449. return $aval < $bval ? -1 : 1;
  450. } elseif ($sort == 'title') { // Natural order for sorting titles is more "human-friendly"
  451. return api_strnatcmp($aval, $bval);
  452. } else {
  453. return api_strcasecmp($aval, $bval);
  454. }
  455. }
  456. /**
  457. * A method that sorts the objects in the sentWork array, dependent on the $sort parameter.
  458. * $sort can be lastDate, firstDate, title, size, ...
  459. *
  460. * @param unknown_type $sort
  461. */
  462. function orderSentWork($sort)
  463. {
  464. switch($sort) {
  465. case 'lastDate':
  466. $this->_orderBy = 'last_upload_date';
  467. break;
  468. case 'firstDate':
  469. $this->_orderBy = 'upload_date';
  470. break;
  471. case 'title':
  472. $this->_orderBy = 'title';
  473. break;
  474. case 'size':
  475. $this->_orderBy = 'filesize';
  476. break;
  477. case 'author':
  478. $this->_orderBy = 'author';
  479. break;
  480. case 'recipient':
  481. $this->_orderBy = 'recipients';
  482. break;
  483. default:
  484. $this->_orderBy = 'last_upload_date';
  485. }
  486. usort($this->sentWork, array($this, '_cmpWork'));
  487. }
  488. /**
  489. * method that sorts the objects in the receivedWork array, dependent on the $sort parameter.
  490. * $sort can be lastDate, firstDate, title, size, ...
  491. * @param unknown_type $sort
  492. */
  493. function orderReceivedWork($sort)
  494. {
  495. switch($sort) {
  496. case 'lastDate':
  497. $this->_orderBy = 'last_upload_date';
  498. break;
  499. case 'firstDate':
  500. $this->_orderBy = 'upload_date';
  501. break;
  502. case 'title':
  503. $this->_orderBy = 'title';
  504. break;
  505. case 'size':
  506. $this->_orderBy = 'filesize';
  507. break;
  508. case 'author':
  509. $this->_orderBy = 'author';
  510. break;
  511. case 'sender':
  512. $this->_orderBy = 'uploaderName';
  513. break;
  514. default:
  515. $this->_orderBy = 'last_upload_date';
  516. }
  517. usort($this->receivedWork, array($this, '_cmpWork'));
  518. }
  519. /**
  520. * Deletes all the received work of this person
  521. */
  522. function deleteAllReceivedWork()
  523. {
  524. $course_id = api_get_course_int_id();
  525. $dropbox_cnf = getDropboxConf();
  526. // Delete entries in person table concerning received works
  527. foreach ($this->receivedWork as $w) {
  528. $sql = "DELETE FROM ".$dropbox_cnf['tbl_person']."
  529. WHERE c_id = $course_id AND user_id='".$this->userId."' AND file_id='".$w->id."'";
  530. Database::query($sql);
  531. }
  532. // Check for unused files
  533. removeUnusedFiles();
  534. }
  535. /**
  536. * Deletes all the received categories and work of this person
  537. * @param integer $id
  538. */
  539. function deleteReceivedWorkFolder($id)
  540. {
  541. $dropbox_cnf = getDropboxConf();
  542. $course_id = api_get_course_int_id();
  543. $id = intval($id);
  544. $sql = "DELETE FROM ".$dropbox_cnf['tbl_file']."
  545. WHERE c_id = $course_id AND cat_id = '".$id."' ";
  546. if (!Database::query($sql)) return false;
  547. $sql = "DELETE FROM ".$dropbox_cnf['tbl_category']."
  548. WHERE c_id = $course_id AND cat_id = '".$id."' ";
  549. if (!Database::query($sql)) return false;
  550. $sql = "DELETE FROM ".$dropbox_cnf['tbl_post']."
  551. WHERE c_id = $course_id AND cat_id = '".$id."' ";
  552. if (!Database::query($sql)) return false;
  553. return true;
  554. }
  555. /**
  556. * Deletes a received dropbox file of this person with id=$id
  557. *
  558. * @param integer $id
  559. */
  560. function deleteReceivedWork($id)
  561. {
  562. $course_id = api_get_course_int_id();
  563. $dropbox_cnf = getDropboxConf();
  564. $id = intval($id);
  565. // index check
  566. $found = false;
  567. foreach ($this->receivedWork as $w) {
  568. if ($w->id == $id) {
  569. $found = true;
  570. break;
  571. }
  572. }
  573. if (!$found) {
  574. if (!$this->deleteReceivedWorkFolder($id)) {
  575. die(get_lang('GeneralError').' (code 216)');
  576. }
  577. }
  578. // Delete entries in person table concerning received works
  579. $sql = "DELETE FROM ".$dropbox_cnf['tbl_person']."
  580. WHERE c_id = $course_id AND user_id = '".$this->userId."' AND file_id ='".$id."'";
  581. Database::query($sql);
  582. removeUnusedFiles(); // Check for unused files
  583. }
  584. /**
  585. * Deletes all the sent dropbox files of this person
  586. */
  587. function deleteAllSentWork()
  588. {
  589. $course_id = api_get_course_int_id();
  590. $dropbox_cnf = getDropboxConf();
  591. //delete entries in person table concerning sent works
  592. foreach ($this->sentWork as $w) {
  593. $sql = "DELETE FROM ".$dropbox_cnf['tbl_person']."
  594. WHERE c_id = $course_id AND user_id='".$this->userId."' AND file_id='".$w->id."'";
  595. Database::query($sql);
  596. removeMoreIfMailing($w->id);
  597. }
  598. removeUnusedFiles(); // Check for unused files
  599. }
  600. /**
  601. * Deletes a sent dropbox file of this person with id=$id
  602. *
  603. * @param unknown_type $id
  604. */
  605. function deleteSentWork($id)
  606. {
  607. $course_id = api_get_course_int_id();
  608. $dropbox_cnf = getDropboxConf();
  609. $id = intval($id);
  610. // index check
  611. $found = false;
  612. foreach ($this->sentWork as $w) {
  613. if ($w->id == $id) {
  614. $found = true;
  615. break;
  616. }
  617. }
  618. if (!$found) {
  619. if (!$this->deleteReceivedWorkFolder($id)) {
  620. die(get_lang('GeneralError').' (code 219)');
  621. }
  622. }
  623. //$file_id = $this->sentWork[$index]->id;
  624. // Delete entries in person table concerning sent works
  625. $sql = "DELETE FROM ".$dropbox_cnf['tbl_person']."
  626. WHERE c_id = $course_id AND user_id='".$this->userId."' AND file_id='".$id."'";
  627. Database::query($sql);
  628. removeMoreIfMailing($id);
  629. removeUnusedFiles(); // Check for unused files
  630. }
  631. /**
  632. * Updates feedback for received work of this person with id=$id
  633. *
  634. * @param string $id
  635. * @param string $text
  636. */
  637. function updateFeedback($id, $text)
  638. {
  639. $course_id = api_get_course_int_id();
  640. $_course = api_get_course_info();
  641. $dropbox_cnf = getDropboxConf();
  642. $id = intval($id);
  643. // index check
  644. $found = false;
  645. $wi = -1;
  646. foreach ($this->receivedWork as $w) {
  647. $wi++;
  648. if ($w->id == $id){
  649. $found = true;
  650. break;
  651. } // foreach (... as $wi -> $w) gives error 221! (no idea why...)
  652. }
  653. if (!$found) {
  654. die(get_lang('GeneralError').' (code 221)');
  655. }
  656. $feedback_date = api_get_utc_datetime();
  657. $this->receivedWork[$wi]->feedback_date = $feedback_date;
  658. $this->receivedWork[$wi]->feedback = $text;
  659. $params = [
  660. 'feedback_date' => $feedback_date,
  661. 'feedback' => $text,
  662. ];
  663. Database::update(
  664. $dropbox_cnf['tbl_post'],
  665. $params,
  666. [
  667. 'c_id = ? AND dest_user_id = ? AND file_id = ?' => [
  668. $course_id,
  669. $this->userId,
  670. $id,
  671. ],
  672. ]
  673. );
  674. // Update item_property table
  675. if (($ownerid = $this->receivedWork[$wi]->uploader_id) > $dropbox_cnf['mailingIdBase']) {
  676. $ownerid = getUserOwningThisMailing($ownerid);
  677. }
  678. api_item_property_update(
  679. $_course,
  680. TOOL_DROPBOX,
  681. $this->receivedWork[$wi]->id,
  682. 'DropboxFileUpdated',
  683. $this->userId,
  684. null,
  685. $ownerid
  686. );
  687. }
  688. /**
  689. * Filter the received work
  690. * @param string $type
  691. * @param string $value
  692. */
  693. function filter_received_work($type, $value)
  694. {
  695. $dropbox_cnf = getDropboxConf();
  696. $new_received_work = array();
  697. foreach ($this->receivedWork as $work) {
  698. switch ($type) {
  699. case 'uploader_id':
  700. if ($work->uploader_id == $value ||
  701. ($work->uploader_id > $dropbox_cnf['mailingIdBase'] &&
  702. getUserOwningThisMailing($work->uploader_id) == $value)
  703. ) {
  704. $new_received_work[] = $work;
  705. }
  706. break;
  707. default:
  708. $new_received_work[] = $work;
  709. }
  710. }
  711. $this->receivedWork = $new_received_work;
  712. }
  713. }