Skip to content

Commit

Permalink
added all grade API functions
Browse files Browse the repository at this point in the history
* added reset gradebook function
* added delete grade item
* bigbluebuttonbn DB table now contains grade value
* fixed failing tests
  • Loading branch information
ssj365 committed Jan 13, 2025
1 parent 4fab461 commit 4547472
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 13 deletions.
3 changes: 2 additions & 1 deletion mod/bigbluebuttonbn/db/install.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="mod/bigbluebuttonbn/db" VERSION="20240719" COMMENT="XMLDB file for Moodle mod/bigbluebuttonbn"
<XMLDB PATH="mod/bigbluebuttonbn/db" VERSION="20250110" COMMENT="XMLDB file for Moodle mod/bigbluebuttonbn"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
>
Expand Down Expand Up @@ -51,6 +51,7 @@
<FIELD NAME="guestlinkuid" TYPE="char" LENGTH="1024" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="guestpassword" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="showpresentation" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="1" SEQUENCE="false"/>
<FIELD NAME="grade" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
Expand Down
13 changes: 13 additions & 0 deletions mod/bigbluebuttonbn/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@ function xmldb_bigbluebuttonbn_upgrade($oldversion = 0) {
// Automatically generated Moodle v4.5.0 release upgrade line.
// Put any upgrade step following this.

if ($oldversion < 2025011000) {
// Define field grade to be added to bigbluebuttonbn.
$table = new xmldb_table('bigbluebuttonbn');
$field = new xmldb_field('grade', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'showpresentation');

// Conditionally launch add field grade.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

upgrade_mod_savepoint(true, 2025011000, 'bigbluebuttonbn');
}

return true;
}

Expand Down
118 changes: 110 additions & 8 deletions mod/bigbluebuttonbn/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ function bigbluebuttonbn_add_instance($bigbluebuttonbn) {
// Call any active subplugin so to signal a new creation.
extension::add_instance($bigbluebuttonbn);

// Create new grade item.
bigbluebuttonbn_grade_item_update($bigbluebuttonbn);

return $bigbluebuttonbn->id;
Expand Down Expand Up @@ -206,6 +207,10 @@ function($gp) {

$result = true;

// Delete grades.
$bigbluebuttonbn = $DB->get_record('bigbluebuttonbn', ['id' => $id]);
bigbluebuttonbn_grade_item_delete($bigbluebuttonbn);

// Call any active subplugin so to signal deletion.
extension::delete_instance($id);

Expand Down Expand Up @@ -353,6 +358,10 @@ function bigbluebuttonbn_reset_userdata(stdClass $data) {
unset($items['logs']);
$status[] = reset::reset_getstatus('logs');
}
// Remove all grades from gradebook.
if (empty($data->reset_gradebook_grades)) {
bigbluebuttonbn_reset_gradebook($data->courseid, 'reset');
}
return $status;
}

Expand Down Expand Up @@ -776,8 +785,7 @@ function bigbluebuttonbn_grade_item_update(stdclass $bigbluebuttonbn, $grades=NU
if (!function_exists('grade_update')) { //workaround for buggy PHP versions
require_once($CFG->libdir.'/gradelib.php');
}

// Hook for extensions. We allow extensions to update the grade item. This takes precedence over the default behaviour.
// Hook for extensions. We allow extension to update the grade item. This takes precedence over the default behaviour.
$extensions = extension::gradebook_addons_instances($bigbluebuttonbn);
foreach ($extensions as $extension) {
$grade_item = $extension->grade_item_update($grades);
Expand All @@ -786,17 +794,111 @@ function bigbluebuttonbn_grade_item_update(stdclass $bigbluebuttonbn, $grades=NU
return $grade_item;
}
}

// Since the grade item is not updated by any extension, we update it here.
$params = array('itemname' => $bigbluebuttonbn->name);
if ($bigbluebuttonbn->grade > 0) {
if ($bigbluebuttonbn->grade == 0) {
$params['gradetype'] = GRADE_TYPE_NONE;
} else if ($bigbluebuttonbn->grade > 0) {
$params['gradetype'] = GRADE_TYPE_VALUE;
$params['grademax'] = $bigbluebuttonbn->grade;
$params['grademin'] = 0;
$params['grademax'] = $bigbluebuttonbn->grade;
$params['grademin'] = 0;
} else if ($bigbluebuttonbn->grade < 0) {
$params['gradetype'] = GRADE_TYPE_SCALE;
$params['scaleid'] = -$bigbluebuttonbn->grade;
}

} else {
$params['gradetype'] = GRADE_TYPE_NONE;
if ($grades === 'reset') {
$params['reset'] = true;
$grades = NULL;
}

return grade_update('mod/bigbluebuttonbn', $bigbluebuttonbn->course, 'mod', 'bigbluebuttonbn', $bigbluebuttonbn->id, 0, $grades, $params);
}

/**
* Update activity grades.
*
* @param object $bigbluebuttonbn
* @param int $userid specific user only, 0 means all
*/
function bigbluebuttonbn_update_grades($bigbluebuttonbn, $userid=0, $nullifnone=true) {
global $CFG, $DB;
require_once($CFG->libdir.'/gradelib.php');

if ($bigbluebuttonbn->grade == 0) {
bigbluebuttonbn_grade_item_update($bigbluebuttonbn);
} else if ($grades = bigbluebuttonbn_get_user_grades($bigbluebuttonbn, $userid)) {
bigbluebuttonbn_grade_item_update($bigbluebuttonbn, $grades);
} else if ($userid and $nullifnone) {
$grade = new stdClass();
$grade->userid = $userid;
$grade->rawgrade = NULL;
bigbluebuttonbn_grade_item_update($bigbluebuttonbn, $grade);
} else {
bigbluebuttonbn_grade_item_update($bigbluebuttonbn);
}
}

/**
* Return grade for given user or all users.
*
* @param stdClass $bigbluebuttonbn record of bigbluebuttonbn
* @param int $userid optional user id, 0 means all users
* @return array array of grades, false if none
*/
function bigbluebuttonbn_get_user_grades($bigbluebuttonbn, $userid=0) {
global $CFG, $DB;
$userids = [];
if (!empty($userid)) {
$userids[] = $userid;
}
// TO DO: Determine the best way to retreive grades. BBB actvity DB tables or Moodle gradebook?
$gradinginfo = grade_get_grades(
$bigbluebuttonbn->course,
'mod',
'bigbluebuttonbn',
$bigbluebuttonbn->id,
$userids
);
$grades = false;
if (empty($grades)) {
return false;
}
return $grades;
}

/**
* Removes all grades from gradebook
*
* @global object
* @global object
* @param int $courseid
* @param string $type optional type
*/
function bigbluebuttonbn_reset_gradebook($courseid, $type='') {
global $CFG, $DB;

$sql = "SELECT b.*, cm.idnumber as cmidnumber, b.course as courseid
FROM {bigbluebuttonbn} b, {course_modules} cm, {modules} m
WHERE m.name='bigbluebuttonbn' AND m.id=cm.module AND cm.instance=b.id AND b.course=?";

if ($bigbluebuttonbns = $DB->get_records_sql($sql, array($courseid))) {
foreach ($bigbluebuttonbns as $bigbluebuttonbn) {
bigbluebuttonbn_grade_item_update($bigbluebuttonbn, 'reset');
}
}
}

/**
* Delete grade item for given activity
*
* @category grade
* @param object $bigbluebuttonbn object
* @return object bigbluebuttonbn
*/
function bigbluebuttonbn_grade_item_delete($bigbluebuttonbn) {
global $CFG;
require_once($CFG->libdir.'/gradelib.php');

return grade_update('mod/bigbluebuttonbn', $bigbluebuttonbn->course, 'mod', 'bigbluebuttonbn', $bigbluebuttonbn->id, 0, null, array('deleted' => 1));
}
3 changes: 2 additions & 1 deletion mod/bigbluebuttonbn/tests/generator/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ public function create_instance($record = null, ?array $options = null) {
"timecreated" => $now,
"timemodified" => $now,
"presentation" => null,
"recordings_preview" => 0
"recordings_preview" => 0,
"grade" => 0
];

$record = (array) $record;
Expand Down
4 changes: 2 additions & 2 deletions mod/bigbluebuttonbn/tests/lib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function test_bigbluebuttonbn_supports(): void {
$this->resetAfterTest();
$this->assertTrue(bigbluebuttonbn_supports(FEATURE_IDNUMBER));
$this->assertTrue(bigbluebuttonbn_supports(FEATURE_MOD_INTRO));
$this->assertFalse(bigbluebuttonbn_supports(FEATURE_GRADE_HAS_GRADE));
$this->assertTrue(bigbluebuttonbn_supports(FEATURE_GRADE_HAS_GRADE));
}

/**
Expand Down Expand Up @@ -570,7 +570,7 @@ public function test_bigbluebuttonbn_check_updates_since(): void {
$result = bigbluebuttonbn_check_updates_since($bbactivitycm, 0);
$this->assertEquals(
'{"configuration":{"updated":false},"contentfiles":{"updated":false},"introfiles":' .
'{"updated":false},"completion":{"updated":false}}',
'{"updated":false},"completion":{"updated":false},"gradeitems":{"updated":false},"outcomes":{"updated":false}}',
json_encode($result)
);
}
Expand Down
2 changes: 1 addition & 1 deletion mod/bigbluebuttonbn/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@
defined('MOODLE_INTERNAL') || die;


$plugin->version = 2024100700;
$plugin->version = 2025011000;
$plugin->requires = 2024100100;
$plugin->component = 'mod_bigbluebuttonbn';

0 comments on commit 4547472

Please sign in to comment.