on-demand release 4.0dev+
[moodle.git] / mod / chat / backup / moodle2 / restore_chat_stepslib.php
CommitLineData
ef8c7358 1<?php
ef8c7358
DM
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/**
a6be3193 18 * @package mod_chat
ef8c7358
DM
19 * @subpackage backup-moodle2
20 * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 */
23
24/**
25 * Define all the restore steps that will be used by the restore_chat_activity_task
26 */
27
28/**
29 * Structure step to restore one chat activity
30 */
31class restore_chat_activity_structure_step extends restore_activity_structure_step {
32
33 protected function define_structure() {
34
35 $paths = array();
36 $userinfo = $this->get_setting_value('userinfo');
37
38 $paths[] = new restore_path_element('chat', '/activity/chat');
39 if ($userinfo) {
40 $paths[] = new restore_path_element('chat_message', '/activity/chat/messages/message');
41 }
42
c52b4db5 43 // Return the paths wrapped into standard activity structure.
ef8c7358
DM
44 return $this->prepare_activity_structure($paths);
45 }
46
47 protected function process_chat($data) {
48 global $DB;
49
50 $data = (object)$data;
51 $oldid = $data->id;
52 $data->course = $this->get_courseid();
53
0d14fcbc
AA
54 // Any changes to the list of dates that needs to be rolled should be same during course restore and course reset.
55 // See MDL-9367.
ef8c7358
DM
56 $data->chattime = $this->apply_date_offset($data->chattime);
57
c52b4db5 58 // Insert the chat record.
ef8c7358 59 $newitemid = $DB->insert_record('chat', $data);
c52b4db5 60 // Immediately after inserting "activity" record, call this.
ef8c7358
DM
61 $this->apply_activity_instance($newitemid);
62 }
63
64 protected function process_chat_message($data) {
65 global $DB;
66
67 $data = (object)$data;
68 $oldid = $data->id;
69 $data->chatid = $this->get_new_parentid('chat');
70 $data->userid = $this->get_mappingid('user', $data->userid);
71 $data->groupid = $this->get_mappingid('group', $data->groupid);
72 $data->message = $data->message_text;
307dce6c 73 $data->issystem = $data->system;
ef8c7358
DM
74
75 $newitemid = $DB->insert_record('chat_messages', $data);
c52b4db5 76 $this->set_mapping('chat_message', $oldid, $newitemid); // Because of decode.
ef8c7358
DM
77 }
78
79 protected function after_execute() {
c52b4db5 80 // Add chat related files, no need to match by itemname (just internally handled context).
ef8c7358
DM
81 $this->add_related_files('mod_chat', 'intro', null);
82 }
83}