Skip to content

Commit

Permalink
Enabled anonymous on state history table.
Browse files Browse the repository at this point in the history
  • Loading branch information
hieuvu authored and timhunt committed May 15, 2024
1 parent 301f52f commit 6b52880
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
1 change: 1 addition & 0 deletions lang/en/studentquiz.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
$string['after_submission_end_date'] = 'This StudentQuiz closed for question submission on {$a}.';
$string['answeringndbeforestart'] = 'Answering deadline can not be specified before the open for answering date';
$string['anonymous_user_name'] = 'Anonymous User #{$a}';
$string['anonymous_user'] = 'Anonymous User';
$string['api_state_change_success_content'] = 'Question state/visibility changed successfully';
$string['api_state_change_success_title'] = 'Success';
$string['api_state_change_error_title'] = 'Error deleting question';
Expand Down
24 changes: 17 additions & 7 deletions renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -1750,7 +1750,7 @@ public function render_comment_nav_tabs($studentquizquestion, $userid, $highligh

if (utils::can_view_state_history($cm->id, $question)) {
$statehistoryrenderer = $this->page->get_renderer('mod_studentquiz', 'state_history');
$statehistorytab = $statehistoryrenderer->state_history_table($studentquizquestion->get_id());
$statehistorytab = $statehistoryrenderer->state_history_table($studentquizquestion);
$tabs[] = [
'id' => 'state_history-tab',
'name' => get_string('history', 'mod_studentquiz'),
Expand Down Expand Up @@ -1875,17 +1875,18 @@ class mod_studentquiz_state_history_renderer extends mod_studentquiz_renderer {
/**
* Render state history table.
*
* @param int $studentquizquestionid studentquizquestion id.
* @param studentquiz_question $studentquizquestion studentquiz question object.
* @return string The content render.
*/
public function state_history_table($studentquizquestionid): string {
public function state_history_table(studentquiz_question $studentquizquestion): string {

$table = new html_table();
$table->head = [
get_string('time'),
get_string('action', 'question'),
];

$studentquizquestionid = $studentquizquestion->get_id();
list($statehistories, $users) = utils::get_state_history_data($studentquizquestionid);

if (get_string_manager()->string_exists('strftimedatetimeshortaccurate', 'core_langconfig')) {
Expand All @@ -1894,8 +1895,11 @@ public function state_history_table($studentquizquestionid): string {
$formatdate = get_string('strftimedatetimeshort', 'core_langconfig');
}

$commentarea = new container($studentquizquestion);
$canviewusername = $commentarea->can_view_username();
foreach ($statehistories as $statehistory) {
$author = !empty($users[$statehistory->userid]) ? $this->action_author($users[$statehistory->userid]) : '-';
$author = !empty($users[$statehistory->userid]) ?
$this->action_author($users[$statehistory->userid], $canviewusername) : '-';
$table->data[] = [
userdate($statehistory->timecreated, $formatdate),
$this->get_desc_action($statehistory->state) . ' ' . $author
Expand All @@ -1910,15 +1914,21 @@ public function state_history_table($studentquizquestionid): string {
* Action author's profile link.
*
* @param stdClass $user The user object.
* @param bool $canviewusername Whether user have permission to view other user name.
* @return string The link to user's profile.
*/
public function action_author(\stdClass $user): string {
public function action_author(\stdClass $user, bool $canviewusername): string {
global $USER;
if ($user->deleted) {
return html_writer::div(get_string('deleteduser', 'mod_forum'));
}

return html_writer::link(new moodle_url('/user/view.php', ['id' => $user->id, 'course' => $this->page->course->id]),
$username = html_writer::link(new moodle_url('/user/view.php',
['id' => $user->id, 'course' => $this->page->course->id]),
fullname($user), ['class' => 'd-table-cell']);
if (!$canviewusername && $user->id !== $USER->id) {
$username = get_string('anonymous_user', 'mod_studentquiz');
}
return $username;
}

/**
Expand Down
21 changes: 21 additions & 0 deletions tests/behat/comment_area_create.feature
Original file line number Diff line number Diff line change
Expand Up @@ -565,3 +565,24 @@ Feature: Create comment as an user
And I wait until ".studentquiz-comment-item:nth-child(1)" "css_element" exists
Then I should see "Comment 1" in the ".studentquiz-comment-item:nth-child(1) .studentquiz-comment-text" "css_element"
And I should not see "Edit" in the ".studentquiz-comment-item:nth-child(1) .studentquiz-comment-commands-box" "css_element"

@javascript
Scenario: Test show anonymous user on state history table.
Given I am on the "StudentQuiz 3" "mod_studentquiz > View" page logged in as "student1"
And I click on "Create new question" "button"
And I set the field "item_qtype_truefalse" to "1"
And I click on "Add" "button" in the "Choose a question type to add" "dialogue"
And I set the field "Question name" to "Question of Student 1"
And I set the field "Question text" to "The correct answer is true"
And I press "id_submitbutton"
And I am on the "StudentQuiz 3" "mod_studentquiz > View" page logged in as "teacher"
And I choose "Preview" action for "Question of Student 1" in the question bank
And I switch to "questionpreview" window
And I set the field "statetype" to "Approved"
And I click on "Change state" "button"
And I press "Close"
And I am on the "StudentQuiz 3" "mod_studentquiz > View" page logged in as "student1"
And I choose "Preview" action for "Question of Student 1" in the question bank
And I switch to "questionpreview" window
When I click on "History" "link"
Then I should see "Question set to 'Approved' Anonymous User"

0 comments on commit 6b52880

Please sign in to comment.