[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/blocks/course_overview/ -> locallib.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   * Helper functions for course_overview block
  19   *
  20   * @package    block_course_overview
  21   * @copyright  2012 Adam Olley <adam.olley@netspot.com.au>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  define('BLOCKS_COURSE_OVERVIEW_SHOWCATEGORIES_NONE', '0');
  26  define('BLOCKS_COURSE_OVERVIEW_SHOWCATEGORIES_ONLY_PARENT_NAME', '1');
  27  define('BLOCKS_COURSE_OVERVIEW_SHOWCATEGORIES_FULL_PATH', '2');
  28  
  29  /**
  30   * Display overview for courses
  31   *
  32   * @param array $courses courses for which overview needs to be shown
  33   * @return array html overview
  34   */
  35  function block_course_overview_get_overviews($courses) {
  36      $htmlarray = array();
  37      if ($modules = get_plugin_list_with_function('mod', 'print_overview')) {
  38          // Split courses list into batches with no more than MAX_MODINFO_CACHE_SIZE courses in one batch.
  39          // Otherwise we exceed the cache limit in get_fast_modinfo() and rebuild it too often.
  40          if (defined('MAX_MODINFO_CACHE_SIZE') && MAX_MODINFO_CACHE_SIZE > 0 && count($courses) > MAX_MODINFO_CACHE_SIZE) {
  41              $batches = array_chunk($courses, MAX_MODINFO_CACHE_SIZE, true);
  42          } else {
  43              $batches = array($courses);
  44          }
  45          foreach ($batches as $courses) {
  46              foreach ($modules as $fname) {
  47                  $fname($courses, $htmlarray);
  48              }
  49          }
  50      }
  51      return $htmlarray;
  52  }
  53  
  54  /**
  55   * Sets user preference for maximum courses to be displayed in course_overview block
  56   *
  57   * @param int $number maximum courses which should be visible
  58   */
  59  function block_course_overview_update_mynumber($number) {
  60      set_user_preference('course_overview_number_of_courses', $number);
  61  }
  62  
  63  /**
  64   * Sets user course sorting preference in course_overview block
  65   *
  66   * @param array $sortorder list of course ids
  67   */
  68  function block_course_overview_update_myorder($sortorder) {
  69      $value = implode(',', $sortorder);
  70      if (core_text::strlen($value) > 1333) {
  71          // The value won't fit into the user preference. Remove courses in the end of the list (mostly likely user won't even notice).
  72          $value = preg_replace('/,[\d]*$/', '', core_text::substr($value, 0, 1334));
  73      }
  74      set_user_preference('course_overview_course_sortorder', $value);
  75  }
  76  
  77  /**
  78   * Gets user course sorting preference in course_overview block
  79   *
  80   * @return array list of course ids
  81   */
  82  function block_course_overview_get_myorder() {
  83      if ($value = get_user_preferences('course_overview_course_sortorder')) {
  84          return explode(',', $value);
  85      }
  86      // If preference was not found, look in the old location and convert if found.
  87      $order = array();
  88      if ($value = get_user_preferences('course_overview_course_order')) {
  89          $order = unserialize($value);
  90          block_course_overview_update_myorder($order);
  91          unset_user_preference('course_overview_course_order');
  92      }
  93      return $order;
  94  }
  95  
  96  /**
  97   * Returns shortname of activities in course
  98   *
  99   * @param int $courseid id of course for which activity shortname is needed
 100   * @return string|bool list of child shortname
 101   */
 102  function block_course_overview_get_child_shortnames($courseid) {
 103      global $DB;
 104      $ctxselect = context_helper::get_preload_record_columns_sql('ctx');
 105      $sql = "SELECT c.id, c.shortname, $ctxselect
 106              FROM {enrol} e
 107              JOIN {course} c ON (c.id = e.customint1)
 108              JOIN {context} ctx ON (ctx.instanceid = e.customint1)
 109              WHERE e.courseid = :courseid AND e.enrol = :method AND ctx.contextlevel = :contextlevel ORDER BY e.sortorder";
 110      $params = array('method' => 'meta', 'courseid' => $courseid, 'contextlevel' => CONTEXT_COURSE);
 111  
 112      if ($results = $DB->get_records_sql($sql, $params)) {
 113          $shortnames = array();
 114          // Preload the context we will need it to format the category name shortly.
 115          foreach ($results as $res) {
 116              context_helper::preload_from_record($res);
 117              $context = context_course::instance($res->id);
 118              $shortnames[] = format_string($res->shortname, true, $context);
 119          }
 120          $total = count($shortnames);
 121          $suffix = '';
 122          if ($total > 10) {
 123              $shortnames = array_slice($shortnames, 0, 10);
 124              $diff = $total - count($shortnames);
 125              if ($diff > 1) {
 126                  $suffix = get_string('shortnamesufixprural', 'block_course_overview', $diff);
 127              } else {
 128                  $suffix = get_string('shortnamesufixsingular', 'block_course_overview', $diff);
 129              }
 130          }
 131          $shortnames = get_string('shortnameprefix', 'block_course_overview', implode('; ', $shortnames));
 132          $shortnames .= $suffix;
 133      }
 134  
 135      return isset($shortnames) ? $shortnames : false;
 136  }
 137  
 138  /**
 139   * Returns maximum number of courses which will be displayed in course_overview block
 140   *
 141   * @param bool $showallcourses if set true all courses will be visible.
 142   * @return int maximum number of courses
 143   */
 144  function block_course_overview_get_max_user_courses($showallcourses = false) {
 145      // Get block configuration
 146      $config = get_config('block_course_overview');
 147      $limit = $config->defaultmaxcourses;
 148  
 149      // If max course is not set then try get user preference
 150      if (empty($config->forcedefaultmaxcourses)) {
 151          if ($showallcourses) {
 152              $limit = 0;
 153          } else {
 154              $limit = get_user_preferences('course_overview_number_of_courses', $limit);
 155          }
 156      }
 157      return $limit;
 158  }
 159  
 160  /**
 161   * Return sorted list of user courses
 162   *
 163   * @param bool $showallcourses if set true all courses will be visible.
 164   * @return array list of sorted courses and count of courses.
 165   */
 166  function block_course_overview_get_sorted_courses($showallcourses = false) {
 167      global $USER;
 168  
 169      $limit = block_course_overview_get_max_user_courses($showallcourses);
 170  
 171      $courses = enrol_get_my_courses();
 172      $site = get_site();
 173  
 174      if (array_key_exists($site->id,$courses)) {
 175          unset($courses[$site->id]);
 176      }
 177  
 178      foreach ($courses as $c) {
 179          if (isset($USER->lastcourseaccess[$c->id])) {
 180              $courses[$c->id]->lastaccess = $USER->lastcourseaccess[$c->id];
 181          } else {
 182              $courses[$c->id]->lastaccess = 0;
 183          }
 184      }
 185  
 186      // Get remote courses.
 187      $remotecourses = array();
 188      if (is_enabled_auth('mnet')) {
 189          $remotecourses = get_my_remotecourses();
 190      }
 191      // Remote courses will have -ve remoteid as key, so it can be differentiated from normal courses
 192      foreach ($remotecourses as $id => $val) {
 193          $remoteid = $val->remoteid * -1;
 194          $val->id = $remoteid;
 195          $courses[$remoteid] = $val;
 196      }
 197  
 198      $order = block_course_overview_get_myorder();
 199  
 200      $sortedcourses = array();
 201      $counter = 0;
 202      // Get courses in sort order into list.
 203      foreach ($order as $key => $cid) {
 204          if (($counter >= $limit) && ($limit != 0)) {
 205              break;
 206          }
 207  
 208          // Make sure user is still enroled.
 209          if (isset($courses[$cid])) {
 210              $sortedcourses[$cid] = $courses[$cid];
 211              $counter++;
 212          }
 213      }
 214      // Append unsorted courses if limit allows
 215      foreach ($courses as $c) {
 216          if (($limit != 0) && ($counter >= $limit)) {
 217              break;
 218          }
 219          if (!in_array($c->id, $order)) {
 220              $sortedcourses[$c->id] = $c;
 221              $counter++;
 222          }
 223      }
 224  
 225      // From list extract site courses for overview
 226      $sitecourses = array();
 227      foreach ($sortedcourses as $key => $course) {
 228          if ($course->id > 0) {
 229              $sitecourses[$key] = $course;
 230          }
 231      }
 232      return array($sortedcourses, $sitecourses, count($courses));
 233  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1