<?php 
$deck_reference_pattern = '/([a-z0-9][^\s]*csv)/m'; 

if (preg_match_all($deck_reference_pattern,$list_of_decks,$matches)) {

foreach ($matches[0] as $deck_ref) {

$file = "inc_drills/".$deck_ref;

echo "<div id=\"not_to_print_deck\">";

//read first line
$first_line = file($file);
list($exercise_type, $exercise_name, $selection_number, $instructions,$background_image,$key_image) = explode(";", $first_line[0]);
//echo "<em>".$first_line[0]."</em><br />";
/*
// not used in sbook
echo "<img valign=\"bottom\" src=\"pix/icons8-training-100.png\" width=\"40\" border=\"0\" alt=\"modelling target language\"> <h3>".$exercise_type."</h3> - ";
echo "<h3>".$exercise_name."</h3> ";
echo "<h3>(".$selection_number.")</h3><br />";
echo "      <em>".$instructions."</em><br /><br />";
 */

//adapted from xml_load_file_ref_sbook.inc 
echo "<div class=\"flex-container\">";
    echo "<div class=\"sbook_left_column\">\n";
        echo "<div class=\"sbook_presentation\"> &nbsp; ".$exercise_name."</div>";
    echo "</div><!-- end sbook_left_column -->\n";

    echo "<div class=\"sbook_middle_right_column\">\n";
    echo "<pre class=\"sbook_lesson_plan\">";
        echo "<span class=\"sbook_exercise_no\">".$activity->activity_id."</span> <span class=\"sbook_instructions\">".$instructions."</span>";
    echo "</pre>";
    echo "</div><!-- end sbook_middle_right_column -->\n";
echo "</div> <!-- end flex-container -->\n";

//    echo "<pre class=\"sbook_lesson_plan\">";

//echo "<div style=\"background-image: url(".$background_image."); background-size: contain no-repeat;\">\n"; // don't forget div at the end
// select size of background below and NOT depending on number of arguments
echo "<div style=\"background: url(".$background_image.") no-repeat right; background-size: contain; height: 50em;\">\n"; // don't forget div at the end

//read first line, but do nothing with it
//$file = rsort($file); // php breaks
//$file = shell_exec("sort -r $file"); // fails
//$file = shell_exec("tac $file"); // works

/*
$file = array($file);
$file = rsort($file); 
$handle = $file;
 */

$handle = fopen($file, "r");
//echo $file;
$line = fgets($handle);
//echo "<hr />\n";
//echo $line;

//now read the rest of the file line by line
//if $file = rsort($file) { 
if ($handle) {
  while (($line = fgets($handle, 4096)) !== false) {
    list($txt_id,$txt_contents) = explode(";", $line);
echo "<style>\n";
echo "#div2mov_cards".$txt_id." {\n";
echo "position: absolute;\n";
//echo "margin-left: -5em;\n";
echo "z-index: 9;\n";
echo "text-align: center;\n";
echo "padding: 0.1em;\n";
echo "display: inline-block;\n";
//echo "display: block;\n";
echo "float: left;\n";
//echo "background-color: #052a3a;\n";
//echo "background-color: #cdcdcd;\n";
echo "background-color: #aaa;\n";
//echo "box-shadow: 0px 4px 4px 0px rgba(0,0,0,0.2);\n";
echo "box-shadow: 0px 0px 2px 2px rgba(0,0,0,0.2);\n";
/*echo "resize: both;";
echo "width: 10em;";
echo "height: 6em;"; 
echo "overflow: scroll;"; */
echo "}\n";
echo "</style>\n";

echo "<div id=\"div2mov_cards".$txt_id."\">";
echo "<span style=\"color: #efefef; font-size: 120%;display: inline-block; padding: 0.3em; float: left;\">".$txt_contents."</span><br /> ";
//echo "<span style=\"color: #000; font-size: 120%;display: inline-block; padding: 0.3em; float: left;\">".$txt_contents."</span><br />";
echo "</div>";

echo "<script>\n";
echo"dragElement(document.getElementById(('div2mov_cards".$txt_id ."')));";
include ('js/div2mov_cards.js');
echo "</script>\n";

}
    if (!feof($handle)) {
        echo "Error: unexpected fgets() fail\n";
    }
    fclose($handle);
}
//} // if $file = rsort($file)

echo "</div>";

echo "<br /><br /><img src=\"pix/icons8-key-50.png\" width=\"35em\" border=\"0\" alt=\"key to exercise\">  Hover to view key:\n";
echo "<span class=\"show_key\">";
echo "<img valign=\"bottom\" src=\"".$key_image."\" width=\"80%\" border=\"0\" alt=\"key\">";
echo "</span>\n";
echo "</div>"; // not to print

//    echo "</pre>"; // end lesson_plan

} // end foreach
} // end if preg_match_all
?>
