MDL-68252 question bank: for 'missingtype' questions, hide actions
[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 ],
ebe53aca
AA
215 'user preferences' => [
216 'datagenerator' => 'user_preferences',
217 'required' => array('user', 'preference', 'value'),
218 'switchids' => array('user' => 'userid')
219 ],
285c7036
TH
220 ];
221 }
222
223 /**
224 * Remove any empty custom fields, to avoid errors when creating the course.
225 *
226 * @param array $data
227 * @return array
228 */
229 protected function preprocess_course($data) {
230 foreach ($data as $fieldname => $value) {
231 if ($value === '' && strpos($fieldname, 'customfield_') === 0) {
232 unset($data[$fieldname]);
233 }
234 }
235 return $data;
236 }
237
238 /**
239 * If password is not set it uses the username.
240 *
241 * @param array $data
242 * @return array
243 */
244 protected function preprocess_user($data) {
245 if (!isset($data['password'])) {
246 $data['password'] = $data['username'];
247 }
248 return $data;
249 }
250
251 /**
252 * If contextlevel and reference are specified for cohort, transform them to the contextid.
253 *
254 * @param array $data
255 * @return array
256 */
257 protected function preprocess_cohort($data) {
258 if (isset($data['contextlevel'])) {
259 if (!isset($data['reference'])) {
260 throw new Exception('If field contextlevel is specified, field reference must also be present');
261 }
262 $context = $this->get_context($data['contextlevel'], $data['reference']);
263 unset($data['contextlevel']);
264 unset($data['reference']);
265 $data['contextid'] = $context->id;
266 }
267 return $data;
268 }
269
270 /**
271 * Preprocesses the creation of a grade item. Converts gradetype text to a number.
272 *
273 * @param array $data
274 * @return array
275 */
276 protected function preprocess_grade_item($data) {
277 global $CFG;
278 require_once("$CFG->libdir/grade/constants.php");
279
280 if (isset($data['gradetype'])) {
281 $data['gradetype'] = constant("GRADE_TYPE_" . strtoupper($data['gradetype']));
282 }
283
284 if (!empty($data['category']) && !empty($data['courseid'])) {
285 $cat = grade_category::fetch(array('fullname' => $data['category'], 'courseid' => $data['courseid']));
286 if (!$cat) {
287 throw new Exception('Could not resolve category with name "' . $data['category'] . '"');
288 }
289 unset($data['category']);
290 $data['categoryid'] = $cat->id;
291 }
292
293 return $data;
294 }
295
296 /**
297 * Adapter to modules generator.
298 *
299 * @throws Exception Custom exception for test writers
300 * @param array $data
301 * @return void
302 */
303 protected function process_activity($data) {
304 global $DB, $CFG;
305
306 // The the_following_exists() method checks that the field exists.
307 $activityname = $data['activity'];
308 unset($data['activity']);
309
310 // Convert scale name into scale id (negative number indicates using scale).
311 if (isset($data['grade']) && strlen($data['grade']) && !is_number($data['grade'])) {
312 $data['grade'] = - $this->get_scale_id($data['grade']);
313 require_once("$CFG->libdir/grade/constants.php");
314
315 if (!isset($data['gradetype'])) {
316 $data['gradetype'] = GRADE_TYPE_SCALE;
317 }
318 }
319
320 // We split $data in the activity $record and the course module $options.
321 $cmoptions = array();
322 $cmcolumns = $DB->get_columns('course_modules');
323 foreach ($cmcolumns as $key => $value) {
324 if (isset($data[$key])) {
325 $cmoptions[$key] = $data[$key];
326 }
327 }
328
329 // Custom exception.
330 try {
331 $this->datagenerator->create_module($activityname, $data, $cmoptions);
332 } catch (coding_exception $e) {
333 throw new Exception('\'' . $activityname . '\' activity can not be added using this step,' .
334 ' use the step \'I add a "ACTIVITY_OR_RESOURCE_NAME_STRING" to section "SECTION_NUMBER"\' instead');
335 }
336 }
337
338 /**
339 * Add a block to a page.
340 *
341 * @param array $data should mostly match the fields of the block_instances table.
342 * The block type is specified by blockname.
343 * The parentcontextid is set from contextlevel and reference.
344 * Missing values are filled in by testing_block_generator::prepare_record.
345 * $data is passed to create_block as both $record and $options. Normally
346 * the keys are different, so this is a way to let people set values in either place.
347 */
348 protected function process_block_instance($data) {
349
350 if (empty($data['blockname'])) {
351 throw new Exception('\'blocks\' requires the field \'block\' type to be specified');
352 }
353
354 if (empty($data['contextlevel'])) {
355 throw new Exception('\'blocks\' requires the field \'contextlevel\' to be specified');
356 }
357
358 if (!isset($data['reference'])) {
359 throw new Exception('\'blocks\' requires the field \'reference\' to be specified');
360 }
361
362 $context = $this->get_context($data['contextlevel'], $data['reference']);
363 $data['parentcontextid'] = $context->id;
364
365 // Pass $data as both $record and $options. I think that is unlikely to
366 // cause problems since the relevant key names are different.
367 // $options is not used in most blocks I have seen, but where it is, it is necessary.
368 $this->datagenerator->create_block($data['blockname'], $data, $data);
369 }
370
371 /**
372 * Creates language customisation.
373 *
374 * @throws Exception
375 * @throws dml_exception
376 * @param array $data
377 * @return void
378 */
379 protected function process_customlang($data) {
380 global $CFG, $DB, $USER;
381
382 require_once($CFG->dirroot . '/' . $CFG->admin . '/tool/customlang/locallib.php');
383 require_once($CFG->libdir . '/adminlib.php');
384
385 if (empty($data['component'])) {
386 throw new Exception('\'customlang\' requires the field \'component\' type to be specified');
387 }
388
389 if (empty($data['stringid'])) {
390 throw new Exception('\'customlang\' requires the field \'stringid\' to be specified');
391 }
392
393 if (!isset($data['value'])) {
394 throw new Exception('\'customlang\' requires the field \'value\' to be specified');
395 }
396
397 $now = time();
398
399 tool_customlang_utils::checkout($USER->lang);
400
401 $record = $DB->get_record_sql("SELECT s.*
402 FROM {tool_customlang} s
403 JOIN {tool_customlang_components} c ON s.componentid = c.id
404 WHERE c.name = ? AND s.lang = ? AND s.stringid = ?",
405 array($data['component'], $USER->lang, $data['stringid']));
406
407 if (empty($data['value']) && !is_null($record->local)) {
408 $record->local = null;
409 $record->modified = 1;
410 $record->outdated = 0;
411 $record->timecustomized = null;
412 $DB->update_record('tool_customlang', $record);
413 tool_customlang_utils::checkin($USER->lang);
414 }
415
416 if (!empty($data['value']) && $data['value'] != $record->local) {
417 $record->local = $data['value'];
418 $record->modified = 1;
419 $record->outdated = 0;
420 $record->timecustomized = $now;
421 $DB->update_record('tool_customlang', $record);
422 tool_customlang_utils::checkin($USER->lang);
423 }
424 }
425
426 /**
427 * Adapter to enrol_user() data generator.
428 *
429 * @throws Exception
430 * @param array $data
431 * @return void
432 */
433 protected function process_enrol_user($data) {
434 global $SITE;
435
436 if (empty($data['roleid'])) {
437 throw new Exception('\'course enrolments\' requires the field \'role\' to be specified');
438 }
439
440 if (!isset($data['userid'])) {
441 throw new Exception('\'course enrolments\' requires the field \'user\' to be specified');
442 }
443
444 if (!isset($data['courseid'])) {
445 throw new Exception('\'course enrolments\' requires the field \'course\' to be specified');
446 }
447
448 if (!isset($data['enrol'])) {
449 $data['enrol'] = 'manual';
450 }
451
452 if (!isset($data['timestart'])) {
453 $data['timestart'] = 0;
454 }
455
456 if (!isset($data['timeend'])) {
457 $data['timeend'] = 0;
458 }
459
460 if (!isset($data['status'])) {
461 $data['status'] = null;
462 }
463
464 // If the provided course shortname is the site shortname we consider it a system role assign.
465 if ($data['courseid'] == $SITE->id) {
466 // Frontpage course assign.
467 $context = context_course::instance($data['courseid']);
468 role_assign($data['roleid'], $data['userid'], $context->id);
469
470 } else {
471 // Course assign.
472 $this->datagenerator->enrol_user($data['userid'], $data['courseid'], $data['roleid'], $data['enrol'],
473 $data['timestart'], $data['timeend'], $data['status']);
474 }
475
476 }
477
478 /**
479 * Allows/denies a capability at the specified context
480 *
481 * @throws Exception
482 * @param array $data
483 * @return void
484 */
485 protected function process_permission_override($data) {
486
487 // Will throw an exception if it does not exist.
488 $context = $this->get_context($data['contextlevel'], $data['reference']);
489
490 switch ($data['permission']) {
491 case get_string('allow', 'role'):
492 $permission = CAP_ALLOW;
493 break;
494 case get_string('prevent', 'role'):
495 $permission = CAP_PREVENT;
496 break;
497 case get_string('prohibit', 'role'):
498 $permission = CAP_PROHIBIT;
499 break;
500 default:
501 throw new Exception('The \'' . $data['permission'] . '\' permission does not exist');
502 break;
503 }
504
505 if (is_null(get_capability_info($data['capability']))) {
506 throw new Exception('The \'' . $data['capability'] . '\' capability does not exist');
507 }
508
509 role_change_permission($data['roleid'], $context, $data['capability'], $permission);
510 }
511
512 /**
513 * Assigns a role to a user at system context
514 *
515 * Used by "system role assigns" can be deleted when
516 * system role assign will be deprecated in favour of
517 * "role assigns"
518 *
519 * @throws Exception
520 * @param array $data
521 * @return void
522 */
523 protected function process_system_role_assign($data) {
524
525 if (empty($data['roleid'])) {
526 throw new Exception('\'system role assigns\' requires the field \'role\' to be specified');
527 }
528
529 if (!isset($data['userid'])) {
530 throw new Exception('\'system role assigns\' requires the field \'user\' to be specified');
531 }
532
533 $context = context_system::instance();
534
535 $this->datagenerator->role_assign($data['roleid'], $data['userid'], $context->id);
536 }
537
538 /**
539 * Assigns a role to a user at the specified context
540 *
541 * @throws Exception
542 * @param array $data
543 * @return void
544 */
545 protected function process_role_assign($data) {
546
547 if (empty($data['roleid'])) {
548 throw new Exception('\'role assigns\' requires the field \'role\' to be specified');
549 }
550
551 if (!isset($data['userid'])) {
552 throw new Exception('\'role assigns\' requires the field \'user\' to be specified');
553 }
554
555 if (empty($data['contextlevel'])) {
556 throw new Exception('\'role assigns\' requires the field \'contextlevel\' to be specified');
557 }
558
559 if (!isset($data['reference'])) {
560 throw new Exception('\'role assigns\' requires the field \'reference\' to be specified');
561 }
562
563 // Getting the context id.
564 $context = $this->get_context($data['contextlevel'], $data['reference']);
565
566 $this->datagenerator->role_assign($data['roleid'], $data['userid'], $context->id);
567 }
568
569 /**
570 * Creates a role.
571 *
572 * @param array $data
573 * @return void
574 */
575 protected function process_role($data) {
576
577 // We require the user to fill the role shortname.
578 if (empty($data['shortname'])) {
579 throw new Exception('\'role\' requires the field \'shortname\' to be specified');
580 }
581
582 $this->datagenerator->create_role($data);
583 }
584
585 /**
586 * Adds members to cohorts
587 *
588 * @param array $data
589 * @return void
590 */
591 protected function process_cohort_member($data) {
592 cohort_add_member($data['cohortid'], $data['userid']);
593 }
594
595 /**
596 * Create a question category.
597 *
598 * @param array $data the row of data from the behat script.
599 */
600 protected function process_question_category($data) {
601 global $DB;
602
603 $context = $this->get_context($data['contextlevel'], $data['reference']);
604
605 // The way this class works, we have already looked up the given parent category
606 // name and found a matching category. However, it is possible, particularly
607 // for the 'top' category, for there to be several categories with the
608 // same name. So far one will have been picked at random, but we need
609 // the one from the right context. So, if we have the wrong category, try again.
610 // (Just fixing it here, rather than getting it right first time, is a bit
611 // of a bodge, but in general this class assumes that names are unique,
612 // and normally they are, so this was the easiest fix.)
613 if (!empty($data['parent'])) {
614 $foundparent = $DB->get_record('question_categories', ['id' => $data['parent']], '*', MUST_EXIST);
615 if ($foundparent->contextid != $context->id) {
616 $rightparentid = $DB->get_field('question_categories', 'id',
617 ['contextid' => $context->id, 'name' => $foundparent->name]);
618 if (!$rightparentid) {
619 throw new Exception('The specified question category with name "' . $foundparent->name .
620 '" does not exist in context "' . $context->get_context_name() . '"."');
621 }
622 $data['parent'] = $rightparentid;
623 }
624 }
625
626 $data['contextid'] = $context->id;
627 $this->datagenerator->get_plugin_generator('core_question')->create_question_category($data);
628 }
629
630 /**
631 * Create a question.
632 *
633 * Creating questions relies on the question/type/.../tests/helper.php mechanism.
634 * We start with test_question_maker::get_question_form_data($data['qtype'], $data['template'])
635 * and then overlay the values from any other fields of $data that are set.
636 *
637 * @param array $data the row of data from the behat script.
638 */
639 protected function process_question($data) {
640 if (array_key_exists('questiontext', $data)) {
641 $data['questiontext'] = array(
642 'text' => $data['questiontext'],
643 'format' => FORMAT_HTML,
644 );
645 }
646
647 if (array_key_exists('generalfeedback', $data)) {
648 $data['generalfeedback'] = array(
649 'text' => $data['generalfeedback'],
650 'format' => FORMAT_HTML,
651 );
652 }
653
654 $which = null;
655 if (!empty($data['template'])) {
656 $which = $data['template'];
657 }
658
659 $this->datagenerator->get_plugin_generator('core_question')->create_question($data['qtype'], $which, $data);
660 }
661
662 /**
663 * Adds user to contacts
664 *
665 * @param array $data
666 * @return void
667 */
668 protected function process_message_contacts($data) {
669 \core_message\api::add_contact($data['userid'], $data['contactid']);
670 }
671
672 /**
673 * Send a new message from user to contact in a private conversation
674 *
675 * @param array $data
676 * @return void
677 */
678 protected function process_private_messages(array $data) {
679 if (empty($data['format'])) {
680 $data['format'] = 'FORMAT_PLAIN';
681 }
682
683 if (!$conversationid = \core_message\api::get_conversation_between_users([$data['userid'], $data['contactid']])) {
684 $conversation = \core_message\api::create_conversation(
685 \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
686 [$data['userid'], $data['contactid']]
687 );
688 $conversationid = $conversation->id;
689 }
690 \core_message\api::send_message_to_conversation(
691 $data['userid'],
692 $conversationid,
693 $data['message'],
694 constant($data['format'])
695 );
696 }
697
698 /**
699 * Send a new message from user to a group conversation
700 *
701 * @param array $data
702 * @return void
703 */
704 protected function process_group_messages(array $data) {
705 global $DB;
706
707 if (empty($data['format'])) {
708 $data['format'] = 'FORMAT_PLAIN';
709 }
710
711 $group = $DB->get_record('groups', ['id' => $data['groupid']]);
712 $coursecontext = context_course::instance($group->courseid);
713 if (!$conversation = \core_message\api::get_conversation_by_area('core_group', 'groups', $data['groupid'],
714 $coursecontext->id)) {
715 $members = $DB->get_records_menu('groups_members', ['groupid' => $data['groupid']], '', 'userid, id');
716 $conversation = \core_message\api::create_conversation(
717 \core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
718 array_keys($members),
719 $group->name,
720 \core_message\api::MESSAGE_CONVERSATION_ENABLED,
721 'core_group',
722 'groups',
723 $group->id,
724 $coursecontext->id);
725 }
726 \core_message\api::send_message_to_conversation(
727 $data['userid'],
728 $conversation->id,
729 $data['message'],
730 constant($data['format'])
731 );
732 }
733
734 /**
735 * Mark a private conversation as favourite for user
736 *
737 * @param array $data
738 * @return void
739 */
740 protected function process_favourite_conversations(array $data) {
741 if (!$conversationid = \core_message\api::get_conversation_between_users([$data['userid'], $data['contactid']])) {
742 $conversation = \core_message\api::create_conversation(
743 \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
744 [$data['userid'], $data['contactid']]
745 );
746 $conversationid = $conversation->id;
747 }
748 \core_message\api::set_favourite_conversation($conversationid, $data['userid']);
749 }
750
751 /**
752 * Mute an existing group conversation for user
753 *
754 * @param array $data
755 * @return void
756 */
757 protected function process_mute_group_conversations(array $data) {
758 if (groups_is_member($data['groupid'], $data['userid'])) {
759 $context = context_course::instance($data['courseid']);
760 $conversation = \core_message\api::get_conversation_by_area(
761 'core_group',
762 'groups',
763 $data['groupid'],
764 $context->id
765 );
766 if ($conversation) {
767 \core_message\api::mute_conversation($data['userid'], $conversation->id);
768 }
769 }
770 }
771
772 /**
773 * Mute a private conversation for user
774 *
775 * @param array $data
776 * @return void
777 */
778 protected function process_mute_private_conversations(array $data) {
779 if (!$conversationid = \core_message\api::get_conversation_between_users([$data['userid'], $data['contactid']])) {
780 $conversation = \core_message\api::create_conversation(
781 \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
782 [$data['userid'], $data['contactid']]
783 );
784 $conversationid = $conversation->id;
785 }
786 \core_message\api::mute_conversation($data['userid'], $conversationid);
787 }
788
789 /**
790 * Transform indicators string into array.
791 *
792 * @param array $data
793 * @return array
794 */
795 protected function preprocess_analytics_model($data) {
796 $data['indicators'] = explode(',', $data['indicators']);
797 return $data;
798 }
799
800 /**
801 * Creates an analytics model
802 *
803 * @param target $data
804 * @return void
805 */
806 protected function process_analytics_model($data) {
807 \core_analytics\manager::create_declared_model($data);
808 }
ebe53aca
AA
809
810 /**
811 * Set a preference value for user
812 *
813 * @param array $data
814 * @return void
815 */
816 protected function process_user_preferences(array $data) {
817 set_user_preference($data['preference'], $data['value'], $data['userid']);
818 }
285c7036 819}