Skip to content
Snippets Groups Projects
view_course_header.php 16.36 KiB
<?php

class view_course_header {

	private $_content = "";

	public function __construct($section_names, $idSection, $course, $progress = false) {

		global $CFG;

		$this->section_names = $section_names;
		$this->idSection = $idSection;
		// $this->completion_total = $completion_total;
		$this->course = $course;
		$this->create_view($progress);
	}

	private function create_view($progress) {
		
		// $this->get_progress_bis($this->section_names, $this->idSection, $this->course);
		
		$prog = $progress != false ? $progress : $this->get_progress_bis($this->section_names, $this->idSection, $this->course);

		$infos = [
			'progress' => $prog,
			'teachers' => $this->get_teachers(),
			'groups' => $this->get_groupes(),
			// 'attendance' => $this->get_attendance_link()
		];
		
		$this->set_html($infos);
	}

	private function get_progress_bis($section_names, $idSection, $course) {

		global $COURSE, $USER, $DB, $CFG;

		$completion = new \completion_info($COURSE);
		if ( $completion->is_enabled_for_site() == false || $completion->has_activities() == false || $completion->is_enabled() == 0 ) {
			return false;
		}

		// $modules = $DB->get_records_sql('SELECT cmc.id, cmc.coursemoduleid, cmc.userid, cmc.completionstate, cm.section
		// 	FROM  {course_modules_completion} as cmc
		// 	inner join {course_modules} as cm on cm.id = cmc.coursemoduleid
		// 	inner join {user} as u on u.id = cmc.userid
		// 	inner join {modules} as m on m.id = cm.module
		// 	where cm.course = ? and cm.deletioninprogress = 0
		// 	and cmc.userid = ?
		// 	order by section, coursemoduleid asc', array($course->id, $USER->id));

		// echo "<pre>";
		// var_dump($modules);
		// echo "</pre>";

		$sections = [];

		$fast_modinfo = get_fast_modinfo($course->id);
		$modinfos_cms = $fast_modinfo->get_cms();

		$modules = [];
		foreach ($modinfos_cms as $cm) {
			$module = $completion->get_data($cm, true, $USER->id, $fast_modinfo);
			$module->url = "$CFG->wwwroot/mod/$cm->modname/view.php?id=$cm->id";
			$module->section = $cm->section;
			if ( $cm->deletioninprogress == 0 && $cm->completion != 0 ) {
				$modules[] = $module;
			}
		}


		$total_completed = 0;
		$total_modules = 0;
		foreach ($idSection as $i => $section_id) {
			$section = new StdClass();
			$section->id = $i;
			$section->name = $section_names[$i];
			$inner_modules = [];
			$inner_completed = 0;
			foreach ($modules as $module) {
				if ( $module->section == $section_id ) {
					$mod = new StdClass();
					$mod->coursemoduleid = $module->coursemoduleid;
					$mod->completion = $module->completionstate;
					if ( $mod->completion == 1 || $mod->completion == 2 ) {
						$inner_completed++;
					}
					foreach ($modinfos_cms as $cm) {
						if ( $cm->id == $mod->coursemoduleid ) {
							$mod->name = $cm->name;
							$mod->url = "$CFG->wwwroot/mod/$cm->modname/view.php?id=$cm->id";
						}
					}
					$inner_modules[] = $mod;
				}
			}
			$section->modules = $inner_modules;
			if ( count($inner_modules) == 0 ) {
				$section->completion = 0;
			} else {
				$section->completion = number_format( 100 * $inner_completed / count($inner_modules), 0 );
			}
			$total_completed += $inner_completed;
			$total_modules += count($inner_modules);
			array_push($sections, $section);
		}

		$progress = new StdClass();
		$progress->total = number_format( 100 * $total_completed / $total_modules, 0);
		$progress->sections = $sections;

		// echo "<pre>";
		// var_dump($progress);
		// echo "</pre>";

		return $progress;

	}

	// private function get_progress($section_names, $idSection, $completion_total) {
	// 	global $COURSE, $USER;

	// 	/* L'achèvement d'activité est activé dans le cours */
	// 	$completion = new \completion_info($COURSE);
	// 	if ( $completion->is_enabled() == 0 ) { return false; }

	// 	/* L'affichage de la complétion est activé dans les paramètres du format de cours */
	// 	$course_params = course_get_format($COURSE->id)->get_course();
	// 	$show_progress = $course_params->viewbreadcrum == 1 ? true : false;
	// 	if ( ! $show_progress ) { return false; }

	// 	$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);
	// 	$modinfo = get_fast_modinfo($COURSE->id);
	// 	$progress = array();
	// 	$content = false;
	// 	$i = 0; // initialement = 1 mais pb avec la section 0
	// 	$total_modules = 0;
	// 	$total_completed_modules = 0;
	// 	$s_it = 0;
	// 	$progress['sections'] = [];
	// 	foreach ($section_entity->get_sections_by_id_course($COURSE->id) as $key => $section) {
	// 		// if ( $section->section == 0 ) {
	// 		// 	continue;
	// 		// }
	// 		if ($section_names[$i]) {
	// 			$sec_name = $section_names[$i];
	// 			$date_section = $section_entity->get_section_settings_by_id_section($idSection[$i]);
	// 			$chaine = mb_strimwidth($section_names[$i], 0, 20, "...");
	// 			$completion_by_section = $this->get_completion_by_section($idSection[$i]);
	// 			$section_modules = $completion_by_section[0];
	// 			$compl_by_section = $completion_by_section[1];
	// 			if ( $compl_by_section != 999 ) {
	// 				$total_modules += count($section_modules);
	// 				$pourcentage_section = $compl_by_section;
	// 			} else {
	// 				$pourcentage_section = 0;
	// 			}
	// 			if (floor($pourcentage_section) >= 100) {
	// 				$couleur = "done";
	// 			} else {
	// 				$couleur = "done";
	// 			}
	// 			$completion_ok = 0;
	// 			$completion_encours = 0;
	// 			$completion_pasfait = 0;
	// 			$modules = array();
	// 			foreach ($modinfo->get_cms() as $cm) {
	// 				$in_modules = false;
	// 				foreach ($section_modules as $mod) {
	// 					if ($mod->id == $cm->id) {
	// 						$in_modules = true;
	// 					}
	// 				}
	// 				if ($cm->section == $idSection[$i] && $cm->completion != 0 && $cm->deletioninprogress == 0 && $in_modules) {
	// 					$dateUp = date_create($date_section->date_rendu);
	// 					if ($date_section->date_rendu) {
	// 						$date1 = $dateUp->getTimestamp();
	// 						$date2 = date_create(date("Y-m-d H:i:s"))->getTimestamp();
	// 						$nbJoursTimestamp = $date1 - $date2;
	// 						$nbJours = $nbJoursTimestamp / 86400;
	// 					} else {
	// 						$nbJours = 0;
	// 					}
	// 					$color = "";
	// 					foreach ($completions as $completion) {
	// 						if ($completion->coursemoduleid == $cm->id && $completion->completionstate != 0) {
	// 							$completion_ok++;
	// 							$total_completed_modules++;
	// 							$color = "done";
	// 							break;
	// 						}
	// 					}
	// 					if ($color == "") {
	// 						if ($nbJours > 0) {
	// 							$color = "todo";
	// 							$completion_pasfait++;
	// 						} else if ($nbJours < 0) {
	// 							$color = "late";
	// 							$completion_encours++;
	// 						} else {
	// 							$color = "todo";
	// 							$completion_pasfait++;
	// 						}
	// 					}
	// 					if ($section_modules !== 0) {
	// 						array_push($modules, [
	// 							'name' => $cm->name,
	// 							'link' => $cm->url,
	// 							'completion' => $color
	// 						]);
	// 					}
	// 				}
	// 			}
	// 		}
	// 		array_push($progress['sections'], [
	// 			'name' => $sec_name,
	// 			'id' => $i,
	// 			'completion' => floor($pourcentage_section),
	// 			'modules' => $modules
	// 		]);
	// 		$i++;
	// 		$s_it++;
	// 	}
	// 	if ($total_modules > 0) {
	// 		$progress["total"] = floor($total_completed_modules*100/$total_modules);
	// 	} else {
	// 		$progress["total"] = false;
	// 	}
	// 	$content .= "</div>";

	// 	// echo "<pre>";
	// 	// var_dump($progress);
	// 	// echo "</pre>";

	// 	return $progress;

	// }

	private function get_completion_by_section($idSection) {
		global $COURSE, $USER;
		$ressources_entity = new course_format_iena_section_ressources();
		$section_entity = new course_format_iena_sections();
		$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);
	}

	private function get_teachers() {
		global $COURSE, $CFG, $USER;
		$course_params = course_get_format($COURSE->id)->get_course();
		if ( $course_params->viewiconmessage == 0 ) {
			return false;
		}
		$course_ctx = context_course::instance($COURSE->id);
		$enrolled_users = get_enrolled_users($course_ctx);
		$teachers = [];
		foreach ($enrolled_users as $user) {
			if ($this->is_teacher($user)) {
				$teachers[] = $user;
			}
		}
		return $teachers;
	}

	private function is_teacher($user) {
		global $COURSE;
		$course_ctx = context_course::instance($COURSE->id);
		foreach (get_user_roles($course_ctx, $user->id) as $role) {
			if ($role->shortname == 'teacher' || $role->shortname == 'editingteacher') {
				return true;
			}
		}
		return false;
	}

	private function get_groupes() {
		global $COURSE, $USER;
		if ( ! $this->is_teacher($USER) ) { return false; }
		$groups = groups_get_all_groups($COURSE->id);
		$array = [];
		foreach ($groups as $group) {
			$array[] = [
				'id' => $group->id,
				'name' => $group->name
			];
		}
		return $array;
	}

	private function set_html($infos) {
		
		global $CFG, $COURSE, $USER;

		// $css = file_get_contents( $CFG->wwwroot.'/course/format/iena/course-header.css' );
		// $this->_content .= '<style>'.$css.'</style>';

		$this->_content .= '<div class="iena-course-header">';


		// begin of : top part (always visible)
		$this->_content .= '<div class="iena-course-header-top">';

		// PROGRESS TOTAL : total progression percentage
		if ( $infos['progress'] !== false && $infos['progress']->total !== false ) {
			$this->_content .= '<a href="#" class="btn btn-outline-primary iena-course-h-total" onclick="iena_toggle_course_header(event)">Ma progression : '.$infos['progress']->total.'%</a>';
		}
		else {
			$this->_content .= '<div class="btn"></div>';
		}
		// $this->_content .= '<a href="#" class="btn btn-outline-primary iena-course-h-total" onclick="iena_toggle_course_header(event)">Ma progression :%</a>';

		// MESSAGES : button to send message to teachers
		if ( $infos['teachers'] !== false ) {
			$this->_content .= '<div class="dropdown iena-course-h-message"><a class="btn btn-outline-primary dropdown-toggle" href="#" role="button" id="iena-course-h-message" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><span class="sr-only">Contact : </span>Enseignants</a><div class="dropdown-menu dropdown-menu-right" aria-labelledby="iena-course-h-message">';
			$my_group_teachers = [];
			foreach ($infos['teachers'] as $teacher) {
				// echo "<pre>";
				// var_dump($teacher->id);
				$teacher_groups = groups_get_user_groups($COURSE->id, $teacher->id)[0];
				// var_dump($teacher_groups);
				$user_groups = groups_get_user_groups($COURSE->id, $USER->id)[0];
				// var_dump($user_groups);
				$user_is_in_teacher_group = false;
				foreach ($user_groups as $user_group) {
					if ( in_array($user_group, $teacher_groups) ) {
						$user_is_in_teacher_group = true;
						$my_group_teachers[] = $teacher;
						break;
					}
				}
				// var_dump($user_is_in_teacher_group);
				// echo "<hr>";
				// echo "</pre>";
			}
			if ( count($my_group_teachers) > 0 ) {	
				$this->_content .= '<span class="dropdown-header">Mon groupe</span>';
				foreach ($my_group_teachers as $teacher) {
					$this->_content .= '<a class="dropdown-item" target="_blank" href="'.$CFG->wwwroot.'/message/index.php?id='.$teacher->id.'"><span class="sr-only">Envoyer un message à (nouvel onglet) : </span>'.$teacher->firstname.' '.$teacher->lastname.'</a>';
				}
				$this->_content .= '<span class="dropdown-header">Tous les groupes</span>';
			}
			foreach ($infos['teachers'] as $teacher) {
				$this->_content .= '<a class="dropdown-item" target="_blank" href="'.$CFG->wwwroot.'/message/index.php?id='.$teacher->id.'"><span class="sr-only">Envoyer un message à (nouvel onglet) : </span>'.$teacher->firstname.' '.$teacher->lastname.'</a>';
			}
			$this->_content .= '</div></div>';
		}

		// ATTENDANCE : button to help with attendance mod if one
		// if ( $infos['attendance'] !== false ) {
		// 	$this->_content .= '<a href="'.$infos['attendance'].'" class="btn btn-outline-warning iena-course-h-attendance" id="iena-attendance-btn">Présences</a>';
		// }

		// GROUP : teacher group select (input-group-lg if needed)
		if ( $infos['groups'] !== false ) {
			$this->_content .= '<script type=\"text/javascript\" charset=\"utf8\" src=\"https://code.jquery.com/jquery-3.3.1.min.js\"></script>';
			$this->_content .= '<script src="'.$CFG->wwwroot.'/course/format/iena/js/groups.js"></script>';
			$this->_content .= '<script>function change_grouplink(obj) {
				var classList = obj.classList;
				var id_groupe = classList[2].split("id_groupe");
				id_group = id_groupe[1];
				var t = obj.parentNode;
				var t_href = t.getAttribute("href");
				t_href += "&groupid=" + id_group;
				t.setAttribute("href", t_href); 
			}
			</script>';
			$this->_content .= '<span class="form-inline iena-course-h-group">
			<div class="input-group-prepend">
			<label class="input-group-text orange-bg" for="select-group">Groupe</label>
			</div>
			<select class="custom-select form-control" id="select-group">';
			$this->_content .= '<option value="id_groupe0" selected>Tous les groupes</option>';
			$this->_content .= '<optgroup label="Mes groupes">';
			$user_groups = groups_get_user_groups($COURSE->id, $USER->id)[0];
			foreach ($infos['groups'] as $group) {
				if ( in_array($group['id'], $user_groups ) ) {
					$this->_content .= '<option value="id_groupe'.$group['id'].'">'.$group['name'].'</option>';
				}
			}
			$this->_content .= '</optgroup>';

			if (has_capability('course/iena:suivi_edit', $context = context_course::instance($COURSE->id), $USER->id)) {
				$this->_content .= '<optgroup label="Tous les groupes">';
				foreach ($infos['groups'] as $group) {
					$this->_content .= '<option value="id_groupe'.$group['id'].'">'.$group['name'].'</option>';
				}
				$this->_content .= '</optgroup>';
			}

			$this->_content .= '</select>
			</span>';
		}

		$this->_content .= '</div><div class="iena-h-clear"></div>'; // end of : top


		// begin of : bottom part visible at toggle
		$this->_content .= '<div class="iena-course-header-bottom" id="iena-h-bottom" aria-expanded="false" style="display:none;">';
		// $this->_content .= '<div class="iena-course-header-bottom" id="iena-h-bottom" aria-expanded="true">';


		// PROGRESS DETAIL
		if ( $infos['progress'] !== false && $infos['progress']->total !== false ) {
			// section progress begin
			$this->_content .= '<div class="iena-h-prog-sect"><h2>Progression détaillée</h2>';
			// section progress total
			foreach ($infos['progress']->sections as $section) {
				if ( count($section->modules) > 0 ) {
					$this->_content .= '<a href="#section-'.$section->id.'" class="iena-h-prog-name btn btn-outline-success"><strong>'.$section->completion.'%</strong> – '.$section->name.'</a>';
					// section progress each module
					foreach ($section->modules as $module) {
						$this->_content .= '<a href="'.$module->url.'" class="iena-h-prog-mod-item iena-g-prog-'.$module->completion.'"><span>'.$module->name.'</span></a>';
					}
				}
			}
			$this->_content .= '</div><div class="iena-h-clear"></div>';
		}

		// course summary
		$summary_copy = $COURSE->summary;
		$to_delete = array('<p>', '</p>', '<br>', '</br>', ' ');
		$summary_copy = str_replace($to_delete, '', $summary_copy);
		$summary_copy = preg_replace('/\s+/', '', $summary_copy);
		if ( $summary_copy != "" ) {
			$this->_content .= '<hr><h2>À propos du cours</h2><div class="iena-h-summary">' . $COURSE->summary . '</div>';
		}

		$this->_content .= '</div>'; // end of : bottom part

		if ( $summary_copy != "" ) {
			$this->_content .= '<div class="iena-course-header-toggler" id="iena-h-toggler"><hr><a href="#" onclick="iena_toggle_course_header(event)">Afficher / masquer les infos du cours</a></div>';
		}

		$this->_content .= '<script>function iena_toggle_course_header(event) { event.preventDefault(); var bottom = document.getElementById("iena-h-bottom"); if(bottom.style.display == "none") { bottom.style.display = ""; bottom.setAttribute("aria-expanded", true); } else { bottom.style.display = "none"; bottom.setAttribute("aria-expanded", false); } }</script>';

		$this->_content .= '</div>'; // end of : iena course header
	}

	public function get_content() {
		return $this->_content;
	}

}