first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
@@ -0,0 +1,57 @@
<?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/>.
/**
* Observer handling events.
*
* @package availability_grade
* @copyright 2014 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace availability_grade;
defined('MOODLE_INTERNAL') || die();
/**
* Callbacks handling grade changes (to clear cache).
*
* This ought to use the hooks system, but it doesn't exist - calls are
* hard-coded. (The new event system is not suitable for this type of use.)
*
* @package availability_grade
* @copyright 2014 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class callbacks {
/**
* A user grade has been updated in gradebook.
*
* @param int $userid User ID
*/
public static function grade_changed($userid) {
\cache::make('availability_grade', 'scores')->delete($userid);
}
/**
* A grade item has been updated in gradebook.
*
* @param int $courseid Course id
*/
public static function grade_item_changed($courseid) {
\cache::make('availability_grade', 'items')->delete($courseid);
}
}
@@ -0,0 +1,319 @@
<?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/>.
/**
* Condition on grades of current user.
*
* @package availability_grade
* @copyright 2014 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace availability_grade;
defined('MOODLE_INTERNAL') || die();
/**
* Condition on grades of current user.
*
* @package availability_grade
* @copyright 2014 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class condition extends \core_availability\condition {
/** @var int Grade item id */
private $gradeitemid;
/** @var float|null Min grade (must be >= this) or null if none */
private $min;
/** @var float|null Max grade (must be < this) or null if none */
private $max;
/**
* Constructor.
*
* @param \stdClass $structure Data structure from JSON decode
* @throws \coding_exception If invalid data structure.
*/
public function __construct($structure) {
// Get grade item id.
if (isset($structure->id) && is_int($structure->id)) {
$this->gradeitemid = $structure->id;
} else {
throw new \coding_exception('Missing or invalid ->id for grade condition');
}
// Get min and max.
if (!property_exists($structure, 'min')) {
$this->min = null;
} else if (is_float($structure->min) || is_int($structure->min)) {
$this->min = $structure->min;
} else {
throw new \coding_exception('Missing or invalid ->min for grade condition');
}
if (!property_exists($structure, 'max')) {
$this->max = null;
} else if (is_float($structure->max) || is_int($structure->max)) {
$this->max = $structure->max;
} else {
throw new \coding_exception('Missing or invalid ->max for grade condition');
}
}
public function save() {
$result = (object)array('type' => 'grade', 'id' => $this->gradeitemid);
if (!is_null($this->min)) {
$result->min = $this->min;
}
if (!is_null($this->max)) {
$result->max = $this->max;
}
return $result;
}
/**
* Returns a JSON object which corresponds to a condition of this type.
*
* Intended for unit testing, as normally the JSON values are constructed
* by JavaScript code.
*
* @param int $gradeitemid Grade item id
* @param number|null $min Min grade (or null if no min)
* @param number|null $max Max grade (or null if no max)
* @return stdClass Object representing condition
*/
public static function get_json($gradeitemid, $min = null, $max = null) {
$result = (object)array('type' => 'grade', 'id' => (int)$gradeitemid);
if (!is_null($min)) {
$result->min = $min;
}
if (!is_null($max)) {
$result->max = $max;
}
return $result;
}
public function is_available($not, \core_availability\info $info, $grabthelot, $userid) {
$course = $info->get_course();
$score = $this->get_cached_grade_score($this->gradeitemid, $course->id, $grabthelot, $userid);
$allow = $score !== false &&
(is_null($this->min) || $score >= $this->min) &&
(is_null($this->max) || $score < $this->max);
if ($not) {
$allow = !$allow;
}
return $allow;
}
public function get_description($full, $not, \core_availability\info $info) {
$course = $info->get_course();
// String depends on type of requirement. We are coy about
// the actual numbers, in case grades aren't released to
// students.
if (is_null($this->min) && is_null($this->max)) {
$string = 'any';
} else if (is_null($this->max)) {
$string = 'min';
} else if (is_null($this->min)) {
$string = 'max';
} else {
$string = 'range';
}
if ($not) {
// The specific strings don't make as much sense with 'not'.
if ($string === 'any') {
$string = 'notany';
} else {
$string = 'notgeneral';
}
}
// We cannot get the name at this point because it requires format_string which is not
// allowed here. Instead, get it later with the callback function below.
$name = $this->description_callback([$this->gradeitemid]);
return get_string('requires_' . $string, 'availability_grade', $name);
}
/**
* Gets the grade name at display time.
*
* @param \course_modinfo $modinfo Modinfo
* @param \context $context Context
* @param string[] $params Parameters (just grade item id)
* @return string Text value
*/
public static function get_description_callback_value(
\course_modinfo $modinfo, \context $context, array $params): string {
if (count($params) !== 1 || !is_number($params[0])) {
return '<!-- Invalid grade description callback -->';
}
$gradeitemid = (int)$params[0];
return self::get_cached_grade_name($modinfo->get_course_id(), $gradeitemid);
}
protected function get_debug_string() {
$out = '#' . $this->gradeitemid;
if (!is_null($this->min)) {
$out .= ' >= ' . sprintf('%.5f', $this->min);
}
if (!is_null($this->max)) {
if (!is_null($this->min)) {
$out .= ',';
}
$out .= ' < ' . sprintf('%.5f', $this->max);
}
return $out;
}
/**
* Obtains the name of a grade item, also checking that it exists. Uses a
* cache. The name returned is suitable for display.
*
* @param int $courseid Course id
* @param int $gradeitemid Grade item id
* @return string Grade name or empty string if no grade with that id
*/
private static function get_cached_grade_name($courseid, $gradeitemid) {
global $DB, $CFG;
require_once($CFG->libdir . '/gradelib.php');
// Get all grade item names from cache, or using db query.
$cache = \cache::make('availability_grade', 'items');
if (($cacheditems = $cache->get($courseid)) === false) {
// We cache the whole items table not the name; the format_string
// call for the name might depend on current user (e.g. multilang)
// and this is a shared cache.
$cacheditems = $DB->get_records('grade_items', array('courseid' => $courseid));
$cache->set($courseid, $cacheditems);
}
// Return name from cached item or a lang string.
if (!array_key_exists($gradeitemid, $cacheditems)) {
return get_string('missing', 'availability_grade');
}
$gradeitemobj = $cacheditems[$gradeitemid];
$item = new \grade_item;
\grade_object::set_properties($item, $gradeitemobj);
return $item->get_name();
}
/**
* Obtains a grade score. Note that this score should not be displayed to
* the user, because gradebook rules might prohibit that. It may be a
* non-final score subject to adjustment later.
*
* @param int $gradeitemid Grade item ID we're interested in
* @param int $courseid Course id
* @param bool $grabthelot If true, grabs all scores for current user on
* this course, so that later ones come from cache
* @param int $userid Set if requesting grade for a different user (does
* not use cache)
* @return float Grade score as a percentage in range 0-100 (e.g. 100.0
* or 37.21), or false if user does not have a grade yet
*/
protected static function get_cached_grade_score($gradeitemid, $courseid,
$grabthelot=false, $userid=0) {
global $USER, $DB;
if (!$userid) {
$userid = $USER->id;
}
$cache = \cache::make('availability_grade', 'scores');
if (($cachedgrades = $cache->get($userid)) === false) {
$cachedgrades = array();
}
if (!array_key_exists($gradeitemid, $cachedgrades)) {
if ($grabthelot) {
// Get all grades for the current course.
$rs = $DB->get_recordset_sql('
SELECT
gi.id,gg.finalgrade,gg.rawgrademin,gg.rawgrademax
FROM
{grade_items} gi
LEFT JOIN {grade_grades} gg ON gi.id=gg.itemid AND gg.userid=?
WHERE
gi.courseid = ?', array($userid, $courseid));
foreach ($rs as $record) {
// This function produces division by zero error warnings when rawgrademax and rawgrademin
// are equal. Below change does not affect function behavior, just avoids the warning.
if (is_null($record->finalgrade) || $record->rawgrademax == $record->rawgrademin) {
// No grade = false.
$cachedgrades[$record->id] = false;
} else {
// Otherwise convert grade to percentage.
$cachedgrades[$record->id] =
(($record->finalgrade - $record->rawgrademin) * 100) /
($record->rawgrademax - $record->rawgrademin);
}
}
$rs->close();
// And if it's still not set, well it doesn't exist (eg
// maybe the user set it as a condition, then deleted the
// grade item) so we call it false.
if (!array_key_exists($gradeitemid, $cachedgrades)) {
$cachedgrades[$gradeitemid] = false;
}
} else {
// Just get current grade.
$record = $DB->get_record('grade_grades', array(
'userid' => $userid, 'itemid' => $gradeitemid));
// This function produces division by zero error warnings when rawgrademax and rawgrademin
// are equal. Below change does not affect function behavior, just avoids the warning.
if ($record && !is_null($record->finalgrade) && $record->rawgrademax != $record->rawgrademin) {
$score = (($record->finalgrade - $record->rawgrademin) * 100) /
($record->rawgrademax - $record->rawgrademin);
} else {
// Treat the case where row exists but is null, same as
// case where row doesn't exist.
$score = false;
}
$cachedgrades[$gradeitemid] = $score;
}
$cache->set($userid, $cachedgrades);
}
return $cachedgrades[$gradeitemid];
}
public function update_after_restore($restoreid, $courseid, \base_logger $logger, $name) {
global $DB;
$rec = \restore_dbops::get_backup_ids_record($restoreid, 'grade_item', $this->gradeitemid);
if (!$rec || !$rec->newitemid) {
// If we are on the same course (e.g. duplicate) then we can just
// use the existing one.
if ($DB->record_exists('grade_items',
array('id' => $this->gradeitemid, 'courseid' => $courseid))) {
return false;
}
// Otherwise it's a warning.
$this->gradeitemid = 0;
$logger->process('Restored item (' . $name .
') has availability condition on grade that was not restored',
\backup::LOG_WARNING);
} else {
$this->gradeitemid = (int)$rec->newitemid;
}
return true;
}
public function update_dependency_id($table, $oldid, $newid) {
if ($table === 'grade_items' && (int)$this->gradeitemid === (int)$oldid) {
$this->gradeitemid = $newid;
return true;
} else {
return false;
}
}
}
@@ -0,0 +1,74 @@
<?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/>.
/**
* Front-end class.
*
* @package availability_grade
* @copyright 2014 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace availability_grade;
defined('MOODLE_INTERNAL') || die();
/**
* Front-end class.
*
* @package availability_grade
* @copyright 2014 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class frontend extends \core_availability\frontend {
protected function get_javascript_strings() {
return array('option_min', 'option_max', 'label_min', 'label_max');
}
protected function get_javascript_init_params($course, \cm_info $cm = null,
\section_info $section = null) {
global $DB, $CFG;
require_once($CFG->libdir . '/gradelib.php');
require_once($CFG->dirroot . '/course/lib.php');
// Get grades as basic associative array.
$gradeoptions = array();
$items = \grade_item::fetch_all(array('courseid' => $course->id));
// For some reason the fetch_all things return null if none.
$items = $items ? $items : array();
foreach ($items as $id => $item) {
// Don't include the grade item if it's linked with a module that is being deleted.
if (course_module_instance_pending_deletion($item->courseid, $item->itemmodule, $item->iteminstance)) {
continue;
}
// Do not include grades for current item.
if ($cm && $cm->instance == $item->iteminstance
&& $cm->modname == $item->itemmodule
&& $item->itemtype == 'mod') {
continue;
}
$gradeoptions[$id] = $item->get_name(true);
}
\core_collator::asort($gradeoptions);
// Change to JS array format and return.
$jsarray = array();
foreach ($gradeoptions as $id => $name) {
$jsarray[] = (object)array('id' => $id, 'name' => $name);
}
return array($jsarray);
}
}
@@ -0,0 +1,46 @@
<?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/>.
/**
* Privacy Subsystem implementation for availability_grade.
*
* @package availability_grade
* @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace availability_grade\privacy;
defined('MOODLE_INTERNAL') || die();
/**
* Privacy Subsystem for availability_grade implementing null_provider.
*
* @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements \core_privacy\local\metadata\null_provider {
/**
* Get the language string identifier with the component's language
* file to explain why this plugin stores no data.
*
* @return string
*/
public static function get_reason(): string {
return 'privacy:metadata';
}
}
@@ -0,0 +1,40 @@
<?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/>.
/**
* Cache definitions.
*
* @package availability_grade
* @copyright 2014 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$definitions = array(
// Used to cache user grades for conditional availability purposes.
'scores' => array(
'mode' => cache_store::MODE_APPLICATION,
'staticacceleration' => true,
'staticaccelerationsize' => 2, // Should not be required for more than one user at a time.
'ttl' => 3600,
),
// Used to cache course grade items for conditional availability purposes.
'items' => array(
'mode' => cache_store::MODE_APPLICATION,
'staticacceleration' => true,
'staticaccelerationsize' => 2, // Should not be required for more than one course at a time.
'ttl' => 3600,
),
);
@@ -0,0 +1,44 @@
<?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/>.
/**
* Language strings.
*
* @package availability_grade
* @copyright 2014 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$string['cachedef_items'] = 'Grade items cached for evaluating conditional availability';
$string['cachedef_scores'] = 'User grades cached for evaluating conditional availability';
$string['description'] = 'Require students to achieve a specified grade.';
$string['error_backwardrange'] = 'When specifying a grade range, the minimum must be lower than the maximum.';
$string['error_invalidnumber'] = 'Grade ranges must be specified with valid percentages.';
$string['error_selectgradeid'] = 'You must select a grade item for the grade condition.';
$string['label_min'] = 'Minimum grade percentage (inclusive)';
$string['label_max'] = 'Maximum grade percentage (exclusive)';
$string['option_min'] = 'must be &#x2265;';
$string['option_max'] = 'must be <';
$string['pluginname'] = 'Restriction by grades';
$string['requires_any'] = 'You have a grade in <strong>{$a}</strong>';
$string['requires_max'] = 'You achieve lower than a certain score in <strong>{$a}</strong>';
$string['requires_min'] = 'You achieve higher than a certain score in <strong>{$a}</strong>';
$string['requires_notany'] = 'You do not have a grade in <strong>{$a}</strong>';
$string['requires_notgeneral'] = 'You do not get certain scores in <strong>{$a}</strong>';
$string['requires_range'] = 'You achieve a score within a certain range in <strong>{$a}</strong>';
$string['missing'] = '(missing activity)';
$string['title'] = 'Grade';
$string['privacy:metadata'] = 'The Restriction by grades plugin does not store any personal data.';
@@ -0,0 +1,136 @@
@availability @availability_grade
Feature: availability_grade
In order to control student access to activities
As a teacher
I need to set date conditions which prevent student access
Background:
Given the following "courses" exist:
| fullname | shortname | format | enablecompletion |
| Course 1 | C1 | topics | 1 |
And the following "users" exist:
| username | email |
| teacher1 | t@example.com |
| student1 | s@example.com |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
| student1 | C1 | student |
# Add an assignment.
And the following "activities" exist:
| activity | course | name | assignsubmission_onlinetext_enabled |
| assign | C1 | A1 | 1 |
| page | C1 | P1 | |
| page | C1 | P2 | |
| page | C1 | P3 | |
| page | C1 | P4 | |
@javascript
Scenario: Test condition
Given I am on the "P2" "page activity editing" page logged in as "teacher1"
And I expand all fieldsets
And I click on "Add restriction..." "button"
And I click on "Grade" "button" in the "Add restriction..." "dialogue"
And I click on ".availability-item .availability-eye img" "css_element"
And I set the field "Grade" to "A1"
And I press "Save and return to course"
# Add a Page with a grade condition for 50%.
And I am on the "P3" "page activity editing" page
And I expand all fieldsets
And I click on "Add restriction..." "button"
And I click on "Grade" "button" in the "Add restriction..." "dialogue"
And I click on ".availability-item .availability-eye img" "css_element"
And I set the field "Grade" to "A1"
And I click on "min" "checkbox" in the ".availability-item" "css_element"
And I set the field "Minimum grade percentage (inclusive)" to "50"
And I click on "max" "checkbox" in the ".availability-item" "css_element"
And I set the field "Maximum grade percentage (exclusive)" to "80"
And I press "Save and return to course"
# Check if disabling a part of the restriction is get saved.
And I am on the "P3" "page activity editing" page
And I expand all fieldsets
And I click on "max" "checkbox" in the ".availability-item" "css_element"
And I press "Save and return to course"
And I am on the "P3" "page activity editing" page
And the field "Maximum grade percentage (exclusive)" matches value ""
And I am on "Course 1" course homepage
# Add a Page with a grade condition for 10%.
And I am on the "P4" "page activity editing" page
And I expand all fieldsets
And I click on "Add restriction..." "button"
And I click on "Grade" "button" in the "Add restriction..." "dialogue"
And I click on ".availability-item .availability-eye img" "css_element"
And I set the field "Grade" to "A1"
And I click on "min" "checkbox" in the ".availability-item" "css_element"
And I set the field "Minimum grade percentage (inclusive)" to "10"
And I press "Save and return to course"
# Log in as student without a grade yet.
When I am on the "A1" "assign activity" page logged in as student1
# Do the assignment.
And I click on "Add submission" "button"
And I set the field "Online text" to "Q"
And I click on "Save changes" "button"
And I am on "Course 1" course homepage
# None of the pages should appear (check assignment though).
Then I should not see "P2" in the "region-main" "region"
And I should not see "P3" in the "region-main" "region"
And I should not see "P4" in the "region-main" "region"
And I should see "A1" in the "region-main" "region"
# Log back in as teacher.
When I am on the "A1" "assign activity" page logged in as teacher1
# Give the assignment 40%.
And I follow "View all submissions"
# Pick the grade link in the row that has s@example.com in it.
And I click on "Grade" "link" in the "s@example.com" "table_row"
And I set the field "Grade out of 100" to "40"
And I click on "Save changes" "button"
And I click on "Edit settings" "link"
And I log out
# Log back in as student.
And I am on the "Course 1" course page logged in as student1
# Check pages are visible.
Then I should see "P2" in the "region-main" "region"
And I should see "P4" in the "region-main" "region"
And I should not see "P3" in the "region-main" "region"
@javascript
Scenario: Condition display with filters
# Teacher sets up a restriction on group G1, using multilang filter.
Given the following "activity" exists:
| activity | assign |
| name | <span lang="en" class="multilang">A-One</span><span lang="fr" class="multilang">A-Un</span> |
| intro | Test |
| course | C1 |
| idnumber | 0001 |
| section | 1 |
And the "multilang" filter is "on"
And the "multilang" filter applies to "content and headings"
# The activity names filter is enabled because it triggered a bug in older versions.
And the "activitynames" filter is "on"
And the "activitynames" filter applies to "content and headings"
And I am on the "C1" "Course" page logged in as "teacher1"
And I turn editing mode on
And I am on the "P1" "page activity editing" page
And I expand all fieldsets
And I click on "Add restriction..." "button"
And I click on "Grade" "button" in the "Add restriction..." "dialogue"
And I set the field "Grade" to "A-One"
And I click on "min" "checkbox" in the ".availability-item" "css_element"
And I set the field "Minimum grade percentage (inclusive)" to "10"
And I press "Save and return to course"
And I log out
# Student sees information about no access to group, with group name in correct language.
When I am on the "C1" "Course" page logged in as "student1"
Then I should see "Not available unless: You achieve higher than a certain score in A-One"
And I should not see "A-Un"
@@ -0,0 +1,246 @@
<?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/>.
namespace availability_grade;
/**
* Unit tests for the grade condition.
*
* @package availability_grade
* @copyright 2014 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class condition_test extends \advanced_testcase {
/**
* Tests constructing and using grade condition.
*/
public function test_usage(): void {
global $USER, $CFG;
require_once($CFG->dirroot . '/mod/assign/locallib.php');
$this->resetAfterTest();
$CFG->enableavailability = true;
// Make a test course and user.
$course = $this->getDataGenerator()->create_course();
$user = $this->getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($user->id, $course->id);
// Make assign module.
$assignrow = $this->getDataGenerator()->create_module('assign', array(
'course' => $course->id, 'name' => 'Test!'));
$assign = new \assign(\context_module::instance($assignrow->cmid), false, false);
$modinfo = get_fast_modinfo($course);
$cm = $modinfo->get_cm($assignrow->cmid);
$info = new \core_availability\info_module($cm);
// Get the actual grade item.
$item = $assign->get_grade_item();
// Construct tree with grade condition (any grade, specified item).
$structure = (object)array('type' => 'grade', 'id' => (int)$item->id);
$cond = new condition($structure);
// Check if available (not available).
$this->assertFalse($cond->is_available(false, $info, true, $user->id));
$information = $cond->get_description(false, false, $info);
$information = \core_availability\info::format_info($information, $course);
$this->assertMatchesRegularExpression('~have a grade.*Test!~', $information);
$this->assertTrue($cond->is_available(true, $info, true, $user->id));
// Add grade and check available.
self::set_grade($assignrow, $user->id, 37.2);
$this->assertTrue($cond->is_available(false, $info, true, $user->id));
$this->assertFalse($cond->is_available(true, $info, true, $user->id));
$information = $cond->get_description(false, true, $info);
$information = \core_availability\info::format_info($information, $course);
$this->assertMatchesRegularExpression('~do not have a grade.*Test!~', $information);
// Construct directly and test remaining conditions; first, min grade (fail).
self::set_grade($assignrow, $user->id, 29.99999);
$structure->min = 30.0;
$cond = new condition($structure);
$this->assertFalse($cond->is_available(false, $info, true, $user->id));
$information = $cond->get_description(false, false, $info);
$information = \core_availability\info::format_info($information, $course);
$this->assertMatchesRegularExpression('~achieve higher than.*Test!~', $information);
$this->assertTrue($cond->is_available(true, $info, true, $user->id));
// Min grade (success).
self::set_grade($assignrow, $user->id, 30);
$this->assertTrue($cond->is_available(false, $info, true, $user->id));
$this->assertFalse($cond->is_available(true, $info, true, $user->id));
$information = $cond->get_description(false, true, $info);
$information = \core_availability\info::format_info($information, $course);
$this->assertMatchesRegularExpression('~do not get certain scores.*Test!~', $information);
// Max grade (fail).
unset($structure->min);
$structure->max = 30.0;
$cond = new condition($structure);
$this->assertFalse($cond->is_available(false, $info, true, $user->id));
$information = $cond->get_description(false, false, $info);
$information = \core_availability\info::format_info($information, $course);
$this->assertMatchesRegularExpression('~achieve lower than a certain score in.*Test!~', $information);
$this->assertTrue($cond->is_available(true, $info, true, $user->id));
// Max grade (success).
self::set_grade($assignrow, $user->id, 29.99999);
$this->assertTrue($cond->is_available(false, $info, true, $user->id));
$this->assertFalse($cond->is_available(true, $info, true, $user->id));
$information = $cond->get_description(false, true, $info);
$information = \core_availability\info::format_info($information, $course);
$this->assertMatchesRegularExpression('~do not get certain scores.*Test!~', $information);
// Max and min (fail).
$structure->min = 30.0;
$structure->max = 34.12345;
$cond = new condition($structure);
$this->assertFalse($cond->is_available(false, $info, true, $user->id));
$information = $cond->get_description(false, false, $info);
$information = \core_availability\info::format_info($information, $course);
$this->assertMatchesRegularExpression('~achieve a score within a certain range.*Test!~', $information);
$this->assertTrue($cond->is_available(true, $info, true, $user->id));
// Still fail (other end).
self::set_grade($assignrow, $user->id, 34.12345);
$this->assertFalse($cond->is_available(false, $info, true, $user->id));
// Success (top end).
self::set_grade($assignrow, $user->id, 34.12344);
$this->assertTrue($cond->is_available(false, $info, true, $user->id));
$this->assertFalse($cond->is_available(true, $info, true, $user->id));
$information = $cond->get_description(false, true, $info);
$information = \core_availability\info::format_info($information, $course);
$this->assertMatchesRegularExpression('~do not get certain scores.*Test!~', $information);
// Success (bottom end).
self::set_grade($assignrow, $user->id, 30.0);
$this->assertTrue($cond->is_available(false, $info, true, $user->id));
$this->assertFalse($cond->is_available(true, $info, true, $user->id));
$information = $cond->get_description(false, true, $info);
$information = \core_availability\info::format_info($information, $course);
$this->assertMatchesRegularExpression('~do not get certain scores.*Test!~', $information);
}
/**
* Tests the constructor including error conditions. Also tests the
* string conversion feature (intended for debugging only).
*/
public function test_constructor(): void {
// No parameters.
$structure = new \stdClass();
try {
$cond = new condition($structure);
$this->fail();
} catch (\coding_exception $e) {
$this->assertStringContainsString('Missing or invalid ->id', $e->getMessage());
}
// Invalid id (not int).
$structure->id = 'bourne';
try {
$cond = new condition($structure);
$this->fail();
} catch (\coding_exception $e) {
$this->assertStringContainsString('Missing or invalid ->id', $e->getMessage());
}
// Invalid min (not number).
$structure->id = 42;
$structure->min = 'ute';
try {
$cond = new condition($structure);
$this->fail();
} catch (\coding_exception $e) {
$this->assertStringContainsString('Missing or invalid ->min', $e->getMessage());
}
// Invalid max (not number).
$structure->min = 3.89;
$structure->max = '9000';
try {
$cond = new condition($structure);
$this->fail();
} catch (\coding_exception $e) {
$this->assertStringContainsString('Missing or invalid ->max', $e->getMessage());
}
// All valid.
$structure->max = 4.0;
$cond = new condition($structure);
$this->assertEquals('{grade:#42 >= 3.89000, < 4.00000}', (string)$cond);
// No max.
unset($structure->max);
$cond = new condition($structure);
$this->assertEquals('{grade:#42 >= 3.89000}', (string)$cond);
// No min.
unset($structure->min);
$structure->max = 32.768;
$cond = new condition($structure);
$this->assertEquals('{grade:#42 < 32.76800}', (string)$cond);
// No nothing (only requires that grade exists).
unset($structure->max);
$cond = new condition($structure);
$this->assertEquals('{grade:#42}', (string)$cond);
}
/**
* Tests the save() function.
*/
public function test_save(): void {
$structure = (object)array('id' => 19);
$cond = new condition($structure);
$structure->type = 'grade';
$this->assertEquals($structure, $cond->save());
$structure = (object)array('id' => 19, 'min' => 4.12345, 'max' => 90);
$cond = new condition($structure);
$structure->type = 'grade';
$this->assertEquals($structure, $cond->save());
}
/**
* Updates the grade of a user in the given assign module instance.
*
* @param \stdClass $assignrow Assignment row from database
* @param int $userid User id
* @param float $grade Grade
*/
protected static function set_grade($assignrow, $userid, $grade) {
$grades = array();
$grades[$userid] = (object)array(
'rawgrade' => $grade, 'userid' => $userid);
$assignrow->cmidnumber = null;
assign_grade_item_update($assignrow, $grades);
}
/**
* Tests the update_dependency_id() function.
*/
public function test_update_dependency_id(): void {
$cond = new condition((object)array('id' => 123));
$this->assertFalse($cond->update_dependency_id('frogs', 123, 456));
$this->assertFalse($cond->update_dependency_id('grade_items', 12, 34));
$this->assertTrue($cond->update_dependency_id('grade_items', 123, 456));
$after = $cond->save();
$this->assertEquals(456, $after->id);
}
}
+29
View File
@@ -0,0 +1,29 @@
<?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/>.
/**
* Version info.
*
* @package availability_grade
* @copyright 2014 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2024042200;
$plugin->requires = 2024041600;
$plugin->component = 'availability_grade';
@@ -0,0 +1,167 @@
YUI.add('moodle-availability_grade-form', function (Y, NAME) {
/**
* JavaScript for form editing grade conditions.
*
* @module moodle-availability_grade-form
*/
M.availability_grade = M.availability_grade || {};
/**
* @class M.availability_grade.form
* @extends M.core_availability.plugin
*/
M.availability_grade.form = Y.Object(M.core_availability.plugin);
/**
* Grade items available for selection.
*
* @property grades
* @type Array
*/
M.availability_grade.form.grades = null;
/**
* Initialises this plugin.
*
* @method initInner
* @param {Array} grades Array of objects containing gradeid => name
*/
M.availability_grade.form.initInner = function(grades) {
this.grades = grades;
this.nodesSoFar = 0;
};
M.availability_grade.form.getNode = function(json) {
// Increment number used for unique ids.
this.nodesSoFar++;
// Create HTML structure.
var html = '<label class="mb-3"><span class="pr-3">' + M.util.get_string('title', 'availability_grade') + '</span> ' +
'<span class="availability-group">' +
'<select name="id" class="custom-select"><option value="0">' + M.util.get_string('choosedots', 'moodle') + '</option>';
for (var i = 0; i < this.grades.length; i++) {
var grade = this.grades[i];
// String has already been escaped using format_string.
html += '<option value="' + grade.id + '">' + grade.name + '</option>';
}
html += '</select></span></label> <br><span class="availability-group mb-3">' +
'<label><input type="checkbox" class="form-check-input position-static mt-0 mx-1" name="min"/>' +
M.util.get_string('option_min', 'availability_grade') +
'</label> <label><span class="accesshide">' + M.util.get_string('label_min', 'availability_grade') +
'</span><input type="text" class="form-control mx-1" name="minval" title="' +
M.util.get_string('label_min', 'availability_grade') + '"/></label>%</span><br>' +
'<span class="availability-group mb-3">' +
'<label><input type="checkbox" class="form-check-input position-static mt-0 mx-1" name="max"/>' +
M.util.get_string('option_max', 'availability_grade') +
'</label> <label><span class="accesshide">' + M.util.get_string('label_max', 'availability_grade') +
'</span><input type="text" class="form-control mx-1" name="maxval" title="' +
M.util.get_string('label_max', 'availability_grade') + '"/></label>%</span>';
var node = Y.Node.create('<div class="d-inline-block d-flex flex-wrap align-items-center">' + html + '</div>');
// Set initial values.
if (json.id !== undefined &&
node.one('select[name=id] > option[value=' + json.id + ']')) {
node.one('select[name=id]').set('value', '' + json.id);
}
if (json.min !== undefined) {
node.one('input[name=min]').set('checked', true);
node.one('input[name=minval]').set('value', json.min);
}
if (json.max !== undefined) {
node.one('input[name=max]').set('checked', true);
node.one('input[name=maxval]').set('value', json.max);
}
// Disables/enables text input fields depending on checkbox.
var updateCheckbox = function(check, focus) {
var input = check.ancestor('label').next('label').one('input');
var checked = check.get('checked');
input.set('disabled', !checked);
if (focus && checked) {
input.focus();
}
return checked;
};
node.all('input[type=checkbox]').each(updateCheckbox);
// Add event handlers (first time only).
if (!M.availability_grade.form.addedEvents) {
M.availability_grade.form.addedEvents = true;
var root = Y.one('.availability-field');
root.delegate('change', function() {
// For the grade item, just update the form fields.
M.core_availability.form.update();
}, '.availability_grade select[name=id]');
root.delegate('click', function() {
updateCheckbox(this, true);
M.core_availability.form.update();
}, '.availability_grade input[type=checkbox]');
root.delegate('valuechange', function() {
// For grade values, just update the form fields.
M.core_availability.form.update();
}, '.availability_grade input[type=text]');
}
return node;
};
M.availability_grade.form.fillValue = function(value, node) {
value.id = parseInt(node.one('select[name=id]').get('value'), 10);
if (node.one('input[name=min]').get('checked')) {
value.min = this.getValue('minval', node);
}
if (node.one('input[name=max]').get('checked')) {
value.max = this.getValue('maxval', node);
}
};
/**
* Gets the numeric value of an input field. Supports decimal points (using
* dot or comma).
*
* @method getValue
* @return {Number|String} Value of field as number or string if not valid
*/
M.availability_grade.form.getValue = function(field, node) {
// Get field value.
var value = node.one('input[name=' + field + ']').get('value');
// If it is not a valid positive number, return false.
if (!(/^[0-9]+([.,][0-9]+)?$/.test(value))) {
return value;
}
// Replace comma with dot and parse as floating-point.
var result = parseFloat(value.replace(',', '.'));
if (result < 0 || result > 100) {
return value;
} else {
return result;
}
};
M.availability_grade.form.fillErrors = function(errors, node) {
var value = {};
this.fillValue(value, node);
// Check grade item id.
if (value.id === 0) {
errors.push('availability_grade:error_selectgradeid');
}
// Check numeric values.
if ((value.min !== undefined && typeof (value.min) === 'string') ||
(value.max !== undefined && typeof (value.max) === 'string')) {
errors.push('availability_grade:error_invalidnumber');
} else if (value.min !== undefined && value.max !== undefined &&
value.min >= value.max) {
errors.push('availability_grade:error_backwardrange');
}
};
}, '@VERSION@', {"requires": ["base", "node", "event", "moodle-core_availability-form"]});
@@ -0,0 +1 @@
YUI.add("moodle-availability_grade-form",function(r,a){M.availability_grade=M.availability_grade||{},M.availability_grade.form=r.Object(M.core_availability.plugin),M.availability_grade.form.grades=null,M.availability_grade.form.initInner=function(a){this.grades=a,this.nodesSoFar=0},M.availability_grade.form.getNode=function(a){var e,i,l,t,n;for(this.nodesSoFar++,e='<label class="mb-3"><span class="pr-3">'+M.util.get_string("title","availability_grade")+'</span> <span class="availability-group"><select name="id" class="custom-select"><option value="0">'+M.util.get_string("choosedots","moodle")+"</option>",i=0;i<this.grades.length;i++)e+='<option value="'+(l=this.grades[i]).id+'">'+l.name+"</option>";return e+='</select></span></label> <br><span class="availability-group mb-3"><label><input type="checkbox" class="form-check-input position-static mt-0 mx-1" name="min"/>'+M.util.get_string("option_min","availability_grade")+'</label> <label><span class="accesshide">'+M.util.get_string("label_min","availability_grade")+'</span><input type="text" class="form-control mx-1" name="minval" title="'+M.util.get_string("label_min","availability_grade")+'"/></label>%</span><br><span class="availability-group mb-3"><label><input type="checkbox" class="form-check-input position-static mt-0 mx-1" name="max"/>'+M.util.get_string("option_max","availability_grade")+'</label> <label><span class="accesshide">'+M.util.get_string("label_max","availability_grade")+'</span><input type="text" class="form-control mx-1" name="maxval" title="'+M.util.get_string("label_max","availability_grade")+'"/></label>%</span>',t=r.Node.create('<div class="d-inline-block d-flex flex-wrap align-items-center">'+e+"</div>"),a.id!==undefined&&t.one("select[name=id] > option[value="+a.id+"]")&&t.one("select[name=id]").set("value",""+a.id),a.min!==undefined&&(t.one("input[name=min]").set("checked",!0),t.one("input[name=minval]").set("value",a.min)),a.max!==undefined&&(t.one("input[name=max]").set("checked",!0),t.one("input[name=maxval]").set("value",a.max)),n=function(a,e){var i=a.ancestor("label").next("label").one("input"),a=a.get("checked");return i.set("disabled",!a),e&&a&&i.focus(),a},t.all("input[type=checkbox]").each(n),M.availability_grade.form.addedEvents||(M.availability_grade.form.addedEvents=!0,(a=r.one(".availability-field")).delegate("change",function(){M.core_availability.form.update()},".availability_grade select[name=id]"),a.delegate("click",function(){n(this,!0),M.core_availability.form.update()},".availability_grade input[type=checkbox]"),a.delegate("valuechange",function(){M.core_availability.form.update()},".availability_grade input[type=text]")),t},M.availability_grade.form.fillValue=function(a,e){a.id=parseInt(e.one("select[name=id]").get("value"),10),e.one("input[name=min]").get("checked")&&(a.min=this.getValue("minval",e)),e.one("input[name=max]").get("checked")&&(a.max=this.getValue("maxval",e))},M.availability_grade.form.getValue=function(a,e){a=e.one("input[name="+a+"]").get("value");return!/^[0-9]+([.,][0-9]+)?$/.test(a)||(e=parseFloat(a.replace(",",".")))<0||100<e?a:e},M.availability_grade.form.fillErrors=function(a,e){var i={};this.fillValue(i,e),0===i.id&&a.push("availability_grade:error_selectgradeid"),i.min!==undefined&&"string"==typeof i.min||i.max!==undefined&&"string"==typeof i.max?a.push("availability_grade:error_invalidnumber"):i.min!==undefined&&i.max!==undefined&&i.max<=i.min&&a.push("availability_grade:error_backwardrange")}},"@VERSION@",{requires:["base","node","event","moodle-core_availability-form"]});
@@ -0,0 +1,167 @@
YUI.add('moodle-availability_grade-form', function (Y, NAME) {
/**
* JavaScript for form editing grade conditions.
*
* @module moodle-availability_grade-form
*/
M.availability_grade = M.availability_grade || {};
/**
* @class M.availability_grade.form
* @extends M.core_availability.plugin
*/
M.availability_grade.form = Y.Object(M.core_availability.plugin);
/**
* Grade items available for selection.
*
* @property grades
* @type Array
*/
M.availability_grade.form.grades = null;
/**
* Initialises this plugin.
*
* @method initInner
* @param {Array} grades Array of objects containing gradeid => name
*/
M.availability_grade.form.initInner = function(grades) {
this.grades = grades;
this.nodesSoFar = 0;
};
M.availability_grade.form.getNode = function(json) {
// Increment number used for unique ids.
this.nodesSoFar++;
// Create HTML structure.
var html = '<label class="mb-3"><span class="pr-3">' + M.util.get_string('title', 'availability_grade') + '</span> ' +
'<span class="availability-group">' +
'<select name="id" class="custom-select"><option value="0">' + M.util.get_string('choosedots', 'moodle') + '</option>';
for (var i = 0; i < this.grades.length; i++) {
var grade = this.grades[i];
// String has already been escaped using format_string.
html += '<option value="' + grade.id + '">' + grade.name + '</option>';
}
html += '</select></span></label> <br><span class="availability-group mb-3">' +
'<label><input type="checkbox" class="form-check-input position-static mt-0 mx-1" name="min"/>' +
M.util.get_string('option_min', 'availability_grade') +
'</label> <label><span class="accesshide">' + M.util.get_string('label_min', 'availability_grade') +
'</span><input type="text" class="form-control mx-1" name="minval" title="' +
M.util.get_string('label_min', 'availability_grade') + '"/></label>%</span><br>' +
'<span class="availability-group mb-3">' +
'<label><input type="checkbox" class="form-check-input position-static mt-0 mx-1" name="max"/>' +
M.util.get_string('option_max', 'availability_grade') +
'</label> <label><span class="accesshide">' + M.util.get_string('label_max', 'availability_grade') +
'</span><input type="text" class="form-control mx-1" name="maxval" title="' +
M.util.get_string('label_max', 'availability_grade') + '"/></label>%</span>';
var node = Y.Node.create('<div class="d-inline-block d-flex flex-wrap align-items-center">' + html + '</div>');
// Set initial values.
if (json.id !== undefined &&
node.one('select[name=id] > option[value=' + json.id + ']')) {
node.one('select[name=id]').set('value', '' + json.id);
}
if (json.min !== undefined) {
node.one('input[name=min]').set('checked', true);
node.one('input[name=minval]').set('value', json.min);
}
if (json.max !== undefined) {
node.one('input[name=max]').set('checked', true);
node.one('input[name=maxval]').set('value', json.max);
}
// Disables/enables text input fields depending on checkbox.
var updateCheckbox = function(check, focus) {
var input = check.ancestor('label').next('label').one('input');
var checked = check.get('checked');
input.set('disabled', !checked);
if (focus && checked) {
input.focus();
}
return checked;
};
node.all('input[type=checkbox]').each(updateCheckbox);
// Add event handlers (first time only).
if (!M.availability_grade.form.addedEvents) {
M.availability_grade.form.addedEvents = true;
var root = Y.one('.availability-field');
root.delegate('change', function() {
// For the grade item, just update the form fields.
M.core_availability.form.update();
}, '.availability_grade select[name=id]');
root.delegate('click', function() {
updateCheckbox(this, true);
M.core_availability.form.update();
}, '.availability_grade input[type=checkbox]');
root.delegate('valuechange', function() {
// For grade values, just update the form fields.
M.core_availability.form.update();
}, '.availability_grade input[type=text]');
}
return node;
};
M.availability_grade.form.fillValue = function(value, node) {
value.id = parseInt(node.one('select[name=id]').get('value'), 10);
if (node.one('input[name=min]').get('checked')) {
value.min = this.getValue('minval', node);
}
if (node.one('input[name=max]').get('checked')) {
value.max = this.getValue('maxval', node);
}
};
/**
* Gets the numeric value of an input field. Supports decimal points (using
* dot or comma).
*
* @method getValue
* @return {Number|String} Value of field as number or string if not valid
*/
M.availability_grade.form.getValue = function(field, node) {
// Get field value.
var value = node.one('input[name=' + field + ']').get('value');
// If it is not a valid positive number, return false.
if (!(/^[0-9]+([.,][0-9]+)?$/.test(value))) {
return value;
}
// Replace comma with dot and parse as floating-point.
var result = parseFloat(value.replace(',', '.'));
if (result < 0 || result > 100) {
return value;
} else {
return result;
}
};
M.availability_grade.form.fillErrors = function(errors, node) {
var value = {};
this.fillValue(value, node);
// Check grade item id.
if (value.id === 0) {
errors.push('availability_grade:error_selectgradeid');
}
// Check numeric values.
if ((value.min !== undefined && typeof (value.min) === 'string') ||
(value.max !== undefined && typeof (value.max) === 'string')) {
errors.push('availability_grade:error_invalidnumber');
} else if (value.min !== undefined && value.max !== undefined &&
value.min >= value.max) {
errors.push('availability_grade:error_backwardrange');
}
};
}, '@VERSION@', {"requires": ["base", "node", "event", "moodle-core_availability-form"]});
@@ -0,0 +1,10 @@
{
"name": "moodle-availability_grade-form",
"builds": {
"moodle-availability_grade-form": {
"jsfiles": [
"form.js"
]
}
}
}
+162
View File
@@ -0,0 +1,162 @@
/**
* JavaScript for form editing grade conditions.
*
* @module moodle-availability_grade-form
*/
M.availability_grade = M.availability_grade || {};
/**
* @class M.availability_grade.form
* @extends M.core_availability.plugin
*/
M.availability_grade.form = Y.Object(M.core_availability.plugin);
/**
* Grade items available for selection.
*
* @property grades
* @type Array
*/
M.availability_grade.form.grades = null;
/**
* Initialises this plugin.
*
* @method initInner
* @param {Array} grades Array of objects containing gradeid => name
*/
M.availability_grade.form.initInner = function(grades) {
this.grades = grades;
this.nodesSoFar = 0;
};
M.availability_grade.form.getNode = function(json) {
// Increment number used for unique ids.
this.nodesSoFar++;
// Create HTML structure.
var html = '<label class="mb-3"><span class="pr-3">' + M.util.get_string('title', 'availability_grade') + '</span> ' +
'<span class="availability-group">' +
'<select name="id" class="custom-select"><option value="0">' + M.util.get_string('choosedots', 'moodle') + '</option>';
for (var i = 0; i < this.grades.length; i++) {
var grade = this.grades[i];
// String has already been escaped using format_string.
html += '<option value="' + grade.id + '">' + grade.name + '</option>';
}
html += '</select></span></label> <br><span class="availability-group mb-3">' +
'<label><input type="checkbox" class="form-check-input position-static mt-0 mx-1" name="min"/>' +
M.util.get_string('option_min', 'availability_grade') +
'</label> <label><span class="accesshide">' + M.util.get_string('label_min', 'availability_grade') +
'</span><input type="text" class="form-control mx-1" name="minval" title="' +
M.util.get_string('label_min', 'availability_grade') + '"/></label>%</span><br>' +
'<span class="availability-group mb-3">' +
'<label><input type="checkbox" class="form-check-input position-static mt-0 mx-1" name="max"/>' +
M.util.get_string('option_max', 'availability_grade') +
'</label> <label><span class="accesshide">' + M.util.get_string('label_max', 'availability_grade') +
'</span><input type="text" class="form-control mx-1" name="maxval" title="' +
M.util.get_string('label_max', 'availability_grade') + '"/></label>%</span>';
var node = Y.Node.create('<div class="d-inline-block d-flex flex-wrap align-items-center">' + html + '</div>');
// Set initial values.
if (json.id !== undefined &&
node.one('select[name=id] > option[value=' + json.id + ']')) {
node.one('select[name=id]').set('value', '' + json.id);
}
if (json.min !== undefined) {
node.one('input[name=min]').set('checked', true);
node.one('input[name=minval]').set('value', json.min);
}
if (json.max !== undefined) {
node.one('input[name=max]').set('checked', true);
node.one('input[name=maxval]').set('value', json.max);
}
// Disables/enables text input fields depending on checkbox.
var updateCheckbox = function(check, focus) {
var input = check.ancestor('label').next('label').one('input');
var checked = check.get('checked');
input.set('disabled', !checked);
if (focus && checked) {
input.focus();
}
return checked;
};
node.all('input[type=checkbox]').each(updateCheckbox);
// Add event handlers (first time only).
if (!M.availability_grade.form.addedEvents) {
M.availability_grade.form.addedEvents = true;
var root = Y.one('.availability-field');
root.delegate('change', function() {
// For the grade item, just update the form fields.
M.core_availability.form.update();
}, '.availability_grade select[name=id]');
root.delegate('click', function() {
updateCheckbox(this, true);
M.core_availability.form.update();
}, '.availability_grade input[type=checkbox]');
root.delegate('valuechange', function() {
// For grade values, just update the form fields.
M.core_availability.form.update();
}, '.availability_grade input[type=text]');
}
return node;
};
M.availability_grade.form.fillValue = function(value, node) {
value.id = parseInt(node.one('select[name=id]').get('value'), 10);
if (node.one('input[name=min]').get('checked')) {
value.min = this.getValue('minval', node);
}
if (node.one('input[name=max]').get('checked')) {
value.max = this.getValue('maxval', node);
}
};
/**
* Gets the numeric value of an input field. Supports decimal points (using
* dot or comma).
*
* @method getValue
* @return {Number|String} Value of field as number or string if not valid
*/
M.availability_grade.form.getValue = function(field, node) {
// Get field value.
var value = node.one('input[name=' + field + ']').get('value');
// If it is not a valid positive number, return false.
if (!(/^[0-9]+([.,][0-9]+)?$/.test(value))) {
return value;
}
// Replace comma with dot and parse as floating-point.
var result = parseFloat(value.replace(',', '.'));
if (result < 0 || result > 100) {
return value;
} else {
return result;
}
};
M.availability_grade.form.fillErrors = function(errors, node) {
var value = {};
this.fillValue(value, node);
// Check grade item id.
if (value.id === 0) {
errors.push('availability_grade:error_selectgradeid');
}
// Check numeric values.
if ((value.min !== undefined && typeof (value.min) === 'string') ||
(value.max !== undefined && typeof (value.max) === 'string')) {
errors.push('availability_grade:error_invalidnumber');
} else if (value.min !== undefined && value.max !== undefined &&
value.min >= value.max) {
errors.push('availability_grade:error_backwardrange');
}
};
@@ -0,0 +1,10 @@
{
"moodle-availability_grade-form": {
"requires": [
"base",
"node",
"event",
"moodle-core_availability-form"
]
}
}