[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/form/ -> course.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  /**
  19   * Course selector field.
  20   *
  21   * Allows auto-complete ajax searching for courses and can restrict by enrolment, permissions, viewhidden...
  22   *
  23   * @package   core_form
  24   * @copyright 2015 Damyon Wiese <damyon@moodle.com>
  25   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  
  28  global $CFG;
  29  require_once($CFG->libdir . '/form/autocomplete.php');
  30  
  31  /**
  32   * Form field type for choosing a course.
  33   *
  34   * Allows auto-complete ajax searching for courses and can restrict by enrolment, permissions, viewhidden...
  35   *
  36   * @package   core_form
  37   * @copyright 2015 Damyon Wiese <damyon@moodle.com>
  38   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class MoodleQuickForm_course extends MoodleQuickForm_autocomplete {
  41  
  42      /**
  43       * @var array $exclude Exclude a list of courses from the list (e.g. the current course).
  44       */
  45      protected $exclude = array();
  46  
  47      /**
  48       * @var boolean $allowmultiple Allow selecting more than one course.
  49       */
  50      protected $multiple = false;
  51  
  52      /**
  53       * @var array $requiredcapabilities Array of extra capabilities to check at the course context.
  54       */
  55      protected $requiredcapabilities = array();
  56  
  57      /**
  58       * @var bool $limittoenrolled Only allow enrolled courses.
  59       */
  60      protected $limittoenrolled = false;
  61  
  62      /**
  63       * Constructor
  64       *
  65       * @param string $elementname Element name
  66       * @param mixed $elementlabel Label(s) for an element
  67       * @param array $options Options to control the element's display
  68       *                       Valid options are:
  69       *                       'multiple' - boolean multi select
  70       *                       'exclude' - array or int, list of course ids to never show
  71       *                       'requiredcapabilities' - array of capabilities. Uses ANY to combine them.
  72       *                       'limittoenrolled' - boolean Limits to enrolled courses.
  73       *                       'includefrontpage' - boolean Enables the frontpage to be selected.
  74       */
  75      public function __construct($elementname = null, $elementlabel = null, $options = array()) {
  76          if (isset($options['multiple'])) {
  77              $this->multiple = $options['multiple'];
  78          }
  79          if (isset($options['exclude'])) {
  80              $this->exclude = $options['exclude'];
  81              if (!is_array($this->exclude)) {
  82                  $this->exclude = array($this->exclude);
  83              }
  84          }
  85          if (isset($options['requiredcapabilities'])) {
  86              $this->requiredcapabilities = $options['requiredcapabilities'];
  87          }
  88          if (isset($options['limittoenrolled'])) {
  89              $this->limittoenrolled = $options['limittoenrolled'];
  90          }
  91  
  92          $validattributes = array(
  93              'ajax' => 'core/form-course-selector',
  94              'data-requiredcapabilities' => implode(',', $this->requiredcapabilities),
  95              'data-exclude' => implode(',', $this->exclude),
  96              'data-limittoenrolled' => (int)$this->limittoenrolled
  97          );
  98          if ($this->multiple) {
  99              $validattributes['multiple'] = 'multiple';
 100          }
 101          if (isset($options['noselectionstring'])) {
 102              $validattributes['noselectionstring'] = $options['noselectionstring'];
 103          }
 104          if (isset($options['placeholder'])) {
 105              $validattributes['placeholder'] = $options['placeholder'];
 106          }
 107          if (!empty($options['includefrontpage'])) {
 108              $validattributes['data-includefrontpage'] = SITEID;
 109          }
 110  
 111          parent::__construct($elementname, $elementlabel, array(), $validattributes);
 112      }
 113  
 114      /**
 115       * Set the value of this element. If values can be added or are unknown, we will
 116       * make sure they exist in the options array.
 117       * @param string|array $value The value to set.
 118       * @return boolean
 119       */
 120      public function setValue($value) {
 121          global $DB;
 122          $values = (array) $value;
 123          $coursestofetch = array();
 124  
 125          foreach ($values as $onevalue) {
 126              if ((!$this->optionExists($onevalue)) &&
 127                      ($onevalue !== '_qf__force_multiselect_submission')) {
 128                  array_push($coursestofetch, $onevalue);
 129              }
 130          }
 131  
 132          if (empty($coursestofetch)) {
 133              return $this->setSelected($values);
 134          }
 135  
 136          // There is no API function to load a list of course from a list of ids.
 137          $ctxselect = context_helper::get_preload_record_columns_sql('ctx');
 138          $fields = array('c.id', 'c.category', 'c.sortorder',
 139                          'c.shortname', 'c.fullname', 'c.idnumber',
 140                          'c.startdate', 'c.visible', 'c.cacherev');
 141          list($whereclause, $params) = $DB->get_in_or_equal($coursestofetch, SQL_PARAMS_NAMED, 'id');
 142  
 143          $sql = "SELECT ". join(',', $fields). ", $ctxselect
 144                  FROM {course} c
 145                  JOIN {context} ctx ON c.id = ctx.instanceid AND ctx.contextlevel = :contextcourse
 146                  WHERE c.id ". $whereclause." ORDER BY c.sortorder";
 147          $list = $DB->get_records_sql($sql, array('contextcourse' => CONTEXT_COURSE) + $params);
 148  
 149          $coursestoselect = array();
 150          foreach ($list as $course) {
 151              context_helper::preload_from_record($course);
 152              $context = context_course::instance($course->id);
 153              // Make sure we can see the course.
 154              if (!$course->visible && !has_capability('moodle/course:viewhiddencourses', $context)) {
 155                  continue;
 156              }
 157              $label = format_string(get_course_display_name_for_list($course), true, ['context' => $context]);
 158              $this->addOption($label, $course->id);
 159              array_push($coursestoselect, $course->id);
 160          }
 161  
 162          return $this->setSelected($values);
 163      }
 164  }


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