From 828f3ae6f9c0531a61c50e469dd9eaa9549fe27d Mon Sep 17 00:00:00 2001
From: Myriam Delaruelle
 <Myriam Delaruelle@bdn-un-mdelarue.ad.univ-lorraine.fr>
Date: Thu, 23 Sep 2021 14:56:32 +0200
Subject: [PATCH] Script upgrade db from old version

---
 db/install.xml |  4 +--
 db/upgrade.php | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++
 version.php    |  2 +-
 3 files changed, 76 insertions(+), 3 deletions(-)
 create mode 100644 db/upgrade.php

diff --git a/db/install.xml b/db/install.xml
index 1f5bd9c..8e5257d 100644
--- a/db/install.xml
+++ b/db/install.xml
@@ -3,7 +3,7 @@
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="../../../../lib/xmldb/xmldb.xsd"
 >
-  <TABLES>
+  <!--<TABLES>
     <TABLE NAME="format_iena" COMMENT="Default comment for format_iena, please edit me">
       <FIELDS>
         <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
@@ -21,5 +21,5 @@
         <KEY NAME="primary" TYPE="primary" FIELDS="id"/>
       </KEYS>
     </TABLE>
-  </TABLES>
+  </TABLES>-->
 </XMLDB>
\ No newline at end of file
diff --git a/db/upgrade.php b/db/upgrade.php
new file mode 100644
index 0000000..8616fe3
--- /dev/null
+++ b/db/upgrade.php
@@ -0,0 +1,73 @@
+<?php
+
+	
+
+function xmldb_format_iena_upgrade($oldversion) {
+    global $CFG, $DB;
+ 	$dbman = $DB->get_manager();
+    $result = TRUE;
+ 
+      if ($oldversion < 2021220900) {
+
+       
+
+        //Support for old versions: we copy format options to the course_format_options table
+        try {
+            $sections= $DB->get_records_sql('select * FROM {format_iena}');
+        } 
+        catch (dml_exception $e) {
+            echo "Aucun cours avec le format hybride dans la base";
+        }
+        if(count($sections) > 0){
+            foreach ($sections as $section) {
+                try {
+
+                    $courseid = $DB->get_record_sql('select course FROM {course_sections} WHERE id=?', array($section->id_section));
+                    
+                    $section_data = new stdClass();
+                    $section_data->courseid = $courseid->course;
+                    $section_data->format = "iena";
+                    $section_data->sectionid = $section->id_section;
+                    //On va faire pour présence, daterendu et daysnotif
+                    $section_data->name = "presence";
+                    $section_data->value = $section->presence;
+                    
+                    $exists = $DB->get_record_sql('select * FROM {course_format_options} WHERE sectionid=? AND name="presence"', array($section->id_section));
+                    
+                    if(!$exists){
+                       
+                        $resultat = $DB->insert_record('course_format_options', $section_data, true);
+                    }
+                    $exists = $DB->get_record_sql('select * FROM {course_format_options} WHERE sectionid=? AND name="daterendu"', array($section->id_section));
+                    if(!$exists){
+                       
+                        if(isset($section->date_rendu)){
+                            $section_data->name = "daterendu";
+                            $section_data->value = strtotime($section->date_rendu);
+                            $resultat = $DB->insert_record('course_format_options', $section_data, true);
+                        }
+                    }
+                    $exists = $DB->get_record_sql('select * FROM {course_format_options} WHERE sectionid=? AND name="daysnotif"', array($section->id_section));
+                    if(!$exists){
+                        
+                        if(!empty($section->day_before)){
+                            $section_data->name = "daysnotif";
+                          
+                            $section_data->value = $section->nb_days_before;
+                            $resultat = $DB->insert_record('course_format_options', $section_data, true);
+                        }
+                    }
+                } 
+                catch (dml_exception $e) {
+                }
+            }
+        }
+
+        // Format_iena savepoint reached.
+        upgrade_plugin_savepoint(true, 2021070600,'format', 'iena');
+    }
+ 
+    return $result;
+}
+
+?>
\ No newline at end of file
diff --git a/version.php b/version.php
index b3aee7f..1bc5d1a 100644
--- a/version.php
+++ b/version.php
@@ -28,7 +28,7 @@
 	
 	defined('MOODLE_INTERNAL') || die();
 	
-	$plugin->version = 2019072200;
+	$plugin->version = 2021220900;
 	$plugin->requires = 2014111000;
 	$plugin->component = 'format_iena';
 	$plugin->release = "1.0";
-- 
GitLab