Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wip642149 #440

Merged
merged 2 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions classes/question/bank/studentquiz_bank_view.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,10 @@ public function get_questions() {
* Override base default sort
*/
protected function default_sort(): array {
return array();
return [
'mod_studentquiz\bank\anonym_creator_name_column-timecreated' => -1,
'mod_studentquiz\bank\question_name_column' => 1,
];
}

/**
Expand Down Expand Up @@ -288,15 +291,6 @@ protected function build_query(): void {
$sorts[] = $this->requiredcolumns[$colname]->sort_expression($order < 0, $subsort);
}

// Default sorting.
if (empty($sorts)) {
$sorts[] = 'q.timecreated DESC,q.id ASC';
}

if (isset($CFG->questionbankcolumns)) {
array_unshift($sorts, 'sqq.pinned DESC');
}

// Build the where clause and load params from search conditions.
foreach ($this->searchconditions as $searchcondition) {
if (!empty($searchcondition->where())) {
Expand All @@ -306,6 +300,7 @@ protected function build_query(): void {
$params = array_merge($params, $searchcondition->params());
}
}
array_unshift($sorts, 'sqq.pinned DESC');

// Build the complete SQL query.
$sql = ' FROM {question} q ' . implode(' ', $joins);
Expand Down Expand Up @@ -350,7 +345,7 @@ public function create_new_question_form($categoryid, $canadd): void {
$qtypecontainer = \html_writer::div(
\qbank_editquestion\editquestion_helper::print_choose_qtype_to_add_form(array(), $allowedtypes, true
), '', array('id' => 'qtypechoicecontainer'));
$questionsubmissionbutton = new \single_button($url, $caption, 'get', true);
$questionsubmissionbutton = new \single_button($url, $caption, 'get', 'primary');

list($message, $questionsubmissionallow) = mod_studentquiz_check_availability($this->studentquiz->opensubmissionfrom,
$this->studentquiz->closesubmissionfrom, 'submission');
Expand Down
6 changes: 3 additions & 3 deletions classes/task/delete_orphaned_questions.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public function execute() {
JOIN {question_bank_entries} qbe ON qr.questionbankentryid = qbe.id
JOIN {question_versions} qv ON qv.questionbankentryid = qr.questionbankentryid
JOIN {question} q ON qv.questionid = q.id
WHERE sq.state = 0 AND :timelimit - q.timemodified > 0
ORDER BY q.questionid ASC",
WHERE sqq.state = 0 AND :timelimit - q.timemodified > 0
ORDER BY q.id ASC",
['timelimit' => $timelimit]);

// Process questionids and generate output.
Expand All @@ -84,7 +84,7 @@ public function execute() {
$a = [
'name' => format_string($question->name),
'qtype' => format_string($question->qtype),
'questionid' => format_string($question->questionid),
'questionid' => format_string($question->id),
];

$output .= get_string('deleteorphanedquestionsquestioninfo', 'mod_studentquiz', $a);
Expand Down
18 changes: 18 additions & 0 deletions classes/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -779,4 +779,22 @@ public static function require_access_to_a_relevant_group(object $cm, \context $
exit();
}
}

/**
* Saves question rating.
*
* @param \stdClass $data requires userid, studentquizquestionid, rate
*/
public static function save_rate(\stdClass $data): void {
global $DB;

$row = $DB->get_record('studentquiz_rate', ['userid' => $data->userid,
'studentquizquestionid' => $data->studentquizquestionid]);
if ($row === false) {
$DB->insert_record('studentquiz_rate', $data);
} else {
$data->id = $row->id;
$DB->update_record('studentquiz_rate', $data);
}
}
}
18 changes: 0 additions & 18 deletions locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -865,24 +865,6 @@ function mod_studentquiz_check_availability($openform, $closefrom, $type) {
return [$message, $availabilityallow];
}

/**
* Saves question rating.
*
* @param stdClass $data requires userid, questionid, rate
*/
function mod_studentquiz_save_rate($data) {
global $DB;

$row = $DB->get_record('studentquiz_rate', ['userid' => $data->userid,
'studentquizquestionid' => $data->studentquizquestionid]);
if ($row === false) {
$DB->insert_record('studentquiz_rate', $data);
} else {
$data->id = $row->id;
$DB->update_record('studentquiz_rate', $data);
}
}

/**
* Compare and create new record for studentquiz_questions table if needed.
*
Expand Down
8 changes: 4 additions & 4 deletions renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function render_error_message(string $errormessage, string $title) : void
$courseurl = new moodle_url('/course/view.php', ['id' => $this->page->course->id]);

$backtocourse = new single_button($courseurl, get_string('back_to_course_button', 'studentquiz'),
'get', true);
'get', 'primary');
echo html_writer::div($this->render($backtocourse), 'studentquizerrormessage');
echo $this->output->footer();
}
Expand Down Expand Up @@ -1508,11 +1508,11 @@ public function render_question_names(array $questions): string {
public function render_change_state_dialog($message, $continue, $cancel) {
if ($continue instanceof single_button) {
// Ok.
$continue->primary = true;
$continue->type = 'primary';
} else if (is_string($continue)) {
$continue = new single_button(new moodle_url($continue), get_string('continue'), 'get', true);
$continue = new single_button(new moodle_url($continue), get_string('continue'), 'get', 'primary');
} else if ($continue instanceof moodle_url) {
$continue = new single_button($continue, get_string('continue'), 'get', true);
$continue = new single_button($continue, get_string('continue'), 'get', 'primary');
} else {
throw new coding_exception('The continue param to $OUTPUT->confirm() must be either a URL (string/moodle_url)' .
'or a single_button instance.');
Expand Down
2 changes: 1 addition & 1 deletion save.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
throw new moodle_exception("invalidrate");
}

mod_studentquiz_save_rate($data);
mod_studentquiz\utils::save_rate($data);
break;
}

Expand Down
38 changes: 8 additions & 30 deletions tests/behat/pagination.feature
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,21 @@ Feature: Test pagination for StudentQuiz
And the following "activities" exist:
| activity | name | intro | course | idnumber | publishnewquestion |
| studentquiz | StudentQuiz 1 | Quiz 1 description | C1 | studentquiz1 | 1 |
And the following "questions" exist:
| questioncategory | qtype | name | questiontext |
| Default for StudentQuiz 1 | essay | Test question 1 | Write about whatever you want |
| Default for StudentQuiz 1 | essay | Test question 2 | Write about whatever you want |
| Default for StudentQuiz 1 | essay | Test question 3 | Write about whatever you want |
| Default for StudentQuiz 1 | essay | Test question 4 | Write about whatever you want |
| Default for StudentQuiz 1 | essay | Test question 5 | Write about whatever you want |
| Default for StudentQuiz 1 | essay | Test question 6 | Write about whatever you want |
| Default for StudentQuiz 1 | essay | Test question 7 | Write about whatever you want |
| Default for StudentQuiz 1 | essay | Test question 8 | Write about whatever you want |
| Default for StudentQuiz 1 | essay | Test question 9 | Write about whatever you want |
| Default for StudentQuiz 1 | essay | Test question 10 | Write about whatever you want |
| Default for StudentQuiz 1 | essay | Test question 11 | Write about whatever you want |
| Default for StudentQuiz 1 | essay | Test question 12 | Write about whatever you want |
| Default for StudentQuiz 1 | essay | Test question 13 | Write about whatever you want |
| Default for StudentQuiz 1 | essay | Test question 14 | Write about whatever you want |
| Default for StudentQuiz 1 | essay | Test question 15 | Write about whatever you want |
| Default for StudentQuiz 1 | essay | Test question 16 | Write about whatever you want |
| Default for StudentQuiz 1 | essay | Test question 17 | Write about whatever you want |
| Default for StudentQuiz 1 | essay | Test question 18 | Write about whatever you want |
| Default for StudentQuiz 1 | essay | Test question 19 | Write about whatever you want |
| Default for StudentQuiz 1 | essay | Test question 20 | Write about whatever you want |
| Default for StudentQuiz 1 | essay | Test question 21 | Write about whatever you want |
| Default for StudentQuiz 1 | essay | Test question 22 | Write about whatever you want |
| Default for StudentQuiz 1 | essay | Test question 23 | Write about whatever you want |
| Default for StudentQuiz 1 | essay | Test question 24 | Write about whatever you want |
And 24 "questions" exist with the following data:
| questioncategory | Default for StudentQuiz 1 |
| qtype | essay |
| name | Test question [count] |
| questiontext | Write about whatever you want |

@javascript
Scenario: Users can change the state right multi-question has been chosen after paging.
Given I am on the "StudentQuiz 1" "mod_studentquiz > View" page logged in as "admin"
And I should see "" in the "Test question 1" "table_row"
And I set the field "qperpage" to "4"
And I press enter
And I click on "Question is new. Click here to change the state of this question" "link" in the "Test question 2" "table_row"
And I should see "Test question 2"
And I should not see "Test question 1"
And I should not see "Test question 3"
And I click on "Question is new. Click here to change the state of this question" "link" in the "Test question 11" "table_row"
And I should see "Test question 11"
And I should not see "Test question 21"

@javascript
Scenario: Users edit question should keep the same pagination.
Expand Down
2 changes: 2 additions & 0 deletions tests/behat/state_visibility.feature
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ Feature: Question states and visibility
And I should see "Unpin question" action for "TF 01" in the question bank
And I should not see "Unpin question" action for "TF 02" in the question bank
And "Pinned" "icon" should exist in the "TF 01" "table_row"
And "Pinned" "icon" should exist in the "#categoryquestions tr.r0" "css_element"
And "Pinned" "icon" should not exist in the "TF 02" "table_row"
And I log out
And I log in as "student1"
Expand All @@ -232,6 +233,7 @@ Feature: Question states and visibility
And I should not see "Pin question" action for "TF 01" in the question bank
And I should not see "Pin question" action for "TF 02" in the question bank
And "Pinned" "icon" should exist in the "TF 01" "table_row"
And "Pinned" "icon" should exist in the "#categoryquestions tr.r0" "css_element"
And "Pinned" "icon" should not exist in the "TF 02" "table_row"

@javascript @_switch_window
Expand Down
47 changes: 47 additions & 0 deletions tests/cron_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
namespace mod_studentquiz;

use mod_studentquiz\local\studentquiz_question;
use mod_studentquiz\local\studentquiz_helper;

/**
* Cron test.
Expand Down Expand Up @@ -104,6 +105,18 @@ protected function setUp(): void {
$this->questions[1] = \question_bank::load_question($this->questions[1]->id);
$this->studentquizquestions[0] = studentquiz_question::get_studentquiz_question_from_question($this->questions[0]);
$this->studentquizquestions[1] = studentquiz_question::get_studentquiz_question_from_question($this->questions[1]);
// Prepare comment.
$commentrecord = new \stdClass();
$commentrecord->studentquizquestionid = $this->studentquizquestions[0]->id;
$commentrecord->userid = $this->student1->id;
$this->getDataGenerator()->get_plugin_generator('mod_studentquiz')->create_comment($commentrecord);

// Prepare rate.
$raterecord = new \stdClass();
$raterecord->rate = 5;
$raterecord->studentquizquestionid = $this->studentquizquestions[0]->id;
$raterecord->userid = $this->student1->id;
\mod_studentquiz\utils::save_rate($raterecord);
}

/**
Expand Down Expand Up @@ -251,4 +264,38 @@ public function test_mod_studentquiz_prepare_notify_data(): void {
$this->assertEquals($anonstudent, $notifydata->actorname);

}

/**
* Test delete_orphaned_questions
*
* @covers \mod_studentquiz\task\delete_orphaned_questions
*/
public function test_delete_orphaned_questions(): void {
global $DB;
set_config('deleteorphanedquestions', true, 'studentquiz');
set_config('deleteorphanedtimelimit', 30, 'studentquiz');

// Change the question to disapprove.
$this->studentquizquestions[0]->change_state_visibility('state', studentquiz_helper::STATE_DISAPPROVED);

// Make sure modified time lower than time limit.
$updatedquestion = new \stdClass();
$updatedquestion->id = $this->questions[0]->id;
$updatedquestion->timemodified = $this->questions[0]->timemodified - 31;
$DB->update_record('question', $updatedquestion);

// Execute the cron.
cron_setup_user();
$cron = new task\delete_orphaned_questions();
$cron->set_component('mod_studentquiz');
$cron->execute();

$this->assertEquals(0, $DB->count_records('question', ['id' => $this->questions[0]->id]));
$this->assertEquals(0, $DB->count_records('studentquiz_rate',
['studentquizquestionid' => $this->studentquizquestions[0]->id]));
$this->assertEquals(0, $DB->count_records('studentquiz_comment',
['studentquizquestionid' => $this->studentquizquestions[0]->id]));
$this->assertEquals(0, $DB->count_records('studentquiz_question',
['id' => $this->studentquizquestions[0]->id]));
}
}
9 changes: 4 additions & 5 deletions tests/generator_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ public function test_create_comment() {

/**
* Test create rate
* @coversNothing
*
* @covers \mod_studentquiz\utils::save_rate
*/
public function test_create_rate() {
global $DB;
Expand All @@ -98,9 +99,7 @@ public function test_create_rate() {
$raterecord->rate = 5;
$raterecord->studentquizquestionid = $this->studentquizquestion->id;
$raterecord->userid = $user->id;

$rec = $this->studentquizgenerator->create_comment($raterecord);
$this->assertEquals($count + 1, $DB->count_records('studentquiz_comment'));
$this->assertEquals(5, $rec->rate);
\mod_studentquiz\utils::save_rate($raterecord);
$this->assertEquals($count + 1, $DB->count_records('studentquiz_rate'));
}
}