linker.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * Sloodle choice linker (for Sloodle 0.3).
  4. * Allows an SL object to access a Moodle choice instance.
  5. *
  6. * @package sloodlechoice
  7. * @copyright Copyright (c) 2007-8 Sloodle (various contributors)
  8. * @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
  9. *
  10. * @contributor Peter R. Bloomfield
  11. */
  12. // This script should be called with the following parameters:
  13. // sloodlecontrollerid = ID of a Sloodle Controller through which to access Moodle
  14. // sloodlepwd = the prim password or object-specific session key to authenticate the request
  15. // sloodlemoduleid = ID of a choice
  16. //
  17. // If called with only the above parameters, then summary information about the choice is fetched.
  18. // Status code 10001 will be returned, with the first few data lines having the following format:
  19. // <choice_name>
  20. // <choice_text>
  21. // <is_available>|<timestamp_open>|<timestamp_close>
  22. // <num_unanswered>
  23. //
  24. // Following that will be one line for each available option, with the following format:
  25. // <option_id>|<option_text>|<num_selected>
  26. //
  27. // The "num_unanswered" and "num_selected" values will be -1 if they are not allowed to be shown.
  28. // The "is_available" will be 1 if the choice is open and accepting answers, or 0 otherwise.
  29. // The timestamp values indicate when the choice opens and closes respectively, but will be 0 if there is no opening or closing time.
  30. //
  31. //
  32. // An option can be selected by including the following parameters
  33. // sloodleuuid = UUID of the avatar
  34. // sloodleavname = name of the avatar
  35. // sloodleoptionid = the ID of a particular option (unique site-wide)
  36. //
  37. // If successful, the return code will be 10011, 10012, or 10013, depending what has been done. No data.
  38. // See the status codes list for further information.
  39. //
  40. /** Lets Sloodle know we are in a linker script. */
  41. define('SLOODLE_LINKER_SCRIPT', true);
  42. /** Grab the Sloodle/Moodle configuration. */
  43. require_once('../../init.php');
  44. /** Include the Sloodle PHP API. */
  45. require_once(SLOODLE_LIBROOT.'/sloodle_session.php');
  46. ///// HORRIBLE HACK! /////
  47. // I've copied this function from Moodle 1.9.
  48. // Adding it here makes it work for Moodle 1.8 too.
  49. if (!function_exists('choice_get_response_data')) {
  50. function choice_get_response_data($choice, $cm, $groupmode) {
  51. global $CFG, $USER;
  52. $context = get_context_instance(CONTEXT_MODULE, $cm->id);
  53. /// Get the current group
  54. if ($groupmode > 0) {
  55. $currentgroup = groups_get_activity_group($cm);
  56. } else {
  57. $currentgroup = 0;
  58. }
  59. /// Initialise the returned array, which is a matrix: $allresponses[responseid][userid] = responseobject
  60. $allresponses = array();
  61. /// First get all the users who have access here
  62. /// To start with we assume they are all "unanswered" then move them later
  63. $allresponses[0] = get_users_by_capability($context, 'mod/choice:choose', 'u.id, u.picture, u.firstname, u.lastname, u.idnumber', 'u.firstname ASC', '', '', $currentgroup, '', false, true);
  64. /// Get all the recorded responses for this choice
  65. $rawresponses = sloodle_get_records('choice_answers', 'choiceid', $choice->id);
  66. /// Use the responses to move users into the correct column
  67. if ($rawresponses) {
  68. foreach ($rawresponses as $response) {
  69. if (isset($allresponses[0][$response->userid])) { // This person is enrolled and in correct group
  70. $allresponses[0][$response->userid]->timemodified = $response->timemodified;
  71. $allresponses[$response->optionid][$response->userid] = clone($allresponses[0][$response->userid]);
  72. unset($allresponses[0][$response->userid]); // Remove from unanswered column
  73. }
  74. }
  75. }
  76. return $allresponses;
  77. }
  78. }
  79. ///// END HORRIBLE HACK! /////
  80. // Authenticate the request, and load a choice module
  81. $sloodle = new SloodleSession();
  82. $sloodle->authenticate_request();
  83. $sloodle->load_module('choice', true);
  84. // Has an option been specified?
  85. $sloodleoptionid = $sloodle->request->optional_param('sloodleoptionid');
  86. if ($sloodleoptionid === null) {
  87. // No - we are simply querying for choice data
  88. $sloodle->response->set_status_code(10001);
  89. $sloodle->response->set_status_descriptor('CHOICE_QUERY');
  90. // Check availability and results
  91. $isavailable = '0';
  92. if ($sloodle->module->is_open()) $isavailable = '1';
  93. $canshowresults = $sloodle->module->can_show_results();
  94. // Fetch the intro (question) text, but cut everything after the first line
  95. $qtext = trim($sloodle->module->get_intro());
  96. $qlines = explode("<br />", $qtext);
  97. if (is_array($qlines) && count($qlines) > 0) $qtext = stripslashes(strip_tags($qlines[0]));
  98. else $qtext = '';
  99. // Whatever happens, make sure the question text isn't too long
  100. $maxqtextlen = 128;
  101. if (strlen($qtext) > $maxqtextlen) {
  102. $qlines = str_split($qtext, $maxqtextlen);
  103. if (is_array($qlines) && count($qlines) > 0) $qtext = trim($qlines[0]).'...';
  104. }
  105. // Add the data to the response
  106. $sloodle->response->add_data_line($sloodle->module->get_name());
  107. $sloodle->response->add_data_line($qtext);
  108. $sloodle->response->add_data_line(array($isavailable, $sloodle->module->get_opening_time(), $sloodle->module->get_closing_time()));
  109. if ($canshowresults) $sloodle->response->add_data_line($sloodle->module->get_num_unanswered());
  110. else $sloodle->response->add_data_line('-1');
  111. // Go through each option
  112. foreach ($sloodle->module->options as $optionid => $option) {
  113. // Prepare a data array for this option
  114. $optiondata = array();
  115. $optiondata[] = $optionid;
  116. $optiondata[] = stripslashes(strip_tags($option->text));
  117. if ($canshowresults) $optiondata[] = $option->numselections;
  118. else $optiondata[] = -1;
  119. $sloodle->response->add_data_line($optiondata);
  120. }
  121. } else {
  122. // Yes - validate the user, and permit auto-registration/enrolment
  123. $sloodle->validate_user();
  124. $sloodle->response->set_status_descriptor('CHOICE_SELECT');
  125. // Attempt to select the option
  126. $result = $sloodle->module->select_option($sloodleoptionid);
  127. if (!$result) {
  128. $sloodle->response->set_status_code(-10016);
  129. $sloodle->response->add_data_line('Unknown error selecting option.');
  130. } else {
  131. $sloodle->response->set_status_code($result);
  132. }
  133. }
  134. // Output our response
  135. $sloodle->response->render_to_output();
  136. ?>