Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 25... Línea 25...
25
 * @subpackage questionbank
25
 * @subpackage questionbank
26
 * @copyright  2009 The Open University
26
 * @copyright  2009 The Open University
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 */
28
 */
Línea -... Línea 29...
-
 
29
 
-
 
30
use core\output\notification;
-
 
31
use core_cache\application_cache;
-
 
32
use core_cache\data_source_interface;
29
 
33
use core_cache\definition;
30
use core_question\local\bank\question_version_status;
34
use core_question\local\bank\question_version_status;
-
 
35
use core_question\output\question_version_info;
Línea 31... Línea 36...
31
use core_question\output\question_version_info;
36
use qbank_previewquestion\question_preview_options;
Línea 32... Línea 37...
32
 
37
 
Línea 78... Línea 83...
78
        $plugindir = core_component::get_plugin_directory('qtype', $qtypename);
83
        $plugindir = core_component::get_plugin_directory('qtype', $qtypename);
79
        return $plugindir && is_readable($plugindir . '/questiontype.php');
84
        return $plugindir && is_readable($plugindir . '/questiontype.php');
80
    }
85
    }
Línea 81... Línea 86...
81
 
86
 
-
 
87
    /**
-
 
88
     * Check if a given question type is one that is installed and usable.
-
 
89
     *
-
 
90
     * Use this before doing things like rendering buttons/options which will only work for
-
 
91
     * installed question types.
-
 
92
     *
-
 
93
     * When loaded through most of the core_question areas, qtype will still be the uninstalled type, e.g. 'mytype',
-
 
94
     * but when we get to the quiz pages, it will have been converted to 'missingtype'. So we need to check that
-
 
95
     * as well here.
-
 
96
     *
-
 
97
     * @param string $qtypename e.g. 'multichoice'.
-
 
98
     * @return bool
-
 
99
     */
-
 
100
    public static function is_qtype_usable(string $qtypename): bool {
-
 
101
        return self::is_qtype_installed($qtypename) && $qtypename !== 'missingtype';
-
 
102
    }
-
 
103
 
82
    /**
104
    /**
83
     * Get the question type class for a particular question type.
105
     * Get the question type class for a particular question type.
84
     * @param string $qtypename the question type name. For example 'multichoice' or 'shortanswer'.
106
     * @param string $qtypename the question type name. For example 'multichoice' or 'shortanswer'.
85
     * @param bool $mustexist if false, the missing question type is returned when
107
     * @param bool $mustexist if false, the missing question type is returned when
86
     *      the requested question type is not installed.
108
     *      the requested question type is not installed.
Línea 291... Línea 313...
291
        question_version_info::$pendingdefinitions[$definition->id] = $definition;
313
        question_version_info::$pendingdefinitions[$definition->id] = $definition;
292
        return $definition;
314
        return $definition;
293
    }
315
    }
Línea 294... Línea 316...
294
 
316
 
-
 
317
    /**
-
 
318
     * Render a throw-away preview of a question.
-
 
319
     *
-
 
320
     * If the question cannot be rendered (e.g. because it is not installed)
-
 
321
     * then display a message instead.
-
 
322
     *
-
 
323
     * @param question_definition $question a question.
-
 
324
     * @return string HTML to output.
-
 
325
     */
-
 
326
    public static function render_preview_of_question(question_definition $question): string {
-
 
327
        global $DB, $OUTPUT, $USER;
-
 
328
 
-
 
329
        if (!self::is_qtype_usable($question->qtype->name())) {
-
 
330
            // TODO MDL-84902 ideally this would be changed to render at least the qeuestion text.
-
 
331
            // See, for example, test_render_missing in question/type/missingtype/tests/missingtype_test.php.
-
 
332
            return $OUTPUT->notification(
-
 
333
                get_string('invalidquestiontype', 'question', $question->qtype->name()),
-
 
334
                notification::NOTIFY_WARNING,
-
 
335
                closebutton: false);
-
 
336
        }
-
 
337
 
-
 
338
        // TODO MDL-84902 remove this dependency on a class from qbank_previewquestion plugin.
-
 
339
        if (!class_exists(question_preview_options::class)) {
-
 
340
            debugging('Preview cannot be rendered. The standard plugin ' .
-
 
341
                'qbank_previewquestion plugin has been removed.', DEBUG_DEVELOPER);
-
 
342
            return '';
-
 
343
        }
-
 
344
 
-
 
345
        $quba = question_engine::make_questions_usage_by_activity(
-
 
346
            'core_question_preview', context_user::instance($USER->id));
-
 
347
        $options = new question_preview_options($question);
-
 
348
        $quba->set_preferred_behaviour($options->behaviour);
-
 
349
 
-
 
350
        $slot = $quba->add_question($question, $options->maxmark);
-
 
351
        $quba->start_question($slot, $options->variant);
-
 
352
 
-
 
353
        $transaction = $DB->start_delegated_transaction();
-
 
354
        question_engine::save_questions_usage_by_activity($quba);
-
 
355
        $transaction->allow_commit();
-
 
356
 
-
 
357
        return $quba->render_question($slot, $options, '1');
-
 
358
    }
-
 
359
 
295
    /**
360
    /**
296
     * Get all the versions of a particular question.
361
     * Get all the versions of a particular question.
297
     *
362
     *
298
     * @param int $questionid id of the question
363
     * @param int $questionid id of the question
299
     * @return array The array keys are version number, and the values are objects with three int fields
364
     * @return array The array keys are version number, and the values are objects with three int fields
Línea 337... Línea 402...
337
 
402
 
338
        return $result;
403
        return $result;
Línea 339... Línea 404...
339
    }
404
    }
-
 
405
 
-
 
406
    /**
-
 
407
     * Retrieves version information for a list of questions.
-
 
408
     *
-
 
409
     * @param array $questionids Array of question ids.
-
 
410
     * @return array An array question_bank_entries.id => version number => question.id.
-
 
411
     */
-
 
412
    public static function get_version_of_questions(array $questionids): array {
-
 
413
        global $DB;
-
 
414
 
-
 
415
        [$listquestionid, $params] = $DB->get_in_or_equal($questionids, SQL_PARAMS_NAMED);
-
 
416
        $sql = "SELECT qv.questionid, qv.version, qv.questionbankentryid
-
 
417
                  FROM {question_versions} qv
-
 
418
                  JOIN {question_bank_entries} qbe ON qv.questionbankentryid = qbe.id
-
 
419
                 WHERE qv.questionid $listquestionid
-
 
420
              ORDER BY qv.version DESC";
-
 
421
 
-
 
422
        $rows = $DB->get_recordset_sql($sql, $params);
-
 
423
        $result = [];
-
 
424
        foreach ($rows as $row) {
-
 
425
            $result[$row->questionbankentryid][$row->version] = $row->questionid;
-
 
426
        }
-
 
427
 
-
 
428
        return $result;
-
 
429
    }
340
 
430
 
341
    /**
431
    /**
342
     * @return question_finder a question finder.
432
     * @return question_finder a question finder.
343
     */
433
     */
344
    public static function get_finder() {
434
    public static function get_finder() {
Línea 484... Línea 574...
484
 * Class for loading questions according to various criteria.
574
 * Class for loading questions according to various criteria.
485
 *
575
 *
486
 * @copyright  2009 The Open University
576
 * @copyright  2009 The Open University
487
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
577
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
488
 */
578
 */
489
class question_finder implements cache_data_source {
579
class question_finder implements data_source_interface {
490
    /** @var question_finder the singleton instance of this class. */
580
    /** @var question_finder the singleton instance of this class. */
491
    protected static $questionfinder = null;
581
    protected static $questionfinder = null;
Línea 492... Línea 582...
492
 
582
 
493
    /**
583
    /**
Línea 498... Línea 588...
498
            self::$questionfinder = new question_finder();
588
            self::$questionfinder = new question_finder();
499
        }
589
        }
500
        return self::$questionfinder;
590
        return self::$questionfinder;
501
    }
591
    }
Línea 502... Línea 592...
502
 
592
 
503
    /* See cache_data_source::get_instance_for_cache. */
593
    #[\Override]
504
    public static function get_instance_for_cache(cache_definition $definition) {
594
    public static function get_instance_for_cache(definition $definition) {
505
        return self::get_instance();
595
        return self::get_instance();
Línea 506... Línea 596...
506
    }
596
    }
507
 
597
 
508
    /**
598
    /**
509
     * @return cache_application the question definition cache we are using.
599
     * @return application_cache the question definition cache we are using.
510
     */
600
     */
511
    protected function get_data_cache() {
601
    protected function get_data_cache() {
512
        // Do not double cache here because it may break cache resetting.
602
        // Do not double cache here because it may break cache resetting.
Línea 670... Línea 760...
670
                                               WHERE $where $extraconditions
760
                                               WHERE $where $extraconditions
671
                                            ORDER BY previous_attempts",
761
                                            ORDER BY previous_attempts",
672
                $qubaids->from_where_params() + $params + $extraparams);
762
                $qubaids->from_where_params() + $params + $extraparams);
673
    }
763
    }
Línea 674... Línea 764...
674
 
764
 
675
    /* See cache_data_source::load_for_cache. */
765
    #[\Override]
676
    public function load_for_cache($questionid) {
766
    public function load_for_cache($questionid) {
Línea 677... Línea 767...
677
        global $DB;
767
        global $DB;
678
 
768
 
Línea 694... Línea 784...
694
        $questiondata = $DB->get_record_sql($sql, ['id' => $questionid], MUST_EXIST);
784
        $questiondata = $DB->get_record_sql($sql, ['id' => $questionid], MUST_EXIST);
695
        get_question_options($questiondata);
785
        get_question_options($questiondata);
696
        return $questiondata;
786
        return $questiondata;
697
    }
787
    }
Línea 698... Línea 788...
698
 
788
 
699
    /* See cache_data_source::load_many_for_cache. */
789
    #[\Override]
700
    public function load_many_for_cache(array $questionids) {
790
    public function load_many_for_cache(array $questionids) {
Línea 701... Línea 791...
701
        global $DB;
791
        global $DB;
702
 
792