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 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]

   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 displaying 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  require_once(__DIR__.'/../../config.php');
  27  require_once("{$CFG->libdir}/completionlib.php");
  28  
  29  // Load data.
  30  $id = required_param('course', PARAM_INT);
  31  $userid = optional_param('user', 0, PARAM_INT);
  32  
  33  // Load course.
  34  $course = $DB->get_record('course', array('id' => $id), '*', MUST_EXIST);
  35  
  36  // Load user.
  37  if ($userid) {
  38      $user = $DB->get_record('user', array('id' => $userid), '*', MUST_EXIST);
  39  } else {
  40      $user = $USER;
  41  }
  42  
  43  // Check permissions.
  44  require_login($course);
  45  
  46  if (!completion_can_view_data($user->id, $course)) {
  47      throw new \moodle_exception('cannotviewreport');
  48  }
  49  
  50  // Load completion data.
  51  $info = new completion_info($course);
  52  
  53  $returnurl = new moodle_url('/course/view.php', array('id' => $id));
  54  
  55  // Don't display if completion isn't enabled.
  56  if (!$info->is_enabled()) {
  57      throw new \moodle_exception('completionnotenabled', 'completion', $returnurl);
  58  }
  59  
  60  // Check this user is enroled.
  61  if (!$info->is_tracked_user($user->id)) {
  62      if ($USER->id == $user->id) {
  63          throw new \moodle_exception('notenroled', 'completion', $returnurl);
  64      } else {
  65          throw new \moodle_exception('usernotenroled', 'completion', $returnurl);
  66      }
  67  }
  68  
  69  // Print header.
  70  $page = get_string('completionprogressdetails', 'block_completionstatus');
  71  $title = format_string($course->fullname) . ': ' . $page;
  72  
  73  $PAGE->navbar->add($page);
  74  $PAGE->set_pagelayout('report');
  75  $PAGE->set_url('/blocks/completionstatus/details.php', array('course' => $course->id, 'user' => $user->id));
  76  $PAGE->set_title(get_string('course') . ': ' . $course->fullname);
  77  $PAGE->set_heading($title);
  78  echo $OUTPUT->header();
  79  
  80  
  81  // Display completion status.
  82  echo html_writer::start_tag('table', array('class' => 'generalbox boxaligncenter'));
  83  echo html_writer::start_tag('tbody');
  84  
  85  // If not display logged in user, show user name.
  86  if ($USER->id != $user->id) {
  87      echo html_writer::start_tag('tr');
  88      echo html_writer::start_tag('td', array('colspan' => '2'));
  89      echo html_writer::tag('b', get_string('showinguser', 'completion') . ' ');
  90      $url = new moodle_url('/user/view.php', array('id' => $user->id, 'course' => $course->id));
  91      echo html_writer::link($url, fullname($user));
  92      echo html_writer::end_tag('td');
  93      echo html_writer::end_tag('tr');
  94  }
  95  
  96  echo html_writer::start_tag('tr');
  97  echo html_writer::start_tag('td', array('colspan' => '2'));
  98  echo html_writer::tag('b', get_string('status') . ' ');
  99  
 100  // Is course complete?
 101  $coursecomplete = $info->is_course_complete($user->id);
 102  
 103  // Has this user completed any criteria?
 104  $criteriacomplete = $info->count_course_user_data($user->id);
 105  
 106  // Load course completion.
 107  $params = array(
 108      'userid' => $user->id,
 109      'course' => $course->id,
 110  );
 111  $ccompletion = new completion_completion($params);
 112  
 113  // Save row data.
 114  $rows = array();
 115  
 116  // Flag to set if current completion data is inconsistent with what is stored in the database.
 117  $pendingupdate = false;
 118  
 119  // Load criteria to display.
 120  $completions = $info->get_completions($user->id);
 121  
 122  // Loop through course criteria.
 123  foreach ($completions as $completion) {
 124      $criteria = $completion->get_criteria();
 125  
 126      if (!$pendingupdate && $criteria->is_pending($completion)) {
 127          $pendingupdate = true;
 128      }
 129  
 130      $row = array();
 131      $row['type'] = $criteria->criteriatype;
 132      $row['title'] = $criteria->get_title();
 133      $row['status'] = $completion->get_status();
 134      $row['complete'] = $completion->is_complete();
 135      $row['timecompleted'] = $completion->timecompleted;
 136      $row['details'] = $criteria->get_details($completion);
 137      $rows[] = $row;
 138  }
 139  
 140  if ($pendingupdate) {
 141      echo html_writer::tag('i', get_string('pending', 'completion'));
 142  } else if ($coursecomplete) {
 143      echo get_string('complete');
 144  } else if (!$criteriacomplete && !$ccompletion->timestarted) {
 145      echo html_writer::tag('i', get_string('notyetstarted', 'completion'));
 146  } else {
 147      echo html_writer::tag('i', get_string('inprogress', 'completion'));
 148  }
 149  
 150  echo html_writer::end_tag('td');
 151  echo html_writer::end_tag('tr');
 152  
 153  // Check if this course has any criteria.
 154  if (empty($completions)) {
 155      echo html_writer::start_tag('tr');
 156      echo html_writer::start_tag('td', array('colspan' => '2'));
 157      echo html_writer::start_tag('br');
 158      echo $OUTPUT->box(get_string('nocriteriaset', 'completion'), 'noticebox');
 159      echo html_writer::end_tag('td');
 160      echo html_writer::end_tag('tr');
 161      echo html_writer::end_tag('tbody');
 162      echo html_writer::end_tag('table');
 163  } else {
 164      echo html_writer::start_tag('tr');
 165      echo html_writer::start_tag('td', array('colspan' => '2'));
 166      echo html_writer::tag('b', get_string('required') . ' ');
 167  
 168      // Get overall aggregation method.
 169      $overall = $info->get_aggregation_method();
 170  
 171      if ($overall == COMPLETION_AGGREGATION_ALL) {
 172          echo get_string('criteriarequiredall', 'completion');
 173      } else {
 174          echo get_string('criteriarequiredany', 'completion');
 175      }
 176  
 177      echo html_writer::end_tag('td');
 178      echo html_writer::end_tag('tr');
 179      echo html_writer::end_tag('tbody');
 180      echo html_writer::end_tag('table');
 181  
 182      // Generate markup for criteria statuses.
 183      echo html_writer::start_tag('table',
 184              array('class' => 'generalbox logtable boxaligncenter', 'id' => 'criteriastatus', 'width' => '100%'));
 185      echo html_writer::start_tag('tbody');
 186      echo html_writer::start_tag('tr', array('class' => 'ccheader'));
 187      echo html_writer::tag('th', get_string('criteriagroup', 'block_completionstatus'), array('class' => 'c0 header', 'scope' => 'col'));
 188      echo html_writer::tag('th', get_string('criteria', 'completion'), array('class' => 'c1 header', 'scope' => 'col'));
 189      echo html_writer::tag('th', get_string('requirement', 'block_completionstatus'), array('class' => 'c2 header', 'scope' => 'col'));
 190      echo html_writer::tag('th', get_string('status'), array('class' => 'c3 header', 'scope' => 'col'));
 191      echo html_writer::tag('th', get_string('complete'), array('class' => 'c4 header', 'scope' => 'col'));
 192      echo html_writer::tag('th', get_string('completiondate', 'report_completion'), array('class' => 'c5 header', 'scope' => 'col'));
 193      echo html_writer::end_tag('tr');
 194  
 195      // Print table.
 196      $last_type = '';
 197      $agg_type = false;
 198      $oddeven = 0;
 199  
 200      foreach ($rows as $row) {
 201  
 202          echo html_writer::start_tag('tr', array('class' => 'r' . $oddeven));
 203          // Criteria group.
 204          echo html_writer::start_tag('td', array('class' => 'cell c0'));
 205          if ($last_type !== $row['details']['type']) {
 206              $last_type = $row['details']['type'];
 207              echo $last_type;
 208  
 209              // Reset agg type.
 210              $agg_type = true;
 211          } else {
 212              // Display aggregation type.
 213              if ($agg_type) {
 214                  $agg = $info->get_aggregation_method($row['type']);
 215                  echo '('. html_writer::start_tag('i');
 216                  if ($agg == COMPLETION_AGGREGATION_ALL) {
 217                      echo core_text::strtolower(get_string('all', 'completion'));
 218                  } else {
 219                      echo core_text::strtolower(get_string('any', 'completion'));
 220                  }
 221  
 222                  echo ' ' . html_writer::end_tag('i') .core_text::strtolower(get_string('required')).')';
 223                  $agg_type = false;
 224              }
 225          }
 226          echo html_writer::end_tag('td');
 227  
 228          // Criteria title.
 229          echo html_writer::start_tag('td', array('class' => 'cell c1'));
 230          echo $row['details']['criteria'];
 231          echo html_writer::end_tag('td');
 232  
 233          // Requirement.
 234          echo html_writer::start_tag('td', array('class' => 'cell c2'));
 235          echo $row['details']['requirement'];
 236          echo html_writer::end_tag('td');
 237  
 238          // Status.
 239          echo html_writer::start_tag('td', array('class' => 'cell c3'));
 240          echo $row['details']['status'];
 241          echo html_writer::end_tag('td');
 242  
 243          // Is complete.
 244          echo html_writer::start_tag('td', array('class' => 'cell c4'));
 245          echo $row['complete'] ? get_string('yes') : get_string('no');
 246          echo html_writer::end_tag('td');
 247  
 248          // Completion data.
 249          echo html_writer::start_tag('td', array('class' => 'cell c5'));
 250          if ($row['timecompleted']) {
 251              echo userdate($row['timecompleted'], get_string('strftimedatemonthtimeshort', 'langconfig'));
 252          } else {
 253              echo '-';
 254          }
 255          echo html_writer::end_tag('td');
 256          echo html_writer::end_tag('tr');
 257          // For row striping.
 258          $oddeven = $oddeven ? 0 : 1;
 259      }
 260  
 261      echo html_writer::end_tag('tbody');
 262      echo html_writer::end_tag('table');
 263  }
 264  
 265  echo $OUTPUT->footer();