<?php
/**
* @file
* Enables users to submit jeopardy games..
*/

/** 
* Get Drupal information
*/

define('JEOPARDY_PATH', drupal_get_path('module', 'jeopardy'));

/**
* Implementation of hook_help().
*/
function jeopardy_help($section) {
	switch ($section) {
		case 'admin/help#jeopardy':
			$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 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 .= 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 .= '<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>';
			return $output;
		case 'admin/modules#description':
			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.');
		case 'node/add#jeopardy':
			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.');
	}
}

/**
* Implementation of hook_node_info().
*/
function jeopardy_node_info() {
  return array(
			'jeopardy' => array(
				'name' => t('Jeopardy'),
				'module' => 'jeopardy',
				'base' => 'jeopardy',
				'description' => t('Create jeopardy games.')
			)
		);
}

/**
* Implementation of hook_perm().
*/
function jeopardy_perm() {
	return array('create jeopardy games.', 'edit own jeopardy games.', 'view jeopardy game.');
}

/**
* Implementation of hook_access().
*/
function jeopardy_access($op, $node) {
	global $user;

	if ($op == 'create') {
		return user_access('create jeopardy games.');
	}

	if ($op == 'update' || $op == 'delete') {
		if (user_access('edit own jeopardy games.') && ($user->uid == $node->uid)) {
			return TRUE;
		}
	}
	
	if ($op == 'view') {
		if (user_access('view jeopardy games.')) {
			return TRUE;
		}
	}
}

/**
* Implementation of hook_menu().
*/
function jeopardy_menu($may_cache) {
	$items = array();

	if ($may_cache) {
		$items[] = array(
		    'path' => 'node/add/jeopardy', 
		    'title' => t('Jeopardy'),
		    '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;
}

/**
* Implementation of hook_form().
*/
function jeopardy_form(&$node) {
	$rows = variable_get('number_of_questions', 5);
	$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;
}

/**
 * Displays and allows an administrator to change the settings for this module.
 */
function jeopardy_settings() {
	return drupal_get_form('jeopardy_settings_form');
}

function jeopardy_settings_form() {
    $form['number_of_questions'] = array(
      '#type' => 'textfield',
      '#title' => t('Number of questions per category'),
      '#default_value' =>  variable_get('number_of_questions', 5),
      '#description' => t('This sets the number of questions in each category of a Jeopardy game.'),
      '#required' => TRUE);

    $form['number_of_categories'] = array(
      '#type' => 'textfield', 
      '#title' => t('Number of Categories'),
      '#default_value' => variable_get('number_of_categories', 5),
      '#description' => t('This sets how many categories should appear in each Jeopardy game.'),
      '#required' => TRUE
	);
	  
	return system_settings_form($form);	
}

/* 
* implementation of Drupal's theme functions
*/
function jeopardy_view(&$node, $teaser = FALSE, $page = FALSE) {
	drupal_add_css(JEOPARDY_PATH .'/jeopardy.css');
	drupal_add_js(JEOPARDY_PATH .'/jeopardy.js', 'module');
	$output = '';
	
	$output .= '<table class="jeopardy-table">';
	
	$rows = $node->jeopardy_rows;
	$cols = $node->jeopardy_cols;
	
	
	$output .= '<tr class="jeopardy-table-header-row">';
	for ($j=1;$j<=$cols;$j++) {
		$c = "jeopardy_category$j";
		$category = $node->$c;
		$output .= '<th class="jeopardy-table-header">'.$category.'</th>';
	}
	
	$output .= '</tr>';
	for ($j=1;$j<=$rows;$j++) {
		$output .= '<tr class="jeopardy-table-question-row">';
		for ($k=1;$k<=$cols;$k++) {
			$q = "jeopardy_question$j$k";
			$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>';
	}
	
	$output .= '</table>';
	$output .= '<div class="hidden-jeopardy-question" id="hidden-jeopardy"></div>';
		
	$node->content['jeopardy'] = array(
		'#value' => $output,
		'#weight' => 1
	);
	return $node;
	
}

/**
 * Implementation of hook_load
 */
function jeopardy_load(&$node) {
	$result = db_query('SELECT * FROM {jeopardy_questions} WHERE nid = %d', $node->nid);
	$result_array = db_query('SELECT rows, cols FROM {jeopardy_questions} WHERE nid = %d', $node->nid);
	$row = db_fetch_array($result_array);
	$output = new StdClass;
	$rows = $row['rows'];
	$cols = $row['cols'];
	for ($j=1;$j<=$rows;$j++) {
		$c = "jeopardy_category$j";
		for ($k=1;$k<=$cols;$k++) {
			$q = "jeopardy_question$j$k";
			$row = db_fetch_array($result);
			$output->$c = $row['category'];
			$output->$q = $row['question'];
		}
	}
	$output->jeopardy_rows = $rows;
	$output->jeopardy_cols = $cols;
	return $output;
}

/**
 * Implementation of hook_insert
 */
function jeopardy_insert(&$node) {
	$rows = variable_get('number_of_questions', 5);
	$cols = variable_get('number_of_categories', 5);
	$nid = $node->nid;
	for ($j=1;$j<=$rows;$j++) {
		$c = "jeopardy_category$j";
		for ($k=1;$k<=$cols;$k++) {
			$q = "jeopardy_question$j$k";
			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);
		}
	} 
}

/**
 * Implementation of hook_update
 */
function jeopardy_update(&$node) {
	$rows = $node->jeopardy_rows;
	$cols = $node->jeopardy_categoryols;
	$nid = $node->nid;
	for ($j=1;$j<=$rows;$j++) {
		$c = "jeopardy_category$j";
		for ($k=1;$k<=$cols;$k++) {
			$q = "jeopardy_question$j$k";
			db_query("UPDATE {jeopardy_questions} SET category = '%s', question = '%s' WHERE qid = %d", $node->$c, $node->$q, intval($nid.$j.$k));
		}
	} 
}

/**
 * Implementation of hook_delete.
 */
function jeopardy_delete(&$node) {
	db_query('DELETE FROM {jeopardy_questions} WHERE nid = %d', $node->nid);

}
?>
