Chapter 10. Personalization

Table of Contents
Personalization
Usage examples
Case: Storing time of last visit
Setting the timestamp
Showing last visit information
Showing updated news items

Personalization

Personalization is a great service for your users that enables them to make your Web site behave the way they want it to.

They can choose what they want to see, great being Yahoo's My Yahoo service. If your user is interested in stock quotes, weather, news or whatever you can imagine, user can see only what's he's interested in.

You can easily make personalizable Web sites by using Midgard's parameter functions. Midgard stores its parameters into a parameter table containing name and value pairs, ordered by parameter domains. There are two object methods you can use for accessing this data.

Usage examples

Note: The examples assume that $midgard->user is set, in other words the user is authenticated, either through an HTTP challenge, or based upon a cookie through use of the mgd_auth_midgard() function.

This is an example for attaching a parameter to a user record:


<a href="&(midgard.uri);&(pref);">Show on the front page</a>
<?php
  if (pref) {
    $value = 1;
    $user=mgd_get_person($midgard->user);
    $user->parameter('mysite', 'pref', $value);
  }
?>

The first line is making a link for preference on the site, when a visitor clicks it, pref will be set, causing creation of a new parameter pref in parameter-domain mysite. The value of pref is set to 1.

The next example shows the same parameter:


<?php
  $user=mgd_get_person($midgard->user);
  $pref = $user->parameter('mysite', pref);

  if ($pref == 1) {
    echo 'Will say Hello world';
  } else {
    echo "Don't say anything";
  }
?>

It first checks if parameter pref in domain mysite is set to 1, and then takes some action based on the result.

The third example will delete a parameter:


<?php
  $user=mgd_get_person($midgard->user);
  $user->parameter('mysite','pref','');
?>

This shows that by specifying an empty string for a parameter, it will be deleted.

Example for updating a parameter:


<?php
  $user=mgd_get_person($midgard->user);
  $user->parameter('mysite','pref','0');
?>

showing that there is no syntactical difference in creating a parameter for the first time, or updating it to a new value.

Case: Storing time of last visit

It is often useful to know when a particular user has last visited a site or a particular page. This can be used to monitor usage patterns, and to alert the users on new or updated content.

Since this is relatively easy to achieve using the Midgard parameters mechanism, that is what we're using here. The example assumes that you are monitoring visits on a news page, and want to show what articles have been created since the last visit.

Setting the timestamp

The first task on generating the page is to check whether the user has visited the page before, and so whether he has the timestamp parameter set.

If the user doesn't have the timestamp from last visit set, we assume that she is a new user, and so set the timestamp to current date to avoid showing needless "NEW article" clutter.

After this, we fetch the last visit's timestamp to the $lastvisit variable, and replace the parameter with the current timestamp.


<?php

  /* Get current time as UNIX timestamp */
  $timestamp = time();

  /* Check for authenticated user */
  if ($midgard->user) {
    $user=mgd_get_person($midgard->user);
    /* Get timestamp from last visit */
    $lastvisit = $user->parameter('mysite', 'lastvisit');

    if (!$lastvisit) {
      /* If it doesn't exist, create it */
      $user->parameter('mysite', 'lastvisit', $timestamp);

      /* User hasn't visited the page before,
       so all content is new to her. No need
       to clutter the page with needless NEW
       markings. */
      $lastvisit = $timestamp;

    } else {
      /* Fetch the timestamp from last visit to the
       $lastvisit variable. */
      $lastvisit = $user->parameter('mysite', 'lastvisit');

      /* Update the parameter to include current timestamp */
      $user->parameter('mysite', 'lastvisit', $timestamp);

    }
  }
?>

Showing last visit information

Now that we know the time of user's last visit, we can welcome her back to the site.

We also know if the user is a new one (because the last visit timestamp is the current time), and can present a different welcome message.

Please note that we are slightly more careful than needed by checking for authenticated user. This is because I wanted the example to be also applicable for sites that support both registered and unregistered users.


<?php

  /* Check for authenticated user */
  if ($midgard->user) {

    /* Create a Person object with the user's
       information */
    $user = mgd_get_person($midgard->user);

    if ($timestamp > $lastvisit) {
      /* This user has been here before */
  
      /* Format the $lastvisit string for viewing */
      $visitstring = date("D F jS Y H:i:s", $lastvisit);

      ?>
         <h2>Welcome to my site,
                &(user.name);!</h2>

         <p>
            Last visit to My Site: &(visitstring);
         </p>
      <?php
    }
    elseif ($timestamp == $lastvisit) {
      /* This is a new user! */
      ?>
         <h2>Welcome to my site,
                &(user.name);!</h2>

         <p>
          This is a very nice Web site that contains
          lots of useful information. Enjoy!
         </p>

         <p>
          Please note that we have stored the time of
          your current visit to our user database. We
          will use this information for enhancing our
          services by showing you what information has
          been updated.
         </p>
      <?php
    }
  }

?>

Showing updated news items

The next thing to do then is to add some intelligence to the site's newsfeed. Midgard stores timestamps to Article records every time they are updated, and so we can know which of them have been created or updated after the user's previous visit.

The article fields we're interested in here are revised and created. These variables contain information on last article revision, and the original revision date, both in UNIX timestamp format. Please read the function reference of mgd_get_article() for more information on article fields.

We're showing six latest articles from topic 10 (which would correspond to 'Latest announcements' in our example here).


<h2>Latest news</h2>

<?php

  /* Set the topic to use */
  $news_topic = 10;

  /* Set number of articles to retrieve */
  $number = 6;

  /* Return a list of articles latest first */
  $article = mgd_list_topic_articles($news_topic, 'reverse created');

  /* If we got articles */
  if ($article) {

    ?>
       <ul>
    <?php

    /* Fetch the six latest ones */
    while ($number-- && $article->fetch()) {

      /* Do for each article fetched */

      ?>
        <li>
      <?php

      /* Check for authenticated user */
      if ($midgard->user) {

        /* If the article has been created after last visit */
        if ($article->created > $lastvisit) { ?>

          <strong class="red">NEW</strong>

        <?php }

        /* If the article has been updated after last visit */
        elseif ($article->revised > $lastvisit) { ?>

          <strong class="green">UPDATED</strong>

        <?php }

      }

      /* Then just display the headline */
      ?>
        <a href="/path/to/news/&(article.id);.html"
           >&(article.title);</a>
             (&(article.adate);)<br />
           >&(article.title);</a>
             (&(article.adate);)<br />
             &(article.abstract);</li>
      <?php

    }

    ?>
      </ul>
    <?php

  }
?>