Skip to content
Snippets Groups Projects
renderer.php 41.05 KiB
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * format_iena
 *
 * @package    format_iena
 * @category   format
 * @copyright  2018 Softia/Université lorraine
 * @author     vrignaud camille/Thomas Fradet
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

defined('MOODLE_INTERNAL') || die();

require_once($CFG->dirroot . '/course/format/topics/renderer.php');
require_once($CFG->dirroot . '/course/format/iena/view/view_course_header.php');

/**
 * format_iena_renderer
 *
 * @package    format_iena
 * @category   format
 * @copyright  2018 Softia/Université lorraine
 * @author     vrignaud camille
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
class format_iena_renderer extends format_topics_renderer{


	/**
	 * start_section_list
	 *
	 * @return string
	 */
	protected function start_section_list()
	{
		return html_writer::start_tag('ul', ['class' => 'iena-editing']);
	}

	/**
	 * section_header
	 *
	 * @param stdclass $section
	 * @param stdclass $course
	 * @param bool $onsectionpage
	 * @param int $sectionreturn
	 * @return string
	 */
	protected function section_header($section, $course, $onsectionpage, $sectionreturn = null, $iena = false){
		global $PAGE, $CFG;
		$o = '';
		$currenttext = '';
		$sectionstyle = '';
		if ($section->section != 0) {
			if (!$section->visible) {
				$sectionstyle = ' hidden';
			} elseif (course_get_format($course)->is_section_current($section)) {
				$sectionstyle = ' current';
			}
		}
		if ($PAGE->user_is_editing()) {
			$o .= html_writer::start_tag('li', ['id' => 'section-' . $section->section,
				'class' => 'section main clearfix' . $sectionstyle,
				'role' => 'region', 'aria-label' => get_section_name($course, $section)]);
		}
		
		$o .= html_writer::tag('span', $this->section_title($section, $course), ['class' => 'hidden sectionname']);

		if ($PAGE->user_is_editing()) {
			$leftcontent = $this->section_left_content($section, $course, $onsectionpage);
			$o .= html_writer::tag('div', $leftcontent, ['class' => 'left side']);
			$rightcontent = $this->section_right_content($section, $course, $onsectionpage);
			$o .= html_writer::tag('div', $rightcontent, ['class' => 'right side']);
		}

		$hasnamenotsecpg = (!$onsectionpage && ($section->section != 0 || !is_null($section->name)));
		$hasnamesecpg = ($onsectionpage && ($section->section == 0 && !is_null($section->name)));
		$classes = ' accesshide';
		if ($hasnamenotsecpg || $hasnamesecpg) {
			$classes = '';
		}
		$sectionname = html_writer::tag('span', $this->section_title($section, $course));
		if ($PAGE->user_is_editing()) {
			$o .= $this->output->heading($sectionname, 3, 'sectionname' . $classes);
		}
		// if ($course->showdefaultsectionname) {
		// 	$o .= $this->output->heading($sectionname, 3, 'sectionname' . $classes);
		// }
		// if (!$iena) { $o .= $sectionname; }

		$context = context_course::instance($course->id);
		
		/* résumé de section */
		if ( $section->summary != "" ) {
			$o .= html_writer::start_tag('div', ['class' => 'iena-description']);
			$o .= html_writer::start_tag('div', ['class' => 'small']);

			$o .= html_writer::start_tag('div', ['class' => 'iena-summary']);
			$o .= $this->format_summary_text($section);
			$o .= html_writer::end_tag('div');

			$o .= html_writer::end_tag('div');
			$o .= html_writer::end_tag('div');
			$o .= html_writer::tag('a', 'Voir la description complète', ['href' => '#', 'class' => 'voir-plus']);
		}
		
		$o .= $this->section_availability_message($section, has_capability('moodle/course:viewhiddensections', $context));

		if ($PAGE->user_is_editing()) {
			// $o .= html_writer::end_tag('li');
		}

		return $o;
	}

	/**
	 * Add style attributes, classes (useless?), format section summary and add the hidden/restricted message 
	 * @param type $section 
	 * @param type $course 
	 * @param type $onsectionpage 
	 * @param type|null $sectionreturn 
	 * @param type|bool $iena 
	 * @return type
	 */
	protected function get_section_header($section, $course, $onsectionpage, $sectionreturn = null, $iena = false, $groups=false){
	
		global $PAGE, $CFG, $COURSE, $USER;
		$section_entity = new course_format_iena_sections();
		$o = '';
		$currenttext = '';
		$sectionstyle = '';
		if ($section->section != 0) {
			if (!$section->visible) {
				$section->sectionstyle = ' hidden';
			} elseif (course_get_format($course)->is_section_current($section)) {
				$section->sectionstyle = ' current';
			}
		}
		

		$hasnamenotsecpg = (!$onsectionpage && ($section->section != 0 || !is_null($section->name)));
		$hasnamesecpg = ($onsectionpage && ($section->section == 0 && !is_null($section->name)));
		$section->classes = ' accesshide';
		if ($hasnamenotsecpg || $hasnamesecpg) {
			$section->classes = '';
		}
		$section->sectionname = get_section_name($course, $section);
		
		$context = context_course::instance($course->id);
		if($section->summary != ""){
			$section->summary=$this->format_summary_text($section);
		}
		
		$section->messageavailability= $this->section_availability_message($section, has_capability('moodle/course:viewhiddensections', $context));


		/* Paramètres */
		$presence = "";
		$format = course_get_format($course);
		
		$param_section = (object)$format->get_format_options($section);
		if ($param_section !== false) {

				if (isset($param_section->presence)) {
					if ($param_section->presence == 1) {
						$section->presence = "En présence";
						$string_date_presence="Pour le ";
					} else if ($param_section->presence == 2) {
						$section->presence = "À distance";
						$string_date_presence="Le ";
					}
				}

				if (isset($param_section->daterendu) && $param_section->daterendu != 0) {
					$section->dateUp = new DateTime("@$param_section->daterendu");
					$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");
					$section->dateUp = $section->dateUp->getTimestamp();
					if($string_date_presence){
						$section->string_date=$string_date_presence.$section->date_jour." à ".$section->date_heure;
					}
					else{
						$section->string_date="Le ".$section->date_jour." à ".$section->date_heure;
					}
				} else {
					$section->date = "";
				}
		} else {
			$section->date = "";
		}

		//Bouton de suivi dans chaque section : si pas d'activités suivies on n'affiche pas le bouton
		if (has_capability('course/iena:suivi', $context = context_course::instance($COURSE->id), $USER->id)) {
			$ressources_entity = new course_format_iena_section_ressources();
			$modules = $ressources_entity->get_ressources_completion_on_by_id_section($section->id);
			$nb_modules = count($modules);
			if($nb_modules>0){
				//si trop d'étudiants on va afficher le premier groupe dans le suivi
				if($this->count_students($context)>299 && count($groups)>0){
					$section->link_suivi = $CFG->wwwroot . "/course/format/iena/suivi_unit.php?courseid=" . $COURSE->id . "&sectionid=" . $section->id . "&groupid=".reset($groups)->id;
				}
				else{
					$section->link_suivi = $CFG->wwwroot . "/course/format/iena/suivi_unit.php?courseid=" . $COURSE->id . "&sectionid=" . $section->id . "&groupid=0";
				}
				
			}
			
			
		}


		return $section;
	}

	protected function get_section_header_edit($section, $course, $onsectionpage, $sectionreturn = null, $iena = false){
		global $PAGE, $CFG;
		$o = '';
		$currenttext = '';
		$sectionstyle = '';
		if ($section->section != 0) {
			if (!$section->visible) {
				$sectionstyle = ' hidden';
			} elseif (course_get_format($course)->is_section_current($section)) {
				$sectionstyle = ' current';
			}
		}
		
		$o .= html_writer::start_tag('li', ['id' => 'section-' . $section->section,
			'class' => 'section main clearfix' . $sectionstyle,
			'role' => 'region', 'aria-label' => get_section_name($course, $section)]);
		
		
		$o .= html_writer::tag('span', $this->section_title($section, $course), ['class' => 'hidden sectionname']);

		
		$leftcontent = $this->section_left_content($section, $course, $onsectionpage);
		$o .= html_writer::tag('div', $leftcontent, ['class' => 'left side']);
		$rightcontent = $this->section_right_content($section, $course, $onsectionpage);
		$o .= html_writer::tag('div', $rightcontent, ['class' => 'right side']);
		

		$hasnamenotsecpg = (!$onsectionpage && ($section->section != 0 || !is_null($section->name)));
		$hasnamesecpg = ($onsectionpage && ($section->section == 0 && !is_null($section->name)));
		$classes = ' accesshide';
		if ($hasnamenotsecpg || $hasnamesecpg) {
			$classes = '';
		}
		$sectionname = html_writer::tag('span', $this->section_title($section, $course));
		
		$o .= $this->output->heading($sectionname, 3, 'sectionname' . $classes);
		
		// if ($course->showdefaultsectionname) {
		// 	$o .= $this->output->heading($sectionname, 3, 'sectionname' . $classes);
		// }
		// if (!$iena) { $o .= $sectionname; }

		$context = context_course::instance($course->id);
		
		/* résumé de section */
		if ( $section->summary != "" ) {
			$o .= html_writer::start_tag('div', ['class' => 'iena-description']);
			$o .= html_writer::start_tag('div', ['class' => 'small']);

			$o .= html_writer::start_tag('div', ['class' => 'iena-summary']);
			$o .= $this->format_summary_text($section);
			$o .= html_writer::end_tag('div');

			$o .= html_writer::end_tag('div');
			$o .= html_writer::end_tag('div');
			$o .= html_writer::tag('a', 'Voir la description complète', ['href' => '#', 'class' => 'voir-plus']);
		}
		
		$o .= $this->section_availability_message($section, has_capability('moodle/course:viewhiddensections', $context));

		if ($PAGE->user_is_editing()) {
			// $o .= html_writer::end_tag('li');
		}

		return $o;
	}

	public function get_completion_by_section($idSection)
	{
		global $COURSE, $USER;
		$ressources_entity = new course_format_iena_section_ressources();
		$section_entity = new course_format_iena_sections();
		//$completions = $ressources_entity->get_completions_by_userid($USER->id, $COURSE->id);

		// list modules in a section
		$modules = $ressources_entity->get_ressources_completion_on_by_id_section($idSection);
		$nb_modules = count($modules);
		if ($nb_modules == 0) {
			return array($nb_modules, 999);
		}

		$valueI = 100 / $nb_modules;
		$valueTotal = 0;
		foreach ($modules as $module) {
			$complet = $ressources_entity->get_completions_by_module($USER->id, $COURSE->id, $module->id);
			if ($complet->completionstate != 0) {
				$valueTotal += $valueI;
			}
		}

		return array($modules, $valueTotal);
	}

	public function is_student($userId) {
		// do NOT trust this method
		global $COURSE;
		$course_ctx = context_course::instance($COURSE->id);
		foreach (get_user_roles($course_ctx, $userId) as $role) {
			if ($role->shortname == 'student') {
				return true;
			}
		}
	}

	public function get_render_competences($diSection){
		global $DB, $COURSE, $CFG, $USER;
		$section_entity = new course_format_iena_section_ressources();
		$modules = $section_entity->get_ressources_by_id_section($diSection);
		$id_competence = array();
		foreach ($modules as $module) {
			$comp_module = $DB->get_records('competency_modulecomp', array('cmid' => $module->id));
			if ($comp_module) {
				foreach ($comp_module as $comp){
					$id_competence[$comp->id] = $comp->competencyid;
				}
			}
		}
		$competences = array();
		foreach ($id_competence as $val) {
			$req = $DB->get_record('competency', array('id' => $val));
			$competences[$val] = $req->shortname;
		}
		if (count($competences) == 0) {
			return "";
		}
		$render = false;
		$render .= "
		<div class=\"dropdown d-inline\">
		<a href=\"#\" class=\"dropdown-toggle\" id=\"dropdown-2\" title=\"Actions\" role=\"button\" data-toggle=\"dropdown\"
		aria-haspopup=\"true\" aria-expanded=\"false\" style=\"color : white\">
		<i class=\"fa fa-lightbulb-o\" aria-hidden=\"true\"></i>
		</a>
		<div class=\"dropdown-menu dropdown- dropdown-menu-right menu align-tr-br\" id=\"action-menu-2-menu\" data-rel=\"menu-content\"
		aria-labelledby=\"action-menu-toggle-2\" role=\"menu\" data-align=\"tr-br\">
		<div class=\"contenu\">
		<div class='centered'>
		<h3>Compétences</h3>

		</div>
		<ul class='bulle'>
		";
		foreach ($competences as $key => $competence) {
			$render .= "<li><a href='" . $CFG->wwwroot . "/admin/tool/lp/user_competency_in_course.php?courseid=" . $COURSE->id .
			"&competencyid=" . $key . "&userid=" . $USER->id . "'> $competence </a></li>";
		}
		$render .= "
		</ul>
		<div class='centered'>
		<a href ='" . $CFG->wwwroot . "/admin/tool/lp/coursecompetencies.php?courseid=" . $COURSE->id . "'>
		<button class='btn  btn-sm  btn_blue' >Compétence du cours</button></a>
		</div>

		</div>
		</div>
		</div>";

		return $render;
	}

	public function get_view_iena($course, $htmlsection, $nameSection, $introSection, $idSection)
	{
		global $CFG, $COURSE, $USER;
		$section_entity = new course_format_iena_sections();

		//recommenté
		// $course_sections_instance = new course_format_iena_section_ressources();

		// $course_sections = new course_format_iena_sections();
		// $liste_sections = $course_sections->get_sections_by_id_course($COURSE->id);

		// $course_format_iena_groups_instance = new course_format_iena_groups();
		// $groups = $course_format_iena_groups_instance->get_groups_by_id_course($COURSE->id);
		// fin recommenté

		// $groups = [];

		//$students_group = $course_format_iena_groups_instance->get_students_group($course->id);


		// COUPE ICI



		// $tab_group_indicateur = array();

		// foreach($groups as $group)
		// {
		// 	$tab_student_completion = array();
		// 	$tab_section_indicateur = array();
		// 	foreach ($liste_sections as $section_ligne)
		// 	{
		// 		//get modules for one section
		// 		$modules_states_section = $course_sections->get_hidden_modules_by_section($section_ligne->id);

		// 		// if rows is empty in BDD => check count modules of student
		// 		// hide => cm (course module (activity) is linked to a section indicator)
		// 		// $nb_modules => numbre of activities linked to this section indicator
		// 		$nb_modules = 0;
		// 		foreach ($modules_states_section as $mod) {
		// 			if ($mod->hide == 1) {
		// 				$nb_modules++;
		// 			}
		// 		}

		// 		// Count each student of group who as not all validate in section
		// 		foreach ($group->list_userid as $student)
		// 		{
		// 			// si le user peut consulter le suivi, il n'est pas étudiant et ne doit pas y apparaître
		// 			if (has_capability('course/iena:suivi', $context = context_course::instance($COURSE->id), $student->userid)) {
		// 				continue;
		// 			}
		// 			//retrieving the student's module completion chart for this course (then check if module in section)
		// 			// $tab_student_completion = $course_sections_instance->get_completions_by_userid($student->userid, $COURSE->id);
		// 			$tab_student_completion = NULL;


		// 			// if no info completion of student is not in BDD
		// 			if (empty($tab_student_completion)) {
		// 				$indicateur = 1;
		// 			} else {
		// 				$compteur = 0;
		// 				foreach ($modules_states_section as $mod) {
		// 					if ($mod->hide == 1) {
		// 						$break_one = 0;
		// 						foreach ($tab_student_completion as $module_completion) {
		// 							if ($module_completion->coursemoduleid == $mod->cmid) {
		// 								$compteur++;
		// 								if ($module_completion->completionstate == 0) {
		// 									$indicateur = 1;
		// 									$break_one = 1;
		// 									break;
		// 								} else {
		// 									$indicateur = 0;
		// 								}
		// 							}
		// 						}
		// 						if ($break_one == 1) {
		// 							break;
		// 						}
		// 					}
		// 				}
		// 				if ($compteur < $nb_modules) {
		// 					if ($indicateur == 0) {
		// 						$indicateur = 1;
		// 					}
		// 				}
		// 			}

		// 			if ($this->is_student($student->userid) !== true) {
		// 				$indicateur = 0;
		// 			}

		// 			$tab_section_indicateur[$section_ligne->id] += $indicateur;

		// 		}

		// 		if ($nb_modules == 0) {
		// 			$tab_section_indicateur[$section_ligne->id] = "<a href=\"$CFG->wwwroot/course/format/iena/param_indicateur.php?courseid=$COURSE->id&sectionid=$section_ligne->id\">-</a>";
		// 		} else if ($tab_section_indicateur[$section_ligne->id] == NULL) {
		// 			$tab_section_indicateur[$section_ligne->id] = 0;
		// 		}
		// 	}
		// 	$tab_group_indicateur[$group->idnumber] = $tab_section_indicateur;

		// }

		// $course_ctx = context_course::instance($COURSE->id);
		// $students = get_enrolled_users($course_ctx, 'mod/assignment:submit', 0);
		// $tab_student_completion = array();
		// $tab_section_indicateur = array();
		// foreach ($liste_sections as $section_ligne) {
		// 	//retrieves modules for a section
		// 	$modules_states_section = $course_sections->get_hidden_modules_by_section($section_ligne->id);

		// 	// if empty lines in the DB => check student module counter
		// 	$nb_modules = 0;
		// 	foreach ($modules_states_section as $mod) {
		// 		if ($mod->hide == 1) {
		// 			$nb_modules++;
		// 		}
		// 	}

		// 	// Count each student in the course who has not validated everything in the
		// 	foreach ($students as $student) {
		// 		// si le user peut consulter le suivi, il n'est pas étudiant et ne doit pas y apparaître
		// 		if (has_capability('course/iena:suivi', $context = context_course::instance($COURSE->id), $student->id)) {
		// 			continue;
		// 		}
		// 		//retrieving the student's module completion chart for this course (then check if module in section)
		// 		// $tab_student_completion = $course_sections_instance->get_completions_by_userid($student->id, $COURSE->id);
		// 		$tab_student_completion = NULL;

		// 		// if no student completion information is in the database
		// 		if (empty($tab_student_completion)) {
		// 			$indicateur = 1;
		// 		} else {
		// 			$compteur = 0;
		// 			foreach ($modules_states_section as $mod) {
		// 				if ($mod->hide == 1) {
		// 					$break_one = 0;
		// 					foreach ($tab_student_completion as $module_completion) {
		// 						if ($module_completion->coursemoduleid == $mod->cmid) {
		// 							$compteur++;
		// 							if ($module_completion->completionstate == 0) {
		// 								$indicateur = 1;
		// 								$break_one = 1;
		// 								break;
		// 							} else {
		// 								$indicateur = 0;
		// 							}
		// 						}
		// 					}
		// 					if ($break_one == 1) {
		// 						break;
		// 					}
		// 				}
		// 			}
		// 			if ($compteur < $nb_modules) {
		// 				if ($indicateur == 0) {
		// 					$indicateur = 1;
		// 				}
		// 			}
		// 		}
		// 		$tab_section_indicateur[$section_ligne->id] += $indicateur;
		// 	}
		// 	if ($nb_modules == 0) {
		// 		$tab_section_indicateur[$section_ligne->id] = "<a href=\"$CFG->wwwroot/course/format/iena/param_indicateur.php?courseid=$COURSE->id&sectionid=$section_ligne->id\">-</a>";
		// 	} else if ($tab_section_indicateur[$section_ligne->id] == NULL) {
		// 		$tab_section_indicateur[$section_ligne->id] = 0;
		// 	}
		// }
		// $tab_group_indicateur['id_groupe0']=$tab_section_indicateur;
		// $view = "";



		// COUPE ICI
		

		// nouvelle génération du header

		$view = "";

		require_once($CFG->dirroot . '/course/format/iena/entity/course_format_iena_completion.php');
		// return false if completion disabled (site or course)
		$cpl = course_format_iena_completion::get_completion_by_section($nameSection, $idSection);

		/* Passer le calcul de complétion de la classe course_format_iena_completion à part à la vue qui fait le header permet de ne pas récupérer deux fois les mêmes informations. */
		$header = new view_course_header($nameSection, $idSection, $course, $cpl);
		$view .= $header->get_content();

		$i = 0;
		$link = $CFG->wwwroot . "/course/format/iena/suivi_unit.php?courseid=" . $COURSE->id;
		$view .= "<!-- <script defer src=\"https://use.fontawesome.com/releases/v5.0.8/js/all.js\"></script>  -->
		<style>

		/*
		style for clean page
		*/

		/* [BOOST] : course header */
		#page-header .card {
					border: none;
				}
		#page-header .card-body {
				padding-left: 0;
			}
			/* [BOOST] : left nav */
		#nav-drawer,
			[data-region='drawer'] {
				background-color: #fff;
			}
		#nav-drawer .list-group-item {
			border: 1px solid #ffffff;
		}
		#nav-drawer .list-group-item:hover {
		background-color: #e7e9ea;
		}
		#nav-drawer .list-group-item.active:hover {
		color: #000000;
		}

		/* [BOOST] : blocks */
		#block-region-side-pre section.block {
		border: none;
		}
		#block-region-side-pre section.block .card-body {
		padding-top: 0;
		}

		/* [BOOST] : margin for ul, because ul is used to list sections */

		ul, ul ul {
			padding-left: 2.5rem;
			margin-bottom: 0.5rem !important;
		}

		/* style for ? */

					#completionprogressid {
		display: none;
		}

		#region-main > .card {
		border: none;
		overflow-x: visible !important;
		overflow-y: visible;
		}

		#region-main > .card > .card-body {
		border: none;
		padding: 0;
		}

		.centered {
			display:flex;justify-content:center;align-items:center;
		}
		.contenu {
			min-width: 15rem;
		}
		ul.bulle {
			list-style: none;
			padding:5%;
		}

		.bulle {
			list-style: none;
		}

		.bulle > li {
			list-style: none;
			font-weight: normal;
			font-size: 0.8rem;
			line-height: 1rem;
			padding-top: 5%;
		}
		</style>";
		foreach ($htmlsection as $section) {
			$presence = "";
			if (!$section) {
				continue;
			}
			$param_section = $section_entity->get_section_settings_by_id_section($idSection[$i]);
			// dates, modality and notification section parameters : false if not set. 
			if ($param_section !== false) {
				if ($param_section->presence && $i != 0) {
					if ($param_section->presence == 1) {
						$presence = "En présence";
					} else if ($param_section->presence == 2) {
						$presence = "A distance";
					}
				}

				if ($param_section->date_rendu) {
					$dateUp = date_create($param_section->date_rendu);
					$date = $dateUp->format("j/m H:i");
					$date_jour = $dateUp->format("j/m");
					$date_heure = $dateUp->format("H:i");
					$dateUp = $dateUp->getTimestamp();
				} else {
					$date = "";
				}
			} else {
				$date = "";
			}

			$titre = $nameSection[$i];
			$sectionIntro = $introSection[$i];
						//If section is hidden continue
			if ($titre == null && !(has_capability('moodle/course:update', $context = context_course::instance($COURSE->id), $USER->id))) {
				$i++;
				continue;
			}
			$view .= "<section class=\"section iena-section\" id=\"section-$i\">
			<div class=\"card card_block\">
			<div class=\"heading-iena set_height\">";


			//&& !has_capability('course/iena:suivi', $context = context_course::instance($COURSE->id), $USER->id) 
			if ( $cpl != false && count($cpl->sections[$i]->modules) > 0 && !has_capability('course/iena:suivi', $context = context_course::instance($COURSE->id), $USER->id)) {
				$view .= "<div class='iena-percent set_height'>" . $cpl->sections[$i]->completion . "%</div>";
			}

			// if ($compl_by_section != 999 && $completion->is_enabled()) {
			// 	$view .= "<div class=\"iena-percent set_height\">
			// 	" . floor($compl_by_section) . "%
			// 	</div>";
			// }


			// if (has_capability('moodle/course:update', $context = context_course::instance($COURSE->id), $USER->id) && $i != 0) {

			
			if (has_capability('course/iena:suivi', $context = context_course::instance($COURSE->id), $USER->id) && $i != 0) {
			

				$course_groups = groups_get_all_groups($COURSE->id);

				$group_indicateur = 0;

				$view .= "<a href='$link&sectionid=".$idSection[$i]."style='color : white'>";
				$view .="<div onclick='change_grouplink(this)' style='display:block' class='nb_pers set_height id_groupe0'>";
				$view .= "Suivi étudiants";
				$view .= "</div>";

				/*foreach ($course_groups as $group) {
					$view .="<div onclick='change_grouplink(this)' style=\"display:none;\" class=\"nb_pers set_height id_groupe".$group->id."\">";
					$view .= "Suivi étudiants";
					$view .= "</div>";
				}*/
				$view .="</a>";
				
				// if (has_capability('course/iena:suivi_edit', $context = context_course::instance($COURSE->id), $USER->id) && $i != 0) {
				// 	$view .= "<a href=\"$CFG->wwwroot/course/format/iena/param_indicateur.php?courseid=$COURSE->id&sectionid=$idSection[$i]\" class=\"set_height\" style=\"color: white;float: left;line-height: 4rem;margin-left: 1rem;\">Editer suivi</a>";
				// }


				// id_groupeXXX


				// foreach($tab_group_indicateur as $group_indicateur => $val) {
				// 	$view .="<div onclick='change_grouplink(this)' style=\"display:none;\" class=\"nb_pers set_height ".$group_indicateur."\">";
				// 	foreach ($val as $key => $value) {
				// 		if ($idSection[$i] == $key) {
				// 			$view .= $value;
				// 		}
				// 	}
				// 	$view .= "</div>";
				// }

				// $view .="</a>";
			}



			$view .= "<div class=\"titre_section set_height\">
			<p>$titre</p>
			</div>
			<div class=\"right_info\">
			";
						// if ($presence) {
						// 	$view .= "
						// 	<div class=\"label_item\">
						// 	$presence
						// 	</div>";
						// }
			if ($date) {
				$link_date = $CFG->wwwroot . "/calendar/view.php?view=month&time=" . $dateUp . "&course=" . $COURSE->id;
				if ($presence && $presence == "A distance") {
					$view .= "
					<div class=\"label_item sect-date\">
					À distance
					</div>
					<div class=\"label_item sect-date\">
					Pour le $date_jour à $date_heure
					</div>";
				} else if ($presence && $presence == "En présence") {	
					$view .= "
					<div class=\"label_item sect-date\">
					En présence
					</div>
					<div class=\"label_item sect-date\">
					Le $date_jour à $date_heure
					</div>";
				} else {
					$view .= "
					<div class=\"label_item sect-date\">
					Le $date_jour à $date_heure
					</div>";
				}
			} else {
				if ($presence && $presence == "A distance") {
					$view .= "
					<div class=\"label_item sect-date\">
					À distance
					</div>";
				} else if ($presence && $presence == "En présence") {	
					$view .= "
					<div class=\"label_item sect-date\">
					En présence
					</div>";
				}
			}
			// $cpt = $this->get_render_competences($idSection[$i]);
			$cpt = "";
			if ($cpt != "") {
				$view .= "
				<div class=\"titre_section set_height\">

				";
				$view .= $cpt;
				$view .= "</div>
				";
			}

			$link_param = $CFG->wwwroot . "/course/format/iena/param_section.php?courseid=" . $COURSE->id . "&sectionid=" . $idSection[$i];
			if (has_capability('moodle/course:update', $context = context_course::instance($COURSE->id), $USER->id) && $i != 0) {
				$view .= "<div class=\"titre_section set_height\">
				<a href='$link_param' style=\"color : white\">
				<i class=\"fa fa-cog \" aria-hidden=\"true\" ></i>
				</a>
				</div>";
			}
			$view .= "</div>

			</div>
			";
			$view .= "<div class=\"wrapper section\">";
			$view .= $sectionIntro;
			// à présent géré au niveau de la génération du header de section par la méthode dédiée
			// if (strpos($sectionIntro, "</p>")) {
			// 	$view .= "<div class=\"iena-description\">";
			// 	$view .= "
			// 	<div class=\"small\">
			// 	$sectionIntro
			// 	</div>
			// 	</div>
			// 	<a href=\"#\">Voir la description complète</a>
			// 	";
			// 	$view .= "</div>";
			// }
			$view .= "<div class=\"wrapper\">
			$section
			</div >

			</div>

			</section>";
			$i++;
		}
		return $view;
	}

	public function get_view_iena_new($course,$nameSection, $idSection)
	{
		global $CFG, $COURSE, $USER;
		$section_entity = new course_format_iena_sections();

		

		$view = "";

		require_once($CFG->dirroot . '/course/format/iena/entity/course_format_iena_completion.php');
		// return false if completion disabled (site or course)
		$cpl = course_format_iena_completion::get_completion_by_section($nameSection, $idSection);

		/* Passer le calcul de complétion de la classe course_format_iena_completion à part à la vue qui fait le header permet de ne pas récupérer deux fois les mêmes informations. */
		$header = new view_course_header($nameSection, $idSection, $course, $cpl);
		$course_infos= $header->create_view($cpl);


		echo $this->render_from_template('format_iena/course-header', $course_infos);

		$i = 0;
		//$link = $CFG->wwwroot . "/course/format/iena/suivi_unit.php?courseid=" . $COURSE->id;




		$view .= "<!-- <script defer src=\"https://use.fontawesome.com/releases/v5.0.8/js/all.js\"></script>  -->
		<style>

		/*
		style for clean page
		*/

		/* [BOOST] : course header */
		#page-header .card {
					border: none;
				}
		#page-header .card-body {
				padding-left: 0;
			}
			/* [BOOST] : left nav */
		#nav-drawer,
			[data-region='drawer'] {
				background-color: #fff;
			}
		#nav-drawer .list-group-item {
			border: 1px solid #ffffff;
		}
		#nav-drawer .list-group-item:hover {
		background-color: #e7e9ea;
		}
		#nav-drawer .list-group-item.active:hover {
		color: #000000;
		}

		/* [BOOST] : blocks */
		#block-region-side-pre section.block {
		border: none;
		}
		#block-region-side-pre section.block .card-body {
		padding-top: 0;
		}

		/* [BOOST] : margin for ul, because ul is used to list sections */

		ul, ul ul {
			padding-left: 2.5rem;
			margin-bottom: 0.5rem !important;
		}

		/* style for ? */

					#completionprogressid {
		display: none;
		}

		#region-main > .card {
		border: none;
		overflow-x: visible !important;
		overflow-y: visible;
		}

		#region-main > .card > .card-body {
		border: none;
		padding: 0;
		}

		.centered {
			display:flex;justify-content:center;align-items:center;
		}
		.contenu {
			min-width: 15rem;
		}
		ul.bulle {
			list-style: none;
			padding:5%;
		}

		.bulle {
			list-style: none;
		}

		.bulle > li {
			list-style: none;
			font-weight: normal;
			font-size: 0.8rem;
			line-height: 1rem;
			padding-top: 5%;
		}
		</style>";
		
	}



	public function count_students($context){
		$count = count_enrolled_users($context);
		return $count;
	}




	public function print_iena_section_pages($course){
		global $PAGE, $USER;

		

		$context = context_course::instance($course->id);
		$course = course_get_format($course)->get_course();
		$completion = new \completion_info($course);


		$modinfo = get_fast_modinfo($course);
		// $course = course_get_format($course)->get_course();
		
		// $completioninfo = new completion_info($course);
		if (isset($_COOKIE['sectionvisible_' . $course->id])) {
			$sectionvisible = $_COOKIE['sectionvisible_' . $course->id];
		} elseif ($course->marker > 0) {
			$sectionvisible = $course->marker;
		} else {
			$sectionvisible = 1;
		}
		$htmlsection = false;
		$nameSection = [];
		$idSection = false;
		$introSection = false;
		
		//On récupère toutes les infos des sections
		foreach ($modinfo->get_section_info_all() as $section => $thissection) {

			//Nom de la section

			$htmlsection[$section] = '';
			
			$numsections = course_get_format($course)->get_last_section_number();

			// if ($section > $numsections) {
			// // if ($section > $course->numsections) {
			// 	// continue;
			// }
			/* if is not editing verify the rules to display the sections */
			if (!$PAGE->user_is_editing() && (!has_capability('moodle/course:viewhiddensections', $context = context_course::instance($course->id), $USER->id))) {

				//si la section est cachée
				if (isset($course->hiddensections) && !(int)$thissection->visible) {
					continue;
				}

				//si la section n'est pas disponible
				if (!$thissection->available && !empty($thissection->availableinfo)) {
					$htmlsection[$section] .= $this->section_header($thissection, $course, false, 0);
					continue;
				}

				if (!$thissection->uservisible || !$thissection->visible) {
					$htmlsection[$section] .= $this->section_hidden($section, $course->id);
					continue;
				}
			}


			//Affiche le nom de la section en mode propre sans lien
			$idSection[$section] = $thissection->id;
			// $nameSection[$section] .= $this->section_title_without_link($thissection, $course);
			$nameSection[$section] = $this->section_title_without_link($thissection, $course);
			if ($PAGE->user_is_editing()) {
				$htmlsection[$section] .= $this->section_header($thissection, $course, false, 0);
			}
			//$htmlsection[$section] .= $this->section_title($thissection,$course);
			//$introSection[$section] .= $this->section_header($thissection, $course, false, 0);
			$introSection[$section] = $this->section_header($thissection, $course, false, 0, true);
			if ($thissection->uservisible) {
				/* Ne pas enlever sinon le activity chooser ne fonctionne pas en JS */
				$htmlsection[$section] .= "<div class='content'>";
				// Renvoie le lien du cours avec icone
				$htmlsection[$section] .= $this->courserenderer->course_section_cm_list($course, $thissection, 0);
				$htmlsection[$section] .= $this->courserenderer->course_section_add_cm_control($course, $section, 0);
				$htmlsection[$section] .= "</div>";
			}
			/* pas de footer car on ne commence par la structure par une div non fermée et que ce footer ferme par une div */
			// $htmlsection[$section] .= $this->section_footer();
			
			
		} //ENDFOREACH

		// echo $completioninfo->display_help_icon();
		
		echo $this->output->heading($this->page_title(), 2, 'accesshide');
		echo $this->course_activity_clipboard($course, 0);
		echo $this->start_section_list();
		
		
		//A ce stade on à toutes les activité déja prête en HTML par contre il manque le nom des sections
		// Il se trouve dans : $thissection->name

		if ($PAGE->user_is_editing()) {
			// echo $completioninfo->display_help_icon();
			echo $this->output->heading($this->page_title(), 2, 'accesshide');
			echo $this->course_activity_clipboard($course, 0);
			echo $this->start_section_list();
		}




			//here all activities are displayed
			//If we are in etition mode then we display the "BASE" page Otherwise our model
		if (!$PAGE->user_is_editing()) {
			echo $this->get_view_iena($course, $htmlsection, $nameSection, $introSection, $idSection);
		} else {
			foreach ($htmlsection as $current) {
				echo $current;
			}
		}
			//At this stage nothing is yet displayed except the icon: Your progress
			// if ($course->sectionposition == 1 and isset($htmlsection0)) {
				// if ($PAGE->user_is_editing())
				// 	echo html_writer::tag('span', $htmlsection0, ['class' => 'below']);
			// }

			//Add the + and - at the end of the page to see which part of the code to really keep
		if ($PAGE->user_is_editing() and has_capability('moodle/course:update', $context)) {
			foreach ($modinfo->get_section_info_all() as $section => $thissection) {
				if ($section <= $numsections or empty($modinfo->sections[$section])) {
					continue;
				}
				echo $this->stealth_section_header($section);
				echo $this->courserenderer->course_section_cm_list($course, $thissection, 0);
				echo $this->stealth_section_footer();
			}
			echo $this->end_section_list();
			echo html_writer::start_tag('div', ['id' => 'changenumsections', 'class' => 'mdl-right']);
			$straddsection = get_string('addsections');
			$url = $url = new moodle_url('/course/changenumsections.php', ['courseid' => $course->id, 'insertsection' => 0, 'increase' => false, 'sesskey' => sesskey(), 'sectionreturn' => 0]);
			$icon = $this->output->pix_icon('t/switch_plus', $straddsection);
			echo html_writer::link($url, $icon . $straddsection, ['class' => 'increase-sections']);
				
			echo html_writer::end_tag('div');
		} else {
			echo $this->end_section_list();
		}
		echo html_writer::tag('style', '.course-content ul.iena #section-' . $sectionvisible . ' { display: block; }');
		if (!$PAGE->user_is_editing()) {
				//$PAGE->requires->js_init_call('M.format_iena.init', [$course->numsections]);
		}

	}

	public function get_iena_sections($course){
		global $PAGE, $USER;

		

		$context = context_course::instance($course->id);
		$course = course_get_format($course)->get_course();
		$completion = new \completion_info($course);
		$groups=groups_get_all_groups($course->id);
		


		$modinfo = get_fast_modinfo($course);
		
		if (isset($_COOKIE['sectionvisible_' . $course->id])) {
			$sectionvisible = $_COOKIE['sectionvisible_' . $course->id];
		} elseif ($course->marker > 0) {
			$sectionvisible = $course->marker;
		} else {
			$sectionvisible = 1;
		}
		$htmlsection = false;
		$nameSections = [];
		$idSections = [];
		$introSection = false;
	


		$sections=['sections'=>[]];


		

		foreach($modinfo->get_section_info_all() as $section => $thissection){
			if (!$PAGE->user_is_editing() && (!has_capability('moodle/course:viewhiddensections', $context = context_course::instance($course->id), $USER->id))) {
				if (isset($course->hiddensections) && !(int)$thissection->visible) {
					continue;
				}
				if (!$thissection->available && !empty($thissection->availableinfo)) {
					$thissection= $this->get_section_header($thissection, $course, false, 0, $groups);
					
					continue;
				}
				if (!$thissection->uservisible || !$thissection->visible) {
					$htmlsection = $this->section_hidden($section, $course->id);
					
					continue;
				}
			}
			$thissection=$this->get_section_header($thissection, $course, false, 0, true, $groups);
			if ($thissection->uservisible) {
				
				
				$thissection->resources= $this->courserenderer->course_section_cm_list($course, $thissection, 0);
				$thissection->addresources = $this->courserenderer->course_section_add_cm_control($course, $section, 0);
		
			}
			array_push($sections['sections'], $thissection);
			array_push($nameSections, $thissection->name);
			array_push($idSections, $thissection->id);
		}

		
		echo $this->get_view_iena_new($course, $nameSections, $idSections);
		echo $this->render_from_template('format_iena/sections', $sections);
		//On récupère toutes les infos des sections
		/*foreach ($modinfo->get_section_info_all() as $section => $thissection) {
			$htmlsection[$section] = '';
			
			$numsections = course_get_format($course)->get_last_section_number();
			if (!$PAGE->user_is_editing() && (!has_capability('moodle/course:viewhiddensections', $context = context_course::instance($course->id), $USER->id))) {
				if ($course->hiddensections && !(int)$thissection->visible) {
					continue;
				}
				if (!$thissection->available && !empty($thissection->availableinfo)) {
					$htmlsection[$section] .= $this->section_header($thissection, $course, false, 0);
					continue;
				}
				if (!$thissection->uservisible || !$thissection->visible) {
					$htmlsection[$section] .= $this->section_hidden($section, $course->id);
					continue;
				}
			}


			$idSection[$section] = $thissection->id;
			
			$nameSection[$section] = $this->section_title_without_link($thissection, $course);
			if ($PAGE->user_is_editing()) {
				$htmlsection[$section] .= $this->section_header($thissection, $course, false, 0);
			}
			$introSection[$section] = $this->section_header($thissection, $course, false, 0, true);
			if ($thissection->uservisible) {
				$htmlsection[$section] .= "<div class='content'>";
				
				$htmlsection[$section] .= $this->courserenderer->course_section_cm_list($course, $thissection, 0);
				$htmlsection[$section] .= $this->courserenderer->course_section_add_cm_control($course, $section, 0);
				$htmlsection[$section] .= "</div>";
			}
			
			
			
		} 
		
		echo $this->output->heading($this->page_title(), 2, 'accesshide');
		echo $this->course_activity_clipboard($course, 0);
		echo $this->start_section_list();
		
		

		if (!$PAGE->user_is_editing()) {
			echo $this->get_view_iena($course, $htmlsection, $nameSection, $introSection, $idSection);
		} else {
			foreach ($htmlsection as $current) {
				echo $current;
			}
		}
		

		
		
		echo html_writer::tag('style', '.course-content ul.iena #section-' . $sectionvisible . ' { display: block; }');
		*/
		
	}

	public function switch_mode($course){
		global $PAGE, $CFG;
		$context = context_course::instance($course->id);
		$course = course_get_format($course)->get_course();
		if($PAGE->user_is_editing() and has_capability('moodle/course:update', $context)){
			$this->print_iena_section_pages($course);
		}
		else{
			//$this->print_iena_section_pages($course);
			$this->get_iena_sections($course);
		}
	}
}