Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions classes/utils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Utility functions for the oumultiresponse question type.
*
* @package qtype_oumultiresponse
* @copyright 2025 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace qtype_oumultiresponse;

/**
* Class that holds utility functions used by the oumultiresponse question type.
*
* @package qtype_oumultiresponse
* @copyright 2025 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class utils {

/** @var string - Less than operator */
const OP_LT = "<";
/** @var string - equal operator */
const OP_E = "=";
/** @var string - greater than operator */
const OP_GT = ">";

/**
* Conveniently compare the current moodle version to a provided version in branch format. This function will
* inflate version numbers to a three digit number before comparing them. This way moodle minor versions greater
* than 9 can be correctly and easily compared.
*
* Examples:
* utils::moodle_version_is("<", "39");
* utils::moodle_version_is("<=", "310");
* utils::moodle_version_is(">", "39");
* utils::moodle_version_is(">=", "38");
* utils::moodle_version_is("=", "41");
*
* CFG reference:
* $CFG->branch = "311", "310", "39", "38", ...
* $CFG->release = "3.11+ (Build: 20210604)", ...
* $CFG->version = "2021051700.04", ...
*
* @param string $operator for the comparison
* @param string $version to compare to
* @return boolean
*/
public static function moodle_version_is(string $operator, string $version): bool {
global $CFG;

if (strlen($version) == 2) {
$version = $version[0]."0".$version[1];
}

$current = $CFG->branch;
if (strlen($current) == 2) {
$current = $current[0]."0".$current[1];
}

$from = intval($current);
$to = intval($version);
$ops = str_split($operator);

foreach ($ops as $op) {
switch ($op) {
case self::OP_LT:
if ($from < $to) {
return true;
}
break;
case self::OP_E:
if ($from == $to) {
return true;
}
break;
case self::OP_GT:
if ($from > $to) {
return true;
}
break;
default:
throw new \coding_exception('invalid operator '.$op);
}
}

return false;
}
}
2 changes: 1 addition & 1 deletion tests/privacy_provider_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function test_export_user_preferences($name, $value, $expected) {
*
* @return array Array of valid user preferences.
*/
public function user_preference_provider() {
public static function user_preference_provider() {
return [
'default mark 2' => ['defaultmark', 2, 2],
'penalty 33.33333%' => ['penalty', 0.3333333, '33.33333%'],
Expand Down
6 changes: 5 additions & 1 deletion tests/walkthrough_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,13 @@ public function test_shows_standrd_instruction_no() {
$this->start_attempt_at_question($mc, 'interactive', 3);
$this->render();

$standardinstructionclass = 'prompt h6 fw-normal visually-hidden';
if (utils::moodle_version_is("<=" , "45")) {
$standardinstructionclass = 'prompt h6 font-weight-normal sr-only';
}
// Check for 'Show standard instruction'.
$standardinstruction = \html_writer::tag('legend', get_string('answer'), [
'class' => 'prompt h6 font-weight-normal sr-only'
'class' => $standardinstructionclass,
]);
$this->assertStringContainsString($standardinstruction, $this->currentoutput);
}
Expand Down