Note that there are some explanatory texts on larger screens.

plurals
  1. POMath drupal module
    text
    copied!<p>I'm trying to learn to write drupal modules and am working on writing a simple math module <a href="http://nodeone.se/en/the-math-question-module" rel="nofollow">http://nodeone.se/en/the-math-question-module</a></p> <p>I've got the GUI down fine but seem to be having issues with the actual math. I never seem to get the correct answer because I think it loads a new set of questions before it checks the current answer.</p> <p>Here's what I have so far:</p> <pre><code> &lt;?php /** * @file * Tests users on their math skills through a series of question and answers */ /** * Implements hook_menu(). */ function math_question_menu() { // add new navigation menu item $items['math_question'] = array( 'title' =&gt; 'Math questions', 'description' =&gt; 'Test you math skills with these questions.', 'page callback' =&gt; 'drupal_get_form', 'page arguments' =&gt; array('math_question_page'), 'access callback' =&gt; 'user_access', 'access arguments' =&gt; array('administer_site_configuration'), ); return $items; } $val1 = rand(1,10); $val2 = rand(1,10); variable_set('val1', $val1); variable_set('val2', $val2); /** * Builds the form for configuring Math Questions. */ function math_question_page() { global $user; $num1 = variable_get('val1'); $num2 = variable_get('val2'); $total = $num1 + $num2; variable_set('total',$total); $form['math_question'] = array( '#type' =&gt; 'item', '#markup' =&gt; 'What is ' . $num1 . ' + ' . $num2 . ', ' . check_plain($user-&gt;name) . '?', ); $form['answer'] = array( '#type' =&gt; 'textfield', '#title' =&gt; t('Answer'), ); $form['submit'] = array( '#type' =&gt; 'submit', '#value' =&gt; t('Check my answer'), ); return $form; } function math_question_page_validate($form, $form_state) { if (empty($form['answer']['#value'])) { form_error($form['answer'], t('This field is required.')); } if ($form['answer']['#value'] == variable_get('total')) { $msg = t('Good job!'); $type = 'status'; } else { $msg = t('Try again...'); $type = 'error'; } drupal_set_message(check_plain($msg), $type); } </code></pre> <hr> <p>modified code: . . </p> <pre><code> /** * Builds the form for configuring Math Questions. */ function math_question_page() { global $user; $val1 = rand(1,10); $val2 = rand(1,10); $total = $val1 + $val2; $form['math_question'] = array( '#type' =&gt; 'item', '#markup' =&gt; 'What is ' . $val1 . ' + ' . $val2 . ', ' . check_plain($user-&gt;name) . '?', ); $form['answer'] = array( '#type' =&gt; 'textfield', '#title' =&gt; t('Answer'), ); $form['total'] = array( '#type' =&gt; 'hidden', '#value' =&gt; $total, ); $form['submit'] = array( '#type' =&gt; 'submit', '#value' =&gt; t('Check my answer'), ); return $form; } function math_question_page_validate($form, $form_state) { if (empty($form['answer']['#value'])) { form_error($form['answer'], t('This field is required.')); } if ($form['answer']['#value'] == $form['total']['#value']) { $msg = t('Good job!'); $type = 'status'; } else { $msg = t('Try again...'); $type = 'error'; } drupal_set_message(check_plain($msg), $type); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload