Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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   * Block for displayed logged in user's course completion status
  19   *
  20   * @package    block_completionstatus
  21   * @copyright  2009-2012 Catalyst IT Ltd
  22   * @author     Aaron Barnes <aaronb@catalyst.net.nz>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  require_once("{$CFG->libdir}/completionlib.php");
  29  
  30  /**
  31   * Course completion status.
  32   * Displays overall, and individual criteria status for logged in user.
  33   */
  34  class block_completionstatus extends block_base {
  35  
  36      public function init() {
  37          $this->title = get_string('pluginname', 'block_completionstatus');
  38      }
  39  
  40      public function applicable_formats() {
  41          return array('course' => true);
  42      }
  43  
  44      public function get_content() {
  45          global $USER;
  46  
  47          $rows = array();
  48          $srows = array();
  49          $prows = array();
  50          // If content is cached.
  51          if ($this->content !== null) {
  52              return $this->content;
  53          }
  54  
  55          $course = $this->page->course;
  56          $context = context_course::instance($course->id);
  57  
  58          // Create empty content.
  59          $this->content = new stdClass();
  60          $this->content->text = '';
  61          $this->content->footer = '';
  62  
  63          // Can edit settings?
  64          $can_edit = has_capability('moodle/course:update', $context);
  65  
  66          // Get course completion data.
  67          $info = new completion_info($course);
  68  
  69          // Don't display if completion isn't enabled!
  70          if (!completion_info::is_enabled_for_site()) {
  71              if ($can_edit) {
  72                  $this->content->text .= get_string('completionnotenabledforsite', 'completion');
  73              }
  74              return $this->content;
  75  
  76          } else if (!$info->is_enabled()) {
  77              if ($can_edit) {
  78                  $this->content->text .= get_string('completionnotenabledforcourse', 'completion');
  79              }
  80              return $this->content;
  81          }
  82  
  83          // Load criteria to display.
  84          $completions = $info->get_completions($USER->id);
  85  
  86          // Check if this course has any criteria.
  87          if (empty($completions)) {
  88              if ($can_edit) {
  89                  $this->content->text .= get_string('nocriteriaset', 'completion');
  90              }
  91              return $this->content;
  92          }
  93  
  94          // Check this user is enroled.
  95          if ($info->is_tracked_user($USER->id)) {
  96  
  97              // Generate markup for criteria statuses.
  98              $data = '';
  99  
 100              // For aggregating activity completion.
 101              $activities = array();
 102              $activities_complete = 0;
 103  
 104              // For aggregating course prerequisites.
 105              $prerequisites = array();
 106              $prerequisites_complete = 0;
 107  
 108              // Flag to set if current completion data is inconsistent with what is stored in the database.
 109              $pending_update = false;
 110  
 111              // Loop through course criteria.
 112              foreach ($completions as $completion) {
 113                  $criteria = $completion->get_criteria();
 114                  $complete = $completion->is_complete();
 115  
 116                  if (!$pending_update && $criteria->is_pending($completion)) {
 117                      $pending_update = true;
 118                  }
 119  
 120                  // Activities are a special case, so cache them and leave them till last.
 121                  if ($criteria->criteriatype == COMPLETION_CRITERIA_TYPE_ACTIVITY) {
 122                      $activities[$criteria->moduleinstance] = $complete;
 123  
 124                      if ($complete) {
 125                          $activities_complete++;
 126                      }
 127  
 128                      continue;
 129                  }
 130  
 131                  // Prerequisites are also a special case, so cache them and leave them till last.
 132                  if ($criteria->criteriatype == COMPLETION_CRITERIA_TYPE_COURSE) {
 133                      $prerequisites[$criteria->courseinstance] = $complete;
 134  
 135                      if ($complete) {
 136                          $prerequisites_complete++;
 137                      }
 138  
 139                      continue;
 140                  }
 141                  $row = new html_table_row();
 142                  $row->cells[0] = new html_table_cell($criteria->get_title());
 143                  $row->cells[1] = new html_table_cell($completion->get_status());
 144                  $row->cells[1]->style = 'text-align: right;';
 145                  $srows[] = $row;
 146              }
 147  
 148              // Aggregate activities.
 149              if (!empty($activities)) {
 150                  $a = new stdClass();
 151                  $a->first = $activities_complete;
 152                  $a->second = count($activities);
 153  
 154                  $row = new html_table_row();
 155                  $row->cells[0] = new html_table_cell(get_string('activitiescompleted', 'completion'));
 156                  $row->cells[1] = new html_table_cell(get_string('firstofsecond', 'block_completionstatus', $a));
 157                  $row->cells[1]->style = 'text-align: right;';
 158                  $srows[] = $row;
 159              }
 160  
 161              // Aggregate prerequisites.
 162              if (!empty($prerequisites)) {
 163                  $a = new stdClass();
 164                  $a->first = $prerequisites_complete;
 165                  $a->second = count($prerequisites);
 166  
 167                  $row = new html_table_row();
 168                  $row->cells[0] = new html_table_cell(get_string('dependenciescompleted', 'completion'));
 169                  $row->cells[1] = new html_table_cell(get_string('firstofsecond', 'block_completionstatus', $a));
 170                  $row->cells[1]->style = 'text-align: right;';
 171                  $prows[] = $row;
 172  
 173                  $srows = array_merge($prows, $srows);
 174              }
 175  
 176              // Display completion status.
 177              $table = new html_table();
 178              $table->width = '100%';
 179              $table->attributes = array('style'=>'font-size: 90%;', 'class'=>'');
 180  
 181              $row = new html_table_row();
 182              $content = html_writer::tag('b', get_string('status').': ');
 183  
 184              // Is course complete?
 185              $coursecomplete = $info->is_course_complete($USER->id);
 186  
 187              // Load course completion.
 188              $params = array(
 189                  'userid' => $USER->id,
 190                  'course' => $course->id
 191              );
 192              $ccompletion = new completion_completion($params);
 193  
 194              // Has this user completed any criteria?
 195              $criteriacomplete = $info->count_course_user_data($USER->id);
 196  
 197              if ($pending_update) {
 198                  $content .= html_writer::tag('i', get_string('pending', 'completion'));
 199              } else if ($coursecomplete) {
 200                  $content .= get_string('complete');
 201              } else if (!$criteriacomplete && !$ccompletion->timestarted) {
 202                  $content .= html_writer::tag('i', get_string('notyetstarted', 'completion'));
 203              } else {
 204                  $content .= html_writer::tag('i', get_string('inprogress', 'completion'));
 205              }
 206  
 207              $row->cells[0] = new html_table_cell($content);
 208              $row->cells[0]->colspan = '2';
 209  
 210              $rows[] = $row;
 211              $row = new html_table_row();
 212              $content = "";
 213              // Get overall aggregation method.
 214              $overall = $info->get_aggregation_method();
 215              if ($overall == COMPLETION_AGGREGATION_ALL) {
 216                  $content .= get_string('criteriarequiredall', 'completion');
 217              } else {
 218                  $content .= get_string('criteriarequiredany', 'completion');
 219              }
 220              $content .= ':';
 221              $row->cells[0] = new html_table_cell($content);
 222              $row->cells[0]->colspan = '2';
 223              $rows[] = $row;
 224  
 225              $row = new html_table_row();
 226              $row->cells[0] = new html_table_cell(html_writer::tag('b', get_string('requiredcriteria', 'completion')));
 227              $row->cells[1] = new html_table_cell(html_writer::tag('b', get_string('status')));
 228              $row->cells[1]->style = 'text-align: right;';
 229              $rows[] = $row;
 230  
 231              // Array merge $rows and $data here.
 232              $rows = array_merge($rows, $srows);
 233  
 234              $table->data = $rows;
 235              $this->content->text .= html_writer::table($table);
 236  
 237              // Display link to detailed view.
 238              $details = new moodle_url('/blocks/completionstatus/details.php', array('course' => $course->id));
 239              $this->content->footer .= html_writer::link($details, get_string('moredetails', 'completion'));
 240          } else {
 241              // If user is not enrolled, show error.
 242              $this->content->text = get_string('nottracked', 'completion');
 243          }
 244  
 245          if (has_capability('report/completion:view', $context)) {
 246              $report = new moodle_url('/report/completion/index.php', array('course' => $course->id));
 247              if (empty($this->content->footer)) {
 248                  $this->content->footer = '';
 249              }
 250              $this->content->footer .= html_writer::empty_tag('br');
 251              $this->content->footer .= html_writer::link($report, get_string('viewcoursereport', 'completion'));
 252          }
 253  
 254          return $this->content;
 255      }
 256  }