/[drupal]/contributions/modules/jeopardy/jeopardy.module
ViewVC logotype

Diff of /contributions/modules/jeopardy/jeopardy.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.1, Tue Jan 30 21:46:56 2007 UTC revision 1.2, Sun Aug 31 12:20:53 2008 UTC
# Line 1  Line 1 
1  <?php  <?php
2    
3    // $Id$
4    
5  /**  /**
6  * @file  * @file
7  * Enables users to submit jeopardy games..  * Enables users to submit jeopardy games..
# Line 7  Line 10 
10  /**  /**
11  * Get Drupal information  * Get Drupal information
12  */  */
   
13  define('JEOPARDY_PATH', drupal_get_path('module', 'jeopardy'));  define('JEOPARDY_PATH', drupal_get_path('module', 'jeopardy'));
14    
15  /**  /**
16  * Implementation of hook_help().  * Implementation of hook_help().
17  */  */
18  function jeopardy_help($section) {  function jeopardy_help($section) {
19          switch ($section) {    switch ($section) {
20                  case 'admin/help#jeopardy':      case 'admin/help#jeopardy':
21                          $output = '<p>'. t('The jeopardy module is used to create a content post type called <em>jeopardy games..</em> Users can either choose from pre-defined categories and questions, or create their categories and questions on the fly. ') .'</p>';        $output = '<p>'. t('The jeopardy module is used to create a content post type called <em>jeopardy games..</em> Users can either choose from pre-defined categories and questions, or create their categories and questions on the fly. ') .'</p>';
22                          $output .= '<p>'. t('The Jeopardy administration interface allows for complex configuration. It provides a submission form, workflow, default view permission, default edit permission, permissions for permission, and attachments. Trackbacks can also be enabled.') .'</p>';        $output .= '<p>'. t('The Jeopardy administration interface allows for complex configuration. It provides a submission form, workflow, default view permission, default edit permission, permissions for permission, and attachments. Trackbacks can also be enabled.') .'</p>';
23                          $output .= t('<p>You can</p><ul><li>post a Jeopardy at <a href="%node-add-jeopardy">create content &gt;&gt; Jeopardy</a>.</li><li>configure Jeopardy at <a href="%admin-settings-content-types-jeopardy">administer &gt;&gt; settings &gt;&gt; content types &gt;&gt; configure Jeopardy</a>.</li></ul>', array('%node-add-jeopardy' =>url('node/add/jeopardy'), '%admin-settings-content-types-jeopardy' => url('admin/settings/content-types/jeopardy')));        $output .= t('<p>You can</p><ul><li>post a Jeopardy at <a href="%node-add-jeopardy">create content &gt;&gt; Jeopardy</a>.</li><li>configure Jeopardy at <a href="%admin-settings-content-types-jeopardy">administer &gt;&gt; settings &gt;&gt; content types &gt;&gt; configure Jeopardy</a>.</li></ul>', array('%node-add-jeopardy' =>url('node/add/jeopardy'), '%admin-settings-content-types-jeopardy' => url('admin/settings/content-types/jeopardy')));
24                          $output .= '<p>'. t('You can assign the default layout of the Jeopardy game here. <a href="%jeopardy">Jeopardy page</a>.', array('%jeopardy' => '**drupal-help-link**')) .'</p>';        $output .= '<p>'. t('You can assign the default layout of the Jeopardy game here. <a href="%jeopardy">Jeopardy page</a>.', array('%jeopardy' => '**drupal-help-link**')) .'</p>';
25                          return $output;        return $output;
26                  case 'admin/modules#description':      case 'admin/modules#description':
27                          return t('Allows users to add Jeopardy games to their posts.  The questions can either be created "on the fly" or added from a database.');        return t('Allows users to add Jeopardy games to their posts.  The questions can either be created "on the fly" or added from a database.');
28                  case 'node/add#jeopardy':      case 'node/add#jeopardy':
29                          return t('You can either choose from a list of predefined questions, or add new questions to your game.  You can also choose categories from a prefined list, or add categories here.');        return t('You can either choose from a list of predefined questions, or add new questions to your game.  You can also choose categories from a prefined list, or add categories here.');
30          }     }
31  }  }
32    
33  /**  /**
# Line 33  function jeopardy_help($section) { Line 35  function jeopardy_help($section) {
35  */  */
36  function jeopardy_node_info() {  function jeopardy_node_info() {
37    return array(    return array(
38                          'jeopardy' => array(      'jeopardy' => array(
39                                  'name' => t('Jeopardy'),        'name' => t('Jeopardy'),
40                                  'module' => 'jeopardy',        'module' => 'jeopardy',
41                                  'base' => 'jeopardy',        'base' => 'jeopardy',
42                                  'description' => t('Create jeopardy games.')        'description' => t('Create jeopardy games.')
43                          )      )
44                  );    );
45  }  }
46    
47  /**  /**
48  * Implementation of hook_perm().  * Implementation of hook_perm().
49  */  */
50  function jeopardy_perm() {  function jeopardy_perm() {
51          return array('create jeopardy games.', 'edit own jeopardy games.', 'view jeopardy game.');    return array('create jeopardy games.', 'edit own jeopardy games.', 'view jeopardy game.');
52  }  }
53    
54  /**  /**
55  * Implementation of hook_access().  * Implementation of hook_access().
56  */  */
57  function jeopardy_access($op, $node) {  function jeopardy_access($op, $node) {
58          global $user;    global $user;
59    
60          if ($op == 'create') {    if ($op == 'create') {
61                  return user_access('create jeopardy games.');      return user_access('create jeopardy games.');
62          }    }
63    
64          if ($op == 'update' || $op == 'delete') {    if ($op == 'update' || $op == 'delete') {
65                  if (user_access('edit own jeopardy games.') && ($user->uid == $node->uid)) {      if (user_access('edit own jeopardy games.') && ($user->uid == $node->uid)) {
66                          return TRUE;        return TRUE;
67                  }      }
68          }    }
69    
70          if ($op == 'view') {    if ($op == 'view') {
71                  if (user_access('view jeopardy games.')) {      if (user_access('view jeopardy games.')) {
72                          return TRUE;        return TRUE;
73                  }      }
74          }    }
75  }  }
76    
77  /**  /**
78  * Implementation of hook_menu().  * Implementation of hook_menu().
79  */  */
80  function jeopardy_menu($may_cache) {  function jeopardy_menu($may_cache) {
81          $items = array();    $items = array();
82    
83      if ($may_cache) {
84        $items[] = array(
85          'path' => 'node/add/jeopardy',
86          'title' => t('Jeopardy'),
87          'access' => user_access('create jeopardy games'),
88          'type' => MENU_NORMAL_ITEM,
89        );
90        $items[] = array(
91          'path' => 'admin/settings/jeopardy',
92          'title' => t('Jeopardy'),
93          'description' => t('Set the number of categories and questions for new games.'),
94          'callback' => 'drupal_get_form',
95          'callback arguments' => array('jeopardy_settings_form'),
96          'access' => user_access('administer site configuration'),
97          'type' => MENU_NORMAL_ITEM,
98        );
99      }
100      else {
101        // try to override node/add here
102        if (arg(0) == 'node' && arg(1) == 'add' && arg(2) == 'jeopardy' && arg(3) == NULL) {
103          $items[] = array(
104            'path' => 'node/add/jeopardy',
105            'title' => t('Create Jeopardy Game'),
106            'access' => user_access('create jeopardy games.'),
107            'callback' => 'drupal_get_form',
108            'callback arguments' => array('jeopardy_choose_size_form'),
109            'type' => MENU_NORMAL_ITEM
110          );
111        }
112      }
113      return $items;
114    }
115    
116    /**
117     * Let user choose the number of rows and columns of the jeopardy nodes
118     */
119    function jeopardy_choose_size_form() {
120      $form = array();
121    
122      $form['jeopardy_rows'] = array(
123        '#type' => 'textfield',
124        '#title' => t('Number of rows'),
125        '#description' => t('Enter the number of rows for your Jeopardy game as a positive whole number, example: 5.'),
126        '#size' => 10,
127        '#required' => TRUE
128      );
129    
130      $form['jeopardy_cols'] = array(
131        '#type' => 'textfield',
132        '#title' => t('Number of columns'),
133        '#description' => t('Enter the number of columns for your Jeopardy game as a positive whole number, example: 5.'),
134        '#size' => 10,
135        '#required' => TRUE
136      );
137    
138      $form['next'] = array(
139        '#type' => 'submit',
140        '#value' => t('Next'),
141      );
142    
143          if ($may_cache) {    $form['#validate'] = array('jeopardy_form_validate_size' => array());
144                  $items[] = array(    $form['#submit'] = array('jeopardy_form_submit_redirect' => array());
145                      'path' => 'node/add/jeopardy',  
146                      'title' => t('Jeopardy'),    return $form;
                     'access' => user_access('create jeopardy games.')  
                         );  
                 $items[] = array(  
                         'path' => 'admin/settings/jeopardy',  
                         'title' => t('Jeopardy'),  
                         'description' => t('Set the number of categories and questions for new games.'),  
                         'callback' => 'jeopardy_settings',  
                         'access' => user_access('administer site configuration'),  
                         'type' => MENU_NORMAL_ITEM, // optional  
                    );  
         }  
         return $items;  
147  }  }
148    
149  /**  /**
150  * Implementation of hook_form().  * Implementation of hook_form().
151  */  */
152  function jeopardy_form(&$node) {  function jeopardy_form(&$node) {
153          $rows = variable_get('number_of_questions', 5);    $form = array();
         $cols = variable_get('number_of_categories', 5);  
         $form['title'] = array(  
                 '#type' => 'textfield',  
                 '#title' => t('Name of game - should be unique!'),  
                 '#required' => TRUE,  
                 '#default_value' => $node->title,  
                 '#weight' => -5);  
         $n = 0;  
         for ($j=1;$j<=$rows;$j++) {  
                 $c = "jeopardy_category$j";  
                 $form['category']['jeopardy_category'.$j] = array(  
                         '#type' => 'textfield',  
                         '#title' => t('Category '.$j),  
                         '#default_value' => $node->$c,  
                         '#size' => 30,  
                         '#required' => TRUE,  
                         '#weight' => $n  
                         );  
                 for ($k=1;$k<=$cols;$k++) {  
                         $n++;  
                         $q = "jeopardy_question$j$k";  
                         $form['question']['jeopardy_question'.$j.$k] = array(  
                                 '#type' => 'textarea',  
                                 '#title' => t('Question '.$k),  
                                 '#default_value' => $node->$q,  
                                 '#rows' => 5,  
                                 '#required' => TRUE,  
                                 '#weight' => $n  
                         );  
                 }  
         }  
         //$form['category']['format'] = filter_form($node->category);  
         //$form['question']['format'] = filter_form($node->question);  
         return $form;  
 }  
154    
155  /**    $rows = $node->jeopardy_rows ? $node->jeopardy_rows : arg(3);
156   * Displays and allows an administrator to change the settings for this module.    $cols = $node->jeopardy_cols ? $node->jeopardy_cols : arg(4);
  */  
 function jeopardy_settings() {  
         return drupal_get_form('jeopardy_settings_form');  
 }  
157    
158  function jeopardy_settings_form() {    if (is_numeric($rows) && $rows > 0 && is_numeric($cols) && $cols > 0) {
159      $form['number_of_questions'] = array(  
160        $form['jeopardy_rows'] = array(
161        '#type' => 'textfield',        '#type' => 'textfield',
162        '#title' => t('Number of questions per category'),        '#default_value' => $rows,
163        '#default_value' =>  variable_get('number_of_questions', 5),        '#title' => t('Number of questions per categories'),
164        '#description' => t('This sets the number of questions in each category of a Jeopardy game.'),        '#description' => t('If you change this number of questions per categories, any information in additional questions can be lost.  So be careful!'),
165        '#required' => TRUE);        '#required' => TRUE
166        );
167    
168      $form['number_of_categories'] = array(      $form['jeopardy_cols'] = array(
169        '#type' => 'textfield',        '#type' => 'textfield',
170        '#title' => t('Number of Categories'),        '#default_value' => $cols,
171        '#default_value' => variable_get('number_of_categories', 5),        '#title' => t('Number of categories'),
172        '#description' => t('This sets how many categories should appear in each Jeopardy game.'),        '#description' => t('If you change this number of categories, any information in additional categories can be lost.  So be careful!'),
173        '#required' => TRUE        '#required' => TRUE
174          );      );
175    
176          return system_settings_form($form);      $form['title'] = array(
177          '#type' => 'textfield',
178          '#title' => t('Name of game'),
179          '#description' => t('If you make this name unique, it can make finding this game easier later.'),
180          '#required' => TRUE,
181          '#default_value' => $node->title,
182          '#weight' => -5
183        );
184    
185        $n = 0;
186        for ($j = 1;$j <= $cols; $j++) {
187          $c = "jeopardy_category$j";
188          $form['jeopardy_category'.$j] = array(
189            '#type' => 'textfield',
190            '#title' => t('Category '. $j),
191            '#default_value' => $node->$c,
192            '#size' => 30,
193            '#required' => TRUE,
194          );
195    
196          for ($k = 1;$k <= $rows; $k++) {
197            $q = "jeopardy_question$j$k";
198            $a = "jeopardy_answer$j$k";
199    
200            $form['jeopardy_question'.$j.$k] = array(
201              '#type' => 'textarea',
202              '#title' => t('Question '. $k),
203              '#default_value' => $node->$q,
204              '#rows' => 5,
205              '#required' => TRUE,
206            );
207    
208            $form['jeopardy_answer'.$j.$k] = array(
209              '#type' => 'textarea',
210              '#title' => t('Answer '. $k),
211              '#default_value' => $node->$a,
212              '#rows' => 5,
213              '#required' => FALSE,
214            );
215          }
216        }
217    
218        $form['#validate'] = array('jeopardy_form_validate_size' => array());
219    
220      }
221      return $form;
222    }
223    
224    function jeopardy_form_validate_size($form_id, $form_values) {
225      if (!is_numeric($form_values['jeopardy_rows']) || $form_values['jeopardy_rows'] == 0) {
226        form_set_error($form_values['jeopardy_rows'], t('You must an enter a positive whole number of rows, example: 5.'));
227      }
228      if (!is_numeric($form_values['jeopardy_cols']) || $form_values['jeopardy_cols'] == 0) {
229        form_set_error($form_values['jeopardy_cols'], t('You must an enter a positive whole number of columns, example: 5.'));
230      }
231    }
232    
233    function jeopardy_form_submit_redirect($form_id, $form_values) {
234      if (is_numeric($form_values['jeopardy_rows']) && is_numeric($form_values['jeopardy_cols'])) {
235        drupal_goto('node/add/jeopardy/'. $form_values['jeopardy_rows'] .'/'. $form_values['jeopardy_cols']);
236      }
237  }  }
238    
239  /*  /*
240  * implementation of Drupal's theme functions  * implementation of Drupal's theme functions
241  */  */
242  function jeopardy_view(&$node, $teaser = FALSE, $page = FALSE) {  function jeopardy_view(&$node, $teaser = FALSE, $page = FALSE) {
243          drupal_add_css(JEOPARDY_PATH .'/jeopardy.css');    drupal_add_css(JEOPARDY_PATH .'/jeopardy.css');
244          drupal_add_js(JEOPARDY_PATH .'/jeopardy.js', 'module');    drupal_add_js(JEOPARDY_PATH .'/jeopardy.js', 'module');
         $output = '';  
   
         $output .= '<table class="jeopardy-table">';  
245    
246          $rows = $node->jeopardy_rows;    $output = '<table class="jeopardy-table">';
         $cols = $node->jeopardy_cols;  
247    
248      $rows = $node->jeopardy_rows;
249      $cols = $node->jeopardy_cols;
250    
251      $output .= '<tr class="jeopardy-table-header-row">';
252      for ($j = 1; $j <= $cols; $j++) {
253        $c = "jeopardy_category$j";
254        $category = $node->$c;
255        $output .= '<th class="jeopardy-table-header">'. $category .'</th>';
256      }
257    
258          $output .= '<tr class="jeopardy-table-header-row">';    $output .= '</tr>';
259          for ($j=1;$j<=$cols;$j++) {    for ($j = 1; $j <= $rows; $j++) {
260                  $c = "jeopardy_category$j";      $output .= '<tr class="jeopardy-table-question-row">';
261                  $category = $node->$c;      for ($k = 1; $k <= $cols; $k++) {
262                  $output .= '<th class="jeopardy-table-header">'.$category.'</th>';        $q = "jeopardy_question$k$j";
263          }        $a = "jeopardy_answer$k$j";
264    
265          $output .= '</tr>';        $output .= '<td class="jeopardy-table-question" id="jeopardy-'. $j .'-'. $k .'" >'. ($j*100) .'</td>';
266          for ($j=1;$j<=$rows;$j++) {        $output .= '<div class="jeopardy-completely-hidden-question" id="question-'. $j .'-'. $k .'">'. filter_xss($node->$q) .'</div>';
267                  $output .= '<tr class="jeopardy-table-question-row">';        $output .= '<div class="jeopardy-completely-hidden-answer" id="answer-'. $j .'-'. $k .'">'. filter_xss($node->$a) .'</div>';
268                  for ($k=1;$k<=$cols;$k++) {      }
269                          $q = "jeopardy_question$j$k";      $output .= '</tr>';
270                          $question = $node->$q;    }
                         $output .= '<td class="jeopardy-table-question" id="jeopardy-'.$j.$k.'" onclick="showquestion(\'question-'.$j.$k.'\')">'.($j*100).'</td>';  
                         $output .= '<div class="jeopardy-completely-hidden-question" id="question-'.$k.$j.'">'.$question.'</div>';  
                 }  
                 $output .= '</tr>';  
         }  
271    
272          $output .= '</table>';    $output .= '</table>';
273          $output .= '<div class="hidden-jeopardy-question" id="hidden-jeopardy"></div>';    $output .= '<div class="hidden-jeopardy-question" id="hidden-jeopardy"></div>';
274    
275          $node->content['jeopardy'] = array(    $node->content['jeopardy'] = array(
276                  '#value' => $output,      '#value' => $output,
277                  '#weight' => 1      '#weight' => 1
278          );    );
279          return $node;  
280      //echo '<pre>';
281      //print_r($node);
282      //echo '</pre>';
283    
284      return $node;
285  }  }
286    
287  /**  /**
288   * Implementation of hook_load   * Implementation of hook_load
289   */   */
290  function jeopardy_load(&$node) {  function jeopardy_load(&$node) {
291          $result = db_query('SELECT * FROM {jeopardy_questions} WHERE nid = %d', $node->nid);    $result = db_fetch_array(db_query('SELECT rows, cols FROM {jeopardy_rows_cols} WHERE nid = %d', $node->nid));
292          $result_array = db_query('SELECT rows, cols FROM {jeopardy_questions} WHERE nid = %d', $node->nid);    $rows = $result['rows'];
293          $row = db_fetch_array($result_array);    $cols = $result['cols'];
294          $output = new StdClass;  
295          $rows = $row['rows'];    $additions = new StdClass;
296          $cols = $row['cols'];    $additions->jeopardy_rows = $rows;
297          for ($j=1;$j<=$rows;$j++) {    $additions->jeopardy_cols = $cols;
298                  $c = "jeopardy_category$j";  
299                  for ($k=1;$k<=$cols;$k++) {    $result = db_query('SELECT * FROM {jeopardy_questions} WHERE nid = %d', $node->nid);
300                          $q = "jeopardy_question$j$k";    while($row = db_fetch_array($result)) {
301                          $row = db_fetch_array($result);      $c = "jeopardy_category". $row['row'];
302                          $output->$c = $row['category'];      $q = "jeopardy_question". $row['row'] . $row['col'];
303                          $output->$q = $row['question'];      $a = "jeopardy_answer". $row['row'] . $row['col'];
304                  }  
305          }      $additions->$c = $row['category'];
306          $output->jeopardy_rows = $rows;      $additions->$q = $row['question'];
307          $output->jeopardy_cols = $cols;      $additions->$a = $row['answer'];
308          return $output;    }
309    
310      return $additions;
311  }  }
312    
313  /**  /**
314   * Implementation of hook_insert   * Implementation of hook_insert
315   */   */
316  function jeopardy_insert(&$node) {  function jeopardy_insert(&$node) {
317          $rows = variable_get('number_of_questions', 5);    $rows = $node->jeopardy_rows;
318          $cols = variable_get('number_of_categories', 5);    $cols = $node->jeopardy_cols;
319          $nid = $node->nid;    $nid = $node->nid;
320          for ($j=1;$j<=$rows;$j++) {    for ($j = 1;$j <= $cols; $j++) {
321                  $c = "jeopardy_category$j";      $c = "jeopardy_category$j";
322                  for ($k=1;$k<=$cols;$k++) {      for ($k = 1;$k <= $rows; $k++) {
323                          $q = "jeopardy_question$j$k";        $q = "jeopardy_question$j$k";
324                          db_query("INSERT INTO {jeopardy_questions} (nid, category, question, qid, rows, cols) VALUES (%d,'%s','%s',%d,%d,%d)", $node->nid, $node->$c, $node->$q, intval($nid.$j.$k), $rows, $cols);        $a = "jeopardy_answer$j$k";
325                  }  
326          }        db_query("INSERT INTO {jeopardy_questions} (nid, category, question, answer, row, col) VALUES (%d, '%s', '%s', '%s', %d, %d)", $node->nid, $node->$c, $node->$q, $node->$a, $j, $k);
327        }
328      }
329      db_query("INSERT INTO {jeopardy_rows_cols} (nid, rows, cols) VALUES (%d, %d, %d)", $node->nid, $rows, $cols);
330  }  }
331    
332  /**  /**
333   * Implementation of hook_update   * Implementation of hook_update
334   */   */
335  function jeopardy_update(&$node) {  function jeopardy_update(&$node) {
336          $rows = $node->jeopardy_rows;    $rows = $node->jeopardy_rows;
337          $cols = $node->jeopardy_categoryols;    $cols = $node->jeopardy_cols;
338          $nid = $node->nid;    $nid = $node->nid;
339          for ($j=1;$j<=$rows;$j++) {    for ($j = 1; $j <= $cols; $j++) {
340                  $c = "jeopardy_category$j";      $c = "jeopardy_category$j";
341                  for ($k=1;$k<=$cols;$k++) {      for ($k = 1; $k <= $rows; $k++) {
342                          $q = "jeopardy_question$j$k";        $q = "jeopardy_question$j$k";
343                          db_query("UPDATE {jeopardy_questions} SET category = '%s', question = '%s' WHERE qid = %d", $node->$c, $node->$q, intval($nid.$j.$k));        $a = "jeopardy_answer$j$k";
344                  }  
345          }        db_query("UPDATE {jeopardy_questions} SET category = '%s', question = '%s', answer = '%s' WHERE row = %d AND col = %d", $node->$c, $node->$q, $node->$a, $j, $k);
346        }
347      }
348      db_query("UPDATE {jeopardy_rows_cols} SET rows = %d, cols = %d WHERE nid = %d", $rows, $cols, $nid);
349  }  }
350    
351  /**  /**
352   * Implementation of hook_delete.   * Implementation of hook_delete.
353   */   */
354  function jeopardy_delete(&$node) {  function jeopardy_delete(&$node) {
355          db_query('DELETE FROM {jeopardy_questions} WHERE nid = %d', $node->nid);    db_query('DELETE FROM {jeopardy_questions} WHERE nid = %d', $node->nid);
356      db_query('DELETE FROM {jeopardy_rows_cols} WHERE nid = %d', $node->nid);
357  }  }
358  ?>  

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

  ViewVC Help
Powered by ViewVC 1.1.2