<?php
// 20240829
// Usage:
// Moodle → My courses → Course → Topic (#) → Settings → External url
// displays only block with given mdlid (Moodle id)
// Example:
// http://127.0.0.1/www/ictnle.com3/xml4teaching/xml_load_file_mdl.php?file=../sdata/inc_xml_mdl/course_log.xml&mdlid=22

require_once('../xml4teaching/xml4t_config.php');
echo "<html>\n";
include('inc/xml4t_heading.php');
echo "<body>\n";
  
$file = $_GET["file"];
$mdlid = $_GET["mdlid"];

$sessions_log = file_get_contents($file); 
include('inc/xml4t_preg_replace_path_in_clog.php');
?>

<div id="main" class="main" type="text/css"> 
<div id="topic_area" class="topic_area" type="text/css"> <!-- needs resizing -->

<?php
if (preg_match_all("#^<root>$(.*?)^<\/root>$#sim",$sessions_log,$matches)) {
//if (preg_match_all("#^<clog_session>$(.*?)^<\/clog_session>$#sim",$sessions_log,$matches)) {

      foreach ($matches[0] as $clog_xml) {
$clog_xml=simplexml_load_string($clog_xml); // or die("Error: Cannot create object $ xml course details"    );
//if (preg_match_all("#^<mdlid>".$mdlid."<\/mdlid>$#sim",$sessions_log,$matches)) {
//if (preg_match_all("#^<mdlid>".$mdlid."<\/mdlid>$#sim",$clog_xml,$matches)) {
//if ($clog_xml->clog_session->clog_activity->mdlid = $mdlid) {

//foreach ($clog_xml->clog_session->clog_support_material->clog_activity as $clog_activity) // fails
foreach ($clog_xml->clog_session as $clog_session) {
foreach ($clog_session->clog_support_material as $clog_support_material) {
foreach ($clog_support_material->clog_activity as $clog_activity) {

if (($clog_activity->activity_type == "textbook") 
    && $clog_activity->mdlid == $mdlid
    && ($clog_activity->activity_status != "wip")) {

echo "<pre class=\"lesson_plan\">\n";

  include ('inc/xml4t_activity_textbook.php');

  $txt = trim($clog_activity->activity_contents);
  include ('inc/xml4t_txt_preg_replace.php');
  echo $txt."\n"; // new contents with updated string, i.e. icons
//echo "<div contenteditable=\"true\">".$txt."</div>\n";

  include ('inc/xml4t_save_contents_editable.php');
//    echo "<br />";
  include ('inc/xml4t_activity_types_mdlid.php');
//include ('inc/xml4t_activity_background.php'); // wip - seldom used

}
} //foreach ($clog_support_material->clog_activity as $clog_activity) 
} // foreach ($clog_session->clog_support_material as $clog_support_material)
} // foreach ($clog_xml->clog_session as $clog_session)

include ('sbook_plugins/wordlist_selection_from_xml_course_log_mdl.inc'); // mdl only!
include ('inc/xml4t_new_expressions_mdl.php');
include ('inc/xml4t_deco_favourite_mistakes.php'); // needs updating per different mdlid?

} // foreach ($matches[0] as $clog_xml)
} // if (preg_match_all("#^<root>$(.*?)^<\/root>$#sim",$sessions_log,$matches))

echo "</pre>\n";
echo "</div>\n";

echo "<div id=\"not_to_print_area03\"><p align=\"center\"><a style=\"not_to_print_area03\" id=\"#top\" href=\"#top\">[top]</a></p></div>\n";

echo "</body>\n";
echo "</html>\n";
?>
