diff --git lib/dml/moodle_database.php lib/dml/moodle_database.php index c7ba052..41e9f38 100644 --- lib/dml/moodle_database.php +++ lib/dml/moodle_database.php @@ -2081,6 +2081,44 @@ abstract class moodle_database { } /** + * Does this driver support RAND() or RANDOM() or equivalent + */ + public function sql_random_supported() { + return false; + } + + /** + * Does this driver support passing a seed to the native random function + * Implementing this because some DB (SQLite) do not support passing seed values + */ + public function sql_random_seed_supported() { + return false; + } + + /** + * Returns the supported random fragment as part of a query. + * + * Note that not all DB Platforms support seed values in their random function + * Also, if random is supported but not seed, do not enable the use of random at this point in time + * + * @param int $seed integer value to use as the seed, so the DB will maintain the rows returned in the same order when RAND() is invoked + * @param string $default_column default column to sort by should RANDOM(seed) is not supported + * @return string SQL code fragment + */ + public function sql_order_by_random($seed, $default_column) { + if ($this->sql_random_supported() && $this->sql_random_seed_supported() && $seed) { + return $this->sql_random($seed); + } +// Disabled. Randomly orders and doesn't persist order. May enable later if required +// elseif ($this->sql_random_supported()) { +// return $this->sql_random(); +// } + else { + return $default_column; + } + } + + /** * Returns the SQL text to be used to calculate the length in characters of one expression. * @param string $fieldname The fieldname/expression to calculate its length in characters. * @return string the piece of SQL code to be used in the statement. diff --git lib/dml/mysqli_native_moodle_database.php lib/dml/mysqli_native_moodle_database.php index 127d389..6557186 100644 --- lib/dml/mysqli_native_moodle_database.php +++ lib/dml/mysqli_native_moodle_database.php @@ -1529,6 +1529,32 @@ class mysqli_native_moodle_database extends moodle_database { } /** + * Does this driver support RAND() or RANDOM() or equivalent + */ + public function sql_random_supported() { + return true; + } + + /** + * Does this driver support passing a seed to the native random function + */ + public function sql_random_seed_supported() { + return true; + } + + /** + * Returns the supported random fragment as part of a query. + * + * @param int $seed integer value to use as the seed, so the DB will maintain the rows returned in the same order when RAND() is invoked + * @return string SQL code fragment + */ + public function sql_random ($seed) { + return ($seed) + ? "RAND($seed)" + : "RAND()"; + } + + /** * Returns the proper SQL to do CONCAT between the elements passed * Can take many parameters * diff --git lib/tablelib.php lib/tablelib.php index d2ec1e3..e34ddef 100644 --- lib/tablelib.php +++ lib/tablelib.php @@ -555,6 +555,14 @@ class flexible_table { } /** + * @return SQL fragment for RANDOM with seed that can be used in an ORDER BY clause. + */ + public function get_sql_random() { + global $DB; + return $DB->sql_order_by_random($this->randomsortseed, $this->sort_default_column); + } + + /** * Get the columns to sort by, in the form required by {@link construct_order_by()}. * @return array column name => SORT_... constant. */ @@ -1434,6 +1442,7 @@ class table_sql extends flexible_table { */ function query_db($pagesize, $useinitialsbar=true) { global $DB; + if (!$this->is_downloading()) { if ($this->countsql === NULL) { $this->countsql = 'SELECT COUNT(1) FROM '.$this->sql->from.' WHERE '.$this->sql->where; @@ -1461,7 +1470,11 @@ class table_sql extends flexible_table { } // Fetch the attempts - $sort = $this->get_sql_sort(); + // Random sort trumps ordinary sort, because if you declare you wanted a random sort, you wouldn't really + // care about sorting by specific columns, yay/nay? + $sort = ($random_sort) + ? $this->get_sql_sort() + : $this->get_sql_random(); if ($sort) { $sort = "ORDER BY $sort"; } diff --git mod/assign/gradingtable.php mod/assign/gradingtable.php index e54bd80..3bb9ed1 100644 --- mod/assign/gradingtable.php +++ mod/assign/gradingtable.php @@ -423,6 +423,14 @@ class assign_grading_table extends table_sql implements renderable { } } + // If blind marking, assign randomly with seed + if ($this->assignment->is_blind_marking()) { + $this->randomsort = true; + // Currently using course id as the seed value. However, recommend generating a different seed value + // which can persist over the session + $this->randomsortseed = $this->assignment->get_course()->id; + } + // When there is no data we still want the column headers printed in the csv file. if ($this->is_downloading()) { $this->start_output();