2023-05-29 23:01:04 +02:00
|
|
|
<?php
|
2023-08-24 23:02:41 +02:00
|
|
|
// This file is part of the Studyplan plugin for Moodle
|
|
|
|
//
|
|
|
|
// 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 <https://www.gnu.org/licenses/>.
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @package local_treestudyplan
|
|
|
|
* @copyright 2023 P.M. Kuipers
|
|
|
|
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
2023-05-29 23:01:04 +02:00
|
|
|
|
|
|
|
namespace local_treestudyplan;
|
|
|
|
require_once($CFG->libdir.'/externallib.php');
|
|
|
|
|
|
|
|
use \grade_item;
|
|
|
|
|
2023-08-25 10:41:56 +02:00
|
|
|
class completionscanner {
|
2023-08-25 09:33:42 +02:00
|
|
|
private static $modsupported = [];
|
|
|
|
private static $coursestudents = [];
|
2023-05-29 23:01:04 +02:00
|
|
|
private $scanner = null;
|
|
|
|
private $course = null;
|
|
|
|
private $cm = null;
|
|
|
|
private $gi = null;
|
2023-08-25 09:33:42 +02:00
|
|
|
private $pendingcache = [];
|
2023-05-29 23:01:04 +02:00
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
public static function supported($mod) {
|
2023-08-25 09:33:42 +02:00
|
|
|
if (!array_key_exists($mod, self::$modsupported)) {
|
|
|
|
self::$modsupported[$mod] = class_exists("\local_treestudyplan\\local\\ungradedscanners\\{$mod}_scanner");
|
2023-05-29 23:01:04 +02:00
|
|
|
}
|
2023-08-25 09:33:42 +02:00
|
|
|
return self::$modsupported[$mod];
|
2023-05-29 23:01:04 +02:00
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
public static function get_course_students($courseid) {
|
2023-05-29 23:01:04 +02:00
|
|
|
global $CFG;
|
2023-08-25 09:33:42 +02:00
|
|
|
if (!array_key_exists($courseid, self::$coursestudents)) {
|
2023-05-29 23:01:04 +02:00
|
|
|
$students = [];
|
|
|
|
$context = \context_course::instance($courseid);
|
2023-08-24 23:02:41 +02:00
|
|
|
foreach (explode(', ', $CFG->gradebookroles) as $roleid) {
|
2023-05-29 23:01:04 +02:00
|
|
|
$roleid = trim($roleid);
|
|
|
|
$students = array_keys(get_role_users($roleid, $context, false, 'u.id', 'u.id ASC'));
|
|
|
|
}
|
2023-08-25 09:33:42 +02:00
|
|
|
self::$coursestudents[$courseid] = $students;
|
2023-05-29 23:01:04 +02:00
|
|
|
}
|
2023-08-25 09:33:42 +02:00
|
|
|
return self::$coursestudents[$courseid];
|
2023-05-29 23:01:04 +02:00
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
public function __construct(\completion_criteria $crit, $course) {
|
2023-06-16 23:12:17 +02:00
|
|
|
$this->courseid = $course->id;
|
|
|
|
$this->course = $course;
|
|
|
|
$this->modinfo = get_fast_modinfo($course);
|
2023-05-29 23:01:04 +02:00
|
|
|
$this->crit = $crit;
|
2023-06-16 23:12:17 +02:00
|
|
|
|
|
|
|
$this->completioninfo = new \completion_info($course);
|
2023-08-24 23:02:41 +02:00
|
|
|
|
|
|
|
// Find a related scanner if the type is an activity type.
|
|
|
|
if ($crit->criteriatype == COMPLETION_CRITERIA_TYPE_ACTIVITY) {
|
|
|
|
// First find the course module.
|
2023-06-16 23:12:17 +02:00
|
|
|
$this->cm = $this->modinfo->get_cm($crit->moduleinstance);
|
2023-08-25 11:52:05 +02:00
|
|
|
$gi = grade_item::fetch( ['itemtype' => 'mod',
|
|
|
|
'itemmodule' => $this->cm->modname,
|
|
|
|
'iteminstance' => $this->cm->instance,
|
|
|
|
'courseid' => $this->courseid]);
|
2023-08-24 23:02:41 +02:00
|
|
|
if ($gi !== false) {
|
2023-08-25 11:52:05 +02:00
|
|
|
/* Grade none items should not be relevant.
|
|
|
|
Note that the grade status is probably only relevant if the item
|
|
|
|
has not yet received a completion, but has been submitted.
|
|
|
|
*/
|
2023-08-24 23:02:41 +02:00
|
|
|
if (($gi->gradetype == GRADE_TYPE_VALUE || $gi->gradetype == GRADE_TYPE_SCALE)) {
|
|
|
|
// If it's a relevant grade type, initialize a scanner if possible.
|
|
|
|
$this->gi = $gi;
|
|
|
|
if (self::supported($gi->itemmodule)) {
|
2023-05-29 23:01:04 +02:00
|
|
|
$scannerclass = "\local_treestudyplan\\local\ungradedscanners\\{$gi->itemmodule}_scanner";
|
|
|
|
$this->scanner = new $scannerclass($gi);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
public function pending($userid) {
|
|
|
|
if (!array_key_exists($userid, $this->pending_cache)) {
|
|
|
|
if ($this->scanner === null) {
|
2023-05-29 23:01:04 +02:00
|
|
|
$this->pending_cache[$userid] = false;
|
2023-08-24 23:09:20 +02:00
|
|
|
} else {
|
2023-08-24 23:02:41 +02:00
|
|
|
$this->pending_cache[$userid] = $this->scanner->has_ungraded_submission($userid);;
|
2023-05-29 23:01:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->pending_cache[$userid];
|
2023-08-24 23:02:41 +02:00
|
|
|
}
|
2023-05-29 23:01:04 +02:00
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
public static function structure($value=VALUE_OPTIONAL) {
|
2023-05-29 23:01:04 +02:00
|
|
|
return new \external_single_structure([
|
|
|
|
"ungraded" => new \external_value(PARAM_INT, 'number of ungraded submissions'),
|
2023-06-03 00:01:43 +02:00
|
|
|
"completed" => new \external_value(PARAM_INT, 'number of completed students'),
|
|
|
|
"completed_pass" => new \external_value(PARAM_INT, 'number of completed-pass students'),
|
|
|
|
"completed_fail" => new \external_value(PARAM_INT, 'number of completed-fail students'),
|
2023-05-29 23:01:04 +02:00
|
|
|
"students" => new \external_value(PARAM_INT, 'number of students that should submit'),
|
2023-08-24 23:02:41 +02:00
|
|
|
], "details about gradable submissions", $value);
|
2023-05-29 23:01:04 +02:00
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
public function model() {
|
|
|
|
|
2023-08-25 09:44:34 +02:00
|
|
|
// Get completion info.
|
2023-06-16 23:12:17 +02:00
|
|
|
$students = self::get_course_students($this->courseid);
|
|
|
|
$completed = 0;
|
|
|
|
$ungraded = 0;
|
2023-08-25 09:33:42 +02:00
|
|
|
$completedpass = 0;
|
|
|
|
$completedfail = 0;
|
2023-08-24 23:02:41 +02:00
|
|
|
foreach ($students as $userid) {
|
|
|
|
if ($this->pending($userid)) {
|
|
|
|
// First check if the completion needs grading.
|
2023-06-16 23:12:17 +02:00
|
|
|
$ungraded++;
|
|
|
|
} else {
|
2023-08-24 23:02:41 +02:00
|
|
|
$completion = $this->completioninfo->get_user_completion($userid, $this->crit);
|
|
|
|
|
|
|
|
if ($this->crit->criteriatype == COMPLETION_CRITERIA_TYPE_ACTIVITY) {
|
|
|
|
// If it's an activity completion, add all the relevant activities as sub-items.
|
2023-08-25 09:33:42 +02:00
|
|
|
$completionstatus = $this->completioninfo->get_grade_completion($this->cm, $userid);
|
2023-08-24 23:02:41 +02:00
|
|
|
|
2023-08-25 09:33:42 +02:00
|
|
|
if ($completionstatus == COMPLETION_COMPLETE_PASS) {
|
|
|
|
$completedpass++;
|
|
|
|
} else if ($completionstatus == COMPLETION_COMPLETE_FAIL) {
|
|
|
|
$completedfail++;
|
|
|
|
} else if ($completionstatus == COMPLETION_COMPLETE) {
|
2023-06-16 23:12:17 +02:00
|
|
|
$completed++;
|
|
|
|
}
|
2023-08-25 10:41:56 +02:00
|
|
|
} else {
|
2023-08-24 23:02:41 +02:00
|
|
|
if ($completion->is_complete()) {
|
2023-06-16 23:12:17 +02:00
|
|
|
$completed++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-29 23:01:04 +02:00
|
|
|
return [
|
2023-06-16 23:12:17 +02:00
|
|
|
'ungraded' => $ungraded,
|
|
|
|
'completed' => $completed,
|
2023-08-25 09:33:42 +02:00
|
|
|
'completed_pass' => $completedpass,
|
|
|
|
'completed_fail' => $completedfail,
|
2023-06-16 23:12:17 +02:00
|
|
|
'students' => count($students),
|
2023-05-29 23:01:04 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2023-08-25 11:52:05 +02:00
|
|
|
}
|