<?php
// 20250116 current master script - wip
// ictnle.com3/xml4teaching/xml4t_config.php
$xml4tpath="../xml4teaching/"; 
$xml4tpath0="../sdata/";
$xml4tpathmedia="../../sdata2_offline/bups/"; // local hard disk
$xml4tpathmedia2="../../sdata2_offline/films2/"; // local hard disk


/************************************************************/
// SOOSH CONFIG 
$sooshpath0="cli/";

// needed when generating external url
$sooshwroot="http://127.0.0.1/www/ictnle.com3/"; // local webroot 
// $sooshwroot="https://www.ictnle.com/"; // online webroot

// check also below
//$original_wwwroot = $sooshwroot.'ictnle.com3';
//$original_wwwroot = $sooshwroot.'ictnle.com';

// wip
//$template == 'blank'; // make anonymous to import

//  check on-line category id & change accordingly
$categoryid = 28; // localhost
$category_name = 'Templates';


/************************************************************/
// USAGE
// php xml2mbzp.php courselog.xml
// where courselog.xml is $argv[1], i.e. the 1st argument passed into the php script


/************************************************************/
// CREATE WORKING DIRECTORY FOR NEW FILES
//$filename = "template01_short.xml";
$filename = $argv[1];
$shortname = preg_replace ('/.xml/m', '', $filename);
mkdir ($shortname."_2mbz");

// run all scripts from here, incl. targz or else moodle won't accept mbz structure
chdir($shortname."_2mbz"); 


/************************************************************/
// CREATE & POPULATE CURRENT course/course.xml with mdlid
//$old_clog = file_get_contents('../'.$argv[1]); //
$old_clog = file('../'.$argv[1]); // use file and NOT file_get_contents

include ($sooshpath0.'xml2mdl.php');

// now use $sessions_log = file_get_contents($new_clog);
// see below
// don't forget to delete $new_clog = $shortname1."_mdl.xml";


/************************************************************/
// CREATE & POPULATE FUTURE course/course.xml

$courseid = 1;
$contextid = 1;

$shortname = preg_replace ('/.xml/m', '', $filename);
$fullname = preg_replace ('/_/m', ' ', $shortname);
//ucfirst() function converts the first character of a string to uppercase 
//ucwords() - converts the first character of each word in a string to uppercase 
$fullname = ucwords($fullname);
$visible = 0; // change after checking in wui

shell_exec("mkdir activities");
// keep the following line in loop xml2sections_section_sectionid.php 
//shell_exec("mkdir sections/section_{$sectionid}");
shell_exec("mkdir course");

include ($sooshpath0.'xml2course_calendar.php');
include ($sooshpath0.'xml2course_competencies.php');
include ($sooshpath0.'xml2course_completiondefaults.php');
include ($sooshpath0.'xml2course_contentbank.php');
include ($sooshpath0.'xml2course_course.php');
include ($sooshpath0.'xml2course_enrolments.php');
include ($sooshpath0.'xml2course_inforef.php');
include ($sooshpath0.'xml2course_roles.php');

/************************************************************/
// CREATE SECTION #0 (aka general introduction) FOR THE WHOLE COURSE
// i.e. w/o a module

$sectionid = 0; // will start from section 0 with no modules
$sectionnumber = 0;
$allsectionnumbers = '0';

$activityid = 1; // will start from section 1 with modules
$moduleid = 1;
//$contextid = 1;


/************************************************************/
// USE UPDATED MDL READY COURSE LOG
//$sessions_log = file_get_contents($filename); 
//$sessions_log = file_get_contents('../'.$filename); // now in working dir
//$sessions_log = file_get_contents('../'.$argv[1]); //
$sessions_log = file_get_contents($new_clog);


$clog_xml=simplexml_load_string($sessions_log) or die("Error: Cannot create object $ sessions log");

$sectionname = preg_replace ('/.xml/m', '', $filename);
$sectionname = preg_replace ('/_/m', ' ', $sectionname);
//$sectionsummary = 'This course is based on...';// retrieve from clog.xml
$sectionsummary = "Contents of this course were generated before migrating to Moodle. Most activities are embedded in html for portability, however drag &amp; drop functions in javascript may break in the future as they won't be maintained any more.";// retrieve from clog.xml

include ($sooshpath0.'xml2sections_section0.php');

/************************************************************/
// CREATE GENERIC (BLANK) FILES

include ($sooshpath0.'xml2generic_files.php');

include ($sooshpath0.'xml2gradebook.php'); // check if needs customising

$original_wwwroot = $sooshwroot.'ictnle.com3';
//$backupid = `date +%Y%m%d-%H%m`."_".$filename;
$backupid = "20250122_bup_".$filename;
// try generating w/o the following
// moodle_backup.xml


// START WRITING XML2MOODLE_BACKUP.XML
$line_activity ='';
$line_backup_sections ='';
$line_backup_settings_setting_section ='';
$line_backup_settings_setting_activity ='';
$line_backup_end ='';

include ($sooshpath0.'xml2moodle_backup_details.php');

$file_moodle_backup = "moodle_backup.xml";
$fh_backup = fopen($file_moodle_backup, 'w') or die("can't open file $file_moodle_backup");
fwrite($fh_backup, $line_moodle_backup_details);
// fclose($fh_backup) or die("can't fclose fh_backup"); ← see end of file

// skipped generating this file
// .ARCHIVE_INDEX


$sectionnumber = 1;
$sectionid = 1; // start from section 1 with modules

/************************************************************/
// MATCH ALL SECTIONS WHERE 1 SESSION (DATE) = 1 SECTION
if (preg_match_all("#^<clog_session>$(.*?)^<\/clog_session>$#sim",$sessions_log,$matches)) {

// courselog.xml is written from bottom to top to avoid 'the scroll of death'
// → do the same in moodle
// start from last in clog.xml 
$matches = array_reverse($matches, true);

/************************************************************/
// LOOP 1
// MATCH ALL SECTIONS WHERE 1 SESSION (DATE) = 1 SECTION

      foreach ($matches[0] as $clog_xml) {
$clog_xml=simplexml_load_string($clog_xml) or die("Error: Cannot create object $ xml course details");

if (($clog_xml->clog_session_status == "tbc") || ($clog_xml->clog_session_status == "lc")) {
// hidden from ss
}

//if (trim($clog_xml->clog_session_status !='lc')
//&& (!empty($clog_xml->clog_session_date_cancelled)) 
if (!empty($clog_xml->clog_session_date)) {

// CREATE CONTENT FOR SECTION SUMMARY
$sessiondate = (trim($clog_xml->clog_session_date));
// consider removing date in future versions for Moodle bc it can  generate dates from attendance sheet...
$sectionname = $sessiondate." ".(trim($clog_xml->clog_session_title));
//$sectionname = (trim($clog_xml->clog_session_title));
$sectionsummary = '';

// support material (e.g. books, individual pdfs, etc)
if (!empty($clog_xml->clog_support_material->clog_book_title)) {
foreach ($clog_xml->clog_support_material as $clog_support_material) {

//$sectionsummary .= "<img src=\"".$xml4tpath0."pix/icons8-magazine-100_white.png\" width=\"35\" border=\"0\" alt=\"icon\"> ".$clog_support_material->clog_book_title;
// 〆fails

// include icons in activities (disabled by default in moodle)
// admin/settings.php?section=htmlsettings 
// → Remove HTML tags from all activity names formatstringstriptags 
// → add emoticon in the bottom row

// or follow instructions at
// https://docs.moodle.org/405/en/Emoticon_images
//$sectionsummary .= "name_of_emoticon ".$clog_support_material->clog_book_title;
// :!cp ~/www/ictnle.com3/sdata/pix/icons8-magazine-100_white.png ~/www/ictnle.com3/pix/s/
// $sectionsummary .= "icons8-magazine-100_white ".$clog_support_material->clog_book_title; // fails

$sectionsummary .= $clog_support_material->clog_book_title;
$sectionsummary .= " ".$clog_support_material->clog_book_level;
    if (!empty($clog_support_material->clog_book_unit)) {
//$sectionsummary .= " Unit ".$clog_support_material->clog_book_unit;
$sectionsummary .= " ".$clog_support_material->clog_book_unit."<br />";
        }
    } // foreach ($clog_xml->clog_support_material as $clog_support_material)
$sectionsummary .= "<br />";
} // endif not empty clog_book_title

// prep based on url contents from previous lesson(s)
if (!empty(trim($clog_session_hw_activity->activity->session_date))) {
$session_hw_activity = '';
foreach ($clog_xml->clog_session_hw_activity as $clog_session_hw_activity) {
$session_hw_activity .= "<br />".trim($clog_session_hw_activity->activity->activity_title);
$session_hw_activity .= "<br />".trim($clog_session_hw_activity->activity->session_date);
$session_hw_activity .= "<br />".trim($clog_session_hw_activity->activity->instructions);
} // foreach ($clog_xml->clog_session_hw_activity as $clog_session_hw_activity)
} // if (!empty(trim($clog_session_hw_activity->activity->session_date))) 

// homework (incl. video conf details to be removed in blank template)
$hw = trim($clog_xml->clog_session_hw);
/***
if ($template == 'blank') {
     $string_to_replace = '<src="pix\\/zoom_meeting(.*)<\\a>';
     $hw = preg_replace ('/^'.$string_to_replace.'$/m', '', $hw);
     $string_to_replace = '<src="pix\\/vznaniya(.*)<\\a>';
     $hw = preg_replace ('/^'.$string_to_replace.'$/m', '', $hw);
}
***/
     $string_to_replace = 'src="pix\\/';
     $string_to_add = 'src="'.$xml4tpath0.'pix/';
     $hw = preg_replace ('/'.$string_to_replace.'/m', ''.$string_to_add.'', $hw);

$sectionsummary .= "<br />".$session_hw_activity."<br /><pre>".$hw."</pre>";

/************************************************************/
//

foreach ($clog_xml->clog_support_material as $clog_support_material) {

// LOOP 2 (inside 1)
// CREATE MODULE FOR EACH SECTION

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

// needed to display in xml order and not just retrieve per script order
// if sbook_plugins/* then
// if (!empty($activity_contents))

$activity = $clog_activity;
$mdlid = $clog_activity->mdlid;
//$activityintro = '';

/***
// fails to load icon though path seems OK???
  if (!empty(trim($activity->activity_icon)))  {
$activityicon = "<img src=\"".$xml4tpath0."pix/icons8-collaboration-100_white.png\" width=\"35\" border=\"0\" alt=\"icon\"> ";
//$activityicon = "<img src=\"".$xml4tpath0.$activity->activity_icon."\" width=\"35\" border=\"0\" alt=\"icon\"> ";
}
else {
$activityicon = "<img src=\"".$xml4tpath0."pix/icons8-collaboration-100_white.png\" width=\"35\" border=\"0\" alt=\"icon\"> ";
}

//$activityname = $activity->activity_title;
$activityname = $activityicon." ".$activity->activity_title;

// tmp workaround also fails!... though works for activity with dictionary below?
$activityname =  "<img src=\"".$xml4tpath0."pix/icons8-magazine-100_white.png\" width=\"35\" border=\"0\" alt=\"icon\" /> ".$activity->activity_title;
***/

$activityname =  $activity->activity_title;

// in Course → Section → Module → Settings → enable Display description on course page 
if (!empty($activity->activity_lead_in)) {
$activityintro = "<em>".$activity->activity_lead_in."</em>";

///
/************************************************************/
// adapted from from xml4teaching/xml_load_file_course_log.php
//
// DISPLAY GRAMMATICAL MATERIAL FROM GIVEN DATE
if($clog_activity->activity_type == "textbook") {
foreach ($clog_activity->activity_contents as $activity_contents) {
if (preg_match_all("#Grammar: (.*?)^#sim",$activity_contents,$session_grammatical_materials_matched)) {
$activityintro .= "<br />";
$activityintro .= "<img src=\"".$xml4tpath0."pix/icons8-automation-100.png\" width=\"20\" border=\"0\" alt=\"grammar\" /> Grammar review: <br />\n";
foreach ($session_grammatical_materials_matched[0] as $session_grammatical_material) {
$session_grammatical_material = preg_replace('/Grammar: /m','',$session_grammatical_material);
$session_grammatical_material = preg_replace("/$/m","\n",$session_grammatical_material);
$activityintro .= $session_grammatical_material."<br />\n";
}}}}
// implemented 
if (($clog_activity->activity_type == "grammar") && (!empty(trim($clog_activity->activity_title))) && ($clog_activity->activity_status !== "wip")){
$activityintro .= "<br />";
$activityintro .= "<img src=\"".$xml4tpath0."pix/icons8-automation-100.png\" width=\"20\" border=\"0\" alt=\"grammar\" /> Grammar presentation: <br />\n";
foreach ($clog_activity->activity_title as $activity_title) {
$activityintro .= $activity_title."<br />\n";
}}
// implemented 
if ((!empty(trim($clog_activity->grammar))) && ($clog_activity->activity_status !== "wip")){
$activityintro .= "<br />";
$activityintro .= "<img src=\"".$xml4tpath0."pix/icons8-automation-100.png\" width=\"20\" border=\"0\" alt=\"grammar\" /> Grammar review: <br />\n";
foreach ($clog_activity->grammar as $grammar) {
$activityintro .= $grammar."<br />\n";
}}

// DISPLAY LEXICAL MATERIAL (vocab) FROM GIVEN DATE
if($clog_activity->activity_type == "textbook") {
foreach ($clog_activity->activity_contents as $activity_contents) {
if (preg_match_all("#Lexical material: (.*?)^#sim",$activity_contents,$session_lexical_materials_matched)) {
$activityintro .= "<br />";
$activityintro .= "<img src=\"".$xml4tpath0."pix/icons8-dictionary-100.png\" width=\"20\" border=\"0\" alt=\"lexical material\" /> Lexical material: <br />\n";
foreach ($session_lexical_materials_matched[0] as $session_lexical_material) {
$session_lexical_material = preg_replace('/Lexical material: /m','',$session_lexical_material);
$session_lexical_material = preg_replace("/$/m","\n",$session_lexical_material);
$activityintro .= $session_lexical_material."<br />\n";
}}}}
// not implemented (yet?)
if (($clog_activity->activity_type == "vocab") && (!empty(trim($clog_activity->activity_title))) && ($clog_activity->activity_status !== "wip")){
$activityintro .= "<br />";
$activityintro .= "<img src=\"".$xml4tpath0."pix/icons8-dictionary-100.png\" width=\"20\" border=\"0\" alt=\"lexical material\" /> Lexical material: <br />\n";
foreach ($clog_activity->activity_title as $activity_title) {
$activityintro .= $activity_title."<br />\n";
}}
// implemented 
if ((!empty(trim($clog_activity->vocab))) && ($clog_activity->activity_status !== "wip")){
$activityintro .= "<br />";
$activityintro .= "<img src=\"".$xml4tpath0."pix/icons8-dictionary-100.png\" width=\"20\" border=\"0\" alt=\"lexical material\" /> Lexical material: <br />\n";
foreach ($clog_activity->vocab as $vocab) {
$activityintro .= $vocab."<br />\n";
}}

// DISPLAY FUNCTIONAL LANGUAGE FROM GIVEN DATE
if($clog_activity->activity_type == "textbook") {
foreach ($clog_activity->activity_contents as $activity_contents) {
if (preg_match_all("#Functional language: (.*?)^#sim",$activity_contents,$session_functional_languages_matched)) {
$activityintro .= "<br />";
$activityintro .= "<img src=\"".$xml4tpath0."pix/icons8-hammer-100.png\" width=\"20\" border=\"0\" alt=\"functional language\" /> Functional language: <br />\n";
foreach ($session_functional_languages_matched[0] as $session_functional_language) {
$session_functional_language = preg_replace('/Functional language: /m','',$session_functional_language);
$session_functional_language = preg_replace("/$/m","\n",$session_functional_language);
$activityintro .= $session_functional_language."<br />\n";
}}}}
// not implemented (yet?)
if (($clog_activity->activity_type == "functional_language") && (!empty(trim($clog_activity->activity_title))) && ($clog_activity->activity_status !== "wip")){
$activityintro .= "<br />";
$activityintro .= "<img src=\"".$xml4tpath0."pix/icons8-hammer-100.png\" width=\"20\" border=\"0\" alt=\"functional language\" /> Functional language: <br />\n";
foreach ($clog_activity->activity_title as $activity_title) {
$activityintro .= $activity_title."<br />\n";
}}
// implemented 
if ((!empty(trim($clog_activity->functional_language))) && ($clog_activity->activity_status !== "wip")){
$activityintro .= "<br />";
$activityintro .= "<img src=\"".$xml4tpath0."pix/icons8-hammer-100.png\" width=\"20\" border=\"0\" alt=\"functional language\" /> Functional language: <br />\n";
foreach ($clog_activity->functional_language as $functional_language) {
$activityintro .= $functional_language."<br />\n";
}}

// DISPLAY METHODOLOGY FROM GIVEN DATE
if($clog_activity->activity_type == "textbook") {
foreach ($clog_activity->activity_contents as $activity_contents) {
if (preg_match_all("#Methodology: (.*?)^#sim",$activity_contents,$session_methodology_notes_matched)) {
$activityintro .= "<br />";
$activityintro .= "<img src=\"".$xml4tpath0."pix/icons8-graduation-cap-100-unfilled.png\" width=\"20\" border=\"0\" alt=\"methodology\" /> Methodology: <br />\n";
foreach ($session_methodology_notes_matched[0] as $session_methodology_note) {
$session_methodology_note = preg_replace('/Methodology: /m','',$session_methodology_note);
$session_methodology_note = preg_replace("/$/m","\n",$session_methodology_note);
$activityintro .= $session_methodology_note."<br />\n";
}}}}
// not implemented (yet?)
if (($clog_activity->activity_type == "methodology") && (!empty(trim($clog_activity->activity_title))) && ($clog_activity->activity_status !== "wip")){
$activityintro .= "<br />";
$activityintro .= "<img src=\"".$xml4tpath0."pix/icons8-graduation-cap-100-unfilled.png\" width=\"20\" border=\"0\" alt=\"methodology\" /> Methodology: <br />\n";
foreach ($clog_activity->activity_title as $activity_title) {
$activityintro .= $activity_title."<br />\n";
}}
// implemented 
if ((!empty(trim($clog_activity->methodology ))) && ($clog_activity->activity_status !== "wip")){
$activityintro .= "<br />";
$activityintro .= "<img src=\"".$xml4tpath0."pix/icons8-graduation-cap-100-unfilled.png\" width=\"20\" border=\"0\" alt=\"methodology\" /> Methodology: <br />\n";
foreach ($clog_activity->methodology as $methodology ) {
$activityintro .= $methodology ."<br />\n";
}}

///***/// end of extract

// shows description in xml2activities_url.php → activities/url_{$moduleid}/module.xml
$moduleshowdescription = "1"; 
}
else {
$activityintro = "";
$moduleshowdescription = "0"; 
}

// add into config.php
// path to old clog
//$activityexternalurl = $sooshwroot."xml4teaching/xml_load_file_mdl.php?file=".$xml4tpath0."inc_xml_mdl/".$filename."&mdlid=".$mdlid;
// path to new clog
$activityexternalurl = $sooshwroot."xml4teaching/xml_load_file_mdl.php?file=".$xml4tpath0."inc_xml_mdl/".$shortname."_mdl.xml&mdlid=".$mdlid;

include ($sooshpath0.'xml2activities_url.php');
include ($sooshpath0.'xml2moodle_backup_activities.php');
include ($sooshpath0.'xml2moodle_backup_settings_setting_activity.php');

$activityid++;
$moduleid++;
$contextid++;

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

// BACK TO LOOP 1 - was but now still is

/************************************************************/
// MATCH ALL NEW_EXPRESSIONS PER ACTIVITY - incl. from reading task... ie 2025 xml format
// adapt from inc/xml4t_new_expressions.php
//if (!empty(trim($clog_xml->clog_support_material->clog_expressions))) {
if (!empty(trim($clog_support_material->clog_expressions))) {
foreach ($clog_xml->clog_support_material->clog_expressions as $clog_expressions) {

$mdlid = $clog_activity->mdlid;

// $activityname = "Vocabulary (from activity) [sectionnumber:".$sectionnumber." moduleid: ".$moduleid."]<br />"; // debug only
$activityname = "<img src=\"".$xml4tpath0."pix/icons8-dictionary-100.png\" width=\"35\" border=\"0\" alt=\"wordlist\" /> Vocabulary (per activity)"; // vocab from activity
$activityintro = '';
$activityexternalurl = $sooshwroot."xml4teaching/xml_load_file_mdl.php?file=".$xml4tpath0."inc_xml_mdl/".$shortname."_mdl.xml&mdlid=".$mdlid;

include ($sooshpath0.'xml2activities_url.php');
include ($sooshpath0.'xml2moodle_backup_activities.php');
include ($sooshpath0.'xml2moodle_backup_settings_setting_activity.php');

$activityid++;
$moduleid++;
$contextid++;

} // foreach ($clog_xml->clog_support_material->clog_expressions as $clog_expressions)
} // if (!empty(trim($clog_xml->clog_support_material->clog_expressions)))

// end of loop 3
} // foreach ($clog_xml->clog_support_material as $clog_support_material)

/************************************************************/
// MATCH ALL NEW_EXPRESSIONS PER SESSION ie < 2024 xml format
// (only once at the end of each session)
// adapt from inc/xml4t_new_expressions.php
if (!empty(trim($clog_xml->clog_expressions))) {

$mdlid = $clog_activity->mdlid;

$activityname = "<img src=\"".$xml4tpath0."pix/icons8-dictionary-100.png\" width=\"35\" border=\"0\" alt=\"wordlist\" /> Vocabulary (per session)"; // vocab from session
$activityintro = '';
$activityexternalurl = $sooshwroot."xml4teaching/xml_load_file_mdl.php?file=".$xml4tpath0."inc_xml_mdl/".$shortname."_mdl.xml&mdlid=".$mdlid;

include ($sooshpath0.'xml2activities_url.php');
include ($sooshpath0.'xml2moodle_backup_activities.php');
include ($sooshpath0.'xml2moodle_backup_settings_setting_activity.php');

$activityid++;
$moduleid++;
$contextid++;

} // if (!empty(trim($clog_xml->clog_expressions)))


/************************************************************/
// MATCH ONLY ONE DELAYED ERROR CORRECTION MODULE (PER SECTION)
// (only once at the end of each session)
if (!empty(trim($clog_xml->clog_deco))) {

$mdlid = $clog_activity->mdlid;

//$activityname = "Delayed error correction<br />";
$activityname = "<img src=\"".$xml4tpath0."pix/icons8-error-100.png\" style=\"background-color: transparent\" width=\"35\" border=\"0\" alt=\"mistakes\" /> Delayed error correction<br />";
$activityexternalurl = $sooshwroot."xml4teaching/xml_load_file_mdl.php?file=".$xml4tpath0."inc_xml_mdl/".$shortname."_mdl.xml&mdlid=".$mdlid;

include ($sooshpath0.'xml2activities_url.php');
include ($sooshpath0.'xml2moodle_backup_activities.php');
include ($sooshpath0.'xml2moodle_backup_settings_setting_activity.php');

$activityid++;
$moduleid++;
$contextid++;

} // THIS IS THE END OF LOOP 2 FOR EACH ACTIVITY

$allsectionnumbers = $allsectionnumbers.",".$sectionnumber;
$allsectionnumbers = (preg_replace("/^,/","",$allsectionnumbers));
include ($sooshpath0.'xml2sections_section_sectionid.php');

include ($sooshpath0.'xml2moodle_backup_sections.php');
include ($sooshpath0.'xml2moodle_backup_settings_setting_section.php');

// must be AFTER the scripts above
$sectionnumber++;
$sectionid++;

} // if (!empty($clog_xml->clog_session_date))
} // foreach ($matches[0] as $clog_xml)


// POPULATE <sequence> WITH ALL SECTIONS EXCEPT THE 1ST ONE
shell_exec("mv sections/section_0 sections/sectionzero");
// shell_exec("sed -i 's|^<sequence>(.*)</sequence>|<sequence></sequence>|g' sections/section_0/section.xml");

$arrFiles = array();
// pattern for debugging
//$dir = "./sections/section_2";
//$files = glob($dir . "/*");
$dir = "./sections";
$files = glob($dir . "/section_*/*");
foreach ($files as $file) {
$content = file_get_contents($file);
$content = preg_replace('/<sequence>(.*)<\/sequence>/m',"<sequence>$allsectionnumbers</sequence>",$content);
$file = file_put_contents($file,$content);
}

shell_exec("mv sections/sectionzero sections/section_0");

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


/************************************************************/
// FINISH WRITING XML2MOODLE_BACKUP.XML
$line_backup_start_contents_activities = "      <contents>
      <activities> \n";
fwrite($fh_backup, $line_backup_start_contents_activities);

fwrite($fh_backup, $line_activity); // was in a loop

$line_backup_end_activities_start_sections = "      </activities>
      <sections> \n";
fwrite($fh_backup, $line_backup_end_activities_start_sections);

fwrite($fh_backup, $line_backup_sections); // was in a loop

$line_backup_end_sections = "      </sections>\n";
fwrite($fh_backup, $line_backup_end_sections);

include ($sooshpath0.'xml2moodle_backup_contents.php');
fwrite($fh_backup, $line_backup_contents);

fwrite($fh_backup, $line_backup_settings_setting_section); // was in a loop
fwrite($fh_backup, $line_backup_settings_setting_activity); // was in a loop

include ($sooshpath0.'xml2moodle_backup_end.php');
fwrite($fh_backup, $line_backup_end);

fclose($fh_backup) or die("can't fclose fh_backup");


/************************************************************/
// FINALISE & COMPRESS ALL FILES (must be run INSIDE current dir)
include ($sooshpath0.'xml2tar2gz2mbz.php');


/************************************************************/
// REMINDER

echo "Check contents of xml2mbz-generated course.mbz by:
cp course.mbz course.tgz (or else tar gz won't see it when tabbing)
tar -xzvf course.mbz -C course/ \n";
echo "----------------------------------------------------------------------\n";

echo "Consider making backup before importing course.mbz (might break db?)
moosh sql-dump > backup.sql

or delete broken course(s)
moosh course-delete [courseid] \n";
echo "----------------------------------------------------------------------\n";

echo "Restore (Import) (new) course (moosh) into category
(don't forget to run from webroot or moosh will fail!

php ../moosh/moosh.php course-restore course.mbz [categoryid]
where 28 = Templates
php ../moosh/moosh.php course-restore ../soosh/template01_short.xml_2mdl.mbz 28
----------------------------------------------------------------------

Restore course (moosh) into -e existing course
php ../moosh/moosh.php course-restore -e course.mbz [courseid]
----------------------------------------------------------------------

Restore course (moosh) over-write existing course
php ../moosh/moosh.php course-restore --overwrite course.mbz [courseid]
----------------------------------------------------------------------

OR
Restore course (Moodle wui)
Course → More → Course reuse → <strike>Import</strike> (dropdown menu) Restore
---------------------------------------------------------------------- \n";

echo "Once imported, copy modules from template to existing course
php ../../moosh/moosh.php course-list
php ../../moosh/moosh.php module-copy 64 15
php ../../moosh/moosh.php module-copy --section 108 64 15
〆failed (only copied to top section) \n";

echo "----------------------------------------------------------------------\n";

echo "mv ~/www/soosh/template01_short_mdl.xml ~/www/ictnle.com3/sdata/inc_xml_mdl/template01_short.xml

rm -Rf template01_short_2mbz template01_short_mdl.xml template01_short_tmp.xml template01_short.xml_2mdl.mbz \n";

?>
