Skip to content

Commit

Permalink
issue danmarsden#21: add support for mod_hvp
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitriim committed Oct 18, 2023
1 parent 0a638f8 commit 985cf39
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 3 deletions.
13 changes: 13 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,19 @@ 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_hcud', array('course' => backup::VAR_COURSEID));
}
$hvpattempt->annotate_ids('user', 'user_id');

return $plugin;
}

Expand Down
22 changes: 22 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,7 @@ 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_hcud', $elepath.'/hvpattempts/hvpattempt');

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

/**
* Process local_recompletion_hcud table.
* @param stdClass $data
*/
public function process_recompletion_hcud($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_hcud', $data);
}

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

// Fix hvp attempts.
$rcm = $DB->get_recordset('local_recompletion_hcud', 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_hcud', $rc);
}
$rcm->close();
}
}
137 changes: 137 additions & 0 deletions classes/plugins/mod_hvp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?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;

/**
* 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_hvp {

/**
* Add params to form.
*
* @param MoodleQuickForm $mform
*/
public static function editingform(MoodleQuickForm $mform): void {
if (!self::installed()) {
return;
}

$config = get_config('local_recompletion');

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

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

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

$mform->disabledIf('archivehvp', 'enable');
$mform->hideIf('archivehvp', 'hvp');
$mform->disabledIf('hvp', 'enable');
}

/**
* Add site level settings for this plugin.
*
* @param admin_settingpage $settings
*/
public static function settings(admin_settingpage $settings): void {
if (!self::installed()) {
return;
}

$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/hvpattempts',
new lang_string('hvpattempts', 'local_recompletion'),
new lang_string('hvpattempts_help', 'local_recompletion'), LOCAL_RECOMPLETION_NOTHING, $choices));

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

/**
* Reset pulse notification 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 (!self::installed()) {
return;
}

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

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

$selectsql = 'user_id = :userid AND hvp_id IN (SELECT id FROM {hvp} WHERE course = :course)';

if ($config->archivehvp) {
$records = $DB->get_records_select('hvp_content_user_data', $selectsql, $params);
foreach ($records as $record) {
$record->course = $course->id;
}
$DB->insert_records('local_recompletion_hcud', $records);
}

$DB->delete_records_select('hvp_content_user_data', $selectsql, $params);
}
}

/**
* Helper function to check if the plugin is installed.
* @return bool
*/
public static function installed(): bool {
global $CFG;
if (!file_exists($CFG->dirroot . '/mod/hvp/version.php')) {
return false;
}
return true;
}
}
18 changes: 17 additions & 1 deletion db/install.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="local/recompletion/db" VERSION="20230320" COMMENT="XMLDB file for Moodle local/recompletion"
<XMLDB PATH="local/recompletion/db" VERSION="20231018" COMMENT="XMLDB file for Moodle local/recompletion"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
>
Expand Down Expand Up @@ -325,5 +325,21 @@
<INDEX NAME="course" UNIQUE="false" FIELDS="course"/>
</INDEXES>
</TABLE>
<TABLE NAME="local_recompletion_hcud" COMMENT="Archive for hvp_content_user_data table">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="user_id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="Id for the user answering this H5P"/>
<FIELD NAME="hvp_id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="Id of hvp content in the 'hvp' table"/>
<FIELD NAME="sub_content_id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="Subcontent id of hvp content, 0 if this is not subcontent"/>
<FIELD NAME="data_id" TYPE="char" LENGTH="127" NOTNULL="false" SEQUENCE="false" COMMENT="The data type identifier"/>
<FIELD NAME="data" TYPE="text" NOTNULL="false" SEQUENCE="false" COMMENT="The actual user data that was stored."/>
<FIELD NAME="preloaded" TYPE="int" LENGTH="1" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="delete_on_content_change" TYPE="int" LENGTH="1" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="course" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="Extra course to help restoring if needed."/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
</KEYS>
</TABLE>
</TABLES>
</XMLDB>
27 changes: 27 additions & 0 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -694,5 +694,32 @@ function xmldb_local_recompletion_upgrade($oldversion) {

}

if ($oldversion < 2023101800) {
// Define table local_recompletion_hcud to be created.
$table = new xmldb_table('local_recompletion_hcud');

// Adding fields to table local_recompletion_hcud.
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('user_id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('hvp_id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('sub_content_id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('data_id', XMLDB_TYPE_CHAR, '127', null, null, null, null);
$table->add_field('data', XMLDB_TYPE_TEXT, null, null, null, null, null);
$table->add_field('preloaded', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, null);
$table->add_field('delete_on_content_change', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, null);
$table->add_field('course', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');

// Adding keys to table local_recompletion_hcud.
$table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);

// Conditionally launch create table for local_recompletion_hcud.
if (!$dbman->table_exists($table)) {
$dbman->create_table($table);
}

// Recompletion savepoint reached.
upgrade_plugin_savepoint(true, 2023101800, 'local', 'recompletion');
}

return true;
}
3 changes: 3 additions & 0 deletions lang/en/local_recompletion.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,6 @@
$string['archivecustomcertcertificates_help'] = 'Should issued custom certificates be archived?';
$string['recompletionunenrolenable'] = 'Reset completion on un-enrolment';
$string['recompletionunenrolenable_help'] = 'Enable to trigger completion reset on user un-enrolment';
$string['hvpattempts'] = 'H5P attempts';
$string['hvpattempts_help'] = 'How to handle H5P attempts within the course. If archive is selected, the old H5P attempts will be archived in the local_recompletion_hcud table.';
$string['archivehvp'] = 'Archive old H5P attempts';
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

defined('MOODLE_INTERNAL') || die;

$plugin->version = 2023101702;
$plugin->release = 2023101702;
$plugin->version = 2023101800;
$plugin->release = 2023101800;
$plugin->maturity = MATURITY_STABLE;
$plugin->requires = 2022112806; // Requires 4.1
$plugin->component = 'local_recompletion';
Expand Down

0 comments on commit 985cf39

Please sign in to comment.