diff --git a/CHANGES.md b/CHANGES.md index ef07474..adaeaaf 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,6 @@ +Changes in version 2017-08-11 (2017081103) +- New: High score + Changes in version 2017-08-08 (2017080601) - New: check params of crossword/cryptex diff --git a/db/upgrade.php b/db/upgrade.php index 784b9fd..99fbcf0 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -1687,7 +1687,7 @@ function xmldb_game_upgrade($oldversion) { if ($oldversion < ($ver = 2017071901)) { - // Define field completionattemptsexhausted to be added to quiz. + // Define field completionattemptsexhausted to be added to game. $table = new xmldb_table('game'); $field = new xmldb_field('completionattemptsexhausted', XMLDB_TYPE_INTEGER, '1', null, null, null, '0'); @@ -1700,7 +1700,7 @@ function xmldb_game_upgrade($oldversion) { } if ($oldversion < ($ver = 2017071902)) { - // Define field completionpass to be added to quiz. + // Define field completionpass to be added to game. $table = new xmldb_table('game'); $field = new xmldb_field('completionpass', XMLDB_TYPE_INTEGER, '1', null, null, null, 0, 'completionattemptsexhausted'); @@ -1712,6 +1712,17 @@ function xmldb_game_upgrade($oldversion) { upgrade_mod_savepoint(true, $ver, 'game'); } + if ($oldversion < ($ver = 2017081102)) { + // Define field highscore to be added to game. + $table = new xmldb_table('game'); + $field = new xmldb_field('highscore', XMLDB_TYPE_INTEGER, '2', null, null, null, 0); + + // Conditionally launch add field completionpass. + $dbman->add_field($table, $field); + + upgrade_mod_savepoint(true, $ver, 'game'); + } + return true; } diff --git a/lang/en/game.php b/lang/en/game.php index f76bcea..9027b1b 100644 --- a/lang/en/game.php +++ b/lang/en/game.php @@ -308,6 +308,7 @@ $string[ 'sudoku_options'] = 'Sudoku options'; $string[ 'toptext'] = 'Text at the top of page'; $string[ 'userdefined'] = 'User defined'; $string[ 'different_glossary_category'] = "The selected category doesn't corespond to selected glossary"; +$string[ 'highscore'] = 'Show high score (number of students)'; // File preview.php. $string[ 'only_teachers'] = 'Only teacher can see this page'; @@ -322,6 +323,7 @@ $string[ 'reviewofattempt'] = 'Review of Attempt {$a}'; $string[ 'showall'] = 'Show all'; $string[ 'startagain'] = 'Start again'; $string[ 'timetaken'] = 'Time taken'; +$string[ 'col_highscores'] = 'High scores'; // File settings.php. $string[ 'hangmanimagesets'] = 'Number of image sets used by hangman'; diff --git a/mod_form.php b/mod_form.php index 8beaf53..c148d57 100644 --- a/mod_form.php +++ b/mod_form.php @@ -161,6 +161,10 @@ class mod_game_mod_form extends moodleform_mod { // Disable summarize. $mform->addElement('selectyesno', 'disablesummarize', get_string('disablesummarize', 'game')); + // Enable high score. + $mform->addElement('text', 'highscore', get_string('highscore', 'game')); + $mform->setType('highscore', PARAM_INT); + // Grade options. $this->standard_grading_coursemodule_elements(); $mform->removeElement('grade'); diff --git a/version.php b/version.php index 9ea917b..491bdf6 100644 --- a/version.php +++ b/version.php @@ -35,7 +35,7 @@ if (!isset( $plugin)) { } $plugin->component = 'mod_game'; // Full name of the plugin (used for diagnostics). -$plugin->version = 2017081101; // The current module version (Date: YYYYMMDDXX). +$plugin->version = 2017081103; // The current module version (Date: YYYYMMDDXX). $plugin->requires = 2010112400; // Requires Moodle 2.0. $plugin->cron = 0; // Period for cron to check this module (secs). $plugin->release = '2017-08-11'; diff --git a/view.php b/view.php index 3edbd1f..b1a842d 100644 --- a/view.php +++ b/view.php @@ -326,6 +326,11 @@ if ($buttontext) { } echo $OUTPUT->box_end(); +if ($game->highscore > 0) { + // Display high score. + game_highscore( $game); +} + if (has_capability('mod/game:manage', $context)) { require( 'check.php'); $s = game_check_common_problems( $context, $game); @@ -335,3 +340,67 @@ if (has_capability('mod/game:manage', $context)) { } echo $OUTPUT->footer(); + +/** + * Computes high score for this game. Shows the names of $game->highscore students. + * + * @param stdClass $table the name of table + * @return string the high score + */ +function game_highscore( $game) { + global $CFG, $DB, $OUTPUT; + + $sql = "SELECT userid, MAX(score) as maxscore". + " FROM {$CFG->prefix}game_attempts ". + " WHERE gameid={$game->id} AND score > 0". + " GROUP BY userid". + " ORDER BY score DESC"; + $score = 0; + $recs = $DB->get_records_sql( $sql); + foreach ($recs as $rec) { + $score = $rec->maxscore; + } + if ($score == 0) { + return; + } + + $sql = "SELECT u.id, u.lastname, u.firstname, MAX(ga.score) as maxscore". + " FROM {$CFG->prefix}user u, {$CFG->prefix}game_attempts ga ". + " WHERE ga.gameid={$game->id} AND ga.userid = u.id". + " GROUP BY u.id,u.lastname,u.firstname". + " HAVING MAX(ga.score) >= $score". + " ORDER BY MAX(ga.score) DESC"; + + $recs = $DB->get_records_sql( $sql, null, 0, $game->highscore); + if (count( $recs) == 0) { + return false; + } + + // Prepare table header. + $table = new html_table(); + $table->attributes['class'] = 'generaltable gameattemptsummary'; + $table->head = array(); + $table->align = array(); + $table->size = array(); + + $table->head[] = get_string('students'); + $table->align[] = 'left'; + $table->size[] = ''; + + $table->head[] = get_string('percent', 'grades'); + $table->align[] = 'center'; + $table->size[] = ''; + + foreach ($recs as $rec) { + echo ""; + $row = array(); + $row[] = $rec->firstname.' '.$rec->lastname; + $row[] = round( $rec->maxscore * 100).' %'; + + $table->data[$rec->id] = $row; + } + + echo '
'.$OUTPUT->heading(get_string('col_highscores', 'game')); + + echo html_writer::table($table); +}