diff --git a/.github/workflows/moodle-ci.yml b/.github/workflows/moodle-ci.yml index da7b3f4..2766e9c 100644 --- a/.github/workflows/moodle-ci.yml +++ b/.github/workflows/moodle-ci.yml @@ -4,11 +4,11 @@ on: [push, pull_request] jobs: test: - runs-on: ubuntu-18.04 + runs-on: ubuntu-22.04 services: postgres: - image: postgres:12 + image: postgres:13 env: POSTGRES_USER: 'postgres' POSTGRES_HOST_AUTH_METHOD: 'trust' @@ -16,7 +16,7 @@ jobs: - 5432:5432 options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 3 mariadb: - image: mariadb:10.5 + image: mariadb:10.6 env: MYSQL_USER: 'root' MYSQL_ALLOW_EMPTY_PASSWORD: "true" @@ -27,13 +27,107 @@ jobs: strategy: fail-fast: false matrix: - php: ['7.4', '8.0'] - moodle-branch: ['master'] - database: [pgsql, mariadb] + include: + - php: 8.3 + moodle-branch: MOODLE_404_STABLE + database: pgsql + plugin-ci: 4 + - php: 8.3 + moodle-branch: MOODLE_404_STABLE + database: mariadb + plugin-ci: 4 + - php: 8.2 + moodle-branch: MOODLE_404_STABLE + database: pgsql + plugin-ci: 4 + - php: 8.2 + moodle-branch: MOODLE_404_STABLE + database: mariadb + plugin-ci: 4 + - php: 8.1 + moodle-branch: MOODLE_404_STABLE + database: pgsql + plugin-ci: 4 + - php: 8.1 + moodle-branch: MOODLE_404_STABLE + database: mariadb + plugin-ci: 4 + - php: 8.2 + moodle-branch: MOODLE_403_STABLE + database: pgsql + plugin-ci: 4 + - php: 8.2 + moodle-branch: MOODLE_403_STABLE + database: mariadb + plugin-ci: 4 + - php: 8.1 + moodle-branch: MOODLE_403_STABLE + database: pgsql + plugin-ci: 4 + - php: 8.1 + moodle-branch: MOODLE_403_STABLE + database: mariadb + plugin-ci: 4 + - php: 8.0 + moodle-branch: MOODLE_403_STABLE + database: pgsql + plugin-ci: 4 + - php: 8.0 + moodle-branch: MOODLE_403_STABLE + database: mariadb + plugin-ci: 4 + - php: 8.2 + moodle-branch: MOODLE_402_STABLE + database: pgsql + plugin-ci: 4 + - php: 8.2 + moodle-branch: MOODLE_402_STABLE + database: mariadb + plugin-ci: 4 + - php: 8.1 + moodle-branch: MOODLE_402_STABLE + database: pgsql + plugin-ci: 4 + - php: 8.1 + moodle-branch: MOODLE_402_STABLE + database: mariadb + plugin-ci: 4 + - php: 8.0 + moodle-branch: MOODLE_402_STABLE + database: pgsql + plugin-ci: 4 + - php: 8.0 + moodle-branch: MOODLE_402_STABLE + database: mariadb + plugin-ci: 4 + - php: 8.1 + moodle-branch: MOODLE_401_STABLE + database: pgsql + plugin-ci: 4 + - php: 8.1 + moodle-branch: MOODLE_401_STABLE + database: mariadb + plugin-ci: 4 + - php: 8.0 + moodle-branch: MOODLE_401_STABLE + database: pgsql + plugin-ci: 4 + - php: 8.0 + moodle-branch: MOODLE_401_STABLE + database: mariadb + plugin-ci: 4 + - php: 7.4 + moodle-branch: MOODLE_401_STABLE + database: pgsql + plugin-ci: 4 + - php: 7.4 + moodle-branch: MOODLE_401_STABLE + database: mariadb + plugin-ci: 4 steps: - name: Check out repository code - uses: actions/checkout@v2 + uses: actions/checkout@v3 with: path: plugin @@ -46,7 +140,7 @@ jobs: - name: Initialise moodle-plugin-ci run: | - composer create-project -n --no-dev --prefer-dist moodlehq/moodle-plugin-ci ci ^3 + composer create-project -n --no-dev --prefer-dist moodlehq/moodle-plugin-ci ci ^${{ matrix.plugin-ci }} echo $(cd ci/bin; pwd) >> $GITHUB_PATH echo $(cd ci/vendor/bin; pwd) >> $GITHUB_PATH sudo locale-gen en_AU.UTF-8 @@ -63,11 +157,6 @@ jobs: if: ${{ always() }} run: moodle-plugin-ci phplint - - name: PHP Copy/Paste Detector - continue-on-error: true # This step will show errors but will not fail - if: ${{ always() }} - run: moodle-plugin-ci phpcpd - - name: PHP Mess Detector continue-on-error: true # This step will show errors but will not fail if: ${{ always() }} diff --git a/backup/moodle2/backup_quizgame_stepslib.php b/backup/moodle2/backup_quizgame_stepslib.php index a35cbe8..177a6dd 100644 --- a/backup/moodle2/backup_quizgame_stepslib.php +++ b/backup/moodle2/backup_quizgame_stepslib.php @@ -43,14 +43,14 @@ protected function define_structure() { $userinfo = $this->get_setting_value('userinfo'); // Define each element separated. - $quizgame = new backup_nested_element('quizgame', array('id'), array( - 'course', 'name', 'intro', 'introformat', 'timecreated', - 'timemodified', 'questioncategory', 'grade', 'completionscore')); + $quizgame = new backup_nested_element('quizgame', ['id'], + ['course', 'name', 'intro', 'introformat', 'timecreated', + 'timemodified', 'questioncategory', 'grade', 'completionscore', ]); $scores = new backup_nested_element('scores'); - $score = new backup_nested_element('score', array('id'), array( - 'quizgameid', 'userid', 'score', 'timecreated')); + $score = new backup_nested_element('score', ['id'], + ['quizgameid', 'userid', 'score', 'timecreated']); // Build the tree. $quizgame->add_child($scores); @@ -58,7 +58,7 @@ protected function define_structure() { // Define sources. - $quizgame->set_source_table('quizgame', array('id' => backup::VAR_ACTIVITYID)); + $quizgame->set_source_table('quizgame', ['id' => backup::VAR_ACTIVITYID]); // All the rest of elements only happen if we are including user info. if ($userinfo) { @@ -67,7 +67,7 @@ protected function define_structure() { SELECT * FROM {quizgame_scores} WHERE quizgameid = ?', - array(backup::VAR_PARENTID)); + [backup::VAR_PARENTID]); } diff --git a/backup/moodle2/restore_quizgame_activity_task.class.php b/backup/moodle2/restore_quizgame_activity_task.class.php index 83fa635..6b9e161 100644 --- a/backup/moodle2/restore_quizgame_activity_task.class.php +++ b/backup/moodle2/restore_quizgame_activity_task.class.php @@ -56,9 +56,9 @@ protected function define_my_steps() { * processed by the link decoder */ public static function define_decode_contents() { - $contents = array(); + $contents = []; - $contents[] = new restore_decode_content('quizgame', array('intro'), 'quizgame'); + $contents[] = new restore_decode_content('quizgame', ['intro'], 'quizgame'); return $contents; } @@ -68,7 +68,7 @@ public static function define_decode_contents() { * to the activity to be executed by the link decoder */ public static function define_decode_rules() { - $rules = array(); + $rules = []; $rules[] = new restore_decode_rule('QUIZVENTUREVIEWBYID', '/mod/quizgame/view.php?id=$1', 'course_module'); $rules[] = new restore_decode_rule('QUIZVENTUREINDEX', '/mod/quizgame/index.php?id=$1', 'course'); @@ -88,7 +88,7 @@ public static function define_decode_rules() { * activity level. All them are rules not linked to any module instance (cmid = 0) */ public static function define_restore_log_rules_for_course() { - $rules = array(); + $rules = []; // Fix old wrong uses (missing extension). $rules[] = new restore_log_rule('quizgame', 'view all', 'index?id={course}', null, diff --git a/backup/moodle2/restore_quizgame_stepslib.php b/backup/moodle2/restore_quizgame_stepslib.php index 3d65729..b61db4f 100644 --- a/backup/moodle2/restore_quizgame_stepslib.php +++ b/backup/moodle2/restore_quizgame_stepslib.php @@ -38,7 +38,7 @@ class restore_quizgame_activity_structure_step extends restore_activity_structur */ protected function define_structure() { - $paths = array(); + $paths = []; $userinfo = $this->get_setting_value('userinfo'); $paths[] = new restore_path_element('quizgame', '/activity/quizgame'); @@ -70,10 +70,10 @@ protected function process_quizgame($data) { // Get the new mapping to the category. $newcat = $this->get_mappingid('question_category', $category[0]); // Now get the context for this category. - $newcontext = $DB->get_field('question_categories', 'contextid', array('id' => $newcat)); + $newcontext = $DB->get_field('question_categories', 'contextid', ['id' => $newcat]); // Assemble the field data. if (!empty($newcat)) { - $data->questioncategory = implode(',', array($newcat, $newcontext)); + $data->questioncategory = implode(',', [$newcat, $newcontext]); } else { if (!$this->task->is_samesite() || $data->course != $oldcourse) { // We cannot map to the question category. diff --git a/classes/completion/custom_completion.php b/classes/completion/custom_completion.php index 6fa5d17..eda1b40 100644 --- a/classes/completion/custom_completion.php +++ b/classes/completion/custom_completion.php @@ -48,11 +48,11 @@ public function get_state(string $rule): int { $completionscore = $this->cm->customdata['customcompletionrules']['completionscore']; $where = ' quizgameid = :quizgameid AND userid = :userid AND score >= :score'; - $params = array( + $params = [ 'quizgameid' => $quizgameid, 'userid' => $userid, 'score' => $completionscore, - ); + ]; $highscore = $DB->count_records_select('quizgame_scores', $where, $params) > 0; return ($highscore >= 1) ? COMPLETION_COMPLETE : COMPLETION_INCOMPLETE; diff --git a/classes/event/course_module_viewed.php b/classes/event/course_module_viewed.php index 5a8559c..29140bb 100644 --- a/classes/event/course_module_viewed.php +++ b/classes/event/course_module_viewed.php @@ -48,7 +48,7 @@ protected function init() { * @return \moodle_url */ public function get_url() { - $params = array('id' => $this->contextinstanceid); + $params = ['id' => $this->contextinstanceid]; if (!empty($this->other['mode'])) { $params['mode'] = $this->other['mode']; } @@ -60,7 +60,7 @@ public function get_url() { * @return array */ public static function get_objectid_mapping() { - return array('db' => 'quizgame', 'restore' => 'quizgame'); + return ['db' => 'quizgame', 'restore' => 'quizgame']; } } diff --git a/classes/event/game_score_added.php b/classes/event/game_score_added.php index de3c8d3..f6e7cb0 100644 --- a/classes/event/game_score_added.php +++ b/classes/event/game_score_added.php @@ -57,7 +57,7 @@ public static function get_name() { * @return \moodle_url */ public function get_url() { - return new \moodle_url('/mod/quizgame/view.php', array('id' => $this->contextinstanceid)); + return new \moodle_url('/mod/quizgame/view.php', ['id' => $this->contextinstanceid]); } /** @@ -75,6 +75,6 @@ public function get_description() { * @return array */ public static function get_objectid_mapping() { - return array('db' => 'quizgame_scores', 'restore' => 'quizgame_scores'); + return ['db' => 'quizgame_scores', 'restore' => 'quizgame_scores']; } } diff --git a/classes/event/game_scores_viewed.php b/classes/event/game_scores_viewed.php index 1a3c2f7..996dad6 100644 --- a/classes/event/game_scores_viewed.php +++ b/classes/event/game_scores_viewed.php @@ -57,7 +57,7 @@ public static function get_name() { * @return \moodle_url */ public function get_url() { - return new \moodle_url('/mod/quizgame/scores.php', array('id' => $this->objectid)); + return new \moodle_url('/mod/quizgame/scores.php', ['id' => $this->objectid]); } /** @@ -75,6 +75,6 @@ public function get_description() { * @return array */ public static function get_objectid_mapping() { - return array('db' => 'quizgame', 'restore' => 'quizgame'); + return ['db' => 'quizgame', 'restore' => 'quizgame']; } } diff --git a/classes/event/game_started.php b/classes/event/game_started.php index 0cf1f2e..dc42ca7 100644 --- a/classes/event/game_started.php +++ b/classes/event/game_started.php @@ -57,7 +57,7 @@ public static function get_name() { * @return \moodle_url */ public function get_url() { - return new \moodle_url('/mod/quizgame/view.php', array('id' => $this->contextinstanceid)); + return new \moodle_url('/mod/quizgame/view.php', ['id' => $this->contextinstanceid]); } /** @@ -74,6 +74,6 @@ public function get_description() { * @return array */ public static function get_objectid_mapping() { - return array('db' => 'quizgame', 'restore' => 'quizgame'); + return ['db' => 'quizgame', 'restore' => 'quizgame']; } } diff --git a/classes/external.php b/classes/external.php index 387e998..1ed10b7 100644 --- a/classes/external.php +++ b/classes/external.php @@ -50,9 +50,9 @@ public static function update_score_parameters() { // The external_function_parameters constructor expects an array of external_description. return new external_function_parameters( // An external_description can be: external_value, external_single_structure or external_multiple structure. - array('quizgameid' => new external_value(PARAM_INT, 'quizgame instance ID'), + ['quizgameid' => new external_value(PARAM_INT, 'quizgame instance ID'), 'score' => new external_value(PARAM_INT, 'Player final score'), - ) + ] ); } @@ -65,18 +65,18 @@ public static function update_score_parameters() { public static function update_score($quizgameid, $score) { global $DB; - $warnings = array(); + $warnings = []; $params = self::validate_parameters(self::update_score_parameters(), - array( + [ 'quizgameid' => $quizgameid, - 'score' => $score - )); - if (!$quizgame = $DB->get_record("quizgame", array("id" => $params['quizgameid']))) { + 'score' => $score, + ]); + if (!$quizgame = $DB->get_record("quizgame", ["id" => $params['quizgameid']])) { throw new moodle_exception("invalidcoursemodule", "error"); } $cm = get_coursemodule_from_instance('quizgame', $quizgame->id, 0, false, MUST_EXIST); - $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST); + $course = $DB->get_record('course', ['id' => $cm->course], '*', MUST_EXIST); // Validate the context and check capabilities. $context = context_module::instance($cm->id); @@ -109,7 +109,7 @@ public static function start_game_parameters() { // The external_function_parameters constructor expects an array of external_description. return new external_function_parameters( // An external_description can be: external_value, external_single_structure or external_multiple structure. - array('quizgameid' => new external_value(PARAM_INT, 'quizgame instance ID')) + ['quizgameid' => new external_value(PARAM_INT, 'quizgame instance ID')] ); } @@ -121,16 +121,16 @@ public static function start_game_parameters() { public static function start_game($quizgameid) { global $DB; - $warnings = array(); + $warnings = []; $params = self::validate_parameters(self::start_game_parameters(), - array('quizgameid' => $quizgameid) + ['quizgameid' => $quizgameid] ); - if (!$quizgame = $DB->get_record("quizgame", array("id" => $params['quizgameid']))) { + if (!$quizgame = $DB->get_record("quizgame", ["id" => $params['quizgameid']])) { throw new moodle_exception("invalidcoursemodule", "error"); } $cm = get_coursemodule_from_instance('quizgame', $quizgame->id, 0, false, MUST_EXIST); - $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST); + $course = $DB->get_record('course', ['id' => $cm->course], '*', MUST_EXIST); // Validate the context and check capabilities. $context = context_module::instance($cm->id); diff --git a/classes/privacy/provider.php b/classes/privacy/provider.php index 9888680..d268cab 100644 --- a/classes/privacy/provider.php +++ b/classes/privacy/provider.php @@ -52,7 +52,7 @@ class provider implements * @param collection $items a reference to the collection to use to store the metadata. * @return collection the updated collection of metadata items. */ - public static function get_metadata(collection $items) : collection { + public static function get_metadata(collection $items): collection { $items->add_database_table( 'quizgame_scores', [ @@ -73,7 +73,7 @@ public static function get_metadata(collection $items) : collection { * @param int $userid the userid. * @return contextlist the list of contexts containing user info for the user. */ - public static function get_contexts_for_userid(int $userid) : contextlist { + public static function get_contexts_for_userid(int $userid): contextlist { // Fetch all quizgame scores. $sql = "SELECT c.id FROM {context} c diff --git a/classes/table_scores.php b/classes/table_scores.php index 0a8b2a9..3a2ff9b 100644 --- a/classes/table_scores.php +++ b/classes/table_scores.php @@ -37,7 +37,7 @@ class table_scores extends table_sql { /** @var array list of user fullnames shown in report */ - private $userfullnames = array(); + private $userfullnames = []; /** * Constructor @@ -47,11 +47,11 @@ class table_scores extends table_sql { public function __construct($uniqueid) { parent::__construct($uniqueid); // Define the list of columns to show. - $columns = array('userid', 'score', 'timecreated'); + $columns = ['userid', 'score', 'timecreated']; $this->define_columns($columns); // Define the titles of columns to show in header. - $headers = array(get_string('user'), get_string('scoreheader', 'mod_quizgame'), get_string('date')); + $headers = [get_string('user'), get_string('scoreheader', 'mod_quizgame'), get_string('date')]; $this->define_headers($headers); } diff --git a/db/access.php b/db/access.php index b2c8499..fc4e4f6 100644 --- a/db/access.php +++ b/db/access.php @@ -46,39 +46,39 @@ defined('MOODLE_INTERNAL') || die(); -$capabilities = array( +$capabilities = [ - 'mod/quizgame:addinstance' => array( + 'mod/quizgame:addinstance' => [ 'riskbitmask' => RISK_XSS, 'captype' => 'write', 'contextlevel' => CONTEXT_COURSE, - 'archetypes' => array( + 'archetypes' => [ 'editingteacher' => CAP_ALLOW, - 'manager' => CAP_ALLOW - ), - 'clonepermissionsfrom' => 'moodle/course:manageactivities' - ), + 'manager' => CAP_ALLOW, + ], + 'clonepermissionsfrom' => 'moodle/course:manageactivities', + ], - 'mod/quizgame:view' => array( + 'mod/quizgame:view' => [ 'captype' => 'read', 'contextlevel' => CONTEXT_MODULE, - 'legacy' => array( + 'legacy' => [ 'guest' => CAP_ALLOW, 'student' => CAP_ALLOW, 'teacher' => CAP_ALLOW, 'editingteacher' => CAP_ALLOW, - 'manager' => CAP_ALLOW - ) - ), + 'manager' => CAP_ALLOW, + ], + ], - 'mod/quizgame:viewallscores' => array( + 'mod/quizgame:viewallscores' => [ 'captype' => 'read', 'contextlevel' => CONTEXT_MODULE, - 'archetypes' => array( + 'archetypes' => [ 'editingteacher' => CAP_ALLOW, 'teacher' => CAP_ALLOW, - 'manager' => CAP_ALLOW - ), - ), -); + 'manager' => CAP_ALLOW, + ], + ], +]; diff --git a/db/log.php b/db/log.php index 5d8b983..1064612 100644 --- a/db/log.php +++ b/db/log.php @@ -30,9 +30,9 @@ global $DB; -$logs = array( - array('module' => 'quizgame', 'action' => 'add', 'mtable' => 'quizgame', 'field' => 'name'), - array('module' => 'quizgame', 'action' => 'update', 'mtable' => 'quizgame', 'field' => 'name'), - array('module' => 'quizgame', 'action' => 'view', 'mtable' => 'quizgame', 'field' => 'name'), - array('module' => 'quizgame', 'action' => 'view all', 'mtable' => 'quizgame', 'field' => 'name') -); +$logs = [ + ['module' => 'quizgame', 'action' => 'add', 'mtable' => 'quizgame', 'field' => 'name'], + ['module' => 'quizgame', 'action' => 'update', 'mtable' => 'quizgame', 'field' => 'name'], + ['module' => 'quizgame', 'action' => 'view', 'mtable' => 'quizgame', 'field' => 'name'], + ['module' => 'quizgame', 'action' => 'view all', 'mtable' => 'quizgame', 'field' => 'name'], +]; diff --git a/db/services.php b/db/services.php index 5b5f207..001fb40 100644 --- a/db/services.php +++ b/db/services.php @@ -26,22 +26,22 @@ defined('MOODLE_INTERNAL') || die; -$functions = array( +$functions = [ - 'mod_quizgame_update_score' => array( + 'mod_quizgame_update_score' => [ 'classname' => 'mod_quizgame_external', 'methodname' => 'update_score', 'description' => 'Record the score and write to the database.', 'type' => 'write', 'ajax' => true, 'capabilities' => 'mod/quizgame:view', - ), - 'mod_quizgame_start_game' => array( + ], + 'mod_quizgame_start_game' => [ 'classname' => 'mod_quizgame_external', 'methodname' => 'start_game', 'description' => 'Log the player starting the game', 'type' => 'write', 'ajax' => true, 'capabilities' => 'mod/quizgame:view', - ) -); + ], +]; diff --git a/grade.php b/grade.php index ec8528e..ceccc14 100644 --- a/grade.php +++ b/grade.php @@ -31,7 +31,7 @@ $userid = optional_param('userid', 0, PARAM_INT); // Graded user ID (optional). $cm = get_coursemodule_from_id('quizgame', $id, 0, false, MUST_EXIST); -$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST); +$course = $DB->get_record('course', ['id' => $cm->course], '*', MUST_EXIST); require_login($course, false, $cm); // In the simplest case just redirect to the view page. diff --git a/index.php b/index.php index e071f84..150ec04 100644 --- a/index.php +++ b/index.php @@ -30,18 +30,18 @@ $id = required_param('id', PARAM_INT); // Course. -$course = $DB->get_record('course', array('id' => $id), '*', MUST_EXIST); +$course = $DB->get_record('course', ['id' => $id], '*', MUST_EXIST); require_course_login($course); $coursecontext = context_course::instance($course->id); -$event = \mod_quizgame\event\course_module_instance_list_viewed::create(array( - 'context' => $coursecontext -)); +$event = \mod_quizgame\event\course_module_instance_list_viewed::create([ + 'context' => $coursecontext, +]); $event->add_record_snapshot('course', $course); $event->trigger(); -$PAGE->set_url('/mod/quizgame/index.php', array('id' => $id)); +$PAGE->set_url('/mod/quizgame/index.php', ['id' => $id]); $PAGE->set_title(format_string($course->fullname)); $PAGE->set_heading(format_string($course->fullname)); $PAGE->set_context($coursecontext); @@ -49,37 +49,37 @@ echo $OUTPUT->header(); if (! $quizgames = get_all_instances_in_course('quizgame', $course)) { - notice(get_string('noquizgames', 'quizgame'), new moodle_url('/course/view.php', array('id' => $course->id))); + notice(get_string('noquizgames', 'quizgame'), new moodle_url('/course/view.php', ['id' => $course->id])); } $table = new html_table(); if ($course->format == 'weeks') { - $table->head = array(get_string('week'), get_string('name')); - $table->align = array('center', 'left'); + $table->head = [get_string('week'), get_string('name')]; + $table->align = ['center', 'left']; } else if ($course->format == 'topics') { - $table->head = array(get_string('topic'), get_string('name')); - $table->align = array('center', 'left', 'left', 'left'); + $table->head = [get_string('topic'), get_string('name')]; + $table->align = ['center', 'left', 'left', 'left']; } else { - $table->head = array(get_string('name')); - $table->align = array('left', 'left', 'left'); + $table->head = [get_string('name')]; + $table->align = ['left', 'left', 'left']; } foreach ($quizgames as $quizgame) { if (!$quizgame->visible) { $link = html_writer::link( - new moodle_url('/mod/quizgame/view.php', array('id' => $quizgame->coursemodule)), + new moodle_url('/mod/quizgame/view.php', ['id' => $quizgame->coursemodule]), format_string($quizgame->name, true), - array('class' => 'dimmed')); + ['class' => 'dimmed']); } else { $link = html_writer::link( - new moodle_url('/mod/quizgame/view.php', array('id' => $quizgame->coursemodule)), + new moodle_url('/mod/quizgame/view.php', ['id' => $quizgame->coursemodule]), format_string($quizgame->name, true)); } if ($course->format == 'weeks' || $course->format == 'topics') { - $table->data[] = array($quizgame->section, $link); + $table->data[] = [$quizgame->section, $link]; } else { - $table->data[] = array($link); + $table->data[] = [$link]; } } diff --git a/lang/en/quizgame.php b/lang/en/quizgame.php index b63b636..9b44e6b 100644 --- a/lang/en/quizgame.php +++ b/lang/en/quizgame.php @@ -14,7 +14,6 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . - /** * English strings for quizgame * @@ -28,6 +27,10 @@ defined('MOODLE_INTERNAL') || die(); +// Let codechecker ignore some sniffs for this file as it is perfectly well ordered, just not alphabetically. +// phpcs:disable moodle.Files.LangFilesOrdering.UnexpectedComment +// phpcs:disable moodle.Files.LangFilesOrdering.IncorrectOrder + $string['achievedhighscoreof'] = 'Achieved a high score of {$a}'; $string['attempt'] = 'Attempt #{$a}'; $string['completiondetail:score'] = 'Get a minimum score of {$a}'; @@ -60,7 +63,6 @@ $string['modulenameplural'] = 'Quizventure games'; $string['modulename'] = 'Quizventure'; $string['notyetplayed'] = 'Not yet played'; -$string['achievedhighscoreof'] = 'Achieved a high score of {$a}'; $string['playedxtimeswithhighscore'] = 'Played {$a->times} times. The last game ended with a high score of {$a->score}'; $string['pluginadministration'] = 'Quizventure administration'; $string['pluginname'] = 'Quizventure'; diff --git a/lib.php b/lib.php index 2e3e00f..9084feb 100644 --- a/lib.php +++ b/lib.php @@ -65,10 +65,10 @@ function quizgame_supports($feature) { * of the new instance. * * @param stdClass $quizgame An object from the form in mod_form.php - * @param mod_quizgame_mod_form $mform The add instance form + * @param ?mod_quizgame_mod_form $mform The add instance form * @return int The id of the newly inserted quizgame record */ -function quizgame_add_instance(stdClass $quizgame, mod_quizgame_mod_form $mform = null) { +function quizgame_add_instance(stdClass $quizgame, ?mod_quizgame_mod_form $mform = null) { global $DB; $quizgame->timecreated = time(); @@ -86,10 +86,10 @@ function quizgame_add_instance(stdClass $quizgame, mod_quizgame_mod_form $mform * will update an existing instance with new data. * * @param stdClass $quizgame An object from the form in mod_form.php - * @param mod_quizgame_mod_form $mform + * @param ?mod_quizgame_mod_form $mform The add instance form * @return boolean Success/Fail */ -function quizgame_update_instance(stdClass $quizgame, mod_quizgame_mod_form $mform = null) { +function quizgame_update_instance(stdClass $quizgame, ?mod_quizgame_mod_form $mform = null) { global $DB; $quizgame->timemodified = time(); @@ -111,12 +111,12 @@ function quizgame_update_instance(stdClass $quizgame, mod_quizgame_mod_form $mfo function quizgame_delete_instance($id) { global $DB; - if (! $quizgame = $DB->get_record('quizgame', array('id' => $id))) { + if (! $quizgame = $DB->get_record('quizgame', ['id' => $id])) { return false; } - $DB->delete_records('quizgame', array('id' => $quizgame->id)); - $DB->delete_records('quizgame_scores', array('quizgameid' => $quizgame->id)); + $DB->delete_records('quizgame', ['id' => $quizgame->id]); + $DB->delete_records('quizgame_scores', ['quizgameid' => $quizgame->id]); return true; } @@ -137,12 +137,12 @@ function quizgame_delete_instance($id) { function quizgame_user_outline($course, $user, $mod, $quizgame) { global $DB; - if ($game = $DB->count_records('quizgame_scores', array('quizgameid' => $quizgame->id, 'userid' => $user->id))) { + if ($game = $DB->count_records('quizgame_scores', ['quizgameid' => $quizgame->id, 'userid' => $user->id])) { $result = new stdClass(); if ($game > 0) { $games = $DB->get_records('quizgame_scores', - array('quizgameid' => $quizgame->id, 'userid' => $user->id), 'timecreated DESC', '*', 0, 1); + ['quizgameid' => $quizgame->id, 'userid' => $user->id], 'timecreated DESC', '*', 0, 1); foreach ($games as $last) { $data = new stdClass(); $data->score = $last->score; @@ -175,7 +175,7 @@ function quizgame_user_complete($course, $user, $mod, $quizgame) { global $DB; if ($games = $DB->get_records('quizgame_scores', - array('quizgameid' => $quizgame->id, 'userid' => $user->id), + ['quizgameid' => $quizgame->id, 'userid' => $user->id], 'timecreated ASC')) { $attempt = 1; foreach ($games as $game) { @@ -205,7 +205,7 @@ function quizgame_get_completion_state($course, $cm, $userid, $type) { global $DB; // Get quizgame details. - if (!($quizgame = $DB->get_record('quizgame', array('id' => $cm->instance)))) { + if (!($quizgame = $DB->get_record('quizgame', ['id' => $cm->instance]))) { throw new Exception("Can't find quizgame {$cm->instance}"); } @@ -213,11 +213,11 @@ function quizgame_get_completion_state($course, $cm, $userid, $type) { $result = $type; if ($quizgame->completionscore) { $where = ' quizgameid = :quizgameid AND userid = :userid AND score >= :score'; - $params = array( + $params = [ 'quizgameid' => $quizgame->id, 'userid' => $userid, 'score' => $quizgame->completionscore, - ); + ]; $value = $DB->count_records_select('quizgame_scores', $where, $params) > 0; if ($type == COMPLETION_AND) { $result = $result && $value; @@ -292,11 +292,11 @@ function quizgame_cron () { /** * Returns all other caps used in the module * - * e.g. array('moodle/site:accessallgroups'); + * e.g. ['moodle/site:accessallgroups']; * @return array of capabilities used in the module */ function quizgame_get_extra_capabilities() { - return array(); + return []; } // Gradebook API. @@ -317,7 +317,7 @@ function quizgame_get_extra_capabilities() { function quizgame_scale_used($quizgameid, $scaleid) { global $DB; - if ($scaleid && $DB->record_exists('quizgame', array('id' => $quizgameid, 'grade' => -$scaleid))) { + if ($scaleid && $DB->record_exists('quizgame', ['id' => $quizgameid, 'grade' => -$scaleid])) { return true; } else { return false; @@ -350,7 +350,7 @@ function quizgame_grade_item_update(stdClass $quizgame, $grades=null) { global $CFG; require_once($CFG->libdir.'/gradelib.php'); - $item = array(); + $item = []; $item['itemname'] = clean_param($quizgame->name, PARAM_NOTAGS); $item['gradetype'] = GRADE_TYPE_VALUE; $item['grademax'] = $quizgame->grade; @@ -372,7 +372,7 @@ function quizgame_update_grades(stdClass $quizgame, $userid = 0) { global $CFG; require_once($CFG->libdir.'/gradelib.php'); - $grades = array(); // Populate array of grade objects indexed by userid. + $grades = []; // Populate array of grade objects indexed by userid. grade_update('mod/quizgame', $quizgame->course, 'mod', 'quizgame', $quizgame->id, 0, $grades); } @@ -391,7 +391,7 @@ function quizgame_update_grades(stdClass $quizgame, $userid = 0) { * @return array of [(string)filearea] => (string)description */ function quizgame_get_file_areas($course, $cm, $context) { - return array(); + return []; } /** @@ -429,7 +429,7 @@ function quizgame_get_file_info($browser, $areas, $course, $cm, $context, $filea * @param bool $forcedownload whether or not force download * @param array $options additional options affecting the file serving */ -function quizgame_pluginfile($course, $cm, $context, $filearea, array $args, $forcedownload, array $options=array()) { +function quizgame_pluginfile($course, $cm, $context, $filearea, array $args, $forcedownload, array $options=[]) { if ($context->contextlevel != CONTEXT_MODULE) { send_file_not_found(); @@ -449,9 +449,9 @@ function quizgame_pluginfile($course, $cm, $context, $filearea, array $args, $fo * so it is safe to rely on the $PAGE. * * @param settings_navigation $settingsnav {settings_navigation} - * @param navigation_node $quizgamenode {navigation_node} + * @param ?navigation_node $quizgamenode {navigation_node} */ -function quizgame_extend_settings_navigation(settings_navigation $settingsnav, navigation_node $quizgamenode=null) { +function quizgame_extend_settings_navigation(settings_navigation $settingsnav, ?navigation_node $quizgamenode=null) { } /** @@ -472,7 +472,7 @@ function quizgame_reset_course_form_definition(&$mform) { * @return array */ function quizgame_reset_course_form_defaults($course) { - return array('reset_quizgame_scores' => 1); + return ['reset_quizgame_scores' => 1]; } @@ -486,15 +486,15 @@ function quizgame_reset_course_form_defaults($course) { function quizgame_reset_userdata($data) { global $DB; $componentstr = get_string('modulenameplural', 'quizgame'); - $status = array(); + $status = []; if (!empty($data->reset_quizgame_scores)) { $scoresql = "SELECT qg.id FROM {quizgame} qg WHERE qg.course=?"; - $DB->delete_records_select('quizgame_scores', "quizgameid IN ($scoresql)", array($data->courseid)); - $status[] = array('component' => $componentstr, 'item' => get_string('removescores', 'quizgame'), 'error' => false); + $DB->delete_records_select('quizgame_scores', "quizgameid IN ($scoresql)", [$data->courseid]); + $status[] = ['component' => $componentstr, 'item' => get_string('removescores', 'quizgame'), 'error' => false]; } return $status; @@ -514,7 +514,7 @@ function quizgame_reset_gradebook($courseid, $type='') { FROM {quizgame} g, {course_modules} cm, {modules} m WHERE m.name='quizgame' AND m.id=cm.module AND cm.instance=g.id AND g.course=?"; - if ($quizgames = $DB->get_records_sql($sql, array($courseid))) { + if ($quizgames = $DB->get_records_sql($sql, [$courseid])) { foreach ($quizgames as $quizgame) { quizgame_grade_item_update($quizgame, 'reset'); } diff --git a/locallib.php b/locallib.php index ede7b7e..fb6e66a 100644 --- a/locallib.php +++ b/locallib.php @@ -53,7 +53,7 @@ function quizgame_add_highscore($quizgame, $score) { global $USER, $DB; $cm = get_coursemodule_from_instance('quizgame', $quizgame->id, 0, false, MUST_EXIST); - $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST); + $course = $DB->get_record('course', ['id' => $cm->course], '*', MUST_EXIST); $context = context_module::instance($cm->id); // Write the high score to the DB. @@ -65,11 +65,12 @@ function quizgame_add_highscore($quizgame, $score) { $record->id = $DB->insert_record('quizgame_scores', $record); // Trigger the game score added event. - $event = \mod_quizgame\event\game_score_added::create(array( - 'objectid' => $record->id, + $event = \mod_quizgame\event\game_score_added::create( + ['objectid' => $record->id, 'context' => $context, - 'other' => array('score' => $score) - )); + 'other' => ['score' => $score], + ] + ); $event->add_record_snapshot('quizgame', $quizgame); $event->add_record_snapshot('quizgame_scores', $record); @@ -93,14 +94,15 @@ function quizgame_log_game_start($quizgame) { global $DB; $cm = get_coursemodule_from_instance('quizgame', $quizgame->id, 0, false, MUST_EXIST); - $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST); + $course = $DB->get_record('course', ['id' => $cm->course], '*', MUST_EXIST); $context = context_module::instance($cm->id); // Trigger the game score added event. - $event = \mod_quizgame\event\game_started::create(array( - 'objectid' => $quizgame->id, + $event = \mod_quizgame\event\game_started::create( + ['objectid' => $quizgame->id, 'context' => $context, - )); + ] + ); $event->add_record_snapshot('quizgame', $quizgame); $event->trigger(); diff --git a/mod_form.php b/mod_form.php index 69d1aee..c0fffc2 100644 --- a/mod_form.php +++ b/mod_form.php @@ -49,7 +49,7 @@ public function definition() { $mform->addElement('header', 'general', get_string('general', 'form')); // Adding the standard "name" field. - $mform->addElement('text', 'name', get_string('quizgamename', 'quizgame'), array('size' => '64')); + $mform->addElement('text', 'name', get_string('quizgamename', 'quizgame'), ['size' => '64']); if (!empty($CFG->formatstringstriptags)) { $mform->setType('name', PARAM_TEXT); } else { @@ -67,7 +67,7 @@ public function definition() { } $context = context_course::instance($COURSE->id); - $categories = qbank_managecategories\helper::question_category_options(array($context), false, 0); + $categories = qbank_managecategories\helper::question_category_options([$context], false, 0); $mform->addElement('selectgroups', 'questioncategory', get_string('questioncategory', 'quizgame'), $categories); $mform->addHelpButton('questioncategory', 'questioncategory', 'quizgame'); @@ -84,16 +84,16 @@ public function definition() { */ public function add_completion_rules() { $mform =& $this->_form; - $group = array(); + $group = []; $group[] =& $mform->createElement('checkbox', 'completionscoreenabled', '', get_string('completionscore', 'quizgame')); - $group[] =& $mform->createElement('text', 'completionscore', '', array('size' => 3)); + $group[] =& $mform->createElement('text', 'completionscore', '', ['size' => 3]); $mform->setType('completionscore', PARAM_INT); $mform->addGroup($group, 'completionscoregroup', - get_string('completionscoregroup', 'quizgame'), array(' '), false); + get_string('completionscoregroup', 'quizgame'), [' '], false); $mform->disabledIf('completionscore', 'completionscoreenabled', 'notchecked'); $mform->addHelpButton('completionscoregroup', 'completionscoregroup', 'quizgame'); - return array('completionscoregroup'); + return ['completionscoregroup']; } /** diff --git a/renderer.php b/renderer.php index 67a4f61..480d8d4 100644 --- a/renderer.php +++ b/renderer.php @@ -43,12 +43,12 @@ public function render_game($quizgame, $context) { $questionids = question_bank::get_finder()->get_questions_from_categories(intval($categoryid), ''); $questions = question_load_questions($questionids); - $this->page->requires->strings_for_js(array( - 'score', + $this->page->requires->strings_for_js( + ['score', 'emptyquiz', 'endofgame', - 'spacetostart' - ), 'mod_quizgame'); + 'spacetostart', + ], 'mod_quizgame'); $qjson = []; foreach ($questions as $question) { @@ -65,7 +65,7 @@ public function render_game($quizgame, $context) { $qjson[] = ["question" => $questiontext, "answers" => $answers, "type" => $question->qtype]; } else { $qjson[] = ["question" => $questiontext, "answers" => $answers, "type" => $question->qtype, - "single" => $question->qtype == "multichoice" && $question->options->single == 1]; + "single" => $question->qtype == "multichoice" && $question->options->single == 1, ]; } } if ($question->qtype == "match") { @@ -79,7 +79,7 @@ public function render_game($quizgame, $context) { } } - $this->page->requires->js_call_amd('mod_quizgame/quizgame', 'init', array($qjson, $quizgame->id)); + $this->page->requires->js_call_amd('mod_quizgame/quizgame', 'init', [$qjson, $quizgame->id]); $display = '
'; $display .= get_string('howtoplay', 'mod_quizgame') . $this->output->help_icon('howtoplay', 'mod_quizgame', ''); @@ -105,7 +105,7 @@ public function render_game($quizgame, $context) { $display .= '   '; $display .= html_writer::checkbox('sound', '', false, get_string('sound', 'mod_quizgame'), - array('id' => 'mod_quizgame_sound_on')); + ['id' => 'mod_quizgame_sound_on']); $display .= '
'; return $display; @@ -118,11 +118,11 @@ public function render_game($quizgame, $context) { */ public function render_score_link($quizgame) { - $url = new moodle_url('/mod/quizgame/scores.php', array('id' => $quizgame->id)); + $url = new moodle_url('/mod/quizgame/scores.php', ['id' => $quizgame->id]); $scorestring = get_string('scoreslink', 'quizgame'); $scorestringhelp = get_string('scoreslinkhelp', 'quizgame'); - $display = html_writer::start_tag('div', array('class' => 'quizgame-scores')); - $display .= html_writer::tag('a', $scorestring, array('title' => $scorestringhelp, 'href' => $url)); + $display = html_writer::start_tag('div', ['class' => 'quizgame-scores']); + $display .= html_writer::tag('a', $scorestring, ['title' => $scorestringhelp, 'href' => $url]); $display .= html_writer::end_tag('div'); return $display; } diff --git a/scores.php b/scores.php index 7ffe80d..bc9b22e 100644 --- a/scores.php +++ b/scores.php @@ -30,8 +30,8 @@ $download = optional_param('download', '', PARAM_ALPHA); if ($id) { - $quizgame = $DB->get_record('quizgame', array('id' => $id), '*', MUST_EXIST); - $course = $DB->get_record('course', array('id' => $quizgame->course), '*', MUST_EXIST); + $quizgame = $DB->get_record('quizgame', ['id' => $id], '*', MUST_EXIST); + $course = $DB->get_record('course', ['id' => $quizgame->course], '*', MUST_EXIST); $cm = get_coursemodule_from_instance('quizgame', $quizgame->id, $course->id, false, MUST_EXIST); } else { error('You must specify a course_module ID or an instance ID'); @@ -43,16 +43,18 @@ // Trigger scores viewed event. -$event = \mod_quizgame\event\game_scores_viewed::create(array( +$event = \mod_quizgame\event\game_scores_viewed::create( + [ 'objectid' => $quizgame->id, 'context' => $context, -)); + ] +); $event->add_record_snapshot('quizgame', $quizgame); $event->trigger(); // Print the page header. -$PAGE->set_url('/mod/quizgame/scores.php', array('id' => $quizgame->id)); +$PAGE->set_url('/mod/quizgame/scores.php', ['id' => $quizgame->id]); $PAGE->set_context($context); // Generate the table. @@ -63,7 +65,7 @@ // Print the page header. $PAGE->set_title(format_string($quizgame->name)); $PAGE->set_heading(format_string($course->fullname)); - $url = new moodle_url('/mod/quizgame/scores.php', array('id' => $quizgame->id)); + $url = new moodle_url('/mod/quizgame/scores.php', ['id' => $quizgame->id]); $PAGE->navbar->add(get_string('playerscores', 'mod_quizgame'), $url); echo $OUTPUT->header(); echo $OUTPUT->heading(get_string('modulename', 'mod_quizgame')); @@ -71,12 +73,12 @@ // Work out the sql for the table. $sqlconditions = 'quizgameid = :quizgameid'; -$sqlparams = array('quizgameid' => $quizgame->id); +$sqlparams = ['quizgameid' => $quizgame->id]; $table->set_sql('*', "{quizgame_scores}", $sqlconditions, $sqlparams); $table->define_baseurl($PAGE->url); -$columns = array('userid', 'score', 'timecreated'); -$headers = array(get_string('user'), get_string('scoreheader', 'mod_quizgame'), get_string('date')); +$columns = ['userid', 'score', 'timecreated']; +$headers = [get_string('user'), get_string('scoreheader', 'mod_quizgame'), get_string('date')]; $table->define_columns($columns); $table->define_headers($headers); $table->sortable(true, 'timecreated', SORT_DESC); diff --git a/tests/behat/behat_mod_quizgame.php b/tests/behat/behat_mod_quizgame.php index 2889391..07bdabb 100644 --- a/tests/behat/behat_mod_quizgame.php +++ b/tests/behat/behat_mod_quizgame.php @@ -13,6 +13,7 @@ // // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . + /** * Steps definitions related to mod_quiz. * @@ -52,7 +53,7 @@ public function user_has_played_with_a_score_of($username, $quizgamename, $score $user = $DB->get_record('user', ['username' => $username], '*', MUST_EXIST); $this->set_user($user); - $attemptdata = array(); + $attemptdata = []; if (isset($user->id)) { $attemptdata['userid'] = $user->id; } diff --git a/tests/behat/quizgame_add.feature b/tests/behat/quizgame_add.feature index 77e74bf..2c937bb 100644 --- a/tests/behat/quizgame_add.feature +++ b/tests/behat/quizgame_add.feature @@ -26,8 +26,7 @@ Feature: Teachers can create a quizgame activity for students to review content @javascript Scenario: Create the activity. Given I log in as "teacher1" - And I am on "Course 1" course homepage with editing mode on - When I add a "Quizventure" to section "1" and I fill the form with: + And I add a quizgame activity to course "Course 1" section "1" and I fill the form with: | Quizventure name | Test quizventure name | | Description | Test quizventure description | | ID number | Test quizventure name | diff --git a/tests/custom_completion_test.php b/tests/custom_completion_test.php index cfc8550..9c6d23f 100644 --- a/tests/custom_completion_test.php +++ b/tests/custom_completion_test.php @@ -45,26 +45,26 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @covers \mod_quizgame\completion\custom_completion */ -class custom_completion_test extends advanced_testcase { +final class custom_completion_test extends advanced_testcase { /** * Data provider for get_state(). * * @return array[] */ - public function get_state_provider(): array { + public static function get_state_provider(): array { return [ 'Undefined rule' => [ - 'somenonexistentrule', COMPLETION_DISABLED, 0, null, coding_exception::class + 'somenonexistentrule', COMPLETION_DISABLED, 0, null, coding_exception::class, ], 'Rule not available' => [ - 'completionscore', COMPLETION_DISABLED, 0, null, moodle_exception::class + 'completionscore', COMPLETION_DISABLED, 0, null, moodle_exception::class, ], 'Rule available, user has not finished' => [ - 'completionscore', COMPLETION_ENABLED, 0, COMPLETION_INCOMPLETE, null + 'completionscore', COMPLETION_ENABLED, 0, COMPLETION_INCOMPLETE, null, ], 'Rule available, user has finished' => [ - 'completionscore', COMPLETION_ENABLED, 1, COMPLETION_COMPLETE, null + 'completionscore', COMPLETION_ENABLED, 1, COMPLETION_COMPLETE, null, ], ]; } @@ -79,7 +79,7 @@ public function get_state_provider(): array { * @param int|null $status Expected status. * @param string|null $exception Expected exception. */ - public function test_get_state(string $rule, int $available, int $highscorecount, ?int $status, ?string $exception) { + public function test_get_state(string $rule, int $available, int $highscorecount, ?int $status, ?string $exception): void { global $DB; if (!is_null($exception)) { @@ -89,8 +89,8 @@ public function test_get_state(string $rule, int $available, int $highscorecount // Custom completion rule data for cm_info::customdata. $customdataval = [ 'customcompletionrules' => [ - $rule => $available - ] + $rule => $available, + ], ]; // Build a mock cm_info instance. @@ -120,7 +120,7 @@ public function test_get_state(string $rule, int $available, int $highscorecount /** * Test for get_defined_custom_rules(). */ - public function test_get_defined_custom_rules() { + public function test_get_defined_custom_rules(): void { $rules = custom_completion::get_defined_custom_rules(); $this->assertCount(1, $rules); $this->assertEquals('completionscore', $rules[0]); @@ -129,7 +129,7 @@ public function test_get_defined_custom_rules() { /** * Test for get_defined_custom_rule_descriptions(). */ - public function test_get_custom_rule_descriptions() { + public function test_get_custom_rule_descriptions(): void { // Get defined custom rules. $rules = custom_completion::get_defined_custom_rules(); @@ -155,7 +155,7 @@ public function test_get_custom_rule_descriptions() { /** * Test for is_defined(). */ - public function test_is_defined() { + public function test_is_defined(): void { // Build a mock cm_info instance. $mockcminfo = $this->getMockBuilder(cm_info::class) ->disableOriginalConstructor() @@ -175,13 +175,13 @@ public function test_is_defined() { * * @return array[] */ - public function get_available_custom_rules_provider(): array { + public static function get_available_custom_rules_provider(): array { return [ 'Completion submit available' => [ - COMPLETION_ENABLED, ['completionscore'] + COMPLETION_ENABLED, ['completionscore'], ], 'Completion submit not available' => [ - COMPLETION_DISABLED, [] + COMPLETION_DISABLED, [], ], ]; } @@ -193,11 +193,11 @@ public function get_available_custom_rules_provider(): array { * @param int $status * @param array $expected */ - public function test_get_available_custom_rules(int $status, array $expected) { + public function test_get_available_custom_rules(int $status, array $expected): void { $customdataval = [ 'customcompletionrules' => [ - 'completionscore' => $status - ] + 'completionscore' => $status, + ], ]; // Build a mock cm_info instance. diff --git a/tests/generator/lib.php b/tests/generator/lib.php index 3ce938e..b021941 100644 --- a/tests/generator/lib.php +++ b/tests/generator/lib.php @@ -14,6 +14,10 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +// Let codechecker ignore the sniff for this file for nullable types since the super method of +// create_instance is not yet rewritten and mod_quizgame_generator::create_instance must have an identical signature. +// phpcs:disable PHPCompatibility.FunctionDeclarations.RemovedImplicitlyNullableParam.Deprecated + /** * mod_quizgame data generator class. * @@ -26,17 +30,17 @@ class mod_quizgame_generator extends testing_module_generator { /** * Create an instance of mod_quizgame with some default settings - * @param object $record - * @param array $options + * @param array|stdClass $record quizgame settings + * @param null|array $options quizgame options */ public function create_instance($record = null, array $options = null) { // Add default values for quizgame. - $record = (array)$record + array( + $record = (array)$record + [ 'questioncategory' => 0, 'grade' => 100, 'completionscore' => 0, - ); + ]; return parent::create_instance($record, (array)$options); } @@ -47,18 +51,18 @@ public function create_instance($record = null, array $options = null) { * @param array $record quizgame settings * @return object */ - public function create_content($quizgame, $record = array()) { + public function create_content($quizgame, $record = []) { global $DB, $USER; $now = time(); - $record = (array)$record + array( + $record = (array)$record + [ 'quizgameid' => $quizgame->id, 'timecreated' => $now, 'userid' => $USER->id, 'score' => mt_rand (0, 50000), - ); + ]; $id = $DB->insert_record('quizgame_scores', $record); - return $DB->get_record('quizgame_scores', array('id' => $id), '*', MUST_EXIST); + return $DB->get_record('quizgame_scores', ['id' => $id], '*', MUST_EXIST); } } diff --git a/tests/mod_quizgame_event_testcase.php b/tests/mod_quizgame_event_testcase.php index 9a95ad4..243c86b 100644 --- a/tests/mod_quizgame_event_testcase.php +++ b/tests/mod_quizgame_event_testcase.php @@ -55,16 +55,16 @@ public function test_course_module_viewed() { // doing here is simply making sure that the events returns the right information. $course = $this->getDataGenerator()->create_course(); - $quizgame = $this->getDataGenerator()->create_module('quizgame', array('course' => $course->id)); + $quizgame = $this->getDataGenerator()->create_module('quizgame', ['course' => $course->id]); - $dbcourse = $DB->get_record('course', array('id' => $course->id)); - $dbquizgame = $DB->get_record('quizgame', array('id' => $quizgame->id)); + $dbcourse = $DB->get_record('course', ['id' => $course->id]); + $dbquizgame = $DB->get_record('quizgame', ['id' => $quizgame->id]); $context = \context_module::instance($quizgame->cmid); - $event = \mod_quizgame\event\course_module_viewed::create(array( + $event = \mod_quizgame\event\course_module_viewed::create([ 'objectid' => $dbquizgame->id, 'context' => $context, - )); + ]); $event->add_record_snapshot('course', $dbcourse); $event->add_record_snapshot('quizgame', $dbquizgame); @@ -81,10 +81,10 @@ public function test_course_module_viewed() { $this->assertEquals(CONTEXT_MODULE, $event->contextlevel); $this->assertEquals($quizgame->cmid, $event->contextinstanceid); $this->assertEquals($quizgame->id, $event->objectid); - $expected = array($course->id, 'quizgame', 'view', 'view.php?id=' . $quizgame->cmid, - $quizgame->id, $quizgame->cmid); + $expected = [$course->id, 'quizgame', 'view', 'view.php?id=' . $quizgame->cmid, + $quizgame->id, $quizgame->cmid, ]; - $this->assertEquals(new \moodle_url('/mod/quizgame/view.php', array('id' => $quizgame->cmid)), $event->get_url()); + $this->assertEquals(new \moodle_url('/mod/quizgame/view.php', ['id' => $quizgame->cmid]), $event->get_url()); $this->assertEventContextNotUsed($event); } @@ -97,9 +97,9 @@ public function test_course_module_instance_list_viewed() { $course = $this->getDataGenerator()->create_course(); - $event = \mod_quizgame\event\course_module_instance_list_viewed::create(array( - 'context' => \context_course::instance($course->id) - )); + $event = \mod_quizgame\event\course_module_instance_list_viewed::create([ + 'context' => \context_course::instance($course->id), + ]); // Triggering and capturing the event. $sink = $this->redirectEvents(); @@ -112,7 +112,7 @@ public function test_course_module_instance_list_viewed() { $this->assertInstanceOf('\mod_quizgame\event\course_module_instance_list_viewed', $event); $this->assertEquals(CONTEXT_COURSE, $event->contextlevel); $this->assertEquals($course->id, $event->contextinstanceid); - $expected = array($course->id, 'quizgame', 'view all', 'index.php?id='.$course->id, ''); + $expected = [$course->id, 'quizgame', 'view all', 'index.php?id='.$course->id, '']; $this->assertEventLegacyLogData($expected, $event); $this->assertEventContextNotUsed($event); } @@ -124,7 +124,7 @@ public function test_score_added() { $this->setAdminUser(); $course = $this->getDataGenerator()->create_course(); - $quizgame = $this->getDataGenerator()->create_module('quizgame', array('course' => $course)); + $quizgame = $this->getDataGenerator()->create_module('quizgame', ['course' => $course]); $context = \context_module::instance($quizgame->cmid); $score = mt_rand (0, 50000); @@ -149,7 +149,7 @@ public function test_game_started() { $this->setAdminUser(); $course = $this->getDataGenerator()->create_course(); - $quizgame = $this->getDataGenerator()->create_module('quizgame', array('course' => $course)); + $quizgame = $this->getDataGenerator()->create_module('quizgame', ['course' => $course]); $context = \context_module::instance($quizgame->cmid); $sink = $this->redirectEvents(); @@ -174,16 +174,16 @@ public function test_game_scores_viewed() { $this->setAdminUser(); $course = $this->getDataGenerator()->create_course(); - $quizgame = $this->getDataGenerator()->create_module('quizgame', array('course' => $course)); + $quizgame = $this->getDataGenerator()->create_module('quizgame', ['course' => $course]); $context = \context_module::instance($quizgame->cmid); $quizgamegenerator = $this->getDataGenerator()->get_plugin_generator('mod_quizgame'); $scores = $quizgamegenerator->create_content($quizgame); - $event = \mod_quizgame\event\game_scores_viewed::create(array( + $event = \mod_quizgame\event\game_scores_viewed::create([ 'objectid' => $quizgame->id, - 'context' => $context - )); + 'context' => $context, + ]); $sink = $this->redirectEvents(); $event->trigger(); diff --git a/tests/mod_quizgame_generator_testcase.php b/tests/mod_quizgame_generator_testcase.php index 7c113b3..fbb8d28 100644 --- a/tests/mod_quizgame_generator_testcase.php +++ b/tests/mod_quizgame_generator_testcase.php @@ -45,15 +45,15 @@ public function test_create_instance() { $course = $this->getDataGenerator()->create_course(); - $this->assertFalse($DB->record_exists('quizgame', array('course' => $course->id))); - $quizgame = $this->getDataGenerator()->create_module('quizgame', array('course' => $course)); - $records = $DB->get_records('quizgame', array('course' => $course->id), 'id'); + $this->assertFalse($DB->record_exists('quizgame', ['course' => $course->id])); + $quizgame = $this->getDataGenerator()->create_module('quizgame', ['course' => $course]); + $records = $DB->get_records('quizgame', ['course' => $course->id], 'id'); $this->assertCount(1, $records); $this->assertTrue(array_key_exists($quizgame->id, $records)); - $params = array('course' => $course->id, 'name' => 'Another quizgame'); + $params = ['course' => $course->id, 'name' => 'Another quizgame']; $quizgame = $this->getDataGenerator()->create_module('quizgame', $params); - $records = $DB->get_records('quizgame', array('course' => $course->id), 'id'); + $records = $DB->get_records('quizgame', ['course' => $course->id], 'id'); $this->assertCount(2, $records); $this->assertEquals('Another quizgame', $records[$quizgame->id]->name); } @@ -67,12 +67,12 @@ public function test_create_content() { $this->setAdminUser(); $course = $this->getDataGenerator()->create_course(); - $quizgame = $this->getDataGenerator()->create_module('quizgame', array('course' => $course)); + $quizgame = $this->getDataGenerator()->create_module('quizgame', ['course' => $course]); $quizgamegenerator = $this->getDataGenerator()->get_plugin_generator('mod_quizgame'); $playthrough1 = $quizgamegenerator->create_content($quizgame); - $playthrough2 = $quizgamegenerator->create_content($quizgame, array('score' => 3550)); - $records = $DB->get_records('quizgame_scores', array('quizgameid' => $quizgame->id), 'id'); + $playthrough2 = $quizgamegenerator->create_content($quizgame, ['score' => 3550]); + $records = $DB->get_records('quizgame_scores', ['quizgameid' => $quizgame->id], 'id'); $this->assertCount(2, $records); $this->assertEquals($playthrough1->id, $records[$playthrough1->id]->id); $this->assertEquals($playthrough2->id, $records[$playthrough2->id]->id); diff --git a/tests/mod_quizgame_lib_testcase.php b/tests/mod_quizgame_lib_testcase.php index fdebf2a..d4bb1eb 100644 --- a/tests/mod_quizgame_lib_testcase.php +++ b/tests/mod_quizgame_lib_testcase.php @@ -17,10 +17,10 @@ /** * Unit tests for lib.php * - * @package mod_quizgame - * @category test - * @copyright 2019 Stephen Bourget - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @package mod_quizgame + * @category test + * @copyright 2019 Stephen Bourget + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ namespace mod_quizgame; @@ -32,13 +32,14 @@ /** * Unit tests for quizgame calendar events. * - * @package mod_quizgame - * @category test - * @copyright 2019 Stephen Bourget + * @package mod_quizgame + * @category test + * @copyright 2019 Stephen Bourget * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class mod_quizgame_lib_testcase extends \advanced_testcase { + /** * Test calendar event creation. */ @@ -47,10 +48,12 @@ public function test_quizgame_core_calendar_provide_event_action() { $this->setAdminUser(); // Create the activity. $course = $this->getDataGenerator()->create_course(); - $quizgame = $this->getDataGenerator()->create_module('quizgame', array('course' => $course->id)); + $quizgame = $this->getDataGenerator()->create_module('quizgame', ['course' => $course->id]); // Create a calendar event. - $event = $this->create_action_event($course->id, $quizgame->id, - \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED); + $event = $this->create_action_event( + $course->id, $quizgame->id, + \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED + ); // Create an action factory. $factory = new \core_calendar\action_factory(); // Decorate action event. @@ -72,10 +75,12 @@ public function test_quizgame_core_calendar_provide_event_action_for_non_user() $this->setAdminUser(); // Create the activity. $course = $this->getDataGenerator()->create_course(); - $quizgame = $this->getDataGenerator()->create_module('quizgame', array('course' => $course->id)); + $quizgame = $this->getDataGenerator()->create_module('quizgame', ['course' => $course->id]); // Create a calendar event. - $event = $this->create_action_event($course->id, $quizgame->id, - \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED); + $event = $this->create_action_event( + $course->id, $quizgame->id, + \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED + ); // Now, log out. $CFG->forcelogin = true; // We don't want to be logged in as guest, as guest users might still have some capabilities. $this->setUser(); @@ -96,11 +101,13 @@ public function test_quizgame_core_calendar_provide_event_action_for_user() { $this->setAdminUser(); // Create the activity. $course = $this->getDataGenerator()->create_course(); - $quizgame = $this->getDataGenerator()->create_module('quizgame', array('course' => $course->id)); + $quizgame = $this->getDataGenerator()->create_module('quizgame', ['course' => $course->id]); $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); // Create a calendar event. - $event = $this->create_action_event($course->id, $quizgame->id, - \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED); + $event = $this->create_action_event( + $course->id, $quizgame->id, + \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED + ); // Now log out. $CFG->forcelogin = true; // We don't want to be logged in as guest, as guest users might still have some capabilities. $this->setUser(); @@ -125,14 +132,18 @@ public function test_quizgame_core_calendar_provide_event_action_already_complet $this->setAdminUser(); $CFG->enablecompletion = 1; // Create the activity. - $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1)); - $quizgame = $this->getDataGenerator()->create_module('quizgame', array('course' => $course->id), - array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS)); + $course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]); + $quizgame = $this->getDataGenerator()->create_module( + 'quizgame', ['course' => $course->id], + ['completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS] + ); // Get some additional data. $cm = get_coursemodule_from_instance('quizgame', $quizgame->id); // Create a calendar event. - $event = $this->create_action_event($course->id, $quizgame->id, - \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED); + $event = $this->create_action_event( + $course->id, $quizgame->id, + \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED + ); // Mark the activity as completed. $completion = new \completion_info($course); $completion->set_module_viewed($cm); @@ -153,17 +164,21 @@ public function test_quizgame_core_calendar_provide_event_action_already_complet $this->setAdminUser(); $CFG->enablecompletion = 1; // Create the activity. - $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1)); - $quizgame = $this->getDataGenerator()->create_module('quizgame', array('course' => $course->id), - array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS)); + $course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]); + $quizgame = $this->getDataGenerator()->create_module( + 'quizgame', ['course' => $course->id], + ['completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS] + ); // Create 2 students and enrol them into the course. $student1 = $this->getDataGenerator()->create_and_enrol($course, 'student'); $student2 = $this->getDataGenerator()->create_and_enrol($course, 'student'); // Get some additional data. $cm = get_coursemodule_from_instance('quizgame', $quizgame->id); // Create a calendar event. - $event = $this->create_action_event($course->id, $quizgame->id, - \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED); + $event = $this->create_action_event( + $course->id, $quizgame->id, + \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED + ); // Mark the activity as completed for the $student1. $completion = new \completion_info($course); $completion->set_module_viewed($cm, $student1->id); @@ -179,9 +194,9 @@ public function test_quizgame_core_calendar_provide_event_action_already_complet /** * Creates an action event. * - * @param int $courseid The course id. - * @param int $instanceid The instance id. - * @param string $eventtype The event type. + * @param int $courseid The course id. + * @param int $instanceid The instance id. + * @param string $eventtype The event type. * @return bool|calendar_event */ private function create_action_event($courseid, $instanceid, $eventtype) { diff --git a/tests/privacy/provider_test.php b/tests/privacy/provider_test.php index 0856eb5..e5d8b67 100644 --- a/tests/privacy/provider_test.php +++ b/tests/privacy/provider_test.php @@ -35,7 +35,7 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @covers \mod_quizgame\privacy\provider */ -class provider_test extends \core_privacy\tests\provider_testcase { +final class provider_test extends \core_privacy\tests\provider_testcase { /** @var stdClass The student object. */ protected $student; @@ -49,12 +49,13 @@ class provider_test extends \core_privacy\tests\provider_testcase { * {@inheritdoc} */ protected function setUp(): void { + parent::setUp(); $this->resetAfterTest(); global $DB; $generator = $this->getDataGenerator(); $course = $generator->create_course(); - $quizgame = $this->getDataGenerator()->create_module('quizgame', array('course' => $course)); + $quizgame = $this->getDataGenerator()->create_module('quizgame', ['course' => $course]); // Create a quizgame activity. $quizgamegenerator = $this->getDataGenerator()->get_plugin_generator('mod_quizgame'); @@ -65,7 +66,7 @@ protected function setUp(): void { $generator->enrol_user($student->id, $course->id, $studentrole->id); // Have the student play through the game. - $playthrough = $quizgamegenerator->create_content($quizgame, array('userid' => $student->id, 'score' => '9999')); + $playthrough = $quizgamegenerator->create_content($quizgame, ['userid' => $student->id, 'score' => '9999']); $this->student = $student; $this->quizgame = $quizgame; @@ -75,7 +76,7 @@ protected function setUp(): void { /** * Test for provider::get_metadata(). */ - public function test_get_metadata() { + public function test_get_metadata(): void { $collection = new collection('mod_quizgame'); $newcollection = provider::get_metadata($collection); $itemcollection = $newcollection->get_collection(); @@ -96,7 +97,7 @@ public function test_get_metadata() { /** * Test for provider::get_contexts_for_userid(). */ - public function test_get_contexts_for_userid() { + public function test_get_contexts_for_userid(): void { $cm = get_coursemodule_from_instance('quizgame', $this->quizgame->id); $contextlist = provider::get_contexts_for_userid($this->student->id); @@ -109,7 +110,7 @@ public function test_get_contexts_for_userid() { /** * Test for provider::export_user_data(). */ - public function test_export_for_context() { + public function test_export_for_context(): void { $cm = get_coursemodule_from_instance('quizgame', $this->quizgame->id); $cmcontext = \context_module::instance($cm->id); @@ -122,7 +123,7 @@ public function test_export_for_context() { /** * Test for provider::delete_data_for_all_users_in_context(). */ - public function test_delete_data_for_all_users_in_context() { + public function test_delete_data_for_all_users_in_context(): void { global $DB; $quizgame = $this->quizgame; @@ -134,7 +135,7 @@ public function test_delete_data_for_all_users_in_context() { $student = $generator->create_user(); $studentrole = $DB->get_record('role', ['shortname' => 'student']); $generator->enrol_user($student->id, $this->course->id, $studentrole->id); - $playthrough = $quizgamegenerator->create_content($quizgame, array('userid' => $student->id, 'score' => '100001')); + $playthrough = $quizgamegenerator->create_content($quizgame, ['userid' => $student->id, 'score' => '100001']); // Before deletion, we should have 2 responses. $count = $DB->count_records('quizgame_scores', ['quizgameid' => $quizgame->id]); @@ -152,7 +153,7 @@ public function test_delete_data_for_all_users_in_context() { /** * Test for provider::delete_data_for_user(). */ - public function test_delete_data_for_user_() { + public function test_delete_data_for_user_(): void { global $DB; $quizgame = $this->quizgame; @@ -160,20 +161,20 @@ public function test_delete_data_for_user_() { $cm1 = get_coursemodule_from_instance('quizgame', $this->quizgame->id); // Create a second quizgame activity. - $params = array('course' => $this->course->id, 'name' => 'Another quizgame'); + $params = ['course' => $this->course->id, 'name' => 'Another quizgame']; $plugingenerator = $generator->get_plugin_generator('mod_quizgame'); $quizgame2 = $plugingenerator->create_instance($params); $plugingenerator->create_instance($params); $cm2 = get_coursemodule_from_instance('quizgame', $quizgame2->id); // Make a playthrough for the first student in the 2nd quizgame activity. - $playthrough = $plugingenerator->create_content($quizgame2, array('userid' => $this->student->id, 'score' => '100')); + $playthrough = $plugingenerator->create_content($quizgame2, ['userid' => $this->student->id, 'score' => '100']); // Create another student who will play the first quizgame activity. $otherstudent = $generator->create_user(); $studentrole = $DB->get_record('role', ['shortname' => 'student']); $generator->enrol_user($otherstudent->id, $this->course->id, $studentrole->id); - $playthrough2 = $plugingenerator->create_content($quizgame, array('userid' => $otherstudent->id, 'score' => '999')); + $playthrough2 = $plugingenerator->create_content($quizgame, ['userid' => $otherstudent->id, 'score' => '999']); // Before deletion, we should have 2 responses. $count = $DB->count_records('quizgame_scores', ['quizgameid' => $quizgame->id]); diff --git a/version.php b/version.php index 5096016..a5a311c 100644 --- a/version.php +++ b/version.php @@ -14,7 +14,6 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . - /** * Defines the version of quizgame * diff --git a/view.php b/view.php index 45a6487..98ed786 100644 --- a/view.php +++ b/view.php @@ -34,11 +34,11 @@ if ($id) { $cm = get_coursemodule_from_id('quizgame', $id, 0, false, MUST_EXIST); - $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST); - $quizgame = $DB->get_record('quizgame', array('id' => $cm->instance), '*', MUST_EXIST); + $course = $DB->get_record('course', ['id' => $cm->course], '*', MUST_EXIST); + $quizgame = $DB->get_record('quizgame', ['id' => $cm->instance], '*', MUST_EXIST); } else if ($n) { - $quizgame = $DB->get_record('quizgame', array('id' => $n), '*', MUST_EXIST); - $course = $DB->get_record('course', array('id' => $quizgame->course), '*', MUST_EXIST); + $quizgame = $DB->get_record('quizgame', ['id' => $n], '*', MUST_EXIST); + $course = $DB->get_record('course', ['id' => $quizgame->course], '*', MUST_EXIST); $cm = get_coursemodule_from_instance('quizgame', $quizgame->id, $course->id, false, MUST_EXIST); } else { throw new moodle_exception('invalidcmorid', 'quizgame'); @@ -49,10 +49,11 @@ $context = context_module::instance($cm->id); // Trigger module viewed event. -$event = \mod_quizgame\event\course_module_viewed::create(array( - 'objectid' => $quizgame->id, +$event = \mod_quizgame\event\course_module_viewed::create( + ['objectid' => $quizgame->id, 'context' => $context, -)); + ] +); $event->add_record_snapshot('course', $course); $event->add_record_snapshot('course_modules', $cm); @@ -64,7 +65,7 @@ $completion->set_module_viewed($cm); // Print the page header. -$PAGE->set_url('/mod/quizgame/view.php', array('id' => $cm->id)); +$PAGE->set_url('/mod/quizgame/view.php', ['id' => $cm->id]); $PAGE->set_title(format_string($quizgame->name)); $PAGE->set_heading(format_string($course->fullname)); $PAGE->set_context($context);