Skip to content

Commit

Permalink
EVOSTDM-3175 MDL 4.3 - Valider nos plugins - Partie 3 - Divers (parta…
Browse files Browse the repository at this point in the history
…gés)
  • Loading branch information
binhdv1983 committed Jan 25, 2024
1 parent 08ea4a0 commit 30b4f6a
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 71 deletions.
11 changes: 7 additions & 4 deletions classes/quiz_attemptreport.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@

namespace quiz_markspersection;

use mod_quiz\access_manager;
use mod_quiz\quiz_attempt;

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

global $CFG;
Expand All @@ -36,7 +39,7 @@
* @author Marie-Eve Lévesque <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class quiz_attemptreport extends \quiz_attempt {
class quiz_attemptreport extends quiz_attempt {

/** @var array Already calculated sections marks, stored in this variable for further use (to avoid recalculation). */
private $sectionmarks = null;
Expand Down Expand Up @@ -97,7 +100,7 @@ public function get_sectionid($slot) {
* @return quiz_attempt the new quiz_attempt object
*/
public static function create($attemptid) {
return self::create_helper(array('id' => $attemptid));
return self::create_helper(['id' => $attemptid]);
}

/**
Expand All @@ -110,8 +113,8 @@ protected static function create_helper($conditions) {
global $DB;

$attempt = $DB->get_record('quiz_attempts', $conditions, '*', MUST_EXIST);
$quiz = \quiz_access_manager::load_quiz_and_settings($attempt->quiz);
$course = $DB->get_record('course', array('id' => $quiz->course), '*', MUST_EXIST);
$quiz = access_manager::load_quiz_and_settings($attempt->quiz);
$course = $DB->get_record('course', ['id' => $quiz->course], '*', MUST_EXIST);
$cm = get_coursemodule_from_instance('quiz', $quiz->id, $course->id, false, MUST_EXIST);

// Update quiz with override information.
Expand Down
6 changes: 2 additions & 4 deletions markspersection_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

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

require_once($CFG->dirroot . '/mod/quiz/report/attemptsreport_form.php');
use mod_quiz\local\reports\attempts_report_options_form;

/**
* Quiz marks per section report settings form.
Expand All @@ -34,6 +32,6 @@
* @author Marie-Eve Lévesque <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class quiz_markspersection_settings_form extends mod_quiz_attempts_report_form {
class quiz_markspersection_settings_form extends attempts_report_options_form {

}
11 changes: 5 additions & 6 deletions markspersection_options.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

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

require_once($CFG->dirroot . '/mod/quiz/report/attemptsreport_options.php');
use mod_quiz\local\reports\attempts_report;
use mod_quiz\local\reports\attempts_report_options;

/**
* Class to store the options for a quiz_markspersection_report.
Expand All @@ -34,7 +33,7 @@
* @author Marie-Eve Lévesque <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class quiz_markspersection_options extends mod_quiz_attempts_report_options {
class quiz_markspersection_options extends attempts_report_options {

/** @var bool whether to show only attempt that need regrading.
* This variable is necessary to stay compatible with quiz_overview_options even if we do not extend it. */
Expand All @@ -53,7 +52,7 @@ public function resolve_dependencies() {
// We only want to show the checkbox to delete attempts
// if the user has permissions and if the report mode is showing attempts.
$this->checkboxcolumn = has_any_capability(
array('mod/quiz:deleteattempts'), context_module::instance($this->cm->id))
&& ($this->attempts != quiz_attempts_report::ENROLLED_WITHOUT);
['mod/quiz:deleteattempts'], context_module::instance($this->cm->id))
&& ($this->attempts != attempts_report::ENROLLED_WITHOUT);
}
}
29 changes: 14 additions & 15 deletions markspersection_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@
*/


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

require_once($CFG->dirroot . '/mod/quiz/report/attemptsreport_table.php');
use mod_quiz\local\reports\attempts_report_table;
use mod_quiz\quiz_settings;
use quiz_markspersection\quiz_attemptreport;

/**
Expand All @@ -39,10 +38,10 @@
class quiz_markspersection_table extends quiz_overview_table {

/** @var array Array of quiz_attemptreport (kept for further usage, to avoid unnecessary initialisations and calculations). */
private $attemptreports = array();
private $attemptreports = [];

/** @var array Array of attemps section marks. */
private $attemptsectionsmarks = array();
private $attemptsectionsmarks = [];

/** @var Array Array of attempts ids in the report. */
private $attemptsids = [];
Expand All @@ -61,7 +60,7 @@ class quiz_markspersection_table extends quiz_overview_table {
public function __construct($quiz, $context, $qmsubselect,
$options, \core\dml\sql_join $groupstudentsjoins,
\core\dml\sql_join $studentsjoins, $questions, $reporturl) {
quiz_attempts_report_table::__construct('mod-quiz-report-markspersection-report', $quiz , $context,
attempts_report_table::__construct('mod-quiz-report-markspersection-report', $quiz , $context,
$qmsubselect, $options, $groupstudentsjoins, $studentsjoins, $questions, $reporturl);
}

Expand Down Expand Up @@ -161,12 +160,12 @@ public function compute_average_row($label, \core\dml\sql_join $usersjoins) {
} else {
$namekey = 'fullname';
}
$averagerow = array(
$averagerow = [
$namekey => $label,
'sumgrades' => $this->format_average($record),
'feedbacktext' => strip_tags(quiz_report_feedback_for_grade(
$record->grade, $this->quiz->id, $this->context))
);
$record->grade, $this->quiz->id, $this->context)),
];
$qubaids = new qubaid_join("{quiz_attempts} quizaouter
JOIN (
SELECT DISTINCT quiza.id
Expand Down Expand Up @@ -201,7 +200,7 @@ public function compute_average_row($label, \core\dml\sql_join $usersjoins) {
* Redefine the parent function with only relevant information (quiz_overview_table has too many).
*/
protected function submit_buttons() {
quiz_attempts_report_table::submit_buttons();
attempts_report_table::submit_buttons();
}

/**
Expand Down Expand Up @@ -256,7 +255,7 @@ protected function add_latest_state_join($slot) {
// therefore, it is better to use a very simple join, which may include
// too many records, than to do a super-accurate join.
$qubaids = new qubaid_join("{quiz_attempts} {$alias}quiza", "{$alias}quiza.uniqueid",
"{$alias}quiza.quiz = :{$alias}quizid", array("{$alias}quizid" => $this->sql->params['quizid']));
"{$alias}quiza.quiz = :{$alias}quizid", ["{$alias}quizid" => $this->sql->params['quizid']]);

list($inlineview, $viewparams) = $this->question_attempt_latest_state_view($alias, $qubaids);

Expand Down Expand Up @@ -286,15 +285,15 @@ public function question_attempt_latest_state_view($alias, qubaid_condition $qub
$where = $qubaids->where() . " AND {$alias}qa.questionid IN($questionsidsstr)";
$whereparams = $qubaids->from_where_params();
// The name "marksection" givent to the SUM function is the one to reuse in get_required_latest_state_fields.
return array("(
return ["(
SELECT {$alias}qa.questionusageid,
SUM({$alias}qas.fraction * {$alias}qa.maxmark) AS marksection,
{$alias}qas.userid
FROM {$qubaids->from_question_attempts($alias . 'qa')}
JOIN {question_attempt_steps} {$alias}qas ON {$alias}qas.questionattemptid = {$alias}qa.id
WHERE {$where}
GROUP BY {$alias}qa.questionusageid, {$alias}qas.userid
) {$alias}", $whereparams);
) {$alias}", $whereparams];
}

/**
Expand All @@ -304,12 +303,12 @@ public function question_attempt_latest_state_view($alias, qubaid_condition $qub
*/
public function get_questions_in_section($sectionid) {
$cm = get_coursemodule_from_instance('quiz', $this->quiz->id, $this->quiz->course, false, MUST_EXIST);
$quizobj = new \quiz($this->quiz, $cm, $this->quiz->course);
$quizobj = new quiz_settings($this->quiz, $cm, $this->quiz->course);
$structure = $quizobj->get_structure();
$slots = $structure->get_slots_in_section($sectionid);

// Get all the questions in the slots of this section.
$questionsinsection = array();
$questionsinsection = [];
foreach ($this->questions as $question) {
if (in_array($question->slot, $slots)) {
$questionsinsection[] = $question->id;
Expand Down
12 changes: 7 additions & 5 deletions report.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
require_once($CFG->dirroot . '/mod/quiz/report/markspersection/markspersection_form.php');
require_once($CFG->dirroot . '/mod/quiz/report/markspersection/markspersection_options.php');
require_once($CFG->dirroot . '/mod/quiz/report/markspersection/markspersection_table.php');

use mod_quiz\quiz_settings;
use quiz_markspersection\quiz_attemptreport;

/**
Expand Down Expand Up @@ -70,7 +72,7 @@ public function display($quiz, $cm, $course) {

// Prepare for downloading, if applicable.
$courseshortname = format_string($course->shortname, true,
array('context' => context_course::instance($course->id)));
['context' => context_course::instance($course->id)]);
$table = new quiz_markspersection_table($quiz, $this->context, $this->qmsubselect,
$options, $groupstudentsjoins, $studentsjoins, $questions, $options->get_url());
$filename = quiz_report_download_filename(get_string('markspersectionfilename', 'quiz_markspersection'),
Expand Down Expand Up @@ -109,7 +111,7 @@ public function display($quiz, $cm, $course) {

$hasquestions = quiz_has_questions($quiz->id);

$quizobj = quiz::create($quiz->id);
$quizobj = quiz_settings::create($quiz->id);
$sections = $quizobj->get_sections();
if (count($sections) <= 1) {
$this->print_header_and_tabs($cm, $course, $quiz);
Expand Down Expand Up @@ -141,8 +143,8 @@ public function display($quiz, $cm, $course) {
}

// Define table columns.
$columns = array();
$headers = array();
$columns = [];
$headers = [];

if (!$table->is_downloading() && $options->checkboxcolumn) {
$columnname = 'checkbox';
Expand Down Expand Up @@ -207,7 +209,7 @@ protected function quiz_no_sections_message($cm, $context) {
$output = $OUTPUT->notification(get_string('nosections', 'quiz_markspersection'));
if (has_capability('mod/quiz:manage', $context)) {
$output .= $OUTPUT->single_button(new moodle_url('/mod/quiz/edit.php',
array('cmid' => $cm->id)), get_string('editquiz', 'quiz'), 'get');
['cmid' => $cm->id]), get_string('editquiz', 'quiz'), 'get');
}

return $output;
Expand Down
53 changes: 27 additions & 26 deletions tests/report_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@
defined('MOODLE_INTERNAL') || die();

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

use quiz_markspersection\quiz_attemptreport;
use mod_quiz\local\reports\attempts_report;
use mod_quiz\quiz_attempt;
use mod_quiz\quiz_settings;

/**
* Tests for the quiz marks per section report.
Expand All @@ -56,13 +57,13 @@ public function test_get_sections_marks() {
// Create 2 quizzes : one without sections and one with sections.
$quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');

$quiz1 = $quizgenerator->create_instance(array('course' => $course->id, 'questionsperpage' => 0,
'grade' => 100.0, 'sumgrades' => 2, 'preferredbehaviour' => 'immediatefeedback'));
$quiz2 = $quizgenerator->create_instance(array('course' => $course->id, 'questionsperpage' => 0,
'grade' => 100.0, 'sumgrades' => 2, 'preferredbehaviour' => 'immediatefeedback'));
$quiz1 = $quizgenerator->create_instance(['course' => $course->id, 'questionsperpage' => 0,
'grade' => 100.0, 'sumgrades' => 2, 'preferredbehaviour' => 'immediatefeedback']);
$quiz2 = $quizgenerator->create_instance(['course' => $course->id, 'questionsperpage' => 0,
'grade' => 100.0, 'sumgrades' => 2, 'preferredbehaviour' => 'immediatefeedback']);

$quizobj1 = \quiz::create($quiz1->id, $user->id);
$quizobj2 = \quiz::create($quiz2->id, $user->id);
$quizobj1 = quiz_settings::create($quiz1->id, $user->id);
$quizobj2 = quiz_settings::create($quiz2->id, $user->id);

$quba1 = \question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj1->get_context());
$quba1->set_preferred_behaviour($quizobj1->get_quiz()->preferredbehaviour);
Expand Down Expand Up @@ -105,7 +106,7 @@ public function test_get_sections_marks() {
// Case 1 : When there are no sections in the quiz.

// Submit answers to create attempt for user.
$attemptobj = $this->submitanswers($quizobj1, $quba1, $user, array(0, 0, 1, 0, 1, 1));
$attemptobj = $this->submitanswers($quizobj1, $quba1, $user, [0, 0, 1, 0, 1, 1]);

// Check the data for the report - there is at least one section.
$quizattemptsreport = quiz_attemptreport::create($attemptobj->get_attemptid());
Expand Down Expand Up @@ -134,7 +135,7 @@ public function test_get_sections_marks() {
$structure->add_section_heading(4, 'Section 4');

// Submit answers to create attempt for user.
$attemptobj = $this->submitanswers($quizobj2, $quba2, $user, array(0, 0, 1, 0, 1, 1, 'My essay.'));
$attemptobj = $this->submitanswers($quizobj2, $quba2, $user, [0, 0, 1, 0, 1, 1, 'My essay.']);

// Check the data for the report.
$quizattemptsreport = new \quiz_markspersection_report();
Expand Down Expand Up @@ -173,11 +174,11 @@ public function test_compute_average_row() {
// Create 2 quizzes : one without sections and one with sections.
$quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');

$quiz1 = $quizgenerator->create_instance(array('course' => $course->id, 'questionsperpage' => 0,
'grade' => 100.0, 'sumgrades' => 2, 'preferredbehaviour' => 'immediatefeedback'));
$quiz1 = $quizgenerator->create_instance(['course' => $course->id, 'questionsperpage' => 0,
'grade' => 100.0, 'sumgrades' => 2, 'preferredbehaviour' => 'immediatefeedback']);

$quizobj1 = \quiz::create($quiz1->id, $user->id);
$quizobj2 = \quiz::create($quiz1->id, $user2->id);
$quizobj1 = quiz_settings::create($quiz1->id, $user->id);
$quizobj2 = quiz_settings::create($quiz1->id, $user2->id);

$quba1 = \question_engine::make_questions_usage_by_activity('mod_quiz', $quizobj1->get_context());
$quba1->set_preferred_behaviour($quizobj1->get_quiz()->preferredbehaviour);
Expand Down Expand Up @@ -228,32 +229,32 @@ public function test_compute_average_row() {
$section4 = $structure->add_section_heading(4, 'Section 4');

// Submit answers to create attempt for user 1.
$this->submitanswers($quizobj1, $quba1, $user, array(0, 0, 1, 0, 1, 1, 'My essay.'));
$this->submitanswers($quizobj1, $quba1, $user, [0, 0, 1, 0, 1, 1, 'My essay.']);

// Submit answers to create attempt for user 2.
$this->submitanswers($quizobj2, $quba2, $user2, array(0, 1, 1));
$this->submitanswers($quizobj2, $quba2, $user2, [0, 1, 1]);

// Check the data for the report.
$context = \context_module::instance($cm1->id);
$cm = get_coursemodule_from_id('quiz', $cm1->id);
$qmsubselect = quiz_report_qm_filter_select($quiz1);
$studentsjoins = get_enrolled_with_capabilities_join($context, '',
array('mod/quiz:attempt', 'mod/quiz:reviewmyattempts'));
['mod/quiz:attempt', 'mod/quiz:reviewmyattempts']);
$empty = new \core\dml\sql_join();

// Set the options.
$reportoptions = new \quiz_markspersection_options('overview', $quiz1, $cm, null);
$reportoptions->attempts = \quiz_attempts_report::ENROLLED_ALL;
$reportoptions->attempts = attempts_report::ENROLLED_ALL;
$reportoptions->onlygraded = true;
$reportoptions->states = array(\quiz_attempt::IN_PROGRESS, \quiz_attempt::OVERDUE, \quiz_attempt::FINISHED);
$reportoptions->states = [quiz_attempt::IN_PROGRESS, quiz_attempt::OVERDUE, quiz_attempt::FINISHED];

// Load the required questions.
$questions = quiz_report_get_significant_questions($quiz1);

$table = new \quiz_markspersection_table($quiz1, $context, $qmsubselect, $reportoptions,
$empty, $studentsjoins, $questions, null);
$table->download = null;
$table->define_columns(array('fullname'));
$table->define_columns(['fullname']);
$table->sortable(true, 'uniqueid');
$table->define_baseurl(new \moodle_url('/mod/quiz/report.php'));
$table->setup();
Expand All @@ -279,7 +280,7 @@ public function test_compute_average_row() {
$table = new \quiz_markspersection_table($quiz1, $context, $qmsubselect, $reportoptions,
$empty, $studentsjoins, $questions, null);
$table->download = null;
$table->define_columns(array('fullname'));
$table->define_columns(['fullname']);
$table->sortable(true, 'uniqueid');
$table->define_baseurl(new \moodle_url('/mod/quiz/report.php'));
$table->setup();
Expand Down Expand Up @@ -307,7 +308,7 @@ public function test_compute_average_row() {
* @param \stdClass $user The user record object who submitted this attempt.
* @param array $answers The answers submitted by the user.
*
* @return \quiz_attempt The attempt object created.
* @return quiz_attempt The attempt object created.
*/
private function submitanswers($quizobj, $quba1, $user, $answers) {
$timenow = time();
Expand All @@ -316,13 +317,13 @@ private function submitanswers($quizobj, $quba1, $user, $answers) {
quiz_attempt_save_started($quizobj, $quba1, $attempt);

// Process some responses from the student 1.
$attemptobj = \quiz_attempt::create($attempt->id);
$tosubmit = array();
$attemptobj = quiz_attempt::create($attempt->id);
$tosubmit = [];
foreach ($answers as $ianswer => $answer) {
if (is_numeric($answer)) {
$tosubmit[$ianswer + 1] = array('answer' => $answer);
$tosubmit[$ianswer + 1] = ['answer' => $answer];
} else {
$tosubmit[$ianswer + 1] = array('answer' => $answer, 'answerformat' => FORMAT_PLAIN);
$tosubmit[$ianswer + 1] = ['answer' => $answer, 'answerformat' => FORMAT_PLAIN];
}
}
$attemptobj->process_submitted_actions($timenow, false, $tosubmit);
Expand Down
Loading

0 comments on commit 30b4f6a

Please sign in to comment.