[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/question/ -> editlib.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   * Functions used to show question editing interface
  19   *
  20   * @package    moodlecore
  21   * @subpackage questionbank
  22   * @copyright  1999 onwards Martin Dougiamas and others {@link http://moodle.com}
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  
  27  use core_question\bank\search\category_condition;
  28  
  29  defined('MOODLE_INTERNAL') || die();
  30  
  31  require_once($CFG->libdir . '/questionlib.php');
  32  
  33  define('DEFAULT_QUESTIONS_PER_PAGE', 20);
  34  
  35  function get_module_from_cmid($cmid) {
  36      global $CFG, $DB;
  37      if (!$cmrec = $DB->get_record_sql("SELECT cm.*, md.name as modname
  38                                 FROM {course_modules} cm,
  39                                      {modules} md
  40                                 WHERE cm.id = ? AND
  41                                       md.id = cm.module", array($cmid))){
  42          print_error('invalidcoursemodule');
  43      } elseif (!$modrec =$DB->get_record($cmrec->modname, array('id' => $cmrec->instance))) {
  44          print_error('invalidcoursemodule');
  45      }
  46      $modrec->instance = $modrec->id;
  47      $modrec->cmid = $cmrec->id;
  48      $cmrec->name = $modrec->name;
  49  
  50      return array($modrec, $cmrec);
  51  }
  52  /**
  53  * Function to read all questions for category into big array
  54  *
  55  * @param int $category category number
  56  * @param bool $noparent if true only questions with NO parent will be selected
  57  * @param bool $recurse include subdirectories
  58  * @param bool $export set true if this is called by questionbank export
  59  */
  60  function get_questions_category( $category, $noparent=false, $recurse=true, $export=true ) {
  61      global $DB;
  62  
  63      // Build sql bit for $noparent
  64      $npsql = '';
  65      if ($noparent) {
  66        $npsql = " and parent='0' ";
  67      }
  68  
  69      // Get list of categories
  70      if ($recurse) {
  71          $categorylist = question_categorylist($category->id);
  72      } else {
  73          $categorylist = array($category->id);
  74      }
  75  
  76      // Get the list of questions for the category
  77      list($usql, $params) = $DB->get_in_or_equal($categorylist);
  78      $questions = $DB->get_records_select('question', "category {$usql} {$npsql}", $params, 'qtype, name');
  79  
  80      // Iterate through questions, getting stuff we need
  81      $qresults = array();
  82      foreach($questions as $key => $question) {
  83          $question->export_process = $export;
  84          $qtype = question_bank::get_qtype($question->qtype, false);
  85          if ($export && $qtype->name() == 'missingtype') {
  86              // Unrecognised question type. Skip this question when exporting.
  87              continue;
  88          }
  89          $qtype->get_question_options($question);
  90          $qresults[] = $question;
  91      }
  92  
  93      return $qresults;
  94  }
  95  
  96  /**
  97   * @param int $categoryid a category id.
  98   * @return bool whether this is the only top-level category in a context.
  99   */
 100  function question_is_only_toplevel_category_in_context($categoryid) {
 101      global $DB;
 102      return 1 == $DB->count_records_sql("
 103              SELECT count(*)
 104                FROM {question_categories} c1,
 105                     {question_categories} c2
 106               WHERE c2.id = ?
 107                 AND c1.contextid = c2.contextid
 108                 AND c1.parent = 0 AND c2.parent = 0", array($categoryid));
 109  }
 110  
 111  /**
 112   * Check whether this user is allowed to delete this category.
 113   *
 114   * @param int $todelete a category id.
 115   */
 116  function question_can_delete_cat($todelete) {
 117      global $DB;
 118      if (question_is_only_toplevel_category_in_context($todelete)) {
 119          print_error('cannotdeletecate', 'question');
 120      } else {
 121          $contextid = $DB->get_field('question_categories', 'contextid', array('id' => $todelete));
 122          require_capability('moodle/question:managecategory', context::instance_by_id($contextid));
 123      }
 124  }
 125  
 126  
 127  /**
 128   * Base class for representing a column in a {@link question_bank_view}.
 129   *
 130   * @copyright  2009 Tim Hunt
 131   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 132   * @deprecated since Moodle 2.7 MDL-40457
 133   */
 134  class_alias('core_question\bank\column_base', 'question_bank_column_base', true);
 135  
 136  /**
 137   * A column with a checkbox for each question with name q{questionid}.
 138   *
 139   * @copyright  2009 Tim Hunt
 140   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 141   * @deprecated since Moodle 2.7 MDL-40457
 142   */
 143  class_alias('core_question\bank\checkbox_column', 'question_bank_checkbox_column', true);
 144  
 145  /**
 146   * A column type for the name of the question type.
 147   *
 148   * @copyright  2009 Tim Hunt
 149   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 150   * @deprecated since Moodle 2.7 MDL-40457
 151   */
 152  class_alias('core_question\bank\question_type_column', 'question_bank_question_type_column', true);
 153  
 154  
 155  /**
 156   * A column type for the name of the question name.
 157   *
 158   * @copyright  2009 Tim Hunt
 159   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 160   * @deprecated since Moodle 2.7 MDL-40457
 161   */
 162  class_alias('core_question\bank\question_name_column', 'question_bank_question_name_column', true);
 163  
 164  
 165  /**
 166   * A column type for the name of the question creator.
 167   *
 168   * @copyright  2009 Tim Hunt
 169   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 170   * @deprecated since Moodle 2.7 MDL-40457
 171   */
 172  class_alias('core_question\bank\creator_name_column', 'question_bank_creator_name_column', true);
 173  
 174  
 175  /**
 176   * A column type for the name of the question last modifier.
 177   *
 178   * @copyright  2009 Tim Hunt
 179   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 180   * @deprecated since Moodle 2.7 MDL-40457
 181   */
 182  class_alias('core_question\bank\modifier_name_column', 'question_bank_modifier_name_column', true);
 183  
 184  
 185  /**
 186   * A base class for actions that are an icon that lets you manipulate the question in some way.
 187   *
 188   * @copyright  2009 Tim Hunt
 189   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 190   * @deprecated since Moodle 2.7 MDL-40457
 191   */
 192  class_alias('core_question\bank\action_column_base', 'question_bank_action_column_base', true);
 193  
 194  
 195  /**
 196   * Base class for question bank columns that just contain an action icon.
 197   *
 198   * @copyright  2009 Tim Hunt
 199   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 200   * @deprecated since Moodle 2.7 MDL-40457
 201   */
 202  class_alias('core_question\bank\edit_action_column', 'question_bank_edit_action_column', true);
 203  
 204  /**
 205   * Question bank column for the duplicate action icon.
 206   *
 207   * @copyright  2013 The Open University
 208   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 209   * @deprecated since Moodle 2.7 MDL-40457
 210   */
 211  class_alias('core_question\bank\copy_action_column', 'question_bank_copy_action_column', true);
 212  
 213  /**
 214   * Question bank columns for the preview action icon.
 215   *
 216   * @copyright  2009 Tim Hunt
 217   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 218   * @deprecated since Moodle 2.7 MDL-40457
 219   */
 220  class_alias('core_question\bank\preview_action_column', 'question_bank_preview_action_column', true);
 221  
 222  
 223  /**
 224   * action to delete (or hide) a question, or restore a previously hidden question.
 225   *
 226   * @copyright  2009 Tim Hunt
 227   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 228   * @deprecated since Moodle 2.7 MDL-40457
 229   */
 230  class_alias('core_question\bank\delete_action_column', 'question_bank_delete_action_column', true);
 231  
 232  /**
 233   * Base class for 'columns' that are actually displayed as a row following the main question row.
 234   *
 235   * @copyright  2009 Tim Hunt
 236   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 237   * @deprecated since Moodle 2.7 MDL-40457
 238   */
 239  class_alias('core_question\bank\row_base', 'question_bank_row_base', true);
 240  
 241  /**
 242   * A column type for the name of the question name.
 243   *
 244   * @copyright  2009 Tim Hunt
 245   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 246   * @deprecated since Moodle 2.7 MDL-40457
 247   */
 248  class_alias('core_question\bank\question_text_row', 'question_bank_question_text_row', true);
 249  
 250  /**
 251   * @copyright  2009 Tim Hunt
 252   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 253   * @deprecated since Moodle 2.7 MDL-40457
 254   */
 255  class_alias('core_question\bank\view', 'question_bank_view', true);
 256  
 257  /**
 258   * Common setup for all pages for editing questions.
 259   * @param string $baseurl the name of the script calling this funciton. For examle 'qusetion/edit.php'.
 260   * @param string $edittab code for this edit tab
 261   * @param bool $requirecmid require cmid? default false
 262   * @param bool $requirecourseid require courseid, if cmid is not given? default true
 263   * @return array $thispageurl, $contexts, $cmid, $cm, $module, $pagevars
 264   */
 265  function question_edit_setup($edittab, $baseurl, $requirecmid = false, $requirecourseid = true) {
 266      global $DB, $PAGE;
 267  
 268      $thispageurl = new moodle_url($baseurl);
 269      $thispageurl->remove_all_params(); // We are going to explicity add back everything important - this avoids unwanted params from being retained.
 270  
 271      if ($requirecmid){
 272          $cmid =required_param('cmid', PARAM_INT);
 273      } else {
 274          $cmid = optional_param('cmid', 0, PARAM_INT);
 275      }
 276      if ($cmid){
 277          list($module, $cm) = get_module_from_cmid($cmid);
 278          $courseid = $cm->course;
 279          $thispageurl->params(compact('cmid'));
 280          require_login($courseid, false, $cm);
 281          $thiscontext = context_module::instance($cmid);
 282      } else {
 283          $module = null;
 284          $cm = null;
 285          if ($requirecourseid){
 286              $courseid  = required_param('courseid', PARAM_INT);
 287          } else {
 288              $courseid  = optional_param('courseid', 0, PARAM_INT);
 289          }
 290          if ($courseid){
 291              $thispageurl->params(compact('courseid'));
 292              require_login($courseid, false);
 293              $thiscontext = context_course::instance($courseid);
 294          } else {
 295              $thiscontext = null;
 296          }
 297      }
 298  
 299      if ($thiscontext){
 300          $contexts = new question_edit_contexts($thiscontext);
 301          $contexts->require_one_edit_tab_cap($edittab);
 302  
 303      } else {
 304          $contexts = null;
 305      }
 306  
 307      $PAGE->set_pagelayout('admin');
 308  
 309      $pagevars['qpage'] = optional_param('qpage', -1, PARAM_INT);
 310  
 311      //pass 'cat' from page to page and when 'category' comes from a drop down menu
 312      //then we also reset the qpage so we go to page 1 of
 313      //a new cat.
 314      $pagevars['cat'] = optional_param('cat', 0, PARAM_SEQUENCE); // if empty will be set up later
 315      if ($category = optional_param('category', 0, PARAM_SEQUENCE)) {
 316          if ($pagevars['cat'] != $category) { // is this a move to a new category?
 317              $pagevars['cat'] = $category;
 318              $pagevars['qpage'] = 0;
 319          }
 320      }
 321      if ($pagevars['cat']){
 322          $thispageurl->param('cat', $pagevars['cat']);
 323      }
 324      if (strpos($baseurl, '/question/') === 0) {
 325          navigation_node::override_active_url($thispageurl);
 326      }
 327  
 328      if ($pagevars['qpage'] > -1) {
 329          $thispageurl->param('qpage', $pagevars['qpage']);
 330      } else {
 331          $pagevars['qpage'] = 0;
 332      }
 333  
 334      $pagevars['qperpage'] = question_get_display_preference(
 335              'qperpage', DEFAULT_QUESTIONS_PER_PAGE, PARAM_INT, $thispageurl);
 336  
 337      for ($i = 1; $i <= question_bank_view::MAX_SORTS; $i++) {
 338          $param = 'qbs' . $i;
 339          if (!$sort = optional_param($param, '', PARAM_TEXT)) {
 340              break;
 341          }
 342          $thispageurl->param($param, $sort);
 343      }
 344  
 345      $defaultcategory = question_make_default_categories($contexts->all());
 346  
 347      $contextlistarr = array();
 348      foreach ($contexts->having_one_edit_tab_cap($edittab) as $context){
 349          $contextlistarr[] = "'{$context->id}'";
 350      }
 351      $contextlist = join($contextlistarr, ' ,');
 352      if (!empty($pagevars['cat'])){
 353          $catparts = explode(',', $pagevars['cat']);
 354          if (!$catparts[0] || (false !== array_search($catparts[1], $contextlistarr)) ||
 355                  !$DB->count_records_select("question_categories", "id = ? AND contextid = ?", array($catparts[0], $catparts[1]))) {
 356              print_error('invalidcategory', 'question');
 357          }
 358      } else {
 359          $category = $defaultcategory;
 360          $pagevars['cat'] = "{$category->id},{$category->contextid}";
 361      }
 362  
 363      // Display options.
 364      $pagevars['recurse']    = question_get_display_preference('recurse',    1, PARAM_BOOL, $thispageurl);
 365      $pagevars['showhidden'] = question_get_display_preference('showhidden', 0, PARAM_BOOL, $thispageurl);
 366      $pagevars['qbshowtext'] = question_get_display_preference('qbshowtext', 0, PARAM_BOOL, $thispageurl);
 367  
 368      // Category list page.
 369      $pagevars['cpage'] = optional_param('cpage', 1, PARAM_INT);
 370      if ($pagevars['cpage'] != 1){
 371          $thispageurl->param('cpage', $pagevars['cpage']);
 372      }
 373  
 374      return array($thispageurl, $contexts, $cmid, $cm, $module, $pagevars);
 375  }
 376  
 377  /**
 378   * Get the category id from $pagevars.
 379   * @param array $pagevars from {@link question_edit_setup()}.
 380   * @return int the category id.
 381   */
 382  function question_get_category_id_from_pagevars(array $pagevars) {
 383      list($questioncategoryid) = explode(',', $pagevars['cat']);
 384      return $questioncategoryid;
 385  }
 386  
 387  /**
 388   * Get a particular question preference that is also stored as a user preference.
 389   * If the the value is given in the GET/POST request, then that value is used,
 390   * and the user preference is updated to that value. Otherwise, the last set
 391   * value of the user preference is used, or if it has never been set the default
 392   * passed to this function.
 393   *
 394   * @param string $param the param name. The URL parameter set, and the GET/POST
 395   *      parameter read. The user_preference name is 'question_bank_' . $param.
 396   * @param mixed $default The default value to use, if not otherwise set.
 397   * @param int $type one of the PARAM_... constants.
 398   * @param moodle_url $thispageurl if the value has been explicitly set, we add
 399   *      it to this URL.
 400   * @return mixed the parameter value to use.
 401   */
 402  function question_get_display_preference($param, $default, $type, $thispageurl) {
 403      $submittedvalue = optional_param($param, null, $type);
 404      if (is_null($submittedvalue)) {
 405          return get_user_preferences('question_bank_' . $param, $default);
 406      }
 407  
 408      set_user_preference('question_bank_' . $param, $submittedvalue);
 409      $thispageurl->param($param, $submittedvalue);
 410      return $submittedvalue;
 411  }
 412  
 413  /**
 414   * Make sure user is logged in as required in this context.
 415   */
 416  function require_login_in_context($contextorid = null){
 417      global $DB, $CFG;
 418      if (!is_object($contextorid)){
 419          $context = context::instance_by_id($contextorid, IGNORE_MISSING);
 420      } else {
 421          $context = $contextorid;
 422      }
 423      if ($context && ($context->contextlevel == CONTEXT_COURSE)) {
 424          require_login($context->instanceid);
 425      } else if ($context && ($context->contextlevel == CONTEXT_MODULE)) {
 426          if ($cm = $DB->get_record('course_modules',array('id' =>$context->instanceid))) {
 427              if (!$course = $DB->get_record('course', array('id' => $cm->course))) {
 428                  print_error('invalidcourseid');
 429              }
 430              require_course_login($course, true, $cm);
 431  
 432          } else {
 433              print_error('invalidcoursemodule');
 434          }
 435      } else if ($context && ($context->contextlevel == CONTEXT_SYSTEM)) {
 436          if (!empty($CFG->forcelogin)) {
 437              require_login();
 438          }
 439  
 440      } else {
 441          require_login();
 442      }
 443  }
 444  
 445  /**
 446   * Print a form to let the user choose which question type to add.
 447   * When the form is submitted, it goes to the question.php script.
 448   * @param $hiddenparams hidden parameters to add to the form, in addition to
 449   *      the qtype radio buttons.
 450   * @param $allowedqtypes optional list of qtypes that are allowed. If given, only
 451   *      those qtypes will be shown. Example value array('description', 'multichoice').
 452   */
 453  function print_choose_qtype_to_add_form($hiddenparams, array $allowedqtypes = null, $enablejs = true) {
 454      global $CFG, $PAGE, $OUTPUT;
 455  
 456      if ($enablejs) {
 457          // Add the chooser.
 458          $PAGE->requires->yui_module('moodle-question-chooser', 'M.question.init_chooser', array(array()));
 459      }
 460  
 461      $realqtypes = array();
 462      $fakeqtypes = array();
 463      foreach (question_bank::get_creatable_qtypes() as $qtypename => $qtype) {
 464          if ($allowedqtypes && !in_array($qtypename, $allowedqtypes)) {
 465              continue;
 466          }
 467          if ($qtype->is_real_question_type()) {
 468              $realqtypes[] = $qtype;
 469          } else {
 470              $fakeqtypes[] = $qtype;
 471          }
 472      }
 473  
 474      $renderer = $PAGE->get_renderer('question', 'bank');
 475      return $renderer->qbank_chooser($realqtypes, $fakeqtypes, $PAGE->course, $hiddenparams);
 476  }
 477  
 478  /**
 479   * Print a button for creating a new question. This will open question/addquestion.php,
 480   * which in turn goes to question/question.php before getting back to $params['returnurl']
 481   * (by default the question bank screen).
 482   *
 483   * @param int $categoryid The id of the category that the new question should be added to.
 484   * @param array $params Other paramters to add to the URL. You need either $params['cmid'] or
 485   *      $params['courseid'], and you should probably set $params['returnurl']
 486   * @param string $caption the text to display on the button.
 487   * @param string $tooltip a tooltip to add to the button (optional).
 488   * @param bool $disabled if true, the button will be disabled.
 489   */
 490  function create_new_question_button($categoryid, $params, $caption, $tooltip = '', $disabled = false) {
 491      global $CFG, $PAGE, $OUTPUT;
 492      static $choiceformprinted = false;
 493      $params['category'] = $categoryid;
 494      $url = new moodle_url('/question/addquestion.php', $params);
 495      echo $OUTPUT->single_button($url, $caption, 'get', array('disabled'=>$disabled, 'title'=>$tooltip));
 496  
 497      if (!$choiceformprinted) {
 498          echo '<div id="qtypechoicecontainer">';
 499          echo print_choose_qtype_to_add_form(array());
 500          echo "</div>\n";
 501          $choiceformprinted = true;
 502      }
 503  }
 504  
 505  


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