diff --git a/backup/moodle2/backup_local_recompletion_plugin.class.php b/backup/moodle2/backup_local_recompletion_plugin.class.php index 137f5b8..c3ea905 100644 --- a/backup/moodle2/backup_local_recompletion_plugin.class.php +++ b/backup/moodle2/backup_local_recompletion_plugin.class.php @@ -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; } diff --git a/backup/moodle2/restore_local_recompletion_plugin.class.php b/backup/moodle2/restore_local_recompletion_plugin.class.php index 15fc9fd..ae2a8f5 100644 --- a/backup/moodle2/restore_local_recompletion_plugin.class.php +++ b/backup/moodle2/restore_local_recompletion_plugin.class.php @@ -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; } @@ -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. */ @@ -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(); } } diff --git a/classes/plugins/mod_hvp.php b/classes/plugins/mod_hvp.php new file mode 100644 index 0000000..65a6e97 --- /dev/null +++ b/classes/plugins/mod_hvp.php @@ -0,0 +1,137 @@ +. + +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; + } +} diff --git a/db/install.xml b/db/install.xml index a4ddb3e..b031096 100644 --- a/db/install.xml +++ b/db/install.xml @@ -1,5 +1,5 @@ - @@ -325,5 +325,21 @@ + + + + + + + + + + + + + + + +
diff --git a/db/upgrade.php b/db/upgrade.php index 0f8791b..251cfdd 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -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; } diff --git a/lang/en/local_recompletion.php b/lang/en/local_recompletion.php index efd34f7..083946a 100644 --- a/lang/en/local_recompletion.php +++ b/lang/en/local_recompletion.php @@ -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'; diff --git a/version.php b/version.php index 3fd8e7a..a5ae17a 100644 --- a/version.php +++ b/version.php @@ -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';