<?php

//Function from rorypicko (Stack Overflow)
function seoUrl($string) {
    //Lower case everything
    $string = strtolower($string);
    //Make alphanumeric (removes all other characters)
    $string = preg_replace("/[^a-z0-9_\s-]/", "", $string);
    //Clean up multiple dashes or whitespaces
    $string = preg_replace("/[\s-]+/", " ", $string);
    //Convert whitespaces and underscore to dash
    $string = preg_replace("/[\s_]/", "-", $string);
    return $string;
}

header('Content-type: application/vnd.ms-word');
header('Content-Disposition: attachment; Filename='.seoURL($rubric->name));

echo '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns="http://www.w3.org/TR/REC-html40">';
echo '<meta http-equiv=\'Content-Type\' content=\'text/html; charset=utf-8\'>';
echo '<body>';

//Inline styles (necessary because it's a download)
echo
'<style>
    body
    {
        font: "Arial", sans-serif;
    }
    .header-text
    {
        text-align:center;
        font-weight: bold;
        margin:0;
    }

    table
    {
        border-collapse: collapse;
        border: 1px solid black;
        width: 100%;
    }
    td, th
    {
        border: 1px solid black;
        padding: 5px;
    }

@page
{
    mso-page-orientation: landscape;
}
</style>';

?>

<p class="header-text">University of Puerto Rico, Río Piedras Campus</p>
<p class="header-text">Online Learning Assessment System</p>
<p class="header-text">{{$course->program->name}} Program</p>
<p class="header-text">{{{ $course->code }}} {{{ $course->number }}} - {{$course->name}}</p>
<h1 class="header-text">Assessment Rubric </h1>

<span>Rubric: {{{ $rubric->name}}}</span><br>
<span>Activity: {{{ $activity->name}}} </span><br>
<span>Passing Criteria:
  <span id="expected_percentage">{{{ $rubric->expected_percentage }}}% of students must obtain at least </span>
  <span id="expected_points">{{{$rubric->expected_points}}}</span> points
</span><br><br>

<table class="table table-striped table-condensed">
  <thead><tr><th>Criterion</th><th>Beginning (1-2)</th><th>In Progress (3-4)</th><th>Good (5-6)</th><th>Excellent (7-8)</th></tr></thead>
  <tbody>
  @foreach(json_decode($rubric->contents) as $criterion)
    <tr>
      @if(isset($criterion->notes))
      <td>
        <em data-toggle="tooltip" data-placement="top" title="{{ $criterion->notes }}">{{{ $criterion->name }}}</em>
      </td>
      @else
      <td>
        {{{ $criterion->name }}}
      </td>
      @endif
      <td>{{{ $criterion->description12 }}}</td>
      <td>{{{ $criterion->description34 }}}</td>
      <td>{{{ $criterion->description56 }}}</td>
      <td>{{{ $criterion->description78 }}}</td>
    </tr>
  @endforeach
  </tbody>
</table>


<?

echo '</body>';
echo '</html>';
?>
