linker.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * Sloodle quiz linker (for Sloodle 0.3).
  4. * Allows in-world objects to interact with Moodle quizzes.
  5. * Part of the Sloodle project (www.sloodle.org).
  6. *
  7. * @package sloodlequiz
  8. * @copyright Copyright (c) 2006-8 Sloodle (various contributors)
  9. * @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3
  10. *
  11. * @contributor (various Moodle authors)
  12. * @contributor Edmund Edgar
  13. * @contributor Peter R. Bloomfield
  14. */
  15. // This script is expected to be requested by an in-world object.
  16. // The following parameters are required:
  17. //
  18. // sloodlecontrollerid = ID of the controller through which to access Moodle
  19. // sloodlepwd = password to authenticate the request
  20. // sloodlemoduleid = the course module ID of the quiz to access
  21. // sloodleuuid = UUID of the avatar making the request (optional if 'sloodleavname' is specified)
  22. // sloodleavname = avatar name of the user making the request (optional if 'sloodleuuid' is specified)
  23. //
  24. //
  25. //
  26. // The following are the original quiz parameters:
  27. //
  28. // q = ID of the quiz course module instance
  29. // id = ID of the course which this request relates to
  30. // page = ?
  31. // questionids = ?
  32. // finishattempt = ?
  33. // timeup = true if submission was by timer
  34. // forcenew = teacher has requested a new preview
  35. // action = ??
  36. /** Lets Sloodle know we are in a linker script. */
  37. define('SLOODLE_LINKER_SCRIPT', true);
  38. /** Grab the Sloodle/Moodle configuration. */
  39. require_once('../../init.php');
  40. /** Include the Sloodle PHP API. */
  41. require_once(SLOODLE_LIBROOT.'/sloodle_session.php');
  42. /** Include the Moodle quiz code. */
  43. require_once($CFG->dirroot.'/mod/quiz/locallib.php');
  44. //ini_set('display_errors','On');
  45. // Authenticate the request and login the user
  46. $sloodle = new SloodleSession();
  47. $sloodle->authenticate_request();
  48. $sloodle->validate_user();
  49. $sloodle->user->login();
  50. $sloodle->validate_requirements();
  51. // Grab our additional parameters
  52. $id = $sloodle->request->get_module_id();
  53. $limittoquestion = optional_param('ltq', 0, PARAM_INT);
  54. $output = array();
  55. $courseid = optional_param('courseid', 0, PARAM_INT); // Course Module ID
  56. //$id = optional_param('id', 0, PARAM_INT); // Course Module ID
  57. $q = optional_param('q', 0, PARAM_INT); // or quiz ID
  58. $page = optional_param('page', 0, PARAM_INT);
  59. $questionids = optional_param('questionids', '', PARAM_RAW);
  60. $finishattempt = optional_param('finishattempt', 0, PARAM_BOOL);
  61. $timeup = optional_param('timeup', 0, PARAM_BOOL); // True if form was submitted by timer.
  62. $forcenew = optional_param('forcenew', false, PARAM_BOOL); // Teacher has requested new preview
  63. $isnotify = ( optional_param( 'action', false, PARAM_RAW ) == 'notify' ) ;
  64. // Prior to Moodle 2.1 this information had to be passed in through an obscure $_POST var called $resp.
  65. // We didn't handle it - we just called the relevant functions that did.
  66. // TODO: Update the quiz to set "response" instead of making this crazy variable name.
  67. $quizresponse = optional_param('response', -1, PARAM_RAW);
  68. // Backwards compatibility for old LSL scripts that send us data the < 2.0 way:
  69. if ( $isnotify && ($quizresponse < 0) ) {
  70. $responseparam = 'resp'.$questionids.'_';
  71. $quizresponse = optional_param($responseparam, -1, PARAM_RAW);
  72. }
  73. // Remove the above when we no longer need to support old objects
  74. // ...belonging to SLOODLE 2.0.13-alpha or earlier.
  75. // remember the current time as the time any responses were submitted
  76. // (so as to make sure students don't get penalized for slow processing on this page)
  77. $timestamp = time();
  78. // We treat automatically closed attempts just like normally closed attempts
  79. if ($timeup) {
  80. $finishattempt = 1;
  81. }
  82. if ( ($courseid != 0) && ($id == 0) && ($q == 0) ) {
  83. // fetch a quiz with the id for the course
  84. if (! $mod = sloodle_get_record("modules", "name", "quiz") ) {
  85. $sloodle->response->quick_output(-712, 'MODULE_INSTANCE', 'Could not find quiz module', FALSE);
  86. exit;
  87. }
  88. //if (! $coursemod = sloodle_get_record("course_modules", "courseid", $courseid, "module", $mod->id)) {
  89. if (! $coursemod = sloodle_get_record("course_modules", "course", $courseid, "module", $mod->id)) {
  90. $sloodle->response->quick_output(-712, 'MODULE_INSTANCE', 'Could not find course module instance', FALSE);
  91. exit;
  92. }
  93. $id = $coursemod->id;
  94. }
  95. if ($id) {
  96. if (! $cm = get_coursemodule_from_id('quiz', $id)) {
  97. $sloodle->response->quick_output(-701, 'MODULE_INSTANCE', 'Identified module instance is not a quiz', FALSE);
  98. exit();
  99. }
  100. if (! $course = sloodle_get_record("course", "id", $cm->course)) {
  101. $sloodle->response->quick_output(-701, 'MODULE_INSTANCE', 'Quiz module is misconfigured', FALSE);
  102. exit();
  103. }
  104. if (! $quiz = sloodle_get_record("quiz", "id", $cm->instance)) {
  105. $sloodle->response->quick_output(-712, 'MODULE_INSTANCE', 'Part of quiz module instance is missing', FALSE);
  106. exit();
  107. }
  108. } else {
  109. if (! $quiz = sloodle_get_record("quiz", "id", $q)) {
  110. $sloodle->response->quick_output(-712, 'MODULE_INSTANCE', 'Could not find quiz module', FALSE);
  111. exit();
  112. }
  113. if (! $course = sloodle_get_record("course", "id", $quiz->course)) {
  114. $sloodle->response->quick_output(-712, 'MODULE_INSTANCE', 'Part of quiz module instance is missing', FALSE);
  115. exit();
  116. }
  117. if (! $cm = get_coursemodule_from_instance("quiz", $quiz->id, $course->id)) {
  118. $sloodle->response->quick_output(-701, 'MODULE_INSTANCE', 'Unable to resolve course module instance', FALSE);
  119. exit();
  120. }
  121. }
  122. // Anything below Moodle 2.1 should use the old version.
  123. // In case we're wrong about the cut-off point,
  124. // ...we'll also check for the existence of get_question_states,
  125. // ...which went away in the new quiz engine.
  126. if ( ( $CFG->version < 2011070100 ) && ( function_exists('get_question_states') ) ) {
  127. require_once(SLOODLE_DIRROOT.'/mod/quiz-1.0/'.'linker_moodle_2_0_down.php');
  128. } else {
  129. require_once(SLOODLE_DIRROOT.'/mod/quiz-1.0/'.'linker_moodle_2_1_up.php');
  130. }
  131. ?>