qengine.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // This file is part of Moodle - http://moodle.org/
  2. //
  3. // Moodle is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation, either version 3 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // Moodle is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  15. /**
  16. * JavaScript required by the question engine.
  17. *
  18. * @package moodlecore
  19. * @subpackage questionengine
  20. * @copyright 2008 The Open University
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. /**
  24. * Scroll manager is a class that help with saving the scroll positing when you
  25. * click on an action icon, and then when the page is reloaded after processing
  26. * the action, it scrolls you to exactly where you were. This is much nicer for
  27. * the user.
  28. *
  29. * To use this in your code, you need to ensure that:
  30. * 1. The button that triggers the action has to have a click event handler that
  31. * calls M.core_scroll_manager.save_scroll_pos
  32. * 2. The script that process the action has to grab the scrollpos parameter
  33. * using $scrollpos = optional_param('scrollpos', 0, PARAM_INT);
  34. * 3. After doing the processing, it must add ->param('scrollpos', $scrollpos)
  35. * to the URL that it redirects to.
  36. * 4. Finally, on the page that is reloaded (which should be the same as the one
  37. * the user started on) you need to call M.core_scroll_manager.scroll_to_saved_pos
  38. * on page load.
  39. */
  40. M.core_scroll_manager = M.core_scroll_manager || {};
  41. /**
  42. * In the form that contains the element, set the value of the form field with
  43. * name scrollpos to the current scroll position. If there is no element with
  44. * that name, it creates a hidden form field wiht that name within the form.
  45. * @param element the element in the form. Should be something that can be
  46. * passed to Y.one.
  47. */
  48. M.core_scroll_manager.save_scroll_pos = function(Y, element) {
  49. if (typeof(element) == 'string') {
  50. // Have to use getElementById here because element id can contain :.
  51. element = Y.one(document.getElementById(element));
  52. }
  53. var form = element.ancestor('form');
  54. if (!form) {
  55. return;
  56. }
  57. var scrollpos = form.one('input[name=scrollpos]');
  58. if (!scrollpos) {
  59. scrollpos = form.appendChild(form.create('<input type="hidden" name="scrollpos" />'));
  60. }
  61. scrollpos.set('value', form.get('docScrollY'));
  62. }
  63. /**
  64. * Event handler that can be used on a link. Assumes that the link already
  65. * contains at least one URL parameter.
  66. */
  67. M.core_scroll_manager.save_scroll_action = function(e) {
  68. var link = e.target.ancestor('a[href]');
  69. if (!link) {
  70. M.core_scroll_manager.save_scroll_pos({}, e.target);
  71. return;
  72. }
  73. link.set('href', link.get('href') + '&scrollpos=' + link.get('docScrollY'));
  74. }
  75. /**
  76. * If there is a parameter like scrollpos=123 in the URL, scroll to that saved position.
  77. */
  78. M.core_scroll_manager.scroll_to_saved_pos = function(Y) {
  79. var matches = window.location.href.match(/^.*[?&]scrollpos=(\d*)(?:&|$|#).*$/, '$1');
  80. if (matches) {
  81. // onDOMReady is the effective one here. I am leaving the immediate call to
  82. // window.scrollTo in case it reduces flicker.
  83. window.scrollTo(0, matches[1]);
  84. Y.on('domready', function() { window.scrollTo(0, matches[1]); });
  85. // And the following horror is necessary to make it work in IE 8.
  86. // Note that the class ie8 on body is only there in Moodle 2.0 and OU Moodle.
  87. if (Y.one('body').hasClass('ie')) {
  88. M.core_scroll_manager.force_ie_to_scroll(Y, matches[1])
  89. }
  90. }
  91. }
  92. /**
  93. * Beat IE into submission.
  94. * @param targetpos the target scroll position.
  95. */
  96. M.core_scroll_manager.force_ie_to_scroll = function(Y, targetpos) {
  97. var hackcount = 25;
  98. function do_scroll() {
  99. window.scrollTo(0, targetpos);
  100. hackcount -= 1;
  101. if (hackcount > 0) {
  102. setTimeout(do_scroll, 10);
  103. }
  104. }
  105. Y.on('load', do_scroll, window);
  106. }
  107. M.core_question_engine = M.core_question_engine || {};
  108. /**
  109. * Flag used by M.core_question_engine.prevent_repeat_submission.
  110. */
  111. M.core_question_engine.questionformalreadysubmitted = false;
  112. /**
  113. * Initialise a question submit button. This saves the scroll position and
  114. * sets the fragment on the form submit URL so the page reloads in the right place.
  115. * @param id the id of the button in the HTML.
  116. * @param slot the number of the question_attempt within the usage.
  117. */
  118. M.core_question_engine.init_submit_button = function(Y, button, slot) {
  119. var buttonel = document.getElementById(button);
  120. Y.on('click', function(e) {
  121. M.core_scroll_manager.save_scroll_pos(Y, button);
  122. buttonel.form.action = buttonel.form.action + '#q' + slot;
  123. }, buttonel);
  124. }
  125. /**
  126. * Initialise a form that contains questions printed using print_question.
  127. * This has the effect of:
  128. * 1. Turning off browser autocomlete.
  129. * 2. Stopping enter from submitting the form (or toggling the next flag) unless
  130. * keyboard focus is on the submit button or the flag.
  131. * 3. Removes any '.questionflagsavebutton's, since we have JavaScript to toggle
  132. * the flags using ajax.
  133. * 4. Scroll to the position indicated by scrollpos= in the URL, if it is there.
  134. * 5. Prevent the user from repeatedly submitting the form.
  135. * @param Y the Yahoo object. Needs to have the DOM and Event modules loaded.
  136. * @param form something that can be passed to Y.one, to find the form element.
  137. */
  138. M.core_question_engine.init_form = function(Y, form) {
  139. Y.one(form).setAttribute('autocomplete', 'off');
  140. Y.on('submit', M.core_question_engine.prevent_repeat_submission, form, form, Y);
  141. Y.on('key', function (e) {
  142. if (!e.target.test('a') && !e.target.test('input[type=submit]') &&
  143. !e.target.test('input[type=img]') && !e.target.test('textarea') && !e.target.test('[contenteditable=true]')) {
  144. e.preventDefault();
  145. }
  146. }, form, 'press:13');
  147. Y.one(form).all('.questionflagsavebutton').remove();
  148. M.core_scroll_manager.scroll_to_saved_pos(Y);
  149. }
  150. /**
  151. * Event handler to stop a question form being submitted more than once.
  152. * @param e the form submit event.
  153. * @param form the form element.
  154. */
  155. M.core_question_engine.prevent_repeat_submission = function(e, Y) {
  156. if (M.core_question_engine.questionformalreadysubmitted) {
  157. e.halt();
  158. return;
  159. }
  160. setTimeout(function() {
  161. Y.all('input[type=submit]').set('disabled', true);
  162. }, 0);
  163. M.core_question_engine.questionformalreadysubmitted = true;
  164. }