[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/blocks/completionstatus/ -> block_completionstatus.php (source)

   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 <[email protected]>
  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('all' => true, 'mod' => false, 'tag' => false, 'my' => false);
  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  
  61          // Can edit settings?
  62          $can_edit = has_capability('moodle/course:update', $context);
  63  
  64          // Get course completion data.
  65          $info = new completion_info($course);
  66  
  67          // Don't display if completion isn't enabled!
  68          if (!completion_info::is_enabled_for_site()) {
  69              if ($can_edit) {
  70                  $this->content->text = get_string('completionnotenabledforsite', 'completion');
  71              }
  72              return $this->content;
  73  
  74          } else if (!$info->is_enabled()) {
  75              if ($can_edit) {
  76                  $this->content->text = get_string('completionnotenabledforcourse', 'completion');
  77              }
  78              return $this->content;
  79          }
  80  
  81          // Load criteria to display.
  82          $completions = $info->get_completions($USER->id);
  83  
  84          // Check if this course has any criteria.
  85          if (empty($completions)) {
  86              if ($can_edit) {
  87                  $this->content->text = get_string('nocriteriaset', 'completion');
  88              }
  89              return $this->content;
  90          }
  91  
  92          // Check this user is enroled.
  93          if ($info->is_tracked_user($USER->id)) {
  94  
  95              // Generate markup for criteria statuses.
  96              $data = '';
  97  
  98              // For aggregating activity completion.
  99              $activities = array();
 100              $activities_complete = 0;
 101  
 102              // For aggregating course prerequisites.
 103              $prerequisites = array();
 104              $prerequisites_complete = 0;
 105  
 106              // Flag to set if current completion data is inconsistent with what is stored in the database.
 107              $pending_update = false;
 108  
 109              // Loop through course criteria.
 110              foreach ($completions as $completion) {
 111                  $criteria = $completion->get_criteria();
 112                  $complete = $completion->is_complete();
 113  
 114                  if (!$pending_update && $criteria->is_pending($completion)) {
 115                      $pending_update = true;
 116                  }
 117  
 118                  // Activities are a special case, so cache them and leave them till last.
 119                  if ($criteria->criteriatype == COMPLETION_CRITERIA_TYPE_ACTIVITY) {
 120                      $activities[$criteria->moduleinstance] = $complete;
 121  
 122                      if ($complete) {
 123                          $activities_complete++;
 124                      }
 125  
 126                      continue;
 127                  }
 128  
 129                  // Prerequisites are also a special case, so cache them and leave them till last.
 130                  if ($criteria->criteriatype == COMPLETION_CRITERIA_TYPE_COURSE) {
 131                      $prerequisites[$criteria->courseinstance] = $complete;
 132  
 133                      if ($complete) {
 134                          $prerequisites_complete++;
 135                      }
 136  
 137                      continue;
 138                  }
 139                  $row = new html_table_row();
 140                  $row->cells[0] = new html_table_cell($criteria->get_title());
 141                  $row->cells[1] = new html_table_cell($completion->get_status());
 142                  $row->cells[1]->style = 'text-align: right;';
 143                  $srows[] = $row;
 144              }
 145  
 146              // Aggregate activities.
 147              if (!empty($activities)) {
 148                  $a = new stdClass();
 149                  $a->first = $activities_complete;
 150                  $a->second = count($activities);
 151  
 152                  $row = new html_table_row();
 153                  $row->cells[0] = new html_table_cell(get_string('activitiescompleted', 'completion'));
 154                  $row->cells[1] = new html_table_cell(get_string('firstofsecond', 'block_completionstatus', $a));
 155                  $row->cells[1]->style = 'text-align: right;';
 156                  $srows[] = $row;
 157              }
 158  
 159              // Aggregate prerequisites.
 160              if (!empty($prerequisites)) {
 161                  $a = new stdClass();
 162                  $a->first = $prerequisites_complete;
 163                  $a->second = count($prerequisites);
 164  
 165                  $row = new html_table_row();
 166                  $row->cells[0] = new html_table_cell(get_string('dependenciescompleted', 'completion'));
 167                  $row->cells[1] = new html_table_cell(get_string('firstofsecond', 'block_completionstatus', $a));
 168                  $row->cells[1]->style = 'text-align: right;';
 169                  $prows[] = $row;
 170  
 171                  $srows = array_merge($prows, $srows);
 172              }
 173  
 174              // Display completion status.
 175              $table = new html_table();
 176              $table->width = '100%';
 177              $table->attributes = array('style'=>'font-size: 90%;', 'class'=>'');
 178  
 179              $row = new html_table_row();
 180              $content = html_writer::tag('b', get_string('status').': ');
 181  
 182              // Is course complete?
 183              $coursecomplete = $info->is_course_complete($USER->id);
 184  
 185              // Load course completion.
 186              $params = array(
 187                  'userid' => $USER->id,
 188                  'course' => $course->id
 189              );
 190              $ccompletion = new completion_completion($params);
 191  
 192              // Has this user completed any criteria?
 193              $criteriacomplete = $info->count_course_user_data($USER->id);
 194  
 195              if ($pending_update) {
 196                  $content .= html_writer::tag('i', get_string('pending', 'completion'));
 197              } else if ($coursecomplete) {
 198                  $content .= get_string('complete');
 199              } else if (!$criteriacomplete && !$ccompletion->timestarted) {
 200                  $content .= html_writer::tag('i', get_string('notyetstarted', 'completion'));
 201              } else {
 202                  $content .= html_writer::tag('i', get_string('inprogress', 'completion'));
 203              }
 204  
 205              $row->cells[0] = new html_table_cell($content);
 206              $row->cells[0]->colspan = '2';
 207  
 208              $rows[] = $row;
 209              $row = new html_table_row();
 210              $content = "";
 211              // Get overall aggregation method.
 212              $overall = $info->get_aggregation_method();
 213              if ($overall == COMPLETION_AGGREGATION_ALL) {
 214                  $content .= get_string('criteriarequiredall', 'completion');
 215              } else {
 216                  $content .= get_string('criteriarequiredany', 'completion');
 217              }
 218              $content .= ':';
 219              $row->cells[0] = new html_table_cell($content);
 220              $row->cells[0]->colspan = '2';
 221              $rows[] = $row;
 222  
 223              $row = new html_table_row();
 224              $row->cells[0] = new html_table_cell(html_writer::tag('b', get_string('requiredcriteria', 'completion')));
 225              $row->cells[1] = new html_table_cell(html_writer::tag('b', get_string('status')));
 226              $row->cells[1]->style = 'text-align: right;';
 227              $rows[] = $row;
 228  
 229              // Array merge $rows and $data here.
 230              $rows = array_merge($rows, $srows);
 231  
 232              $table->data = $rows;
 233              $this->content->text = html_writer::table($table);
 234  
 235              // Display link to detailed view.
 236              $details = new moodle_url('/blocks/completionstatus/details.php', array('course' => $course->id));
 237              $this->content->footer = html_writer::link($details, get_string('moredetails', 'completion'));
 238          } else {
 239              // If user is not enrolled, show error.
 240              $this->content->text = get_string('nottracked', 'completion');
 241          }
 242  
 243          if (has_capability('report/completion:view', $context)) {
 244              $report = new moodle_url('/report/completion/index.php', array('course' => $course->id));
 245              if (empty($this->content->footer)) {
 246                  $this->content->footer = '';
 247              }
 248              $this->content->footer .= html_writer::empty_tag('br');
 249              $this->content->footer .= html_writer::link($report, get_string('viewcoursereport', 'completion'));
 250          }
 251  
 252          return $this->content;
 253      }
 254  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1