Skip to content

Commit

Permalink
Merge branch 'MDL-67942-master' of https://github.com/HuongNV13/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
stronk7 committed Feb 19, 2020
2 parents 2b97ab8 + fbad7a5 commit 287c044
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mod/quiz/report/attemptsreport.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ protected function delete_selected_attempts($quiz, $cm, $attemptids, \core\dml\s
WHERE {$allowedjoins->wheres} AND quiza.id = :attemptid";
}
$params = $allowedjoins->params + array('attemptid' => $attemptid);
$attempt = $DB->get_record_sql($sql, $params);
$attempt = $DB->get_record_sql($sql, $params, IGNORE_MULTIPLE);
if (!$attempt || $attempt->quiz != $quiz->id || $attempt->preview != 0) {
// Ensure the attempt exists, belongs to this quiz and belongs to
// a student included in the report. If not skip.
Expand Down
59 changes: 59 additions & 0 deletions mod/quiz/report/overview/tests/helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Makes some protected methods of quiz_attempts_report public to facilitate testing.
*
* @package quiz_overview
* @copyright 2020 Huong Nguyen <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

global $CFG;
require_once($CFG->dirroot . '/mod/quiz/report/attemptsreport.php');

/**
* Makes some protected methods of quiz_attempts_report public to facilitate testing.
*
* @copyright 2020 Huong Nguyen <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class testable_quiz_attempts_report extends quiz_attempts_report {

/**
* Override this function to displays the report.
* @param object $cm the course-module for this quiz.
* @param object $course the course we are in.
* @param object $quiz this quiz.
*/
public function display($cm, $course, $quiz) {

}

/**
* Testable delete_selected_attempts function.
*
* @param object $quiz
* @param object $cm
* @param array $attemptids
* @param \core\dml\sql_join $allowedjoins
*/
public function delete_selected_attempts($quiz, $cm, $attemptids, \core\dml\sql_join $allowedjoins) {
parent::delete_selected_attempts($quiz, $cm, $attemptids, $allowedjoins);
}
}
51 changes: 51 additions & 0 deletions mod/quiz/report/overview/tests/report_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
require_once($CFG->dirroot . '/mod/quiz/report/reportlib.php');
require_once($CFG->dirroot . '/mod/quiz/report/default.php');
require_once($CFG->dirroot . '/mod/quiz/report/overview/report.php');
require_once($CFG->dirroot . '/mod/quiz/report/overview/tests/helpers.php');


/**
Expand Down Expand Up @@ -263,4 +264,54 @@ public function test_get_bands_count_and_width($grade, $expected) {
$this->assertEquals($expected, quiz_overview_report::get_bands_count_and_width($quiz));
}

/**
* Test delete_selected_attempts function.
*/
public function test_delete_selected_attempts() {
$this->resetAfterTest(true);

$timestamp = 1234567890;
$timestart = $timestamp + 3600;

// Create a course and a quiz.
$generator = $this->getDataGenerator();
$course = $generator->create_course();
$quizgenerator = $generator->get_plugin_generator('mod_quiz');
$quiz = $quizgenerator->create_instance([
'course' => $course->id,
'grademethod' => QUIZ_GRADEHIGHEST,
'grade' => 100.0,
'sumgrades' => 10.0,
'attempts' => 10
]);

// Add one question.
$questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
$cat = $questiongenerator->create_question_category();
$q = $questiongenerator->create_question('essay', 'plain', ['category' => $cat->id]);
quiz_add_quiz_question($q->id, $quiz, 0 , 10);

// Create student and enrol them in the course.
// Note: we create two enrolments, to test the problem reported in MDL-67942.
$student = $generator->create_user();
$generator->enrol_user($student->id, $course->id);
$generator->enrol_user($student->id, $course->id, null, 'self');

$context = context_module::instance($quiz->cmid);
$cm = get_coursemodule_from_id('quiz', $quiz->cmid);
$allowedjoins = get_enrolled_with_capabilities_join($context, '', ['mod/quiz:attempt', 'mod/quiz:reviewmyattempts']);
$quizattemptsreport = new testable_quiz_attempts_report();

// Create the new attempt and initialize the question sessions.
$quizobj = quiz::create($quiz->id, $student->id);
$quba = question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj->get_context());
$quba->set_preferred_behaviour($quizobj->get_quiz()->preferredbehaviour);
$attempt = quiz_create_attempt($quizobj, 1, null, $timestart, false, $student->id);
$attempt = quiz_start_new_attempt($quizobj, $quba, $attempt, 1, $timestamp);
$attempt = quiz_attempt_save_started($quizobj, $quba, $attempt);

// Delete the student's attempt.
$quizattemptsreport->delete_selected_attempts($quiz, $cm, [$attempt->id], $allowedjoins);
}

}

0 comments on commit 287c044

Please sign in to comment.