Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 401 and 402] [Versions 401 and 403]

   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   * Front-end class.
  19   *
  20   * @package availability_completion
  21   * @copyright 2014 The Open University
  22   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace availability_completion;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Front-end class.
  31   *
  32   * @package availability_completion
  33   * @copyright 2014 The Open University
  34   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class frontend extends \core_availability\frontend {
  37      /**
  38       * @var array Cached init parameters
  39       */
  40      protected $cacheparams = [];
  41  
  42      /**
  43       * @var string IDs of course, cm, and section for cache (if any)
  44       */
  45      protected $cachekey = '';
  46  
  47      protected function get_javascript_strings() {
  48          return ['option_complete', 'option_fail', 'option_incomplete', 'option_pass',
  49                          'label_cm', 'label_completion'];
  50      }
  51  
  52      protected function get_javascript_init_params($course, \cm_info $cm = null,
  53              \section_info $section = null) {
  54          // Use cached result if available. The cache is just because we call it
  55          // twice (once from allow_add) so it's nice to avoid doing all the
  56          // print_string calls twice.
  57          $cachekey = $course->id . ',' . ($cm ? $cm->id : '') . ($section ? $section->id : '');
  58          if ($cachekey !== $this->cachekey) {
  59              // Get list of activities on course which have completion values,
  60              // to fill the dropdown.
  61              $context = \context_course::instance($course->id);
  62              $cms = [];
  63              $modinfo = get_fast_modinfo($course);
  64              $previouscm = false;
  65              foreach ($modinfo->cms as $id => $othercm) {
  66                  // Add each course-module if it has completion turned on and is not
  67                  // the one currently being edited.
  68                  if ($othercm->completion && (empty($cm) || $cm->id != $id) && !$othercm->deletioninprogress) {
  69                      $cms[] = (object)['id' => $id,
  70                          'name' => format_string($othercm->name, true, ['context' => $context]),
  71                          'completiongradeitemnumber' => $othercm->completiongradeitemnumber];
  72                  }
  73                  if (count($cms) && (empty($cm) || $cm->id == $id)) {
  74                      $previouscm = true;
  75                  }
  76              }
  77              if ($previouscm) {
  78                  $previous = (object)['id' => \availability_completion\condition::OPTION_PREVIOUS,
  79                          'name' => get_string('option_previous', 'availability_completion'),
  80                          'completiongradeitemnumber' => \availability_completion\condition::OPTION_PREVIOUS];
  81                  array_unshift($cms, $previous);
  82              }
  83              $this->cachekey = $cachekey;
  84              $this->cacheinitparams = [$cms];
  85          }
  86          return $this->cacheinitparams;
  87      }
  88  
  89      protected function allow_add($course, \cm_info $cm = null,
  90              \section_info $section = null) {
  91          global $CFG;
  92  
  93          // Check if completion is enabled for the course.
  94          require_once($CFG->libdir . '/completionlib.php');
  95          $info = new \completion_info($course);
  96          if (!$info->is_enabled()) {
  97              return false;
  98          }
  99  
 100          // Check if there's at least one other module with completion info.
 101          $params = $this->get_javascript_init_params($course, $cm, $section);
 102          return ((array)$params[0]) != false;
 103      }
 104  }