From 7f4b6dfe70f07f32193916d97ae04bc7ab7f059c Mon Sep 17 00:00:00 2001 From: Simey Lameze Date: Wed, 31 Jan 2018 13:43:05 +0800 Subject: [PATCH] MDL-61133 core_question: tags form and fragment callback --- question/lib.php | 67 +++++++++++++++++++++++++++++++++++++ question/type/tags_form.php | 61 +++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 question/lib.php create mode 100644 question/type/tags_form.php diff --git a/question/lib.php b/question/lib.php new file mode 100644 index 00000000000..5c04b33831d --- /dev/null +++ b/question/lib.php @@ -0,0 +1,67 @@ +. + +/** + * Question related functions. + * + * This file was created just because Fragment API expects callbacks to be defined on lib.php. + * + * Please, do not add new functions to this file. + * + * @package core_question + * @copyright 2018 Simey Lameze + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +/** + * Question tags fragment callback. + * + * @param array $args Arguments to the form. + * @return null|string The rendered form. + */ +function core_question_output_fragment_tags_form($args) { + + if (!empty($args['id'])) { + global $CFG, $DB; + require_once($CFG->dirroot . '/question/type/tags_form.php'); + require_once($CFG->libdir . '/questionlib.php'); + $id = clean_param($args['id'], PARAM_INT); + + $question = $DB->get_record('question', ['id' => $id]); + $category = $DB->get_record('question_categories', array('id' => $question->category)); + $context = \context::instance_by_id($category->contextid); + + $toform = new stdClass(); + $toform->id = $question->id; + $toform->questioncategory = $category->name; + $toform->questionname = $question->name; + $toform->categoryid = $category->id; + $toform->contextid = $category->contextid; + $toform->context = $context->get_context_name(); + + if (core_tag_tag::is_enabled('core_question', 'question')) { + $toform->tags = core_tag_tag::get_item_tags_array('core_question', 'question', $question->id); + } + + $canedit = question_has_capability_on($question, 'edit'); + $mform = new \core_question\form\tags(null, null, 'post', '', null, $canedit, $toform); + $mform->set_data($toform); + + return $mform->render(); + } +} diff --git a/question/type/tags_form.php b/question/type/tags_form.php new file mode 100644 index 00000000000..f2a59c66df7 --- /dev/null +++ b/question/type/tags_form.php @@ -0,0 +1,61 @@ +. + +/** + * The mform to manage question tags. + * + * @package core_question + * @copyright 2018 Simey Lameze + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace core_question\form; + +defined('MOODLE_INTERNAL') || die(); + +require_once($CFG->dirroot . '/lib/formslib.php'); + +/** + * The mform class for manage question tags. + * + * @copyright 2018 Simey Lameze + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class tags extends \moodleform { + + /** + * The form definition + */ + public function definition() { + $mform = $this->_form; + + $mform->addElement('hidden', 'id'); + $mform->setType('id', PARAM_INT); + + $mform->addElement('hidden', 'categoryid'); + $mform->setType('categoryid', PARAM_INT); + + $mform->addElement('hidden', 'contextid'); + $mform->setType('contextid', PARAM_INT); + + $mform->addElement('static', 'questionname', get_string('questionname', 'question')); + $mform->addElement('static', 'questioncategory', get_string('categorycurrent', 'question')); + $mform->addElement('static', 'context', ''); + + $mform->addElement('tags', 'tags', get_string('tags'), + ['itemtype' => 'question', 'component' => 'core_question']); + } +} -- 2.43.0