Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
   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  
  41      /**
  42       * Constructor.
  43       *
  44       * @param course_format $format the course format
  45       * @param section_info $section the section info
  46       * @param cm_info $mod the course module ionfo
  47       */
  48      public function __construct(
  49          protected course_format $format,
  50          protected section_info $section,
  51          protected cm_info $mod,
  52      ) {
  53      }
  54  
  55      /**
  56       * Export this data so it can be used as the context for a mustache template.
  57       *
  58       * @param \renderer_base $output typically, the renderer that's calling this function
  59       * @return stdClass data context for a mustache template
  60       */
  61      public function export_for_template(\renderer_base $output): ?stdClass {
  62          global $USER;
  63  
  64          $course = $this->mod->get_course();
  65  
  66          $showcompletionconditions = $course->showcompletionconditions == COMPLETION_SHOW_CONDITIONS;
  67          $completiondetails = cm_completion_details::get_instance($this->mod, $USER->id, $showcompletionconditions);
  68  
  69          $showcompletioninfo = $completiondetails->has_completion() &&
  70              ($showcompletionconditions || $completiondetails->show_manual_completion());
  71          if (!$showcompletioninfo) {
  72              return null;
  73          }
  74  
  75          $completion = new activity_completion($this->mod, $completiondetails);
  76          $completiondata = $completion->export_for_template($output);
  77  
  78          if ($completiondata->isautomatic || ($completiondata->ismanual && !$completiondata->istrackeduser)) {
  79              $completiondata->completiondialog = $this->get_completion_dialog($output, $completiondata);
  80          }
  81  
  82          return $completiondata;
  83      }
  84  
  85      /**
  86       * Get the completion dialog.
  87       *
  88       * @param \renderer_base $output typically, the renderer that's calling this function
  89       * @param stdClass $completioninfo the completion info
  90       * @return array the completion dialog exported for template
  91       */
  92      private function get_completion_dialog(\renderer_base $output, stdClass $completioninfo): array {
  93          global $PAGE;
  94  
  95          $editurl = new \moodle_url(
  96              '/course/modedit.php',
  97              ['update' => $this->mod->id, 'showonly' => 'activitycompletionheader']
  98          );
  99          $completioninfo->editurl = $editurl->out(false);
 100          $completioninfo->editing = $PAGE->user_is_editing();
 101          $completioninfo->hasconditions = $completioninfo->ismanual || count($completioninfo->completiondetails) > 0;
 102          $dialogcontent = $output->render_from_template('core_courseformat/local/content/cm/completion_dialog', $completioninfo);
 103  
 104          $buttoncontent = get_string('completionmenuitem', 'completion');
 105          $buttonclass = '';
 106          if ($completioninfo->istrackeduser) {
 107              $buttoncontent = get_string('todo', 'completion');
 108              if ($completioninfo->overallcomplete) {
 109                  $buttoncontent = $output->pix_icon('i/checked', '') . " " . get_string('completion_manual:done', 'core_course');
 110                  $buttonclass = 'btn-success';
 111              }
 112          }
 113  
 114          $completiondialog = new dropdown_dialog($buttoncontent, $dialogcontent, [
 115              'classes' => 'completion-dropdown',
 116              'buttonclasses' => 'btn btn-sm dropdown-toggle icon-no-margin ' . $buttonclass,
 117              'dropdownposition' => dropdown_dialog::POSITION['end'],
 118          ]);
 119  
 120          return $completiondialog->export_for_template($output);
 121      }
 122  }