License 
 *  - http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 *  
 * @param $oldversion Number of the old version. 
 */
require_once ($CFG->dirroot . '/mod/iassign/locallib.php');
function xmldb_iassign_upgrade ($oldversion) {
  global $CFG, $DB, $USER;
  $dbman = $DB->get_manager();
  //L if ($oldversion < 2019112219) {
  if ($oldversion < 2020070613) {
    $table = new xmldb_table('iassign_submission');
    $field = new xmldb_field('previous_grade', XMLDB_TYPE_FLOAT, null, null, null, null, null);
    if (!$dbman->field_exists($table, $field)) {
      $dbman->add_field($table, $field);
      }
    $records = array( // iLM adjusted to iAssign 2020/08/03
      array_combine( // iGeom 5.9.22
        array('name', 'url', 'version', 'type', 'description', 'extension', 'file_jar', 'file_class', 'width', 'height', 'enable', 'timemodified', 'author', 'timecreated', 'evaluate', 'reevaluate'),
        array('iGeom', 'http://www.matematica.br/igeom', '5.9.22', 'Java', '{"en":"Interactive Geometry on the Internet","pt_br":"Geometria Interativa na Internet"}', 'geo', 'ilm/iGeom/5.9.22/iGeom.jar', 'IGeomApplet.class', 800, 600, 1, time(), $USER->id, time(), 1, 0)),
      array_combine( // iGraf 4.4.0.10
        array('name', 'url', 'version', 'type', 'description', 'extension', 'file_jar', 'file_class', 'width', 'height', 'enable', 'timemodified', 'author', 'timecreated', 'evaluate', 'reevaluate'),
        array('iGraf', 'http://www.matematica.br/igraf', '4.4.0.10', 'Java', '{"en":"Interactive Graphic on the Internet","pt_br":"Gráficos Interativos na Internet"}', 'grf', 'ilm/iGraf/4.4.0.10/iGraf.jar', 'igraf.IGraf.class', 840, 600, 1, time(), $USER->id, time(), 1, 0)),
      array_combine( // iHanoi 1.0.20200803
        array('name', 'url', 'version', 'type', 'description', 'extension', 'file_jar', 'file_class', 'width', 'height', 'enable', 'timemodified', 'author', 'timecreated', 'evaluate', 'reevaluate'),
        array('iHanoi', 'http://www.matematica.br/ihanoi', '1.0.20200803', 'HTML5', '{"en":"interactive Tower os Hanoi (by LInE)", "pt_br":"Torres de Hanói (do LInE)"}', 'ihn', 'ilm/iHanoi/1.0.20200803/ihanoi/', 'index.html', 1100, 500, 1, time(), $USER->id, time(), 1, 0)),
      array_combine( // iVProg 1.0.20200221/ - HTML5 - 2020
        array('name', 'url', 'version', 'type', 'description', 'extension', 'file_jar', 'file_class', 'width', 'height', 'enable', 'timemodified', 'author', 'timecreated', 'evaluate', 'reevaluate'),
        array('iVProg', 'http://www.usp.br/line/ivprog/', '1.0.20200221', 'HTML5', '{"en":"Visual Interactive Programming on the Internet (HTML)","pt_br":"Programação visual interativa na Internet"}', 'ivph', 'ilm/iVProg/1.0.20200221/ivprog/', 'index.html', 800, 600, 1, time(), $USER->id, time(), 1, 1)),
      array_combine( // iFractions 0.1.20200221 - HTML5
        array('name', 'url', 'version', 'type', 'description', 'extension', 'file_jar', 'file_class', 'width', 'height', 'enable', 'timemodified', 'author', 'timecreated', 'evaluate', 'reevaluate'), 
        array('iFractions', 'http://www.matematica.br/ifractions', '0.1.20200221', 'HTML5', '{"en":"Interactive Fractions game","pt_br":"Jogo interativa de frações"}', 'frc', 'ilm/iFractions/0.1.20200221/ifractions/', 'index.html', 1000, 600, 1, time(), $USER->id, time(), 1, 0))
        );
    $iassign_ilm = $DB->get_records('iassign_ilm');
    $strNot_installed = '';
    foreach ($records as $record) { // this version 'iassign_ilm' does not has field 'reevaluate'
      $newentry = new stdClass();
      $newentry->name = $record['name'];
      $newentry->version = $record['version'];
      $newentry->type = $record['type'];
      $newentry->url = $record['url'];
      $newentry->description = $record['description'];
      $newentry->extension = $record['extension'];
      $newentry->file_jar = $record['file_jar'];
      $newentry->file_class = $record['file_class'];
      $newentry->width = $record['width'];
      $newentry->height = $record['height'];
      $newentry->enable = $record['enable'];
      $newentry->timemodified = time();
      $newentry->author = $USER->id;
      $newentry->timecreated = time();
      $newentry->evaluate = $record['evaluate'];
      $exists = 0;
      if ($iassign_ilm) { // Search if there is any iLM of this with this version
        $record_type = strtolower($record['type']);
        foreach ($iassign_ilm as $iassign) {
          if ($iassign->name == $record['name'] && strtolower($iassign->type) == $record_type) {
            if ($iassign->version == $record['version']) { // or with the one with the same version
              $exists = 1; // iLM found - exit with the last one, same version
              $strNot_installed += "\n" + '  
' + $record['name'] + ';' + $record['type'] + ';' + $record['version'] + ' ' + "\n";
              break;
              }
            }
          }
        }
      if ($exists == 0) try { // iLM does not exists or it has old version
        $DB->insert_record("iassign_ilm", $newentry, false);
      } catch (Exception $e) {
        print 'Caught exception: ' . $e->getMessage() . "
\n";
        }
      } // foreach ($records as $record)
    if ($strNot_installed != '') { // ATTENTION: this implies that Moodle administrator updated this iLM, please verify if it is really the current one.
      print '' + "\n";
      print get_string('upgrade_alert_exists', 'iassign'); // iLM previouly existent
      print ' 
' + "\n";
      print $strNot_installed;
      print ' 
' + "\n";
      print '
' + "\n";
      print get_string('upgrade_alert_iMA_msg', 'iassign'); // Updated but some previous iLM installed are not available in fresh iAssign
      print '
' + "\n";
      print $strNot_found;
      print '
' + "\n";
      print get_string('upgrade_alert_iMA_solution_pt1', 'iassign');
      print '
' . get_string('upgrade_alert_iMA_solution_pt2', 'iassign') . '.' . "\n";
      print '
' + "\n";
      print get_string('upgrade_alert_exists', 'iassign'); // iLM previouly existent
      print ' 
' + "\n";
      print $strNot_installed;
      print ' 
' + "\n";
      print '