The My Schedule tab on user profiles page in COD

Drupal's COD distribution is a terrific way to get an event website up and running. However there is no way for a registered user to see his personal schedule. He can click the "add to my schedule" button on a session page, but then what?

Add to my schedule

The session is in my schedule. Now what?

 

To create this functionality, I added a tab in the user profile page called "My Schedule" which displays to the user her schedule. I'm adding this user feature to the NYCCamp website. NYCCamp takes place this year from April 10th through April 13th. Registration is currently open. This year we're having the camp at the United Nations. You should definitely stop by. smiley

The only change I made to COD was to add the Room assignment field to the Session content type because I want the Room assignment included in the My Schedule tab. Remember that if there is no data Views will likely not show anything. Before building this view it's a good idea to have Session nodes, Room nodes, and Time slot nodes entered into your system.

 

Add the Room assignment field to the Session content type

 

Create a new view called Personal schedule. The view will show Content of type Session sorted by Newest first. Set the path to user/%/schedule, Display format to Unformatted list of titles (linked). Click Continue & edit.

 

Add new view

 

Relationships

Before we can add more fields we need to add Relationships and Contextual Filters. The My Schedule tab is based on the flag module and we'll add the flag relationships first. Click the word Advanced (in the right column) to open the collapsed column and click Add which is to the right of the word Relationships. Filter on Flags and tick the checkbox for Flags: Node flag. Click Apply (all displays).

 

Add new view

 

Change the Identifier to schedule, check the Session Attending Flag, and change By to Any user. Click Apply (all displays).

 

Add new view

 

Let's add the Flags: User relationship next. Click Add next to Relationships in the Advanced column. Filter on Flags, which is the same as before. Tick the checkbox for Flags: User and Click Apply (all displays). There is only one previous relationship, schedule, and that is what we see in the Relationship option list in this second modal dialog box. Change the Identifier to Schedule user and click Apply (all displays).

 

Add new view

 

The next relationship we'll add will be for Time Slot(s). Click Add next to Relationships in the Advanced column and filter on Entity Reference. Add the word time to the Search field, if you like, and tick the checkbox for Entity Reference: Referenced Entity. Click Apply (all displays).

 

Add new view

 

On the next modal dialog box, Configure Relationship: Entity Reference: Referenced Entity, click Apply (all displays).

The last relationship is to the content author. The content author is the Session presenter. The Session presenter authored the Session node for the Session. Click Add next to Relationships in the Advanced column and filter on Content. Add the word Author to the Search field, if you like, and tick the checkbox for Content: Author. Click Apply (all displays).

 

Add new view

 

The Identifier is author in the Configure Relationship: Content: Author modal dialog box which comes next. Click Apply (all displays).

Contextual Filters

Now that our relationships are are set up, we'll add a Contextual Filter. This time click Add next to Contextual Filters in the Advanced column and filter on Flags. Tick the checkbox for Flags: User uid. Click Apply (all displays). The Relationship options list shows schedule which is what we want. Tick the Provide default value radio button and select from the Type options list User ID from logged in user.

 

Add new view

 

We're not done with this dialog box yet. Scroll down to the When the Filter Value is in the URL or a Default is Provided fieldset and tick the checkbox for Specify validation criteria. For a Validator select PHP Code. For those who are wondering about using PHP Code for a Validator, you're not alone. I'm wondering too. The PHP filter module is not enabled and we add the following code to the PHP validate code text area:

if($GLOBALS['user']->uid == $argument) {
return TRUE;
} elseif((arg(0) == 'user' && is_numeric(arg(1)) && arg(2) == 'schedule')) {
drupal_set_message(t('You may view your schedule only. Schedules are not publicly shared.'), 'warning');
}

Select Show "Page not found" in the Action to take if filter value does not validate options list. Click Apply (all displays).

 

Add new view

 

The validator displays the My Schedule tab for the logged in user only. On another user's profile page, a Page not found is displayed along with the warning message. A better solution may be a redirect back to the user's own schedule tab.

The Fields

Okay, the hard part is over. Let's add the Time Slot field, the Room assignment field, and the User:Name field. I don't think there is a need for screen shots for the fields since I'm including the view file at the end of this post. Remember that Time Slot is a Field collection item, Room assignment is Content, and User: Name is a User field whose Relationship is author.

Now preview the view with a valid user id and you will see correct results.

 

Add new view

 

Page Settings

The middle column of the Views administrative interface is the Page Settings. Let's add to our Page Settings a Menu Tab called My Schedule. Title the Menu tab My Schedule and add it to the Navigation menu.

 

Add new view

 

The menu tab, along with the Path: /user/%/schedule which we set up when we initialized the View is how the My Schedule tab is added to the user profile page.

Continuing on with the Page Settings, let's set access to Role | authenticated user. Setting access may not be necessary after what we set up in the Contextual Filter, but let's set it anyway. And let's not use a pager. Lastly, let's change the Machine Name of our Page to my_schedule. The Machine Name is in the Advanced column. I think changing machine names helps when naming Views templates.

Our Views administrative page is now complete and will look like this:

 

The finished View

 

Theming

We will need one theme template for this View. Add a template called views-view-fields--personal-schedule--my-schedule.tpl.php whose code looks like this:

<div class="personal-schedule-outer">
  <?php foreach ($fields as $id => $field): ?>
    <?php if (!empty($field->separator)): ?>
      <?php print $field->separator; ?>
    <?php endif; ?>
  
    <?php print $field->wrapper_prefix; ?>
      <?php print $field->label_html; ?>
      <?php print $field->content; ?>
     
    <?php print $field->wrapper_suffix; ?>
  <?php endforeach; ?>
    
  <?php print flag_create_link("session_schedule", $row->nid); ?>
</div>

Note that <?php print flag_create_link("session_schedule", $row->nid); ?> outside of the foreach loop. Save the View and clear the cache for the Menu Tab and the new template. When you go to your user profile page there is now a My Schedule tab in the menu that contains your personal schedule! When you go to another user's My Schedule tab, you are presented with Page not found. You can access other tabs belong to another user normally. See slides two and three of the following lightbox for more information.

 

My Schedule

Schedules are not publicly shared.

Users can still interact on other tabs.

Add new comment