Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new mods support #71

Merged
merged 12 commits into from
Oct 28, 2024
46 changes: 46 additions & 0 deletions classes/assessment/activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,17 @@ abstract class activity extends assessment {
/** @var \stdClass Course module object */
public \stdClass $coursemodule;

/** @var \stdClass Context object */
public \context_module $context;

/**
* Constructor.
*
* @param \stdClass $coursemodule
*/
public function __construct(\stdClass $coursemodule) {
$this->coursemodule = $coursemodule;
$this->context = \context_module::instance($this->coursemodule->id);
parent::__construct(assessmentfactory::SOURCETYPE_MOD, $coursemodule->id);
}

Expand Down Expand Up @@ -146,6 +150,9 @@ public function get_user_grade(int $userid, ?int $partid = null): ?array {
* @return array
*/
public function get_grade_items(): array {
global $CFG;
require_once("$CFG->libdir/gradelib.php");

$gradeitems = grade_item::fetch_all([
'itemtype' => $this->get_type(),
'itemmodule' => $this->get_module_name(),
Expand All @@ -165,4 +172,43 @@ protected function set_instance(): void {
global $DB;
$this->sourceinstance = $DB->get_record($this->coursemodule->modname, ['id' => $this->coursemodule->instance]);
}

/**
* Get the user IDs of gradeable users in this context i.e. students not teachers.
* @return int[] user IDs
*/
protected function get_gradeable_user_ids(): array {
global $DB, $CFG;

// Code adapted from grade/report/lib.php to limit to users with a gradeable role, i.e. students.
// The $CFG->gradebookroles setting is exposed on /admin/search.php?query=gradebookroles admin page.
$gradebookroles = explode(',', $CFG->gradebookroles);
if (empty($gradebookroles)) {
return[];
}
list($gradebookrolessql, $gradebookrolesparams) =
$DB->get_in_or_equal($gradebookroles, SQL_PARAMS_NAMED, 'gradebookroles');

// We want to query both the current context and parent contexts.
list($relatedctxsql, $relatedctxparams) = $DB->get_in_or_equal(
$this->context->get_parent_context_ids(true), SQL_PARAMS_NAMED, 'relatedctx'
);
$sql = "SELECT DISTINCT userid FROM {role_assignments} WHERE roleid $gradebookrolessql AND contextid $relatedctxsql";
return $DB->get_fieldset_sql($sql, array_merge($gradebookrolesparams, $relatedctxparams));
}

/**
* Get the details of gradeable (i.e. students not teachers) enrolled users in this context with specified capability.
* Can be used to get list of participants where activity has no 'student only' capability like 'mod/xxx:submit'.
* @param string $capability the capability string e.g. 'mod/lti:view'.
* @return array user details
*/
protected function get_gradeable_enrolled_users_with_capability(string $capability): array {
$enrolledusers = get_enrolled_users($this->context, $capability);
// Filter out non-gradeable users e.g. teachers.
$gradeableids = self::get_gradeable_user_ids();
return array_filter($enrolledusers, function($u) use ($gradeableids) {
return in_array($u->id, $gradeableids);
});
}
}
3 changes: 1 addition & 2 deletions classes/assessment/assign.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ class assign extends activity {
* @return array
*/
public function get_all_participants(): array {
$context = \context_module::instance($this->coursemodule->id);
return get_enrolled_users($context, 'mod/assign:submit');
return get_enrolled_users($this->context, 'mod/assign:submit');
}

/**
Expand Down
60 changes: 60 additions & 0 deletions classes/assessment/coursework.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?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 local_sitsgradepush\assessment;

/**
* Class for coursework plugin (mod_coursework) assessment.
*
* @package local_sitsgradepush
* @copyright 2024 onwards University College London {@link https://www.ucl.ac.uk/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author David Watson <[email protected]>
*/
class coursework extends activity {

/**
* Get all participants.
* @see \mod_coursework\models\coursework::get_students() which we don't use as it returns objects.
* @return \stdClass[]
*/
public function get_all_participants(): array {
$modinfo = get_fast_modinfo($this->get_course_id());
$cm = $modinfo->get_cm($this->coursemodule->id);
$info = new \core_availability\info_module($cm);

$users = get_enrolled_users($this->context, 'mod/coursework:submit');
return $info->filter_user_list($users);
}

/**
* Get the start date of this assessment.
*
* @return int|null
*/
public function get_start_date(): ?int {
return $this->sourceinstance->startdate > 0 ? $this->sourceinstance->startdate : null;
}

/**
* Get the end date of this assessment.
*
* @return int|null
*/
public function get_end_date(): ?int {
return $this->sourceinstance->deadline > 0 ? $this->sourceinstance->deadline : null;
}
}
60 changes: 60 additions & 0 deletions classes/assessment/hvp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?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 local_sitsgradepush\assessment;

/**
* Class for hvp (mod_hvp plugin, not core H5P) assessment.
* Note that the default for "Grade > Maximum grade" when HVP is added to a course is 10.
* If it is to be a candidate for marks transfer, this needs to be set to 100 by the teacher.
* Otherwise it will not be shown as a source for marks transfer.
*
* @package local_sitsgradepush
* @copyright 2024 onwards University College London {@link https://www.ucl.ac.uk/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author David Watson <[email protected]>
*/
class hvp extends activity {

/**
* Get all participants.
*
* @return array
*/
public function get_all_participants(): array {
return self::get_gradeable_enrolled_users_with_capability('mod/hvp:view');
}

/**
* Get the start date of the assessment.
*
* @return int|null
*/
public function get_start_date(): ?int {
// Activity does not have a start date.
return null;
}

/**
* Get the end date of the assessment.
*
* @return int|null
*/
public function get_end_date(): ?int {
// Activity does not have an end date.
return null;
}
}
73 changes: 73 additions & 0 deletions classes/assessment/lesson.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?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 local_sitsgradepush\assessment;

/**
* Class for lesson assessment.
*
* @package local_sitsgradepush
* @copyright 2024 onwards University College London {@link https://www.ucl.ac.uk/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author David Watson <[email protected]>
*/
class lesson extends activity {

/**
* Get all participants.
*
* @return array
*/
public function get_all_participants(): array {
if ($this->sourceinstance->practice) {
// If this is a "Practice Lesson" it does not appear in gradebook.
return [];
}
return self::get_gradeable_enrolled_users_with_capability('mod/lesson:view');
}

/**
* Get the start date of this assessment.
*
* @return int|null
*/
public function get_start_date(): ?int {
return !$this->sourceinstance->practice && $this->sourceinstance->available > 0
? $this->sourceinstance->available : null;
}

/**
* Get the end date of this assessment.
*
* @return int|null
*/
public function get_end_date(): ?int {
return !$this->sourceinstance->practice && $this->sourceinstance->deadline > 0
? $this->sourceinstance->deadline : null;
}

/**
* Check assessment is valid for mapping.
*
* @return \stdClass
*/
public function check_assessment_validity(): \stdClass {
if ($this->sourceinstance->practice) {
return $this->set_validity_result(false, 'error:lesson_practice');
}
return parent::check_assessment_validity();
}
}
92 changes: 92 additions & 0 deletions classes/assessment/lti.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?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 local_sitsgradepush\assessment;

/**
* Class for LTI assessment.
*
* @package local_sitsgradepush
* @copyright 2024 onwards University College London {@link https://www.ucl.ac.uk/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author David Watson <[email protected]>
*/
class lti extends activity {

/**
* Get all participants.
*
* @return array
*/
public function get_all_participants(): array {
return self::get_gradeable_enrolled_users_with_capability('mod/lti:view');
}

/**
* Get the start date of this assessment.
*
* @return int|null
*/
public function get_start_date(): ?int {
// This activity does not have a start date.
return null;
}

/**
* Get the end date of this assessment.
*
* @return int|null
*/
public function get_end_date(): ?int {
// This activity does not have an end date.
return null;
}

/**
* Check assessment is valid for mapping.
*
* @return \stdClass
*/
public function check_assessment_validity(): \stdClass {
global $CFG, $DB;

// For LTI_SETTING_ constants.
require_once("$CFG->dirroot/mod/lti/locallib.php");

// Which type of LTI tool is this?
$typeid = $this->sourceinstance->typeid;
if (!$typeid) {
$tool = lti_get_tool_by_url_match($this->sourceinstance->toolurl, $this->sourceinstance->course);
if ($tool) {
$typeid = $tool->id;
}
}

// Has this tool been configured to accept grades globally or not?
$acceptgradestool = $DB->get_field(
'lti_types_config', 'value', ['typeid' => $typeid, 'name' => 'acceptgrades']
);
if ($acceptgradestool == LTI_SETTING_ALWAYS) {
// At system level, LTI grades are set to be sent to gradebook.
return parent::check_assessment_validity();
} else if ($acceptgradestool == LTI_SETTING_DELEGATE &&
$this->sourceinstance->instructorchoiceacceptgrades == LTI_SETTING_ALWAYS) {
// Whether or not grades are accepted is delegated to course level, which is set to yes.
return parent::check_assessment_validity();
}
return $this->set_validity_result(false, 'error:lti_no_grades');
}
}
3 changes: 1 addition & 2 deletions classes/assessment/quiz.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ class quiz extends activity {
* @return array
*/
public function get_all_participants(): array {
$context = \context_module::instance($this->coursemodule->id);
return get_enrolled_users($context, 'mod/quiz:attempt');
return get_enrolled_users($this->context, 'mod/quiz:attempt');
}

/**
Expand Down
3 changes: 1 addition & 2 deletions classes/assessment/turnitintooltwo.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ public function __construct(\stdClass $coursemodule) {
* @return array
*/
public function get_all_participants(): array {
$context = \context_module::instance($this->coursemodule->id);
return get_enrolled_users($context, 'mod/turnitintooltwo:submit');
return get_enrolled_users($this->context, 'mod/turnitintooltwo:submit');
}

/**
Expand Down
Loading
Loading