Skip to content

Commit

Permalink
Merge branch 'NashTechOpenUniversity-wip436376'
Browse files Browse the repository at this point in the history
  • Loading branch information
dionysius committed Jul 11, 2021
2 parents 6f6fb40 + 89d3d0c commit e18386d
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 92 deletions.
52 changes: 2 additions & 50 deletions classes/commentarea/container.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class container {
private $studentquiz;

/** @var string - Basic order to get comments. */
private $basicorder = 'c.created ASC';
private $basicorder = 'c.created DESC';

/** @var object|stdClass - Current user of Moodle. Only call it once when __construct */
private $user;
Expand Down Expand Up @@ -372,20 +372,7 @@ public function query_comments($numbertoshow, $params) {
$join = 'JOIN {user} u ON u.id = c.userid';
}

$userpreferencesort = $this->get_sort();

// If have limit, always get latest.
if ($haslimit) {
$order = 'c.created DESC';
if ($this->is_user_table_sort()) {
$order = $userpreferencesort . ', ' . $order;
}
} else {
$order = $this->get_sort();
if ($this->is_user_table_sort()) {
$order = $userpreferencesort . ', ' . $this->basicorder;
}
}
$order = $this->get_sort() . ', ' . $this->basicorder;

// Build a where string a = :a AND b = :b.
$where = '';
Expand All @@ -411,9 +398,6 @@ public function query_comments($numbertoshow, $params) {

$data = [];
if (!empty($roots)) {
if ($haslimit) {
$roots = $this->resort($roots);
}
list($ids, $listids) = $DB->get_in_or_equal(array_column($roots, 'id'));
$query = "SELECT *
FROM {studentquiz_comment}
Expand Down Expand Up @@ -857,38 +841,6 @@ public function get_sort_select() {
return $data;
}

/**
* Re-sort data when get limit (limit always get latest).
*
* @param array $data
* @return array
*/
private function resort($data) {
// If sort by date desc, do not need re-sort.
if ($this->sortfeature === self::SORT_DATE_DESC) {
return $data;
}
// If sort by user name, keep name as it is. But sort time created DESC => ASC.
if ($this->is_user_table_sort()) {
$orders = [];
foreach ($data as $k => $v) {
$orders[$v->userid][] = $k;
}
foreach ($orders as $k => $v) {
$orders[$k] = array_reverse($v);
}
$res = [];
foreach ($orders as $v) {
foreach ($v as $commentid) {
$res["$commentid"] = $data[$commentid];
}
}
return $res;
}
// Otherwise just reverse data.
return array_reverse($data, true);
}

/**
* Get comment history by given comment id
*
Expand Down
68 changes: 44 additions & 24 deletions tests/behat/comment_area_create.feature
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Feature: Create comment as an user
| student3 | Chris | Bron | student3@example.com |
| student4 | Danny | Civi | student4@example.com |
| student5 | Bob | Alex | student5@example.com |
| student6 | James | Potter | student6@example.com |
And the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
Expand All @@ -26,6 +27,7 @@ Feature: Create comment as an user
| student3 | C1 | student |
| student4 | C1 | student |
| student5 | C1 | student |
| student6 | C1 | student |
And the following "activities" exist:
| activity | name | intro | course | idnumber | forcecommenting | publishnewquestion | anonymrank |
| studentquiz | StudentQuiz 1 | Quiz 1 description | C1 | studentquiz1 | 1 | 1 | 0 |
Expand Down Expand Up @@ -95,12 +97,11 @@ Feature: Create comment as an user
Then I should see "Expand all comments"
And I should not see "Collapse all comments"
And I should see "5 of 6" in the ".studentquiz-comment-postcount" "css_element"
And I should not see "Comment 1" in the ".studentquiz-comment-item:nth-child(1) .studentquiz-comment-text" "css_element"
And I should see "Comment 2" in the ".studentquiz-comment-item:nth-child(1) .studentquiz-comment-text" "css_element"
And I should see "Comment 3" in the ".studentquiz-comment-item:nth-child(2) .studentquiz-comment-text" "css_element"
And I should see "Comment 4" in the ".studentquiz-comment-item:nth-child(3) .studentquiz-comment-text" "css_element"
And I should see "Comment 5" in the ".studentquiz-comment-item:nth-child(4) .studentquiz-comment-text" "css_element"
And I should see "Comment 6" in the ".studentquiz-comment-item:nth-child(5) .studentquiz-comment-text" "css_element"
And I should see "Comment 1" in the ".studentquiz-comment-item:nth-child(1) .studentquiz-comment-text" "css_element"
And I should see "Comment 2" in the ".studentquiz-comment-item:nth-child(2) .studentquiz-comment-text" "css_element"
And I should see "Comment 3" in the ".studentquiz-comment-item:nth-child(3) .studentquiz-comment-text" "css_element"
And I should see "Comment 4" in the ".studentquiz-comment-item:nth-child(4) .studentquiz-comment-text" "css_element"
And I should see "Comment 5" in the ".studentquiz-comment-item:nth-child(5) .studentquiz-comment-text" "css_element"
# Click "Expand all comments" button, check that all comments and replies is show.
When I press "Expand all comments"
And I wait until the page is ready
Expand Down Expand Up @@ -132,12 +133,11 @@ Feature: Create comment as an user
And I switch to "questionpreview" window
And I wait until the page is ready
# We only show max 5 latest comments.
Then I should not see "Comment 1" in the ".studentquiz-comment-item:nth-child(1) .studentquiz-comment-text" "css_element"
And I should see "Comment 2" in the ".studentquiz-comment-item:nth-child(1) .studentquiz-comment-text" "css_element"
And I should see "Comment 3" in the ".studentquiz-comment-item:nth-child(2) .studentquiz-comment-text" "css_element"
And I should see "Comment 4" in the ".studentquiz-comment-item:nth-child(3) .studentquiz-comment-text" "css_element"
And I should see "Comment 5" in the ".studentquiz-comment-item:nth-child(4) .studentquiz-comment-text" "css_element"
And I should see "Comment 6" in the ".studentquiz-comment-item:nth-child(5) .studentquiz-comment-text" "css_element"
Then I should see "Comment 1" in the ".studentquiz-comment-item:nth-child(1) .studentquiz-comment-text" "css_element"
And I should see "Comment 2" in the ".studentquiz-comment-item:nth-child(2) .studentquiz-comment-text" "css_element"
And I should see "Comment 3" in the ".studentquiz-comment-item:nth-child(3) .studentquiz-comment-text" "css_element"
And I should see "Comment 4" in the ".studentquiz-comment-item:nth-child(4) .studentquiz-comment-text" "css_element"
And I should see "Comment 5" in the ".studentquiz-comment-item:nth-child(5) .studentquiz-comment-text" "css_element"

@javascript
Scenario: Test reply comment.
Expand Down Expand Up @@ -344,6 +344,20 @@ Feature: Create comment as an user
And I press "Add comment"
And I wait until the page is ready
And I log out
# Student 6
Given I log in as "student6"
And I am on "Course 1" course homepage
And I follow "StudentQuiz 1"
When I click on "Start Quiz" "button"
Then I set the field "True" to "1"
And I press "Check"
And I wait until the page is ready
When I enter the text "Comment 6" into the "Add comment" editor
And I press "Add comment"
When I enter the text "Comment 7" into the "Add comment" editor
And I press "Add comment"
And I wait until the page is ready
And I log out
# Log in as admin
Given I log in as "admin"
And I am on "Course 1" course homepage
Expand All @@ -356,32 +370,36 @@ Feature: Create comment as an user
When I click on "Date" "link" in the ".studentquiz-comment-filters" "css_element"
# Prevent behat fails (even single run is fine).
And I wait until the page is ready
Then I should see "Comment 5" in the ".studentquiz-comment-item:nth-child(1) .studentquiz-comment-text" "css_element"
Then I should see "Comment 4" in the ".studentquiz-comment-item:nth-child(2) .studentquiz-comment-text" "css_element"
Then I should see "Comment 3" in the ".studentquiz-comment-item:nth-child(3) .studentquiz-comment-text" "css_element"
Then I should see "Comment 2" in the ".studentquiz-comment-item:nth-child(4) .studentquiz-comment-text" "css_element"
Then I should see "Comment 7" in the ".studentquiz-comment-item:nth-child(1) .studentquiz-comment-text" "css_element"
Then I should see "Comment 6" in the ".studentquiz-comment-item:nth-child(2) .studentquiz-comment-text" "css_element"
Then I should see "Comment 5" in the ".studentquiz-comment-item:nth-child(3) .studentquiz-comment-text" "css_element"
Then I should see "Comment 4" in the ".studentquiz-comment-item:nth-child(4) .studentquiz-comment-text" "css_element"
Then I should see "Comment 3" in the ".studentquiz-comment-item:nth-child(5) .studentquiz-comment-text" "css_element"
# Sort Date ASC.
When I click on "Date" "link" in the ".studentquiz-comment-filters" "css_element"
And I wait until the page is ready
Then I should see "Comment 2" in the ".studentquiz-comment-item:nth-child(1) .studentquiz-comment-text" "css_element"
And I should see "Comment 3" in the ".studentquiz-comment-item:nth-child(2) .studentquiz-comment-text" "css_element"
And I should see "Comment 4" in the ".studentquiz-comment-item:nth-child(3) .studentquiz-comment-text" "css_element"
And I should see "Comment 5" in the ".studentquiz-comment-item:nth-child(4) .studentquiz-comment-text" "css_element"
And I should see "Comment 6" in the ".studentquiz-comment-item:nth-child(5) .studentquiz-comment-text" "css_element"
# Sort first name ASC.
When I click on "Forename" "link" in the ".studentquiz-comment-filters" "css_element"
And I wait until the page is ready
Then I should see "Comment 2" in the ".studentquiz-comment-item:nth-child(1) .studentquiz-comment-text" "css_element"
And I should see "Comment 5" in the ".studentquiz-comment-item:nth-child(2) .studentquiz-comment-text" "css_element"
And I should see "Comment 3" in the ".studentquiz-comment-item:nth-child(3) .studentquiz-comment-text" "css_element"
And I should see "Comment 4" in the ".studentquiz-comment-item:nth-child(4) .studentquiz-comment-text" "css_element"
And I should see "Comment 7" in the ".studentquiz-comment-item:nth-child(5) .studentquiz-comment-text" "css_element"
# Sort first name DESC.
When I click on "Forename" "link" in the ".studentquiz-comment-filters" "css_element"
# Prevent behat fails (even single run is fine).
And I wait until the page is ready
Then I should see "Comment 4" in the ".studentquiz-comment-item:nth-child(1) .studentquiz-comment-text" "css_element"
And I should see "Comment 3" in the ".studentquiz-comment-item:nth-child(2) .studentquiz-comment-text" "css_element"
And I should see "Comment 5" in the ".studentquiz-comment-item:nth-child(3) .studentquiz-comment-text" "css_element"
And I should see "Comment 2" in the ".studentquiz-comment-item:nth-child(4) .studentquiz-comment-text" "css_element"
Then I should see "Comment 7" in the ".studentquiz-comment-item:nth-child(1) .studentquiz-comment-text" "css_element"
And I should see "Comment 6" in the ".studentquiz-comment-item:nth-child(2) .studentquiz-comment-text" "css_element"
And I should see "Comment 4" in the ".studentquiz-comment-item:nth-child(3) .studentquiz-comment-text" "css_element"
And I should see "Comment 3" in the ".studentquiz-comment-item:nth-child(4) .studentquiz-comment-text" "css_element"
And I should see "Comment 5" in the ".studentquiz-comment-item:nth-child(5) .studentquiz-comment-text" "css_element"
# Sort last name ASC.
When I click on "Surname" "link" in the ".studentquiz-comment-filters" "css_element"
# Prevent behat fails (even single run is fine).
Expand All @@ -390,14 +408,16 @@ Feature: Create comment as an user
And I should see "Comment 3" in the ".studentquiz-comment-item:nth-child(2) .studentquiz-comment-text" "css_element"
And I should see "Comment 4" in the ".studentquiz-comment-item:nth-child(3) .studentquiz-comment-text" "css_element"
And I should see "Comment 2" in the ".studentquiz-comment-item:nth-child(4) .studentquiz-comment-text" "css_element"
And I should see "Comment 7" in the ".studentquiz-comment-item:nth-child(5) .studentquiz-comment-text" "css_element"
# Sort last name DESC.
When I click on "Surname" "link" in the ".studentquiz-comment-filters" "css_element"
# Prevent behat fails (even single run is fine).
And I wait until the page is ready
Then I should see "Comment 2" in the ".studentquiz-comment-item:nth-child(1) .studentquiz-comment-text" "css_element"
And I should see "Comment 4" in the ".studentquiz-comment-item:nth-child(2) .studentquiz-comment-text" "css_element"
And I should see "Comment 3" in the ".studentquiz-comment-item:nth-child(3) .studentquiz-comment-text" "css_element"
And I should see "Comment 5" in the ".studentquiz-comment-item:nth-child(4) .studentquiz-comment-text" "css_element"
Then I should see "Comment 7" in the ".studentquiz-comment-item:nth-child(1) .studentquiz-comment-text" "css_element"
And I should see "Comment 6" in the ".studentquiz-comment-item:nth-child(2) .studentquiz-comment-text" "css_element"
And I should see "Comment 2" in the ".studentquiz-comment-item:nth-child(3) .studentquiz-comment-text" "css_element"
And I should see "Comment 4" in the ".studentquiz-comment-item:nth-child(4) .studentquiz-comment-text" "css_element"
And I should see "Comment 3" in the ".studentquiz-comment-item:nth-child(5) .studentquiz-comment-text" "css_element"
And I log out
# Check as student 1.
Given I log in as "student1"
Expand Down
50 changes: 32 additions & 18 deletions tests/studentquiz_comment_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ class mod_studentquiz_comment_testcase extends advanced_testcase {
'firstname' => 'Bob',
'lastname' => 'Alex'
],
[
'firstname' => 'Ely',
'lastname' => 'Potter'
],
[
'firstname' => 'Flashy',
'lastname' => 'Granger'
],
];

/**
Expand Down Expand Up @@ -334,56 +342,62 @@ public function test_sort_feature() {
// Test sort by date asc.
$commentarea = new mod_studentquiz\commentarea\container($this->studentquiz, $q1, $this->cm, $this->context, null,
$base::SORT_DATE_ASC);
$comments = $commentarea->fetch_all();
$comments = $commentarea->fetch_all(5);
$this->assertEquals($this->users[0]->id, $comments[0]->get_comment_data()->userid);
$this->assertEquals($this->users[1]->id, $comments[1]->get_comment_data()->userid);
$this->assertEquals($this->users[2]->id, $comments[2]->get_comment_data()->userid);
$this->assertEquals($this->users[3]->id, $comments[3]->get_comment_data()->userid);
$this->assertEquals($this->users[4]->id, $comments[4]->get_comment_data()->userid);

// Test sort by desc.
$commentarea = new mod_studentquiz\commentarea\container($this->studentquiz, $q1, $this->cm, $this->context, null,
$base::SORT_DATE_DESC);
$comments = $commentarea->fetch_all();
$this->assertEquals($this->users[0]->id, $comments[3]->get_comment_data()->userid);
$this->assertEquals($this->users[1]->id, $comments[2]->get_comment_data()->userid);
$this->assertEquals($this->users[2]->id, $comments[1]->get_comment_data()->userid);
$this->assertEquals($this->users[3]->id, $comments[0]->get_comment_data()->userid);
$comments = $commentarea->fetch_all(5);
$this->assertEquals($this->users[5]->id, $comments[0]->get_comment_data()->userid);
$this->assertEquals($this->users[4]->id, $comments[1]->get_comment_data()->userid);
$this->assertEquals($this->users[3]->id, $comments[2]->get_comment_data()->userid);
$this->assertEquals($this->users[2]->id, $comments[3]->get_comment_data()->userid);
$this->assertEquals($this->users[1]->id, $comments[4]->get_comment_data()->userid);

// Test sort by first name asc.
$commentarea = new mod_studentquiz\commentarea\container($this->studentquiz, $q1, $this->cm, $this->context, null,
$base::SORT_FIRSTNAME_ASC);
$comments = $commentarea->fetch_all();
$comments = $commentarea->fetch_all(5);
$this->assertEquals($this->users[0]->id, $comments[0]->get_comment_data()->userid);
$this->assertEquals($this->users[3]->id, $comments[1]->get_comment_data()->userid);
$this->assertEquals($this->users[1]->id, $comments[2]->get_comment_data()->userid);
$this->assertEquals($this->users[2]->id, $comments[3]->get_comment_data()->userid);
$this->assertEquals($this->users[4]->id, $comments[4]->get_comment_data()->userid);

// Test sort by first name desc.
$commentarea = new mod_studentquiz\commentarea\container($this->studentquiz, $q1, $this->cm, $this->context, null,
$base::SORT_FIRSTNAME_DESC);
$comments = $commentarea->fetch_all();
$this->assertEquals($this->users[2]->id, $comments[0]->get_comment_data()->userid);
$this->assertEquals($this->users[1]->id, $comments[1]->get_comment_data()->userid);
$this->assertEquals($this->users[3]->id, $comments[2]->get_comment_data()->userid);
$this->assertEquals($this->users[0]->id, $comments[3]->get_comment_data()->userid);
$comments = $commentarea->fetch_all(5);
$this->assertEquals($this->users[5]->id, $comments[0]->get_comment_data()->userid);
$this->assertEquals($this->users[4]->id, $comments[1]->get_comment_data()->userid);
$this->assertEquals($this->users[2]->id, $comments[2]->get_comment_data()->userid);
$this->assertEquals($this->users[1]->id, $comments[3]->get_comment_data()->userid);
$this->assertEquals($this->users[3]->id, $comments[4]->get_comment_data()->userid);

// Test sort by last name asc.
$commentarea = new mod_studentquiz\commentarea\container($this->studentquiz, $q1, $this->cm, $this->context, null,
$base::SORT_LASTNAME_ASC);
$comments = $commentarea->fetch_all();
$comments = $commentarea->fetch_all(5);
$this->assertEquals($this->users[3]->id, $comments[0]->get_comment_data()->userid);
$this->assertEquals($this->users[1]->id, $comments[1]->get_comment_data()->userid);
$this->assertEquals($this->users[2]->id, $comments[2]->get_comment_data()->userid);
$this->assertEquals($this->users[0]->id, $comments[3]->get_comment_data()->userid);
$this->assertEquals($this->users[5]->id, $comments[4]->get_comment_data()->userid);

// Test sort by last name desc.
$commentarea = new mod_studentquiz\commentarea\container($this->studentquiz, $q1, $this->cm, $this->context, null,
$base::SORT_LASTNAME_DESC);
$comments = $commentarea->fetch_all();
$this->assertEquals($this->users[0]->id, $comments[0]->get_comment_data()->userid);
$this->assertEquals($this->users[2]->id, $comments[1]->get_comment_data()->userid);
$this->assertEquals($this->users[1]->id, $comments[2]->get_comment_data()->userid);
$this->assertEquals($this->users[3]->id, $comments[3]->get_comment_data()->userid);
$comments = $commentarea->fetch_all(5);
$this->assertEquals($this->users[4]->id, $comments[0]->get_comment_data()->userid);
$this->assertEquals($this->users[5]->id, $comments[1]->get_comment_data()->userid);
$this->assertEquals($this->users[0]->id, $comments[2]->get_comment_data()->userid);
$this->assertEquals($this->users[2]->id, $comments[3]->get_comment_data()->userid);
$this->assertEquals($this->users[1]->id, $comments[4]->get_comment_data()->userid);
}

/**
Expand Down

0 comments on commit e18386d

Please sign in to comment.