Skip to content
Snippets Groups Projects
course_format_iena_completion.php 5.43 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/>.

/**
 *
 * This file give completion datas to get and display completion infos. 
 *
 * @package    format_iena
 * @category   format
 * @copyright  2018 Softia/Université lorraine
 * @author     Thomas Fradet
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
class course_format_iena_completion {

	public static function get_completion_by_section($section_names, $idSection) {

		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;

	}

	public static function get_completion_by_section_old() {

		global $COURSE, $DB, $USER;

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

		$modinfo = get_fast_modinfo($COURSE->id);

		$sections_infos = $modinfo->get_section_info_all();
		$section_ids = [];
		foreach ($sections_infos as $section) {
			array_push($section_ids, $section->__get('id') );
		}

		$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));

		$sections = [];

		foreach ($section_ids as $i => $section_id) {
			$section = new StdClass();
			$section->id = $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++;
					}
					$inner_modules[] = $mod;
				}
			}
			$section->modules = $inner_modules;
			if ( count($inner_modules) == 0 ) {
				$section->completion = false;
			} else {
				$section->completion = number_format( 100 * $inner_completed / count($inner_modules), 0 );
			}
			array_push($sections, $section);
		}

		// echo '<pre>';
		// var_dump($sections);
		// echo '</pre>';

		return $sections;


	}

}