diff --git a/classes/question/bank/studentquiz_bank_view.php b/classes/question/bank/studentquiz_bank_view.php index 84076e1a..7cf80556 100755 --- a/classes/question/bank/studentquiz_bank_view.php +++ b/classes/question/bank/studentquiz_bank_view.php @@ -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, + ]; } /** @@ -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())) { @@ -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); @@ -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'); diff --git a/classes/task/delete_orphaned_questions.php b/classes/task/delete_orphaned_questions.php index c5d64478..091a91cc 100644 --- a/classes/task/delete_orphaned_questions.php +++ b/classes/task/delete_orphaned_questions.php @@ -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. @@ -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); diff --git a/classes/utils.php b/classes/utils.php index 92a500e4..302358bd 100644 --- a/classes/utils.php +++ b/classes/utils.php @@ -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); + } + } } diff --git a/locallib.php b/locallib.php index bcd19bec..7fa9414c 100644 --- a/locallib.php +++ b/locallib.php @@ -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. * diff --git a/renderer.php b/renderer.php index 9d9f0d8d..f06bea8e 100755 --- a/renderer.php +++ b/renderer.php @@ -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(); } @@ -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.'); diff --git a/save.php b/save.php index 88bcf097..18a6a30b 100644 --- a/save.php +++ b/save.php @@ -67,7 +67,7 @@ throw new moodle_exception("invalidrate"); } - mod_studentquiz_save_rate($data); + mod_studentquiz\utils::save_rate($data); break; } diff --git a/tests/behat/pagination.feature b/tests/behat/pagination.feature index ceef7a2e..8822dc21 100644 --- a/tests/behat/pagination.feature +++ b/tests/behat/pagination.feature @@ -14,32 +14,11 @@ 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. @@ -47,10 +26,9 @@ Feature: Test pagination for StudentQuiz 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. diff --git a/tests/behat/state_visibility.feature b/tests/behat/state_visibility.feature index af2c617a..34813781 100644 --- a/tests/behat/state_visibility.feature +++ b/tests/behat/state_visibility.feature @@ -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" @@ -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 diff --git a/tests/cron_test.php b/tests/cron_test.php index bea70d18..6496c41b 100644 --- a/tests/cron_test.php +++ b/tests/cron_test.php @@ -17,6 +17,7 @@ namespace mod_studentquiz; use mod_studentquiz\local\studentquiz_question; +use mod_studentquiz\local\studentquiz_helper; /** * Cron test. @@ -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); } /** @@ -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])); + } } diff --git a/tests/generator_test.php b/tests/generator_test.php index 677118eb..b4e49bba 100644 --- a/tests/generator_test.php +++ b/tests/generator_test.php @@ -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; @@ -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')); } }