| 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_courseformat\output\local\content\cm;
 | 
        
           |  |  | 18 |   | 
        
           |  |  | 19 | use cm_info;
 | 
        
           |  |  | 20 | use core_course\output\activity_completion;
 | 
        
           |  |  | 21 | use section_info;
 | 
        
           |  |  | 22 | use renderable;
 | 
        
           |  |  | 23 | use stdClass;
 | 
        
           |  |  | 24 | use core\output\named_templatable;
 | 
        
           |  |  | 25 | use core\output\local\dropdown\dialog as dropdown_dialog;
 | 
        
           |  |  | 26 | use core_completion\cm_completion_details;
 | 
        
           |  |  | 27 | use core_courseformat\base as course_format;
 | 
        
           |  |  | 28 | use core_courseformat\output\local\courseformat_named_templatable;
 | 
        
           |  |  | 29 |   | 
        
           |  |  | 30 | /**
 | 
        
           |  |  | 31 |  * Base class to render course module completion.
 | 
        
           |  |  | 32 |  *
 | 
        
           |  |  | 33 |  * @package   core_courseformat
 | 
        
           |  |  | 34 |  * @copyright 2023 Mikel Martin <mikel@moodle.com>
 | 
        
           |  |  | 35 |  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 36 |  */
 | 
        
           |  |  | 37 | class completion implements named_templatable, renderable {
 | 
        
           |  |  | 38 |   | 
        
           |  |  | 39 |     use courseformat_named_templatable;
 | 
        
           |  |  | 40 |   | 
        
           | 1441 | ariadna | 41 |     /** @var bool $smallbutton if the button is rendered small (like in course page). */
 | 
        
           |  |  | 42 |     private bool $smallbutton = true;
 | 
        
           |  |  | 43 |   | 
        
           | 1 | efrain | 44 |     /**
 | 
        
           |  |  | 45 |      * Constructor.
 | 
        
           |  |  | 46 |      *
 | 
        
           |  |  | 47 |      * @param course_format $format the course format
 | 
        
           |  |  | 48 |      * @param section_info $section the section info
 | 
        
           |  |  | 49 |      * @param cm_info $mod the course module ionfo
 | 
        
           |  |  | 50 |      */
 | 
        
           |  |  | 51 |     public function __construct(
 | 
        
           |  |  | 52 |         protected course_format $format,
 | 
        
           |  |  | 53 |         protected section_info $section,
 | 
        
           |  |  | 54 |         protected cm_info $mod,
 | 
        
           |  |  | 55 |     ) {
 | 
        
           |  |  | 56 |     }
 | 
        
           |  |  | 57 |   | 
        
           |  |  | 58 |     /**
 | 
        
           | 1441 | ariadna | 59 |      * Set if the button is rendered small.
 | 
        
           |  |  | 60 |      *
 | 
        
           |  |  | 61 |      * By default, the button is rendered small like in the course page. However,
 | 
        
           |  |  | 62 |      * in other pages like the activities overview, the button is rendered like
 | 
        
           |  |  | 63 |      * a regular button.
 | 
        
           |  |  | 64 |      *
 | 
        
           |  |  | 65 |      * @param bool $smallbutton if the button is rendered small (like in course page).
 | 
        
           |  |  | 66 |      */
 | 
        
           |  |  | 67 |     public function set_smallbutton(bool $smallbutton): void {
 | 
        
           |  |  | 68 |         $this->smallbutton = $smallbutton;
 | 
        
           |  |  | 69 |     }
 | 
        
           |  |  | 70 |   | 
        
           |  |  | 71 |     /**
 | 
        
           | 1 | efrain | 72 |      * Export this data so it can be used as the context for a mustache template.
 | 
        
           |  |  | 73 |      *
 | 
        
           |  |  | 74 |      * @param \renderer_base $output typically, the renderer that's calling this function
 | 
        
           | 1441 | ariadna | 75 |      * @return stdClass|null data context for a mustache template
 | 
        
           | 1 | efrain | 76 |      */
 | 
        
           |  |  | 77 |     public function export_for_template(\renderer_base $output): ?stdClass {
 | 
        
           |  |  | 78 |         global $USER;
 | 
        
           |  |  | 79 |   | 
        
           | 1441 | ariadna | 80 |         if (!$this->format->show_activity_editor_options($this->mod)) {
 | 
        
           |  |  | 81 |             return null;
 | 
        
           |  |  | 82 |         }
 | 
        
           |  |  | 83 |   | 
        
           | 1 | efrain | 84 |         $course = $this->mod->get_course();
 | 
        
           |  |  | 85 |   | 
        
           |  |  | 86 |         $showcompletionconditions = $course->showcompletionconditions == COMPLETION_SHOW_CONDITIONS;
 | 
        
           |  |  | 87 |         $completiondetails = cm_completion_details::get_instance($this->mod, $USER->id, $showcompletionconditions);
 | 
        
           |  |  | 88 |   | 
        
           |  |  | 89 |         $showcompletioninfo = $completiondetails->has_completion() &&
 | 
        
           |  |  | 90 |             ($showcompletionconditions || $completiondetails->show_manual_completion());
 | 
        
           |  |  | 91 |         if (!$showcompletioninfo) {
 | 
        
           |  |  | 92 |             return null;
 | 
        
           |  |  | 93 |         }
 | 
        
           |  |  | 94 |   | 
        
           | 1441 | ariadna | 95 |         $completion = new activity_completion($this->mod, $completiondetails, $this->smallbutton);
 | 
        
           | 1 | efrain | 96 |         $completiondata = $completion->export_for_template($output);
 | 
        
           |  |  | 97 |   | 
        
           |  |  | 98 |         if ($completiondata->isautomatic || ($completiondata->ismanual && !$completiondata->istrackeduser)) {
 | 
        
           |  |  | 99 |             $completiondata->completiondialog = $this->get_completion_dialog($output, $completiondata);
 | 
        
           |  |  | 100 |         }
 | 
        
           |  |  | 101 |   | 
        
           |  |  | 102 |         return $completiondata;
 | 
        
           |  |  | 103 |     }
 | 
        
           |  |  | 104 |   | 
        
           |  |  | 105 |     /**
 | 
        
           |  |  | 106 |      * Get the completion dialog.
 | 
        
           |  |  | 107 |      *
 | 
        
           |  |  | 108 |      * @param \renderer_base $output typically, the renderer that's calling this function
 | 
        
           |  |  | 109 |      * @param stdClass $completioninfo the completion info
 | 
        
           |  |  | 110 |      * @return array the completion dialog exported for template
 | 
        
           |  |  | 111 |      */
 | 
        
           | 1441 | ariadna | 112 |     protected function get_completion_dialog(\renderer_base $output, stdClass $completioninfo): array {
 | 
        
           | 1 | efrain | 113 |         global $PAGE;
 | 
        
           |  |  | 114 |   | 
        
           |  |  | 115 |         $editurl = new \moodle_url(
 | 
        
           |  |  | 116 |             '/course/modedit.php',
 | 
        
           |  |  | 117 |             ['update' => $this->mod->id, 'showonly' => 'activitycompletionheader']
 | 
        
           |  |  | 118 |         );
 | 
        
           |  |  | 119 |         $completioninfo->editurl = $editurl->out(false);
 | 
        
           |  |  | 120 |         $completioninfo->editing = $PAGE->user_is_editing();
 | 
        
           |  |  | 121 |         $completioninfo->hasconditions = $completioninfo->ismanual || count($completioninfo->completiondetails) > 0;
 | 
        
           |  |  | 122 |         $dialogcontent = $output->render_from_template('core_courseformat/local/content/cm/completion_dialog', $completioninfo);
 | 
        
           |  |  | 123 |   | 
        
           |  |  | 124 |         $buttoncontent = get_string('completionmenuitem', 'completion');
 | 
        
           | 1441 | ariadna | 125 |         $buttonclass = 'btn-subtle-body';
 | 
        
           | 1 | efrain | 126 |         if ($completioninfo->istrackeduser) {
 | 
        
           |  |  | 127 |             $buttoncontent = get_string('todo', 'completion');
 | 
        
           |  |  | 128 |             if ($completioninfo->overallcomplete) {
 | 
        
           |  |  | 129 |                 $buttoncontent = $output->pix_icon('i/checked', '') . " " . get_string('completion_manual:done', 'core_course');
 | 
        
           | 1441 | ariadna | 130 |                 $buttonclass = 'btn-subtle-success';
 | 
        
           | 1 | efrain | 131 |             }
 | 
        
           |  |  | 132 |         }
 | 
        
           |  |  | 133 |   | 
        
           | 1441 | ariadna | 134 |         $buttonclass .= $this->smallbutton ? ' btn-sm' : '';
 | 
        
           |  |  | 135 |   | 
        
           | 1 | efrain | 136 |         $completiondialog = new dropdown_dialog($buttoncontent, $dialogcontent, [
 | 
        
           |  |  | 137 |             'classes' => 'completion-dropdown',
 | 
        
           | 1441 | ariadna | 138 |             'buttonclasses' => 'btn dropdown-toggle icon-no-margin ' . $buttonclass,
 | 
        
           | 1 | efrain | 139 |             'dropdownposition' => dropdown_dialog::POSITION['end'],
 | 
        
           |  |  | 140 |         ]);
 | 
        
           |  |  | 141 |   | 
        
           |  |  | 142 |         return $completiondialog->export_for_template($output);
 | 
        
           |  |  | 143 |     }
 | 
        
           |  |  | 144 | }
 |