Commit 4ca60a56 authored by VinhLe's avatar VinhLe
Browse files

MDL-36088 questions: Add new events

parent 93e435b9
......@@ -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';
......
......@@ -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;
}
}
......@@ -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;
}
}
......@@ -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;
}
/**
......
......@@ -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;
}
/**
......
......@@ -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;
}
/**
......
......@@ -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;
}
/**
......
......@@ -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;
}
/**
......
......@@ -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]);
}
}
......@@ -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'] . "'.";
}
/**
......
......@@ -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;
}
......
......@@ -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;
}
/**
......
......@@ -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'.";
}
}
......@@ -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 {