From e6890b1164ca3499205fe18bf582a12be838f4e2 Mon Sep 17 00:00:00 2001 From: Simey Lameze Date: Mon, 26 Mar 2018 11:24:50 +0800 Subject: [PATCH] MDL-61364 question: add support for course tags on modal --- question/amd/build/edit_tags.min.js | Bin 1612 -> 1858 bytes question/amd/build/repository.min.js | Bin 193 -> 222 bytes question/amd/src/edit_tags.js | 47 +++++++++++- question/amd/src/repository.js | 4 +- question/classes/bank/tags_action_column.php | 7 +- question/classes/external.php | 74 ++++++++++++------- question/lib.php | 44 +++++++---- question/type/tags_form.php | 28 ++++++- 8 files changed, 155 insertions(+), 49 deletions(-) diff --git a/question/amd/build/edit_tags.min.js b/question/amd/build/edit_tags.min.js index cb3dc1915b0b562302ee01864c3c5d6026badcc9..f452866bfe7876a23ff6f952cb6e05de2e9c76bc 100644 GIT binary patch delta 239 zcmX@ZbBJ$)F_TWBPLgJIqF#DxiBo<`rG}QdC zPQm0*Mg?z>jD`|I3Mf%%n^u~aT#}ier-9G7!qU`YkRq6QMIiG;A?6{;0SzqPtjUzX z$XGJDi}^NV!Q?cSbRc<-WgSl;(1KJ=oqQlwFu96V52$1%t1`DvifxfbqNYx&ZNcP| Ztm-T&I;lF7Kd=UK7Ha?O^M~v%=EU;*!k#yv!7yOq9X0>} delta 50 zcmcb|c#v^|4ZCJ_Sz?hw@get_record('question_categories', ['id' => $question->category], 'contextid'); - $url = $this->qbank->edit_question_url($question->id); + $qbank = $this->qbank; + $url = $qbank->edit_question_url($question->id); + $editingcontext = $qbank->get_most_specific_context(); - $this->print_tag_icon($question->id, $url, $cantag, $category->contextid); + $this->print_tag_icon($question->id, $url, $cantag, $editingcontext->id); } } diff --git a/question/classes/external.php b/question/classes/external.php index ecc48767739..f2011d45e6b 100644 --- a/question/classes/external.php +++ b/question/classes/external.php @@ -122,6 +122,8 @@ class core_question_external extends external_api { */ public static function submit_tags_form_parameters() { return new external_function_parameters([ + 'questionid' => new external_value(PARAM_INT, 'The question id'), + 'contextid' => new external_value(PARAM_INT, 'The editing context id'), 'formdata' => new external_value(PARAM_RAW, 'The data from the tag form'), ]); } @@ -129,51 +131,67 @@ class core_question_external extends external_api { /** * Handles the tags form submission. * + * @param int $questionid The question id. + * @param int $contextid The editing context id. * @param string $formdata The question tag form data in a URI encoded param string * @return array The created or modified question tag - * @throws moodle_exception */ - public static function submit_tags_form($formdata) { - global $USER, $DB, $CFG; + public static function submit_tags_form($questionid, $contextid, $formdata) { + global $DB, $CFG; $data = []; $result = ['status' => false]; // Parameter validation. - $params = self::validate_parameters(self::submit_tags_form_parameters(), ['formdata' => $formdata]); - $context = \context_user::instance($USER->id); + $params = self::validate_parameters(self::submit_tags_form_parameters(), [ + 'questionid' => $questionid, + 'contextid' => $contextid, + 'formdata' => $formdata + ]); - self::validate_context($context); + $editingcontext = \context::instance_by_id($contextid); + self::validate_context($editingcontext); parse_str($params['formdata'], $data); - if (!empty($data['id'])) { - $questionid = clean_param($data['id'], PARAM_INT); - $question = $DB->get_record('question', array('id' => $questionid)); + if (!$question = $DB->get_record_sql(' + SELECT q.*, qc.contextid + FROM {question} q + JOIN {question_categories} qc ON qc.id = q.category + WHERE q.id = ?', [$questionid])) { + print_error('questiondoesnotexist', 'question'); + } + + require_once($CFG->libdir . '/questionlib.php'); + require_once($CFG->dirroot . '/question/type/tags_form.php'); - require_once($CFG->libdir . '/questionlib.php'); - $cantag = question_has_capability_on($question, 'tag'); + $cantag = question_has_capability_on($question, 'tag'); + $questioncontext = \context::instance_by_id($question->contextid); + $formoptions = [ + 'editingcontext' => $editingcontext, + 'questioncontext' => $questioncontext + ]; - require_once($CFG->dirroot . '/question/type/tags_form.php'); - $mform = new \core_question\form\tags(null, null, 'post', '', null, $cantag, $data); + $mform = new \core_question\form\tags(null, $formoptions, 'post', '', null, $cantag, $data); - if ($validateddata = $mform->get_data()) { - // Due to a mform bug, if there's no tags set on the tag element, it submits the name as the value. - // The only way to discover is checking if the tag element is an array. - if ($cantag) { - if (is_array($validateddata->tags)) { - $categorycontext = context::instance_by_id($validateddata->contextid); + if ($validateddata = $mform->get_data()) { + if ($cantag) { + if (isset($validateddata->tags)) { + // Due to a mform bug, if there's no tags set on the tag element, it submits the name as the value. + // The only way to discover is checking if the tag element is an array. + $tags = is_array($validateddata->tags) ? $validateddata->tags : []; - core_tag_tag::set_item_tags('core_question', 'question', $validateddata->id, - $categorycontext, $validateddata->tags); + core_tag_tag::set_item_tags('core_question', 'question', $validateddata->id, + $questioncontext, $tags); + + $result['status'] = true; + } - $result['status'] = true; - } else { - // If the tags element is not array, this means we don't have any tags to be set. - // This is the only way to assume the user removed all tags from the question. - core_tag_tag::remove_all_item_tags('core_question', 'question', $validateddata->id); + if (isset($validateddata->coursetags)) { + $coursetags = is_array($validateddata->coursetags) ? $validateddata->coursetags : []; + core_tag_tag::set_item_tags('core_question', 'question', $validateddata->id, + $editingcontext->get_course_context(false), $coursetags); - $result['status'] = true; - } + $result['status'] = true; } } } diff --git a/question/lib.php b/question/lib.php index 842e050a95e..5a731c492c2 100644 --- a/question/lib.php +++ b/question/lib.php @@ -41,26 +41,42 @@ function core_question_output_fragment_tags_form($args) { require_once($CFG->dirroot . '/question/type/tags_form.php'); require_once($CFG->libdir . '/questionlib.php'); $id = clean_param($args['id'], PARAM_INT); + $editingcontext = $args['context']; $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); + if ($coursecontext = $editingcontext->get_course_context(false)) { + $course = $DB->get_record('course', ['id' => $coursecontext->instanceid]); + $filtercourses = [$course]; + } else { + $filtercourses = null; } + // Load the question tags and filter the course tags by the current + // course. + get_question_options($question, true, $filtercourses); + + $category = $question->categoryobject; + $questioncontext = \context::instance_by_id($category->contextid); + + $formoptions = [ + 'editingcontext' => $editingcontext, + 'questioncontext' => $questioncontext + ]; + $data = [ + 'id' => $question->id, + 'questioncategory' => $category->name, + 'questionname' => $question->name, + 'categoryid' => $category->id, + 'contextid' => $category->contextid, + 'context' => $questioncontext->get_context_name(), + 'tags' => isset($question->tags) ? $question->tags : [], + 'coursetags' => isset($question->coursetags) ? $question->coursetags : [], + ]; + $cantag = question_has_capability_on($question, 'tag'); - $mform = new \core_question\form\tags(null, null, 'post', '', null, $cantag, $toform); - $mform->set_data($toform); + $mform = new \core_question\form\tags(null, $formoptions, 'post', '', null, $cantag, $data); + $mform->set_data($data); return $mform->render(); } diff --git a/question/type/tags_form.php b/question/type/tags_form.php index a3af411f8dc..bcc2fc502a0 100644 --- a/question/type/tags_form.php +++ b/question/type/tags_form.php @@ -41,6 +41,8 @@ class tags extends \moodleform { */ public function definition() { $mform = $this->_form; + $customdata = $this->_customdata; + $mform->disable_form_change_checker(); $mform->addElement('hidden', 'id'); @@ -56,7 +58,29 @@ class tags extends \moodleform { $mform->addElement('static', 'questioncategory', get_string('categorycurrent', 'question')); $mform->addElement('static', 'context', ''); - $mform->addElement('tags', 'tags', get_string('tags'), - ['itemtype' => 'question', 'component' => 'core_question']); + if (\core_tag_tag::is_enabled('core_question', 'question')) { + $mform->addElement('tags', 'tags', get_string('tags'), + ['itemtype' => 'question', 'component' => 'core_question']); + + // Is the question category in a course context? + $qcontext = $customdata['questioncontext']; + $qcoursecontext = $qcontext->get_course_context(false); + $iscourseoractivityquestion = !empty($qcoursecontext); + // Is the current context we're editing in a course context? + $editingcontext = $customdata['editingcontext']; + $editingcoursecontext = $editingcontext->get_course_context(false); + $iseditingcontextcourseoractivity = !empty($editingcoursecontext); + + if ($iseditingcontextcourseoractivity && !$iscourseoractivityquestion) { + // If the question is being edited in a course or activity context + // and the question isn't a course or activity level question then + // allow course tags to be added to the course. + $coursetagheader = get_string('questionformtagheader', 'core_question', + $editingcoursecontext->get_context_name(true)); + $mform->addElement('tags', 'coursetags', $coursetagheader, + array('itemtype' => 'question', 'component' => 'core_question')); + + } + } } } -- 2.43.0