RU beehive logo ITEC dept promo banner
ITEC 325
2012fall
ibarland

homelectsexamshws
D2Lbreeze (snow day)

hw02
Forms

Part I

Due: 2012.Sep.26 (Wed) 23:59

We shall start developing a file of useful utility functions for php. We'll have two files, named something like util.php and util-test.php. The second file will just require the first, and then contain calls to test (as per lecture and hw01). You can migrate the functions/tests hyperlink and pluralize from hw01. Unless otherwise specified, none of these functions should print any output — they should return a value.

  1. (10pts) Write a function which takes in an array of strings, and returns a single, long string: a comma-separated list of all individual strings, with “and” before the last element (if more than one)1. The resulting string should be suitable for splicing into the middle of a paragraph of prose, and should be proper, grammatical English. (This function has nothing to do with html.) Include at least three test cases, each testing a different situation.
  2. (20pts) Write a function which takes in (a) an array of strings (checkbox-descriptions) and (b) a group-name, and returns a string that happens to be valid html for a series of checkboxes. (The string should include newlines, and (if you want) each line might start with 6 spaces, to serve as indentation.)

    For example, the checkboxes( 'family', array('Brother', 'Sister', 'Pet') ) would return html which renders as something like:




    Include at least two test cases; you'll probably want to write a sample series of checkboxes by hand, to serve as your expected-output. Use a label tag, to group the checkbox with its adjacent text.

  3. (10pts) Write a function show_source_between, which takes three strings: a filename, a start-date, and and end-date. The function will print (not return) html for:

    1. the name of the file and a link to it,
    2. the file's last-modified time (see filemtime),
    3. and
    4. that file's source (if between the start- and end-dates), or a message along the lines of “this file's source only available between 2012-Sep-31 23:59 and 2012-Dec-31 23:59.”
      (Recall the functions strtotime and date.)
    The result should render nicely in a browser. Feel to add extra markup or any class attributes you like.

    Since this is a void function (it only prints), testing is problematic, and so we won't have any unit tests for it. (So be extra-sure to test it carefully by hand.)

  4. (Very similar to the lecture notes:) Write a function getTrimmedInput which takes in a string (the name attribute of an input tag), and returns the value of that element in $_POST, or false if that element didn't exist in $_POST. Your function should stripslashes iff gpc_get_magic_quotes. Your function should trim the result.

    (5pts) You can test this be assigning to $_POST, and then calling your function, and verifying the result. It's slightly tricky, writing a test case regarding gpc_get_magic_quotes. Your testing-code can also call gpc_get_magic_quotes, and store a plain-or-quoted string into $_POST appropriately. (Yes, this test only tests the code for the current server configurations; we'll just mumble under our breath about how global state makes testing difficult, and accept the weakness2 of our test suite.)

  5. (5pts) Write a function sanitizeHtml which takes in a string of raw text, and returns sanitized html: that is, nl2br and htmlspecialchars are both applied to the string, appropriately. Include at least two test cases.

Note that fully half the points are awarded for having good tests (when possible), a good signature (function and parameter names), and a short description of the arguments/types (one sentence usually suffices). In addition to correctness, code quality and readability count.

Part II

Due: 2012.Sep.30 (Sun) 23:59

Scenario

You are working for Lizard Entertainment, makers of the popular adventure game, .World of Wowcraft (“.W.o.W.”). In .W.o.W., players choose a type of character: either warrior, mage, priest, or rogue. Over the course of the game, their characters can learn various skills, depending on type: For example, the diplomacy skill (which is learnable only by rogues and priests), or the acrobatics skill (which is learnable only by rogues and warriors), etc.. Each skill is be tied to one of six built-in character traits: Strength, Intelligence, Wisdom, Dexterity, Constitution, and Charisma. Finally, each skill has a minimum-level (between 30 and 100 inclusive).

Your boss decides that a key feature of the game is that people can add their own skills to the game. Your job is to build a web-page that lets people enter new skills for the game to use. People will enter the name of the skill, a detailed description, which character-types can use it, what trait it's tied to, and their own name (to get the glory). When they submit their form, they will see a response page summarizing the information they entered.

An example of filling out the form The result of the processing the form

Your task

Other requirements

These apply to all homeworks for this class:

1I personally recommend using the Oxford comma, but will leave it to your preference.      

2 I'm not sure: you might be able to use ini_set to change the quoting behavior, and test both ways after all. If you can demonstrate that this does work, then let me know!      

3But please, don't have your pages include instructions to email me.      

4the function to show-source a file is one of the few exceptions      

homelectsexamshws
D2Lbreeze (snow day)


©2012, Ian Barland, Radford University
Last modified 2012.Sep.22 (Sat)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Powered by PLT Scheme