lp_ajax_save_objectives.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This script contains the server part of the xajax interaction process.
  5. * The client part is located in lp_api.php or other api's.
  6. * This is a first attempt at using xajax and AJAX in general,
  7. * so the code might be a bit unsettling.
  8. * @package chamilo.learnpath
  9. * @author Yannick Warnier <ywarnier@beeznest.org>
  10. */
  11. // Flag to allow for anonymous user - needs to be set before global.inc.php.
  12. $use_anonymous = true;
  13. require_once __DIR__.'/../inc/global.inc.php';
  14. /**
  15. * Writes an item's new values into the database and returns the operation result
  16. * @param integer Learnpath ID
  17. * @param integer User ID
  18. * @param integer View ID
  19. * @param integer Item ID
  20. * @param array Objectives array
  21. */
  22. function save_objectives($lp_id, $user_id, $view_id, $item_id, $objectives = array())
  23. {
  24. $debug = 0;
  25. $return = '';
  26. if ($debug > 0) {
  27. error_log('In xajax_save_objectives('.$lp_id.','.$user_id.','.$view_id.','.$item_id.',"'.(count($objectives) > 0 ? count($objectives) : '').'")', 0);
  28. }
  29. $mylp = learnpath::getLpFromSession(api_get_course_id(), $lp_id, $user_id);
  30. $mylpi = & $mylp->items[$item_id];
  31. if (is_array($objectives) && count($objectives) > 0) {
  32. foreach ($objectives as $index => $objective) {
  33. $mylpi->add_objective($index, $objectives[$index]);
  34. }
  35. $mylpi->write_objectives_to_db();
  36. }
  37. return $return;
  38. }
  39. $objectives = array();
  40. if (isset($_REQUEST['objectives'])) {
  41. if (is_array($_REQUEST['objectives'])) {
  42. foreach ($_REQUEST['objectives'] as $idx => $ob) {
  43. $objectives[$idx] = explode(',', substr($ob, 1, -1));
  44. if (!isset($objectives[$idx][4])) {
  45. // Make sure there are 7 elements.
  46. $objectives[$idx][4] = '';
  47. }
  48. }
  49. }
  50. }
  51. echo save_objectives(
  52. $_REQUEST['lid'],
  53. $_REQUEST['uid'],
  54. $_REQUEST['vid'],
  55. $_REQUEST['iid'],
  56. $objectives
  57. );