Test Exam, Fall 2000

for Web Publishing with Databases, Fall 2000

by Martin Elsman modified for PHP/MySQL and translated into English


Introduction

To ease the administration of reserving and lending out video projectors at an IT University in the Copenhagen area, you have been assigned the task of developing a Web based video projector reservation system.

The personel in the reception at the IT University each day have the need to see who have booked the University's 5 video projectors. The personel also have the need to see which video projectors are available for reservations. In the following, it can be assumed that video projectors are lend out one day at a time. The system should give individual teachers and other employees the possibility of reserving projectors for teaching and research purposes via the Web.

Although administrative features are important for such a reservation system, we shall restrain ourselves not to consider these in what follows.

Problem 1 (15 percent) - HTML

Problem 1.1

Here is a simple "About"-page for the video projector reservation system:

Write the HTML code for presenting the page shown above (the frame is not part of the page). The solution should appear as HTML-code containing both <html> and </html> tags.

The black bar on the page should be implemented using a table of width 100 percent, with background color black (attribute bgcolor=black), and containing one row with two fields:

  1. Left aligned field with bold text "Video Projector Reservation System"
  2. Right aligned field with logo. It can be assumed that the logo is available on the URL-address http://www.itu.dk/logo_black.jpg. In isolation, the logo looks as shown to the right.

Problem 1.2

For this problem you are asked to write HTML-code representing an HTML-form for reserving a projector. The HTML-code should take the form
  <select ...>
    ...
  </select>
and show a SELECT-box with three of the projectors owned by the IT University. (see the image to the right).

For use in the HTML-form, the name for the SELECT-box, must be proj_id and the values for each of the projectors should be the projector number. In this way, the choice with the text "Projector 1" has the value "1", the choice with the text "Projector 2" has the value "2", and so on.

Problem 2 (20 percent) - PHP

Problem 2.1

Show the HTML-code resulting from a call to the PHP-function projector_list, presented below, with the array $pjs given as argument:
  $pjs[0][0] = "4";
  $pjs[0][1] = "Projector 4";
  $pjs[1][0] = "1";
  $pjs[1][1] = "Projector 1";

  function projector_list ( $l ) {
      $res = "<ul>\n";
      for ( $i = 0 ; $i < count($l) ; $i ++ ) {
         $id = $l[$i][0];
	 $text = $l[$i][1];
         $res = $res . " <li> <a href='projector_show.php?id=$id'>$text</a></li>\n";
      }
      $res = $res . "</ul>";
      return $res;
  }

Problem 2.2

Write a PHP-function select_box that returns HTML-code for a SELECT-box with a list of choices. The function should take two arguments, a name for the SELECT-box, to be used for the name-attribute, and an array of projectors as in Problem 2.1. You can use the function projector_list from Problem 2.1 as a template for the solution.

Note: The following call to the function select_box should return the answer to Problem 1.2:

  $pjs[0][0] = "1";
  $pjs[0][1] = "Projector 1";
  $pjs[1][0] = "2";
  $pjs[1][1] = "Projector 2";
  $pjs[2][0] = "3";
  $pjs[2][1] = "Projector 3";
  
  select_box ("proj_id", $pjs);

Problem 2.3

Write a function vp_return_page that uses the built-in function echo to return a page to the Web client (the browser). The function vp_return_page should take two arguments. The first argument is the title for the page (title), which should appear in the <title>...</title> tags in the header for the resulting page. The second argument is the content of the page (body). All pages that are returned using the function vp_return_page should - independent of the arguments - contain the black bar and the email signature, as in Problem 1.1.

Note: The following call to the function vp_return_page should return the page presented in Problem 1:

  vp_return_page ("About", 
    "<h2>About</h2>
    The Video Projector Reservation System allows registered users to 
    make reservations of video projectors at the IT University of Copenhagen.");

Problem 3 (15 percent) - Regular expressions

Problem 3.1

The regular expression [0-9]+ is matched by one or more appearances of the digits 0-9. Write a regular expression that is matched by either
  1. the digit 0
  2. or a digit 1-9 followed by zero or more appearances of digits 0-9.
Thus, the digit sequences "012" and "00" should not match the pattern, whereas "12" and "0" should.

Problem 3.2

Write a regular expression for dates on the form YYYY-MM-DD, where YYYY represents a year (e.g., 2000), MM represents a month (e.g., 05), and DD represents a day (e.g., 12).

Problem 3.3

Write a PHP-function vp_check_date that can determine if a string has the form YYYY-MM-DD, as described in Problem 3.2. If a string, given to the function as argument, has the correct form, the function should return immediately to the point from which the function was called. Otherwise, the function should send an error message to the user's browser, whereafter the script should stop by calling the built-in PHP-function exit.

Use the function vp_return_page from Problem 2.3 to return a message to the user's browser, in case the date is not on the correct form. To check whether the date takes the correct form, use the built-in PHP-function ereg.

Problem 4 (20 percent) - SQL

The data model for the video projector reservation system should consist of three tables:
  1. vp_person - contains information about persons who can reserve projectors
  2. vp_projector - contains information about projectors
  3. vp_reservation - links persons to projectors for individual days
The tables vp_person and vp_projector are constructed using the following MySQL SQL commands:
  create table vp_person (
    id integer auto_increment primary key,
    name varchar(100) not null,
    email varchar(100) not null,
    password varchar(100) not null
  );

  create table vp_projector (
    id integer auto_increment primary key,
    name varchar(100)
  );
It can be assumed that the following SQL insert commands have been executed:
  insert into vp_person (id, name, email, password)
  values (1, 'John Paulin', 'paulin@itu.dk', 'yes!');

  insert into vp_person (id, name, email, password)
  values (2, 'Niels Hallenberg', 'nh@itu.dk', 'no!');

  insert into vp_projector (id, name) values (1, 'Projector 1');
  insert into vp_projector (id, name) values (2, 'Projector 2');
  insert into vp_projector (id, name) values (3, 'Projector 3');

Problem 4.1

Show the result of executing the following select statement:
  select name, email from vp_person;

Problem 4.2

Write an SQL select statement for returning the number of projectors in the table vp_projector.

Problem 4.3

Write an SQL statement for introducing the table vp_reservation. The table should have three columns: Use MySQL's primary key-constraint to specify that no two rows contains the same projector_id and res_date.

Problem 4.4

Write two SQL statements for introducing the following reservations in the table vp_reservation:
  1. Projector 2 for John Paulin, February 6, 2001 (2001-02-06)
  2. Projector 1 for Niels Hallenberg, February 8, 2001 (2001-02-08)

Problem 4.5

Write an SQL statement for returning projector reservations for the date 2001-02-06. The result of executing the statement should result in the following data - column names are written in bold:

proj_idproj_namepers_idpers_nameemail
2Projector 21John Paulinpaulin@itu.dk

Problem 4.6

For reserving a projector it is valuable to ask which projectors are available for reservation a given day. Write an SQL statement that shows the projectors that are available on the date 2001-02-08. The result of executing the statement should result in the following data:

proj_idproj_name
2Projector 2
3Projector 3

Note: It is necessary to use a left join together with a "group by ... having ..." construct.

Problem 5 (30 percent) - Web-service

For the construction of PHP-files (scripts) that implements the video projector reservation system, a site map has been developed, as shown below. In the site-map, boxes represent Web pages (implemented as PHP-scripts), arrows without annotations represent links, and annotated arrows represent transactions:
Below is a template for the file projector.php. It can be assumed that the library vp.php includes the mydb.php-library from the lectures and the functions vp_check_date and vp_return_page.
 
<?php
  include("vp.php");

  if ( $_REQUEST['date'] == "" ) {
      // if a form variable `date' is not set, we use todays date
      $date = date("Y-m-d");
  } else {
      vp_check_date($date);
  }

  # Now date is set and on the form YYYY-MM-DD. Let's ask 
  # MySQL about the dates of the day following $date 
  # and the day preceeding $date; we do this by adding one 
  # to $date and subtracting one from $date, respectively.

  $year = substr($date, 0, 4);
  $month = substr($date, 5, 2);
  $day = substr($date, 8, 2);

  $nextday = date("Y-m-d", mktime(0,0,0,$month,$day+1,$year));
  $prevday = date("Y-m-d", mktime(0,0,0,$month,$day-1,$year));

  mydb_connect();

  # Extract information about the projector reservations for `date'
  $query = " -- YOUR ANSWER TO PROBLEM 4.5 WITH THE DATE '2001-02-06' 
                REPLACED WITH '$date' -- ";

  $rows = mysql_query($query);
  $reservations = "";
  while ( $row = mysql_fetch_row($rows) ) {
      $reservations = $reservations . "<li>$row[1] - $row[3]</a></li>";
  }

  $reservations = $reservations . "<p> 
    <li> <a href='res_add_form.php?date=$date'>Add reservation</a></li>";

  vp_return_page ("Projector Reservations",
     "<h3>Projector Reservations for $date</h3>
      [ <a href='projector.php?date=$prevday'>Prev Day</a> | 
        <a href='projector.php?date=$nextday'>Next Day</a> ]
      <ul>$reservations</ul>");
?>

Problem 5.1

Write the PHP-code for defining two PHP-variables $nextweek and $prevweek, containing the date a week later than $date and a week earlier than $date, respectively. The idea is that the two definitions are inserted in the PHP-code after the definition of the variable $prevday.

Rewrite the call to the function vp_return_page, at the end of the script projector.php, so that links appear to the week after and the week before the given date.

Problem 5.2

Construct the file res_add_form.php. It can be assumed that the script is always requested together with a form variable date containing the date for which a projector should be reserved.

Here is a template for the script:

<?php
  include ("vp.php");

  // CHECK FORM VARIABLES
  ## A ## ...

  mydb_connect();
  $query = " -- YOUR ANSWER TO PROBLEM 4.6 WITH THE DATE '2001-02-08' 
                REPLACED WITH '$date' -- ";

  $rows = mysql_query($query);

  // loop through the rows
  $i = 0;
  while ( $row = mysql_fetch_row($rows) ) {
      $pjs[$i][0] = $row[0]; // id
      $pjs[$i][1] = $row[1]; // name
      $i ++;
  }

  // use the function selectbox to actually create a 
  // selection box to put in an HTML form
  $sel_box = select_box("proj_id", $pjs);

  // CODE FOR RETURNING A FORM TO THE USER
  ## B ## ...
?>
Notice that the function select_box from Problem 2.2 is used for constructing the SELECT-box.

As the answer to the problem, you should write code for examining the content of the form variable date (program point ## A ## in the code above). Moreover, you should write code for returning a form to the user (program point ## B ## in the code above).

Problem 5.3

Construct the file add_res.php. Remember to write code for checking that the form variables have the correct form. You can assume that the function vp_check_date is available, besides the functions vp_check_id and vp_check_email, which can be used to check if a string contains an integer or an email address.

Remember that a person should be allowed to reserve a projector only if the submitted password is identical to the password appearing in the database for that person.


mael@itu.dk