Madison Drupal Users Group -- October

Events happening in the community are now at Drupal community events on www.drupal.org.
blakehall's picture
Start: 
2009-10-01 18:00 - 20:00 America/Chicago
Organizers: 
Event type: 
User group meeting

Where: The 2nd floor conference room at DANEnet. 517 N Segoe.

This month we'll have our usual Q&A / Tips & Tricks sharing from 6-7.

From 7-8 we'll talk about writing a custom module (accessing the database, altering a form, adding permissions, etc).

If anyone has any specific ideas for a module we can write during the meeting, please leave a comment below.

Comments

writing custom modules

greenlayk's picture

Given a database of student, info (e.g. applications, grades, courses taken), I'd like to see how to create a module that allows a logged in student to access the database but see only their personal info.
I've already written a module that allows editors to search the database for any student's info. Setting permissions to restrict access for student user types is my next step.

October Meetup

greenlayk's picture

See you Thursday.

custom uc_profile module

jday's picture

I have a module that adds a checkout pane to an ubercart store site and it works great for anonymous users or user 1 but not for authenticated users.
The premise: a percentage of the purchase price is donated to an organization of choice, this organization is a select option in the user profile. The module will display the dropdown during checkout for anonymous users and save the selection to their profile. For authenticated users who've already made a selection it will just display the selection (pulled from their profile) on the checkout page.

So the value is pulled correctly for user 1 but when logged in as any other user, clicking the checkout button will take you to your profile page. If anyone is interested in reviewing the code let me know.

Here is the module from tonight

posco's picture

So I finished the module. Here it is.

I wanted to put this up on bespin.mozilla.org and share it, because I thought it would be cool. But, since I do want to get to bed tonight, I'll just paste it into here:

view_students.info:

; $Id$
name = View Student
description = "Allows a student to view just their information out of a database."

; Core version (required)
core = 6.x

; Package name (see http://drupal.org/node/101009 for a list of names)
package = Students

; PHP version requirement (optional)
; php = 5.2

; Module dependencies
; dependencies[] = mymodule
; dependencies[] = theirmodule


; For further information about configuration options, see
; - http://drupal.org/node/231036

view_students.module:

<?php
// $Id$

/<em>
* @
file
* Sample file of a small module that allows a user to view information that
* pretains only to them. (We are using a fake table in this madule that
* doesn't exist).
*/

/</em>
* Implementation of hook_perm().
<em>/
function view_students_perm() {
  // hook_perm() allows us to define our own permissions for this module that
  // will show up on admin/user/permissions under the view_students table.
  return array('
view own student information');
}

/</em>
* Implementation of hook_menu().
<em>/
function view_students_menu() {
  $items = array();
 
  // This is a basic menu item for Drupal. It maps the URL path
  // '
view_students/lookup' to the function view_students_lookup().
  $items['
view_students/lookup'] = array(
    '
page callback' => 'view_students_lookup',
    '
access arguments' => array('view own student information'),
  );
 
  return $items;
}

/</em>
* Our custom page callback that was defined in hook_menu(). This needs to
* return HTML that will be displayed in the $content variable in page.tpl.php.
*
* @return HTML for view_students/lookup.
*/
function view_students_lookup() {
  // This brings the current user object from global scope into our
  // function. This represents the user that is viewing the current site.
  // This is not a full user object, you need user_load($user->uid) for a full
  // user object.
  global $user;
 
  // This is not really needed since we have access arguments set in hook_menu, but just to be
  // paranoid let'
s put this here to prevent user 0 access.
  if (
$user->uid) {
   
// Here we use Drupal's database abstraction layer. Putting the table name in braces
    // is a Drupal-ism. It is used because table names can be prefixed when you install
    // Drupal. Drupal will replace the table name in the braces with the prefixed
    // table name. The braces are optional, but it is considered good style in
    // the Drupal community.
    //
    // The second Drupal-ism in this line is the %d. If you have ever used printf(), you
    // will notice that these look like format strings. In fact, they are used the same
    // way. The only difference is that Drupal will sanitize the variables before
    // the format strings are replaced with their arguments. This prevents SQL
    // injection attacks and is one reason Drupal is such a secure CMS. Under no
    // circumstance should you ever not use the format strings with db_query().
    // Look up db_query() on api.drupal.org for more info.
   
$query = db_query("SELECT data FROM {students} WHERE uid = %d", $user->uid);

   
// Here we procsses the information from the database in a loop. We are going
    // to output our info in a table, so we setup the $table_rows needed by
    // theme('table');
   
$table_rows = array();
    while (
$result = db_fetch_object($query)) {
     
$table_rows[] = $result->data;
    }
   
   
$table_header = array(t('Your Data'));
   
// Here is the end of our function. It is one call to a theme
    // function. This is how your page callbacks should always end.
    // You always want the theme() functions to produce all the HTML
    // for you. This makes your code cleaner, and it gives your themers
    // a chance to override this theme function later on so they can
    // make it look exactly how they want. You can also write all
    // the HTML in string and do things manually, but this is really frowned
    // upon and you aren't taking advantage of the Drupal framework
    // by doing so.
   
return theme('table', $table_header, $table_rows);
  }
}
?>

Sleep is for losers

posco's picture

OK, so the PHP highlighter really isn't making me happy. If you want to get the above source code on http://bespin.mozilla.com/, follow these steps:

  1. Make an account on bespin.mozilla.com.
  2. Open the console and type follow posco2k8
  3. Open your file browser in Bespin and you should see the view_students project.

There you go! It is world readable and writable, so everyone can hack away at it if they want.

losing sleep over views_students

greenlayk's picture

Hey there,
Thanks a bunch for doing this, I appreciate all the comments!

Wisconsin

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: