Skip to content

Commit

Permalink
Merge branch 'MDL-65769-master' of git://github.com/lameze/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Jul 17, 2019
2 parents 4de3b06 + b209893 commit 4165860
Show file tree
Hide file tree
Showing 129 changed files with 3,560 additions and 3,621 deletions.
80 changes: 61 additions & 19 deletions analytics/tests/privacy_test.php
Expand Up @@ -70,6 +70,10 @@ public function setUp() {
$this->u2 = $this->getDataGenerator()->create_user(['firstname' => 'a222222222222', 'lastname' => 'a']);
$this->u3 = $this->getDataGenerator()->create_user(['firstname' => 'b333333333333', 'lastname' => 'b']);
$this->u4 = $this->getDataGenerator()->create_user(['firstname' => 'b444444444444', 'lastname' => 'b']);
$this->u5 = $this->getdatagenerator()->create_user(['firstname' => 'a555555555555', 'lastname' => 'a']);
$this->u6 = $this->getdatagenerator()->create_user(['firstname' => 'a666666666666', 'lastname' => 'a']);
$this->u7 = $this->getdatagenerator()->create_user(['firstname' => 'b777777777777', 'lastname' => 'b']);
$this->u8 = $this->getDataGenerator()->create_user(['firstname' => 'b888888888888', 'lastname' => 'b']);

$this->c1 = $this->getDataGenerator()->create_course(['visible' => false]);
$this->c2 = $this->getDataGenerator()->create_course();
Expand All @@ -78,10 +82,18 @@ public function setUp() {
$this->getDataGenerator()->enrol_user($this->u2->id, $this->c1->id, 'student');
$this->getDataGenerator()->enrol_user($this->u3->id, $this->c1->id, 'student');
$this->getDataGenerator()->enrol_user($this->u4->id, $this->c1->id, 'student');
$this->getDataGenerator()->enrol_user($this->u5->id, $this->c1->id, 'student');
$this->getDataGenerator()->enrol_user($this->u6->id, $this->c1->id, 'student');
$this->getDataGenerator()->enrol_user($this->u7->id, $this->c1->id, 'student');
$this->getDataGenerator()->enrol_user($this->u8->id, $this->c1->id, 'student');
$this->getDataGenerator()->enrol_user($this->u1->id, $this->c2->id, 'student');
$this->getDataGenerator()->enrol_user($this->u2->id, $this->c2->id, 'student');
$this->getDataGenerator()->enrol_user($this->u3->id, $this->c2->id, 'student');
$this->getDataGenerator()->enrol_user($this->u4->id, $this->c2->id, 'student');
$this->getDataGenerator()->enrol_user($this->u5->id, $this->c2->id, 'student');
$this->getDataGenerator()->enrol_user($this->u6->id, $this->c2->id, 'student');
$this->getDataGenerator()->enrol_user($this->u7->id, $this->c2->id, 'student');
$this->getDataGenerator()->enrol_user($this->u8->id, $this->c2->id, 'student');

$this->setAdminUser();

Expand Down Expand Up @@ -111,7 +123,8 @@ public function test_get_users_in_context() {
$course1context = \context_course::instance($this->c1->id);
$course2context = \context_course::instance($this->c2->id);
$systemcontext = \context_system::instance();
$expected = [$this->u1->id, $this->u2->id, $this->u3->id, $this->u4->id];
$expected = [$this->u1->id, $this->u2->id, $this->u3->id, $this->u4->id, $this->u5->id, $this->u6->id,
$this->u7->id, $this->u8->id];

// Check users exist in the relevant contexts.
$userlist = new \core_privacy\local\request\userlist($course1context, $component);
Expand Down Expand Up @@ -145,9 +158,9 @@ public function test_get_users_in_context() {
public function test_delete_context_data() {
global $DB;

// We have 2 predictions for model1 and 4 predictions for model2.
$this->assertEquals(6, $DB->count_records('analytics_predictions'));
$this->assertEquals(14, $DB->count_records('analytics_indicator_calc'));
// We have 4 predictions for model1 and 8 predictions for model2.
$this->assertEquals(12, $DB->count_records('analytics_predictions'));
$this->assertEquals(26, $DB->count_records('analytics_indicator_calc'));

// We have 1 prediction action.
$this->assertEquals(1, $DB->count_records('analytics_prediction_actions'));
Expand All @@ -157,8 +170,8 @@ public function test_delete_context_data() {
// Delete the course that was used for prediction.
provider::delete_data_for_all_users_in_context($coursecontext);

// The course predictions are deleted.
$this->assertEquals(4, $DB->count_records('analytics_predictions'));
// The course1 predictions are deleted.
$this->assertEquals(8, $DB->count_records('analytics_predictions'));

// Calculations related to that context are deleted.
$this->assertEmpty($DB->count_records('analytics_indicator_calc', ['contextid' => $coursecontext->id]));
Expand All @@ -181,21 +194,21 @@ public function test_delete_user_data() {
provider::delete_data_for_user($contextlist);

// The site level prediction for u3 was deleted.
$this->assertEquals(3, $DB->count_records('analytics_predictions'));
$this->assertEquals(9, $DB->count_records('analytics_predictions'));
$this->assertEquals(0, $DB->count_records('analytics_prediction_actions'));

$usercontexts = provider::get_contexts_for_userid($this->u1->id);
$contextlist = new \core_privacy\local\request\approved_contextlist($this->u1, 'core_analytics',
$usercontexts->get_contextids());
provider::delete_data_for_user($contextlist);
// We have nothing for u1.
$this->assertEquals(3, $DB->count_records('analytics_predictions'));
$this->assertEquals(9, $DB->count_records('analytics_predictions'));

$usercontexts = provider::get_contexts_for_userid($this->u4->id);
$contextlist = new \core_privacy\local\request\approved_contextlist($this->u4, 'core_analytics',
$usercontexts->get_contextids());
provider::delete_data_for_user($contextlist);
$this->assertEquals(0, $DB->count_records('analytics_predictions'));
$this->assertEquals(6, $DB->count_records('analytics_predictions'));
}

/**
Expand All @@ -218,6 +231,10 @@ public function test_delete_data_for_users() {
$this->u2->id => provider::get_contexts_for_userid($this->u2->id)->get_contextids(),
$this->u3->id => provider::get_contexts_for_userid($this->u3->id)->get_contextids(),
$this->u4->id => provider::get_contexts_for_userid($this->u4->id)->get_contextids(),
$this->u5->id => provider::get_contexts_for_userid($this->u5->id)->get_contextids(),
$this->u6->id => provider::get_contexts_for_userid($this->u6->id)->get_contextids(),
$this->u7->id => provider::get_contexts_for_userid($this->u7->id)->get_contextids(),
$this->u8->id => provider::get_contexts_for_userid($this->u8->id)->get_contextids(),
];

foreach ($actualcontexts as $userid => $unused) {
Expand All @@ -226,9 +243,9 @@ public function test_delete_data_for_users() {
}

// Test initial record counts are as expected.
$this->assertEquals(6, $DB->count_records('analytics_predictions'));
$this->assertEquals(12, $DB->count_records('analytics_predictions'));
$this->assertEquals(1, $DB->count_records('analytics_prediction_actions'));
$this->assertEquals(14, $DB->count_records('analytics_indicator_calc'));
$this->assertEquals(26, $DB->count_records('analytics_indicator_calc'));

// Delete u1 and u3 from system context.
$approveduserids = [$this->u1->id, $this->u3->id];
Expand All @@ -241,13 +258,21 @@ public function test_delete_data_for_users() {
$this->u2->id => [$systemcontext->id, $course1context->id, $course2context->id],
$this->u3->id => [$course1context->id, $course2context->id],
$this->u4->id => [$systemcontext->id, $course1context->id, $course2context->id],
$this->u5->id => [$systemcontext->id, $course1context->id, $course2context->id],
$this->u6->id => [$systemcontext->id, $course1context->id, $course2context->id],
$this->u7->id => [$systemcontext->id, $course1context->id, $course2context->id],
$this->u8->id => [$systemcontext->id, $course1context->id, $course2context->id],
];

$actualcontexts = [
$this->u1->id => provider::get_contexts_for_userid($this->u1->id)->get_contextids(),
$this->u2->id => provider::get_contexts_for_userid($this->u2->id)->get_contextids(),
$this->u3->id => provider::get_contexts_for_userid($this->u3->id)->get_contextids(),
$this->u4->id => provider::get_contexts_for_userid($this->u4->id)->get_contextids(),
$this->u5->id => provider::get_contexts_for_userid($this->u5->id)->get_contextids(),
$this->u6->id => provider::get_contexts_for_userid($this->u6->id)->get_contextids(),
$this->u7->id => provider::get_contexts_for_userid($this->u7->id)->get_contextids(),
$this->u8->id => provider::get_contexts_for_userid($this->u8->id)->get_contextids(),
];

foreach ($actualcontexts as $userid => $unused) {
Expand All @@ -257,12 +282,13 @@ public function test_delete_data_for_users() {
}

// Test expected number of records have been deleted.
$this->assertEquals(5, $DB->count_records('analytics_predictions'));
$this->assertEquals(11, $DB->count_records('analytics_predictions'));
$this->assertEquals(1, $DB->count_records('analytics_prediction_actions'));
$this->assertEquals(12, $DB->count_records('analytics_indicator_calc'));
$this->assertEquals(24, $DB->count_records('analytics_indicator_calc'));

// Delete for all 4 users in course 2 context.
$approveduserids = [$this->u1->id, $this->u2->id, $this->u3->id, $this->u4->id];
// Delete for all 8 users in course 2 context.
$approveduserids = [$this->u1->id, $this->u2->id, $this->u3->id, $this->u4->id, $this->u5->id, $this->u6->id,
$this->u7->id, $this->u8->id];
$approvedlist = new approved_userlist($course2context, $component, $approveduserids);
provider::delete_data_for_users($approvedlist);

Expand All @@ -272,13 +298,21 @@ public function test_delete_data_for_users() {
$this->u2->id => [$systemcontext->id, $course1context->id],
$this->u3->id => [$course1context->id],
$this->u4->id => [$systemcontext->id, $course1context->id],
$this->u5->id => [$systemcontext->id, $course1context->id],
$this->u6->id => [$systemcontext->id, $course1context->id],
$this->u7->id => [$systemcontext->id, $course1context->id],
$this->u8->id => [$systemcontext->id, $course1context->id],
];

$actualcontexts = [
$this->u1->id => provider::get_contexts_for_userid($this->u1->id)->get_contextids(),
$this->u2->id => provider::get_contexts_for_userid($this->u2->id)->get_contextids(),
$this->u3->id => provider::get_contexts_for_userid($this->u3->id)->get_contextids(),
$this->u4->id => provider::get_contexts_for_userid($this->u4->id)->get_contextids(),
$this->u5->id => provider::get_contexts_for_userid($this->u5->id)->get_contextids(),
$this->u6->id => provider::get_contexts_for_userid($this->u6->id)->get_contextids(),
$this->u7->id => provider::get_contexts_for_userid($this->u7->id)->get_contextids(),
$this->u8->id => provider::get_contexts_for_userid($this->u8->id)->get_contextids(),
];

foreach ($actualcontexts as $userid => $unused) {
Expand All @@ -288,9 +322,9 @@ public function test_delete_data_for_users() {
}

// Test expected number of records have been deleted.
$this->assertEquals(3, $DB->count_records('analytics_predictions'));
$this->assertEquals(7, $DB->count_records('analytics_predictions'));
$this->assertEquals(1, $DB->count_records('analytics_prediction_actions'));
$this->assertEquals(8, $DB->count_records('analytics_indicator_calc'));
$this->assertEquals(16, $DB->count_records('analytics_indicator_calc'));

$approveduserids = [$this->u3->id];
$approvedlist = new approved_userlist($course1context, $component, $approveduserids);
Expand All @@ -302,13 +336,21 @@ public function test_delete_data_for_users() {
$this->u2->id => [$systemcontext->id, $course1context->id],
$this->u3->id => [],
$this->u4->id => [$systemcontext->id, $course1context->id],
$this->u5->id => [$systemcontext->id, $course1context->id],
$this->u6->id => [$systemcontext->id, $course1context->id],
$this->u7->id => [$systemcontext->id, $course1context->id],
$this->u8->id => [$systemcontext->id, $course1context->id],
];

$actualcontexts = [
$this->u1->id => provider::get_contexts_for_userid($this->u1->id)->get_contextids(),
$this->u2->id => provider::get_contexts_for_userid($this->u2->id)->get_contextids(),
$this->u3->id => provider::get_contexts_for_userid($this->u3->id)->get_contextids(),
$this->u4->id => provider::get_contexts_for_userid($this->u4->id)->get_contextids(),
$this->u5->id => provider::get_contexts_for_userid($this->u5->id)->get_contextids(),
$this->u6->id => provider::get_contexts_for_userid($this->u6->id)->get_contextids(),
$this->u7->id => provider::get_contexts_for_userid($this->u7->id)->get_contextids(),
$this->u8->id => provider::get_contexts_for_userid($this->u8->id)->get_contextids(),
];
foreach ($actualcontexts as $userid => $unused) {
sort($actualcontexts[$userid]);
Expand All @@ -317,9 +359,9 @@ public function test_delete_data_for_users() {
}

// Test expected number of records have been deleted.
$this->assertEquals(2, $DB->count_records('analytics_predictions'));
$this->assertEquals(6, $DB->count_records('analytics_predictions'));
$this->assertEquals(0, $DB->count_records('analytics_prediction_actions'));
$this->assertEquals(7, $DB->count_records('analytics_indicator_calc'));
$this->assertEquals(15, $DB->count_records('analytics_indicator_calc'));
}

/**
Expand Down
15 changes: 13 additions & 2 deletions lib/mlbackend/php/classes/processor.php
Expand Up @@ -30,6 +30,7 @@
use Phpml\CrossValidation\RandomSplit;
use Phpml\Dataset\ArrayDataset;
use Phpml\ModelManager;
use Phpml\Classification\Linear\LogisticRegression;

/**
* PHP predictions processor.
Expand Down Expand Up @@ -110,7 +111,7 @@ public function train_classification($uniqueid, \stored_file $dataset, $outputdi
if (file_exists($modelfilepath)) {
$classifier = $modelmanager->restoreFromFile($modelfilepath);
} else {
$classifier = new \Phpml\Classification\Linear\LogisticRegression(self::TRAIN_ITERATIONS, Normalizer::NORM_L2);
$classifier = $this->instantiate_algorithm();
}

$fh = $dataset->get_content_file_handle();
Expand Down Expand Up @@ -314,7 +315,7 @@ public function evaluate_classification($uniqueid, $maxdeviation, $niterations,
for ($i = 0; $i < $niterations; $i++) {

if (!$trainedmodeldir) {
$classifier = new \Phpml\Classification\Linear\LogisticRegression(self::TRAIN_ITERATIONS, Normalizer::NORM_L2);
$classifier = $this->instantiate_algorithm();

// Split up the dataset in classifier and testing.
$data = new RandomSplit(new ArrayDataset($samples, $targets), 0.2);
Expand Down Expand Up @@ -561,4 +562,14 @@ protected function extract_metadata($fh) {
$metadata = fgetcsv($fh);
return array_combine($metadata, fgetcsv($fh));
}

/**
* Instantiates the ML algorithm.
*
* @return \Phpml\Classification\Linear\LogisticRegression
*/
protected function instantiate_algorithm(): \Phpml\Classification\Linear\LogisticRegression {
return new LogisticRegression(self::TRAIN_ITERATIONS, true,
LogisticRegression::CONJUGATE_GRAD_TRAINING, 'log');
}
}
2 changes: 1 addition & 1 deletion lib/mlbackend/php/phpml/LICENSE
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2016 Arkadiusz Kondas <arkadiusz.kondas[at]gmail>
Copyright (c) 2016-2018 Arkadiusz Kondas <arkadiusz.kondas[at]gmail>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion lib/mlbackend/php/phpml/readme_moodle.txt
@@ -1,4 +1,4 @@
Current version is 12b8b11
Current version is 0.8.0

# Download latest stable version from https://github.com/php-ai/php-ml
# Remove all files but:
Expand Down

0 comments on commit 4165860

Please sign in to comment.