Merge branch 'MDL-66748-master' of git://github.com/andrewnicols/moodle

This commit is contained in:
Eloy Lafuente (stronk7)
2019-09-24 23:26:51 +02:00
14 changed files with 1897 additions and 500 deletions
@@ -0,0 +1,114 @@
<?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/>.
/**
* Generator for the gradingforum_guide plugin.
*
* @package gradingform_guide
* @category test
* @copyright 2018 Adrian Greeve <adriangreeve.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tests\gradingform_guide\generator;
/**
* Convenience class to create guide criterion.
*
* @package gradingform_guide
* @copyright 2018 Adrian Greeve <adriangreeve.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class criterion {
/** @var string $shortname A shortened name of the criterion. */
private $shortname;
/** @var string $description A description of the criterion. */
private $description;
/** @var string $descriptionmarkers A description of the criterion for markers. */
private $descriptionmarkers;
/** @var float Maximum score */
private $maxscore = 0;
/**
* Constructor for this test_criterion object
*
* @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 float $maxscore The maximum score possible for this criterion
*/
public function __construct(string $shortname, string $description, string $descriptionmarkers, float $maxscore) {
$this->shortname = $shortname;
$this->description = $description;
$this->descriptionmarkers = $descriptionmarkers;
$this->maxscore = $maxscore;
}
/**
* Get the description for this criterion.
*
* @return string
*/
public function get_description(): string {
return $this->description;
}
/**
* Get the description for markers of this criterion.
*
* @return string
*/
public function get_descriptionmarkers(): string {
return $this->descriptionmarkers;
}
/**
* Get the shortname for this criterion.
*
* @return string
*/
public function get_shortname(): string {
return $this->shortname;
}
/**
* Get the maxscore for this criterion.
*
* @return float
*/
public function get_maxscore(): float {
return $this->maxscore;
}
/**
* Get all values in an array for use when creating a new guide.
*
* @param int $sortorder
* @return array
*/
public function get_all_values(int $sortorder): array {
return [
'sortorder' => $sortorder,
'shortname' => $this->get_shortname(),
'description' => $this->get_description(),
'descriptionmarkers' => $this->get_descriptionmarkers(),
'maxscore' => $this->get_maxscore(),
];
}
}
@@ -0,0 +1,125 @@
<?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/>.
/**
* Generator for the gradingforum_guide plugin.
*
* @package gradingform_guide
* @category test
* @copyright 2018 Adrian Greeve <adriangreeve.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tests\gradingform_guide\generator;
use gradingform_controller;
use stdClass;
/**
* Test guide.
*
* @package gradingform_guide
* @category test
* @copyright 2018 Adrian Greeve <adriangreeve.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class guide {
/** @var array $criteria The criteria for this guide. */
protected $criteria = [];
/** @var string The name of this guide. */
protected $name;
/** @var string A description for this guide. */
protected $description;
/** @var array The guide options. */
protected $options = [];
/**
* Create a new gradingform_guide_generator_criterion.
*
* @param string $name
* @param string $description
*/
public function __construct(string $name, string $description) {
$this->name = $name;
$this->description = $description;
$this->set_option('alwaysshowdefinition', 1);
$this->set_option('showmarkspercriterionstudents', 1);
}
/**
* Creates the guide using the appropriate APIs.
*/
public function get_definition(): stdClass {
return (object) [
'name' => $this->name,
'description_editor' => [
'text' => $this->description,
'format' => FORMAT_HTML,
'itemid' => 1
],
'guide' => [
'criteria' => $this->get_critiera_as_array(),
'options' => $this->options,
'comments' => [],
],
'saveguide' => 'Continue',
'status' => gradingform_controller::DEFINITION_STATUS_READY,
];
}
/**
* Set an option for the rubric.
*
* @param string $key
* @param mixed $value
* @return self
*/
public function set_option(string $key, $value): self {
$this->options[$key] = $value;
return $this;
}
/**
* Adds a criterion to the guide.
*
* @param criterion $criterion The criterion object (class below).
* @return self
*/
public function add_criteria(criterion $criterion): self {
$this->criteria[] = $criterion;
return $this;
}
/**
* Get the criteria as an array for use in creation.
*
* @return array
*/
protected function get_critiera_as_array(): array {
$return = [];
foreach ($this->criteria as $index => $criterion) {
$return["NEWID{$index}"] = $criterion->get_all_values($index + 1);
}
return $return;
}
}
@@ -0,0 +1,235 @@
<?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/>.
/**
* Generator for the gradingforum_guide plugin.
*
* @package gradingform_guide
* @category test
* @copyright 2019 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/guide.php');
require_once(__DIR__ . '/criterion.php');
use tests\gradingform_guide\generator\guide;
use tests\gradingform_guide\generator\criterion;
/**
* Generator for the gradingforum_guide plugintype.
*
* @package gradingform_guide
* @category test
* @copyright 2019 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class gradingform_guide_generator extends component_generator_base {
/**
* Create an instance of a marking guide.
*
* @param context $context
* @param string $component
* @param string $area
* @param string $name
* @param string $description
* @param array $criteria The list of criteria to add to the generated guide
* @return gradingform_guide_controller
*/
public function create_instance(
context $context,
string $component,
string $area,
string $name,
string $description,
array $criteria
): gradingform_guide_controller {
global $USER;
if ($USER->id === 0) {
throw new \coding_exception('Creation of a guide must currently be run as a user.');
}
// Fetch the controller for this context/component/area.
$generator = \testing_util::get_data_generator();
$gradinggenerator = $generator->get_plugin_generator('core_grading');
$controller = $gradinggenerator->create_instance($context, $component, $area, 'guide');
// Generate a definition for the supplied guide.
$guide = $this->get_guide($name, $description);
foreach ($criteria as $name => $options) {
$guide->add_criteria($this->get_criterion(
$name,
$options['description'],
$options['descriptionmarkers'],
$options['maxscore']
));
}
// Update the controller wih the guide definition.
$controller->update_definition($guide->get_definition());
return $controller;
}
/**
* Get a new guide for use with the guide controller.
*
* Note: This is just a helper class used to build a new definition. It does not persist the data.
*
* @param string $name
* @param string $description
* @return generator_guide
*/
protected function get_guide(string $name, string $description): guide {
return new \tests\gradingform_guide\generator\guide($name, $description);
}
/**
* Get a new criterion for use with a guide.
*
* Note: This is just a helper class used to build a new definition. It does not persist the data.
*
* @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 float $maxscore The maximum score possible for this criterion
* @return criterion
*/
protected function get_criterion(
string $shortname,
string $description,
string $descriptionmarkers,
float $maxscore
): criterion {
return new criterion($shortname, $description, $descriptionmarkers, $maxscore);
}
/**
* Given a controller instance, fetch the level and criterion information for the specified values.
*
* @param gradingform_controller $controller
* @param string $shortname The shortname to match the criterion on
* @return stdClass
*/
public function get_criterion_for_values(gradingform_controller $controller, string $shortname): ?stdClass {
$definition = $controller->get_definition();
$criteria = $definition->guide_criteria;
$criterion = array_reduce($criteria, function($carry, $criterion) use ($shortname) {
if ($criterion['shortname'] === $shortname) {
$carry = (object) $criterion;
}
return $carry;
}, null);
return $criterion;
}
/**
* Get submitted form data
*
* @param gradingform_guide_controller $controller
* @param int $itemid
* @param array $values A set of array values where the array key is the name of the criterion, and the value is an
* array with the desired score, and any remark.
*/
public function get_submitted_form_data(gradingform_guide_controller $controller, int $itemid, array $values): array {
$result = [
'itemid' => $itemid,
'criteria' => [],
];
foreach ($values as $criterionname => ['score' => $score, 'remark' => $remark]) {
$criterion = $this->get_criterion_for_values($controller, $criterionname);
$result['criteria'][$criterion->id] = [
'score' => $score,
'remark' => $remark,
];
}
return $result;
}
/**
* Generate a guide controller with sample data required for testing of this class.
*
* @param context_module $context
* @return gradingform_guide_controller
*/
public function get_test_guide(context_module $context): gradingform_guide_controller {
$generator = \testing_util::get_data_generator();
$gradinggenerator = $generator->get_plugin_generator('core_grading');
$controller = $gradinggenerator->create_instance($context, 'mod_assign', 'submission', 'guide');
$generator = \testing_util::get_data_generator();
$guidegenerator = $generator->get_plugin_generator('gradingform_guide');
$guide = $guidegenerator->get_guide('testguide', 'Description text');
$guide->add_criteria($guidegenerator->get_criterion(
'Spelling mistakes',
'Full marks will be given for no spelling mistakes.',
'Deduct 5 points per spelling mistake made.',
25
));
$guide->add_criteria($guidegenerator->get_criterion(
'Pictures',
'Full marks will be given for including 3 pictures.',
'Give 5 points for each picture present',
15
));
$controller->update_definition($guide->get_definition());
return $controller;
}
/**
* Fetch a set of sample data.
*
* @param gradingform_guide_controller $controller
* @param int $itemid
* @param float $spellingscore
* @param string $spellingremark
* @param float $picturescore
* @param string $pictureremark
* @return array
*/
public function get_test_form_data(
gradingform_guide_controller $controller,
int $itemid,
float $spellingscore,
string $spellingremark,
float $picturescore,
string $pictureremark
): array {
$generator = \testing_util::get_data_generator();
$guidegenerator = $generator->get_plugin_generator('gradingform_guide');
return $guidegenerator->get_submitted_form_data($controller, $itemid, [
'Spelling mistakes' => [
'score' => $spellingscore,
'remark' => $spellingremark,
],
'Pictures' => [
'score' => $picturescore,
'remark' => $pictureremark,
],
]);
}
}
@@ -0,0 +1,282 @@
<?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/>.
/**
* Generator testcase for the gradingforum_guide generator.
*
* @package gradingform_guide
* @category test
* @copyright 2019 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tests\gradingform_guide;
use context_module;
use gradingform_controller;
use gradingform_guide_controller;
/**
* Generator testcase for the gradingforum_guide generator.
*
* @package gradingform_guide
* @category test
* @copyright 2019 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class generator_testcase extends \advanced_testcase {
/**
* Test guide creation.
*/
public function test_guide_creation(): void {
global $DB;
$this->resetAfterTest(true);
// Fetch generators.
$generator = \testing_util::get_data_generator();
$guidegenerator = $generator->get_plugin_generator('gradingform_guide');
// Create items required for testing.
$course = $generator->create_course();
$module = $generator->create_module('assign', ['course' => $course]);
$user = $generator->create_user();
$context = context_module::instance($module->cmid);
// Data for testing.
$name = 'myfirstguide';
$description = 'My first guide';
$criteria = [
'Alphabet' => [
'description' => 'How well you know your alphabet',
'descriptionmarkers' => 'Basic literacy: Alphabet',
'maxscore' => 5,
],
'Times tables' => [
'description' => 'How well you know your times-tables',
'descriptionmarkers' => 'Basic numeracy: Multiplication',
'maxscore' => 10,
],
];
// Unit under test.
$this->setUser($user);
$controller = $guidegenerator->create_instance($context, 'mod_assign', 'submission', $name, $description, $criteria);
$this->assertInstanceOf(gradingform_guide_controller::class, $controller);
$definition = $controller->get_definition();
$this->assertEquals('guide', $definition->method);
$this->assertNotEmpty($definition->id);
$this->assertEquals($name, $definition->name);
$this->assertEquals($description, $definition->description);
$this->assertEquals(gradingform_controller::DEFINITION_STATUS_READY, $definition->status);
$this->assertNotEmpty($definition->timecreated);
$this->assertNotEmpty($definition->timemodified);
$this->assertEquals($user->id, $definition->usercreated);
$this->assertNotEmpty($definition->guide_criteria);
$this->assertCount(2, $definition->guide_criteria);
// Check the alphabet criteria.
$criteriaids = array_keys($definition->guide_criteria);
$alphabet = $definition->guide_criteria[$criteriaids[0]];
$this->assertNotEmpty($alphabet['id']);
$this->assertEquals(1, $alphabet['sortorder']);
$this->assertEquals('How well you know your alphabet', $alphabet['description']);
$this->assertEquals('Basic literacy: Alphabet', $alphabet['descriptionmarkers']);
$this->assertEquals(5, $alphabet['maxscore']);
// Check the times tables criteria.
$tables = $definition->guide_criteria[$criteriaids[1]];
$this->assertNotEmpty($tables['id']);
$this->assertEquals(2, $tables['sortorder']);
$this->assertEquals('How well you know your times-tables', $tables['description']);
$this->assertEquals('Basic numeracy: Multiplication', $tables['descriptionmarkers']);
$this->assertEquals(10, $tables['maxscore']);
}
/**
* Test the get_criterion_for_values function.
* This is used for finding criterion and level information within a guide.
*/
public function test_get_criterion_for_values(): void {
global $DB;
$this->resetAfterTest(true);
// Fetch generators.
$generator = \testing_util::get_data_generator();
$guidegenerator = $generator->get_plugin_generator('gradingform_guide');
// Create items required for testing.
$course = $generator->create_course();
$module = $generator->create_module('assign', ['course' => $course]);
$user = $generator->create_user();
$context = context_module::instance($module->cmid);
// Data for testing.
$name = 'myfirstguide';
$description = 'My first guide';
$criteria = [
'Alphabet' => [
'description' => 'How well you know your alphabet',
'descriptionmarkers' => 'Basic literacy: Alphabet',
'maxscore' => 5,
],
'Times tables' => [
'description' => 'How well you know your times-tables',
'descriptionmarkers' => 'Basic numeracy: Multiplication',
'maxscore' => 10,
],
];
$this->setUser($user);
$controller = $guidegenerator->create_instance($context, 'mod_assign', 'submission', $name, $description, $criteria);
// Valid criterion.
$result = $guidegenerator->get_criterion_for_values($controller, 'Alphabet', 2);
$this->assertEquals('Alphabet', $result->shortname);
$this->assertEquals('How well you know your alphabet', $result->description);
$this->assertEquals('Basic literacy: Alphabet', $result->descriptionmarkers);
$this->assertEquals(5, $result->maxscore);
// Invalid criterion.
$result = $guidegenerator->get_criterion_for_values($controller, 'Foo', 0);
$this->assertNull($result);
}
/**
* Tests for the get_test_guide function.
*/
public function test_get_test_guide(): void {
global $DB;
$this->resetAfterTest(true);
// Fetch generators.
$generator = \testing_util::get_data_generator();
$guidegenerator = $generator->get_plugin_generator('gradingform_guide');
// Create items required for testing.
$course = $generator->create_course();
$module = $generator->create_module('assign', ['course' => $course]);
$user = $generator->create_user();
$context = context_module::instance($module->cmid);
$this->setUser($user);
$guide = $guidegenerator->get_test_guide($context, 'assign', 'submissions');
$definition = $guide->get_definition();
$this->assertEquals('testguide', $definition->name);
$this->assertEquals('Description text', $definition->description);
$this->assertEquals(gradingform_controller::DEFINITION_STATUS_READY, $definition->status);
// Should create a guide with 2 criterion.
$this->assertCount(2, $definition->guide_criteria);
}
/**
* Test the get_submitted_form_data function.
*/
public function test_get_submitted_form_data(): void {
global $DB;
$this->resetAfterTest(true);
// Fetch generators.
$generator = \testing_util::get_data_generator();
$guidegenerator = $generator->get_plugin_generator('gradingform_guide');
// Create items required for testing.
$course = $generator->create_course();
$module = $generator->create_module('assign', ['course' => $course]);
$user = $generator->create_user();
$context = context_module::instance($module->cmid);
$this->setUser($user);
$controller = $guidegenerator->get_test_guide($context, 'assign', 'submissions');
$result = $guidegenerator->get_submitted_form_data($controller, 93, [
'Spelling mistakes' => [
'score' => 10,
'remark' => 'Pretty good but you had a couple of errors',
],
'Pictures' => [
'score' => 15,
'remark' => 'Lots of nice pictures!',
]
]);
$this->assertIsArray($result);
$this->assertEquals(93, $result['itemid']);
$this->assertIsArray($result['criteria']);
$this->assertCount(2, $result['criteria']);
$spelling = $guidegenerator->get_criterion_for_values($controller, 'Spelling mistakes');
$this->assertIsArray($result['criteria'][$spelling->id]);
$this->assertEquals(10, $result['criteria'][$spelling->id]['score']);
$this->assertEquals('Pretty good but you had a couple of errors', $result['criteria'][$spelling->id]['remark']);
$pictures = $guidegenerator->get_criterion_for_values($controller, 'Pictures', 2);
$this->assertIsArray($result['criteria'][$pictures->id]);
$this->assertEquals(15, $result['criteria'][$pictures->id]['score']);
$this->assertEquals('Lots of nice pictures!', $result['criteria'][$pictures->id]['remark']);
}
/**
* Test the get_test_form_data function.
*/
public function test_get_test_form_data(): void {
global $DB;
$this->resetAfterTest(true);
// Fetch generators.
$generator = \testing_util::get_data_generator();
$guidegenerator = $generator->get_plugin_generator('gradingform_guide');
// Create items required for testing.
$course = $generator->create_course();
$module = $generator->create_module('assign', ['course' => $course]);
$user = $generator->create_user();
$context = context_module::instance($module->cmid);
$this->setUser($user);
$controller = $guidegenerator->get_test_guide($context, 'assign', 'submissions');
// Unit under test.
$result = $guidegenerator->get_test_form_data(
$controller,
1839,
10, 'Propper good speling',
0, 'ASCII art is not a picture'
);
$this->assertIsArray($result);
$this->assertEquals(1839, $result['itemid']);
$this->assertIsArray($result['criteria']);
$this->assertCount(2, $result['criteria']);
$spelling = $guidegenerator->get_criterion_for_values($controller, 'Spelling mistakes');
$this->assertIsArray($result['criteria'][$spelling->id]);
$this->assertEquals(10, $result['criteria'][$spelling->id]['score']);
$this->assertEquals('Propper good speling', $result['criteria'][$spelling->id]['remark']);
$pictures = $guidegenerator->get_criterion_for_values($controller, 'Pictures');
$this->assertIsArray($result['criteria'][$pictures->id]);
$this->assertEquals(0, $result['criteria'][$pictures->id]['score']);
$this->assertEquals('ASCII art is not a picture', $result['criteria'][$pictures->id]['remark']);
}
}
+73 -69
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\writer;
@@ -35,6 +34,7 @@ use \gradingform_guide\privacy\provider;
/**
* Privacy tests for gradingform_guide.
*
* @package gradingform_guide
* @copyright 2018 Sara Arjona <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
@@ -79,7 +79,7 @@ class gradingform_guide_privacy_testcase extends provider_testcase {
}
/**
* Test the export of rubric data.
* Test the export of guide data.
*/
public function test_get_gradingform_export_data() {
global $DB;
@@ -91,28 +91,19 @@ class gradingform_guide_privacy_testcase extends provider_testcase {
$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 = $this->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 = $this->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');
// Let's try the method we are testing.
@@ -126,7 +117,7 @@ class gradingform_guide_privacy_testcase extends provider_testcase {
}
/**
* Test the deletion of rubric user information via the instance ID.
* Test the deletion of guide user information via the instance ID.
*/
public function test_delete_gradingform_for_instances() {
global $DB;
@@ -138,64 +129,32 @@ class gradingform_guide_privacy_testcase extends provider_testcase {
$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 = $this->get_test_guide($modulecontext);
$controller = $guide->manager->get_controller('guide');
// In the situation of mod_assign this would be the id from assign_grades.
$itemid = 1;
$instance = $controller->create_instance($user->id, $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' => []];
foreach ($criteria as $key => $value) {
if ($value->shortname == 'Spelling mistakes') {
$data['criteria'][$key]['remark'] = 'This user made several mistakes.';
$data['criteria'][$key]['remarkformat'] = 0;
$data['criteria'][$key]['score'] = 5;
} else {
$data['criteria'][$key]['remark'] = 'This user has two pictures.';
$data['criteria'][$key]['remarkformat'] = 0;
$data['criteria'][$key]['score'] = 10;
}
}
$data['itemid'] = $itemid;
$data = $this->get_test_form_data(
$controller,
$itemid,
5, 'This user made several mistakes.',
10, 'This user has two pictures.'
);
// Update this instance with data.
$instance->update($data);
$instanceid = $instance->get_data('id');
$itemid = 2;
$instance = $controller->create_instance($user->id, $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' => []];
foreach ($criteria as $key => $value) {
if ($value->shortname == 'Spelling mistakes') {
$data['criteria'][$key]['remark'] = 'This user made no mistakes.';
$data['criteria'][$key]['remarkformat'] = 0;
$data['criteria'][$key]['score'] = 25;
} else {
$data['criteria'][$key]['remark'] = 'This user has one pictures.';
$data['criteria'][$key]['remarkformat'] = 0;
$data['criteria'][$key]['score'] = 5;
}
}
$data['itemid'] = $itemid;
$data = $this->get_test_form_data(
$controller,
$itemid,
25, 'This user made no mistakes.',
5, 'This user has one pictures.'
);
// Update this instance with data.
$instance->update($data);
$instanceid = $instance->get_data('id');
// Check how many records we have in the fillings table.
$records = $DB->get_records('gradingform_guide_fillings');
@@ -208,4 +167,49 @@ class gradingform_guide_privacy_testcase extends provider_testcase {
$this->assertNotEquals($instance->get_id(), $record->instanceid);
}
}
/**
* Generate a guide controller with sample data required for testing of this class.
*
* @param context_module $context
* @return gradingform_guide_controller
*/
protected function get_test_guide(context_module $context): gradingform_guide_controller {
$generator = \testing_util::get_data_generator();
$guidegenerator = $generator->get_plugin_generator('gradingform_guide');
return $guidegenerator->get_test_guide($context);
}
/**
* Fetch a set of sample data.
*
* @param gradingform_guide_controller $controller
* @param int $itemid
* @param float $spellingscore
* @param string $spellingremark
* @param float $picturescore
* @param string $pictureremark
* @return array
*/
protected function get_test_form_data(
gradingform_guide_controller $controller,
int $itemid,
float $spellingscore,
string $spellingremark,
float $picturescore,
string $pictureremark
): array {
$generator = \testing_util::get_data_generator();
$guidegenerator = $generator->get_plugin_generator('gradingform_guide');
return $guidegenerator->get_test_form_data(
$controller,
$itemid,
$spellingscore,
$spellingremark,
$picturescore,
$pictureremark
);
}
}
@@ -0,0 +1,121 @@
<?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/>.
/**
* Generator for the gradingforum_rubric plugin.
*
* @package gradingform_rubric
* @category test
* @copyright 2018 Adrian Greeve <adriangreeve.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tests\gradingform_rubric\generator;
/**
* Convenience class to create rubric criterion.
*
* @package gradingform_rubric
* @copyright 2018 Adrian Greeve <adriangreeve.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class criterion {
/** @var string $description A description of the criterion. */
public $description;
/** @var integer $sortorder sort order of the criterion. */
public $sortorder = 0;
/** @var array $levels The levels for this criterion. */
public $levels = [];
/**
* Constructor for this test_criterion object
*
* @param string $description A description of this criterion.
* @param array $levels
*/
public function __construct(string $description, array $levels = []) {
$this->description = $description;
foreach ($levels as $definition => $score) {
$this->add_level($definition, $score);
}
}
/**
* Adds levels to the criterion.
*
* @param string $definition The definition for this level.
* @param int $score The score received if this level is selected.
* @return self
*/
public function add_level(string $definition, int $score): self {
$this->levels[] = [
'definition' => $definition,
'score' => $score
];
return $this;
}
/**
* Get the description for this criterion.
*
* @return string
*/
public function get_description(): string {
return $this->description;
}
/**
* Get the levels for this criterion.
*
* @return array
*/
public function get_levels(): array {
return $this->levels;
}
/**
* Get all values in an array for use when creating a new guide.
*
* @param int $sortorder
* @return array
*/
public function get_all_values(int $sortorder): array {
return [
'sortorder' => $sortorder,
'description' => $this->get_description(),
'levels' => $this->get_all_level_values(),
];
}
/**
* Get all level values.
*
* @return array
*/
public function get_all_level_values(): array {
$result = [];
foreach ($this->get_levels() as $index => $level) {
$id = $index + 1;
$result["NEWID{$id}"] = $level;
}
return $result;
}
}
@@ -0,0 +1,240 @@
<?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/>.
/**
* Generator for the gradingforum_rubric plugin.
*
* @package gradingform_rubric
* @category test
* @copyright 2019 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/rubric.php');
require_once(__DIR__ . '/criterion.php');
use tests\gradingform_rubric\generator\rubric;
use tests\gradingform_rubric\generator\criterion;
/**
* Generator for the gradingforum_rubric plugintype.
*
* @package gradingform_rubric
* @category test
* @copyright 2019 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class gradingform_rubric_generator extends component_generator_base {
/**
* Create an instance of a rubric.
*
* @param context $context
* @param string $component
* @param string $area
* @param string $name
* @param string $description
* @param array $criteria The list of criteria to add to the generated rubric
* @return gradingform_rubric_controller
*/
public function create_instance(
context $context,
string $component,
string $area,
string $name,
string $description,
array $criteria
): gradingform_rubric_controller {
global $USER;
if ($USER->id === 0) {
throw new \coding_exception('Creation of a rubric must currently be run as a user.');
}
// Fetch the controller for this context/component/area.
$generator = \testing_util::get_data_generator();
$gradinggenerator = $generator->get_plugin_generator('core_grading');
$controller = $gradinggenerator->create_instance($context, $component, $area, 'rubric');
// Generate a definition for the supplied rubric.
$rubric = $this->get_rubric($name, $description);
foreach ($criteria as $name => $criterion) {
$rubric->add_criteria($this->get_criterion($name, $criterion));
}
// Update the controller wih the rubric definition.
$controller->update_definition($rubric->get_definition());
return $controller;
}
/**
* Get a new rubric for use with the rubric controller.
*
* Note: This is just a helper class used to build a new definition. It does not persist the data.
*
* @param string $name
* @param string $description
* @return rubric
*/
protected function get_rubric(string $name, string $description): rubric {
return new rubric($name, $description);
}
/**
* Get a new rubric for use with a gradingform_rubric_generator_rubric.
*
* Note: This is just a helper class used to build a new definition. It does not persist the data.
*
* @param string $description
* @param array $levels Set of levels in the form definition => score
* @return gradingform_rubric_generator_criterion
*/
protected function get_criterion(string $description, array $levels = []): criterion {
return new criterion($description, $levels);
}
/**
* Given a controller instance, fetch the level and criterion information for the specified values.
*
* @param gradingform_controller $controller
* @param string $description The description to match the criterion on
* @param float $score The value to match the level on
* @return array
*/
public function get_level_and_criterion_for_values(
gradingform_controller $controller,
string $description,
float $score
): array {
$definition = $controller->get_definition();
$criteria = $definition->rubric_criteria;
$criterion = $level = null;
$criterion = array_reduce($criteria, function($carry, $criterion) use ($description) {
if ($criterion['description'] === $description) {
$carry = $criterion;
}
return $carry;
}, null);
if ($criterion) {
$criterion = (object) $criterion;
$level = array_reduce($criterion->levels, function($carry, $level) use ($score) {
if ($level['score'] == $score) {
$carry = $level;
}
return $carry;
});
$level = $level ? (object) $level : null;
}
return [
'criterion' => $criterion,
'level' => $level,
];
}
/**
* Get submitted form data for the supplied controller, itemid, and values.
* The returned data is in the format used by rubric when handling form submission.
*
* @param gradingform_rubric_controller $controller
* @param int $itemid
* @param array $values A set of array values where the array key is the name of the criterion, and the value is an
* array with the desired score, and any remark.
*/
public function get_submitted_form_data(gradingform_rubric_controller $controller, int $itemid, array $values): array {
$result = [
'itemid' => $itemid,
'criteria' => [],
];
foreach ($values as $criterionname => ['score' => $score, 'remark' => $remark]) {
[
'criterion' => $criterion,
'level' => $level,
] = $this->get_level_and_criterion_for_values($controller, $criterionname, $score);
$result['criteria'][$criterion->id] = [
'levelid' => $level->id,
'remark' => $remark,
];
}
return $result;
}
/**
* Generate a rubric controller with sample data required for testing of this class.
*
* @param context $context
* @param string $component
* @param string $area
* @return gradingform_rubric_controller
*/
public function get_test_rubric(context $context, string $component, string $area): gradingform_rubric_controller {
$criteria = [
'Spelling is important' => [
'Nothing but mistakes' => 0,
'Several mistakes' => 1,
'No mistakes' => 2,
],
'Pictures' => [
'No pictures' => 0,
'One picture' => 1,
'More than one picture' => 2,
],
];
return $this->create_instance($context, $component, $area, 'testrubric', 'Description text', $criteria);
}
/**
* Fetch a set of sample data.
*
* @param gradingform_rubric_controller $controller
* @param int $itemid
* @param float $spellingscore
* @param string $spellingremark
* @param float $picturescore
* @param string $pictureremark
* @return array
*/
public function get_test_form_data(
gradingform_rubric_controller $controller,
int $itemid,
float $spellingscore,
string $spellingremark,
float $picturescore,
string $pictureremark
): array {
$generator = \testing_util::get_data_generator();
$rubricgenerator = $generator->get_plugin_generator('gradingform_rubric');
return $rubricgenerator->get_submitted_form_data($controller, $itemid, [
'Spelling is important' => [
'score' => $spellingscore,
'remark' => $spellingremark,
],
'Pictures' => [
'score' => $picturescore,
'remark' => $pictureremark,
],
]);
}
}
@@ -0,0 +1,133 @@
<?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/>.
/**
* Generator for the gradingforum_rubric plugin.
*
* @package gradingform_rubric
* @category test
* @copyright 2018 Adrian Greeve <adriangreeve.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tests\gradingform_rubric\generator;
use gradingform_controller;
use stdClass;
/**
* Test rubric.
*
* @package gradingform_rubric
* @category test
* @copyright 2018 Adrian Greeve <adriangreeve.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class rubric {
/** @var array $criteria The criteria for this rubric. */
protected $criteria = [];
/** @var string The name of this rubric. */
protected $name;
/** @var string A description for this rubric. */
protected $description;
/** @var array The rubric options. */
protected $options = [];
/**
* Create a new gradingform_rubric_generator_rubric.
*
* @param string $name
* @param string $description
*/
public function __construct(string $name, string $description) {
$this->name = $name;
$this->description = $description;
$this->set_option('sortlevelsasc', 1);
$this->set_option('lockzeropoints', 1);
$this->set_option('showdescriptionteacher', 1);
$this->set_option('showdescriptionstudent', 1);
$this->set_option('showscoreteacher', 1);
$this->set_option('showscorestudent', 1);
$this->set_option('enableremarks', 1);
$this->set_option('showremarksstudent', 1);
}
/**
* Creates the rubric using the appropriate APIs.
*/
public function get_definition(): stdClass {
return (object) [
'name' => $this->name,
'description_editor' => [
'text' => $this->description,
'format' => FORMAT_HTML,
'itemid' => 1
],
'rubric' => [
'criteria' => $this->get_all_criterion_values(),
'options' => $this->options,
],
'saverubric' => 'Save rubric and make it ready',
'status' => gradingform_controller::DEFINITION_STATUS_READY,
];
}
/**
* Set an option for the rubric.
*
* @param string $key
* @param mixed $value
* @return self
*/
public function set_option(string $key, $value): self {
$this->options[$key] = $value;
return $this;
}
/**
* Adds a criterion to the rubric.
*
* @param criterion $criterion The criterion object (class below).
* @return self
*/
public function add_criteria(criterion $criterion): self {
$this->criteria[] = $criterion;
return $this;
}
/**
* Get all criterion values.
*
* @return array
*/
protected function get_all_criterion_values(): array {
$result = [];
foreach ($this->criteria as $index => $criterion) {
$id = $index + 1;
$result["NEWID{$id}"] = $criterion->get_all_values($id);
}
return $result;
}
}
@@ -0,0 +1,319 @@
<?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/>.
/**
* Generator testcase for the gradingforum_rubric generator.
*
* @package gradingform_rubric
* @category test
* @copyright 2019 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tests\gradingform_rubric;
use advanced_testcase;
use context_module;
use gradingform_rubric_controller;
use gradingform_controller;
/**
* Generator testcase for the gradingforum_rubric generator.
*
* @package gradingform_rubric
* @category test
* @copyright 2019 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class generator_testcase extends advanced_testcase {
/**
* Test rubric creation.
*/
public function test_rubric_creation(): void {
$this->resetAfterTest(true);
// Fetch generators.
$generator = \testing_util::get_data_generator();
$rubricgenerator = $generator->get_plugin_generator('gradingform_rubric');
// Create items required for testing.
$course = $generator->create_course();
$module = $generator->create_module('assign', ['course' => $course]);
$user = $generator->create_user();
$context = context_module::instance($module->cmid);
// Data for testing.
$name = 'myfirstrubric';
$description = 'My first rubric';
$criteria = [
'Alphabet' => [
'Not known' => 0,
'Letters known but out of order' => 1,
'Letters known in order ascending' => 2,
'Letters known and can recite forwards and backwards' => 4,
],
'Times tables' => [
'Not known' => 0,
'2 times table known' => 2,
'2 and 5 times table known' => 4,
'2, 5, and 10 times table known' => 8,
],
];
// Unit under test.
$this->setUser($user);
$controller = $rubricgenerator->create_instance($context, 'mod_assign', 'submission', $name, $description, $criteria);
$this->assertInstanceOf(gradingform_rubric_controller::class, $controller);
$definition = $controller->get_definition();
$this->assertNotEmpty($definition->id);
$this->assertEquals($name, $definition->name);
$this->assertEquals($description, $definition->description);
$this->assertEquals(gradingform_controller::DEFINITION_STATUS_READY, $definition->status);
$this->assertNotEmpty($definition->timecreated);
$this->assertNotEmpty($definition->timemodified);
$this->assertEquals($user->id, $definition->usercreated);
$this->assertNotEmpty($definition->rubric_criteria);
$this->assertCount(2, $definition->rubric_criteria);
// Check the alphabet criteria.
$criteriaids = array_keys($definition->rubric_criteria);
$alphabet = $definition->rubric_criteria[$criteriaids[0]];
$this->assertNotEmpty($alphabet['id']);
$this->assertEquals(1, $alphabet['sortorder']);
$this->assertEquals('Alphabet', $alphabet['description']);
$this->assertNotEmpty($alphabet['levels']);
$levels = $alphabet['levels'];
$levelids = array_keys($levels);
$level = $levels[$levelids[0]];
$this->assertEquals(0, $level['score']);
$this->assertEquals('Not known', $level['definition']);
$level = $levels[$levelids[1]];
$this->assertEquals(1, $level['score']);
$this->assertEquals('Letters known but out of order', $level['definition']);
$level = $levels[$levelids[2]];
$this->assertEquals(2, $level['score']);
$this->assertEquals('Letters known in order ascending', $level['definition']);
$level = $levels[$levelids[3]];
$this->assertEquals(4, $level['score']);
$this->assertEquals('Letters known and can recite forwards and backwards', $level['definition']);
// Check the times tables criteria.
$tables = $definition->rubric_criteria[$criteriaids[1]];
$this->assertNotEmpty($tables['id']);
$this->assertEquals(2, $tables['sortorder']);
$this->assertEquals('Times tables', $tables['description']);
$this->assertNotEmpty($tables['levels']);
$levels = $tables['levels'];
$levelids = array_keys($levels);
$level = $levels[$levelids[0]];
$this->assertEquals(0, $level['score']);
$this->assertEquals('Not known', $level['definition']);
$level = $levels[$levelids[1]];
$this->assertEquals(2, $level['score']);
$this->assertEquals('2 times table known', $level['definition']);
$level = $levels[$levelids[2]];
$this->assertEquals(4, $level['score']);
$this->assertEquals('2 and 5 times table known', $level['definition']);
$level = $levels[$levelids[3]];
$this->assertEquals(8, $level['score']);
$this->assertEquals('2, 5, and 10 times table known', $level['definition']);
}
/**
* Test the get_level_and_criterion_for_values function.
* This is used for finding criterion and level information within a rubric.
*/
public function test_get_level_and_criterion_for_values(): void {
$this->resetAfterTest(true);
// Fetch generators.
$generator = \testing_util::get_data_generator();
$rubricgenerator = $generator->get_plugin_generator('gradingform_rubric');
// Create items required for testing.
$course = $generator->create_course();
$module = $generator->create_module('assign', ['course' => $course]);
$user = $generator->create_user();
$context = context_module::instance($module->cmid);
// Data for testing.
$description = 'My first rubric';
$criteria = [
'Alphabet' => [
'Not known' => 0,
'Letters known but out of order' => 1,
'Letters known in order ascending' => 2,
'Letters known and can recite forwards and backwards' => 4,
],
'Times tables' => [
'Not known' => 0,
'2 times table known' => 2,
'2 and 5 times table known' => 4,
'2, 5, and 10 times table known' => 8,
],
];
$this->setUser($user);
$controller = $rubricgenerator->create_instance($context, 'mod_assign', 'submission', 'rubric', $description, $criteria);
// Valid criterion and level.
$result = $rubricgenerator->get_level_and_criterion_for_values($controller, 'Alphabet', 2);
$this->assertEquals('Alphabet', $result['criterion']->description);
$this->assertEquals('2', $result['level']->score);
$this->assertEquals('Letters known in order ascending', $result['level']->definition);
// Valid criterion. Invalid level.
$result = $rubricgenerator->get_level_and_criterion_for_values($controller, 'Alphabet', 3);
$this->assertEquals('Alphabet', $result['criterion']->description);
$this->assertNull($result['level']);
// Invalid criterion.
$result = $rubricgenerator->get_level_and_criterion_for_values($controller, 'Foo', 0);
$this->assertNull($result['criterion']);
}
/**
* Tests for the get_test_rubric function.
*/
public function test_get_test_rubric(): void {
$this->resetAfterTest(true);
// Fetch generators.
$generator = \testing_util::get_data_generator();
$rubricgenerator = $generator->get_plugin_generator('gradingform_rubric');
// Create items required for testing.
$course = $generator->create_course();
$module = $generator->create_module('assign', ['course' => $course]);
$user = $generator->create_user();
$context = context_module::instance($module->cmid);
$this->setUser($user);
$rubric = $rubricgenerator->get_test_rubric($context, 'assign', 'submissions');
$definition = $rubric->get_definition();
$this->assertEquals('testrubric', $definition->name);
$this->assertEquals('Description text', $definition->description);
$this->assertEquals(gradingform_controller::DEFINITION_STATUS_READY, $definition->status);
// Should create a rubric with 2 criterion.
$this->assertCount(2, $definition->rubric_criteria);
}
/**
* Test the get_submitted_form_data function.
*/
public function test_get_submitted_form_data(): void {
$this->resetAfterTest(true);
// Fetch generators.
$generator = \testing_util::get_data_generator();
$rubricgenerator = $generator->get_plugin_generator('gradingform_rubric');
// Create items required for testing.
$course = $generator->create_course();
$module = $generator->create_module('assign', ['course' => $course]);
$user = $generator->create_user();
$context = context_module::instance($module->cmid);
$this->setUser($user);
$controller = $rubricgenerator->get_test_rubric($context, 'assign', 'submissions');
$result = $rubricgenerator->get_submitted_form_data($controller, 93, [
'Spelling is important' => [
'score' => 1,
'remark' => 'Good speeling',
],
'Pictures' => [
'score' => 2,
'remark' => 'Lots of nice pictures!',
]
]);
$this->assertIsArray($result);
$this->assertEquals(93, $result['itemid']);
$this->assertIsArray($result['criteria']);
$this->assertCount(2, $result['criteria']);
$spelling = $rubricgenerator->get_level_and_criterion_for_values($controller, 'Spelling is important', 1);
$this->assertIsArray($result['criteria'][$spelling['criterion']->id]);
$this->assertEquals($spelling['level']->id, $result['criteria'][$spelling['criterion']->id]['levelid']);
$this->assertEquals('Good speeling', $result['criteria'][$spelling['criterion']->id]['remark']);
$pictures = $rubricgenerator->get_level_and_criterion_for_values($controller, 'Pictures', 2);
$this->assertIsArray($result['criteria'][$pictures['criterion']->id]);
$this->assertEquals($pictures['level']->id, $result['criteria'][$pictures['criterion']->id]['levelid']);
$this->assertEquals('Lots of nice pictures!', $result['criteria'][$pictures['criterion']->id]['remark']);
}
/**
* Test the get_test_form_data function.
*/
public function test_get_test_form_data(): void {
$this->resetAfterTest(true);
// Fetch generators.
$generator = \testing_util::get_data_generator();
$rubricgenerator = $generator->get_plugin_generator('gradingform_rubric');
// Create items required for testing.
$course = $generator->create_course();
$module = $generator->create_module('assign', ['course' => $course]);
$user = $generator->create_user();
$context = context_module::instance($module->cmid);
$this->setUser($user);
$controller = $rubricgenerator->get_test_rubric($context, 'assign', 'submissions');
// Unit under test.
$result = $rubricgenerator->get_test_form_data(
$controller,
1839,
1, 'Propper good speling',
0, 'ASCII art is not a picture'
);
$this->assertIsArray($result);
$this->assertEquals(1839, $result['itemid']);
$this->assertIsArray($result['criteria']);
$this->assertCount(2, $result['criteria']);
$spelling = $rubricgenerator->get_level_and_criterion_for_values($controller, 'Spelling is important', 1);
$this->assertIsArray($result['criteria'][$spelling['criterion']->id]);
$this->assertEquals($spelling['level']->id, $result['criteria'][$spelling['criterion']->id]['levelid']);
$this->assertEquals('Propper good speling', $result['criteria'][$spelling['criterion']->id]['remark']);
$pictures = $rubricgenerator->get_level_and_criterion_for_values($controller, 'Pictures', 0);
$this->assertIsArray($result['criteria'][$pictures['criterion']->id]);
$this->assertEquals($pictures['level']->id, $result['criteria'][$pictures['criterion']->id]['levelid']);
$this->assertEquals('ASCII art is not a picture', $result['criteria'][$pictures['criterion']->id]['remark']);
}
}
+70 -200
View File
@@ -23,11 +23,13 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
namespace tests\gradingform_rubric;
use \core_privacy\tests\provider_testcase;
use \core_privacy\local\request\writer;
use \gradingform_rubric\privacy\provider;
use core_privacy\tests\provider_testcase;
use core_privacy\local\request\writer;
use gradingform_rubric\privacy\provider;
use gradingform_rubric_controller;
use context_module;
/**
* Privacy tests for gradingform_rubric
@@ -35,7 +37,7 @@ use \gradingform_rubric\privacy\provider;
* @copyright 2018 Adrian Greeve <adriangreeve.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class gradingform_rubric_privacy_testcase extends provider_testcase {
class privacy_testcase extends provider_testcase {
/**
* Test the export of rubric data.
@@ -45,43 +47,23 @@ class gradingform_rubric_privacy_testcase extends provider_testcase {
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$module = $this->getDataGenerator()->create_module('assign', ['course' => $course]);
$modulecontext = context_module::instance($module->cmid);
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
$modulecontext = context_module::instance($module->cmid);
$rubric = new test_rubric($modulecontext, 'testrubrib', 'Description text');
$criterion = new test_criterion('Spelling is important');
$criterion->add_level('Nothing but mistakes', 0);
$criterion->add_level('Several mistakes', 1);
$criterion->add_level('No mistakes', 2);
$rubric->add_criteria($criterion);
$criterion = new test_criterion('Pictures');
$criterion->add_level('No pictures', 0);
$criterion->add_level('One picture', 1);
$criterion->add_level('More than one picture', 2);
$rubric->add_criteria($criterion);
$rubric->create_rubric();
// Generate a test rubric and get its controller.
$controller = $this->get_test_rubric($modulecontext, 'assign', 'submissions');
$controller = $rubric->manager->get_controller('rubric');
// In the situation of mod_assign this would be the id from assign_grades.
$itemid = 1;
$instance = $controller->create_instance($user->id, $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_rubric_criteria');
$data = ['criteria' => []];
foreach ($criteria as $key => $value) {
if ($value->description == 'Spelling is important') {
$level = $DB->get_record('gradingform_rubric_levels', ['criterionid' => $key, 'score' => 1]);
$data['criteria'][$key]['levelid'] = $level->id;
$data['criteria'][$key]['remark'] = 'This user made several mistakes.';
} else {
$level = $DB->get_record('gradingform_rubric_levels', ['criterionid' => $key, 'score' => 0]);
$data['criteria'][$key]['levelid'] = $level->id;
$data['criteria'][$key]['remark'] = 'Please add more pictures.';
}
}
$data['itemid'] = $itemid;
$data = $this->get_test_form_data(
$controller,
$itemid,
1, 'This user made several mistakes.',
0, 'Please add more pictures.'
);
// Update this instance with data.
$instance->update($data);
@@ -105,43 +87,23 @@ class gradingform_rubric_privacy_testcase extends provider_testcase {
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();
$module = $this->getDataGenerator()->create_module('assign', ['course' => $course]);
$modulecontext = context_module::instance($module->cmid);
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
$modulecontext = context_module::instance($module->cmid);
$rubric = new test_rubric($modulecontext, 'testrubrib', 'Description text');
$criterion = new test_criterion('Spelling is important');
$criterion->add_level('Nothing but mistakes', 0);
$criterion->add_level('Several mistakes', 1);
$criterion->add_level('No mistakes', 2);
$rubric->add_criteria($criterion);
$criterion = new test_criterion('Pictures');
$criterion->add_level('No pictures', 0);
$criterion->add_level('One picture', 1);
$criterion->add_level('More than one picture', 2);
$rubric->add_criteria($criterion);
$rubric->create_rubric();
// Generate a test rubric and get its controller.
$controller = $this->get_test_rubric($modulecontext, 'assign', 'submissions');
$controller = $rubric->manager->get_controller('rubric');
// In the situation of mod_assign this would be the id from assign_grades.
$itemid = 1;
$instance = $controller->create_instance($user->id, $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_rubric_criteria');
$data = ['criteria' => []];
foreach ($criteria as $key => $value) {
if ($value->description == 'Spelling is important') {
$level = $DB->get_record('gradingform_rubric_levels', ['criterionid' => $key, 'score' => 1]);
$data['criteria'][$key]['levelid'] = $level->id;
$data['criteria'][$key]['remark'] = 'This user made several mistakes.';
} else {
$level = $DB->get_record('gradingform_rubric_levels', ['criterionid' => $key, 'score' => 0]);
$data['criteria'][$key]['levelid'] = $level->id;
$data['criteria'][$key]['remark'] = 'Please add more pictures.';
}
}
$data['itemid'] = $itemid;
$data = $this->get_test_form_data(
$controller,
$itemid,
1, 'This user made several mistakes.',
0, 'Please add more pictures.'
);
// Update this instance with data.
$instance->update($data);
@@ -149,21 +111,13 @@ class gradingform_rubric_privacy_testcase extends provider_testcase {
// Second instance.
$itemid = 2;
$instance = $controller->create_instance($user->id, $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_rubric_criteria');
$data = ['criteria' => []];
foreach ($criteria as $key => $value) {
if ($value->description == 'Spelling is important') {
$level = $DB->get_record('gradingform_rubric_levels', ['criterionid' => $key, 'score' => 0]);
$data['criteria'][$key]['levelid'] = $level->id;
$data['criteria'][$key]['remark'] = 'Too many mistakes. Please try again.';
} else {
$level = $DB->get_record('gradingform_rubric_levels', ['criterionid' => $key, 'score' => 2]);
$data['criteria'][$key]['levelid'] = $level->id;
$data['criteria'][$key]['remark'] = 'Great number of pictures. Well done.';
}
}
$data['itemid'] = $itemid;
$data = $this->get_test_form_data(
$controller,
$itemid,
0, 'Too many mistakes. Please try again.',
2, 'Great number of pictures. Well done.'
);
// Update this instance with data.
$instance->update($data);
@@ -179,135 +133,51 @@ class gradingform_rubric_privacy_testcase extends provider_testcase {
$this->assertNotEquals($instance->get_id(), $record->instanceid);
}
}
}
/**
* Convenience class to create rubrics.
*
* @copyright 2018 Adrian Greeve <adriangreeve.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class test_rubric {
/** @var array $criteria The criteria for this rubric. */
protected $criteria = [];
/** @var context The context that this rubric is in. */
protected $context;
/** @var string The name of this rubric. */
protected $name;
/** @var string A description for this rubric. */
protected $text;
/** @var integer The current criterion ID. This is incremented when a new criterion is added. */
protected $criterionid = 0;
/** @var grading_manager An object for managing the rubric. */
public $manager;
/**
* Constuctor for this rubric.
* Generate a rubric controller with sample data required for testing of this class.
*
* @param context $context The context that this rubric is being used in.
* @param string $name Name of the rubric.
* @param string $text Description of the rubric.
* @param context_module $context
* @param string $component
* @param string $area
* @return gradingform_rubric_controller
*/
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');
protected function get_test_rubric(context_module $context, string $component, string $area): gradingform_rubric_controller {
$generator = \testing_util::get_data_generator();
$rubricgenerator = $generator->get_plugin_generator('gradingform_rubric');
return $rubricgenerator->get_test_rubric($context, $component, $area);
}
/**
* Creates the rubric using the appropriate APIs.
*/
public function create_rubric() {
$data = (object) [
'areaid' => $this->context->id,
'returnurl' => '',
'name' => $this->name,
'description_editor' => [
'text' => $this->text,
'format' => 1,
'itemid' => 1
],
'rubric' => [
'criteria' => $this->criteria,
'options' => [
'sortlevelsasc' => 1,
'lockzeropoints' => 1,
'showdescriptionteacher' => 1,
'showdescriptionstudent' => 1,
'showscoreteacher' => 1,
'showscorestudent' => 1,
'enableremarks' => 1,
'showremarksstudent' => 1
]
],
'saverubric' => 'Save rubric and make it ready',
'status' => 20
];
$controller = $this->manager->get_controller('rubric');
$controller->update_definition($data);
}
/**
* Adds a criterion to the rubric.
* Fetch a set of sample data.
*
* @param test_criterion $criterion The criterion object (class below).
* @param gradingform_rubric_controller $controller
* @param int $itemid
* @param float $spellingscore
* @param string $spellingremark
* @param float $picturescore
* @param string $pictureremark
* @return array
*/
public function add_criteria($criterion) {
protected function get_test_form_data(
gradingform_rubric_controller $controller,
int $itemid,
float $spellingscore,
string $spellingremark,
float $picturescore,
string $pictureremark
): array {
$generator = \testing_util::get_data_generator();
$rubricgenerator = $generator->get_plugin_generator('gradingform_rubric');
$this->criterionid++;
$this->criteria['NEWID' . $this->criterionid] = [
'description' => $criterion->description,
'sortorder' => $this->criterionid,
'levels' => $criterion->levels
];
}
}
/**
* Convenience class to create rubric criterion.
*
* @copyright 2018 Adrian Greeve <adriangreeve.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class test_criterion {
/** @var string $description A description of the criterion. */
public $description;
/** @var integer $sortorder sort order of the criterion. */
public $sortorder = 0;
/** @var integer $levelid The current level id for this level*/
public $levelid = 0;
/** @var array $levels The levels for this criterion. */
public $levels = [];
/**
* Constructor for this test_criterion object
*
* @param string $description A description of this criterion.
*/
public function __construct($description) {
$this->description = $description;
}
/**
* Adds levels to the criterion.
*
* @param string $definition The definition for this level.
* @param int $score The score received if this level is selected.
*/
public function add_level($definition, $score) {
$this->levelid++;
$this->levels['NEWID' . $this->levelid] = [
'definition' => $definition,
'score' => $score
];
return $rubricgenerator->get_test_form_data(
$controller,
$itemid,
$spellingscore,
$spellingremark,
$picturescore,
$pictureremark
);
}
}
-158
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;
}
}
+56
View File
@@ -0,0 +1,56 @@
<?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/>.
/**
* Generator for the core_grading subsystem generator.
*
* @package core_grading
* @category test
* @copyright 2019 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Generator for the core_grading subsystem generator.
*
* @package core_grading
* @category test
* @copyright 2019 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_grading_generator extends component_generator_base {
/**
* Create an instance of an advanced grading area.
*
* @param context $context
* @param string $component
* @param string $areaname An area belonging to the specified component
* @param string $method An available gradingform type
* @return gradingform_controller The controller for the created instance
*/
public function create_instance(context $context, string $component, string $areaname, string $method): gradingform_controller {
require_once(__DIR__ . '/../../lib.php');
$manager = get_grading_manager($context, $component, $areaname);
$manager->set_active_method($method);
return $manager->get_controller($method);
}
}
+72
View File
@@ -0,0 +1,72 @@
<?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/>.
/**
* Generator testcase for the gradingforum_rubric generator.
*
* @package core_grading
* @category test
* @copyright 2019 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tests\core_grading;
use advanced_testcase;
use context_module;
use gradingform_controller;
use gradingform_rubric_controller;
/**
* Generator testcase for the core_grading generator.
*
* @package core_grading
* @category test
* @copyright 2019 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class generator_testcase extends advanced_testcase {
/**
* Test gradingform controller creation.
*/
public function test_create_instance(): void {
$this->resetAfterTest(true);
// Fetch generators.
$generator = \testing_util::get_data_generator();
$gradinggenerator = $generator->get_plugin_generator('core_grading');
// Create items required for testing.
$course = $generator->create_course();
$module = $generator->create_module('assign', ['course' => $course]);
$user = $generator->create_user();
$context = context_module::instance($module->cmid);
// The assignment module has an itemumber 0 which is an advanced grading area called 'submissions'.
$component = 'mod_assign';
$area = 'submissions';
$controller = $gradinggenerator->create_instance($context, $component, $area, 'rubric');
// This should be a rubric.
$this->assertInstanceOf(gradingform_controller::class, $controller);
$this->assertInstanceOf(gradingform_rubric_controller::class, $controller);
$this->assertEquals($context, $controller->get_context());
$this->assertEquals($component, $controller->get_component());
$this->assertEquals($area, $controller->get_area());
}
}
+57 -73
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');