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  /**
  18   * Self completion block.
  19   *
  20   * @package   block_selfcompletion
  21   * @copyright 1999 onwards Martin Dougiamas  {@link http://moodle.com}
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  require_once($CFG->libdir.'/completionlib.php');
  26  
  27  /**
  28   * Self course completion marking
  29   * Let's a user manually complete a course
  30   *
  31   * Will only display if the course has completion enabled,
  32   * there is a self completion criteria, and the logged in user is yet
  33   * to complete the course.
  34   */
  35  class block_selfcompletion extends block_base {
  36  
  37      public function init() {
  38          $this->title = get_string('pluginname', 'block_selfcompletion');
  39      }
  40  
  41      function applicable_formats() {
  42          return array('course' => true);
  43      }
  44  
  45      public function get_content() {
  46          global $CFG, $USER;
  47  
  48          // If content is cached
  49          if ($this->content !== NULL) {
  50            return $this->content;
  51          }
  52  
  53          // Create empty content
  54          $this->content = new stdClass;
  55  
  56          // Can edit settings?
  57          $can_edit = has_capability('moodle/course:update', context_course::instance($this->page->course->id));
  58  
  59          // Get course completion data
  60          $info = new completion_info($this->page->course);
  61  
  62          // Don't display if completion isn't enabled!
  63          if (!completion_info::is_enabled_for_site()) {
  64              if ($can_edit) {
  65                  $this->content->text = get_string('completionnotenabledforsite', 'completion');
  66              }
  67              return $this->content;
  68  
  69          } else if (!$info->is_enabled()) {
  70              if ($can_edit) {
  71                  $this->content->text = get_string('completionnotenabledforcourse', 'completion');
  72              }
  73              return $this->content;
  74          }
  75  
  76          // Get this user's data
  77          $completion = $info->get_completion($USER->id, COMPLETION_CRITERIA_TYPE_SELF);
  78  
  79          // Check if self completion is one of this course's criteria
  80          if (empty($completion)) {
  81              if ($can_edit) {
  82                  $this->content->text = get_string('selfcompletionnotenabled', 'block_selfcompletion');
  83              }
  84              return $this->content;
  85          }
  86  
  87          // Check this user is enroled
  88          if (!$info->is_tracked_user($USER->id)) {
  89              $this->content->text = get_string('nottracked', 'completion');
  90              return $this->content;
  91          }
  92  
  93          // Is course complete?
  94          if ($info->is_course_complete($USER->id)) {
  95              $this->content->text = get_string('coursealreadycompleted', 'completion');
  96              return $this->content;
  97  
  98          // Check if the user has already marked themselves as complete
  99          } else if ($completion->is_complete()) {
 100              $this->content->text = get_string('alreadyselfcompleted', 'block_selfcompletion');
 101              return $this->content;
 102  
 103          // If user is not complete, or has not yet self completed
 104          } else {
 105              $this->content->text = '';
 106              $this->content->footer = '<br /><a href="'.$CFG->wwwroot.'/course/togglecompletion.php?course='.$this->page->course->id.'">';
 107              $this->content->footer .= get_string('completecourse', 'block_selfcompletion').'</a>...';
 108          }
 109  
 110          return $this->content;
 111      }
 112  }