@@ -72,15 +72,16 @@ public static function get_contexts_for_userid($userid) {
7272 // This block doesn't know who information is stored against unless it
7373 // is at the user context.
7474 $ contextlist = new \core_privacy \local \request \contextlist ();
75- $ contextuser = \context_user::instance ($ userid );
7675
77- $ sql = "SELECT contextid FROM {editor_atto_autosave} WHERE userid = :userid OR contextid = :contextid " ;
78- $ params = [
79- 'userid ' => $ userid ,
80- 'contextid ' => $ contextuser ->id ,
81- ];
76+ $ sql = "SELECT
77+ c.id
78+ FROM {editor_atto_autosave} eas
79+ JOIN {context} c ON c.id = eas.contextid
80+ WHERE contextlevel = :contextuser AND c.instanceid = :userid " ;
81+ $ contextlist ->add_from_sql ($ sql , ['contextuser ' => CONTEXT_USER , 'userid ' => $ userid ]);
8282
83- $ contextlist ->add_from_sql ($ sql , $ params );
83+ $ sql = "SELECT contextid FROM {editor_atto_autosave} WHERE userid = :userid " ;
84+ $ contextlist ->add_from_sql ($ sql , ['userid ' => $ userid ]);
8485
8586 return $ contextlist ;
8687 }
@@ -95,20 +96,34 @@ public static function export_user_data(approved_contextlist $contextlist) {
9596
9697 $ user = $ contextlist ->get_user ();
9798
99+ $ sql = "SELECT *
100+ FROM {editor_atto_autosave}
101+ WHERE userid = :userid AND contextid {$ contextsql }" ;
102+
98103 list ($ contextsql , $ contextparams ) = $ DB ->get_in_or_equal ($ contextlist ->get_contextids (), SQL_PARAMS_NAMED );
99104 $ contextparams ['userid ' ] = $ contextlist ->get_user ()->id ;
105+ $ autosaves = $ DB ->get_recordset_sql ($ sql , $ contextparams );
106+ self ::export_autosaves ($ user , $ autosaves );
100107
101108 $ sql = "SELECT *
102109 FROM {editor_atto_autosave}
103- WHERE
104- (userid = :userid AND contextid {$ contextsql })
105- OR
106- (contextid = :usercontext) " ;
110+ JOIN {context} c ON c.id = eas.contextid
111+ WHERE c.id {$ contextsql } AND contextlevel = :contextuser AND c.instanceid = :userid " ;
107112
108- $ usercontext = \context_user::instance ($ user ->id );
109- $ contextparams ['usercontext ' ] = $ usercontext ->id ;
113+ list ($ contextsql , $ contextparams ) = $ DB ->get_in_or_equal ($ contextlist ->get_contextids (), SQL_PARAMS_NAMED );
114+ $ contextparams ['userid ' ] = $ contextlist ->get_user ()->id ;
115+ $ contextparams ['contextuser ' ] = CONTEXT_USER ;
110116 $ autosaves = $ DB ->get_recordset_sql ($ sql , $ contextparams );
117+ self ::export_autosaves ($ user , $ autosaves );
118+ }
111119
120+ /**
121+ * Export all autosave records in the recordset, and close the recordset when finished.
122+ *
123+ * @param \stdClass $user The user whose data is to be exported
124+ * @param \moodle_recordset $autosaves The recordset containing the data to export
125+ */
126+ protected static function export_autosaves (\stdClass $ user , \moodle_recordset $ autosaves ) {
112127 foreach ($ autosaves as $ autosave ) {
113128 $ context = \context::instance_by_id ($ autosave ->contextid );
114129 $ subcontext = [
0 commit comments