All pastes #920476 Raw Edit

saikat

public text v1 · immutable
#920476 ·published 2008-02-27 13:09 UTC
rendered paste body
<?php
//$Id: annotate.module $
	/**
	*@file
	*Lets users add private annotations to nodes.
	*Adds a text field when a node is displayed
	*so that authenticated uesrs may make notes
	*/
	
	/**
	* Implementation of hook_menu()
	*/
	
	function annotate_menu()
	{
		$items=array();
		$items['admin/annotate']=array(
			'title'=>t('Node Annotation'),
			'description'=>t('Adjust node annotation options'),
			'position'=>'right',
			'page callback'=>'system_admin_menu_block_page',				
			'access callback'=>'user_access',
			'access arguments'=>array('administer site configuration')
		);
		
		$items['admin/settings/annotate']=array(
			'title'=>t('Annotation Settings'),
			'description'=>t('Changes how annotation behave'),
			'page callback'=>'drupal_get_form',			
			'page arguments'=>array('annotate_admin_settings'),	
			'access callback'=>'user_access',
			'access arguments'=>array('administer site configuration')
		);
		
		return $items;
	}
	
	/**
	*define the settings form
	*/
	
	function annotate_admin_settings()
	{
		//setting the nodes which can use annotation
		
		$form['annotate_nodetypes']=array(
			'#type'=>'checkboxes',
			'#title'=>t('Users may annotate these node types'),
			'#description'=>t('A text field will appear to write down note about these node annotation'),
			'#options'=>node_get_types('names',NULL,FALSE),
			'#default_value'=>variable_get('annotate_nodetypes',array('story'))
		);
		
		//preparing the nodes for deleting the annotation
		
		$form['annotate_deletion']=array(
			'#type'=>'radios',
			'#title'=>t('Annotation will be deleted'),
			'#description'=>t('Select a method for deleting annotations'),
			'#options'=>array(
				0=>t('Never'),1=>t('Randomly'),2=>t('After 30 Days')
			),
			'#default_value'=>variable_get('annotate_deletion',0)
		);
		
		//setting the annotation limit per node
		
		$form['annotate_limit_per_node']=array(
			'#type'=>'textfield',
			'#title'=>t('Annotation per node'),
			'#description'=>t('Enetr the maximum no of annotation displayed in a node at a time (0 for no limit).'),
			
			'#default_value'=>variable_get('annotate_limit_per_node',1),
			'#size'=>3
		);

		//validation of settings form

		$form['validate']=array(
			'annotate_admin_settings_validate'=>array()
		);
		
		//hidden form to pass the selected values of the settings
				
		$form['array_filter']=array('#type'=>'hidden');
		
		return system_settings_form($form);
	}
	
	/**
	*validate the settings form
	*/
	
	function annotate_admin_settings_validate($form_id,$form_values)
	{
		if(!is_numeric($form_values['annotate_limit_per_node']))
		{
			form_set_error('annotate_limit_per_node',t('Please enter a number'));
		}
	}
	
	/**
	*Implementaion of hook_nodeapi()
	*/
	
	function annotate_nodeapi(&$node,$op,$teaser,$page)
	{
		switch($op)
		{
			case 'view':
				global $user;
				
				//if only the node summary is being displayed or if the 
				//user is an anonymous user (not logged in), abort.
				
				if($teaser || $user->uid==0)
				{
					break;
				}
				$types_to_annotate=variable_get('annotate_nodetypes',array('story'));
				if(!in_array($node->type,$types_to_annotate))
				{
					break;
				}
				
				//get previously saved note, if any
				
				$result=db_query("SELECT note FROM {annotation} WHERE uid=%d AND nid=%d",$user->uid,$node->nid);
				$node->annotation=db_result($result);
				
				//add our form as a content item
				
				$node->content['annotation form']=array(
					'#value'=>drupal_get_form('annotate_entry_form',$node),
					'#weight'=>10
				);
		}
	}
	
	/**
	*Define the form for entering an annotation
	*/
	
	function annotate_entry_form($node)
	{
	
		$form['annotate']=array(
			'#type'=>'fieldset',
			'#title'=>t('Annotations')
		);
		$form['annotate']['nid']=array(
			'#type'=>'value',
			'#value'=>$node->nid
		);
		$form['annotate']['note']=array(
			'#type'=>'textarea',
			'#title'=>t('Notes'),
			'#default_value'=>$node->annotation,
			'#description'=>t('make your personal annotations here which can be viewed only by you and the administrator')
		);
		$form['annotate']['submit']=array(
			'#type'=>'submit',
			'#value'=>t('Update'),
		);
		$form['#submit']=array('annotate_entry_form_submit');
		return $form;
	}
	
	function annotate_entry_form_submit($form_id,$form_values)
	{
		global $user;
		//echo "";
		$nid=$form_values["value"]["nid"];
		$note=$form_values["default_values"]["note"];
		/*echo "<pre>";
		print_r($from_value);
		echo "</pre>";
		*/
		//watchdog
		db_query("DELETE FROM {annotation} WHERE uid=%d and nid=%d",$user->uid,$nid);
		db_query("INSERT INTO {annotation} (uid,nid,note,timestamp) VALUES (%d,%d,'%s',%d)",$user->uid,$nid,$note,time());
	}
	
?>