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
namespace core_course\output;
18
 
19
use cm_info;
20
use core_availability\info;
21
use core_completion\cm_completion_details;
22
use core_user;
23
use core_user\fields;
24
use renderable;
25
use renderer_base;
26
use stdClass;
27
use templatable;
28
 
29
/**
30
 * The activity completion renderable class.
31
 *
32
 * @package    core_course
33
 * @copyright  2023 Mikel Martín <mikel@moodle.com>
34
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
36
class activity_completion implements renderable, templatable {
37
 
38
    /**
39
     * Constructor.
40
     *
41
     * @param cm_info $cminfo The course module information.
42
     * @param cm_completion_details $cmcompletion The course module information.
43
     */
44
    public function __construct(
1441 ariadna 45
        /** @var cm_info $cminfo the activity cm_info. */
1 efrain 46
        protected cm_info $cminfo,
1441 ariadna 47
        /** @var cm_completion_details $cmcompletion the activity completion details. */
48
        protected cm_completion_details $cmcompletion,
49
        /** @var bool $smallbutton if the button is rendered small (like in course page). */
50
        protected bool $smallbutton = true,
1 efrain 51
    ) {
52
    }
53
 
54
    /**
55
     * Export this data so it can be used as the context for a mustache template.
56
     *
57
     * @param renderer_base $output Renderer base.
58
     * @return stdClass
59
     */
60
    public function export_for_template(renderer_base $output): stdClass {
61
        global $CFG;
62
 
63
        $overallcompletion = $this->cmcompletion->get_overall_completion();
64
        $isoverallcomplete = $this->cmcompletion->is_overall_complete();
65
        $overrideby = $this->get_overrideby();
66
        $course = $this->cminfo->get_course();
67
 
68
        // Whether the completion of this activity controls the availability of other activities/sections in the course.
69
        // An activity with manual completion tracking which is used to enable access to other activities/sections in
70
        // the course needs to refresh the page after having its completion state toggled. This withavailability flag will enable
71
        // this functionality on the course homepage. Otherwise, the completion toggling will just happen normally via ajax.
72
        if ($this->cmcompletion->has_completion() && $this->cmcompletion->is_manual()) {
73
            $withavailability = !empty($CFG->enableavailability) && info::completion_value_used($course, $this->cminfo->id);
74
        }
75
 
76
        return (object) [
77
            'cmid' => $this->cminfo->id,
78
            'activityname' => $this->cminfo->get_formatted_name(),
79
            'uservisible' => $this->cminfo->uservisible,
80
            'hascompletion' => $this->cmcompletion->has_completion(),
81
            'isautomatic' => $this->cmcompletion->is_automatic(),
82
            'ismanual' => $this->cmcompletion->is_manual(),
83
            'showmanualcompletion' => $this->cmcompletion->show_manual_completion(),
84
            'istrackeduser' => $this->cmcompletion->is_tracked_user(),
85
            'overallcomplete' => $isoverallcomplete,
86
            'overallincomplete' => !$isoverallcomplete,
87
            'withavailability' => $withavailability ?? false,
88
            'overrideby' => $overrideby,
89
            'completiondetails' => $this->get_completion_details($overrideby),
90
            'accessibledescription' => $this->get_accessible_description($overrideby, $overallcompletion),
1441 ariadna 91
            // For backward compatibility, the template uses small button by default when not set normal size.
92
            'normalbutton' => !$this->smallbutton,
1 efrain 93
        ];
94
    }
95
 
96
    /**
97
     * Returns the name of the user overriding the completion condition, if available.
98
     *
99
     * @return string
100
     */
101
    private function get_overrideby(): string {
102
        $overrideby = $this->cmcompletion->overridden_by();
103
        if (!empty($overrideby)) {
104
            $userfields = fields::for_name();
105
            $overridebyrecord = core_user::get_user($overrideby, 'id ' . $userfields->get_sql()->selects, MUST_EXIST);
106
            return fullname($overridebyrecord);
107
        }
108
        return '';
109
    }
110
 
111
    /**
112
     * Returns automatic completion details
113
     *
114
     * @param string $overrideby The name of the user overriding the completion condition, if available.
115
     * @return array
116
     */
117
    private function get_completion_details($overrideby): array {
118
        $details = [];
119
 
120
        foreach ($this->cmcompletion->get_details() as $key => $detail) {
121
            $detail->key = $key;
122
            $detail->statuscomplete = in_array($detail->status, [COMPLETION_COMPLETE, COMPLETION_COMPLETE_PASS]);
123
            $detail->statuscompletefail = $detail->status == COMPLETION_COMPLETE_FAIL;
124
            // This is not used by core themes but may be needed in custom themes.
125
            $detail->statuscompletepass = $detail->status == COMPLETION_COMPLETE_PASS;
126
            $detail->statusincomplete = $detail->status == COMPLETION_INCOMPLETE;
127
 
128
            // Add an accessible description to be used for title and aria-label attributes for overridden completion details.
129
            if ($overrideby) {
130
                $setbydata = (object)[
131
                    'condition' => $detail->description,
132
                    'setby' => $overrideby,
133
                ];
134
                $overridestatus = $detail->statuscomplete ? 'done' : 'todo';
135
                $detail->accessibledescription = get_string('completion_setby:auto:' . $overridestatus, 'course', $setbydata);
136
            }
137
 
138
            unset($detail->status);
139
            $details[] = $detail;
140
        }
141
        return $details;
142
    }
143
 
144
    /**
145
     * Returns the accessible description for manual completions with overridden completion state.
146
     *
147
     * @param string $overrideby The name of the user overriding the completion condition, if available.
148
     * @param int $overallcompletion The overall completion state of the activity.
149
     * @return string
150
     */
151
    private function get_accessible_description($overrideby, $overallcompletion): string {
152
        if ($this->cmcompletion->is_manual() && $overrideby) {
153
            $setbydata = (object)[
154
                'activityname' => $this->cminfo->get_formatted_name(),
155
                'setby' => $overrideby,
156
            ];
157
            $isoverallcompleted = $overallcompletion == COMPLETION_COMPLETE;
158
            $setbylangkey = $isoverallcompleted ? 'completion_setby:manual:done' : 'completion_setby:manual:markdone';
159
            return get_string($setbylangkey, 'course', $setbydata);
160
        }
161
        return '';
162
    }
163
}