|
| 1 | +<?php |
| 2 | +// This file is part of Moodle - http://moodle.org/ |
| 3 | +// |
| 4 | +// Moodle is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// Moodle is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with Moodle. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +use block_ai_chat\local\persona; |
| 18 | + |
| 19 | +/** |
| 20 | + * Restore steps for mod_aichat. |
| 21 | + * |
| 22 | + * @package mod_aichat |
| 23 | + * @copyright 2026 ISB Bayern |
| 24 | + * @author Stefan Hanauska <stefan.hanauska@csg-in.de> |
| 25 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 26 | + */ |
| 27 | +class restore_aichat_structure_step extends restore_activity_structure_step { |
| 28 | + /** |
| 29 | + * Define structure. |
| 30 | + */ |
| 31 | + protected function define_structure() { |
| 32 | + $paths = []; |
| 33 | + $paths[] = new restore_path_element('aichat', '/activity/aichat'); |
| 34 | + $paths[] = new restore_path_element('persona', '/activity/aichat/personas/persona'); |
| 35 | + $paths[] = new restore_path_element('persona_selected', '/activity/aichat/personas_selected/persona_selected'); |
| 36 | + $paths[] = new restore_path_element('chat_option', '/activity/aichat/chat_options/chat_option'); |
| 37 | + |
| 38 | + return $this->prepare_activity_structure($paths); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Process an aichat element. Nothing to do here yet as there is no data to restore. |
| 43 | + * |
| 44 | + * @param array $data |
| 45 | + */ |
| 46 | + protected function process_aichat($data) { |
| 47 | + global $DB; |
| 48 | + $data = (object)$data; |
| 49 | + $oldid = $data->id; |
| 50 | + $data->course = $this->get_courseid(); |
| 51 | + $newid = $DB->insert_record('aichat', $data); |
| 52 | + $this->set_mapping('aichat', $oldid, $newid); |
| 53 | + $this->apply_activity_instance($newid); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Extra actions to take once restore is complete. |
| 58 | + */ |
| 59 | + protected function after_execute(): void { |
| 60 | + $this->add_related_files('mod_aichat', 'intro', null); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Process a persona element. |
| 65 | + * |
| 66 | + * @param array $data |
| 67 | + */ |
| 68 | + protected function process_persona($data) { |
| 69 | + global $DB; |
| 70 | + |
| 71 | + $userinfo = $this->get_setting_value('users'); |
| 72 | + |
| 73 | + // If no userinfo, map to current user. |
| 74 | + if (!$userinfo) { |
| 75 | + $data['userid'] = $this->task->get_userid(); |
| 76 | + } else { |
| 77 | + $data['userid'] = $this->get_mappingid('user', $data['userid']); |
| 78 | + } |
| 79 | + |
| 80 | + $search = [ |
| 81 | + 'name' => $data['name'], |
| 82 | + 'prompt' => $data['prompt'], |
| 83 | + 'type' => $data['type'], |
| 84 | + 'userinfo' => $data['userinfo'], |
| 85 | + ]; |
| 86 | + |
| 87 | + $where = 'name = :name AND type = :type'; |
| 88 | + |
| 89 | + if ($data['type'] != persona::TYPE_TEMPLATE) { |
| 90 | + // For user-specific personas, add userid to search. |
| 91 | + $search['userid'] = $data['userid']; |
| 92 | + $where .= ' AND userid = :userid'; |
| 93 | + } |
| 94 | + |
| 95 | + $where .= ' AND ' . $DB->sql_compare_text('prompt') . ' = ' . $DB->sql_compare_text(':prompt'); |
| 96 | + $where .= ' AND ' . $DB->sql_compare_text('userinfo') . ' = ' . $DB->sql_compare_text(':userinfo'); |
| 97 | + |
| 98 | + $persona = $DB->get_record_select('block_ai_chat_personas', $where, $search, 'id', IGNORE_MULTIPLE | IGNORE_MISSING); |
| 99 | + if ($persona) { |
| 100 | + // Persona already exists, map to existing record. |
| 101 | + $this->set_mapping('block_ai_chat_personas', $data['id'], $persona->id); |
| 102 | + return; |
| 103 | + } |
| 104 | + $data = (object)$data; |
| 105 | + // Restored personas are always of type user. |
| 106 | + $data->type = persona::TYPE_USER; |
| 107 | + $oldid = $data->id; |
| 108 | + $data->userid = $this->task->get_userid(); |
| 109 | + |
| 110 | + $newid = $DB->insert_record('block_ai_chat_personas', $data); |
| 111 | + $this->set_mapping('block_ai_chat_personas', $oldid, $newid); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Process a persona_selected element. |
| 116 | + * |
| 117 | + * @param array $data |
| 118 | + */ |
| 119 | + protected function process_persona_selected($data) { |
| 120 | + global $DB; |
| 121 | + $data = (object)$data; |
| 122 | + $data->contextid = $this->task->get_contextid(); |
| 123 | + $data->personasid = $this->get_mappingid('block_ai_chat_personas', $data->personasid); |
| 124 | + $DB->insert_record('block_ai_chat_personas_selected', $data); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Process a chat_option element. |
| 129 | + * |
| 130 | + * @param array $data |
| 131 | + */ |
| 132 | + protected function process_chat_option($data) { |
| 133 | + global $DB; |
| 134 | + $data = (object)$data; |
| 135 | + $data->contextid = $this->task->get_contextid(); |
| 136 | + $DB->insert_record('block_ai_chat_options', $data); |
| 137 | + } |
| 138 | +} |
0 commit comments