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

homelectshws
D2Lbreeze (snow day)

hw02
hw02: Functions that return (html) text

Due 2014.Sep.16 (Tue) 23:59. I'll accept it up to the late-deadline (48hrs) but will waive the late-penalty. Submit on D2L, and a bring hardcopy of your four files utils.php, utils-test.php, silly-page.php, and index.php to the following class. The files should also be viewable on the web, as discussed below.

For this homework, create a directory ~/dynamic_php/itec325/hw02. It will contain four files (three of which are similar to hw01):

  1. (10pts) Write a function string hyperlink( string $url, string-or-boolean $linkTxt ) which returns a string of html. If the provided $linkTxt is false (instead of a string), then use the $url as the link text.
    hyperlink( "http://www.gutenberg.org", "free books!" ) === "<a href='http://www.gutenberg.org'>free books!</a>"
    hyperlink( "myLocalFile.html", false ) === "<a href='myLocalFile.html'>myLocalFile.html</a>"

    Note that this is just a regular ol' function that takes in strings and returns strings!
    The fact that the strings happen to have angle-brackets in them means that the result might be useful to people generating html, but the function itself does't really care.

    (You don't need to come up with additional test cases, but you do need to actually include these tests or their equivalent. You can assume that neither string is ever empty.)

    Note how the input strings don't contain any quote-characters, but the returned result does (since HTML attributes must be quoted). I recommend that your generated html uses single quote marks around attributes, but you'll get full credit for either single- or double-quote marks, as long as it is valid HTML. Make sure your test case(s) express exactly what you want the desired output should be!

  2. (15pts) We will write a function to help us generate thumbnails:
    1. What is the html element for an image with source pony.jpg (in the same directory), that is rendered 300px wide?
      Include your answer in a comment, in your test-file.
    2. What is the html for such an image which is also a link — where the link would take you to that file (not a html page — just that file).
      Include your answer in a comment, in your test-file.
    3. Make a test-case for thumbnail("pony.jpg",300); the expected-output should be a string that happened to be your answer.
      You don't need further test-cases for this problem.

      Pay close attention to the exact spacing you want to have. As discussed in class, I recommend using single-quotes (') around the attribute-values.

    4. Finally, implement the function thumbnail that takes in a string (a URL) and a number (the width in pixels), and returns a string that is an html link containing an image.

      For full credit, your function should call hyperlink as already written!

  3. (20pts) Write a function which takes in an array of strings, and returns a single, long string: a comma-separated list of all the 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.
  4. (20pts) Write a function toc (“table of contents”): It takes an array of strings, and returns a string that is HTML for an un-ordered list of links to anchors within the same document.

    For example, given an array with "thumbnail-problem", "commaList-problem", and "due-date", it would return HTML that would render as:

    (Try clicking on those items, and feel free to view-source, to understand what it's doing. Of course, this function is only helpful if the strings in the array are also used as id attributes elsewhere in the page.)

    For full credit, this function should involve a call to hyperlink.

  5. (10pts) Make sure your files are viewable via the web at https://php.radford.edu/~yourUserName/itec325/hw02/silly-page.php, as well as the file https://php.radford.edu/~yourUserName/itec325/hw02/index.php, as described below.
  6. (15pts) Finally, create one further file, index.php, which (when viewed on the web) will contain:

Notes:


1When having a list of more than 2 items, I personally recommend using the Oxford comma (or, see wikipedia) but I will leave it to your preference.      

2 You presumably want to check that your file-sources really are visible only after the due-date. To test this, first write the code so that it only even mentions index.php itself, and make sure it turns but that the source file is not publically displayed. Add the other files, and confirm that the source files are still not displayed. Then: temporarily change the date to 2013 (instead of 2014) and verify that the source-files display; then immediately change the date back and re-verify that they are not being displayed.      

3 While the defensive-programming can be helpful in bigger projects (esp. in untyped languages like php), it's never interesting code. If you DO want to do the checks anyway, that's fine, but do all the type-validation right away (before you enter the 'real' function-logic), and throw an error if something goes wrong (don't continue and return some sort of odd, undocumented answer). For example:

function foo( $someNum ) {
  // optional error-check:
  if !(is_numeric($someNum)) { throw new InvalidArgumentException( 'foo: expected number, given ' . $someNum ); }

  // ...now put your real code, uncluttered by further type-checks.
}
(Ideally, you'd include the type-requirements in comments, and then a tool would automatically generate the above boilerplate error-checking for you. That's what Java's type-system gives you, for example.)      

homelectshws
D2Lbreeze (snow day)


©2014, Ian Barland, Radford University
Last modified 2014.Nov.30 (Sun)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Rendered by Racket.