MDL-66748 core_grading: Use new gradingform data providers

This commit is contained in:
Andrew Nicols 2019-09-23 19:10:36 +08:00
parent d6576ca229
commit 14d8cdcc35
2 changed files with 57 additions and 231 deletions

View File

@ -1,158 +0,0 @@
<?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/>.
/**
* A test guide class fixture.
*
* @package core_grading
* @category test
* @copyright 2018 Adrian Greeve <adriangreeve.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Convenience class to create marking guides.
*
* @copyright 2018 Adrian Greeve <adriangreeve.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class test_guide {
/** @var array $criteria Criteria for this marking guide. */
protected $criteria = [];
/** @var context $context The context that this marking guide is used in. */
protected $context;
/** @var string $name The name of this marking guide. */
protected $name;
/** @var string $text A description of this marking guide. */
protected $text;
/** @var integer $criterionid The current id for the criterion. */
protected $criterionid = 0;
/** @var integer $sortorder The current id for the sort order. */
protected $sortorder = 0;
/** @var gradingform_controller The grading form controller. */
protected $controller;
/** @var grading_manager $manager The grading manager to handle creating the real marking guide. */
public $manager;
/**
* The constuctor for this test_guide object.
*
* @param context $context The context that this marking guide is used in.
* @param string $name The name of the marking guide.
* @param string $text The description of the marking guide.
*/
public function __construct($context, $name, $text) {
$this->context = $context;
$this->name = $name;
$this->text = $text;
$this->manager = get_grading_manager();
$this->manager->set_context($context);
$this->manager->set_component('mod_assign');
$this->manager->set_area('submission');
}
/**
* Uses the appropriate data and APIs to create a marking guide.
*/
public function create_guide() {
$data = (object) [
'areaid' => $this->context->id,
'returnurl' => '',
'name' => $this->name,
'description_editor' => [
'text' => $this->text,
'format' => 1,
'itemid' => 1
],
'guide' => [
'criteria' => $this->criteria,
'options' => [
'alwaysshowdefinition' => 1,
'showmarkspercriterionstudents' => 1
],
'comments' => []
],
'saveguide' => 'Continue',
'status' => 20
];
$this->controller = $this->manager->get_controller('guide');
$this->controller->update_definition($data);
}
/**
* Adds criteria to the marking guide.
*
* @param string $shortname The shortname for the criterion.
* @param string $description The description for the criterion.
* @param string $descriptionmarkers The description for the marker for this criterion.
* @param int $maxscore The maximum score possible for this criterion.
*/
public function add_criteria($shortname, $description, $descriptionmarkers, $maxscore) {
$this->criterionid++;
$this->sortorder++;
$this->criteria['NEWID' . $this->criterionid] = [
'sortorder' => $this->sortorder,
'shortname' => $shortname,
'description' => $description,
'descriptionmarkers' => $descriptionmarkers,
'maxscore' => $maxscore
];
}
/**
* Update the grade for the item provided.
* Keep the gradeinfo array in the same order as the definition of the criteria.
* The array should be [['remark' => remark, 'score' => intvalue],['remark' => remark, 'score' => intvalue]]
* for a guide that has two criteria.
*
* @param int $userid The user we are updating.
* @param int $itemid The itemid that the grade will be for
* @param array $gradeinfo Comments and grades for the grade.
* @return gradingform_guide_instance The created instance associated with the grade created.
*/
public function grade_item(int $userid, int $itemid, array $gradeinfo) : gradingform_guide_instance {
global $DB;
if (!isset($this->controller)) {
throw new Exception("Please call create_guide before calling this method", 1);
}
$instance = $this->controller->create_instance($userid, $itemid);
// I need the ids for the criteria and there doesn't seem to be a nice method to get it.
$criteria = $DB->get_records('gradingform_guide_criteria');
$data = ['criteria' => []];
$i = 0;
// The sort order should keep everything here in order.
foreach ($criteria as $key => $value) {
$data['criteria'][$key]['remark'] = $gradeinfo[$i]['remark'];
$data['criteria'][$key]['remarkformat'] = 0;
$data['criteria'][$key]['score'] = $gradeinfo[$i]['score'];
$i++;
}
$data['itemid'] = $itemid;
// Update this instance with data.
$instance->update($data);
return $instance;
}
}

View File

@ -26,7 +26,6 @@
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/grade/grading/tests/fixtures/marking_guide.php');
use \core_privacy\tests\provider_testcase;
use \core_privacy\local\request\approved_contextlist;
@ -268,32 +267,25 @@ class core_grading_privacy_testcase extends provider_testcase {
$course = $this->getDataGenerator()->create_course();
$module = $this->getDataGenerator()->create_module('assign', ['course' => $course]);
$user = $this->getDataGenerator()->create_user();
$guidegenerator = \testing_util::get_data_generator()->get_plugin_generator('gradingform_guide');
$this->setUser($user);
$modulecontext = context_module::instance($module->cmid);
$guide = new test_guide($modulecontext, 'testrubrib', 'Description text');
$guide->add_criteria(
'Spelling mistakes',
'Full marks will be given for no spelling mistakes.',
'Deduct 5 points per spelling mistake made.',
25
);
$guide->add_criteria(
'Pictures',
'Full marks will be given for including 3 pictures.',
'Give 5 points for each picture present',
15
);
$guide->create_guide();
$controller = $guidegenerator->get_test_guide($modulecontext);
// In the situation of mod_assign this would be the id from assign_grades.
$itemid = 1;
$gradedata = [
['remark' => 'This user made several mistakes.', 'score' => 5],
['remark' => 'This user has two pictures.', 'score' => 10]
];
$instance = $guide->grade_item($user->id, $itemid, $gradedata);
$instance = $controller->create_instance($user->id, $itemid);
$data = $guidegenerator->get_test_form_data(
$controller,
$itemid,
5, 'This user made several mistakes.',
10, 'This user has two pictures.'
);
$instance->update($data);
$instanceid = $instance->get_data('id');
provider::export_item_data($modulecontext, $itemid, ['Test']);
$data = (array) writer::with_context($modulecontext)->get_data(['Test', 'Marking guide', $instance->get_data('id')]);
@ -313,39 +305,33 @@ class core_grading_privacy_testcase extends provider_testcase {
$course = $this->getDataGenerator()->create_course();
$module = $this->getDataGenerator()->create_module('assign', ['course' => $course]);
$user = $this->getDataGenerator()->create_user();
$guidegenerator = \testing_util::get_data_generator()->get_plugin_generator('gradingform_guide');
$this->setUser($user);
$modulecontext = context_module::instance($module->cmid);
$guide = new test_guide($modulecontext, 'testrubrib', 'Description text');
$guide->add_criteria(
'Spelling mistakes',
'Full marks will be given for no spelling mistakes.',
'Deduct 5 points per spelling mistake made.',
25
);
$guide->add_criteria(
'Pictures',
'Full marks will be given for including 3 pictures.',
'Give 5 points for each picture present',
15
);
$guide->create_guide();
$controller = $guidegenerator->get_test_guide($modulecontext);
// In the situation of mod_assign this would be the id from assign_grades.
$itemid = 1;
$gradedata = [
['remark' => 'This user made several mistakes.', 'score' => 5],
['remark' => 'This user has two pictures.', 'score' => 10]
];
$instance = $guide->grade_item($user->id, $itemid, $gradedata);
$instance = $controller->create_instance($user->id, $itemid);
$data = $guidegenerator->get_test_form_data(
$controller,
$itemid,
5, 'This user made several mistakes.',
10, 'This user has two pictures.'
);
$instance->update($data);
$itemid = 2;
$gradedata = [
['remark' => 'This user made no mistakes.', 'score' => 25],
['remark' => 'This user has one picture.', 'score' => 5]
];
$instance = $guide->grade_item($user->id, $itemid, $gradedata);
$instance = $controller->create_instance($user->id, $itemid);
$data = $guidegenerator->get_test_form_data(
$controller,
$itemid,
25, 'This user made no mistakes.',
5, 'This user has one picture.'
);
$instance->update($data);
// Check how many records we have in the fillings table.
$records = $DB->get_records('gradingform_guide_fillings');
@ -374,52 +360,50 @@ class core_grading_privacy_testcase extends provider_testcase {
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$user3 = $this->getDataGenerator()->create_user();
$guidegenerator = \testing_util::get_data_generator()->get_plugin_generator('gradingform_guide');
$this->setUser($user1);
$modulecontext = context_module::instance($module->cmid);
$guide = new test_guide($modulecontext, 'testrubrib', 'Description text');
$guide->add_criteria(
'Spelling mistakes',
'Full marks will be given for no spelling mistakes.',
'Deduct 5 points per spelling mistake made.',
25
);
$guide->add_criteria(
'Pictures',
'Full marks will be given for including 3 pictures.',
'Give 5 points for each picture present',
15
);
$guide->create_guide();
$controller = $guidegenerator->get_test_guide($modulecontext);
// In the situation of mod_assign this would be the id from assign_grades.
$itemid1 = 1;
$gradedata = [
['remark' => 'This user made several mistakes.', 'score' => 5],
['remark' => 'This user has two pictures.', 'score' => 10]
];
$instance1 = $guide->grade_item($user1->id, $itemid1, $gradedata);
$instance1 = $controller->create_instance($user1->id, $itemid1);
$data = $guidegenerator->get_test_form_data(
$controller,
$itemid1,
5, 'This user made several mistakes.',
10, 'This user has two pictures.'
);
$instance1->update($data);
$itemid2 = 2;
$gradedata = [
['remark' => 'This user made a couple of mistakes.', 'score' => 15],
['remark' => 'This user has one picture.', 'score' => 10]
];
$instance2 = $guide->grade_item($user2->id, $itemid2, $gradedata);
$instance2 = $controller->create_instance($user2->id, $itemid2);
$data = $guidegenerator->get_test_form_data(
$controller,
$itemid2,
15, 'This user made a couple of mistakes.',
10, 'This user has one picture.'
);
$instance2->update($data);
$itemid3 = 3;
$gradedata = [
['remark' => 'This user made one mistakes.', 'score' => 20],
['remark' => 'This user has one picture.', 'score' => 10]
];
$instance3 = $guide->grade_item($user3->id, $itemid3, $gradedata);
$instance3 = $controller->create_instance($user3->id, $itemid3);
$data = $guidegenerator->get_test_form_data(
$controller,
$itemid3,
20, 'This user made one mistakes.',
10, 'This user has one picture.'
);
$instance3->update($data);
$records = $DB->get_records('gradingform_guide_fillings');
$this->assertCount(6, $records);
// Delete all user data for items 1 and 3.
provider::delete_data_for_instances($modulecontext, [$itemid1, $itemid3]);
$records = $DB->get_records('gradingform_guide_fillings');
$this->assertCount(2, $records);
$instanceid = $instance2->get_data('id');