From 7629ef7dbf506a160b5f226e7ce009b6ed89a618 Mon Sep 17 00:00:00 2001
From: Myriam Delaruelle
 <Myriam Delaruelle@bdn-un-mdelarue.ad.univ-lorraine.fr>
Date: Mon, 11 Jan 2021 15:14:13 +0100
Subject: [PATCH] Ajout daterendu aux format options

---
 lib.php      | 63 +++++++++++++++++++++++++++++++++++++++++++++++-----
 renderer.php | 17 ++++++++++----
 2 files changed, 70 insertions(+), 10 deletions(-)

diff --git a/lib.php b/lib.php
index a6a54f0..6087317 100644
--- a/lib.php
+++ b/lib.php
@@ -118,7 +118,12 @@
                 'presence'=>array(
                 	'label'=>"Présence",
                 	'type'=>PARAM_INT
-                )
+                ),
+                'daterendu'=>array(
+                	'label'=>"Date rendu",
+                	'type'=>PARAM_INT
+            	)
+
             );
         }
         if ($foreditform) {
@@ -167,23 +172,37 @@
         global $COURSE;
         global $PAGE;
         $elements = parent::create_edit_form_elements($mform, $forsection);
-        echo "\n hksddkh \n <br> <br> blabla <br>";
+
         if ($forsection) {
            	$mform->removeElement('presence', false);
+           	$mform->removeElement('daterendu', false);
            	$sectionclass = new stdClass();
            	$sectionclass->id=optional_param('id', 0, PARAM_INT);
            
           	$section_config=$this->get_format_options($sectionclass);
 
           	$radioarray=array();
-	    	$radioarray[] = $mform->createElement('radio', 'modalite', '', 'Distance', 2);
-			$radioarray[] = $mform->createElement('radio', 'modalite', '', 'Présentiel', 1);
-			$radioarray[] = $mform->createElement('radio', 'modalite', '', 'Aucune', 0);
+	    	$radioarray[] = $mform->createElement('radio', 'presence', '', 'Distance', 2);
+			$radioarray[] = $mform->createElement('radio', 'presence', '', 'Présentiel', 1);
+			$radioarray[] = $mform->createElement('radio', 'presence', '', 'Aucune', 0);
 			$mform->addGroup($radioarray, 'Modalité', 'Modalité', array(' '), false);
 
 
+			$datearray=array();
+			$datearray[] = $mform->createElement('date_time_selector', 'daterendu', '');
+			$datearray[] = $mform->createElement('checkbox', 'daterenducheck', 'Activer');
+			$mform->addGroup($datearray, 'groupdate', 'Date d\'échéance ou date du cours', array(' '), false);
+			//$mform->disabledIf('daterendu', 'daterenducheck');
+			$mform->disabledIf('groupdate', 'daterenducheck');
+			
+
+
           	if($section_config && $section_config['presence']){
-          		$mform->setDefault('modalite', $section_config['presence']);
+          		$mform->setDefault('presence', $section_config['presence']);
+          	}
+          	if($section_config && $section_config['daterendu']){
+          		$mform->setDefault('daterendu', $section_config['daterendu']);
+          		$mform->setDefault('daterenducheck', "1");
           	}
 	       
 	    	
@@ -207,6 +226,38 @@
         return $elements;
     }
 
+    /**
+     * Updates format options for a course
+     *
+     * In case if course format was changed to 'periods', we try to copy options
+     * 'coursedisplay', 'numsections' and 'hiddensections' from the previous format.
+     * If previous course format did not have 'numsections' option, we populate it with the
+     * current number of sections
+     *
+     * @param stdClass|array $data return value from {@link moodleform::get_data()} or array with data
+     * @param stdClass $oldcourse if this function is called from {@link update_course()}
+     *     this object contains information about the course before update
+     * @return bool whether there were any changes to the options values
+     */
+    public function update_section_format_options($data) {
+        global $DB;
+        
+        $data = (array)$data;
+        $section_config=$this->get_format_options($data['id']);
+        //if a date was set but we want to remove it
+        if(!isset($data['daterenducheck'])){
+        	if(array_key_exists('daterendu', $data)){
+        		$data['daterendu']='NULL';
+        	}
+        	else{
+        		unset($data['daterendu']);
+        	}
+        }
+        unset($data['daterenducheck']);
+
+        return $this->update_format_options($data, $data['id']);
+    }
+
 		
 		/**
 		 * get_view_url
diff --git a/renderer.php b/renderer.php
index f41649f..937325a 100644
--- a/renderer.php
+++ b/renderer.php
@@ -171,9 +171,13 @@ class format_iena_renderer extends format_topics_renderer{
 		/* Paramètres */
 		$presence = "";
 
-		$param_section = $section_entity->get_section_settings_by_id_section($section->id);
+		//$param_section = $section_entity->get_section_settings_by_id_section($section->id);
+		$format = course_get_format($course);
+		
+		$param_section = (object)$format->get_format_options($section);
 		if ($param_section !== false) {
-				if ($param_section->presence) {
+
+				if (isset($param_section->presence)) {
 					if ($param_section->presence == 1) {
 						$section->presence = "En présence";
 						$string_date_presence="Pour le ";
@@ -183,8 +187,13 @@ class format_iena_renderer extends format_topics_renderer{
 					}
 				}
 
-				if ($param_section->date_rendu) {
-					$section->dateUp = date_create($param_section->date_rendu);
+				if (isset($param_section->daterendu)) {
+					//$section->dateUp = date_create($param_section->date_rendu);
+					
+					$section->dateUp = new DateTime("@$param_section->daterendu");
+					
+					//$section->dateUp = $unformattedDate;
+					
 					$section->date = $section->dateUp->format("j/m H:i");
 					$section->date_jour = $section->dateUp->format("j/m");
 					$section->date_heure = $section->dateUp->format("H:i");
-- 
GitLab