Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
/**
18
 * Contains class used to return completion progress information.
19
 *
20
 * @package    core_completion
21
 * @copyright  2017 Mark Nelson <markn@moodle.com>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace core_completion;
26
 
27
defined('MOODLE_INTERNAL') || die();
28
 
29
require_once($CFG->libdir . '/completionlib.php');
30
 
31
/**
32
 * Class used to return completion progress information.
33
 *
34
 * @package    core_completion
35
 * @copyright  2017 Mark Nelson <markn@moodle.com>
36
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37
 */
1436 ariadna 38
class progress
39
{
1 efrain 40
 
41
    /**
42
     * Returns the course percentage completed by a certain user, returns null if no completion data is available.
43
     *
44
     * @param \stdClass $course Moodle course object
45
     * @param int $userid The id of the user, 0 for the current user
46
     * @return null|float The percentage, or null if completion is not supported in the course,
47
     *         or there are no activities that support completion.
48
     */
1436 ariadna 49
    public static function get_course_progress_percentage($course, $userid = 0)
50
    {
1 efrain 51
        global $USER;
52
 
53
        // Make sure we continue with a valid userid.
54
        if (empty($userid)) {
55
            $userid = $USER->id;
56
        }
57
 
58
        $completion = new \completion_info($course);
59
 
60
        // First, let's make sure completion is enabled.
61
        if (!$completion->is_enabled()) {
62
            return null;
63
        }
64
 
65
        if (!$completion->is_tracked_user($userid)) {
66
            return null;
67
        }
68
 
69
        // Before we check how many modules have been completed see if the course has.
70
        if ($completion->is_course_complete($userid)) {
71
            return 100;
72
        }
73
 
74
        // Get the number of modules that support completion.
75
        $modules = $completion->get_activities();
76
        $count = count($modules);
77
        if (!$count) {
78
            return null;
79
        }
80
 
81
        // Get the number of modules that have been completed.
82
        $completed = 0;
83
        foreach ($modules as $module) {
84
            $data = $completion->get_data($module, true, $userid);
85
            if (($data->completionstate == COMPLETION_INCOMPLETE) || ($data->completionstate == COMPLETION_COMPLETE_FAIL)) {
86
                $completed += 0;
87
            } else {
88
                $completed += 1;
89
            };
90
        }
1436 ariadna 91
        if ($count == 0) {
92
            return 0;
93
        }
1 efrain 94
        return ($completed / $count) * 100;
95
    }
96
}