[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/question/classes/bank/search/ -> category_condition.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   * A search class to control from which category questions are listed.
  20   *
  21   * @package   core_question
  22   * @copyright 2013 Ray Morris
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace core_question\bank\search;
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   *  This class controls from which category questions are listed.
  31   *
  32   * @copyright 2013 Ray Morris
  33   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class category_condition extends condition {
  36      /** @var \stdClass The course record. */
  37      protected $course;
  38  
  39      /** @var \stdClass The category record. */
  40      protected $category;
  41  
  42      /** @var array of contexts. */
  43      protected $contexts;
  44  
  45      /** @var bool Whether to include questions from sub-categories. */
  46      protected $recurse;
  47  
  48      /** @var string SQL fragment to add to the where clause. */
  49      protected $where;
  50  
  51      /** @var array query param used in where. */
  52      protected $params;
  53  
  54      /** @var string categoryID,contextID as used with question_bank_view->display(). */
  55      protected $cat;
  56  
  57      /** @var int The maximum displayed length of the category info. */
  58      protected $maxinfolength;
  59  
  60      /**
  61       * Constructor
  62       * @param string     $cat           categoryID,contextID as used with question_bank_view->display()
  63       * @param bool       $recurse       Whether to include questions from sub-categories
  64       * @param array      $contexts      Context objects as used by question_category_options()
  65       * @param \moodle_url $baseurl       The URL the form is submitted to
  66       * @param \stdClass   $course        Course record
  67       * @param integer    $maxinfolength The maximum displayed length of the category info.
  68       */
  69      public function __construct($cat = null, $recurse = false, $contexts, $baseurl, $course, $maxinfolength = null) {
  70          $this->cat = $cat;
  71          $this->recurse = $recurse;
  72          $this->contexts = $contexts;
  73          $this->baseurl = $baseurl;
  74          $this->course = $course;
  75          $this->init();
  76          $this->maxinfolength = $maxinfolength;
  77      }
  78  
  79      /**
  80       * Initialize the object so it will be ready to return where() and params()
  81       */
  82      private function init() {
  83          global $DB;
  84          if (!$this->category = $this->get_current_category($this->cat)) {
  85              return;
  86          }
  87          if ($this->recurse) {
  88              $categoryids = question_categorylist($this->category->id);
  89          } else {
  90              $categoryids = array($this->category->id);
  91          }
  92          list($catidtest, $this->params) = $DB->get_in_or_equal($categoryids, SQL_PARAMS_NAMED, 'cat');
  93          $this->where = 'q.category ' . $catidtest;
  94      }
  95  
  96      public function where() {
  97          return  $this->where;
  98      }
  99  
 100      public function params() {
 101          return $this->params;
 102      }
 103  
 104      /**
 105       * Called by question_bank_view to display the GUI for selecting a category
 106       */
 107      public function display_options() {
 108          $this->display_category_form($this->contexts, $this->baseurl, $this->cat);
 109          $this->print_category_info($this->category);
 110      }
 111  
 112      /**
 113       * Displays the recursion checkbox GUI.
 114       * question_bank_view places this within the section that is hidden by default
 115       */
 116      public function display_options_adv() {
 117          echo \html_writer::start_div();
 118          echo \html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'recurse',
 119                                                 'value' => 0, 'id' => 'recurse_off'));
 120          echo \html_writer::checkbox('recurse', '1', $this->recurse, get_string('includesubcategories', 'question'),
 121                                         array('id' => 'recurse_on', 'class' => 'searchoptions'));
 122          echo \html_writer::end_div() . "\n";
 123      }
 124  
 125      /**
 126       * Display the drop down to select the category.
 127       *
 128       * @param array $contexts of contexts that can be accessed from here.
 129       * @param \moodle_url $pageurl the URL of this page.
 130       * @param string $current 'categoryID,contextID'.
 131       */
 132      protected function display_category_form($contexts, $pageurl, $current) {
 133          global $OUTPUT;
 134  
 135          echo \html_writer::start_div('choosecategory');
 136          $catmenu = question_category_options($contexts, false, 0, true);
 137          echo \html_writer::label(get_string('selectacategory', 'question'), 'id_selectacategory');
 138          echo \html_writer::select($catmenu, 'category', $current, array(), array('class' => 'searchoptions', 'id' => 'id_selectacategory'));
 139          echo \html_writer::end_div() . "\n";
 140      }
 141  
 142      /**
 143       * Look up the category record based on cateogry ID and context
 144       * @param string $categoryandcontext categoryID,contextID as used with question_bank_view->display()
 145       * @return \stdClass The category record
 146       */
 147      protected function get_current_category($categoryandcontext) {
 148          global $DB, $OUTPUT;
 149          list($categoryid, $contextid) = explode(',', $categoryandcontext);
 150          if (!$categoryid) {
 151              $this->print_choose_category_message($categoryandcontext);
 152              return false;
 153          }
 154  
 155          if (!$category = $DB->get_record('question_categories',
 156                  array('id' => $categoryid, 'contextid' => $contextid))) {
 157              echo $OUTPUT->box_start('generalbox questionbank');
 158              echo $OUTPUT->notification('Category not found!');
 159              echo $OUTPUT->box_end();
 160              return false;
 161          }
 162  
 163          return $category;
 164      }
 165  
 166      /**
 167       * Print the category description
 168       * @param stdClass $category the category information form the database.
 169       */
 170      protected function print_category_info($category) {
 171          $formatoptions = new \stdClass();
 172          $formatoptions->noclean = true;
 173          $formatoptions->overflowdiv = true;
 174          echo \html_writer::start_div('boxaligncenter categoryinfo');
 175          if (isset($this->maxinfolength)) {
 176              echo shorten_text(format_text($category->info, $category->infoformat, $formatoptions, $this->course->id),
 177                                       $this->maxinfolength);
 178          } else {
 179              echo format_text($category->info, $category->infoformat, $formatoptions, $this->course->id);
 180          }
 181          echo \html_writer::end_div() . "\n";
 182      }
 183  }


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