moodle_local_treestudyplan/classes/corecompletioninfo.php

682 lines
34 KiB
PHP
Raw Normal View History

<?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
*/
namespace local_treestudyplan;
2023-08-25 12:04:27 +02:00
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir.'/externallib.php');
require_once($CFG->libdir.'/gradelib.php');
require_once($CFG->dirroot.'/course/lib.php');
use core_course\local\repository\caching_content_item_readonly_repository;
use core_course\local\repository\content_item_readonly_repository;
use \grade_item;
use \grade_scale;
use \grade_outcome;
class corecompletioninfo {
private $course;
private $completion;
private $modinfo;
2023-08-25 13:34:31 +02:00
private static $completionhandles = null;
private static $completiontypes = null;
2023-08-24 23:02:41 +02:00
public function id() {
return $this->course->id;
}
2023-08-24 23:02:41 +02:00
public function __construct($course) {
global $DB;
$this->course = $course;
$this->completion = new \completion_info($this->course);
$this->modinfo = get_fast_modinfo($this->course);
}
2023-08-25 13:34:31 +02:00
public static function completiontypes() {
global $COMPLETION_CRITERIA_TYPES;
/* Just return the keys of the global array COMPLETION_CRITERIA_TYPES,
so we don't have to manually add any completion types if moodle decides to add a few.
Unfortunately, the global variable breaks the moodle coding standard...
*/
if (!isset(self::$completiontypes)) {
self::$completiontypes = \array_keys($COMPLETION_CRITERIA_TYPES);
}
return self::$completiontypes;
}
/**
2023-08-24 23:02:41 +02:00
* Translate a numeric completion constant to a text string
* @param $completion The completion code as defined in completionlib.php to translate to a text handle
*/
2023-08-25 13:34:31 +02:00
public static function completion_handle($completion) {
if (empty(self::$completionhandles)) {
2023-08-24 23:02:41 +02:00
// Cache the translation table, to avoid overhead.
2023-08-25 13:34:31 +02:00
self::$completionhandles = [
COMPLETION_INCOMPLETE => "incomplete",
2023-08-24 23:02:41 +02:00
COMPLETION_COMPLETE => "complete",
COMPLETION_COMPLETE_PASS => "complete-pass",
COMPLETION_COMPLETE_FAIL => "complete-fail",
2023-08-25 10:41:56 +02:00
COMPLETION_COMPLETE_FAIL_HIDDEN => "complete-fail"]; // The front end won't differentiate between hidden or not.
}
2023-08-25 13:34:31 +02:00
return self::$completionhandles[$completion] ?? "undefined";
}
2023-08-25 13:04:19 +02:00
public static function completion_item_editor_structure($value = VALUE_REQUIRED) {
return new \external_single_structure([
2023-08-24 23:02:41 +02:00
"id" => new \external_value(PARAM_INT, 'criteria id', VALUE_OPTIONAL),
"title" => new \external_value(PARAM_TEXT, 'name of subitem', VALUE_OPTIONAL),
"link" => new \external_value(PARAM_TEXT, 'optional link to more details', VALUE_OPTIONAL),
"details" => new \external_single_structure([
2023-08-24 23:02:41 +02:00
"type" => new \external_value(PARAM_RAW, 'type', VALUE_OPTIONAL),
"criteria" => new \external_value(PARAM_RAW, 'criteria', VALUE_OPTIONAL),
"requirement" => new \external_value(PARAM_RAW, 'requirement', VALUE_OPTIONAL),
"status" => new \external_value(PARAM_RAW, 'status', VALUE_OPTIONAL),
2023-06-03 00:01:43 +02:00
]),
"progress" => completionscanner::structure(),
2023-08-24 23:02:41 +02:00
], 'completion type', $value);
}
2023-08-25 13:04:19 +02:00
public static function completion_type_editor_structure($value = VALUE_REQUIRED) {
return new \external_single_structure([
2023-08-24 23:02:41 +02:00
"items" => new \external_multiple_structure(self::completion_item_editor_structure(), 'subitems', VALUE_OPTIONAL),
"title" => new \external_value(PARAM_TEXT, 'optional title', VALUE_OPTIONAL),
"desc" => new \external_value(PARAM_TEXT, 'optional description', VALUE_OPTIONAL),
"type" => new \external_value(PARAM_TEXT, 'completion type name'),
2023-08-24 23:02:41 +02:00
"aggregation" => new \external_value(PARAM_TEXT, 'completion aggregation for this type ["all", "any"]'),
], 'completion type', $value);
}
2023-08-25 13:04:19 +02:00
public static function editor_structure($value = VALUE_REQUIRED) {
return new \external_single_structure([
2023-08-24 23:02:41 +02:00
"conditions" => new \external_multiple_structure(self::completion_type_editor_structure(), 'completion conditions'),
"aggregation" => new \external_value(PARAM_TEXT, 'completion aggregation ["all", "any"]'),
"enabled" => new \external_value(PARAM_BOOL, "whether completion is enabled here"),
], 'course completion info', $value);
}
2023-08-25 13:04:19 +02:00
public static function completion_item_user_structure($value = VALUE_REQUIRED) {
return new \external_single_structure([
2023-08-24 23:02:41 +02:00
"id" => new \external_value(PARAM_INT, 'id of completion', VALUE_OPTIONAL),
"title" => new \external_value(PARAM_TEXT, 'name of subitem', VALUE_OPTIONAL),
"details" => new \external_single_structure([
2023-08-24 23:02:41 +02:00
"type" => new \external_value(PARAM_RAW, 'type', VALUE_OPTIONAL),
"criteria" => new \external_value(PARAM_RAW, 'criteria', VALUE_OPTIONAL),
"requirement" => new \external_value(PARAM_RAW, 'requirement', VALUE_OPTIONAL),
"status" => new \external_value(PARAM_RAW, 'status', VALUE_OPTIONAL),
]),
2023-08-24 23:02:41 +02:00
"link" => new \external_value(PARAM_TEXT, 'optional link to more details', VALUE_OPTIONAL),
"completed" => new \external_value(PARAM_BOOL, 'simple completed or not'),
2023-08-25 11:52:05 +02:00
"status" => new \external_value(PARAM_TEXT,
'extended completion status ["incomplete", "progress", "complete", "complete-pass", "complete-fail"]'),
"pending" => new \external_value(PARAM_BOOL,
'optional pending state, for submitted but not yet reviewed activities', VALUE_OPTIONAL),
2023-08-24 23:02:41 +02:00
"grade" => new \external_value(PARAM_TEXT, 'optional grade result for this subitem', VALUE_OPTIONAL),
"feedback" => new \external_value(PARAM_RAW, 'optional feedback for this subitem ', VALUE_OPTIONAL),
], 'completion type', $value);
}
2023-08-25 13:04:19 +02:00
public static function completion_type_user_structure($value = VALUE_REQUIRED) {
return new \external_single_structure([
2023-08-24 23:02:41 +02:00
"items" => new \external_multiple_structure(self::completion_item_user_structure(), 'subitems', VALUE_OPTIONAL),
"title" => new \external_value(PARAM_TEXT, 'optional title', VALUE_OPTIONAL),
"desc" => new \external_value(PARAM_TEXT, 'optional description', VALUE_OPTIONAL),
"type" => new \external_value(PARAM_TEXT, 'completion type name'),
2023-08-24 23:02:41 +02:00
"aggregation" => new \external_value(PARAM_TEXT, 'completion aggregation for this type ["all", "any"]'),
"completed" => new \external_value(PARAM_BOOL, 'current completion value for this type'),
2023-08-25 11:52:05 +02:00
"status" => new \external_value(PARAM_TEXT,
'extended completion status ["incomplete", "progress", "complete", "complete-pass", "complete-fail"]'),
2023-05-20 18:25:22 +02:00
"progress" => new \external_value(PARAM_INT, 'completed sub-conditions'),
"count" => new \external_value(PARAM_INT, 'total number of sub-conditions'),
2023-08-24 23:02:41 +02:00
], 'completion type', $value);
}
2023-08-25 13:04:19 +02:00
public static function user_structure($value = VALUE_REQUIRED) {
return new \external_single_structure([
"progress" => new \external_value(PARAM_INT, 'completed sub-conditions'),
2023-08-24 23:02:41 +02:00
"enabled" => new \external_value(PARAM_BOOL, "whether completion is enabled here"),
"tracked" => new \external_value(PARAM_BOOL, "whether completion is tracked for the user", VALUE_OPTIONAL),
"count" => new \external_value(PARAM_INT, 'total number of sub-conditions'),
2023-08-24 23:02:41 +02:00
"conditions" => new \external_multiple_structure(self::completion_type_user_structure(), 'completion conditions'),
"completed" => new \external_value(PARAM_BOOL, 'current completion value'),
2023-08-24 23:02:41 +02:00
"aggregation" => new \external_value(PARAM_TEXT, 'completion aggregation ["all", "any"]'),
"pending" => new \external_value(PARAM_BOOL, "true if the user has any assignments pending grading", VALUE_OPTIONAL),
], 'course completion info', $value);
}
2023-08-24 23:02:41 +02:00
private static function aggregation_handle($method) {
2023-08-25 13:34:31 +02:00
return ($method == COMPLETION_AGGREGATION_ALL) ? "all" : "any";
}
public function editor_model() {
2023-08-25 13:34:31 +02:00
global $DB, $CFG;
2023-08-24 23:02:41 +02:00
$conditions = [];
2023-08-25 10:41:56 +02:00
$aggregation = "all"; // Default.
$info = [
"conditions" => $conditions,
"aggregation" => self::aggregation_handle($this->completion->get_aggregation_method()),
2023-08-24 23:02:41 +02:00
"enabled" => $this->completion->is_enabled()
];
2023-08-24 23:02:41 +02:00
// Check if completion tracking is enabled for this course - otherwise, revert to defaults .
if ($this->completion->is_enabled()) {
$aggregation = $this->completion->get_aggregation_method();
2023-08-24 23:02:41 +02:00
// Loop through all condition types to see if they are applicable.
foreach (self::completiontypes() as $type) {
$criterias = $this->completion->get_criteria($type); // Returns array of relevant criteria items.
2023-08-25 11:52:05 +02:00
if (count($criterias) > 0 ) {
// Only take it into account if the criteria count is > 0.
$cinfo = [
2023-08-25 13:34:31 +02:00
"type" => self::completiontypes()[$type],
"aggregation" => self::aggregation_handle($this->completion->get_aggregation_method($type)),
"title" => reset($criterias)->get_type_title(),
"items" => [],
];
2023-08-24 23:02:41 +02:00
foreach ($criterias as $criteria) {
// Unfortunately, we cannot easily get the criteria details with get_details() without having a .
2023-08-25 09:44:34 +02:00
// User completion object involved, so'we'll have to retrieve the details per completion type.
2023-08-24 23:02:41 +02:00
// See moodle/completion/criteria/completion_criteria_*.php::get_details() for the code that is.
2023-08-25 09:44:34 +02:00
// In the code below is based on.
2023-08-24 23:02:41 +02:00
if ($type == COMPLETION_CRITERIA_TYPE_SELF) {
$details = [
"type" => $criteria->get_title(),
"criteria" => $criteria->get_title(),
"requirement" => get_string('markingyourselfcomplete', 'completion'),
"status" => "",
];
2023-08-24 23:09:20 +02:00
} else if ($type == COMPLETION_CRITERIA_TYPE_DATE) {
$details = [
"type" => get_string('datepassed', 'completion'),
"criteria" => get_string('remainingenroleduntildate', 'completion'),
2023-08-24 23:02:41 +02:00
"requirement" => date("Y-m-d", $criteria->timeend),
"status" => "",
];
2023-08-24 23:09:20 +02:00
} else if ($type == COMPLETION_CRITERIA_TYPE_UNENROL) {
$details = [
"type" => get_string('unenrolment', 'completion'),
"criteria" => get_string('unenrolment', 'completion'),
"requirement" => get_string('unenrolingfromcourse', 'completion'),
"status" => "",
];
2023-08-24 23:09:20 +02:00
} else if ($type == COMPLETION_CRITERIA_TYPE_ACTIVITY) {
$cm = $this->modinfo->get_cm($criteria->moduleinstance);
2023-08-25 11:52:05 +02:00
/* Criteria and requirements will be built in a moment by code copied
from completion_criteria_activity.php.
*/
$details = [
"type" => $criteria->get_title(),
2023-08-25 11:52:05 +02:00
"criteria" => "",
"requirement" => "",
"status" => "",
];
if ($cm->has_view()) {
$details['criteria'] = \html_writer::link($cm->url, $cm->get_formatted_name());
} else {
$details['criteria'] = $cm->get_formatted_name();
}
2023-08-24 23:02:41 +02:00
// Build requirements.
$details['requirement'] = array();
if ($cm->completion == COMPLETION_TRACKING_MANUAL) {
$details['requirement'][] = get_string('markingyourselfcomplete', 'completion');
2023-08-24 23:02:41 +02:00
} else if ($cm->completion == COMPLETION_TRACKING_AUTOMATIC) {
if ($cm->completionview) {
2023-07-04 23:38:08 +02:00
$modulename = \core_text::strtolower(get_string('modulename', $criteria->module));
$details['requirement'][] = get_string('viewingactivity', 'completion', $modulename);
}
if (!is_null($cm->completiongradeitemnumber)) {
$details['requirement'][] = get_string('achievinggrade', 'completion');
}
if ($cm->completionpassgrade) {
$details['requirement'][] = get_string('achievingpassinggrade', 'completion');
}
}
$details['requirement'] = implode(', ', $details['requirement']);
2023-08-24 23:09:20 +02:00
} else if ($type == COMPLETION_CRITERIA_TYPE_DURATION) {
$details = [
"type" => get_string('periodpostenrolment', 'completion'),
"criteria" => get_string('remainingenroledfortime', 'completion'),
2023-08-25 13:34:31 +02:00
"requirement" => get_string('xdays', 'completion', ceil($criteria->enrolperiod / (60 * 60 * 24))),
"status" => "",
];
2023-08-24 23:09:20 +02:00
} else if ($type == COMPLETION_CRITERIA_TYPE_GRADE) {
$details = [
"type" => get_string('coursegrade', 'completion'),
"criteria" => get_string('graderequired', 'completion'),
2023-08-24 23:02:41 +02:00
// TODO: convert to selected representation (letter, percentage, etc).
2023-08-25 11:52:05 +02:00
"requirement" => get_string('graderequired', 'completion')
.": ".format_float($criteria->gradepass, 1),
"status" => "",
];
2023-08-24 23:09:20 +02:00
} else if ($type == COMPLETION_CRITERIA_TYPE_ROLE) {
$criteria = $criteria->get_title();
2023-08-24 23:02:41 +02:00
$details = [
"type" => get_string('manualcompletionby', 'completion'),
"criteria" => $criteria,
"requirement" => get_string('markedcompleteby', 'completion', $criteria),
"status" => "",
];
2023-08-24 23:09:20 +02:00
} else if ($type == COMPLETION_CRITERIA_TYPE_COURSE) {
$prereq = get_course($criteria->courseinstance);
$coursecontext = \context_course::instance($prereq->id, MUST_EXIST);
$fullname = format_string($prereq->fullname, true, array('context' => $coursecontext));
$details = [
"type" => $criteria->get_title(),
2023-08-25 11:52:05 +02:00
"criteria" => '<a href="'.$CFG->wwwroot.'/course/view.php?id='.
$criteria->courseinstance.'">'.s($fullname).'</a>',
"requirement" => get_string('coursecompleted', 'completion'),
"status" => "",
];
} else {
2023-08-24 23:02:41 +02:00
// Moodle added a criteria type.
$details = [
"type" => "",
"criteria" => "",
"requirement" => "",
"status" => "",
];
}
2023-08-24 23:02:41 +02:00
$scanner = new completionscanner($criteria, $this->course);
2023-08-25 09:44:34 +02:00
// Only add the items list if we actually have items...
$cinfo["items"][] = [
"id" => $criteria->id,
"title" => $criteria->get_title_detailed(),
"details" => $details,
"progress" => $scanner->model(),
];
}
$info['conditions'][] = $cinfo;
}
}
}
return $info;
}
2023-08-24 23:02:41 +02:00
private function aggregate_completions($typeaggregation, $completions) {
$completed = 0;
$count = count($completions);
2023-08-24 23:02:41 +02:00
foreach ($completions as $c) {
if ($c->is_complete()) {
$completed++;
}
}
2023-08-24 23:02:41 +02:00
if ($typeaggregation == COMPLETION_AGGREGATION_ALL) {
return $completed >= $count;
2023-08-24 23:09:20 +02:00
} else { // COMPLETION_AGGREGATION_ANY.
return $completed > 1;
}
}
public function user_model($userid) {
2023-08-25 13:34:31 +02:00
global $DB;
$progress = $this->get_advanced_progress_percentage($userid);
$info = [
'progress' => $progress->completed,
"count" => $progress->count,
"conditions" => [],
"completed" => $this->completion->is_course_complete($userid),
"aggregation" => self::aggregation_handle($this->completion->get_aggregation_method()),
2023-08-24 23:02:41 +02:00
"enabled" => $this->completion->is_enabled(),
"tracked" => $this->completion->is_tracked_user($userid),
];
2023-08-24 23:02:41 +02:00
// Check if completion tracking is enabled for this course - otherwise, revert to defaults .
if ($this->completion->is_enabled() && $this->completion->is_tracked_user($userid)) {
$anypending = false;
2023-08-24 23:02:41 +02:00
// Loop through all conditions to see if they are applicable.
foreach (self::completiontypes() as $type) {
// Get the main completion for this type.
$completions = $this->completion->get_completions($userid, $type);
if (count($completions) > 0) {
$typeaggregation = $this->completion->get_aggregation_method($type);
2023-08-24 23:02:41 +02:00
$completed = $this->aggregate_completions($typeaggregation, $completions);
$cinfo = [
2023-08-25 13:34:31 +02:00
"type" => self::completiontypes()[$type],
"aggregation" => self::aggregation_handle($typeaggregation),
"completed" => $completed,
2023-08-25 10:41:56 +02:00
"status" => $completed ? "complete" : "incomplete",
"title" => reset($completions)->get_criteria()->get_type_title(),
"items" => [],
];
2023-05-20 18:25:22 +02:00
$progress = 0;
2023-08-24 23:02:41 +02:00
foreach ($completions as $completion) {
$criteria = $completion->get_criteria();
2023-08-24 23:02:41 +02:00
if ($completion->is_complete()) {
$progress += 1; // Add a point to the progress counter.
}
2023-05-20 18:25:22 +02:00
$iinfo = [
"id" => $criteria->id,
"title" => $criteria->get_title_detailed(),
"details" => $criteria->get_details($completion),
2023-08-24 23:02:41 +02:00
"completed" => $completion->is_complete(), // Make sure to override for activi.
2023-08-25 11:52:05 +02:00
"status" => self::completion_handle(
$completion->is_complete() ? COMPLETION_COMPLETE : COMPLETION_INCOMPLETE),
];
2023-08-24 23:02:41 +02:00
if ($type == COMPLETION_CRITERIA_TYPE_ACTIVITY) {
$cm = $this->modinfo->get_cm($criteria->moduleinstance);
2023-08-24 23:02:41 +02:00
// If it's an activity completion, add all the relevant activities as sub-items.
2023-08-25 09:33:42 +02:00
$completionstatus = $this->completion->get_grade_completion($cm, $userid);
$iinfo['status'] = self::completion_handle($completionstatus);
2023-08-24 23:02:41 +02:00
// Re-evaluate the completed value, to make sure COMPLETE_FAIL doesn't creep in as completed.
2023-08-25 09:33:42 +02:00
$iinfo['completed'] = in_array($completionstatus, [COMPLETION_COMPLETE, COMPLETION_COMPLETE_PASS]);
2023-08-24 23:02:41 +02:00
// Determine the grade (retrieve from grade item, not from completion).
$grade = $this->get_grade($cm, $userid);
$iinfo['grade'] = $grade->grade;
$iinfo['feedback'] = $grade->feedback;
$iinfo['pending'] = $grade->pending;
$anypending = $anypending || $grade->pending;
2023-08-24 23:02:41 +02:00
// Overwrite the status with progress if something has been graded, or is pending.
2023-08-25 09:33:42 +02:00
if ($completionstatus != COMPLETION_INCOMPLETE || $anypending) {
2023-08-24 23:02:41 +02:00
if ($cinfo["status"] == "incomplete") {
$cinfo["status"] = "progress";
}
}
2023-08-24 23:09:20 +02:00
} else if ($type == COMPLETION_CRITERIA_TYPE_GRADE) {
2023-08-24 23:02:41 +02:00
// Make sure we provide the current course grade.
2023-07-04 23:38:08 +02:00
$iinfo['grade'] = floatval($iinfo['details']['status']);
2023-08-24 23:02:41 +02:00
if ($iinfo["grade"] > 0) {
2023-08-25 11:52:05 +02:00
$iinfo["grade"] = format_float($iinfo["grade"], 1). "/".
format_float(floatval($iinfo['details']['requirement']));
2023-08-25 10:41:56 +02:00
$iinfo["status"] = $completion->is_complete() ? "complete-pass" : "complete-fail";
2023-08-24 23:02:41 +02:00
if ($cinfo["status"] == "incomplete") {
$cinfo["status"] = "progress";
}
}
}
2023-08-25 09:44:34 +02:00
// Finally add the item to the items list.
$cinfo["items"][] = $iinfo;
}
2023-05-20 18:25:22 +02:00
2023-08-24 23:02:41 +02:00
// Set the count and progress stats based on the Type's aggregation style.
if ($typeaggregation == COMPLETION_AGGREGATION_ALL) {
// Count and Progress amount to the sum of items.
2023-05-20 18:25:22 +02:00
$cinfo["count"] = count($cinfo["items"]);
$cinfo["progress"] = $progress;
2023-08-25 09:44:34 +02:00
} else { // Typeaggregation == COMPLETION_AGGREGATION_ANY.
2023-08-24 23:02:41 +02:00
// Count and progress are either 1 or 0, since any of the items.
2023-08-25 09:44:34 +02:00
// Complete's the type.
2023-08-25 12:16:51 +02:00
$cinfo["count"] = (count($cinfo["items"]) > 0) ? 1 : 0;
2023-08-25 13:34:31 +02:00
$cinfo["progress"] = ($progress > 0) ? 1 : 0;
2023-05-20 18:25:22 +02:00
}
$info['conditions'][] = $cinfo;
$info['pending'] = $anypending;
}
}
}
return $info;
}
/**
* Get the grade for a certain course module
* @return stdClass|null object containing 'grade' and optional 'feedback' attribute
*/
2023-08-24 23:02:41 +02:00
private function get_grade($cm, $userid) {
// TODO: Display grade in the way described in the course setup (with letters if needed).
2023-08-25 13:04:19 +02:00
$gi = grade_item::fetch(['itemtype' => 'mod',
2023-08-24 23:02:41 +02:00
'itemmodule' => $cm->modname,
'iteminstance' => $cm->instance,
'courseid' => $this->course->id]); // Make sure we only get results relevant to this course.
if ($gi) {
// Only the following types of grade yield a result.
if (($gi->gradetype == GRADE_TYPE_VALUE || $gi->gradetype == GRADE_TYPE_SCALE)) {
$scale = $gi->load_scale();
2023-08-24 23:02:41 +02:00
$grade = (object)$gi->get_final($userid); // Get the grade for the specified user.
$result = new \stdClass;
2023-08-24 23:02:41 +02:00
// Check if the final grade is available and numeric (safety check).
if (!empty($grade) && !empty($grade->finalgrade) && is_numeric($grade->finalgrade)) {
2023-08-25 09:44:34 +02:00
// Convert scale grades to corresponding scale name.
2023-08-24 23:02:41 +02:00
if (isset($scale)) {
2023-08-25 09:44:34 +02:00
// Get scale value.
$result->grade = $scale->get_nearest_item($grade->finalgrade);
2023-08-25 09:33:42 +02:00
} else {
2023-08-25 09:44:34 +02:00
// Round final grade to 1 decimal point.
2023-08-25 10:41:56 +02:00
$result->grade = round($grade->finalgrade, 1);
}
2023-08-24 23:02:41 +02:00
$result->feedback = trim($grade->feedback);
$result->pending = (new gradingscanner($gi))->pending($userid);
2023-08-24 23:09:20 +02:00
} else {
2023-08-24 23:02:41 +02:00
$result->grade = "-"; // Activity is gradable, but user did not receive a grade yet.
$result->feedback = null;
$result->pending = false;
}
return $result;
}
}
2023-08-24 23:02:41 +02:00
return null; // Activity cannot be graded (Shouldn't be happening, but still....).
}
/**
* Get the grade for a certain course module
* @return stdClass|null object containing 'grade' and optional 'feedback' attribute
*/
2023-08-24 23:02:41 +02:00
private function get_course_grade($userid) {
// TODO: Display grade in the way described in the course setup (with letters if needed).
2023-08-25 13:04:19 +02:00
$gi = grade_item::fetch(['itemtype' => 'course',
2023-08-24 23:02:41 +02:00
'iteminstance' => $this->course->id,
'courseid' => $this->course->id]);
2023-08-24 23:02:41 +02:00
if ($gi) {
// Only the following types of grade yield a result.
if (($gi->gradetype == GRADE_TYPE_VALUE || $gi->gradetype == GRADE_TYPE_SCALE)) {
$scale = $gi->load_scale();
2023-08-24 23:02:41 +02:00
$grade = $gi->get_final($userid); // Get the grade for the specified user.
// Check if the final grade is available and numeric (safety check).
if (!empty($grade) && !empty($grade->finalgrade) && is_numeric($grade->finalgrade)) {
2023-08-25 09:44:34 +02:00
// Convert scale grades to corresponding scale name.
2023-08-24 23:02:41 +02:00
if (isset($scale)) {
2023-08-25 09:44:34 +02:00
// Get scale value.
return $scale->get_nearest_item($grade->finalgrade);
2023-08-25 09:33:42 +02:00
} else {
2023-08-25 09:44:34 +02:00
// Round final grade to 1 decimal point.
2023-08-24 23:02:41 +02:00
return round($grade->finalgrade, 1);
}
2023-08-24 23:09:20 +02:00
} else {
2023-08-24 23:02:41 +02:00
return "-"; // User did not receive a grade yet for this course.
}
}
}
2023-08-24 23:02:41 +02:00
return null; // Course cannot be graded (Shouldn't be happening, but still....).
}
/**
* Returns the percentage completed by a certain user, returns null if no completion data is available.
*
* @param int $userid The id of the user, 0 for the current user
* @return null|float The percentage, or null if completion is not supported in the course,
* or there are no activities that support completion.
2023-08-24 23:02:41 +02:00
*/
2023-08-25 12:04:27 +02:00
public function get_progress_percentage($userid) {
// First, let's make sure completion is enabled.
if (!$this->completion->is_enabled()) {
return null;
}
if (!$this->completion->is_tracked_user($userid)) {
return null;
}
$completions = $this->completion->get_completions($userid);
$count = count($completions);
$completed = 0;
2023-08-24 23:02:41 +02:00
// Before we check how many modules have been completed see if the course has completed. .
if ($this->completion->is_course_complete($userid)) {
$completed = $count;
2023-08-24 23:09:20 +02:00
} else {
2023-08-25 09:44:34 +02:00
// Count all completions, but treat .
2023-08-24 23:02:41 +02:00
foreach ($completions as $completion) {
$crit = $completion->get_criteria();
2023-08-24 23:02:41 +02:00
if ($crit->criteriatype == COMPLETION_CRITERIA_TYPE_ACTIVITY) {
2023-08-25 09:44:34 +02:00
// Get the cm data object.
$cm = $this->modinfo->get_cm($crit->moduleinstance);
2023-08-25 09:44:34 +02:00
// Retrieve data for this object.
$data = $this->completion->get_data($cm, false, $userid);
// Count complete, but failed as incomplete too...
2023-08-25 11:52:05 +02:00
if (($data->completionstate == COMPLETION_INCOMPLETE)
|| ($data->completionstate == COMPLETION_COMPLETE_FAIL)) {
$completed += 0;
} else {
$completed += 1;
2023-08-24 23:02:41 +02:00
}
2023-08-24 23:09:20 +02:00
} else {
2023-08-24 23:02:41 +02:00
if ($completion->is_complete()) {
$completed += 1;
}
2023-08-24 23:02:41 +02:00
}
}
}
$result = new \stdClass;
$result->count = $count;
$result->completed = $completed;
$result->percentage = ($completed / $count) * 100;
2023-08-24 23:02:41 +02:00
return $result;
}
/**
* Returns the percentage completed by a certain user, returns null if no completion data is available.
*
* @param int $userid The id of the user, 0 for the current user
2023-07-04 23:38:08 +02:00
* @return null|\stdClass The percentage, or null if completion is not supported in the course,
* or there are no activities that support completion.
2023-08-24 23:02:41 +02:00
*/
2023-08-25 12:04:27 +02:00
public function get_advanced_progress_percentage($userid):\stdClass {
// First, let's make sure completion is enabled.
if (!$this->completion->is_enabled()) {
return null;
}
if (!$this->completion->is_tracked_user($userid)) {
return null;
}
$completions = $this->completion->get_completions($userid);
$aggregation = $this->completion->get_aggregation_method();
$critcount = [];
2023-08-24 23:02:41 +02:00
// Before we check how many modules have been completed see if the course has completed. .
2023-08-25 09:44:34 +02:00
// Count all completions, but treat .
2023-08-24 23:02:41 +02:00
foreach ($completions as $completion) {
2023-07-04 23:38:08 +02:00
$crit = $completion->get_criteria();
2023-08-24 23:02:41 +02:00
// Make a new object for the type if it's not already there.
2023-07-04 23:38:08 +02:00
$type = $crit->criteriatype;
2023-08-24 23:02:41 +02:00
if (!array_key_exists($type, $critcount)) {
2023-07-04 23:38:08 +02:00
$critcount[$type] = new \stdClass;
$critcount[$type]->count = 0;
$critcount[$type]->completed = 0;
$critcount[$type]->aggregation = $this->completion->get_aggregation_method($type);
}
2023-08-24 23:02:41 +02:00
// Get a reference to the counter object for this type.
2023-07-04 23:38:08 +02:00
$typecount =& $critcount[$type];
$typecount->count += 1;
2023-08-24 23:02:41 +02:00
if ($crit->criteriatype == COMPLETION_CRITERIA_TYPE_ACTIVITY) {
2023-08-25 09:44:34 +02:00
// Get the cm data object.
2023-07-04 23:38:08 +02:00
$cm = $this->modinfo->get_cm($crit->moduleinstance);
2023-08-25 09:44:34 +02:00
// Retrieve data for this object.
2023-07-04 23:38:08 +02:00
$data = $this->completion->get_data($cm, false, $userid);
// Count complete, but failed as incomplete too...
if (($data->completionstate == COMPLETION_INCOMPLETE) || ($data->completionstate == COMPLETION_COMPLETE_FAIL)) {
$typecount->completed += 0;
} else {
$typecount->completed += 1;
2023-08-24 23:02:41 +02:00
}
2023-08-24 23:09:20 +02:00
} else {
2023-08-24 23:02:41 +02:00
if ($completion->is_complete()) {
2023-07-04 23:38:08 +02:00
$typecount->completed += 1;
}
2023-08-24 23:02:41 +02:00
}
}
2023-08-24 23:02:41 +02:00
// Now that we have all completions sorted by type, we can be smart about how to do the count.
$count = 0;
$completed = 0;
2023-08-25 09:33:42 +02:00
$completionpercentage = 0;
2023-08-24 23:02:41 +02:00
foreach ($critcount as $c) {
// Take only types that are actually present into account.
if ($c->count > 0) {
// If the aggregation for the type is ANY, reduce the count to 1 for this type.
// And adjust the progress accordingly (check if any have been completed or not).
if ($c->aggregation == COMPLETION_AGGREGATION_ALL) {
$ct = $c->count;
$cmpl = $c->completed;
2023-08-24 23:09:20 +02:00
} else {
$ct = 1;
2023-08-25 12:16:51 +02:00
$cmpl = ($c->completed > 0) ? 1 : 0;
}
2023-08-25 09:44:34 +02:00
// If ANY completion for the types, count only the criteria type with the highest completion percentage -.
2023-08-24 23:02:41 +02:00
// Overwrite data if current type is more complete.
if ($aggregation == COMPLETION_AGGREGATION_ANY) {
2023-08-25 13:34:31 +02:00
$pct = $cmpl / $ct;
2023-08-25 09:33:42 +02:00
if ($pct > $completionpercentage) {
$count = $ct;
$completed = $cmpl;
2023-08-25 09:33:42 +02:00
$completionpercentage = $pct;
}
2023-08-25 13:34:31 +02:00
} else {
// If ALL completion for the types, add the count for this type to that of the others.
$count += $ct;
$completed += $cmpl;
2023-08-24 23:02:41 +02:00
// Don't really care about recalculating completion percentage every round in this case.
}
}
}
$result = new \stdClass;
$result->count = $count;
$result->completed = $completed;
2023-08-25 10:41:56 +02:00
$result->percentage = ($count > 0) ? (($completed / $count) * 100) : 0;
2023-08-24 23:02:41 +02:00
return $result;
}
}