mirror of
https://github.com/moodle/moodle.git
synced 2025-03-24 09:30:17 +01:00
MDL-61986 mod_lesson: Implement privacy API
This commit is contained in:
parent
6fa694bef0
commit
83ef2b2cf8
584
mod/lesson/classes/privacy/provider.php
Normal file
584
mod/lesson/classes/privacy/provider.php
Normal file
@ -0,0 +1,584 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
* @package mod_lesson
|
||||
* @copyright 2018 Frédéric Massart
|
||||
* @author Frédéric Massart <fred@branchup.tech>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace mod_lesson\privacy;
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
use context;
|
||||
use context_helper;
|
||||
use context_module;
|
||||
use stdClass;
|
||||
use core_privacy\local\metadata\collection;
|
||||
use core_privacy\local\request\approved_contextlist;
|
||||
use core_privacy\local\request\helper;
|
||||
use core_privacy\local\request\transform;
|
||||
use core_privacy\local\request\writer;
|
||||
|
||||
require_once($CFG->dirroot . '/mod/lesson/locallib.php');
|
||||
require_once($CFG->dirroot . '/mod/lesson/pagetypes/essay.php');
|
||||
require_once($CFG->dirroot . '/mod/lesson/pagetypes/matching.php');
|
||||
require_once($CFG->dirroot . '/mod/lesson/pagetypes/multichoice.php');
|
||||
|
||||
/**
|
||||
* Data provider class.
|
||||
*
|
||||
* @package mod_lesson
|
||||
* @copyright 2018 Frédéric Massart
|
||||
* @author Frédéric Massart <fred@branchup.tech>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class provider implements
|
||||
\core_privacy\local\metadata\provider,
|
||||
\core_privacy\local\request\plugin\provider,
|
||||
\core_privacy\local\request\user_preference_provider {
|
||||
|
||||
/**
|
||||
* Returns metadata.
|
||||
*
|
||||
* @param collection $collection The initialised collection to add items to.
|
||||
* @return collection A listing of user data stored through this system.
|
||||
*/
|
||||
public static function get_metadata(collection $collection) : collection {
|
||||
$collection->add_database_table('lesson_attempts', [
|
||||
'userid' => 'privacy:metadata:attempts:userid',
|
||||
'pageid' => 'privacy:metadata:attempts:pageid',
|
||||
'answerid' => 'privacy:metadata:attempts:answerid',
|
||||
'retry' => 'privacy:metadata:attempts:retry',
|
||||
'correct' => 'privacy:metadata:attempts:correct',
|
||||
'useranswer' => 'privacy:metadata:attempts:useranswer',
|
||||
'timeseen' => 'privacy:metadata:attempts:timeseen',
|
||||
], 'privacy:metadata:attempts');
|
||||
|
||||
$collection->add_database_table('lesson_grades', [
|
||||
'userid' => 'privacy:metadata:grades:userid',
|
||||
'grade' => 'privacy:metadata:grades:grade',
|
||||
'completed' => 'privacy:metadata:grades:completed',
|
||||
// The column late is not used.
|
||||
], 'privacy:metadata:grades');
|
||||
|
||||
$collection->add_database_table('lesson_timer', [
|
||||
'userid' => 'privacy:metadata:timer:userid',
|
||||
'starttime' => 'privacy:metadata:timer:starttime',
|
||||
'lessontime' => 'privacy:metadata:timer:lessontime',
|
||||
'completed' => 'privacy:metadata:timer:completed',
|
||||
'timemodifiedoffline' => 'privacy:metadata:timer:timemodifiedoffline',
|
||||
], 'privacy:metadata:timer');
|
||||
|
||||
$collection->add_database_table('lesson_branch', [
|
||||
'userid' => 'privacy:metadata:branch:userid',
|
||||
'pageid' => 'privacy:metadata:branch:pageid',
|
||||
'retry' => 'privacy:metadata:branch:retry',
|
||||
'flag' => 'privacy:metadata:branch:flag',
|
||||
'timeseen' => 'privacy:metadata:branch:timeseen',
|
||||
'nextpageid' => 'privacy:metadata:branch:nextpageid',
|
||||
], 'privacy:metadata:branch');
|
||||
|
||||
$collection->add_database_table('lesson_overrides', [
|
||||
'userid' => 'privacy:metadata:overrides:userid',
|
||||
'available' => 'privacy:metadata:overrides:available',
|
||||
'deadline' => 'privacy:metadata:overrides:deadline',
|
||||
'timelimit' => 'privacy:metadata:overrides:timelimit',
|
||||
'review' => 'privacy:metadata:overrides:review',
|
||||
'maxattempts' => 'privacy:metadata:overrides:maxattempts',
|
||||
'retake' => 'privacy:metadata:overrides:retake',
|
||||
'password' => 'privacy:metadata:overrides:password',
|
||||
], 'privacy:metadata:overrides');
|
||||
|
||||
$collection->add_user_preference('lesson_view', 'privacy:metadata:userpref:lessonview');
|
||||
|
||||
return $collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of contexts that contain user information for the specified user.
|
||||
*
|
||||
* @param int $userid The user to search.
|
||||
* @return contextlist $contextlist The contextlist containing the list of contexts used in this plugin.
|
||||
*/
|
||||
public static function get_contexts_for_userid(int $userid) : \core_privacy\local\request\contextlist {
|
||||
$contextlist = new \core_privacy\local\request\contextlist();
|
||||
|
||||
$sql = "
|
||||
SELECT DISTINCT ctx.id
|
||||
FROM {lesson} l
|
||||
JOIN {modules} m
|
||||
ON m.name = :lesson
|
||||
JOIN {course_modules} cm
|
||||
ON cm.instance = l.id
|
||||
AND cm.module = m.id
|
||||
JOIN {context} ctx
|
||||
ON ctx.instanceid = cm.id
|
||||
AND ctx.contextlevel = :modulelevel
|
||||
LEFT JOIN {lesson_attempts} la
|
||||
ON la.lessonid = l.id
|
||||
LEFT JOIN {lesson_branch} lb
|
||||
ON lb.lessonid = l.id
|
||||
LEFT JOIN {lesson_grades} lg
|
||||
ON lg.lessonid = l.id
|
||||
LEFT JOIN {lesson_overrides} lo
|
||||
ON lo.lessonid = l.id
|
||||
LEFT JOIN {lesson_timer} lt
|
||||
ON lt.lessonid = l.id
|
||||
WHERE la.userid = :userid1
|
||||
OR lb.userid = :userid2
|
||||
OR lg.userid = :userid3
|
||||
OR lt.userid = :userid4
|
||||
OR lo.userid = :userid5";
|
||||
|
||||
$params = [
|
||||
'lesson' => 'lesson',
|
||||
'modulelevel' => CONTEXT_MODULE,
|
||||
'userid1' => $userid,
|
||||
'userid2' => $userid,
|
||||
'userid3' => $userid,
|
||||
'userid4' => $userid,
|
||||
'userid5' => $userid,
|
||||
];
|
||||
$contextlist->add_from_sql($sql, $params);
|
||||
|
||||
return $contextlist;
|
||||
}
|
||||
|
||||
/**
|
||||
* Export all user data for the specified user, in the specified contexts.
|
||||
*
|
||||
* @param approved_contextlist $contextlist The approved contexts to export information for.
|
||||
*/
|
||||
public static function export_user_data(approved_contextlist $contextlist) {
|
||||
global $DB;
|
||||
|
||||
$user = $contextlist->get_user();
|
||||
$userid = $user->id;
|
||||
$cmids = array_reduce($contextlist->get_contexts(), function($carry, $context) {
|
||||
if ($context->contextlevel == CONTEXT_MODULE) {
|
||||
$carry[] = $context->instanceid;
|
||||
}
|
||||
return $carry;
|
||||
}, []);
|
||||
if (empty($cmids)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// If the context export was requested, then let's at least describe the lesson.
|
||||
foreach ($cmids as $cmid) {
|
||||
$context = context_module::instance($cmid);
|
||||
$contextdata = helper::get_context_data($context, $user);
|
||||
helper::export_context_files($context, $user);
|
||||
writer::with_context($context)->export_data([], $contextdata);
|
||||
}
|
||||
|
||||
// Find the lesson IDs.
|
||||
$lessonidstocmids = static::get_lesson_ids_to_cmids_from_cmids($cmids);
|
||||
|
||||
// Prepare the common SQL fragments.
|
||||
list($inlessonsql, $inlessonparams) = $DB->get_in_or_equal(array_keys($lessonidstocmids), SQL_PARAMS_NAMED);
|
||||
$sqluserlesson = "userid = :userid AND lessonid $inlessonsql";
|
||||
$paramsuserlesson = array_merge($inlessonparams, ['userid' => $userid]);
|
||||
|
||||
// Export the overrides.
|
||||
$recordset = $DB->get_recordset_select('lesson_overrides', $sqluserlesson, $paramsuserlesson);
|
||||
static::recordset_loop_and_export($recordset, 'lessonid', null, function($carry, $record) {
|
||||
// We know that there is only one row per lesson, so no need to use $carry.
|
||||
return (object) [
|
||||
'available' => $record->available !== null ? transform::datetime($record->available) : null,
|
||||
'deadline' => $record->deadline !== null ? transform::datetime($record->deadline) : null,
|
||||
'timelimit' => $record->timelimit !== null ? format_time($record->timelimit) : null,
|
||||
'review' => $record->review !== null ? transform::yesno($record->review) : null,
|
||||
'maxattempts' => $record->maxattempts,
|
||||
'retake' => $record->retake !== null ? transform::yesno($record->retake) : null,
|
||||
'password' => $record->password,
|
||||
];
|
||||
}, function($lessonid, $data) use ($lessonidstocmids) {
|
||||
$context = context_module::instance($lessonidstocmids[$lessonid]);
|
||||
writer::with_context($context)->export_related_data([], 'overrides', $data);
|
||||
});
|
||||
|
||||
// Export the grades.
|
||||
$recordset = $DB->get_recordset_select('lesson_grades', $sqluserlesson, $paramsuserlesson, 'lessonid, completed');
|
||||
static::recordset_loop_and_export($recordset, 'lessonid', [], function($carry, $record) {
|
||||
$carry[] = (object) [
|
||||
'grade' => $record->grade,
|
||||
'completed' => transform::datetime($record->completed),
|
||||
];
|
||||
return $carry;
|
||||
}, function($lessonid, $data) use ($lessonidstocmids) {
|
||||
$context = context_module::instance($lessonidstocmids[$lessonid]);
|
||||
writer::with_context($context)->export_related_data([], 'grades', (object) ['grades' => $data]);
|
||||
});
|
||||
|
||||
// Export the timers.
|
||||
$recordset = $DB->get_recordset_select('lesson_timer', $sqluserlesson, $paramsuserlesson, 'lessonid, starttime');
|
||||
static::recordset_loop_and_export($recordset, 'lessonid', [], function($carry, $record) {
|
||||
$carry[] = (object) [
|
||||
'starttime' => transform::datetime($record->starttime),
|
||||
'lastactivity' => transform::datetime($record->lessontime),
|
||||
'completed' => transform::yesno($record->completed),
|
||||
'timemodifiedoffline' => $record->timemodifiedoffline ? transform::datetime($record->timemodifiedoffline) : null,
|
||||
];
|
||||
return $carry;
|
||||
}, function($lessonid, $data) use ($lessonidstocmids) {
|
||||
$context = context_module::instance($lessonidstocmids[$lessonid]);
|
||||
writer::with_context($context)->export_related_data([], 'timers', (object) ['timers' => $data]);
|
||||
});
|
||||
|
||||
// Export the attempts and branches.
|
||||
$sql = "
|
||||
SELECT " . $DB->sql_concat('lp.id', "':'", 'COALESCE(la.id, 0)', "':'", 'COALESCE(lb.id, 0)') . " AS uniqid,
|
||||
lp.lessonid,
|
||||
|
||||
lp.id AS page_id,
|
||||
lp.qtype AS page_qtype,
|
||||
lp.qoption AS page_qoption,
|
||||
lp.title AS page_title,
|
||||
lp.contents AS page_contents,
|
||||
lp.contentsformat AS page_contentsformat,
|
||||
|
||||
la.id AS attempt_id,
|
||||
la.retry AS attempt_retry,
|
||||
la.correct AS attempt_correct,
|
||||
la.useranswer AS attempt_useranswer,
|
||||
la.timeseen AS attempt_timeseen,
|
||||
|
||||
lb.id AS branch_id,
|
||||
lb.retry AS branch_retry,
|
||||
lb.timeseen AS branch_timeseen,
|
||||
|
||||
lpb.id AS nextpage_id,
|
||||
lpb.title AS nextpage_title
|
||||
|
||||
FROM {lesson_pages} lp
|
||||
LEFT JOIN {lesson_attempts} la
|
||||
ON la.pageid = lp.id
|
||||
AND la.userid = :userid1
|
||||
LEFT JOIN {lesson_branch} lb
|
||||
ON lb.pageid = lp.id
|
||||
AND lb.userid = :userid2
|
||||
LEFT JOIN {lesson_pages} lpb
|
||||
ON lpb.id = lb.nextpageid
|
||||
WHERE lp.lessonid $inlessonsql
|
||||
AND (la.id IS NOT NULL OR lb.id IS NOT NULL)
|
||||
ORDER BY lp.lessonid, lp.id, la.retry, lb.retry, la.id, lb.id";
|
||||
$params = array_merge($inlessonparams, ['userid1' => $userid, 'userid2' => $userid]);
|
||||
|
||||
$recordset = $DB->get_recordset_sql($sql, $params);
|
||||
static::recordset_loop_and_export($recordset, 'lessonid', [], function($carry, $record) use ($lessonidstocmids) {
|
||||
$context = context_module::instance($lessonidstocmids[$record->lessonid]);
|
||||
$options = ['context' => $context];
|
||||
|
||||
$take = isset($record->attempt_retry) ? $record->attempt_retry : $record->branch_retry;
|
||||
if (!isset($carry[$take])) {
|
||||
$carry[$take] = (object) [
|
||||
'number' => $take + 1,
|
||||
'answers' => [],
|
||||
'jumps' => []
|
||||
];
|
||||
}
|
||||
|
||||
$pagefilespath = [get_string('privacy:path:pages', 'mod_lesson'), $record->page_id];
|
||||
writer::with_context($context)->export_area_files($pagefilespath, 'mod_lesson', 'page_contents', $record->page_id);
|
||||
$pagecontents = format_text(
|
||||
writer::with_context($context)->rewrite_pluginfile_urls(
|
||||
$pagefilespath,
|
||||
'mod_lesson',
|
||||
'page_contents',
|
||||
$record->page_id,
|
||||
$record->page_contents
|
||||
),
|
||||
$record->page_contentsformat,
|
||||
$options
|
||||
);
|
||||
|
||||
$pagebase = [
|
||||
'id' => $record->page_id,
|
||||
'page' => $record->page_title,
|
||||
'contents' => $pagecontents,
|
||||
'contents_files_folder' => implode('/', $pagefilespath)
|
||||
];
|
||||
|
||||
if (isset($record->attempt_id)) {
|
||||
$carry[$take]->answers[] = array_merge($pagebase, static::transform_attempt($record, $context));
|
||||
|
||||
} else if (isset($record->branch_id)) {
|
||||
if (!empty($record->nextpage_id)) {
|
||||
$wentto = $record->nextpage_title . " (id: {$record->nextpage_id})";
|
||||
} else {
|
||||
$wentto = get_string('endoflesson', 'mod_lesson');
|
||||
}
|
||||
$carry[$take]->jumps[] = array_merge($pagebase, [
|
||||
'went_to' => $wentto,
|
||||
'timeseen' => transform::datetime($record->attempt_timeseen)
|
||||
]);
|
||||
}
|
||||
|
||||
return $carry;
|
||||
|
||||
}, function($lessonid, $data) use ($lessonidstocmids) {
|
||||
$context = context_module::instance($lessonidstocmids[$lessonid]);
|
||||
writer::with_context($context)->export_related_data([], 'attempts', (object) [
|
||||
'attempts' => array_values($data)
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Export all user preferences for the plugin.
|
||||
*
|
||||
* @param int $userid The userid of the user whose data is to be exported.
|
||||
*/
|
||||
public static function export_user_preferences(int $userid) {
|
||||
$lessonview = get_user_preferences('lesson_view', null, $userid);
|
||||
if ($lessonview !== null) {
|
||||
$value = $lessonview;
|
||||
|
||||
// The code seems to indicate that there also is the option 'simple', but it's not
|
||||
// described nor accessible from anywhere so we won't describe it more than being 'simple'.
|
||||
if ($lessonview == 'full') {
|
||||
$value = get_string('full', 'mod_lesson');
|
||||
} else if ($lessonview == 'collapsed') {
|
||||
$value = get_string('collapsed', 'mod_lesson');
|
||||
}
|
||||
|
||||
writer::export_user_preference('mod_lesson', 'lesson_view', $lessonview,
|
||||
get_string('privacy:metadata:userpref:lessonview', 'mod_lesson'));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all data for all users in the specified context.
|
||||
*
|
||||
* @param context $context The specific context to delete data for.
|
||||
*/
|
||||
public static function delete_data_for_all_users_in_context(context $context) {
|
||||
global $DB;
|
||||
|
||||
if ($context->contextlevel != CONTEXT_MODULE) {
|
||||
return;
|
||||
}
|
||||
|
||||
$lessonid = static::get_lesson_id_from_context($context);
|
||||
|
||||
$DB->delete_records('lesson_attempts', ['lessonid' => $lessonid]);
|
||||
$DB->delete_records('lesson_branch', ['lessonid' => $lessonid]);
|
||||
$DB->delete_records('lesson_grades', ['lessonid' => $lessonid]);
|
||||
$DB->delete_records('lesson_timer', ['lessonid' => $lessonid]);
|
||||
$DB->delete_records_select('lesson_overrides', 'lessonid = :id AND userid IS NOT NULL', ['id' => $lessonid]);
|
||||
|
||||
$fs = get_file_storage();
|
||||
$fs->delete_area_files($context->id, 'mod_lesson', 'essay_responses');
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all user data for the specified user, in the specified contexts.
|
||||
*
|
||||
* @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
|
||||
*/
|
||||
public static function delete_data_for_user(approved_contextlist $contextlist) {
|
||||
global $DB;
|
||||
|
||||
$userid = $contextlist->get_user()->id;
|
||||
$cmids = array_reduce($contextlist->get_contexts(), function($carry, $context) {
|
||||
if ($context->contextlevel == CONTEXT_MODULE) {
|
||||
$carry[] = $context->instanceid;
|
||||
}
|
||||
return $carry;
|
||||
}, []);
|
||||
if (empty($cmids)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Find the lesson IDs.
|
||||
$lessonidstocmids = static::get_lesson_ids_to_cmids_from_cmids($cmids);
|
||||
$lessonids = array_keys($lessonidstocmids);
|
||||
if (empty($lessonids)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Prepare the SQL we'll need below.
|
||||
list($insql, $inparams) = $DB->get_in_or_equal($lessonids, SQL_PARAMS_NAMED);
|
||||
$sql = "lessonid $insql AND userid = :userid";
|
||||
$params = array_merge($inparams, ['userid' => $userid]);
|
||||
|
||||
// Delete the attempt files.
|
||||
$fs = get_file_storage();
|
||||
$recordset = $DB->get_recordset_select('lesson_attempts', $sql, $params, '', 'id, lessonid');
|
||||
foreach ($recordset as $record) {
|
||||
$cmid = $lessonidstocmids[$record->lessonid];
|
||||
$context = context_module::instance($cmid);
|
||||
$fs->delete_area_files($context->id, 'mod_lesson', 'essay_responses', $record->id);
|
||||
}
|
||||
$recordset->close();
|
||||
|
||||
// Delete all the things.
|
||||
$DB->delete_records_select('lesson_attempts', $sql, $params);
|
||||
$DB->delete_records_select('lesson_branch', $sql, $params);
|
||||
$DB->delete_records_select('lesson_grades', $sql, $params);
|
||||
$DB->delete_records_select('lesson_timer', $sql, $params);
|
||||
$DB->delete_records_select('lesson_overrides', $sql, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a survey ID from its context.
|
||||
*
|
||||
* @param context_module $context The module context.
|
||||
* @return int
|
||||
*/
|
||||
protected static function get_lesson_id_from_context(context_module $context) {
|
||||
$cm = get_coursemodule_from_id('lesson', $context->instanceid);
|
||||
return $cm ? (int) $cm->instance : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a dict of lesson IDs mapped to their course module ID.
|
||||
*
|
||||
* @param array $cmids The course module IDs.
|
||||
* @return array In the form of [$lessonid => $cmid].
|
||||
*/
|
||||
protected static function get_lesson_ids_to_cmids_from_cmids(array $cmids) {
|
||||
global $DB;
|
||||
list($insql, $inparams) = $DB->get_in_or_equal($cmids, SQL_PARAMS_NAMED);
|
||||
$sql = "
|
||||
SELECT l.id, cm.id AS cmid
|
||||
FROM {lesson} l
|
||||
JOIN {modules} m
|
||||
ON m.name = :lesson
|
||||
JOIN {course_modules} cm
|
||||
ON cm.instance = l.id
|
||||
AND cm.module = m.id
|
||||
WHERE cm.id $insql";
|
||||
$params = array_merge($inparams, ['lesson' => 'lesson']);
|
||||
return $DB->get_records_sql_menu($sql, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loop and export from a recordset.
|
||||
*
|
||||
* @param moodle_recordset $recordset The recordset.
|
||||
* @param string $splitkey The record key to determine when to export.
|
||||
* @param mixed $initial The initial data to reduce from.
|
||||
* @param callable $reducer The function to return the dataset, receives current dataset, and the current record.
|
||||
* @param callable $export The function to export the dataset, receives the last value from $splitkey and the dataset.
|
||||
* @return void
|
||||
*/
|
||||
protected static function recordset_loop_and_export(\moodle_recordset $recordset, $splitkey, $initial,
|
||||
callable $reducer, callable $export) {
|
||||
|
||||
$data = $initial;
|
||||
$lastid = null;
|
||||
|
||||
foreach ($recordset as $record) {
|
||||
if ($lastid && $record->{$splitkey} != $lastid) {
|
||||
$export($lastid, $data);
|
||||
$data = $initial;
|
||||
}
|
||||
$data = $reducer($data, $record);
|
||||
$lastid = $record->{$splitkey};
|
||||
}
|
||||
$recordset->close();
|
||||
|
||||
if (!empty($lastid)) {
|
||||
$export($lastid, $data);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform an attempt.
|
||||
*
|
||||
* @param stdClass $data Data from the database, as per the exporting method.
|
||||
* @param context_module $context The module context.
|
||||
* @return array
|
||||
*/
|
||||
protected static function transform_attempt(stdClass $data, context_module $context) {
|
||||
global $DB;
|
||||
|
||||
$options = ['context' => $context];
|
||||
$answer = $data->attempt_useranswer;
|
||||
$response = null;
|
||||
$responsefilesfolder = null;
|
||||
|
||||
if ($answer !== null) {
|
||||
if ($data->page_qtype == LESSON_PAGE_ESSAY) {
|
||||
// Essay questions serialise data in the answer field.
|
||||
$info = \lesson_page_type_essay::extract_useranswer($answer);
|
||||
$answer = format_text($info->answer, $info->answerformat, $options);
|
||||
if ($info->response !== null) {
|
||||
// We export the files in a subfolder to avoid conflicting files, and tell the user
|
||||
// where those files were exported. That is because we are not using a subfolder for
|
||||
// every single essay response.
|
||||
$responsefilespath = [get_string('privacy:path:essayresponses', 'mod_lesson'), $data->attempt_id];
|
||||
$responsefilesfolder = implode('/', $responsefilespath);
|
||||
$response = format_text(
|
||||
writer::with_context($context)->rewrite_pluginfile_urls(
|
||||
$responsefilespath,
|
||||
'mod_lesson',
|
||||
'essay_responses',
|
||||
$data->attempt_id,
|
||||
$info->response
|
||||
),
|
||||
$info->responseformat,
|
||||
$options
|
||||
);
|
||||
writer::with_context($context)->export_area_files($responsefilespath, 'mod_lesson',
|
||||
'essay_responses', $data->page_id);
|
||||
}
|
||||
|
||||
} else if ($data->page_qtype == LESSON_PAGE_MULTICHOICE && $data->page_qoption) {
|
||||
// Multiple choice quesitons with multiple answers encode the answers.
|
||||
list($insql, $inparams) = $DB->get_in_or_equal(explode(',', $answer), SQL_PARAMS_NAMED);
|
||||
$records = $DB->get_records_select('lesson_answers', "id $insql", $inparams, 'id, answer, answerformat');
|
||||
$answer = array_values(array_map(function($record) use ($options) {
|
||||
return format_text($record->answer, $record->answerformat, $options);
|
||||
}, empty($records) ? [] : $records));
|
||||
|
||||
} else if ($data->page_qtype == LESSON_PAGE_MATCHING) {
|
||||
// Matching questions need sorting.
|
||||
$chosen = explode(',', $answer);
|
||||
$answers = $DB->get_records_select('lesson_answers', 'pageid = :pageid', ['pageid' => $data->page_id],
|
||||
'id', 'id, answer, answerformat', 2); // The two first entries are not options.
|
||||
$i = -1;
|
||||
$answer = array_values(array_map(function($record) use (&$i, $chosen, $options) {
|
||||
$i++;
|
||||
return [
|
||||
'label' => format_text($record->answer, $record->answerformat, $options),
|
||||
'matched_with' => array_key_exists($i, $chosen) ? $chosen[$i] : null
|
||||
];
|
||||
}, empty($answers) ? [] : $answers));
|
||||
}
|
||||
}
|
||||
|
||||
$result = [
|
||||
'answer' => $answer,
|
||||
'correct' => transform::yesno($data->attempt_correct),
|
||||
'timeseen' => transform::datetime($data->attempt_timeseen),
|
||||
];
|
||||
|
||||
if ($response !== null) {
|
||||
$result['response'] = $response;
|
||||
$result['response_files_folder'] = $responsefilesfolder;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
}
|
@ -438,6 +438,43 @@ $string['preview'] = 'Preview';
|
||||
$string['previewlesson'] = 'Preview {$a}';
|
||||
$string['previewpagenamed'] = 'Preview page: {$a}';
|
||||
$string['previouspage'] = 'Previous page';
|
||||
$string['privacy:metadata:attempts:userid'] = 'The user ID';
|
||||
$string['privacy:metadata:attempts:pageid'] = 'The page ID';
|
||||
$string['privacy:metadata:attempts:answerid'] = 'The answer ID';
|
||||
$string['privacy:metadata:attempts:retry'] = 'The attempt number';
|
||||
$string['privacy:metadata:attempts:correct'] = 'Whether the attempt was correct';
|
||||
$string['privacy:metadata:attempts:useranswer'] = 'Details about the user\'s answer';
|
||||
$string['privacy:metadata:attempts:timeseen'] = 'Time at which the attempt was made';
|
||||
$string['privacy:metadata:attempts'] = 'A record of page attempts';
|
||||
$string['privacy:metadata:grades:userid'] = 'The user ID';
|
||||
$string['privacy:metadata:grades:grade'] = 'The grade given';
|
||||
$string['privacy:metadata:grades:completed'] = 'The date at which the grade was given';
|
||||
$string['privacy:metadata:grades'] = 'A record of the grades for each lesson';
|
||||
$string['privacy:metadata:timer:userid'] = 'The user ID';
|
||||
$string['privacy:metadata:timer:starttime'] = 'The date at which the attempt started';
|
||||
$string['privacy:metadata:timer:lessontime'] = 'The last moment when we recorded activity';
|
||||
$string['privacy:metadata:timer:completed'] = 'Whether the attempt is complete';
|
||||
$string['privacy:metadata:timer:timemodifiedoffline'] = 'The last moment when we recorded activity from the mobile app';
|
||||
$string['privacy:metadata:timer'] = 'A record of a lesson attempt';
|
||||
$string['privacy:metadata:branch:userid'] = 'The user ID';
|
||||
$string['privacy:metadata:branch:pageid'] = 'The page ID';
|
||||
$string['privacy:metadata:branch:retry'] = 'The attempt number';
|
||||
$string['privacy:metadata:branch:flag'] = 'Whether the next page was calculated randomely';
|
||||
$string['privacy:metadata:branch:timeseen'] = 'Time at which the page was viewed ';
|
||||
$string['privacy:metadata:branch:nextpageid'] = 'The next page ID';
|
||||
$string['privacy:metadata:branch'] = 'A record of the pages viewed';
|
||||
$string['privacy:metadata:overrides:userid'] = 'The user ID';
|
||||
$string['privacy:metadata:overrides:available'] = 'Time at which the students can start attempting the lesson';
|
||||
$string['privacy:metadata:overrides:deadline'] = 'Time by which students must have completed their attempt';
|
||||
$string['privacy:metadata:overrides:timelimit'] = 'Time limit to complete the lesson, in seconds.';
|
||||
$string['privacy:metadata:overrides:review'] = 'Whether trying a question again is allowed';
|
||||
$string['privacy:metadata:overrides:maxattempts'] = 'The maximium number of attempts';
|
||||
$string['privacy:metadata:overrides:retake'] = 'Whether re-takes are allowed';
|
||||
$string['privacy:metadata:overrides:password'] = 'The password to access the lesson';
|
||||
$string['privacy:metadata:overrides'] = 'A record of overrides per lesson';
|
||||
$string['privacy:metadata:userpref:lessonview'] = 'The preferred display mode when editing lessons';
|
||||
$string['privacy:path:essayresponses'] = 'Essay responses';
|
||||
$string['privacy:path:pages'] = 'Pages';
|
||||
$string['processerror'] = 'Error occurred during processing!';
|
||||
$string['progressbar'] = 'Progress bar';
|
||||
$string['progressbar_help'] = 'If enabled, a bar is displayed at the bottom of lesson pages showing approximate percentage of completion.';
|
||||
|
756
mod/lesson/tests/privacy_test.php
Normal file
756
mod/lesson/tests/privacy_test.php
Normal file
@ -0,0 +1,756 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Data provider tests.
|
||||
*
|
||||
* @package mod_lesson
|
||||
* @category test
|
||||
* @copyright 2018 Frédéric Massart
|
||||
* @author Frédéric Massart <fred@branchup.tech>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
global $CFG;
|
||||
|
||||
use core_privacy\tests\provider_testcase;
|
||||
use core_privacy\local\request\approved_contextlist;
|
||||
use core_privacy\local\request\transform;
|
||||
use core_privacy\local\request\writer;
|
||||
use mod_lesson\privacy\provider;
|
||||
|
||||
/**
|
||||
* Data provider testcase class.
|
||||
*
|
||||
* @package mod_lesson
|
||||
* @category test
|
||||
* @copyright 2018 Frédéric Massart
|
||||
* @author Frédéric Massart <fred@branchup.tech>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class mod_lesson_privacy_testcase extends provider_testcase {
|
||||
|
||||
public function setUp() {
|
||||
global $PAGE;
|
||||
$this->setAdminUser(); // The data generator complains without this.
|
||||
$this->resetAfterTest();
|
||||
$PAGE->get_renderer('core');
|
||||
}
|
||||
|
||||
public function test_get_contexts_for_userid() {
|
||||
$dg = $this->getDataGenerator();
|
||||
$c1 = $dg->create_course();
|
||||
|
||||
$u1 = $dg->create_user();
|
||||
$u2 = $dg->create_user();
|
||||
$u3 = $dg->create_user();
|
||||
$u4 = $dg->create_user();
|
||||
$u5 = $dg->create_user();
|
||||
$u6 = $dg->create_user();
|
||||
|
||||
$cm1 = $dg->create_module('lesson', ['course' => $c1]);
|
||||
$cm2 = $dg->create_module('lesson', ['course' => $c1]);
|
||||
$cm3 = $dg->create_module('lesson', ['course' => $c1]);
|
||||
$cm1ctx = context_module::instance($cm1->cmid);
|
||||
$cm2ctx = context_module::instance($cm2->cmid);
|
||||
$cm3ctx = context_module::instance($cm3->cmid);
|
||||
|
||||
$this->create_attempt($cm1, $u1);
|
||||
$this->create_grade($cm2, $u2);
|
||||
$this->create_timer($cm3, $u3);
|
||||
$this->create_branch($cm2, $u4);
|
||||
$this->create_override($cm1, $u5);
|
||||
|
||||
$this->create_attempt($cm2, $u6);
|
||||
$this->create_grade($cm2, $u6);
|
||||
$this->create_timer($cm1, $u6);
|
||||
$this->create_branch($cm2, $u6);
|
||||
$this->create_override($cm3, $u6);
|
||||
|
||||
$contextids = provider::get_contexts_for_userid($u1->id)->get_contextids();
|
||||
$this->assertCount(1, $contextids);
|
||||
$this->assertTrue(in_array($cm1ctx->id, $contextids));
|
||||
|
||||
$contextids = provider::get_contexts_for_userid($u2->id)->get_contextids();
|
||||
$this->assertCount(1, $contextids);
|
||||
$this->assertTrue(in_array($cm2ctx->id, $contextids));
|
||||
|
||||
$contextids = provider::get_contexts_for_userid($u3->id)->get_contextids();
|
||||
$this->assertCount(1, $contextids);
|
||||
$this->assertTrue(in_array($cm3ctx->id, $contextids));
|
||||
|
||||
$contextids = provider::get_contexts_for_userid($u4->id)->get_contextids();
|
||||
$this->assertCount(1, $contextids);
|
||||
$this->assertTrue(in_array($cm2ctx->id, $contextids));
|
||||
|
||||
$contextids = provider::get_contexts_for_userid($u5->id)->get_contextids();
|
||||
$this->assertCount(1, $contextids);
|
||||
$this->assertTrue(in_array($cm1ctx->id, $contextids));
|
||||
|
||||
$contextids = provider::get_contexts_for_userid($u6->id)->get_contextids();
|
||||
$this->assertCount(3, $contextids);
|
||||
$this->assertTrue(in_array($cm1ctx->id, $contextids));
|
||||
$this->assertTrue(in_array($cm2ctx->id, $contextids));
|
||||
$this->assertTrue(in_array($cm3ctx->id, $contextids));
|
||||
}
|
||||
|
||||
public function test_delete_data_for_all_users_in_context() {
|
||||
global $DB;
|
||||
$dg = $this->getDataGenerator();
|
||||
$c1 = $dg->create_course();
|
||||
$u1 = $dg->create_user();
|
||||
$u2 = $dg->create_user();
|
||||
|
||||
$cm1 = $dg->create_module('lesson', ['course' => $c1]);
|
||||
$cm2 = $dg->create_module('lesson', ['course' => $c1]);
|
||||
$cm3 = $dg->create_module('lesson', ['course' => $c1]);
|
||||
|
||||
$c1ctx = context_course::instance($c1->id);
|
||||
$cm1ctx = context_module::instance($cm1->cmid);
|
||||
$cm2ctx = context_module::instance($cm2->cmid);
|
||||
$cm3ctx = context_module::instance($cm3->cmid);
|
||||
|
||||
$this->create_attempt($cm1, $u1);
|
||||
$this->create_grade($cm1, $u1);
|
||||
$this->create_timer($cm1, $u1);
|
||||
$this->create_branch($cm1, $u1);
|
||||
$this->create_override($cm1, $u1);
|
||||
|
||||
$this->create_attempt($cm1, $u2);
|
||||
$this->create_grade($cm1, $u2);
|
||||
$this->create_timer($cm1, $u2);
|
||||
$this->create_branch($cm1, $u2);
|
||||
$this->create_override($cm1, $u2);
|
||||
|
||||
$this->create_attempt($cm2, $u1);
|
||||
$this->create_grade($cm2, $u1);
|
||||
$this->create_timer($cm2, $u1);
|
||||
$this->create_branch($cm2, $u1);
|
||||
$this->create_override($cm2, $u1);
|
||||
$this->create_attempt($cm2, $u2);
|
||||
$this->create_grade($cm2, $u2);
|
||||
$this->create_timer($cm2, $u2);
|
||||
$this->create_branch($cm2, $u2);
|
||||
$this->create_override($cm2, $u2);
|
||||
|
||||
$assertcm1nochange = function() use ($DB, $u1, $u2, $cm1) {
|
||||
$this->assertTrue($DB->record_exists('lesson_attempts', ['userid' => $u1->id, 'lessonid' => $cm1->id]));
|
||||
$this->assertTrue($DB->record_exists('lesson_grades', ['userid' => $u1->id, 'lessonid' => $cm1->id]));
|
||||
$this->assertTrue($DB->record_exists('lesson_timer', ['userid' => $u1->id, 'lessonid' => $cm1->id]));
|
||||
$this->assertTrue($DB->record_exists('lesson_branch', ['userid' => $u1->id, 'lessonid' => $cm1->id]));
|
||||
$this->assertTrue($DB->record_exists('lesson_overrides', ['userid' => $u1->id, 'lessonid' => $cm1->id]));
|
||||
$this->assertTrue($DB->record_exists('lesson_attempts', ['userid' => $u2->id, 'lessonid' => $cm1->id]));
|
||||
$this->assertTrue($DB->record_exists('lesson_grades', ['userid' => $u2->id, 'lessonid' => $cm1->id]));
|
||||
$this->assertTrue($DB->record_exists('lesson_timer', ['userid' => $u2->id, 'lessonid' => $cm1->id]));
|
||||
$this->assertTrue($DB->record_exists('lesson_branch', ['userid' => $u2->id, 'lessonid' => $cm1->id]));
|
||||
$this->assertTrue($DB->record_exists('lesson_overrides', ['userid' => $u2->id, 'lessonid' => $cm1->id]));
|
||||
};
|
||||
$assertcm2nochange = function() use ($DB, $u1, $u2, $cm2) {
|
||||
$this->assertTrue($DB->record_exists('lesson_attempts', ['userid' => $u1->id, 'lessonid' => $cm2->id]));
|
||||
$this->assertTrue($DB->record_exists('lesson_grades', ['userid' => $u1->id, 'lessonid' => $cm2->id]));
|
||||
$this->assertTrue($DB->record_exists('lesson_timer', ['userid' => $u1->id, 'lessonid' => $cm2->id]));
|
||||
$this->assertTrue($DB->record_exists('lesson_branch', ['userid' => $u1->id, 'lessonid' => $cm2->id]));
|
||||
$this->assertTrue($DB->record_exists('lesson_overrides', ['userid' => $u1->id, 'lessonid' => $cm2->id]));
|
||||
$this->assertTrue($DB->record_exists('lesson_attempts', ['userid' => $u2->id, 'lessonid' => $cm2->id]));
|
||||
$this->assertTrue($DB->record_exists('lesson_grades', ['userid' => $u2->id, 'lessonid' => $cm2->id]));
|
||||
$this->assertTrue($DB->record_exists('lesson_timer', ['userid' => $u2->id, 'lessonid' => $cm2->id]));
|
||||
$this->assertTrue($DB->record_exists('lesson_branch', ['userid' => $u2->id, 'lessonid' => $cm2->id]));
|
||||
$this->assertTrue($DB->record_exists('lesson_overrides', ['userid' => $u2->id, 'lessonid' => $cm2->id]));
|
||||
};
|
||||
|
||||
// Confirm existing state.
|
||||
$assertcm1nochange();
|
||||
$assertcm2nochange();
|
||||
|
||||
// Delete the course: no change.
|
||||
provider::delete_data_for_all_users_in_context(context_course::instance($c1->id));
|
||||
$assertcm1nochange();
|
||||
$assertcm2nochange();
|
||||
|
||||
// Delete another module: no change.
|
||||
provider::delete_data_for_all_users_in_context(context_module::instance($cm3->cmid));
|
||||
$assertcm1nochange();
|
||||
$assertcm2nochange();
|
||||
|
||||
// Delete cm1: no change in cm2.
|
||||
provider::delete_data_for_all_users_in_context(context_module::instance($cm1->cmid));
|
||||
$assertcm2nochange();
|
||||
$this->assertFalse($DB->record_exists('lesson_attempts', ['userid' => $u1->id, 'lessonid' => $cm1->id]));
|
||||
$this->assertFalse($DB->record_exists('lesson_grades', ['userid' => $u1->id, 'lessonid' => $cm1->id]));
|
||||
$this->assertFalse($DB->record_exists('lesson_timer', ['userid' => $u1->id, 'lessonid' => $cm1->id]));
|
||||
$this->assertFalse($DB->record_exists('lesson_branch', ['userid' => $u1->id, 'lessonid' => $cm1->id]));
|
||||
$this->assertFalse($DB->record_exists('lesson_overrides', ['userid' => $u1->id, 'lessonid' => $cm1->id]));
|
||||
$this->assertFalse($DB->record_exists('lesson_attempts', ['userid' => $u2->id, 'lessonid' => $cm1->id]));
|
||||
$this->assertFalse($DB->record_exists('lesson_grades', ['userid' => $u2->id, 'lessonid' => $cm1->id]));
|
||||
$this->assertFalse($DB->record_exists('lesson_timer', ['userid' => $u2->id, 'lessonid' => $cm1->id]));
|
||||
$this->assertFalse($DB->record_exists('lesson_branch', ['userid' => $u2->id, 'lessonid' => $cm1->id]));
|
||||
$this->assertFalse($DB->record_exists('lesson_overrides', ['userid' => $u2->id, 'lessonid' => $cm1->id]));
|
||||
}
|
||||
|
||||
public function test_delete_data_for_user() {
|
||||
global $DB;
|
||||
$dg = $this->getDataGenerator();
|
||||
$c1 = $dg->create_course();
|
||||
$u1 = $dg->create_user();
|
||||
$u2 = $dg->create_user();
|
||||
|
||||
$cm1 = $dg->create_module('lesson', ['course' => $c1]);
|
||||
$cm2 = $dg->create_module('lesson', ['course' => $c1]);
|
||||
$cm3 = $dg->create_module('lesson', ['course' => $c1]);
|
||||
|
||||
$c1ctx = context_course::instance($c1->id);
|
||||
$cm1ctx = context_module::instance($cm1->cmid);
|
||||
$cm2ctx = context_module::instance($cm2->cmid);
|
||||
$cm3ctx = context_module::instance($cm3->cmid);
|
||||
|
||||
$this->create_attempt($cm1, $u1);
|
||||
$this->create_grade($cm1, $u1);
|
||||
$this->create_timer($cm1, $u1);
|
||||
$this->create_branch($cm1, $u1);
|
||||
$this->create_override($cm1, $u1);
|
||||
$this->create_attempt($cm1, $u2);
|
||||
$this->create_grade($cm1, $u2);
|
||||
$this->create_timer($cm1, $u2);
|
||||
$this->create_branch($cm1, $u2);
|
||||
$this->create_override($cm1, $u2);
|
||||
|
||||
$this->create_attempt($cm2, $u1);
|
||||
$this->create_grade($cm2, $u1);
|
||||
$this->create_timer($cm2, $u1);
|
||||
$this->create_branch($cm2, $u1);
|
||||
$this->create_override($cm2, $u1);
|
||||
$this->create_attempt($cm2, $u2);
|
||||
$this->create_grade($cm2, $u2);
|
||||
$this->create_timer($cm2, $u2);
|
||||
$this->create_branch($cm2, $u2);
|
||||
$this->create_override($cm2, $u2);
|
||||
|
||||
$assertu1nochange = function() use ($DB, $u1, $cm1, $cm2) {
|
||||
$this->assertTrue($DB->record_exists('lesson_attempts', ['userid' => $u1->id, 'lessonid' => $cm1->id]));
|
||||
$this->assertTrue($DB->record_exists('lesson_grades', ['userid' => $u1->id, 'lessonid' => $cm1->id]));
|
||||
$this->assertTrue($DB->record_exists('lesson_timer', ['userid' => $u1->id, 'lessonid' => $cm1->id]));
|
||||
$this->assertTrue($DB->record_exists('lesson_branch', ['userid' => $u1->id, 'lessonid' => $cm1->id]));
|
||||
$this->assertTrue($DB->record_exists('lesson_overrides', ['userid' => $u1->id, 'lessonid' => $cm1->id]));
|
||||
$this->assertTrue($DB->record_exists('lesson_attempts', ['userid' => $u1->id, 'lessonid' => $cm2->id]));
|
||||
$this->assertTrue($DB->record_exists('lesson_grades', ['userid' => $u1->id, 'lessonid' => $cm2->id]));
|
||||
$this->assertTrue($DB->record_exists('lesson_timer', ['userid' => $u1->id, 'lessonid' => $cm2->id]));
|
||||
$this->assertTrue($DB->record_exists('lesson_branch', ['userid' => $u1->id, 'lessonid' => $cm2->id]));
|
||||
$this->assertTrue($DB->record_exists('lesson_overrides', ['userid' => $u1->id, 'lessonid' => $cm2->id]));
|
||||
};
|
||||
$assertu2nochange = function() use ($DB, $u2, $cm1, $cm2) {
|
||||
$this->assertTrue($DB->record_exists('lesson_attempts', ['userid' => $u2->id, 'lessonid' => $cm1->id]));
|
||||
$this->assertTrue($DB->record_exists('lesson_grades', ['userid' => $u2->id, 'lessonid' => $cm1->id]));
|
||||
$this->assertTrue($DB->record_exists('lesson_timer', ['userid' => $u2->id, 'lessonid' => $cm1->id]));
|
||||
$this->assertTrue($DB->record_exists('lesson_branch', ['userid' => $u2->id, 'lessonid' => $cm1->id]));
|
||||
$this->assertTrue($DB->record_exists('lesson_overrides', ['userid' => $u2->id, 'lessonid' => $cm1->id]));
|
||||
$this->assertTrue($DB->record_exists('lesson_attempts', ['userid' => $u2->id, 'lessonid' => $cm2->id]));
|
||||
$this->assertTrue($DB->record_exists('lesson_grades', ['userid' => $u2->id, 'lessonid' => $cm2->id]));
|
||||
$this->assertTrue($DB->record_exists('lesson_timer', ['userid' => $u2->id, 'lessonid' => $cm2->id]));
|
||||
$this->assertTrue($DB->record_exists('lesson_branch', ['userid' => $u2->id, 'lessonid' => $cm2->id]));
|
||||
$this->assertTrue($DB->record_exists('lesson_overrides', ['userid' => $u2->id, 'lessonid' => $cm2->id]));
|
||||
};
|
||||
|
||||
// Confirm existing state.
|
||||
$assertu1nochange();
|
||||
$assertu2nochange();
|
||||
|
||||
// Delete the course: no change.
|
||||
provider::delete_data_for_user(new approved_contextlist($u1, 'mod_lesson', [context_course::instance($c1->id)->id]));
|
||||
$assertu1nochange();
|
||||
$assertu2nochange();
|
||||
|
||||
// Delete another module: no change.
|
||||
provider::delete_data_for_user(new approved_contextlist($u1, 'mod_lesson', [context_module::instance($cm3->cmid)->id]));
|
||||
$assertu1nochange();
|
||||
$assertu2nochange();
|
||||
|
||||
// Delete u1 in cm1: no change for u2 and in cm2.
|
||||
provider::delete_data_for_user(new approved_contextlist($u1, 'mod_lesson', [context_module::instance($cm1->cmid)->id]));
|
||||
$assertu2nochange();
|
||||
$this->assertFalse($DB->record_exists('lesson_attempts', ['userid' => $u1->id, 'lessonid' => $cm1->id]));
|
||||
$this->assertFalse($DB->record_exists('lesson_grades', ['userid' => $u1->id, 'lessonid' => $cm1->id]));
|
||||
$this->assertFalse($DB->record_exists('lesson_timer', ['userid' => $u1->id, 'lessonid' => $cm1->id]));
|
||||
$this->assertFalse($DB->record_exists('lesson_branch', ['userid' => $u1->id, 'lessonid' => $cm1->id]));
|
||||
$this->assertFalse($DB->record_exists('lesson_overrides', ['userid' => $u1->id, 'lessonid' => $cm1->id]));
|
||||
$this->assertTrue($DB->record_exists('lesson_attempts', ['userid' => $u1->id, 'lessonid' => $cm2->id]));
|
||||
$this->assertTrue($DB->record_exists('lesson_grades', ['userid' => $u1->id, 'lessonid' => $cm2->id]));
|
||||
$this->assertTrue($DB->record_exists('lesson_timer', ['userid' => $u1->id, 'lessonid' => $cm2->id]));
|
||||
$this->assertTrue($DB->record_exists('lesson_branch', ['userid' => $u1->id, 'lessonid' => $cm2->id]));
|
||||
$this->assertTrue($DB->record_exists('lesson_overrides', ['userid' => $u1->id, 'lessonid' => $cm2->id]));
|
||||
}
|
||||
|
||||
public function test_export_data_for_user_overrides() {
|
||||
$dg = $this->getDataGenerator();
|
||||
$c1 = $dg->create_course();
|
||||
$u1 = $dg->create_user();
|
||||
$u2 = $dg->create_user();
|
||||
|
||||
$cm1 = $dg->create_module('lesson', ['course' => $c1]);
|
||||
$cm2 = $dg->create_module('lesson', ['course' => $c1]);
|
||||
$cm1ctx = context_module::instance($cm1->cmid);
|
||||
$cm2ctx = context_module::instance($cm2->cmid);
|
||||
|
||||
$now = time();
|
||||
$this->create_override($cm1, $u1); // All null.
|
||||
$this->create_override($cm2, $u1, [
|
||||
'available' => $now - 3600,
|
||||
'deadline' => $now + 3600,
|
||||
'timelimit' => 123,
|
||||
'review' => 1,
|
||||
'maxattempts' => 1,
|
||||
'retake' => 0,
|
||||
'password' => '1337 5p34k'
|
||||
]);
|
||||
$this->create_override($cm1, $u2, [
|
||||
'available' => $now - 1230,
|
||||
'timelimit' => 456,
|
||||
'maxattempts' => 5,
|
||||
'retake' => 1,
|
||||
]);
|
||||
|
||||
provider::export_user_data(new approved_contextlist($u1, 'mod_lesson', [$cm1ctx->id, $cm2ctx->id]));
|
||||
$data = writer::with_context($cm1ctx)->get_data([]);
|
||||
$this->assertNotEmpty($data);
|
||||
$data = writer::with_context($cm1ctx)->get_related_data([], 'overrides');
|
||||
$this->assertNull($data->available);
|
||||
$this->assertNull($data->deadline);
|
||||
$this->assertNull($data->timelimit);
|
||||
$this->assertNull($data->review);
|
||||
$this->assertNull($data->maxattempts);
|
||||
$this->assertNull($data->retake);
|
||||
$this->assertNull($data->password);
|
||||
|
||||
$data = writer::with_context($cm2ctx)->get_data([]);
|
||||
$this->assertNotEmpty($data);
|
||||
$data = writer::with_context($cm2ctx)->get_related_data([], 'overrides');
|
||||
$this->assertEquals(transform::datetime($now - 3600), $data->available);
|
||||
$this->assertEquals(transform::datetime($now + 3600), $data->deadline);
|
||||
$this->assertEquals(format_time(123), $data->timelimit);
|
||||
$this->assertEquals(transform::yesno(true), $data->review);
|
||||
$this->assertEquals(1, $data->maxattempts);
|
||||
$this->assertEquals(transform::yesno(false), $data->retake);
|
||||
$this->assertEquals('1337 5p34k', $data->password);
|
||||
|
||||
writer::reset();
|
||||
provider::export_user_data(new approved_contextlist($u2, 'mod_lesson', [$cm1ctx->id, $cm2ctx->id]));
|
||||
$data = writer::with_context($cm1ctx)->get_data([]);
|
||||
$this->assertNotEmpty($data);
|
||||
$data = writer::with_context($cm1ctx)->get_related_data([], 'overrides');
|
||||
$this->assertEquals(transform::datetime($now - 1230), $data->available);
|
||||
$this->assertNull($data->deadline);
|
||||
$this->assertEquals(format_time(456), $data->timelimit);
|
||||
$this->assertNull($data->review);
|
||||
$this->assertEquals(5, $data->maxattempts);
|
||||
$this->assertEquals(transform::yesno(true), $data->retake);
|
||||
$this->assertNull($data->password);
|
||||
|
||||
$data = writer::with_context($cm2ctx)->get_data([]);
|
||||
$this->assertNotEmpty($data);
|
||||
$data = writer::with_context($cm2ctx)->get_related_data([], 'overrides');
|
||||
$this->assertEmpty($data);
|
||||
}
|
||||
|
||||
public function test_export_data_for_user_grades() {
|
||||
$dg = $this->getDataGenerator();
|
||||
$c1 = $dg->create_course();
|
||||
$u1 = $dg->create_user();
|
||||
$u2 = $dg->create_user();
|
||||
|
||||
$cm1 = $dg->create_module('lesson', ['course' => $c1]);
|
||||
$cm2 = $dg->create_module('lesson', ['course' => $c1]);
|
||||
$cm1ctx = context_module::instance($cm1->cmid);
|
||||
$cm2ctx = context_module::instance($cm2->cmid);
|
||||
|
||||
$now = time();
|
||||
$this->create_grade($cm2, $u1, ['grade' => 33.33, 'completed' => $now - 3600]);
|
||||
$this->create_grade($cm2, $u1, ['grade' => 50, 'completed' => $now - 1600]);
|
||||
$this->create_grade($cm2, $u1, ['grade' => 81.23, 'completed' => $now - 100]);
|
||||
$this->create_grade($cm1, $u2, ['grade' => 99.98, 'completed' => $now - 86400]);
|
||||
|
||||
provider::export_user_data(new approved_contextlist($u1, 'mod_lesson', [$cm1ctx->id, $cm2ctx->id]));
|
||||
$data = writer::with_context($cm1ctx)->get_related_data([], 'grades');
|
||||
$this->assertEmpty($data);
|
||||
$data = writer::with_context($cm2ctx)->get_related_data([], 'grades');
|
||||
$this->assertNotEmpty($data);
|
||||
$this->assertCount(3, $data->grades);
|
||||
$this->assertEquals(33.33, $data->grades[0]->grade);
|
||||
$this->assertEquals(50, $data->grades[1]->grade);
|
||||
$this->assertEquals(81.23, $data->grades[2]->grade);
|
||||
$this->assertEquals(transform::datetime($now - 3600), $data->grades[0]->completed);
|
||||
$this->assertEquals(transform::datetime($now - 1600), $data->grades[1]->completed);
|
||||
$this->assertEquals(transform::datetime($now - 100), $data->grades[2]->completed);
|
||||
|
||||
writer::reset();
|
||||
provider::export_user_data(new approved_contextlist($u2, 'mod_lesson', [$cm1ctx->id, $cm2ctx->id]));
|
||||
$data = writer::with_context($cm2ctx)->get_related_data([], 'grades');
|
||||
$this->assertEmpty($data);
|
||||
$data = writer::with_context($cm1ctx)->get_related_data([], 'grades');
|
||||
$this->assertNotEmpty($data);
|
||||
$this->assertCount(1, $data->grades);
|
||||
$this->assertEquals(99.98, $data->grades[0]->grade);
|
||||
$this->assertEquals(transform::datetime($now - 86400), $data->grades[0]->completed);
|
||||
}
|
||||
|
||||
public function test_export_data_for_user_timers() {
|
||||
$dg = $this->getDataGenerator();
|
||||
$c1 = $dg->create_course();
|
||||
$u1 = $dg->create_user();
|
||||
$u2 = $dg->create_user();
|
||||
|
||||
$cm1 = $dg->create_module('lesson', ['course' => $c1]);
|
||||
$cm2 = $dg->create_module('lesson', ['course' => $c1]);
|
||||
$cm1ctx = context_module::instance($cm1->cmid);
|
||||
$cm2ctx = context_module::instance($cm2->cmid);
|
||||
|
||||
$now = time();
|
||||
$this->create_timer($cm2, $u1, ['starttime' => $now - 2000, 'lessontime' => $now + 3600, 'completed' => 0,
|
||||
'timemodifiedoffline' => $now - 7000]);
|
||||
$this->create_timer($cm2, $u1, ['starttime' => $now - 1000, 'lessontime' => $now + 1600, 'completed' => 0]);
|
||||
$this->create_timer($cm2, $u1, ['starttime' => $now - 500, 'lessontime' => $now + 100, 'completed' => 1]);
|
||||
$this->create_timer($cm1, $u2, ['starttime' => $now - 1000, 'lessontime' => $now + 1800, 'completed' => 1]);
|
||||
|
||||
provider::export_user_data(new approved_contextlist($u1, 'mod_lesson', [$cm1ctx->id, $cm2ctx->id]));
|
||||
$data = writer::with_context($cm1ctx)->get_related_data([], 'timers');
|
||||
$this->assertEmpty($data);
|
||||
$data = writer::with_context($cm2ctx)->get_related_data([], 'timers');
|
||||
$this->assertNotEmpty($data);
|
||||
$this->assertCount(3, $data->timers);
|
||||
$this->assertEquals(transform::datetime($now - 2000), $data->timers[0]->starttime);
|
||||
$this->assertEquals(transform::datetime($now + 3600), $data->timers[0]->lastactivity);
|
||||
$this->assertEquals(transform::yesno(false), $data->timers[0]->completed);
|
||||
$this->assertEquals(transform::datetime($now - 7000), $data->timers[0]->timemodifiedoffline);
|
||||
|
||||
$this->assertEquals(transform::datetime($now - 1000), $data->timers[1]->starttime);
|
||||
$this->assertEquals(transform::datetime($now + 1600), $data->timers[1]->lastactivity);
|
||||
$this->assertEquals(transform::yesno(false), $data->timers[1]->completed);
|
||||
$this->assertNull($data->timers[1]->timemodifiedoffline);
|
||||
|
||||
$this->assertEquals(transform::datetime($now - 500), $data->timers[2]->starttime);
|
||||
$this->assertEquals(transform::datetime($now + 100), $data->timers[2]->lastactivity);
|
||||
$this->assertEquals(transform::yesno(true), $data->timers[2]->completed);
|
||||
$this->assertNull($data->timers[2]->timemodifiedoffline);
|
||||
|
||||
writer::reset();
|
||||
provider::export_user_data(new approved_contextlist($u2, 'mod_lesson', [$cm1ctx->id, $cm2ctx->id]));
|
||||
$data = writer::with_context($cm2ctx)->get_related_data([], 'timers');
|
||||
$this->assertEmpty($data);
|
||||
$data = writer::with_context($cm1ctx)->get_related_data([], 'timers');
|
||||
$this->assertCount(1, $data->timers);
|
||||
$this->assertEquals(transform::datetime($now - 1000), $data->timers[0]->starttime);
|
||||
$this->assertEquals(transform::datetime($now + 1800), $data->timers[0]->lastactivity);
|
||||
$this->assertEquals(transform::yesno(true), $data->timers[0]->completed);
|
||||
$this->assertNull($data->timers[0]->timemodifiedoffline);
|
||||
}
|
||||
|
||||
public function test_export_data_for_user_attempts() {
|
||||
global $DB;
|
||||
$dg = $this->getDataGenerator();
|
||||
$lg = $dg->get_plugin_generator('mod_lesson');
|
||||
|
||||
$c1 = $dg->create_course();
|
||||
$u1 = $dg->create_user();
|
||||
$u2 = $dg->create_user();
|
||||
|
||||
$cm1 = $dg->create_module('lesson', ['course' => $c1]);
|
||||
$cm2 = $dg->create_module('lesson', ['course' => $c1]);
|
||||
$cm1ctx = context_module::instance($cm1->cmid);
|
||||
$cm2ctx = context_module::instance($cm2->cmid);
|
||||
|
||||
$page1 = $lg->create_content($cm1);
|
||||
$page2 = $lg->create_question_truefalse($cm1);
|
||||
$page3 = $lg->create_question_multichoice($cm1);
|
||||
$page4 = $lg->create_question_multichoice($cm1, [
|
||||
'qoption' => 1,
|
||||
'answer_editor' => [
|
||||
['text' => 'Cats', 'format' => FORMAT_PLAIN, 'score' => 1],
|
||||
['text' => 'Dogs', 'format' => FORMAT_PLAIN, 'score' => 1],
|
||||
['text' => 'Birds', 'format' => FORMAT_PLAIN, 'score' => 0],
|
||||
],
|
||||
'jumpto' => [LESSON_NEXTPAGE, LESSON_NEXTPAGE, LESSON_THISPAGE]
|
||||
]);
|
||||
$page4answers = array_keys($DB->get_records('lesson_answers', ['pageid' => $page4->id], 'id'));
|
||||
$page5 = $lg->create_question_matching($cm1, [
|
||||
'answer_editor' => [
|
||||
2 => ['text' => 'The plural of cat', 'format' => FORMAT_PLAIN],
|
||||
3 => ['text' => 'The plural of dog', 'format' => FORMAT_PLAIN],
|
||||
4 => ['text' => 'The plural of bird', 'format' => FORMAT_PLAIN],
|
||||
],
|
||||
'response_editor' => [
|
||||
2 => 'Cats',
|
||||
3 => 'Dogs',
|
||||
4 => 'Birds',
|
||||
]
|
||||
]);
|
||||
$page6 = $lg->create_question_shortanswer($cm1);
|
||||
$page7 = $lg->create_question_numeric($cm1);
|
||||
$page8 = $lg->create_question_essay($cm1);
|
||||
$page9 = $lg->create_content($cm1);
|
||||
|
||||
$pageb1 = $lg->create_content($cm2);
|
||||
$pageb2 = $lg->create_question_truefalse($cm2);
|
||||
$pageb3 = $lg->create_question_truefalse($cm2);
|
||||
|
||||
$this->create_branch($cm1, $u1, ['pageid' => $page1->id, 'nextpageid' => $page2->id]);
|
||||
$this->create_attempt($cm1, $u1, ['pageid' => $page2->id, 'useranswer' => 'This is true']);
|
||||
$this->create_attempt($cm1, $u1, ['pageid' => $page3->id, 'useranswer' => 'A', 'correct' => 1]);
|
||||
$this->create_attempt($cm1, $u1, ['pageid' => $page4->id,
|
||||
'useranswer' => implode(',', array_slice($page4answers, 0, 2))]);
|
||||
$this->create_attempt($cm1, $u1, ['pageid' => $page5->id, 'useranswer' => 'Cats,Birds,Dogs']);
|
||||
$this->create_attempt($cm1, $u1, ['pageid' => $page6->id, 'useranswer' => 'Hello world!']);
|
||||
$this->create_attempt($cm1, $u1, ['pageid' => $page7->id, 'useranswer' => '1337']);
|
||||
$this->create_attempt($cm1, $u1, ['pageid' => $page8->id, 'useranswer' => serialize((object) [
|
||||
'sent' => 0, 'graded' => 0, 'score' => 0, 'answer' => 'I like cats', 'answerformat' => FORMAT_PLAIN,
|
||||
'response' => 'Me too!', 'responseformat' => FORMAT_PLAIN
|
||||
])]);
|
||||
$this->create_branch($cm1, $u1, ['pageid' => $page9->id, 'nextpageid' => 0]);
|
||||
|
||||
provider::export_user_data(new approved_contextlist($u1, 'mod_lesson', [$cm1ctx->id, $cm2ctx->id]));
|
||||
$data = writer::with_context($cm2ctx)->get_related_data([], 'attempts');
|
||||
$this->assertEmpty($data);
|
||||
$data = writer::with_context($cm1ctx)->get_related_data([], 'attempts');
|
||||
$this->assertNotEmpty($data);
|
||||
$this->assertCount(1, $data->attempts);
|
||||
$this->assertEquals(1, $data->attempts[0]->number);
|
||||
$this->assertCount(2, $data->attempts[0]->jumps);
|
||||
$this->assertCount(7, $data->attempts[0]->answers);
|
||||
$jump = $data->attempts[0]->jumps[0];
|
||||
$this->assert_attempt_page($page1, $jump);
|
||||
$this->assertTrue(strpos($jump['went_to'], $page2->title) !== false);
|
||||
$jump = $data->attempts[0]->jumps[1];
|
||||
$this->assert_attempt_page($page9, $jump);
|
||||
$this->assertEquals(get_string('endoflesson', 'mod_lesson'), $jump['went_to']);
|
||||
$answer = $data->attempts[0]->answers[0];
|
||||
$this->assert_attempt_page($page2, $answer);
|
||||
$this->assertEquals(transform::yesno(false), $answer['correct']);
|
||||
$this->assertEquals('This is true', $answer['answer']);
|
||||
$answer = $data->attempts[0]->answers[1];
|
||||
$this->assert_attempt_page($page3, $answer);
|
||||
$this->assertEquals(transform::yesno(true), $answer['correct']);
|
||||
$this->assertEquals('A', $answer['answer']);
|
||||
$answer = $data->attempts[0]->answers[2];
|
||||
$this->assert_attempt_page($page4, $answer);
|
||||
$this->assertEquals(transform::yesno(false), $answer['correct']);
|
||||
$this->assertCount(2, $answer['answer']);
|
||||
$this->assertTrue(in_array('Cats', $answer['answer']));
|
||||
$this->assertTrue(in_array('Dogs', $answer['answer']));
|
||||
$answer = $data->attempts[0]->answers[3];
|
||||
$this->assert_attempt_page($page5, $answer);
|
||||
$this->assertEquals(transform::yesno(false), $answer['correct']);
|
||||
$this->assertCount(3, $answer['answer']);
|
||||
$this->assertEquals('The plural of cat', $answer['answer'][0]['label']);
|
||||
$this->assertEquals('Cats', $answer['answer'][0]['matched_with']);
|
||||
$this->assertEquals('The plural of dog', $answer['answer'][1]['label']);
|
||||
$this->assertEquals('Birds', $answer['answer'][1]['matched_with']);
|
||||
$this->assertEquals('The plural of bird', $answer['answer'][2]['label']);
|
||||
$this->assertEquals('Dogs', $answer['answer'][2]['matched_with']);
|
||||
$answer = $data->attempts[0]->answers[4];
|
||||
$this->assert_attempt_page($page6, $answer);
|
||||
$this->assertEquals(transform::yesno(false), $answer['correct']);
|
||||
$this->assertEquals('Hello world!', $answer['answer']);
|
||||
$answer = $data->attempts[0]->answers[5];
|
||||
$this->assert_attempt_page($page7, $answer);
|
||||
$this->assertEquals(transform::yesno(false), $answer['correct']);
|
||||
$this->assertEquals('1337', $answer['answer']);
|
||||
$answer = $data->attempts[0]->answers[6];
|
||||
$this->assert_attempt_page($page8, $answer);
|
||||
$this->assertEquals(transform::yesno(false), $answer['correct']);
|
||||
$this->assertEquals('I like cats', $answer['answer']);
|
||||
$this->assertEquals('Me too!', $answer['response']);
|
||||
|
||||
writer::reset();
|
||||
provider::export_user_data(new approved_contextlist($u2, 'mod_lesson', [$cm1ctx->id, $cm2ctx->id]));
|
||||
$data = writer::with_context($cm1ctx)->get_related_data([], 'attempts');
|
||||
$this->assertEmpty($data);
|
||||
$data = writer::with_context($cm2ctx)->get_related_data([], 'attempts');
|
||||
$this->assertEmpty($data);
|
||||
|
||||
// Let's mess with the data by creating an additional attempt for u1, and create data for u1 and u2 in the other cm.
|
||||
$this->create_branch($cm1, $u1, ['pageid' => $page1->id, 'nextpageid' => $page3->id, 'retry' => 1]);
|
||||
$this->create_attempt($cm1, $u1, ['pageid' => $page3->id, 'useranswer' => 'B', 'retry' => 1]);
|
||||
|
||||
$this->create_branch($cm2, $u1, ['pageid' => $pageb1->id, 'nextpageid' => $pageb2->id]);
|
||||
$this->create_attempt($cm2, $u1, ['pageid' => $pageb2->id, 'useranswer' => 'Abc']);
|
||||
|
||||
$this->create_branch($cm2, $u2, ['pageid' => $pageb1->id, 'nextpageid' => $pageb3->id]);
|
||||
$this->create_attempt($cm2, $u2, ['pageid' => $pageb3->id, 'useranswer' => 'Def']);
|
||||
|
||||
writer::reset();
|
||||
provider::export_user_data(new approved_contextlist($u1, 'mod_lesson', [$cm1ctx->id, $cm2ctx->id]));
|
||||
$data = writer::with_context($cm1ctx)->get_related_data([], 'attempts');
|
||||
$this->assertNotEmpty($data);
|
||||
$this->assertCount(2, $data->attempts);
|
||||
$this->assertEquals(1, $data->attempts[0]->number);
|
||||
$this->assertCount(2, $data->attempts[0]->jumps);
|
||||
$this->assertCount(7, $data->attempts[0]->answers);
|
||||
$attempt = $data->attempts[1];
|
||||
$this->assertEquals(2, $attempt->number);
|
||||
$this->assertCount(1, $attempt->jumps);
|
||||
$this->assertCount(1, $attempt->answers);
|
||||
$this->assert_attempt_page($page1, $attempt->jumps[0]);
|
||||
$this->assertTrue(strpos($attempt->jumps[0]['went_to'], $page3->title) !== false);
|
||||
$this->assert_attempt_page($page3, $attempt->answers[0]);
|
||||
$this->assertEquals('B', $attempt->answers[0]['answer']);
|
||||
|
||||
$data = writer::with_context($cm2ctx)->get_related_data([], 'attempts');
|
||||
$this->assertCount(1, $data->attempts);
|
||||
$attempt = $data->attempts[0];
|
||||
$this->assertEquals(1, $attempt->number);
|
||||
$this->assertCount(1, $attempt->jumps);
|
||||
$this->assertCount(1, $attempt->answers);
|
||||
$this->assert_attempt_page($pageb1, $attempt->jumps[0]);
|
||||
$this->assertTrue(strpos($attempt->jumps[0]['went_to'], $pageb2->title) !== false);
|
||||
$this->assert_attempt_page($pageb2, $attempt->answers[0]);
|
||||
$this->assertEquals('Abc', $attempt->answers[0]['answer']);
|
||||
|
||||
writer::reset();
|
||||
provider::export_user_data(new approved_contextlist($u2, 'mod_lesson', [$cm1ctx->id, $cm2ctx->id]));
|
||||
$data = writer::with_context($cm1ctx)->get_related_data([], 'attempts');
|
||||
$this->assertEmpty($data);
|
||||
|
||||
$data = writer::with_context($cm2ctx)->get_related_data([], 'attempts');
|
||||
$this->assertCount(1, $data->attempts);
|
||||
$attempt = $data->attempts[0];
|
||||
$this->assertEquals(1, $attempt->number);
|
||||
$this->assertCount(1, $attempt->jumps);
|
||||
$this->assertCount(1, $attempt->answers);
|
||||
$this->assert_attempt_page($pageb1, $attempt->jumps[0]);
|
||||
$this->assertTrue(strpos($attempt->jumps[0]['went_to'], $pageb3->title) !== false);
|
||||
$this->assert_attempt_page($pageb3, $attempt->answers[0]);
|
||||
$this->assertEquals('Def', $attempt->answers[0]['answer']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert the page details of an attempt.
|
||||
*
|
||||
* @param object $page The expected page info.
|
||||
* @param array $attempt The exported attempt details.
|
||||
* @return void
|
||||
*/
|
||||
protected function assert_attempt_page($page, $attempt) {
|
||||
$this->assertEquals($page->id, $attempt['id']);
|
||||
$this->assertEquals($page->title, $attempt['page']);
|
||||
$this->assertEquals(format_text($page->contents, $page->contentsformat), $attempt['contents']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an attempt (answer to a question).
|
||||
*
|
||||
* @param object $lesson The lesson.
|
||||
* @param object $user The user.
|
||||
* @param array $options Options.
|
||||
* @return object
|
||||
*/
|
||||
protected function create_attempt($lesson, $user, array $options = []) {
|
||||
global $DB;
|
||||
$record = (object) array_merge([
|
||||
'lessonid' => $lesson->id,
|
||||
'userid' => $user->id,
|
||||
'pageid' => 0,
|
||||
'answerid' => 0,
|
||||
'retry' => 0,
|
||||
'correct' => 0,
|
||||
'useranswer' => '',
|
||||
'timeseen' => time(),
|
||||
], $options);
|
||||
$record->id = $DB->insert_record('lesson_attempts', $record);
|
||||
return $record;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a grade.
|
||||
*
|
||||
* @param object $lesson The lesson.
|
||||
* @param object $user The user.
|
||||
* @param array $options Options.
|
||||
* @return object
|
||||
*/
|
||||
protected function create_grade($lesson, $user, array $options = []) {
|
||||
global $DB;
|
||||
$record = (object) array_merge([
|
||||
'lessonid' => $lesson->id,
|
||||
'userid' => $user->id,
|
||||
'late' => 0,
|
||||
'grade' => 50.0,
|
||||
'completed' => time(),
|
||||
], $options);
|
||||
$record->id = $DB->insert_record('lesson_grades', $record);
|
||||
return $record;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a timer.
|
||||
*
|
||||
* @param object $lesson The lesson.
|
||||
* @param object $user The user.
|
||||
* @param array $options Options.
|
||||
* @return object
|
||||
*/
|
||||
protected function create_timer($lesson, $user, array $options = []) {
|
||||
global $DB;
|
||||
$record = (object) array_merge([
|
||||
'lessonid' => $lesson->id,
|
||||
'userid' => $user->id,
|
||||
'starttime' => time() - 600,
|
||||
'lessontime' => time(),
|
||||
'completed' => 1,
|
||||
'timemodifiedoffline' => 0,
|
||||
], $options);
|
||||
$record->id = $DB->insert_record('lesson_timer', $record);
|
||||
return $record;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a branch (choice on page).
|
||||
*
|
||||
* @param object $lesson The lesson.
|
||||
* @param object $user The user.
|
||||
* @param array $options Options.
|
||||
* @return object
|
||||
*/
|
||||
protected function create_branch($lesson, $user, array $options = []) {
|
||||
global $DB;
|
||||
$record = (object) array_merge([
|
||||
'lessonid' => $lesson->id,
|
||||
'userid' => $user->id,
|
||||
'pageid' => 0,
|
||||
'retry' => 0,
|
||||
'flag' => 0,
|
||||
'timeseen' => time(),
|
||||
'nextpageid' => 0,
|
||||
], $options);
|
||||
$record->id = $DB->insert_record('lesson_branch', $record);
|
||||
return $record;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an override.
|
||||
*
|
||||
* @param object $lesson The lesson.
|
||||
* @param object $user The user.
|
||||
* @param array $options Options.
|
||||
* @return object
|
||||
*/
|
||||
protected function create_override($lesson, $user, array $options = []) {
|
||||
global $DB;
|
||||
$record = (object) array_merge([
|
||||
'lessonid' => $lesson->id,
|
||||
'userid' => $user->id,
|
||||
], $options);
|
||||
$record->id = $DB->insert_record('lesson_overrides', $record);
|
||||
return $record;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user