Question

I have an object that contains data from an exported OLAT backup (OLAT is an e-learning tool written in Java).

Now I need to make another 'type' of backup file, namely a Moodle backup file so I can import it into Moodle itself (another e-learning tool written in PHP). So my plan is to create the folder structure needed for the Moodle backup and write the data to the corresponding .xmls and paths.

I know it sounds vague but my question is this: What would be the best course of action for creating these files? For example, I need to make an .xml with this structure:

<?xml version="1.0" encoding="UTF-8"?>
<course id="8" contextid="78">
  <shortname>Test Course Short</shortname>
  <fullname>Test Course Long</fullname>
  <idnumber></idnumber>
  <summary></summary>
  <summaryformat>1</summaryformat>
  <format>topics</format>
  <showgrades>1</showgrades>
  <newsitems>5</newsitems>
  <startdate>1394668800</startdate>
  <marker>0</marker>
  <maxbytes>0</maxbytes>
  <legacyfiles>0</legacyfiles>
  <showreports>0</showreports>
  <visible>1</visible>
  <groupmode>0</groupmode>
  <groupmodeforce>0</groupmodeforce>
  <defaultgroupingid>0</defaultgroupingid>
  <lang></lang>
  <theme></theme>
  <timecreated>1394632991</timecreated>
  <timemodified>1394632991</timemodified>
  <requested>0</requested>
  <enablecompletion>0</enablecompletion>
  <completionnotify>0</completionnotify>
  <numsections>8</numsections>
  <hiddensections>0</hiddensections>
  <coursedisplay>0</coursedisplay>
  <category id="1">
    <name>Miscellaneous</name>
    <description>$@NULL@$</description>
  </category>
  <tags>
  </tags>
</course>

I know this sounds like basic PHP, but that's also pretty much what it is. How do I for example make this file with just PHP?

Was it helpful?

Solution

You could define the needed Moodle Files as PHP classes (with public attributes).

namespace Moodle\Models;
class Course {
  protected $id;
  protected $contextid;

  public $shortname;
  public $fullname;
  ...
}

to fill the Moodle Objects with the OLAT content you could write a little OLAT2Moodle Converter. The Converter would have several functions handling OLAT objects and creating Moodle Objects. You might want to use the factory design pattern for this.

In your factory methods you could use simple arrays to define source attributes for destination attributes and use a general method to fiull your object. Simple example.

function fillObject($sourceObject,$destinationObject,$conversationMap) {
  foreach (conversationMap as $destinationAttribute => $sourceAttribute) {
  {
    $destinationObject->$destinationAttribute = sourceObject->$sourceAttribute;
  }
}

$courseConversationMap = array(
  'id' => 'id',
  'title' => 'coursetitle',
  'desc' => 'description',
  'start' => 'startDate',
  ...
);

$OLAT_course_object = getOLAT_i_dont_know_how_you_do_that();
$moodleCourse = new Moodle\Course();

fillObject($OLAT_course_object ,$moodleCourse ,$courseConversationMap);

Now you just define the conversationMaps and put that logic in your factory methods. Then you just iterate over the given OLAT objects and call the factory create methods. Don't be scared - it is not Rocket Science nor Brain Surgery ;-)

Then use some PHP XML Library to convert said classes to XML and simply write the XML Objects to files.

Your Converter would be highly reuseable and you could easily write SomethingElse2Moodle Converters on it's Base.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top