diff --git a/lang/en/studentquiz.php b/lang/en/studentquiz.php index 4a9949d9..061ddaff 100644 --- a/lang/en/studentquiz.php +++ b/lang/en/studentquiz.php @@ -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'; diff --git a/renderer.php b/renderer.php index 5051edd0..4bc0a82b 100644 --- a/renderer.php +++ b/renderer.php @@ -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'), @@ -1875,10 +1875,10 @@ 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 = [ @@ -1886,6 +1886,7 @@ public function state_history_table($studentquizquestionid): string { 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')) { @@ -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 @@ -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; } /** diff --git a/tests/behat/comment_area_create.feature b/tests/behat/comment_area_create.feature index 5e3de029..f566d666 100644 --- a/tests/behat/comment_area_create.feature +++ b/tests/behat/comment_area_create.feature @@ -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"