mirrored from git://git.moodle.org/moodle.git
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MDL-74035 qbank_viewquestionname: In place editing
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
1 parent
720bd60
commit 8c13965
Showing
6 changed files
with
159 additions
and
6 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
question/bank/viewquestionname/classes/output/questionname.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
question/bank/viewquestionname/tests/behat/question_in_place_editing.feature
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters