Skip to content

Commit

Permalink
issue #21: add support for mod_hvp and mod_h5pactivity
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitriim committed Oct 24, 2023
1 parent 57eeb3e commit f682b50
Show file tree
Hide file tree
Showing 12 changed files with 822 additions and 14 deletions.
40 changes: 40 additions & 0 deletions backup/moodle2/backup_local_recompletion_plugin.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,46 @@ protected function define_course_plugin_structure() {
}
$choiceanswer->annotate_ids('user', 'userid');

// Now deal with hvp archive tables.
$hvpattempts = new backup_nested_element('hvpattempts');
$hvpattempt = new backup_nested_element('hvpattempt', array('id'), array(
'user_id', 'hvp_id', 'sub_content_id', 'data_id', 'data', 'preloaded', 'delete_on_content_change', 'course'));

$recompletion->add_child($hvpattempts);
$hvpattempts->add_child($hvpattempt);

if ($usercompletion) {
$hvpattempt->set_source_table('local_recompletion_hvp', array('course' => backup::VAR_COURSEID));
}
$hvpattempt->annotate_ids('user', 'user_id');

// Now deal with h5p table.
$h5ps = new backup_nested_element('h5ps');
$h5p = new backup_nested_element('h5p', array('id'), array(
'originalattemptid', 'h5pactivityid', 'userid', 'timecreated', 'timemodified',
'rawscore', 'maxscore', 'scaled', 'duration', 'completion', 'success', 'course'));

// Now deal with h5p results table.
$h5presults = new backup_nested_element('h5presults');
$h5presult = new backup_nested_element('h5presult', array('id'), array(
'attemptid', 'subcontent', 'timecreated', 'interactiontype', 'description',
'correctpattern', 'response', 'additionals', 'rawscore', 'maxscore', 'duration', 'completion', 'success', 'course'));

$recompletion->add_child($h5ps);
$h5ps->add_child($h5p);
$h5p->add_child($h5presults);
$h5presults->add_child($h5presult);

if ($usercompletion) {
$h5p->set_source_table('local_recompletion_h5p', array('course' => backup::VAR_COURSEID));
$h5presult->set_source_table(
'local_recompletion_h5pr',
array('course' => backup::VAR_COURSEID, 'attemptid' => backup::VAR_PARENTID)
);
}

$h5p->annotate_ids('user', 'userid');

return $plugin;
}

Expand Down
63 changes: 63 additions & 0 deletions backup/moodle2/restore_local_recompletion_plugin.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ protected function define_course_plugin_structure() {
$paths[] = new restore_path_element('recompletion_qg', $elepath.'/quizgrades/grade');
$paths[] = new restore_path_element('recompletion_sst', $elepath.'/scormtracks/sco_track');
$paths[] = new restore_path_element('recompletion_cha', $elepath.'/choiceanswers/choiceanswer');
$paths[] = new restore_path_element('recompletion_hvp', $elepath.'/hvpattempts/hvpattempt');
$paths[] = new restore_path_element('recompletion_h5p', $elepath.'/h5ps/h5p');
$paths[] = new restore_path_element('recompletion_h5pr', $elepath.'/h5ps/h5p/h5presults/h5presult');

return $paths;
}
Expand Down Expand Up @@ -164,6 +167,49 @@ public function process_recompletion_cha($data) {
$DB->insert_record('local_recompletion_cha', $data);
}

/**
* Process local_recompletion_hvp table.
* @param stdClass $data
*/
public function process_recompletion_hvp($data) {
global $DB;

$data = (object) $data;
$data->course = $this->task->get_courseid();
$data->user_id = $this->get_mappingid('user', $data->user_id);

$DB->insert_record('local_recompletion_hvp', $data);
}

/**
* Process local_recompletion_h5p table.
* @param stdClass $data
*/
public function process_recompletion_h5p($data) {
global $DB;

$data = (object) $data;
$oldid = $data->id;
$data->course = $this->task->get_courseid();
$data->userid = $this->get_mappingid('user', $data->userid);

$newitemid = $DB->insert_record('local_recompletion_h5p', $data);
$this->set_mapping('recompletion_h5p', $oldid, $newitemid);
}

/**
* Process local_recompletion_h5pr table.
* @param stdClass $data
*/
public function process_recompletion_h5pr($data) {
global $DB;

$data = (object) $data;
$data->course = $this->task->get_courseid();
$data->attemptid = $this->get_new_parentid('recompletion_h5p');
$DB->insert_record('local_recompletion_h5pr', $data);
}

/**
* We call the after restore_course to update the coursemodule ids we didn't know when creating.
*/
Expand Down Expand Up @@ -210,5 +256,22 @@ protected function after_restore_course() {
}
$rcm->close();

// Fix hvp attempts.
$rcm = $DB->get_recordset('local_recompletion_hvp', array('course' => $this->task->get_courseid()));
foreach ($rcm as $rc) {
$rc->hvp_id = $this->get_mappingid('hvp', $rc->hvp_id);
$DB->update_record('local_recompletion_hvp', $rc);
}
$rcm->close();

// Fix h5p attempts.
$rcm = $DB->get_recordset('local_recompletion_h5p', array('course' => $this->task->get_courseid()));
foreach ($rcm as $rc) {
$rc->h5pactivityid = $this->get_mappingid('h5pactivity', $rc->h5pactivityid);
$rc->originalattemptid = 0; // Don't restore orginal attempt id.

$DB->update_record('local_recompletion_h5p', $rc);
}
$rcm->close();
}
}
153 changes: 153 additions & 0 deletions classes/plugins/mod_h5pactivity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?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/>.

namespace local_recompletion\plugins;

use stdClass;
use lang_string;
use admin_setting_configselect;
use admin_setting_configcheckbox;
use admin_settingpage;
use MoodleQuickForm;

defined('MOODLE_INTERNAL') || die;

require_once($CFG->dirroot.'/local/recompletion/locallib.php');

/**
* H5P handler event.
*
* @package local_recompletion
* @author 2023 Dmitrii Metelkin
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_h5pactivity {

/**
* Add params to form.
*
* @param MoodleQuickForm $mform
*/
public static function editingform(MoodleQuickForm $mform): void {
$config = get_config('local_recompletion');

$cba = [];
$cba[] = $mform->createElement('radio', 'h5pactivity', '',
get_string('donothing', 'local_recompletion'), LOCAL_RECOMPLETION_NOTHING);
$cba[] = $mform->createElement('radio', 'h5pactivity', '',
get_string('delete', 'local_recompletion'), LOCAL_RECOMPLETION_DELETE);

$mform->addGroup($cba, 'h5pactivity', get_string('h5pattempts', 'local_recompletion'), [' '], false);
$mform->addHelpButton('h5pactivity', 'h5pattempts', 'local_recompletion');
$mform->setDefault('h5pactivity', $config->h5pattempts);

$mform->addElement('checkbox', 'archiveh5pactivity', get_string('archive', 'local_recompletion'));
$mform->setDefault('archiveh5pactivity', $config->archiveh5p);

$mform->disabledIf('archiveh5pactivity', 'enable');
$mform->hideIf('archiveh5pactivity', 'h5pactivity');
$mform->disabledIf('h5pactivity', 'enable');
}

/**
* Add site level settings for this plugin.
*
* @param admin_settingpage $settings
*/
public static function settings(admin_settingpage $settings): void {
$choices = [
LOCAL_RECOMPLETION_NOTHING => get_string('donothing', 'local_recompletion'),
LOCAL_RECOMPLETION_DELETE => get_string('delete', 'local_recompletion')
];

$settings->add(new admin_setting_configselect('local_recompletion/h5pattempts',
new lang_string('h5pattempts', 'local_recompletion'),
new lang_string('h5pattempts_help', 'local_recompletion'), LOCAL_RECOMPLETION_NOTHING, $choices));

$settings->add(new admin_setting_configcheckbox('local_recompletion/archiveh5p',
new lang_string('archiveh5p', 'local_recompletion'), '', 1));
}

/**
* Reset h5pactivity attempt records.
*
* @param int $userid - user id
* @param stdClass $course - course record.
* @param stdClass $config - recompletion config.
*/
public static function reset(int $userid, stdClass $course, stdClass $config): void {
global $DB;

if (empty($config->h5pactivity)) {
return;
}

if ($config->h5pactivity == LOCAL_RECOMPLETION_DELETE) {
$params = [
'userid' => $userid,
'course' => $course->id
];

$attemptsselectsql = 'userid = :userid AND h5pactivityid IN (SELECT id FROM {h5pactivity} WHERE course = :course)';
$resultsselectsql = 'attemptid IN (SELECT id FROM {h5pactivity_attempts} WHERE ' . $attemptsselectsql . ')';

if ($config->archiveh5pactivity) {

// Archive attempts.
$attempts = $DB->get_records_select('h5pactivity_attempts', $attemptsselectsql, $params);
if (!empty($attempts)) {
$attemptids = array_keys($attempts);

foreach ($attempts as $attempt) {
$attempt->course = $course->id;
$attempt->originalattemptid = $attempt->id;
}

$DB->insert_records('local_recompletion_h5p', $attempts);

// Archive results.
$results = $DB->get_records_select('h5pactivity_attempts_results', $resultsselectsql, $params);
if (!empty($results)) {
foreach ($results as $result) {
$result->course = $course->id;
}
$DB->insert_records('local_recompletion_h5pr', $results);

// Update attemptid for just inserted attempt results with IDs of previously inserted attempts.
// We use temp originalattemptid here as it should be unique.
$sql = 'UPDATE {local_recompletion_h5pr}
SET attemptid = (SELECT id
FROM {local_recompletion_h5p}
WHERE originalattemptid = attemptid)';
$DB->execute($sql);
}

// Now reset originalattemptid as we don't need it anymore.
// As well as to avoid issues with backup and restore when potentially originalattemptid can clash
// if restoring a course from another Moodle instance.
list($insql, $inparams) = $DB->get_in_or_equal($attemptids, SQL_PARAMS_NAMED);
$sql = "UPDATE {local_recompletion_h5p} SET originalattemptid = 0 WHERE originalattemptid $insql";
$DB->execute($sql, $inparams);
}

}

// Finally can delete records.
$DB->delete_records_select('h5pactivity_attempts_results', $resultsselectsql, $params);
$DB->delete_records_select('h5pactivity_attempts', $attemptsselectsql, $params);
}
}
}
Loading

0 comments on commit f682b50

Please sign in to comment.