MDL-20636 ddwtos and gapselect qtypes now very nearly work.
[moodle.git] / question / qengine.js
CommitLineData
157434a5
TH
1M.core_question_engine = M.core_question_engine || {};
2
06f8ed54
TH
3/**
4 * Flag used by M.core_question_engine.prevent_repeat_submission.
5 */
6M.core_question_engine.questionformalreadysubmitted = false;
7
8/**
9 * Initialise a question submit button. This saves the scroll position and
10 * sets the fragment on the form submit URL so the page reloads in the right place.
11 * @param id the id of the button in the HTML.
12 * @param slot the number of the question_attempt within the usage.
13 */
14M.core_question_engine.init_submit_button(Y, button, slot) {
15 Y.on('click', function(e) {
16 var scrollpos = document.getElementById('scrollpos');
17 if (scrollpos) {
18 scrollpos.value = YAHOO.util.Dom.getDocumentScrollTop();
19 }
20 button.form.action = button.form.action + '#q' + slot;
21 }, button);
22}
23
157434a5
TH
24/**
25 * Initialise a form that contains questions printed using print_question.
26 * This has the effect of:
27 * 1. Turning off browser autocomlete.
28 * 2. Stopping enter from submitting the form (or toggling the next flag) unless
29 * keyboard focus is on the submit button or the flag.
30 * 3. Removes any '.questionflagsavebutton's, since we have JavaScript to toggle
06f8ed54
TH
31 * the flags using ajax.
32 * 4. Scroll to the position indicated by scrollpos= in the URL, if it is there.
33 * 5. Prevent the user from repeatedly submitting the form.
157434a5
TH
34 * @param Y the Yahoo object. Needs to have the DOM and Event modules loaded.
35 * @param form something that can be passed to Y.one, to find the form element.
36 */
37M.core_question_engine.init_form = function(Y, form) {
38 Y.one(form).setAttribute('autocomplete', 'off');
06f8ed54
TH
39
40 Y.on('submit', M.core_question_engine.prevent_repeat_submission, form, form, Y);
41
157434a5
TH
42 Y.on('key', function (e) {
43 if (!e.target.test('a') && !e.target.test('input[type=submit]') &&
44 !e.target.test('input[type=img]')) {
45 e.preventDefault();
46 }
47 }, form, 'press:13');
06f8ed54 48
157434a5 49 Y.one(form).all('.questionflagsavebutton').remove();
06f8ed54
TH
50
51 var matches = window.location.href.match(/^.*[?&]scrollpos=(\d*)(?:&|$|#).*$/, '$1');
52 if (matches) {
53 // onDOMReady is the effective one here. I am leaving the immediate call to
54 // window.scrollTo in case it reduces flicker.
55 window.scrollTo(0, matches[1]);
56 Y.on('domready', function() { window.scrollTo(0, matches[1]); });
57
58 // And the following horror is necessary to make it work in IE 8.
59 // Note that the class ie8 on body is only there in Moodle 2.0 and OU Moodle.
60 if (YAHOO.util.Dom.hasClass(document.body, 'ie')) {
61 question_force_ie_to_scroll(matches[1])
62 }
63 }
64}
65
66/**
67 * Event handler to stop the quiz form being submitted more than once.
68 * @param e the form submit event.
69 * @param form the form element.
70 */
71M.core_question_engine.prevent_repeat_submission(e, Y) {
72 if (M.core_question_engine.questionformalreadysubmitted) {
73 e.halt();
74 return;
75 }
76
77 setTimeout(function() {
78 Y.all('input[type=submit]').disabled = true;
79 }, 0);
80 M.core_question_engine.questionformalreadysubmitted = true;
81}
82
83/**
84 * Beat IE into submission.
85 * @param targetpos the target scroll position.
86 */
87M.core_question_engine.force_ie_to_scroll(targetpos) {
88 var hackcount = 25;
89 function do_scroll() {
90 window.scrollTo(0, targetpos);
91 hackcount -= 1;
92 if (hackcount > 0) {
93 setTimeout(do_scroll, 10);
94 }
95 }
96 Y.on('load', do_scroll, window);
157434a5 97}