Enforce bio creation

jscheel - July 25, 2007 - 22:16
Project:Bio
Version:5.x-1.x-dev
Component:Code
Category:feature request
Priority:normal
Assigned:jscheel
Status:duplicate
Description

I have attached a patch that adds the option to force users to create a bio before continuing through the site. It only applies to uid > 1, and drops a 303 (see also) redirect to node/add/bio (or whatever content type is chosen).

P.S. Sorry if the patch is a little janky. I've never really patched anything for contrib.

AttachmentSize
bio_module_enforcer.patch2.24 KB

#1

marcp - July 26, 2007 - 16:06
Status:needs review» needs work

In bio_init() you're duplicating a good chunk of what the function bio_for_user() does. You can just call that and see if it returns FALSE to check if the user hasn't yet created their bio.

This is a great idea, by the way.

#2

jscheel - July 26, 2007 - 21:59
Status:needs work» needs review

Good catch! I need to look through the code a little better, eh? I've made the change. I've also wrapped the message string in t() for proper text handling.

AttachmentSize
bio_module_enforcer2.patch 1.97 KB

#3

marcp - August 9, 2007 - 01:15
Status:needs review» needs work

It looks like you're using $may_cache without having intialized it. It would also be great to run your code through the Coder module, which will help you get the code better "Drupalized."

I haven't thought too much about it, but is hook_init() the best place to do this?

#4

dldege - September 10, 2007 - 19:09

This might be easier - its what I do in my own code at the moment and doesn't require any menu hooking. It simply keeps redirecting the user to user/N/bio until they have submitted it once (ie. bio node exists for the user). The only valid url until this point is q=logout.

<?php
function site_init() {
  global
$user;
  if (
$user->uid > 1 && strcmp('logout', $_GET['q'])) {
     
// Make sure the user creates a bio
     
if (strcmp('user/' . $user->uid . '/bio', $_GET['q'])) {
       
site_verify_user_bio($user);
    }
  }
}

function
site_verify_user_bio($user) {
 
$bio = bio_for_user($user->uid);
  if (!
$bio) {
   
drupal_goto('user/' . $user->uid . '/bio'); 
  }
}
?>

#5

Rob Loach - September 11, 2007 - 04:19
Status:needs work» needs review

Here's a re-work using dldege's solution. It gives you an option in admin/user/bio to turn on Required, which will make the user have to create a Bio node before using the rest of the site.

AttachmentSize
bio.module.biorequired.patch 1.41 KB

#6

Rob Loach - September 11, 2007 - 04:21
Status:needs review» needs work

Now that I look at it, this is pretty much the same as jscheel's solution. I think it would be good to move it to hook_menu, using !$may_cache in there.

#7

dldege - September 11, 2007 - 14:08

Yeah, they are very similiar and it looks like either version will work. hook_init happens a lot earlier in the page request lifecycle which might be the only advantage of doing it there - no need to deserialize and build the menus, etc. I just offered it as another approach - I do think its a feature worth adding though.

Thanks for adding the settings form handling.

#8

Rob Loach - September 11, 2007 - 17:15
Version:5.x-1.0» 5.x-1.x-dev
Status:needs work» needs review

Re-factored with all the above provided solutions, and in hook_menu instead of hook_init. It also displays the Biography node type name instead of just "bio" when asking the user to create a bio node.

AttachmentSize
bio.module.required.patch 1.98 KB

#9

Rob_Feature - October 3, 2007 - 20:44
Status:needs review» reviewed & tested by the community

Installed and works perfectly as advertised. I'd suggest this is ready to commit...and it's a beautiful feature as well.

#10

SamStealth - October 9, 2007 - 22:19

i tried out your latest patch and it works like a charm! great work!

#11

SamStealth - October 10, 2007 - 23:21

it took me ages to get moofie's locationfields to work with 'enforce bio creation'.
somehow dependent dropdowns want to fetch data from 'locationfields/ajax/bio' ... and drupal_goto bio node broke it.
finally i found a one-line solution:

            if (strcmp($_GET['q'],'locationfields')<=0)
                drupal_goto('user/' . $user->uid . '/bio');

maybe it's good to place a 'do not redirect on following pages...' option on config screen.

#12

SamStealth - October 11, 2007 - 00:02

FYI
i noticed, that - if clicking around on my ajax empowered form - more and more error messages are accumulated,
so here is a slight improvement to my previous post:

            if (!preg_match('/^locationfields/i', $_GET['q']))
            {
                drupal_set_message(t('Please create a %bio before continuing.', array('%bio' => $biotypename)), 'error');
                drupal_goto('user/' . $user->uid . '/bio');
            }

#13

jcruz - October 11, 2007 - 06:20

subscribing

#14

Rob Loach - October 15, 2007 - 22:58

SamStealth: Might be better to have it do something similar to the wildcard solution in admin/content/block where you type in what pages are exceptions. In this case, these would be pages where users would be allowed to visit without requiring the Bio node.

#15

jjeff - October 16, 2007 - 13:53

Re: Wildcard solution: Good idea!

I'm working up a new version of the module right now and I'll try to roll this in. However, if anyone else has any interest/time for this, please post here..

#16

jscheel - October 23, 2007 - 16:06

Sorry, I've been kindof awol on this issue. I can write up something to integrate wildcards. Just let me know.

#17

Caleb G2 - October 26, 2007 - 18:26
Status:reviewed & tested by the community» needs review

After applying this patch the following message was always returned when I went to actually submit the bio-node form:

"This user already has a bio. Edit it here or assign this entry to another user."

After a little tinkering around I figure out that just pulling bio_nodeapi() seemed to have no obvious adverse affects. The problem is gone, and I don't get the message anymore, so I guess it was left over cruft??

In any case here is a new patch with bio_nodeapi() taken out.

AttachmentSize
bio.module.required_0.patch 2.71 KB

#18

DesignWork - October 30, 2007 - 07:36

Hi all,

I did a version of the biomodule automatically created for each user, were you can rename the Tab via admin settings etc.
someone want to test it? More explainantions you will find here http://drupal.org/node/184868.

I think this could be nice to have in bio as standart. But you have to see the code first. It works only for 5.x

Dirk

AttachmentSize
user_tab.txt 7.44 KB

#19

Caleb G2 - October 30, 2007 - 15:24

DesignWork, it's great that you're trying to get involved with Drupal and making contributions back to the project, but please realize that you are spamming an active, and unrelated, thread. I've seen your comments about this on another thread somewhere on the site, and I can't remember if anyone there suggested it or not (I believe someone did) -- you should file a new issue to engage the bio maintainers about the possibility of integrating your changes, and/or get your own cvs account and make a separate project. Those are generally the only ways new code gets notice or accepted.

#20

Michael Hofmockel - November 11, 2007 - 03:02

In order to enforce Bio node creation I did the following:

Created a block that is only visible to authenticated user.

Block body =
<?php
if (bio_for_user($user->uid) == false) {
 
$message = 'Please create a biography.';
 
drupal_set_message($message, $type = 'error');
 
drupal_goto('node/add/bio');
}
?>

Show the block only at:
node/add
node/add/book*
node/add/page*
...

Caution! Dont show on "node/add/bio or node/add/*" as that will cause an endless loop.

This way you can use the entire site but you can not create content until you have created a Bio node.
I feel we do need a module based solution but this is an easy and effective solution in the mean time.

#21

amcc - November 21, 2007 - 01:31

I applied this patch: bio.module.required.patch from comment 8 and when i enabled the module i got this error:

warning: array_keys() [function.array-keys]: The first argument should be an array in /Users/macintosh/Sites/monkey/sites/all/modules/bio/bio_views.inc on line 39.

is this something i need to worry about? Thanks for all the hard work guys - and also to jjeff for a great module and amazing and entertaining podcast.

#22

vanko - December 1, 2007 - 23:56

Someone having problems with Imagefield when this patch is applied?

I use bio 1.0 and the latest patch from this thread. My bio content type holds an image. If an user is forced to create bio, he/she is unable to upload a picture. Seems the upload itself passes, but after that the image is not in the needed directory.

I have the feeling that when you press 'Upload' button to insert image, the enforcement is redirecting you again, preventing the processing of the image.

If an image is added to already existing bio node, no problems are found.

#23

Rob Loach - December 2, 2007 - 07:32

vanko: Be sure to be using ImageField 5.x-2.x-dev. I was experiencing this, updated to ImageField 2.x, and the problem seemed to magically go away. Not quite sure what was happening here, but 2.x fixed it. It also comes with some nice AJAX/AHAH magic which will make you very happy.

#24

Rob Loach - December 2, 2007 - 07:34
Status:needs review» duplicate

I believe we'll be moving to this solution as opposed to the drupal_goto solution suggested in this issue. Much cleaner, and most definitely looks nicer.

#25

Zoologico - December 3, 2007 - 15:27

This works well.

In order to enforce Bio node creation I did the following:

Created a block that is only visible to authenticated user.

Block body =

<?php
if (bio_for_user($user->uid) == false) {
 
$message = 'Please create a biography.';
 
drupal_set_message($message, $type = 'error');
 
drupal_goto('node/add/bio');
}
?>

Show the block only at:
node/add
node/add/book*
node/add/page*
...

Caution! Dont show on "node/add/bio or node/add/*" as that will cause an endless loop.

This way you can use the entire site but you can not create content until you have created a Bio node.

I feel we do need a module based solution but this is an easy and effective solution in the mean time.

Thanks, mhofmockel.

#26

vanko - December 2, 2007 - 15:59

Thanks for both of answers!

Unfortunately, imagefield 2.x didn't do any good to me - the upload broke. I got the HTML of altered web page in an error message. I didn't investigate further.

Bio fields on creation sounds nice, very nice! But i got again this problem with images upload :-(. Furthermore, the users are able to delete their own module and after that stay without bio node. Sad.

Will continiue to look for another solution, if you have any ideas - are welcome ;-)

regards,
Vanko

#27

amcc - December 13, 2007 - 13:44

I've used the solution below - it works perfectly. What i'm worried about is the overheads of the code. Whenever a user logs in i want to enforce them to create a bio before they move on if they don't have one already.

Is this going to slow the site down in any significant way for authenticated users as there will be an extra database query on every page (i've included the bio code showing that query below). Or is this so insignificant that i really shouldn't worry about it.

Here is the code that i've put in my block, I'm making the block appear on every page apart from node/add/bio and node/add/*. I'm also only showing the block for authenticated users.

<?php
$bio
bio_for_user();
if (
$bio == FALSE){
   
$message = 'Please create your Profile.';
   
drupal_set_message($message, $type = 'error');
   
drupal_goto('node/add/bio');
}
?>

In an effort to make the code as simple as possible i've taken the $user->uid bit out of the block code as that's already in the bio module function anyway...
<?php
// function called in bio.module
function bio_for_user($uid = NULL){
  if (
is_null($uid)){
    global
$user;
   
$uid = $user->uid;
  }
  return
db_result(db_query("SELECT nid FROM {node} WHERE uid = %d AND type = '%s'", $uid, variable_get('bio_nodetype', 'bio')));
}
?>

#28

Liliplanet - January 19, 2008 - 14:53

subscribe

 
 

Drupal is a registered trademark of Dries Buytaert.