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