Skip to content

Commit

Permalink
MDL-74035 qbank_viewquestionname: In place editing
Browse files Browse the repository at this point in the history
This commit will implement in place editing for the
qbank view where users with permission can edit the
title of the questions from the quesion bank view.
  • Loading branch information
safatshahin authored and andrewnicols committed Nov 4, 2022
1 parent 720bd60 commit 8c13965
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 6 deletions.
49 changes: 49 additions & 0 deletions question/bank/viewquestionname/classes/output/questionname.php
@@ -0,0 +1,49 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

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 <safatshahin@catalyst-au.net>
* @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';
}
}
Expand Up @@ -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.
Expand Down
Expand Up @@ -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}';
57 changes: 57 additions & 0 deletions question/bank/viewquestionname/lib.php
@@ -0,0 +1,57 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Callback and other methods for viewquestionname plugin.
*
* @package qbank_viewquestionname
* @copyright 2022 Catalyst IT Australia Pty Ltd
* @author Safat Shahin <safatshahin@catalyst-au.net>
* @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);
}
}
@@ -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
2 changes: 1 addition & 1 deletion question/bank/viewquestionname/version.php
Expand Up @@ -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;

0 comments on commit 8c13965

Please sign in to comment.