A module I'm developing needs to generate a report based on information submitted in a user form. While the report does have some dynamic elements, the bulk of the page is static. At the moment, I'm writing in the function itself, but that's really ugly and not exactly designer friendly. I'd like to move it out into it's own file. What's the recommended practice for getting that into the a format returnable by the page function?

/**
*  Displays the score report.
*
*  @return
*    HTML to be displayed
*/
function who5_score_report() {
  //get data from cookie
  $score_array = unserialize($_COOKIE['WHO5']);

  //get raw score
  $total_score = array_sum($score_array['scores']);
  $pct_score = $total_score*4 . "%";

  //set score based display parameters
  //TODO: make color configurable
  if ($total_score > 18) {
    $score_color = "green";
  }
  else if ($total_score <= 13) {
    $score_color = "red";
  }
  else {
    $score_color = "yellow";
  }

  //check individual scores for problems (i.e., any score of 0 or 1)
  $failed_question = array();
  foreach ($score_array['scores'] as $i => $score) {
    if ($score <= 1) //any individual score of 1 or 0 represents a warning sign
      $failed_questions[] = $i;
  }

  //create html to ouput
  $output = "<style type=\"text/css\">\n#who5header{text-align:center;margin-bottom:20px}\n.who5score{color:$score_color}\n</style>\n";
  $output .= "<div id=\"who5header\"><h1>" . t('WHO-5 Score Report') . "</h1></div>\n";
  $output .= "<div id=\"who5scoreSummary\"><h2>Summary</h2>\n";
  $output .= t('Assessment completed: ' . date('m-d-Y h:i:s', $score_array['completion_time'])) . "<br />";
  $output .= t('Raw Score: <span class="who5score">%score</span> (<span class="who5score">%pct_score</span>)', array('%score' => $total_score, '%pct_score' => $pct_score));
  $output .= "</div><!--closing who5scoreSummary-->\n";
  $output .= "<div>" . print_r($failed_questions, TRUE) . "</div>";
  $output .= "<div>" . print_r($score_array, TRUE) . "</div>";
  return $output;
}

Comments

dman’s picture

The proper answer is to declare and call your own theme_who5_result() function.
Your code there still has the form and function all pretty mixed together, so I don't think that there's lots to win (yet) by abstracting that into stand-alone html.
But if you can remove the logic, and put just the formatting in a single function call, you'll see what the suitable parameters you need will be.

.dan.
if you are asking a question you think should be documented, please provide a link to the handbook where you think the answer should be found.
| http://www.coders.co.nz/ |

barrett’s picture

Thanks, dman. Guess I've got to knuckle down and learn theming after all.

styro’s picture

You'll still need to learn theming, but you could also use a template for that page if you want a separate file for the markup.

--
Anton

barrett’s picture

Thanks, Anton. For the moment I've just moved the code I had into a theme function but I think I'm going to end up moving it out to a template.