Friday, 13 January 2012

the first time login in drupal

hi

it certainly look pretty simple to detect the first time login event using hook_user to check for "login" operation and check the users object "$user->login" value.

code:


 <?  
  function mycustom_user($op,&$edit,&$account,$category = NULL){  
 if($op == 'login'){  
 if($account->login == 0){ // check the last login  
 //do stuff here  
 }  
 }  
 }  
 ?>  



this work just fine if you want to impletement a redirection once the user login for the first time.

but what if you have to check users first time login on search page or user profile page.in that particular condition the above code does not work. because the
the value of user->login will be updated as soon as user login.

there is a simple module which manage the user Statistics called user_stats 
it manage the user login count which comes in handy here.

here is the code to get the login count from user_stats

 <?php  
  global $user  
 if(user_stats_get_stats('login_count',$user->uid) == 1){ //check that its user first time login  
 drupal_set_message("some message");  
 //do the stuff  
 }  
 ?>  

 
well you can use this module in many way for getting reg date much more..

well this come in very handy to solve my problem.






Monday, 3 October 2011

simple drupal "hello world" block custom modules


hi

this is just a really fast and crude way to printout the content in block.by creating a drupal custom modules

 module name "hello_world"

first step is to create a "info" file for your modules "hello_world.info"

and paste in the before code


 ; $Id$  
 name = hello_world  
 description = Custom functions for this site.  
 core = 6.x  




next step is to create a module file this is the file where all the logic goes
create a file "hello_world.module" and paste in the below code

 <?php  
 // $Id: hello_world.module  
 /**  
 * @file* Custom functions for this site.  
 */  
  //implementing the hook_block functions   
 function hello_world_block($op = 'list',$delta = 0, $edit =array()){  
 if($op == 'list'){ //listing the blocks  
 $blocks[0]=array(  
 'info'=> t('hello world block'),  
 'weight' => 0,  
 );  
 return $blocks;  
 }  
 //loading the content of the blocks   
 if($op == 'view'){  
 if($delta == 0){  
 $blocks = array(  
 'subject' => t('hello world blocks'),  
 'content' => '<h1>hello world</h1>';// alternatively you can call a function which return the data  
 );  
 }  
 return $blocks;  
 }  





this above code can be used to quickly create block to display a banner or ad-script in a block or a even iframe.

Sunday, 2 October 2011

author information in a drupal block



hi

here is simple php snippet used to show the author picture and name in a block .

ok this is how this works it collect the node-nid from the url. load's the node get the author uid and then load the user and print the information about the user


 <?php  
 //url example http://domain.com/node/{nid}  
 if(isnumeric(arg(1)){ //  
 $node=node_load(arg(1)); // using the node load function  
 if($node){ // checking whether node object is loaded  
 $author = user_load($node->uid);//using the user load function to load the user object  
 echo '<div>';  
 echo '<img src="'.$author->picture.'" /><br/>'; // print the path to the user iamge  
 echo '<b>'.$author->name.'</b><br/>'; //print the user name  
 echo '<b>'.$author->mail.'</b><br/>'; // print the email address of the user  
 echo '</div>';  
 }  
 }  

this might be very crude way of doing this. alternatively you can create view to do the same

Thursday, 18 August 2011

php code to redirect the users according to roles in drupal

hi


this is a  simple and crude php code to redirect the user according to roles in drupal

this can be used in drupal hook_user function or panels pages to redirect the user





 <?php  
 global $user;  
 //consider two roles moderator and regular user  
 //moderator role rid is 2  
 //regular user role rid is 4  
 if($user){  
 //roles item is array  
 if($user->roles[2]){// if user has a role moderator  
 //do this  
 drupal_goto("user/moderater-panel");// redirecting  
 }elseif ($user->roles[4]){ // user user have role regular user  
 drupal_goto("user/regular-panel");// redirecting  
 }else{ // if the user have other roles then above roles do this  
 //do smothing  
 }  
 }  

Monday, 7 March 2011

a simple perl timer scripts


hi

here is some simple code to shutdown the computer at a particular time.you probably may have more better script then this. or even write simple shell script. but here is what i use.


 $cont = "075500"; // time at which the computer must halt 07:55:00  
 $var = `date +%H%M%S`;//initializing the $var and getting the current time from the shell command and storing the value in $var  
 while(1)  
 {  
 if($var == $cont) //checking the values  
 {  
 `init 0`//signal command to shut down the computer.  
 `exit`  
 }  
 else  
 {  
 `sleep 1`;  
 $var = `date +%H%M%S`;//after every second getting the current time and matching the value in a infinite while loop.  
 }  
 }  

as halt command need super user privilege. you need to run the script as root or sudoer 

Thursday, 3 March 2011

getting a mutual friend list using custom sql query in drupal

hi

i am using drupal UR module to implement the friends relations.below sql query is used to display mutual friend list. create custom module use this sql query and get the mutual friend list.


(SELECT user_relationships.rid AS rid, users_user_relationships.name AS users_user_relationships_name, users_user_relationships.uid AS users_user_relationships_uid, user_relationship_types_user_relationships.plural_name AS user_relationship_types_user_relationships_plural_name FROM user_relationships user_relationships LEFT JOIN users users_user_relationships ON user_relationships.requestee_id = users_user_relationships.uid LEFT JOIN users users_user_relationships_1 ON user_relationships.requester_id = users_user_relationships_1.uid LEFT JOIN user_relationship_types user_relationship_types_user_relationships ON user_relationships.rtid = user_relationship_types_user_relationships.rtid WHERE (user_relationships.approved = '$user->uid') AND (user_relationships.requester_id = arg(1)) OR (user_relationships.requester_id = build) GROUP BY users_user_relationships_uid HAVING count(*) = '2' ORDER BY user_relationship_types_user_relationships_plural_name ASC )