diff --git a/lang/en/question.php b/lang/en/question.php index 25a204ad38b..7dc290e5ef7 100644 --- a/lang/en/question.php +++ b/lang/en/question.php @@ -149,7 +149,7 @@ $string['eventquestioncategoryviewed'] = 'Question category viewed'; $string['eventquestioncreated'] = 'Question created'; $string['eventquestiondeleted'] = 'Question deleted'; $string['eventquestionmoved'] = 'Question moved'; -$string['eventquestionpreviewed'] = 'Question previewed'; +$string['eventquestionviewed'] = 'Question viewed'; $string['eventquestionsexported'] = 'Questions exported'; $string['eventquestionsimported'] = 'Questions imported'; $string['eventquestionupdated'] = 'Question updated'; diff --git a/lib/classes/event/question_base.php b/lib/classes/event/question_base.php index 3690a4cdf19..2f07b8587cb 100644 --- a/lib/classes/event/question_base.php +++ b/lib/classes/event/question_base.php @@ -18,7 +18,7 @@ * Base class for question events. * * @package core - * @copyright 2016 Stephen Bourget + * @copyright 2019 Stephen Bourget * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ @@ -30,16 +30,17 @@ defined('MOODLE_INTERNAL') || die(); * Base class for question events. * * @package core - * @since Moodle 3.2 - * @copyright 2016 Stephen Bourget + * @since Moodle 3.7 + * @copyright 2019 Stephen Bourget * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class question_base extends base { +abstract class question_base extends base { /** * Init method. */ protected function init() { + $this->data['objecttable'] = 'question'; $this->data['edulevel'] = self::LEVEL_TEACHING; } @@ -52,12 +53,15 @@ class question_base extends base { if ($this->courseid) { $cat = $this->other['categoryid'] . ',' . $this->contextid; if ($this->contextlevel == CONTEXT_MODULE) { - return new \moodle_url('/question/edit.php', array('cmid' => $this->contextinstanceid, 'cat' => $cat, 'lastchanged' => $this->objectid)); + return new \moodle_url('/question/edit.php', + ['cmid' => $this->contextinstanceid, 'cat' => $cat, 'lastchanged' => $this->objectid]); } - return new \moodle_url('/question/edit.php', array('courseid' => $this->courseid, 'cat' => $cat, 'lastchanged' => $this->objectid)); + return new \moodle_url('/question/edit.php', + ['courseid' => $this->courseid, 'cat' => $cat, 'lastchanged' => $this->objectid]); } // Lets try viewing from the frontpage for contexts above course. - return new \moodle_url('/question/category.php', array('courseid' => SITEID, 'edit' => $this->other['categoryid'], 'lastchanged' => $this->objectid)); + return new \moodle_url('/question/category.php', + ['courseid' => SITEID, 'edit' => $this->other['categoryid'], 'lastchanged' => $this->objectid]); } /** @@ -80,7 +84,7 @@ class question_base extends base { * @return array */ public static function get_objectid_mapping() { - return array('db' => 'question', 'restore' => 'question'); + return ['db' => 'question', 'restore' => 'question']; } /** @@ -90,9 +94,36 @@ class question_base extends base { */ public static function get_other_mapping() { - $othermapped = array(); - $othermapped['categoryid'] = array('db' => 'question_categories', 'restore' => 'question_categories'); + $othermapped = []; + $othermapped['categoryid'] = ['db' => 'question_categories', 'restore' => 'question_categories']; return $othermapped; } + + /** + * Create a event from question object + * + * @param object $question + * @param object|null $context + * @param array|null $other will override the categoryid pre-filled out on the first line. + * @return base + * @throws \coding_exception + */ + public static function create_from_question_instance($question, $context = null, $other = null) { + + $params = ['objectid' => $question->id, 'other' => ['categoryid' => $question->category]]; + + if (!empty($question->contextid)) { + $params['contextid'] = $question->contextid; + } + + $params['context'] = $context; + + if (!empty($other)) { + $params['other'] = $other; + } + + $event = self::create($params); + return $event; + } } diff --git a/lib/classes/event/question_category_base.php b/lib/classes/event/question_category_base.php index dce12b02c53..fa1ffc79c3a 100644 --- a/lib/classes/event/question_category_base.php +++ b/lib/classes/event/question_category_base.php @@ -18,7 +18,7 @@ * Base class for question category events. * * @package core - * @copyright 2016 Stephen Bourget + * @copyright 2019 Stephen Bourget * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ @@ -30,11 +30,11 @@ defined('MOODLE_INTERNAL') || die(); * Base class for question category events * * @package core - * @since Moodle 3.6 - * @copyright 2016 Stephen Bourget + * @since Moodle 3.7 + * @copyright 2019 Stephen Bourget * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class question_category_base extends base { +abstract class question_category_base extends base { /** * Init method. @@ -53,20 +53,43 @@ class question_category_base extends base { if ($this->courseid) { $cat = $this->objectid . ',' . $this->contextid; if ($this->contextlevel == CONTEXT_MODULE) { - return new \moodle_url('/question/edit.php', array('cmid' => $this->contextinstanceid, 'cat' => $cat)); + return new \moodle_url('/question/edit.php', ['cmid' => $this->contextinstanceid, 'cat' => $cat]); } - return new \moodle_url('/question/edit.php', array('courseid' => $this->courseid, 'cat' => $cat)); + return new \moodle_url('/question/edit.php', ['courseid' => $this->courseid, 'cat' => $cat]); } // Lets try viewing from the frontpage for contexts above course. - return new \moodle_url('/question/category.php', array('courseid' => SITEID, 'edit' => $this->objectid)); + return new \moodle_url('/question/category.php', ['courseid' => SITEID, 'edit' => $this->objectid]); } /** * Returns DB mappings used with backup / restore. + * * @return array */ public static function get_objectid_mapping() { - return array('db' => 'question_categories', 'restore' => 'question_categories'); + return ['db' => 'question_categories', 'restore' => 'question_categories']; + } + + /** + * Create a event from question category object + * + * @param object $category + * @param object|null $context + * @return base + * @throws \coding_exception + */ + public static function create_from_question_category_instance($category, $context = null) { + + $params = ['objectid' => $category->id]; + + if (!empty($category->contextid)) { + $params['contextid'] = $category->contextid; + } + + $params['context'] = $context; + + $event = self::create($params); + return $event; } } diff --git a/lib/classes/event/question_category_created.php b/lib/classes/event/question_category_created.php index 98388719d5a..2b6baf14e05 100644 --- a/lib/classes/event/question_category_created.php +++ b/lib/classes/event/question_category_created.php @@ -40,9 +40,8 @@ class question_category_created extends question_category_base { * Init method. */ protected function init() { - $this->data['objecttable'] = 'question_categories'; + parent::init(); $this->data['crud'] = 'c'; - $this->data['edulevel'] = self::LEVEL_TEACHING; } /** diff --git a/lib/classes/event/question_category_deleted.php b/lib/classes/event/question_category_deleted.php index 8e6571f4738..94c5e76a38c 100644 --- a/lib/classes/event/question_category_deleted.php +++ b/lib/classes/event/question_category_deleted.php @@ -18,7 +18,7 @@ * Question category deleted event. * * @package core - * @copyright 2016 Stephen Bourget + * @copyright 2019 Stephen Bourget * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ @@ -30,8 +30,8 @@ defined('MOODLE_INTERNAL') || die(); * Question category deleted event class. * * @package core - * @since Moodle 3.2 - * @copyright 2016 Stephen Bourget + * @since Moodle 3.7 + * @copyright 2019 Stephen Bourget * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class question_category_deleted extends question_category_base { @@ -40,9 +40,8 @@ class question_category_deleted extends question_category_base { * Init method. */ protected function init() { - $this->data['objecttable'] = 'question_categories'; + parent::init(); $this->data['crud'] = 'd'; - $this->data['edulevel'] = self::LEVEL_TEACHING; } /** diff --git a/lib/classes/event/question_category_moved.php b/lib/classes/event/question_category_moved.php index b3d603ba86b..05e867b9e1b 100644 --- a/lib/classes/event/question_category_moved.php +++ b/lib/classes/event/question_category_moved.php @@ -18,7 +18,7 @@ * Question category moved event. * * @package core - * @copyright 2016 Stephen Bourget + * @copyright 2019 Stephen Bourget * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ @@ -30,8 +30,8 @@ defined('MOODLE_INTERNAL') || die(); * Question category moved event class. * * @package core - * @since Moodle 3.2 - * @copyright 2016 Stephen Bourget + * @since Moodle 3.7 + * @copyright 2019 Stephen Bourget * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class question_category_moved extends question_category_base { @@ -40,9 +40,8 @@ class question_category_moved extends question_category_base { * Init method. */ protected function init() { - $this->data['objecttable'] = 'question_categories'; + parent::init(); $this->data['crud'] = 'u'; - $this->data['edulevel'] = self::LEVEL_TEACHING; } /** diff --git a/lib/classes/event/question_category_updated.php b/lib/classes/event/question_category_updated.php index fff098daf14..e5b0e244919 100644 --- a/lib/classes/event/question_category_updated.php +++ b/lib/classes/event/question_category_updated.php @@ -18,7 +18,7 @@ * Question category updated event. * * @package core - * @copyright 2016 Stephen Bourget + * @copyright 2019 Stephen Bourget * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ @@ -30,8 +30,8 @@ defined('MOODLE_INTERNAL') || die(); * Question category updated event class. * * @package core - * @since Moodle 3.6 - * @copyright 2016 Stephen Bourget + * @since Moodle 3.7 + * @copyright 2019 Stephen Bourget * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class question_category_updated extends question_category_base { @@ -40,9 +40,8 @@ class question_category_updated extends question_category_base { * Init method. */ protected function init() { - $this->data['objecttable'] = 'question_categories'; + parent::init(); $this->data['crud'] = 'u'; - $this->data['edulevel'] = self::LEVEL_TEACHING; } /** diff --git a/lib/classes/event/question_category_viewed.php b/lib/classes/event/question_category_viewed.php index c5558fa60a7..d6595eb5ddb 100644 --- a/lib/classes/event/question_category_viewed.php +++ b/lib/classes/event/question_category_viewed.php @@ -18,7 +18,7 @@ * Question category viewed event. * * @package core - * @copyright 2016 Stephen Bourget + * @copyright 2019 Stephen Bourget * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ @@ -30,8 +30,8 @@ defined('MOODLE_INTERNAL') || die(); * Question category viewed event class. * * @package core - * @since Moodle 3.2 - * @copyright 2016 Stephen Bourget + * @since Moodle 3.7 + * @copyright 2019 Stephen Bourget * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class question_category_viewed extends question_category_base { @@ -40,9 +40,8 @@ class question_category_viewed extends question_category_base { * Init method. */ protected function init() { - $this->data['objecttable'] = 'question_categories'; + parent::init(); $this->data['crud'] = 'r'; - $this->data['edulevel'] = self::LEVEL_TEACHING; } /** diff --git a/lib/classes/event/question_created.php b/lib/classes/event/question_created.php index 2b884de55c3..b05e1fb151d 100644 --- a/lib/classes/event/question_created.php +++ b/lib/classes/event/question_created.php @@ -18,7 +18,7 @@ * Question created event. * * @package core - * @copyright 2016 Stephen Bourget + * @copyright 2019 Stephen Bourget * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ @@ -36,8 +36,8 @@ defined('MOODLE_INTERNAL') || die(); * } * * @package core - * @since Moodle 3.2 - * @copyright 2016 Stephen Bourget + * @since Moodle 3.7 + * @copyright 2019 Stephen Bourget * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class question_created extends question_base { @@ -46,9 +46,8 @@ class question_created extends question_base { * Init method. */ protected function init() { - $this->data['objecttable'] = 'question'; + parent::init(); $this->data['crud'] = 'c'; - $this->data['edulevel'] = self::LEVEL_TEACHING; } /** @@ -66,8 +65,8 @@ class question_created extends question_base { * @return string */ public function get_description() { - return "The user with id '$this->userid' created a question with the id of '$this->objectid'". - " in the category with the id '".$this->other['categoryid']."'."; + return "The user with id '$this->userid' created a question with the id of '$this->objectid'" . + " in the category with the id '" . $this->other['categoryid'] . "'."; } /** @@ -78,11 +77,11 @@ class question_created extends question_base { public function get_url() { if ($this->courseid) { if ($this->contextlevel == CONTEXT_MODULE) { - return new \moodle_url('/question/preview.php', array('cmid' => $this->contextinstanceid, 'id' => $this->objectid)); + return new \moodle_url('/question/preview.php', ['cmid' => $this->contextinstanceid, 'id' => $this->objectid]); } - return new \moodle_url('/question/preview.php', array('courseid' => $this->courseid, 'id' => $this->objectid)); + return new \moodle_url('/question/preview.php', ['courseid' => $this->courseid, 'id' => $this->objectid]); } // Lets try editing from the frontpage for contexts above course. - return new \moodle_url('/question/preview.php', array('courseid' => SITEID, 'id' => $this->objectid)); + return new \moodle_url('/question/preview.php', ['courseid' => SITEID, 'id' => $this->objectid]); } } diff --git a/lib/classes/event/question_deleted.php b/lib/classes/event/question_deleted.php index ba7c34dcb91..17363d10147 100644 --- a/lib/classes/event/question_deleted.php +++ b/lib/classes/event/question_deleted.php @@ -18,7 +18,7 @@ * Question deleted event. * * @package core - * @copyright 2016 Stephen Bourget + * @copyright 2019 Stephen Bourget * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ @@ -36,8 +36,8 @@ defined('MOODLE_INTERNAL') || die(); * } * * @package core - * @since Moodle 3.2 - * @copyright 2016 Stephen Bourget + * @since Moodle 3.7 + * @copyright 2019 Stephen Bourget * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class question_deleted extends question_base { @@ -46,9 +46,8 @@ class question_deleted extends question_base { * Init method. */ protected function init() { - $this->data['objecttable'] = 'question'; + parent::init(); $this->data['crud'] = 'd'; - $this->data['edulevel'] = self::LEVEL_TEACHING; } /** @@ -66,8 +65,8 @@ class question_deleted extends question_base { * @return string */ public function get_description() { - return "The user with id '$this->userid' deleted the question with id '$this->objectid'". - " from the category with the id '".$this->other['categoryid']."'."; + return "The user with id '$this->userid' deleted the question with id '$this->objectid'" . + " from the category with the id '" . $this->other['categoryid'] . "'."; } /** diff --git a/lib/classes/event/question_moved.php b/lib/classes/event/question_moved.php index 6f1ea7a2d9c..a798f813df2 100644 --- a/lib/classes/event/question_moved.php +++ b/lib/classes/event/question_moved.php @@ -18,7 +18,7 @@ * Question moved event. * * @package core - * @copyright 2016 Stephen Bourget + * @copyright 2019 Stephen Bourget * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ @@ -32,23 +32,23 @@ defined('MOODLE_INTERNAL') || die(); * @property-read array $other { * Extra information about the event. * - * - int categoryid: The ID of the category where the question resides + * - int newcategoryid: The ID of the new category of the question + * - int oldcategoryid: The ID of the old category of the question * } * * @package core - * @since Moodle 3.6 - * @copyright 2016 Stephen Bourget + * @since Moodle 3.7 + * @copyright 2019 Stephen Bourget * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class question_moved extends base { +class question_moved extends question_base { /** * Init method. */ protected function init() { - $this->data['objecttable'] = 'question'; + parent::init(); $this->data['crud'] = 'u'; - $this->data['edulevel'] = self::LEVEL_TEACHING; } /** @@ -66,9 +66,9 @@ class question_moved extends base { * @return string */ public function get_description() { - return "The user with id '$this->userid' moved the question with the id of '$this->objectid'". - " from the category with the id of '".$this->other['oldcategoryid']. - "' to the category with the id of '".$this->other['newcategoryid']."'."; + return "The user with id '$this->userid' moved the question with the id of '$this->objectid'" . + " from the category with the id of '" . $this->other['oldcategoryid'] . + "' to the category with the id of '" . $this->other['newcategoryid'] . "'."; } /** @@ -80,12 +80,15 @@ class question_moved extends base { if ($this->courseid) { $cat = $this->other['newcategoryid'] . ',' . $this->contextid; if ($this->contextlevel == CONTEXT_MODULE) { - return new \moodle_url('/question/edit.php', array('cmid' => $this->contextinstanceid, 'cat' => $cat, 'lastchanged' => $this->objectid)); + return new \moodle_url('/question/edit.php', + ['cmid' => $this->contextinstanceid, 'cat' => $cat, 'lastchanged' => $this->objectid]); } - return new \moodle_url('/question/edit.php', array('courseid' => $this->courseid, 'cat' => $cat, 'lastchanged' => $this->objectid)); + return new \moodle_url('/question/edit.php', + ['courseid' => $this->courseid, 'cat' => $cat, 'lastchanged' => $this->objectid]); } // Lets try viewing from the frontpage for contexts above course. - return new \moodle_url('/question/category.php', array('courseid' => SITEID, 'edit' => $this->other['newcategoryid'], 'lastchanged' => $this->objectid)); + return new \moodle_url('/question/category.php', + ['courseid' => SITEID, 'edit' => $this->other['newcategoryid'], 'lastchanged' => $this->objectid]); } /** @@ -95,7 +98,6 @@ class question_moved extends base { * @return void */ protected function validate_data() { - parent::validate_data(); if (!isset($this->other['oldcategoryid'])) { throw new \coding_exception('The \'oldcategoryid\' must be set in \'other\'.'); @@ -111,7 +113,7 @@ class question_moved extends base { * @return array */ public static function get_objectid_mapping() { - return array('db' => 'question', 'restore' => 'question'); + return ['db' => 'question', 'restore' => 'question']; } /** @@ -121,9 +123,9 @@ class question_moved extends base { */ public static function get_other_mapping() { - $othermapped = array(); - $othermapped['newcategoryid'] = array('db' => 'question_categories', 'restore' => 'question_categories'); - $othermapped['oldcategoryid'] = array('db' => 'question_categories', 'restore' => 'question_categories'); + $othermapped = []; + $othermapped['newcategoryid'] = ['db' => 'question_categories', 'restore' => 'question_categories']; + $othermapped['oldcategoryid'] = ['db' => 'question_categories', 'restore' => 'question_categories']; return $othermapped; } diff --git a/lib/classes/event/question_updated.php b/lib/classes/event/question_updated.php index 2006c819ec3..4e5bdf93fa8 100644 --- a/lib/classes/event/question_updated.php +++ b/lib/classes/event/question_updated.php @@ -18,7 +18,7 @@ * Question updated event. * * @package core - * @copyright 2016 Stephen Bourget + * @copyright 2019 Stephen Bourget * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ @@ -36,8 +36,8 @@ defined('MOODLE_INTERNAL') || die(); * } * * @package core - * @since Moodle 3.6 - * @copyright 2016 Stephen Bourget + * @since Moodle 3.7 + * @copyright 2019 Stephen Bourget * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class question_updated extends question_base { @@ -46,9 +46,8 @@ class question_updated extends question_base { * Init method. */ protected function init() { - $this->data['objecttable'] = 'question'; + parent::init(); $this->data['crud'] = 'u'; - $this->data['edulevel'] = self::LEVEL_TEACHING; } /** diff --git a/lib/classes/event/question_previewed.php b/lib/classes/event/question_viewed.php similarity index 78% rename from lib/classes/event/question_previewed.php rename to lib/classes/event/question_viewed.php index b36508c66c2..9ef0d37ee39 100644 --- a/lib/classes/event/question_previewed.php +++ b/lib/classes/event/question_viewed.php @@ -18,7 +18,7 @@ * Question previewed event. * * @package core - * @copyright 2016 Stephen Bourget + * @copyright 2019 Stephen Bourget * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ @@ -36,19 +36,18 @@ defined('MOODLE_INTERNAL') || die(); * } * * @package core - * @since Moodle 3.2 - * @copyright 2016 Stephen Bourget + * @since Moodle 3.7 + * @copyright 2019 Stephen Bourget * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class question_previewed extends question_base { +class question_viewed extends question_base { /** * Init method. */ protected function init() { - $this->data['objecttable'] = 'question'; + parent::init(); $this->data['crud'] = 'r'; - $this->data['edulevel'] = self::LEVEL_TEACHING; } /** @@ -57,7 +56,7 @@ class question_previewed extends question_base { * @return string */ public static function get_name() { - return get_string('eventquestionpreviewed', 'question'); + return get_string('eventquestionviewed', 'question'); } /** @@ -66,7 +65,7 @@ class question_previewed extends question_base { * @return string */ public function get_description() { - return "The user with id '$this->userid' previewed the question with the id of '$this->objectid'."; + return "The user with id '$this->userid' viewed the question with the id of '$this->objectid'."; } } diff --git a/lib/classes/event/questions_exported.php b/lib/classes/event/questions_exported.php index b42735389cd..adc3117a481 100644 --- a/lib/classes/event/questions_exported.php +++ b/lib/classes/event/questions_exported.php @@ -18,7 +18,7 @@ * Questions exported event. * * @package core - * @copyright 2016 Stephen Bourget + * @copyright 2019 Stephen Bourget * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ @@ -29,12 +29,19 @@ defined('MOODLE_INTERNAL') || die(); /** * Question category exported event class. * + * @property-read array $other { + * Extra information about the event. + * + * - int categoryid: The ID of the category where the question resides + * - string format: The format of file export + * } + * * @package core - * @since Moodle 3.2 - * @copyright 2016 Stephen Bourget + * @since Moodle 3.7 + * @copyright 2019 Stephen Bourget * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class questions_exported extends base { +class questions_exported extends question_base { /** * Init method. @@ -59,8 +66,8 @@ class questions_exported extends base { * @return string */ public function get_description() { - return "The user with id '$this->userid' exported questions in '". $this->other['format']. - "' format from the category with id '".$this->other['categoryid']."'."; + return "The user with id '$this->userid' exported questions in '" . $this->other['format'] . + "' format from the category with id '" . $this->other['categoryid'] . "'."; } /** @@ -72,16 +79,18 @@ class questions_exported extends base { if ($this->courseid) { $cat = $this->other['categoryid'] . ',' . $this->contextid; if ($this->contextlevel == CONTEXT_MODULE) { - return new \moodle_url('/question/edit.php', array('cmid' => $this->contextinstanceid, 'cat' => $cat)); + return new \moodle_url('/question/edit.php', ['cmid' => $this->contextinstanceid, 'cat' => $cat]); } - return new \moodle_url('/question/edit.php', array('courseid' => $this->courseid, 'cat' => $cat)); + return new \moodle_url('/question/edit.php', ['courseid' => $this->courseid, 'cat' => $cat]); } - return new \moodle_url('/question/category.php', array('courseid' => SITEID, 'edit' => $this->other['categoryid'])); + return new \moodle_url('/question/category.php', ['courseid' => SITEID, 'edit' => $this->other['categoryid']]); } /** * Custom validations. * + * other['categoryid'] and other['format'] is required. + * * @throws \coding_exception * @return void */ @@ -101,7 +110,7 @@ class questions_exported extends base { */ public static function get_objectid_mapping() { // No mappings. - return array(); + return []; } } diff --git a/lib/classes/event/questions_imported.php b/lib/classes/event/questions_imported.php index 875aa4682b8..d774c40b9d8 100644 --- a/lib/classes/event/questions_imported.php +++ b/lib/classes/event/questions_imported.php @@ -18,7 +18,7 @@ * Questions imported event. * * @package core - * @copyright 2016 Stephen Bourget + * @copyright 2019 Stephen Bourget * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ @@ -29,9 +29,16 @@ defined('MOODLE_INTERNAL') || die(); /** * Question category imported event class. * + * @property-read array $other { + * Extra information about the event. + * + * - int categoryid: The ID of the category where the question resides + * - string format: The format of file import + * } + * * @package core - * @since Moodle 3.2 - * @copyright 2016 Stephen Bourget + * @since Moodle 3.7 + * @copyright 2019 Stephen Bourget * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class questions_imported extends question_base { @@ -59,8 +66,8 @@ class questions_imported extends question_base { * @return string */ public function get_description() { - return "The user with id '$this->userid' imported questions in '". $this->other['format']. - "' format into the category with id '".$this->other['categoryid']."'."; + return "The user with id '$this->userid' imported questions in '" . $this->other['format'] . + "' format into the category with id '" . $this->other['categoryid'] . "'."; } /** @@ -72,25 +79,24 @@ class questions_imported extends question_base { if ($this->courseid) { $cat = $this->other['categoryid'] . ',' . $this->contextid; if ($this->contextlevel == CONTEXT_MODULE) { - return new \moodle_url('/question/edit.php', array('cmid' => $this->contextinstanceid, 'cat' => $cat)); + return new \moodle_url('/question/edit.php', ['cmid' => $this->contextinstanceid, 'cat' => $cat]); } - return new \moodle_url('/question/edit.php', array('courseid' => $this->courseid, 'cat' => $cat)); + return new \moodle_url('/question/edit.php', ['courseid' => $this->courseid, 'cat' => $cat]); } - return new \moodle_url('/question/category.php', array('courseid' => SITEID, 'edit' => $this->other['categoryid'])); + return new \moodle_url('/question/category.php', ['courseid' => SITEID, 'edit' => $this->other['categoryid']]); } /** * Custom validations. * + * other['categoryid'] and other['format'] is required. + * * @throws \coding_exception * @return void */ protected function validate_data() { parent::validate_data(); - if (!isset($this->other['categoryid'])) { - throw new \coding_exception('The \'categoryid\' must be set in \'other\'.'); - } if (!isset($this->other['format'])) { throw new \coding_exception('The \'format\' must be set in \'other\'.'); } @@ -104,6 +110,6 @@ class questions_imported extends question_base { */ public static function get_objectid_mapping() { // No mappings. - return array(); + return []; } } diff --git a/lib/questionlib.php b/lib/questionlib.php index 2252758839b..6d96f8452a3 100644 --- a/lib/questionlib.php +++ b/lib/questionlib.php @@ -382,13 +382,8 @@ function question_delete_question($questionid) { question_bank::notify_question_edited($questionid); // Log the deletion of this question. - $eventparams = array( - 'contextid' => $question->contextid, - 'objectid' => $question->id, - 'other' => array('categoryid' => $question->category) - ); - - $event = \core\event\question_deleted::create($eventparams); + $event = \core\event\question_deleted::create_from_question_instance($question); + $event->add_record_snapshot('question', $question); $event->trigger(); } @@ -715,12 +710,8 @@ function question_move_questions_to_category($questionids, $newcategoryid) { } // Log this question move. - $eventparams = array( - 'contextid' => $question->contextid, - 'objectid' => $question->id, - 'other' => array('oldcategoryid' => $question->category, 'newcategoryid' => $newcategoryid) - ); - $event = \core\event\question_moved::create($eventparams); + $event = \core\event\question_moved::create_from_question_instance($question, context::instance_by_id($question->contextid), + ['oldcategoryid' => $question->category, 'newcategoryid' => $newcategoryid]); $event->trigger(); } diff --git a/question/category.php b/question/category.php index 60f3c3d12b8..c740522a63a 100644 --- a/question/category.php +++ b/question/category.php @@ -82,11 +82,10 @@ if ($param->moveupcontext || $param->movedowncontext) { } $oldcat = $DB->get_record('question_categories', array('id' => $catid), '*', MUST_EXIST); // Log the move to another context. - $params = array( - 'objectid' => explode(',',$pagevars['cat'], -1)[0], - 'contextid' => $param->tocontext, - ); - $event = \core\event\question_category_moved::create($params); + $category = new stdClass(); + $category->id = explode(',', $pagevars['cat'], -1)[0]; + $category->contextid = $param->tocontext; + $event = \core\event\question_category_moved::create_from_question_category_instance($category); $event->trigger(); $qcobject->update_category($catid, "{$newtopcat->id},{$param->tocontext}", $oldcat->name, $oldcat->info); // The previous line does a redirect(). diff --git a/question/category_class.php b/question/category_class.php index f1193f33a91..856da40420c 100644 --- a/question/category_class.php +++ b/question/category_class.php @@ -77,29 +77,35 @@ class question_category_list extends moodle_list { return $topcategory->id; } + /** + * process any actions. + * + * @param integer $left id of item to move left + * @param integer $right id of item to move right + * @param integer $moveup id of item to move up + * @param integer $movedown id of item to move down + * @return void + * @throws coding_exception + */ public function process_actions($left, $right, $moveup, $movedown) { + $category = new stdClass(); if (!empty($left)) { // Moved Left (In to another category). - $params = array( - 'objectid' => $left, - 'contextid' => $this->context->id - ); - $event = \core\event\question_category_moved::create($params); + $category->id = $left; + $category->contextid = $this->context->id; + $event = \core\event\question_category_moved::create_from_question_category_instance($category); $event->trigger(); } else if (!empty($right)) { // Moved Right (Out of the current category). - $params = array( - 'objectid' => $right, - 'contextid' => $this->context->id - ); - $event = \core\event\question_category_moved::create($params); + $category->id = $right; + $category->contextid = $this->context->id; + $event = \core\event\question_category_moved::create_from_question_category_instance($category); $event->trigger(); - } + } parent::process_actions($left, $right, $moveup, $movedown); } } - /** * An item in a list of question categories. * @@ -398,11 +404,8 @@ class question_category_object { $DB->delete_records("question_categories", array("id" => $category->id)); // Log the deletion of this category. - $params = array( - 'objectid' => $category->id, - 'contextid' => $category->contextid - ); - $event = \core\event\question_category_deleted::create($params); + $event = \core\event\question_category_deleted::create_from_question_category_instance($category); + $event->add_record_snapshot('question_categories', $category); $event->trigger(); } @@ -472,11 +475,10 @@ class question_category_object { $categoryid = $DB->insert_record("question_categories", $cat); // Log the creation of this category. - $params = array( - 'objectid' => $categoryid, - 'contextid' => $contextid - ); - $event = \core\event\question_category_created::create($params); + $category = new stdClass(); + $category->id = $categoryid; + $category->contextid = $contextid; + $event = \core\event\question_category_created::create_from_question_category_instance($category); $event->trigger(); if ($return) { @@ -559,11 +561,7 @@ class question_category_object { $DB->update_record('question_categories', $cat); // Log the update of this category. - $params = array( - 'objectid' => $cat->id, - 'contextid' => $cat->contextid - ); - $event = \core\event\question_category_updated::create($params); + $event = \core\event\question_category_updated::create_from_question_category_instance($cat); $event->trigger(); // If the category name has changed, rename any random questions in that category. diff --git a/question/edit.php b/question/edit.php index ef8cff05ca2..8c6e069c2a9 100644 --- a/question/edit.php +++ b/question/edit.php @@ -56,12 +56,11 @@ $questionbank->display('questions', $pagevars['qpage'], $pagevars['qperpage'], echo "\n"; // Log the view of this category. -$categoryid = explode(',' , urldecode($pagevars['cat'])); -$params = array( - 'objectid' => $categoryid[0], - 'context' => $context -); -$event = \core\event\question_category_viewed::create($params); +list($categoryid, $contextid) = explode(',', $pagevars['cat']); +$category = new stdClass(); +$category->id = $categoryid; +$catcontext = \context::instance_by_id($contextid); +$event = \core\event\question_category_viewed::create_from_question_category_instance($category, $catcontext); $event->trigger(); echo $OUTPUT->footer(); diff --git a/question/export.php b/question/export.php index 81b86fae33f..07d48dc0af1 100644 --- a/question/export.php +++ b/question/export.php @@ -77,13 +77,13 @@ if ($from_form = $export_form->get_data()) { echo get_string('yourfileshoulddownload', 'question', $export_url->out()); echo $OUTPUT->box_end(); - // Log the export of these questions. - $eventparams = array( + // Log the export of these questions. + $eventparams = [ 'contextid' => $category->contextid, - 'other' => array('format' => $from_form->format, 'categoryid' => $category->id) - ); - $event = \core\event\questions_exported::create($eventparams); - $event->trigger(); + 'other' => ['format' => $from_form->format, 'categoryid' => $category->id], + ]; + $event = \core\event\questions_exported::create($eventparams); + $event->trigger(); // Don't allow force download for behat site, as pop-up can't be handled by selenium. if (!defined('BEHAT_SITE_RUNNING')) { diff --git a/question/format.php b/question/format.php index 60fd4fa4cee..6cf40e5b0df 100644 --- a/question/format.php +++ b/question/format.php @@ -426,6 +426,8 @@ class qformat_default { ); $question->id = $DB->insert_record('question', $question); + $event = \core\event\question_created::create_from_question_instance($question, $this->importcontext); + $event->trigger(); if (isset($question->questiontextitemid)) { $question->questiontext = file_save_draft_area_files($question->questiontextitemid, @@ -613,6 +615,8 @@ class qformat_default { $category->stamp = make_unique_id_code(); $category->id = $DB->insert_record('question_categories', $category); $parent = $category->id; + $event = \core\event\question_category_created::create_from_question_category_instance($category, $context); + $event->trigger(); } } return $category; diff --git a/question/import.php b/question/import.php index 56faf529cef..0f68d1ee684 100644 --- a/question/import.php +++ b/question/import.php @@ -129,13 +129,13 @@ if ($form = $import_form->get_data()) { print_error('cannotimport', '', $thispageurl->out()); } - // Log the import into this category. - $eventparams = array( - 'contextid' => $category->contextid, - 'other' => array('format' => $form->format, 'categoryid' => $category->id) - ); - $event = \core\event\questions_imported::create($eventparams); - $event->trigger(); + // Log the import into this category. + $eventparams = [ + 'contextid' => $qformat->category->contextid, + 'other' => ['format' => $form->format, 'categoryid' => $qformat->category->id], + ]; + $event = \core\event\questions_imported::create($eventparams); + $event->trigger(); $params = $thispageurl->params() + array( 'category' => $qformat->category->id . ',' . $qformat->category->contextid); diff --git a/question/preview.php b/question/preview.php index 230ccc97a37..cff9994674b 100644 --- a/question/preview.php +++ b/question/preview.php @@ -281,13 +281,7 @@ if (question_has_capability_on($question, 'view')) { } // Log the preview of this question. -$eventparams = array( - 'context' => $context, - 'objectid' => $question->id, - 'other' => array('categoryid' => $question->category) -); -$newquestion = true; -$event = \core\event\question_previewed::create($eventparams); +$event = \core\event\question_viewed::create_from_question_instance($question, $context); $event->trigger(); // Display the settings form. diff --git a/question/tests/events_test.php b/question/tests/events_test.php index 1133738d84b..0f93b8605f9 100644 --- a/question/tests/events_test.php +++ b/question/tests/events_test.php @@ -80,21 +80,21 @@ class core_question_events_testcase extends advanced_testcase { public function test_question_category_deleted() { $this->setAdminUser(); $course = $this->getDataGenerator()->create_course(); - $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id)); + $quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id]); $contexts = new question_edit_contexts(context_module::instance($quiz->cmid)); - $defaultcategoryobj = question_make_default_categories(array($contexts->lowest())); + $defaultcategoryobj = question_make_default_categories([$contexts->lowest()]); $defaultcategory = $defaultcategoryobj->id . ',' . $defaultcategoryobj->contextid; $qcobject = new question_category_object( - 1, - new moodle_url('/mod/quiz/edit.php', array('cmid' => $quiz->cmid)), - $contexts->having_one_edit_tab_cap('categories'), - $defaultcategoryobj->id, - $defaultcategory, - null, - $contexts->having_cap('moodle/question:add')); + 1, + new moodle_url('/mod/quiz/edit.php', ['cmid' => $quiz->cmid]), + $contexts->having_one_edit_tab_cap('categories'), + $defaultcategoryobj->id, + $defaultcategory, + null, + $contexts->having_cap('moodle/question:add')); // Create the category. $categoryid = $qcobject->add_category($defaultcategory, 'newcategory', '', true); @@ -118,21 +118,21 @@ class core_question_events_testcase extends advanced_testcase { public function test_question_category_updated() { $this->setAdminUser(); $course = $this->getDataGenerator()->create_course(); - $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id)); + $quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id]); $contexts = new question_edit_contexts(context_module::instance($quiz->cmid)); - $defaultcategoryobj = question_make_default_categories(array($contexts->lowest())); + $defaultcategoryobj = question_make_default_categories([$contexts->lowest()]); $defaultcategory = $defaultcategoryobj->id . ',' . $defaultcategoryobj->contextid; $qcobject = new question_category_object( - 1, - new moodle_url('/mod/quiz/edit.php', array('cmid' => $quiz->cmid)), - $contexts->having_one_edit_tab_cap('categories'), - $defaultcategoryobj->id, - $defaultcategory, - null, - $contexts->having_cap('moodle/question:add')); + 1, + new moodle_url('/mod/quiz/edit.php', ['cmid' => $quiz->cmid]), + $contexts->having_one_edit_tab_cap('categories'), + $defaultcategoryobj->id, + $defaultcategory, + null, + $contexts->having_cap('moodle/question:add')); // Create the category. $categoryid = $qcobject->add_category($defaultcategory, 'newcategory', '', true); @@ -159,32 +159,30 @@ class core_question_events_testcase extends advanced_testcase { $this->setAdminUser(); $course = $this->getDataGenerator()->create_course(); - $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id)); + $quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id]); $contexts = new question_edit_contexts(context_module::instance($quiz->cmid)); - $defaultcategoryobj = question_make_default_categories(array($contexts->lowest())); + $defaultcategoryobj = question_make_default_categories([$contexts->lowest()]); $defaultcategory = $defaultcategoryobj->id . ',' . $defaultcategoryobj->contextid; $qcobject = new question_category_object( - 1, - new moodle_url('/mod/quiz/edit.php', array('cmid' => $quiz->cmid)), - $contexts->having_one_edit_tab_cap('categories'), - $defaultcategoryobj->id, - $defaultcategory, - null, - $contexts->having_cap('moodle/question:add')); + 1, + new moodle_url('/mod/quiz/edit.php', ['cmid' => $quiz->cmid]), + $contexts->having_one_edit_tab_cap('categories'), + $defaultcategoryobj->id, + $defaultcategory, + null, + $contexts->having_cap('moodle/question:add')); // Create the category. $categoryid = $qcobject->add_category($defaultcategory, 'newcategory', '', true); // Log the view of this category. - $params = array( - 'objectid' => $categoryid, - 'context' => context_module::instance($quiz->cmid) - ); - - $event = \core\event\question_category_viewed::create($params); + $category = new stdClass(); + $category->id = $categoryid; + $context = context_module::instance($quiz->cmid); + $event = \core\event\question_category_viewed::create_from_question_category_instance($category, $context); // Trigger and capture the event. $sink = $this->redirectEvents(); @@ -209,30 +207,30 @@ class core_question_events_testcase extends advanced_testcase { $this->setAdminUser(); $course = $this->getDataGenerator()->create_course(); - $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id)); + $quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id]); $contexts = new question_edit_contexts(context_module::instance($quiz->cmid)); - $defaultcategoryobj = question_make_default_categories(array($contexts->lowest())); + $defaultcategoryobj = question_make_default_categories([$contexts->lowest()]); $defaultcategory = $defaultcategoryobj->id . ',' . $defaultcategoryobj->contextid; $qcobject = new question_category_object( - 1, - new moodle_url('/mod/quiz/edit.php', array('cmid' => $quiz->cmid)), - $contexts->having_one_edit_tab_cap('categories'), - $defaultcategoryobj->id, - $defaultcategory, - null, - $contexts->having_cap('moodle/question:add')); + 1, + new moodle_url('/mod/quiz/edit.php', ['cmid' => $quiz->cmid]), + $contexts->having_one_edit_tab_cap('categories'), + $defaultcategoryobj->id, + $defaultcategory, + null, + $contexts->having_cap('moodle/question:add')); // Create the category. $categoryid = $qcobject->add_category($defaultcategory, 'newcategory', '', true); // Log the view of this category. - $params = array( - 'context' => context_module::instance($quiz->cmid), - 'other' => array('categoryid' => $categoryid, 'format' => 'testformat') - ); + $params = [ + 'context' => context_module::instance($quiz->cmid), + 'other' => ['categoryid' => $categoryid, 'format' => 'testformat'], + ]; $event = \core\event\questions_imported::create($params); @@ -260,30 +258,30 @@ class core_question_events_testcase extends advanced_testcase { $this->setAdminUser(); $course = $this->getDataGenerator()->create_course(); - $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id)); + $quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id]); $contexts = new question_edit_contexts(context_module::instance($quiz->cmid)); - $defaultcategoryobj = question_make_default_categories(array($contexts->lowest())); + $defaultcategoryobj = question_make_default_categories([$contexts->lowest()]); $defaultcategory = $defaultcategoryobj->id . ',' . $defaultcategoryobj->contextid; $qcobject = new question_category_object( - 1, - new moodle_url('/mod/quiz/edit.php', array('cmid' => $quiz->cmid)), - $contexts->having_one_edit_tab_cap('categories'), - $defaultcategoryobj->id, - $defaultcategory, - null, - $contexts->having_cap('moodle/question:add')); + 1, + new moodle_url('/mod/quiz/edit.php', ['cmid' => $quiz->cmid]), + $contexts->having_one_edit_tab_cap('categories'), + $defaultcategoryobj->id, + $defaultcategory, + null, + $contexts->having_cap('moodle/question:add')); // Create the category. $categoryid = $qcobject->add_category($defaultcategory, 'newcategory', '', true); // Log the view of this category. - $params = array( - 'context' => context_module::instance($quiz->cmid), - 'other' => array('categoryid' => $categoryid, 'format' => 'testformat') - ); + $params = [ + 'context' => context_module::instance($quiz->cmid), + 'other' => ['categoryid' => $categoryid, 'format' => 'testformat'], + ]; $event = \core\event\questions_exported::create($params); @@ -310,12 +308,11 @@ class core_question_events_testcase extends advanced_testcase { $this->setAdminUser(); $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); - $cat = $generator->create_question_category(array( - 'name' => 'My category', 'sortorder' => 1)); + $cat = $generator->create_question_category(['name' => 'My category', 'sortorder' => 1]); // Trigger and capture the event. $sink = $this->redirectEvents(); - $questiondata = $generator->create_question('description', null, array('category' => $cat->id)); + $questiondata = $generator->create_question('description', null, ['category' => $cat->id]); $question = question_bank::load_question($questiondata->id); $events = $sink->get_events(); @@ -337,10 +334,9 @@ class core_question_events_testcase extends advanced_testcase { $this->setAdminUser(); $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); - $cat = $generator->create_question_category(array( - 'name' => 'My category', 'sortorder' => 1)); + $cat = $generator->create_question_category(['name' => 'My category', 'sortorder' => 1]); - $questiondata = $generator->create_question('description', null, array('category' => $cat->id)); + $questiondata = $generator->create_question('description', null, ['category' => $cat->id]); $question = question_bank::load_question($questiondata->id); // Trigger and capture the event. @@ -370,16 +366,15 @@ class core_question_events_testcase extends advanced_testcase { $this->setAdminUser(); $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); - $cat = $generator->create_question_category(array( - 'name' => 'My category', 'sortorder' => 1)); + $cat = $generator->create_question_category(['name' => 'My category', 'sortorder' => 1]); - $questiondata = $generator->create_question('description', null, array('category' => $cat->id)); + $questiondata = $generator->create_question('description', null, ['category' => $cat->id]); $question = question_bank::load_question($questiondata->id); $qtype = new qtype_description(); $formdata = test_question_maker::get_question_form_data('description'); $formdata->category = "{$cat->id},{$cat->contextid}"; - qtype_description_edit_form::mock_submit((array)$formdata); + qtype_description_edit_form::mock_submit((array) $formdata); $form = qtype_description_test_helper::get_question_editing_form($cat, $questiondata); $fromform = $form->get_data(); @@ -406,18 +401,18 @@ class core_question_events_testcase extends advanced_testcase { $this->setAdminUser(); $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); - $cat1 = $generator->create_question_category(array( - 'name' => 'My category 1', 'sortorder' => 1)); + $cat1 = $generator->create_question_category([ + 'name' => 'My category 1', 'sortorder' => 1]); - $cat2 = $generator->create_question_category(array( - 'name' => 'My category 2', 'sortorder' => 2)); + $cat2 = $generator->create_question_category([ + 'name' => 'My category 2', 'sortorder' => 2]); - $questiondata = $generator->create_question('description', null, array('category' => $cat1->id)); + $questiondata = $generator->create_question('description', null, ['category' => $cat1->id]); $question = question_bank::load_question($questiondata->id); // Trigger and capture the event. $sink = $this->redirectEvents(); - question_move_questions_to_category(array($question->id), $cat2->id); + question_move_questions_to_category([$question->id], $cat2->id); $events = $sink->get_events(); $event = reset($events); @@ -440,19 +435,12 @@ class core_question_events_testcase extends advanced_testcase { $this->setAdminUser(); $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); - $cat = $generator->create_question_category(array( - 'name' => 'My category', 'sortorder' => 1)); + $cat = $generator->create_question_category(['name' => 'My category', 'sortorder' => 1]); - $questiondata = $generator->create_question('description', null, array('category' => $cat->id)); + $questiondata = $generator->create_question('description', null, ['category' => $cat->id]); $question = question_bank::load_question($questiondata->id); - $params = array( - 'objectid' => $question->id, - 'context' => context::instance_by_id($cat->contextid), - 'other' => array('categoryid' => $question->category) - ); - - $event = \core\event\question_previewed::create($params); + $event = \core\event\question_viewed::create_from_question_instance($question, context::instance_by_id($cat->contextid)); // Trigger and capture the event. $sink = $this->redirectEvents(); @@ -461,7 +449,7 @@ class core_question_events_testcase extends advanced_testcase { $event = reset($events); // Check that the event data is valid. - $this->assertInstanceOf('\core\event\question_previewed', $event); + $this->assertInstanceOf('\core\event\question_viewed', $event); $this->assertEquals($question->id, $event->objectid); $this->assertEquals($cat->id, $event->other['categoryid']); $this->assertDebuggingNotCalled(); diff --git a/question/type/questiontypebase.php b/question/type/questiontypebase.php index a7806a1603f..7af20957460 100644 --- a/question/type/questiontypebase.php +++ b/question/type/questiontypebase.php @@ -395,21 +395,11 @@ class question_type { if ($newquestion) { // Log the creation of this question. - $eventparams = array( - 'context' => $context, - 'objectid' => $question->id, - 'other' => array('categoryid' => $question->category) - ); - $event = \core\event\question_created::create($eventparams); + $event = \core\event\question_created::create_from_question_instance($question, $context); $event->trigger(); } else { // Log the update of this question. - $eventparams = array( - 'context' => $context, - 'objectid' => $question->id, - 'other' => array('categoryid' => $question->category) - ); - $event = \core\event\question_updated::create($eventparams); + $event = \core\event\question_updated::create_from_question_instance($question, $context); $event->trigger(); }