diff --git a/question/bank/viewquestionname/classes/output/questionname.php b/question/bank/viewquestionname/classes/output/questionname.php new file mode 100644 index 0000000000000..4444c809217fc --- /dev/null +++ b/question/bank/viewquestionname/classes/output/questionname.php @@ -0,0 +1,49 @@ +. + +namespace qbank_viewquestionname\output; + +use core\output\inplace_editable; +use core\output\named_templatable; +use renderable; + +/** + * Question in place editing api call. + * + * @package qbank_viewquestionname + * @copyright 2022 Catalyst IT Australia Pty Ltd + * @author Safat Shahin + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class questionname extends inplace_editable implements named_templatable, renderable { + public function __construct(\stdClass $question) { + parent::__construct( + 'qbank_viewquestionname', + 'questionname', + $question->id, + question_has_capability_on($question, 'edit'), + format_string($question->name), $question->name, + get_string('edit_question_name_hint', 'qbank_viewquestionname'), + get_string('edit_question_name_label', 'qbank_viewquestionname', (object) [ + 'name' => $question->name, + ]) + ); + } + + public function get_template_name(\renderer_base $renderer): string { + return 'core/inplace_editable'; + } +} diff --git a/question/bank/viewquestionname/classes/question_name_idnumber_tags_column.php b/question/bank/viewquestionname/classes/question_name_idnumber_tags_column.php index 6c76a2bff2b2b..354755bc30a6a 100644 --- a/question/bank/viewquestionname/classes/question_name_idnumber_tags_column.php +++ b/question/bank/viewquestionname/classes/question_name_idnumber_tags_column.php @@ -34,14 +34,16 @@ protected function display_content($question, $rowclasses): void { global $OUTPUT; echo \html_writer::start_tag('div', ['class' => 'd-inline-flex flex-nowrap overflow-hidden w-100']); - - $questionname = format_string($question->name); + $questiondisplay = $OUTPUT->render(new \qbank_viewquestionname\output\questionname($question)); $labelfor = $this->label_for($question); if ($labelfor) { - echo \html_writer::label($questionname, $labelfor); + echo \html_writer::tag('label', $questiondisplay, [ + 'for' => $labelfor, + ]); } else { - // Question name. - echo \html_writer::span($questionname, 'questionname flex-grow-1 flex-shrink-1 text-truncate'); + echo \html_writer::start_span('questionname flex-grow-1 flex-shrink-1 text-truncate'); + echo $questiondisplay; + echo \html_writer::end_span(); } // Question idnumber. diff --git a/question/bank/viewquestionname/lang/en/qbank_viewquestionname.php b/question/bank/viewquestionname/lang/en/qbank_viewquestionname.php index 7c537dc1409c5..8f7b22996dcd6 100644 --- a/question/bank/viewquestionname/lang/en/qbank_viewquestionname.php +++ b/question/bank/viewquestionname/lang/en/qbank_viewquestionname.php @@ -25,3 +25,6 @@ $string['pluginname'] = 'View question name'; $string['privacy:metadata'] = 'The View question name question bank plugin does not store any personal data.'; +// In place editing. +$string['edit_question_name_hint'] = 'Edit question name'; +$string['edit_question_name_label'] = 'New value for {$a->name}'; diff --git a/question/bank/viewquestionname/lib.php b/question/bank/viewquestionname/lib.php new file mode 100644 index 0000000000000..c1577ee58f13a --- /dev/null +++ b/question/bank/viewquestionname/lib.php @@ -0,0 +1,57 @@ +. + +/** + * Callback and other methods for viewquestionname plugin. + * + * @package qbank_viewquestionname + * @copyright 2022 Catalyst IT Australia Pty Ltd + * @author Safat Shahin + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * In place editing callback for question name. + * + * @param string $itemtype type of the item, questionname for this instance + * @param int $itemid question id to change the title + * @param string $newvalue the changed question title + * @return \core\output\inplace_editable + */ +function qbank_viewquestionname_inplace_editable ($itemtype, $itemid, $newvalue) : \core\output\inplace_editable { + if ($itemtype === 'questionname') { + global $CFG, $DB; + require_once($CFG->libdir . '/questionlib.php'); + // Get the question data and to confirm any invalud itemid is not passed. + $record = $DB->get_record('question', ['id' => $itemid], '*', MUST_EXIST); + // Load question data from question engine. + $question = question_bank::load_question($record->id); + // Context validation. + \external_api::validate_context(context::instance_by_id($question->contextid)); + + // Now update the question data. + $record->name = $newvalue; + $DB->update_record('question', $record); + + // Trigger events. + question_bank::notify_question_edited($record->id); + $event = \core\event\question_updated::create_from_question_instance($question); + $event->trigger(); + + // Prepare the element for the output. + return new \qbank_viewquestionname\output\questionname($record); + } +} diff --git a/question/bank/viewquestionname/tests/behat/question_in_place_editing.feature b/question/bank/viewquestionname/tests/behat/question_in_place_editing.feature new file mode 100644 index 0000000000000..279854ce37eb4 --- /dev/null +++ b/question/bank/viewquestionname/tests/behat/question_in_place_editing.feature @@ -0,0 +1,42 @@ +@qbank @qbank_viewquestionname @javascript +Feature: Use the qbank view page to edit question title using in place edit feature + + Background: + Given the following "users" exist: + | username | firstname | lastname | email | + | teacher1 | T1 | Teacher1 | teacher1@example.com | + And the following "courses" exist: + | fullname | shortname | category | + | Course 1 | C1 | 0 | + And the following "course enrolments" exist: + | user | course | role | + | teacher1 | C1 | editingteacher | + And the following "activities" exist: + | activity | name | course | idnumber | + | quiz | Test quiz | C1 | quiz1 | + And the following "question categories" exist: + | contextlevel | reference | name | + | Course | C1 | Test questions | + And the following "questions" exist: + | questioncategory | qtype | name | questiontext | + | Test questions | truefalse | First question | Answer the first question | + + @javascript + Scenario: Question title can be changed from the question bank view + Given I am on the "Test quiz" "mod_quiz > question bank" page logged in as "teacher1" + And I set the field "Select a category" to "Test questions" + When I set the field "Edit question name" in the "First question" "table_row" to "Edited question" + Then I should not see "First question" + And I should see "Edited question" + + @javascript + Scenario: Teacher without permission can not change the title from question bank view + Given I log in as "admin" + And I set the following system permissions of "Teacher" role: + | capability | permission | + | moodle/question:editall | Prevent | + And I log out + And I am on the "Test quiz" "mod_quiz > question bank" page logged in as "teacher1" + When I set the field "Select a category" to "Test questions" + And I should see "First question" + And "Edit question name" "field" should not exist diff --git a/question/bank/viewquestionname/version.php b/question/bank/viewquestionname/version.php index 0e0dce41bac5d..698200e859fa3 100644 --- a/question/bank/viewquestionname/version.php +++ b/question/bank/viewquestionname/version.php @@ -26,6 +26,6 @@ defined('MOODLE_INTERNAL') || die(); $plugin->component = 'qbank_viewquestionname'; -$plugin->version = 2022041900; +$plugin->version = 2022103100; $plugin->requires = 2022041200; $plugin->maturity = MATURITY_STABLE;