MDL-65620 core_message: Default notification preferences
[moodle.git] / lib / behat / classes / behat_core_generator.php
CommitLineData
285c7036
TH
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/**
18 * Data generators for acceptance testing.
19 *
20 * @package core
21 * @category test
22 * @copyright 2012 David MonllaĆ³
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
27
28defined('MOODLE_INTERNAL') || die();
29
30
31/**
32 * Behat data generator class for core entities.
33 *
34 * @package core
35 * @category test
36 * @copyright 2012 David MonllaĆ³
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class behat_core_generator extends behat_generator_base {
40
41 protected function get_creatable_entities(): array {
42 return [
43 'users' => [
44 'datagenerator' => 'user',
45 'required' => ['username'],
46 ],
47 'categories' => [
48 'datagenerator' => 'category',
49 'required' => ['idnumber'],
50 'switchids' => ['category' => 'parent'],
51 ],
52 'courses' => [
53 'datagenerator' => 'course',
54 'required' => ['shortname'],
55 'switchids' => ['category' => 'category'],
56 ],
57 'groups' => [
58 'datagenerator' => 'group',
59 'required' => ['idnumber', 'course'],
60 'switchids' => ['course' => 'courseid'],
61 ],
62 'groupings' => [
63 'datagenerator' => 'grouping',
64 'required' => ['idnumber', 'course'],
65 'switchids' => ['course' => 'courseid'],
66 ],
67 'course enrolments' => [
68 'datagenerator' => 'enrol_user',
69 'required' => ['user', 'course', 'role'],
70 'switchids' => ['user' => 'userid', 'course' => 'courseid', 'role' => 'roleid'],
71 ],
72 'custom field categories' => [
73 'datagenerator' => 'custom_field_category',
74 'required' => ['name', 'component', 'area', 'itemid'],
75 'switchids' => [],
76 ],
77 'custom fields' => [
78 'datagenerator' => 'custom_field',
79 'required' => ['name', 'category', 'type', 'shortname'],
80 'switchids' => [],
81 ],
82 'permission overrides' => [
83 'datagenerator' => 'permission_override',
84 'required' => ['capability', 'permission', 'role', 'contextlevel', 'reference'],
85 'switchids' => ['role' => 'roleid'],
86 ],
87 'system role assigns' => [
88 'datagenerator' => 'system_role_assign',
89 'required' => ['user', 'role'],
90 'switchids' => ['user' => 'userid', 'role' => 'roleid'],
91 ],
92 'role assigns' => [
93 'datagenerator' => 'role_assign',
94 'required' => ['user', 'role', 'contextlevel', 'reference'],
95 'switchids' => ['user' => 'userid', 'role' => 'roleid'],
96 ],
97 'activities' => [
98 'datagenerator' => 'activity',
99 'required' => ['activity', 'idnumber', 'course'],
100 'switchids' => ['course' => 'course', 'gradecategory' => 'gradecat', 'grouping' => 'groupingid'],
101 ],
102 'blocks' => [
103 'datagenerator' => 'block_instance',
104 'required' => ['blockname', 'contextlevel', 'reference'],
105 ],
106 'group members' => [
107 'datagenerator' => 'group_member',
108 'required' => ['user', 'group'],
109 'switchids' => ['user' => 'userid', 'group' => 'groupid'],
110 ],
111 'grouping groups' => [
112 'datagenerator' => 'grouping_group',
113 'required' => ['grouping', 'group'],
114 'switchids' => ['grouping' => 'groupingid', 'group' => 'groupid'],
115 ],
116 'cohorts' => [
117 'datagenerator' => 'cohort',
118 'required' => ['idnumber'],
119 ],
120 'cohort members' => [
121 'datagenerator' => 'cohort_member',
122 'required' => ['user', 'cohort'],
123 'switchids' => ['user' => 'userid', 'cohort' => 'cohortid'],
124 ],
125 'roles' => [
126 'datagenerator' => 'role',
127 'required' => ['shortname'],
128 ],
129 'grade categories' => [
130 'datagenerator' => 'grade_category',
131 'required' => ['fullname', 'course'],
132 'switchids' => ['course' => 'courseid', 'gradecategory' => 'parent'],
133 ],
134 'grade items' => [
135 'datagenerator' => 'grade_item',
136 'required' => ['course'],
137 'switchids' => [
138 'scale' => 'scaleid',
139 'outcome' => 'outcomeid',
140 'course' => 'courseid',
141 'gradecategory' => 'categoryid',
142 ],
143 ],
144 'grade outcomes' => [
145 'datagenerator' => 'grade_outcome',
146 'required' => ['shortname', 'scale'],
147 'switchids' => ['course' => 'courseid', 'gradecategory' => 'categoryid', 'scale' => 'scaleid'],
148 ],
149 'scales' => [
150 'datagenerator' => 'scale',
151 'required' => ['name', 'scale'],
152 'switchids' => ['course' => 'courseid'],
153 ],
154 'question categories' => [
155 'datagenerator' => 'question_category',
156 'required' => ['name', 'contextlevel', 'reference'],
157 'switchids' => ['questioncategory' => 'parent'],
158 ],
159 'questions' => [
160 'datagenerator' => 'question',
161 'required' => ['qtype', 'questioncategory', 'name'],
162 'switchids' => ['questioncategory' => 'category', 'user' => 'createdby'],
163 ],
164 'tags' => [
165 'datagenerator' => 'tag',
166 'required' => ['name'],
167 ],
168 'events' => [
169 'datagenerator' => 'event',
170 'required' => ['name', 'eventtype'],
171 'switchids' => [
172 'user' => 'userid',
173 'course' => 'courseid',
174 'category' => 'categoryid',
175 ],
176 ],
177 'message contacts' => [
178 'datagenerator' => 'message_contacts',
179 'required' => ['user', 'contact'],
180 'switchids' => ['user' => 'userid', 'contact' => 'contactid'],
181 ],
182 'private messages' => [
183 'datagenerator' => 'private_messages',
184 'required' => ['user', 'contact', 'message'],
185 'switchids' => ['user' => 'userid', 'contact' => 'contactid'],
186 ],
187 'favourite conversations' => [
188 'datagenerator' => 'favourite_conversations',
189 'required' => ['user', 'contact'],
190 'switchids' => ['user' => 'userid', 'contact' => 'contactid'],
191 ],
192 'group messages' => [
193 'datagenerator' => 'group_messages',
194 'required' => ['user', 'group', 'message'],
195 'switchids' => ['user' => 'userid', 'group' => 'groupid'],
196 ],
197 'muted group conversations' => [
198 'datagenerator' => 'mute_group_conversations',
199 'required' => ['user', 'group', 'course'],
200 'switchids' => ['user' => 'userid', 'group' => 'groupid', 'course' => 'courseid'],
201 ],
202 'muted private conversations' => [
203 'datagenerator' => 'mute_private_conversations',
204 'required' => ['user', 'contact'],
205 'switchids' => ['user' => 'userid', 'contact' => 'contactid'],
206 ],
207 'language customisations' => [
208 'datagenerator' => 'customlang',
209 'required' => ['component', 'stringid', 'value'],
210 ],
211 'analytics model' => [
212 'datagenerator' => 'analytics_model',
213 'required' => ['target', 'indicators', 'timesplitting', 'enabled'],
214 ],
215 ];
216 }
217
218 /**
219 * Remove any empty custom fields, to avoid errors when creating the course.
220 *
221 * @param array $data
222 * @return array
223 */
224 protected function preprocess_course($data) {
225 foreach ($data as $fieldname => $value) {
226 if ($value === '' && strpos($fieldname, 'customfield_') === 0) {
227 unset($data[$fieldname]);
228 }
229 }
230 return $data;
231 }
232
233 /**
234 * If password is not set it uses the username.
235 *
236 * @param array $data
237 * @return array
238 */
239 protected function preprocess_user($data) {
240 if (!isset($data['password'])) {
241 $data['password'] = $data['username'];
242 }
243 return $data;
244 }
245
246 /**
247 * If contextlevel and reference are specified for cohort, transform them to the contextid.
248 *
249 * @param array $data
250 * @return array
251 */
252 protected function preprocess_cohort($data) {
253 if (isset($data['contextlevel'])) {
254 if (!isset($data['reference'])) {
255 throw new Exception('If field contextlevel is specified, field reference must also be present');
256 }
257 $context = $this->get_context($data['contextlevel'], $data['reference']);
258 unset($data['contextlevel']);
259 unset($data['reference']);
260 $data['contextid'] = $context->id;
261 }
262 return $data;
263 }
264
265 /**
266 * Preprocesses the creation of a grade item. Converts gradetype text to a number.
267 *
268 * @param array $data
269 * @return array
270 */
271 protected function preprocess_grade_item($data) {
272 global $CFG;
273 require_once("$CFG->libdir/grade/constants.php");
274
275 if (isset($data['gradetype'])) {
276 $data['gradetype'] = constant("GRADE_TYPE_" . strtoupper($data['gradetype']));
277 }
278
279 if (!empty($data['category']) && !empty($data['courseid'])) {
280 $cat = grade_category::fetch(array('fullname' => $data['category'], 'courseid' => $data['courseid']));
281 if (!$cat) {
282 throw new Exception('Could not resolve category with name "' . $data['category'] . '"');
283 }
284 unset($data['category']);
285 $data['categoryid'] = $cat->id;
286 }
287
288 return $data;
289 }
290
291 /**
292 * Adapter to modules generator.
293 *
294 * @throws Exception Custom exception for test writers
295 * @param array $data
296 * @return void
297 */
298 protected function process_activity($data) {
299 global $DB, $CFG;
300
301 // The the_following_exists() method checks that the field exists.
302 $activityname = $data['activity'];
303 unset($data['activity']);
304
305 // Convert scale name into scale id (negative number indicates using scale).
306 if (isset($data['grade']) && strlen($data['grade']) && !is_number($data['grade'])) {
307 $data['grade'] = - $this->get_scale_id($data['grade']);
308 require_once("$CFG->libdir/grade/constants.php");
309
310 if (!isset($data['gradetype'])) {
311 $data['gradetype'] = GRADE_TYPE_SCALE;
312 }
313 }
314
315 // We split $data in the activity $record and the course module $options.
316 $cmoptions = array();
317 $cmcolumns = $DB->get_columns('course_modules');
318 foreach ($cmcolumns as $key => $value) {
319 if (isset($data[$key])) {
320 $cmoptions[$key] = $data[$key];
321 }
322 }
323
324 // Custom exception.
325 try {
326 $this->datagenerator->create_module($activityname, $data, $cmoptions);
327 } catch (coding_exception $e) {
328 throw new Exception('\'' . $activityname . '\' activity can not be added using this step,' .
329 ' use the step \'I add a "ACTIVITY_OR_RESOURCE_NAME_STRING" to section "SECTION_NUMBER"\' instead');
330 }
331 }
332
333 /**
334 * Add a block to a page.
335 *
336 * @param array $data should mostly match the fields of the block_instances table.
337 * The block type is specified by blockname.
338 * The parentcontextid is set from contextlevel and reference.
339 * Missing values are filled in by testing_block_generator::prepare_record.
340 * $data is passed to create_block as both $record and $options. Normally
341 * the keys are different, so this is a way to let people set values in either place.
342 */
343 protected function process_block_instance($data) {
344
345 if (empty($data['blockname'])) {
346 throw new Exception('\'blocks\' requires the field \'block\' type to be specified');
347 }
348
349 if (empty($data['contextlevel'])) {
350 throw new Exception('\'blocks\' requires the field \'contextlevel\' to be specified');
351 }
352
353 if (!isset($data['reference'])) {
354 throw new Exception('\'blocks\' requires the field \'reference\' to be specified');
355 }
356
357 $context = $this->get_context($data['contextlevel'], $data['reference']);
358 $data['parentcontextid'] = $context->id;
359
360 // Pass $data as both $record and $options. I think that is unlikely to
361 // cause problems since the relevant key names are different.
362 // $options is not used in most blocks I have seen, but where it is, it is necessary.
363 $this->datagenerator->create_block($data['blockname'], $data, $data);
364 }
365
366 /**
367 * Creates language customisation.
368 *
369 * @throws Exception
370 * @throws dml_exception
371 * @param array $data
372 * @return void
373 */
374 protected function process_customlang($data) {
375 global $CFG, $DB, $USER;
376
377 require_once($CFG->dirroot . '/' . $CFG->admin . '/tool/customlang/locallib.php');
378 require_once($CFG->libdir . '/adminlib.php');
379
380 if (empty($data['component'])) {
381 throw new Exception('\'customlang\' requires the field \'component\' type to be specified');
382 }
383
384 if (empty($data['stringid'])) {
385 throw new Exception('\'customlang\' requires the field \'stringid\' to be specified');
386 }
387
388 if (!isset($data['value'])) {
389 throw new Exception('\'customlang\' requires the field \'value\' to be specified');
390 }
391
392 $now = time();
393
394 tool_customlang_utils::checkout($USER->lang);
395
396 $record = $DB->get_record_sql("SELECT s.*
397 FROM {tool_customlang} s
398 JOIN {tool_customlang_components} c ON s.componentid = c.id
399 WHERE c.name = ? AND s.lang = ? AND s.stringid = ?",
400 array($data['component'], $USER->lang, $data['stringid']));
401
402 if (empty($data['value']) && !is_null($record->local)) {
403 $record->local = null;
404 $record->modified = 1;
405 $record->outdated = 0;
406 $record->timecustomized = null;
407 $DB->update_record('tool_customlang', $record);
408 tool_customlang_utils::checkin($USER->lang);
409 }
410
411 if (!empty($data['value']) && $data['value'] != $record->local) {
412 $record->local = $data['value'];
413 $record->modified = 1;
414 $record->outdated = 0;
415 $record->timecustomized = $now;
416 $DB->update_record('tool_customlang', $record);
417 tool_customlang_utils::checkin($USER->lang);
418 }
419 }
420
421 /**
422 * Adapter to enrol_user() data generator.
423 *
424 * @throws Exception
425 * @param array $data
426 * @return void
427 */
428 protected function process_enrol_user($data) {
429 global $SITE;
430
431 if (empty($data['roleid'])) {
432 throw new Exception('\'course enrolments\' requires the field \'role\' to be specified');
433 }
434
435 if (!isset($data['userid'])) {
436 throw new Exception('\'course enrolments\' requires the field \'user\' to be specified');
437 }
438
439 if (!isset($data['courseid'])) {
440 throw new Exception('\'course enrolments\' requires the field \'course\' to be specified');
441 }
442
443 if (!isset($data['enrol'])) {
444 $data['enrol'] = 'manual';
445 }
446
447 if (!isset($data['timestart'])) {
448 $data['timestart'] = 0;
449 }
450
451 if (!isset($data['timeend'])) {
452 $data['timeend'] = 0;
453 }
454
455 if (!isset($data['status'])) {
456 $data['status'] = null;
457 }
458
459 // If the provided course shortname is the site shortname we consider it a system role assign.
460 if ($data['courseid'] == $SITE->id) {
461 // Frontpage course assign.
462 $context = context_course::instance($data['courseid']);
463 role_assign($data['roleid'], $data['userid'], $context->id);
464
465 } else {
466 // Course assign.
467 $this->datagenerator->enrol_user($data['userid'], $data['courseid'], $data['roleid'], $data['enrol'],
468 $data['timestart'], $data['timeend'], $data['status']);
469 }
470
471 }
472
473 /**
474 * Allows/denies a capability at the specified context
475 *
476 * @throws Exception
477 * @param array $data
478 * @return void
479 */
480 protected function process_permission_override($data) {
481
482 // Will throw an exception if it does not exist.
483 $context = $this->get_context($data['contextlevel'], $data['reference']);
484
485 switch ($data['permission']) {
486 case get_string('allow', 'role'):
487 $permission = CAP_ALLOW;
488 break;
489 case get_string('prevent', 'role'):
490 $permission = CAP_PREVENT;
491 break;
492 case get_string('prohibit', 'role'):
493 $permission = CAP_PROHIBIT;
494 break;
495 default:
496 throw new Exception('The \'' . $data['permission'] . '\' permission does not exist');
497 break;
498 }
499
500 if (is_null(get_capability_info($data['capability']))) {
501 throw new Exception('The \'' . $data['capability'] . '\' capability does not exist');
502 }
503
504 role_change_permission($data['roleid'], $context, $data['capability'], $permission);
505 }
506
507 /**
508 * Assigns a role to a user at system context
509 *
510 * Used by "system role assigns" can be deleted when
511 * system role assign will be deprecated in favour of
512 * "role assigns"
513 *
514 * @throws Exception
515 * @param array $data
516 * @return void
517 */
518 protected function process_system_role_assign($data) {
519
520 if (empty($data['roleid'])) {
521 throw new Exception('\'system role assigns\' requires the field \'role\' to be specified');
522 }
523
524 if (!isset($data['userid'])) {
525 throw new Exception('\'system role assigns\' requires the field \'user\' to be specified');
526 }
527
528 $context = context_system::instance();
529
530 $this->datagenerator->role_assign($data['roleid'], $data['userid'], $context->id);
531 }
532
533 /**
534 * Assigns a role to a user at the specified context
535 *
536 * @throws Exception
537 * @param array $data
538 * @return void
539 */
540 protected function process_role_assign($data) {
541
542 if (empty($data['roleid'])) {
543 throw new Exception('\'role assigns\' requires the field \'role\' to be specified');
544 }
545
546 if (!isset($data['userid'])) {
547 throw new Exception('\'role assigns\' requires the field \'user\' to be specified');
548 }
549
550 if (empty($data['contextlevel'])) {
551 throw new Exception('\'role assigns\' requires the field \'contextlevel\' to be specified');
552 }
553
554 if (!isset($data['reference'])) {
555 throw new Exception('\'role assigns\' requires the field \'reference\' to be specified');
556 }
557
558 // Getting the context id.
559 $context = $this->get_context($data['contextlevel'], $data['reference']);
560
561 $this->datagenerator->role_assign($data['roleid'], $data['userid'], $context->id);
562 }
563
564 /**
565 * Creates a role.
566 *
567 * @param array $data
568 * @return void
569 */
570 protected function process_role($data) {
571
572 // We require the user to fill the role shortname.
573 if (empty($data['shortname'])) {
574 throw new Exception('\'role\' requires the field \'shortname\' to be specified');
575 }
576
577 $this->datagenerator->create_role($data);
578 }
579
580 /**
581 * Adds members to cohorts
582 *
583 * @param array $data
584 * @return void
585 */
586 protected function process_cohort_member($data) {
587 cohort_add_member($data['cohortid'], $data['userid']);
588 }
589
590 /**
591 * Create a question category.
592 *
593 * @param array $data the row of data from the behat script.
594 */
595 protected function process_question_category($data) {
596 global $DB;
597
598 $context = $this->get_context($data['contextlevel'], $data['reference']);
599
600 // The way this class works, we have already looked up the given parent category
601 // name and found a matching category. However, it is possible, particularly
602 // for the 'top' category, for there to be several categories with the
603 // same name. So far one will have been picked at random, but we need
604 // the one from the right context. So, if we have the wrong category, try again.
605 // (Just fixing it here, rather than getting it right first time, is a bit
606 // of a bodge, but in general this class assumes that names are unique,
607 // and normally they are, so this was the easiest fix.)
608 if (!empty($data['parent'])) {
609 $foundparent = $DB->get_record('question_categories', ['id' => $data['parent']], '*', MUST_EXIST);
610 if ($foundparent->contextid != $context->id) {
611 $rightparentid = $DB->get_field('question_categories', 'id',
612 ['contextid' => $context->id, 'name' => $foundparent->name]);
613 if (!$rightparentid) {
614 throw new Exception('The specified question category with name "' . $foundparent->name .
615 '" does not exist in context "' . $context->get_context_name() . '"."');
616 }
617 $data['parent'] = $rightparentid;
618 }
619 }
620
621 $data['contextid'] = $context->id;
622 $this->datagenerator->get_plugin_generator('core_question')->create_question_category($data);
623 }
624
625 /**
626 * Create a question.
627 *
628 * Creating questions relies on the question/type/.../tests/helper.php mechanism.
629 * We start with test_question_maker::get_question_form_data($data['qtype'], $data['template'])
630 * and then overlay the values from any other fields of $data that are set.
631 *
632 * @param array $data the row of data from the behat script.
633 */
634 protected function process_question($data) {
635 if (array_key_exists('questiontext', $data)) {
636 $data['questiontext'] = array(
637 'text' => $data['questiontext'],
638 'format' => FORMAT_HTML,
639 );
640 }
641
642 if (array_key_exists('generalfeedback', $data)) {
643 $data['generalfeedback'] = array(
644 'text' => $data['generalfeedback'],
645 'format' => FORMAT_HTML,
646 );
647 }
648
649 $which = null;
650 if (!empty($data['template'])) {
651 $which = $data['template'];
652 }
653
654 $this->datagenerator->get_plugin_generator('core_question')->create_question($data['qtype'], $which, $data);
655 }
656
657 /**
658 * Adds user to contacts
659 *
660 * @param array $data
661 * @return void
662 */
663 protected function process_message_contacts($data) {
664 \core_message\api::add_contact($data['userid'], $data['contactid']);
665 }
666
667 /**
668 * Send a new message from user to contact in a private conversation
669 *
670 * @param array $data
671 * @return void
672 */
673 protected function process_private_messages(array $data) {
674 if (empty($data['format'])) {
675 $data['format'] = 'FORMAT_PLAIN';
676 }
677
678 if (!$conversationid = \core_message\api::get_conversation_between_users([$data['userid'], $data['contactid']])) {
679 $conversation = \core_message\api::create_conversation(
680 \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
681 [$data['userid'], $data['contactid']]
682 );
683 $conversationid = $conversation->id;
684 }
685 \core_message\api::send_message_to_conversation(
686 $data['userid'],
687 $conversationid,
688 $data['message'],
689 constant($data['format'])
690 );
691 }
692
693 /**
694 * Send a new message from user to a group conversation
695 *
696 * @param array $data
697 * @return void
698 */
699 protected function process_group_messages(array $data) {
700 global $DB;
701
702 if (empty($data['format'])) {
703 $data['format'] = 'FORMAT_PLAIN';
704 }
705
706 $group = $DB->get_record('groups', ['id' => $data['groupid']]);
707 $coursecontext = context_course::instance($group->courseid);
708 if (!$conversation = \core_message\api::get_conversation_by_area('core_group', 'groups', $data['groupid'],
709 $coursecontext->id)) {
710 $members = $DB->get_records_menu('groups_members', ['groupid' => $data['groupid']], '', 'userid, id');
711 $conversation = \core_message\api::create_conversation(
712 \core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
713 array_keys($members),
714 $group->name,
715 \core_message\api::MESSAGE_CONVERSATION_ENABLED,
716 'core_group',
717 'groups',
718 $group->id,
719 $coursecontext->id);
720 }
721 \core_message\api::send_message_to_conversation(
722 $data['userid'],
723 $conversation->id,
724 $data['message'],
725 constant($data['format'])
726 );
727 }
728
729 /**
730 * Mark a private conversation as favourite for user
731 *
732 * @param array $data
733 * @return void
734 */
735 protected function process_favourite_conversations(array $data) {
736 if (!$conversationid = \core_message\api::get_conversation_between_users([$data['userid'], $data['contactid']])) {
737 $conversation = \core_message\api::create_conversation(
738 \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
739 [$data['userid'], $data['contactid']]
740 );
741 $conversationid = $conversation->id;
742 }
743 \core_message\api::set_favourite_conversation($conversationid, $data['userid']);
744 }
745
746 /**
747 * Mute an existing group conversation for user
748 *
749 * @param array $data
750 * @return void
751 */
752 protected function process_mute_group_conversations(array $data) {
753 if (groups_is_member($data['groupid'], $data['userid'])) {
754 $context = context_course::instance($data['courseid']);
755 $conversation = \core_message\api::get_conversation_by_area(
756 'core_group',
757 'groups',
758 $data['groupid'],
759 $context->id
760 );
761 if ($conversation) {
762 \core_message\api::mute_conversation($data['userid'], $conversation->id);
763 }
764 }
765 }
766
767 /**
768 * Mute a private conversation for user
769 *
770 * @param array $data
771 * @return void
772 */
773 protected function process_mute_private_conversations(array $data) {
774 if (!$conversationid = \core_message\api::get_conversation_between_users([$data['userid'], $data['contactid']])) {
775 $conversation = \core_message\api::create_conversation(
776 \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
777 [$data['userid'], $data['contactid']]
778 );
779 $conversationid = $conversation->id;
780 }
781 \core_message\api::mute_conversation($data['userid'], $conversationid);
782 }
783
784 /**
785 * Transform indicators string into array.
786 *
787 * @param array $data
788 * @return array
789 */
790 protected function preprocess_analytics_model($data) {
791 $data['indicators'] = explode(',', $data['indicators']);
792 return $data;
793 }
794
795 /**
796 * Creates an analytics model
797 *
798 * @param target $data
799 * @return void
800 */
801 protected function process_analytics_model($data) {
802 \core_analytics\manager::create_declared_model($data);
803 }
804}