2023-05-17 21:19:14 +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/>.
|
|
|
|
/**
|
2023-08-27 21:23:39 +02:00
|
|
|
* Collect, process and display information about gradable items
|
2023-08-24 23:02:41 +02:00
|
|
|
* @package local_treestudyplan
|
|
|
|
* @copyright 2023 P.M. Kuipers
|
|
|
|
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
2023-05-17 21:19:14 +02:00
|
|
|
|
|
|
|
namespace local_treestudyplan;
|
2023-08-25 12:04:27 +02:00
|
|
|
defined('MOODLE_INTERNAL') || die();
|
|
|
|
|
2023-05-17 21:19:14 +02:00
|
|
|
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;
|
2023-08-27 21:23:39 +02:00
|
|
|
use core_course\local\entity\content_item;
|
2024-06-02 18:47:23 +02:00
|
|
|
use grade_item;
|
|
|
|
use grade_scale;
|
|
|
|
use grade_outcome;
|
|
|
|
use core_plugin_manager;
|
2023-08-27 21:23:39 +02:00
|
|
|
/**
|
|
|
|
* Collect, process and display information about gradable items
|
|
|
|
*/
|
2023-05-17 21:19:14 +02:00
|
|
|
class gradeinfo {
|
2023-08-27 21:23:39 +02:00
|
|
|
/** @var studyitem */
|
2023-05-17 21:19:14 +02:00
|
|
|
private $studyitem = null;
|
2023-08-27 21:23:39 +02:00
|
|
|
/** @var int */
|
2023-05-17 21:19:14 +02:00
|
|
|
private $id;
|
2023-08-27 21:23:39 +02:00
|
|
|
/** @var grade_item */
|
2023-05-17 21:19:14 +02:00
|
|
|
private $gradeitem;
|
2023-08-24 23:02:41 +02:00
|
|
|
|
2023-08-27 21:23:39 +02:00
|
|
|
/** @var string */
|
2023-05-17 21:19:14 +02:00
|
|
|
private $icon;
|
2023-08-27 21:23:39 +02:00
|
|
|
/** @var string */
|
2023-05-17 21:19:14 +02:00
|
|
|
private $link;
|
2023-08-27 21:23:39 +02:00
|
|
|
/** @var string */
|
2023-05-17 21:19:14 +02:00
|
|
|
private $gradinglink;
|
2023-08-27 21:23:39 +02:00
|
|
|
/** @var grade_scale*/
|
2023-05-17 21:19:14 +02:00
|
|
|
private $scale;
|
2023-08-27 21:23:39 +02:00
|
|
|
/** @var grade_outcome*/
|
2023-05-17 21:19:14 +02:00
|
|
|
private $outcome;
|
2023-08-27 21:23:39 +02:00
|
|
|
/** @var bool*/
|
2023-05-17 21:19:14 +02:00
|
|
|
private $hidden = false;
|
2023-08-27 21:23:39 +02:00
|
|
|
/** @var string */
|
2023-05-17 21:19:14 +02:00
|
|
|
private $name;
|
2023-08-27 21:23:39 +02:00
|
|
|
/** @var string */
|
2023-05-17 21:19:14 +02:00
|
|
|
private $typename;
|
2023-08-27 21:23:39 +02:00
|
|
|
/** @var int */
|
2023-05-17 21:19:14 +02:00
|
|
|
private $section;
|
2023-08-27 21:23:39 +02:00
|
|
|
/** @var int */
|
2023-05-17 21:19:14 +02:00
|
|
|
private $sectionorder;
|
2023-08-27 21:23:39 +02:00
|
|
|
/** @var int */
|
2023-05-17 21:19:14 +02:00
|
|
|
private $cmid;
|
2023-08-27 21:23:39 +02:00
|
|
|
/** @var int */
|
2023-05-17 21:19:14 +02:00
|
|
|
private $coursesort;
|
|
|
|
|
2023-08-27 21:23:39 +02:00
|
|
|
/** @var array */
|
2023-05-17 21:19:14 +02:00
|
|
|
private static $contentitems = null;
|
2023-08-27 21:23:39 +02:00
|
|
|
/** @var gradingscanner */
|
2023-05-17 21:19:14 +02:00
|
|
|
private $gradingscanner;
|
|
|
|
|
2023-08-27 21:23:39 +02:00
|
|
|
/** @var array */
|
2023-05-17 21:19:14 +02:00
|
|
|
private static $sections = [];
|
|
|
|
|
2023-08-27 21:23:39 +02:00
|
|
|
/**
|
|
|
|
* Get the sequence of activities for a given section id
|
|
|
|
* @param mixed $sectionid Id of section
|
|
|
|
* @return int[] Sequence of cms in a section
|
|
|
|
*/
|
2023-08-25 17:33:20 +02:00
|
|
|
protected static function get_sectionsequence($sectionid) {
|
2023-05-17 21:19:14 +02:00
|
|
|
global $DB;
|
2023-08-24 23:02:41 +02:00
|
|
|
if (!array_key_exists($sectionid, self::$sections)) {
|
2024-06-03 23:24:16 +02:00
|
|
|
self::$sections[$sectionid] = explode(",", $DB->get_field("course_sections", "sequence", ["id" => $sectionid]));
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
return self::$sections[$sectionid];
|
|
|
|
}
|
|
|
|
|
2023-08-27 21:23:39 +02:00
|
|
|
/**
|
|
|
|
* Get the grade_item
|
2023-08-27 22:20:17 +02:00
|
|
|
* @return grade_item
|
2023-08-27 21:23:39 +02:00
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public function get_gradeitem(): grade_item {
|
2023-05-17 21:19:14 +02:00
|
|
|
return $this->gradeitem;
|
|
|
|
}
|
2023-08-27 21:23:39 +02:00
|
|
|
/**
|
|
|
|
* Get the gradingscanner
|
|
|
|
* @return gradingscanner
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public function get_gradingscanner(): gradingscanner {
|
2023-05-17 21:19:14 +02:00
|
|
|
return $this->gradingscanner;
|
|
|
|
}
|
|
|
|
|
2023-08-27 21:23:39 +02:00
|
|
|
/**
|
|
|
|
* Get the grade's scale if applicable
|
|
|
|
* @return grade_scale|null
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public function get_scale(): ?grade_scale {
|
2023-05-17 21:19:14 +02:00
|
|
|
return $this->scale;
|
|
|
|
}
|
|
|
|
|
2023-08-27 21:23:39 +02:00
|
|
|
/**
|
|
|
|
* Get content items (activity icons) from the repository
|
|
|
|
* @return content_item[]
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
protected static function get_contentitems(): array {
|
2023-05-17 21:19:14 +02:00
|
|
|
global $PAGE;
|
2023-08-24 23:02:41 +02:00
|
|
|
if (empty(static::$contentitems)) {
|
2024-06-12 21:12:00 +02:00
|
|
|
if (empty($PAGE->context)) {
|
2024-06-03 23:24:16 +02:00
|
|
|
$PAGE->set_context(\context_system::instance());
|
|
|
|
}
|
2023-05-17 21:19:14 +02:00
|
|
|
static::$contentitems = (new content_item_readonly_repository())->find_all();
|
|
|
|
}
|
|
|
|
return static::$contentitems;
|
|
|
|
}
|
|
|
|
|
2023-08-27 21:23:39 +02:00
|
|
|
/**
|
|
|
|
* Get specific contentitem (activity icons) by name
|
|
|
|
* @param mixed $name Name of content item
|
|
|
|
* @return content_item|null
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public static function get_contentitem($name): ?content_item {
|
2023-05-17 21:19:14 +02:00
|
|
|
$contentitems = static::get_contentitems();
|
2023-08-25 10:41:56 +02:00
|
|
|
for ($i = 0; $i < count($contentitems); $i++) {
|
2023-08-24 23:02:41 +02:00
|
|
|
if ($contentitems[$i]->get_name() == $name) {
|
2023-05-17 21:19:14 +02:00
|
|
|
return $contentitems[$i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-08-27 21:23:39 +02:00
|
|
|
/**
|
|
|
|
* Get a specific course context from grade item id
|
|
|
|
* @param int $id Grade item id
|
2023-08-27 23:27:07 +02:00
|
|
|
* @return \context_course
|
2023-08-27 21:23:39 +02:00
|
|
|
* @throws InvalidArgumentException if grade id is not found
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public static function get_coursecontext_by_id($id): \context_course {
|
2023-05-17 21:19:14 +02:00
|
|
|
$gi = grade_item::fetch(["id" => $id]);
|
2023-08-24 23:02:41 +02:00
|
|
|
if (!$gi || course_module_instance_pending_deletion($gi->courseid, $gi->itemmodule, $gi->iteminstance)) {
|
2023-08-24 23:09:20 +02:00
|
|
|
throw new \InvalidArgumentException ("Grade {$id} not found in database");
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
return \context_course::instance($gi->courseid);;
|
|
|
|
}
|
|
|
|
|
2023-08-27 21:23:39 +02:00
|
|
|
/**
|
2023-08-27 22:20:17 +02:00
|
|
|
* Create new object around a grade_item
|
2023-08-27 21:23:39 +02:00
|
|
|
* @param int $id Grade item id of the grade item to use as base
|
|
|
|
* @param studyitem|null $studyitem Studyitem containg the course that references this grade
|
|
|
|
*/
|
2024-06-03 23:24:16 +02:00
|
|
|
public function __construct($id, ?studyitem $studyitem = null) {
|
2023-05-17 21:19:14 +02:00
|
|
|
global $DB;
|
|
|
|
$this->studyitem = $studyitem;
|
|
|
|
|
|
|
|
$gi = grade_item::fetch(["id" => $id]);
|
2023-08-24 23:02:41 +02:00
|
|
|
if (!$gi || course_module_instance_pending_deletion($gi->courseid, $gi->itemmodule, $gi->iteminstance)) {
|
2023-08-24 23:09:20 +02:00
|
|
|
throw new \InvalidArgumentException ("Grade {$id} not found in database");
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
$this->id = $id;
|
|
|
|
$this->gradeitem = $gi;
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
// Determine the icon for the associated activity.
|
2023-05-17 21:19:14 +02:00
|
|
|
$contentitem = static::get_contentitem($gi->itemmodule);
|
2024-06-02 23:23:32 +02:00
|
|
|
$this->icon = empty($contentitem) ? "" : $contentitem->get_icon();
|
2023-05-17 21:19:14 +02:00
|
|
|
|
2023-10-07 23:19:54 +02:00
|
|
|
$this->scale = $gi->load_scale();
|
|
|
|
$this->outcome = $gi->load_outcome();
|
|
|
|
|
|
|
|
$this->hidden = ($gi->hidden || (!empty($outcome) && $outcome->hidden)) ? true : false;
|
|
|
|
|
|
|
|
$this->name = empty($outcome) ? $gi->itemname : $outcome->name;
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
// Determine a link to the associated activity.
|
|
|
|
if ($gi->itemtype != "mod" || empty($gi->itemmodule) || empty($gi->iteminstance)) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$this->link = "";
|
|
|
|
$this->cmid = 0;
|
|
|
|
$this->section = 0;
|
|
|
|
$this->sectionorder = 0;
|
2023-08-24 23:09:20 +02:00
|
|
|
} else {
|
2023-08-24 23:02:41 +02:00
|
|
|
list($c, $cminfo) = get_course_and_cm_from_instance($gi->iteminstance, $gi->itemmodule);
|
2023-05-17 21:19:14 +02:00
|
|
|
$this->cmid = $cminfo->id;
|
2023-08-25 09:44:34 +02:00
|
|
|
// Sort by position in course.
|
2023-08-24 23:02:41 +02:00
|
|
|
// .
|
2023-05-17 21:19:14 +02:00
|
|
|
$this->section = $cminfo->sectionnum;
|
2023-08-25 17:33:20 +02:00
|
|
|
$ssequence = self::get_sectionsequence($cminfo->section);
|
2023-08-24 23:02:41 +02:00
|
|
|
$this->sectionorder = array_search($cminfo->id, $ssequence);
|
2023-05-17 21:19:14 +02:00
|
|
|
|
2023-10-07 23:19:54 +02:00
|
|
|
$this->name = $cminfo->get_formatted_name();
|
|
|
|
|
2023-05-17 21:19:14 +02:00
|
|
|
$this->link = "/mod/{$gi->itemmodule}/view.php?id={$cminfo->id}";
|
2023-08-24 23:02:41 +02:00
|
|
|
if ($gi->itemmodule == 'quiz') {
|
2023-05-17 21:19:14 +02:00
|
|
|
$this->gradinglink = "/mod/{$gi->itemmodule}/report.php?id={$cminfo->id}&mode=grading";
|
2023-08-24 23:09:20 +02:00
|
|
|
} else if ($gi->itemmodule == "assign") {
|
2023-05-17 21:19:14 +02:00
|
|
|
$this->gradinglink = $this->link ."&action=grading";
|
2023-08-24 23:09:20 +02:00
|
|
|
} else {
|
2023-05-17 21:19:14 +02:00
|
|
|
$this->gradinglink = $this->link;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-25 10:41:56 +02:00
|
|
|
$this->typename = empty($contentitem) ? $gi->itemmodule : $contentitem->get_title()->get_value();
|
2023-05-17 21:19:14 +02:00
|
|
|
$this->gradingscanner = new gradingscanner($gi);
|
|
|
|
|
|
|
|
$this->coursesort = $this->section * 1000 + $this->sectionorder;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-08-27 21:23:39 +02:00
|
|
|
/**
|
|
|
|
* Check if this gradable item is selected in the studyitem
|
|
|
|
* @return bool
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public function is_selected(): bool {
|
2023-05-17 21:19:14 +02:00
|
|
|
global $DB;
|
2023-08-24 23:02:41 +02:00
|
|
|
if ($this->studyitem) {
|
|
|
|
// Check if selected for this studyitem.
|
2023-08-25 11:52:05 +02:00
|
|
|
$r = $DB->get_record('local_treestudyplan_gradeinc',
|
|
|
|
['studyitem_id' => $this->studyitem->id(), 'grade_item_id' => $this->gradeitem->id]);
|
2023-08-24 23:02:41 +02:00
|
|
|
if ($r && $r->include) {
|
2023-05-17 21:19:14 +02:00
|
|
|
return(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return(false);
|
|
|
|
}
|
|
|
|
|
2023-08-27 21:23:39 +02:00
|
|
|
/**
|
|
|
|
* Check if this gradable item is marked required in the studyitem
|
|
|
|
* @return bool
|
|
|
|
*/
|
2023-08-24 23:02:41 +02:00
|
|
|
public function is_required() {
|
2023-05-17 21:19:14 +02:00
|
|
|
global $DB;
|
2023-08-24 23:02:41 +02:00
|
|
|
if ($this->studyitem) {
|
|
|
|
// Check if selected for this studyitem.
|
2023-08-25 11:52:05 +02:00
|
|
|
$r = $DB->get_record('local_treestudyplan_gradeinc',
|
|
|
|
['studyitem_id' => $this->studyitem->id(), 'grade_item_id' => $this->gradeitem->id]);
|
2023-08-24 23:02:41 +02:00
|
|
|
if ($r && $r->include && $r->required) {
|
2023-05-17 21:19:14 +02:00
|
|
|
return(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return(false);
|
|
|
|
}
|
|
|
|
|
2023-08-27 21:23:39 +02:00
|
|
|
/**
|
|
|
|
* Webservice structure for editor info
|
|
|
|
* @param int $value Webservice requirement constant
|
|
|
|
*/
|
2023-08-27 21:57:21 +02:00
|
|
|
/**
|
|
|
|
* Webservice structure for editor info
|
|
|
|
* @param int $value Webservice requirement constant
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public static function editor_structure($value = VALUE_REQUIRED): \external_description {
|
2023-05-17 21:19:14 +02:00
|
|
|
return new \external_single_structure([
|
|
|
|
"id" => new \external_value(PARAM_INT, 'grade_item id'),
|
|
|
|
"cmid" => new \external_value(PARAM_INT, 'course module id'),
|
2023-10-07 23:19:54 +02:00
|
|
|
"name" => new \external_value(PARAM_RAW, 'grade item name'),
|
2023-05-17 21:19:14 +02:00
|
|
|
"typename" => new \external_value(PARAM_TEXT, 'grade item type name'),
|
2023-08-25 10:41:56 +02:00
|
|
|
"outcome" => new \external_value(PARAM_BOOL, 'is outcome'),
|
|
|
|
"selected" => new \external_value(PARAM_BOOL, 'is selected for current studyitem'),
|
2023-05-17 21:19:14 +02:00
|
|
|
"icon" => new \external_value(PARAM_RAW, 'html for icon of related activity'),
|
2023-10-07 23:19:54 +02:00
|
|
|
"link" => new \external_value(PARAM_RAW, 'link to related activity'),
|
|
|
|
"gradinglink" => new \external_value(PARAM_RAW, 'link to related activity'),
|
2023-05-17 21:19:14 +02:00
|
|
|
"grading" => gradingscanner::structure(),
|
2023-08-25 10:41:56 +02:00
|
|
|
"required" => new \external_value(PARAM_BOOL, 'is required for current studyitem'),
|
2023-08-24 23:02:41 +02:00
|
|
|
], 'referenced course information', $value);
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
|
2023-08-27 21:23:39 +02:00
|
|
|
/**
|
|
|
|
* Webservice model for editor info
|
2024-06-03 23:24:16 +02:00
|
|
|
* @param studyitem|null $studyitem Related studyitem to check for
|
2023-08-27 21:23:39 +02:00
|
|
|
* @return array Webservice data model
|
|
|
|
*/
|
2024-06-03 23:24:16 +02:00
|
|
|
public function editor_model(?studyitem $studyitem = null) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$model = [
|
|
|
|
"id" => $this->id,
|
|
|
|
"cmid" => $this->cmid,
|
|
|
|
"name" => $this->name,
|
|
|
|
"typename" => $this->typename,
|
|
|
|
"outcome" => isset($this->outcome),
|
|
|
|
"selected" => $this->is_selected(),
|
|
|
|
"icon" => $this->icon,
|
|
|
|
"link" => $this->link,
|
|
|
|
"gradinglink" => $this->gradinglink,
|
2023-08-25 10:41:56 +02:00
|
|
|
"required" => $this->is_required(),
|
2023-05-17 21:19:14 +02:00
|
|
|
];
|
2023-06-16 23:12:17 +02:00
|
|
|
// Unfortunately, lazy loading of the completion data is off, since we need the data to show study item completion...
|
2023-08-25 11:52:05 +02:00
|
|
|
if ($studyitem !== null
|
|
|
|
&& $this->is_selected()
|
|
|
|
&& has_capability('local/treestudyplan:viewuserreports', $studyitem->studyline()->studyplan()->context())
|
2023-08-24 23:02:41 +02:00
|
|
|
&& $this->gradingscanner->is_available()) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$model['grading'] = $this->gradingscanner->model();
|
|
|
|
}
|
|
|
|
return $model;
|
|
|
|
}
|
|
|
|
|
2023-08-27 21:23:39 +02:00
|
|
|
/**
|
|
|
|
* Webservice structure for user info
|
|
|
|
* @param int $value Webservice requirement constant
|
|
|
|
*/
|
2023-08-27 21:57:21 +02:00
|
|
|
/**
|
|
|
|
* Webservice structure for userinfo
|
|
|
|
* @param int $value Webservice requirement constant
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public static function user_structure($value = VALUE_REQUIRED): \external_description {
|
2023-05-17 21:19:14 +02:00
|
|
|
return new \external_single_structure([
|
|
|
|
"id" => new \external_value(PARAM_INT, 'grade_item id'),
|
|
|
|
"cmid" => new \external_value(PARAM_INT, 'course module id'),
|
2023-10-07 23:19:54 +02:00
|
|
|
"name" => new \external_value(PARAM_RAW, 'grade item name'),
|
2023-05-17 21:19:14 +02:00
|
|
|
"typename" => new \external_value(PARAM_TEXT, 'grade item type name'),
|
2023-10-07 23:19:54 +02:00
|
|
|
"grade" => new \external_value(PARAM_TEXT, 'grade value'),
|
2023-05-17 21:19:14 +02:00
|
|
|
"gradetype" => new \external_value(PARAM_TEXT, 'grade type (completion|grade)'),
|
|
|
|
"feedback" => new \external_value(PARAM_RAW, 'html for feedback'),
|
2023-08-25 10:41:56 +02:00
|
|
|
"completion" => new \external_value(PARAM_TEXT, 'completion state (incomplete|progress|completed|excellent)'),
|
2023-05-17 21:19:14 +02:00
|
|
|
"icon" => new \external_value(PARAM_RAW, 'html for icon of related activity'),
|
2023-10-07 23:19:54 +02:00
|
|
|
"link" => new \external_value(PARAM_RAW, 'link to related activity'),
|
2023-08-25 10:41:56 +02:00
|
|
|
"pendingsubmission" => new \external_value(PARAM_BOOL, 'is selected for current studyitem', VALUE_OPTIONAL),
|
|
|
|
"required" => new \external_value(PARAM_BOOL, 'is required for current studyitem'),
|
|
|
|
"selected" => new \external_value(PARAM_BOOL, 'is selected for current studyitem'),
|
2023-08-24 23:02:41 +02:00
|
|
|
], 'referenced course information', $value);
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
|
2023-08-27 21:23:39 +02:00
|
|
|
/**
|
|
|
|
* Webservice model for user info
|
2023-08-27 22:20:17 +02:00
|
|
|
* @param int $userid ID of user to check specific info for
|
2023-08-27 21:23:39 +02:00
|
|
|
* @return array Webservice data model
|
|
|
|
*/
|
2023-05-17 21:19:14 +02:00
|
|
|
public function user_model($userid) {
|
|
|
|
global $DB;
|
|
|
|
$grade = $this->gradeitem->get_final($userid);
|
2024-06-02 23:23:32 +02:00
|
|
|
// Format grade for proper display.
|
2024-06-02 19:23:40 +02:00
|
|
|
if (is_object($grade)) {
|
|
|
|
$finalgrade = \grade_format_gradevalue($grade->finalgrade, $this->gradeitem, true, null, 1);
|
2024-05-31 20:41:18 +02:00
|
|
|
} else {
|
2024-06-02 19:23:40 +02:00
|
|
|
$finalgrade = \grade_format_gradevalue(0, $this->gradeitem, true, null, 1);
|
2024-05-31 20:41:18 +02:00
|
|
|
}
|
2023-05-17 21:19:14 +02:00
|
|
|
|
2023-08-25 09:44:34 +02:00
|
|
|
// Retrieve the aggregator and determine completion.
|
2023-08-24 23:02:41 +02:00
|
|
|
if (!isset($this->studyitem)) {
|
2023-05-17 21:19:14 +02:00
|
|
|
throw new \UnexpectedValueException("Study item not set (null) for gradeinfo in report mode");
|
|
|
|
}
|
2023-07-23 16:25:08 +02:00
|
|
|
$aggregator = $this->studyitem->studyline()->studyplan()->aggregator();
|
2023-08-24 23:02:41 +02:00
|
|
|
$completion = $aggregator->grade_completion($this, $userid);
|
2023-05-17 21:19:14 +02:00
|
|
|
|
|
|
|
$model = [
|
|
|
|
"id" => $this->id,
|
|
|
|
"cmid" => $this->cmid,
|
|
|
|
"name" => $this->name,
|
|
|
|
"typename" => $this->typename,
|
|
|
|
"grade" => $finalgrade,
|
2024-06-02 23:23:32 +02:00
|
|
|
"gradetype" => isset($this->scale) ? "completion" : "grade",
|
|
|
|
"feedback" => empty($grade) ? null : $grade->feedback,
|
2023-05-17 21:19:14 +02:00
|
|
|
"completion" => completion::label($completion),
|
|
|
|
"icon" => $this->icon,
|
|
|
|
"link" => $this->link,
|
|
|
|
"pendingsubmission" => $this->gradingscanner->pending($userid),
|
2023-08-25 10:41:56 +02:00
|
|
|
"required" => $this->is_required(),
|
2023-05-17 21:19:14 +02:00
|
|
|
"selected" => $this->is_selected(),
|
|
|
|
];
|
|
|
|
|
|
|
|
return $model;
|
|
|
|
}
|
|
|
|
|
2023-08-27 21:23:39 +02:00
|
|
|
/**
|
|
|
|
* Export essential information for export
|
|
|
|
* @return array information model
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public function export_model(): array {
|
2023-05-17 21:19:14 +02:00
|
|
|
return [
|
|
|
|
"name" => $this->name,
|
|
|
|
"type" => $this->gradeitem->itemmodule,
|
|
|
|
"selected" => $this->is_selected(),
|
2023-08-25 10:41:56 +02:00
|
|
|
"required" => $this->is_required(),
|
2023-05-17 21:19:14 +02:00
|
|
|
];
|
|
|
|
}
|
2023-08-27 21:23:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Import data from exported model into database
|
|
|
|
* @param studyitem $item Studyitem related to this gradable
|
|
|
|
* @param array $model Model data previously exported
|
|
|
|
*/
|
2023-08-24 23:02:41 +02:00
|
|
|
public static function import(studyitem $item, array $model) {
|
|
|
|
if ($item->type() == studyitem::COURSE) {
|
2023-08-25 09:33:42 +02:00
|
|
|
$courseid = $item->courseid();
|
2023-08-25 13:04:19 +02:00
|
|
|
$gradeitems = grade_item::fetch_all(['itemtype' => 'mod', 'courseid' => $courseid]);
|
2023-08-24 23:02:41 +02:00
|
|
|
foreach ($gradeitems as $gi) {
|
2023-08-25 10:41:56 +02:00
|
|
|
$giname = empty($outcome) ? $gi->itemname : $outcome->name;
|
2023-08-25 09:33:42 +02:00
|
|
|
$gitype = $gi->itemmodule;
|
2023-05-17 21:19:14 +02:00
|
|
|
|
2023-08-25 09:33:42 +02:00
|
|
|
if ($giname == $model["name"] && $gitype == $model["type"]) {
|
2023-08-25 09:44:34 +02:00
|
|
|
// We have a match.
|
2023-08-25 10:41:56 +02:00
|
|
|
if (!isset($model["selected"])) {
|
|
|
|
$model["selected"] = true;
|
|
|
|
}
|
|
|
|
if (!isset($model["required"])) {
|
|
|
|
$model["required"] = false;
|
|
|
|
}
|
2023-08-24 23:02:41 +02:00
|
|
|
if ($model["selected"] || $model["required"]) {
|
|
|
|
static::include_grade($gi->id, $item->id(), $model["selected"], $model["required"]);
|
|
|
|
}
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
}
|
2023-08-24 23:02:41 +02:00
|
|
|
}
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
|
2023-08-27 21:23:39 +02:00
|
|
|
/**
|
|
|
|
* Get a list if all gradable activities in a given course
|
|
|
|
* @param \stdClass $course Course database record
|
|
|
|
* @param studyitem|null $studyitem Studyitem linked to the course which can be linked to created gradeinfo objects
|
2023-08-28 08:51:52 +02:00
|
|
|
* @return gradeinfo[] Array of gradeinfo
|
2023-08-27 21:23:39 +02:00
|
|
|
*/
|
2024-06-03 23:24:16 +02:00
|
|
|
public static function list_course_gradables($course, ?studyitem $studyitem = null): array {
|
2023-05-17 21:19:14 +02:00
|
|
|
$list = [];
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
if (method_exists("\course_modinfo", "get_array_of_activities")) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$activities = \course_modinfo::get_array_of_activities($course);
|
|
|
|
} else {
|
2023-08-24 23:02:41 +02:00
|
|
|
// Deprecated in Moodle 4.0+, but not yet available in Moodle 3.11.
|
2023-05-17 21:19:14 +02:00
|
|
|
$activities = get_array_of_activities($course->id);
|
|
|
|
}
|
2023-08-24 23:02:41 +02:00
|
|
|
foreach ($activities as $act) {
|
|
|
|
if ($act->visible) {
|
2023-08-25 13:04:19 +02:00
|
|
|
$gradeitems = grade_item::fetch_all(['itemtype' => 'mod',
|
2023-08-25 11:52:05 +02:00
|
|
|
'itemmodule' => $act->mod,
|
|
|
|
'iteminstance' => $act->id,
|
|
|
|
'courseid' => $course->id]);
|
2023-08-24 23:02:41 +02:00
|
|
|
if (!empty($gradeitems)) {
|
|
|
|
foreach ($gradeitems as $gi) {
|
|
|
|
if (($gi->gradetype == GRADE_TYPE_VALUE || $gi->gradetype == GRADE_TYPE_SCALE)) {
|
2023-05-17 21:19:14 +02:00
|
|
|
try {
|
2023-08-24 23:02:41 +02:00
|
|
|
$gradable = new static($gi->id, $studyitem);
|
2023-05-17 21:19:14 +02:00
|
|
|
$list[] = $gradable;
|
2023-08-25 10:41:56 +02:00
|
|
|
} catch (\InvalidArgumentException $x) {
|
2023-08-25 17:33:20 +02:00
|
|
|
// Pass and do not add to the list if an error occurs.
|
|
|
|
$gradable = null; // Clean up gradable variable.
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
usort($list, function($a, $b) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$course = $a->coursesort <=> $b->coursesort;
|
2023-08-25 10:41:56 +02:00
|
|
|
return ($course != 0) ? $course : $a->gradeitem->sortorder <=> $b->gradeitem->sortorder;
|
2023-05-17 21:19:14 +02:00
|
|
|
});
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
2023-08-27 21:23:39 +02:00
|
|
|
/**
|
|
|
|
* List all gradables enabled for a given study item
|
|
|
|
* @param studyitem $studyitem The studyitem to search for
|
2023-08-28 08:51:52 +02:00
|
|
|
* @return gradeinfo[] Array of gradeinfo
|
2023-08-27 21:23:39 +02:00
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public static function list_studyitem_gradables(studyitem $studyitem): array {
|
2023-05-17 21:19:14 +02:00
|
|
|
global $DB;
|
|
|
|
$table = 'local_treestudyplan_gradeinc';
|
|
|
|
$list = [];
|
2023-08-24 23:02:41 +02:00
|
|
|
$records = $DB->get_records($table, ['studyitem_id' => $studyitem->id()]);
|
|
|
|
foreach ($records as $r) {
|
|
|
|
if (isset($r->grade_item_id)) {
|
2023-05-17 21:19:14 +02:00
|
|
|
try {
|
2023-08-24 23:02:41 +02:00
|
|
|
if ($r->include || $r->required) {
|
|
|
|
$list[] = new static($r->grade_item_id, $studyitem);
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
2023-08-25 10:41:56 +02:00
|
|
|
} catch (\InvalidArgumentException $x) {
|
2023-08-25 09:44:34 +02:00
|
|
|
// On InvalidArgumentException, the grade_item id can no longer be found.
|
2023-08-24 23:02:41 +02:00
|
|
|
// Remove the link to avoid database record hogging.
|
2023-05-17 21:19:14 +02:00
|
|
|
$DB->delete_records($table, ['id' => $r->id]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
usort($list, function($a, $b) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$course = $a->coursesort <=> $b->coursesort;
|
2023-08-25 10:41:56 +02:00
|
|
|
return ($course != 0) ? $course : $a->gradeitem->sortorder <=> $b->gradeitem->sortorder;
|
2023-05-17 21:19:14 +02:00
|
|
|
});
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
2023-08-27 21:23:39 +02:00
|
|
|
/**
|
|
|
|
* Webservice executor to include grade with studyitem or not.
|
|
|
|
* if both $inclue and $required are false, any existing DB record will be removed
|
|
|
|
* @param int $gradeid ID of the grade_item
|
|
|
|
* @param int $itemid ID of the study item
|
|
|
|
* @param bool $include Select grade_item for studyitem or not
|
|
|
|
* @param bool $required Mark grade_item as required or not
|
2023-08-27 22:20:17 +02:00
|
|
|
* @return success Always returns successful success object
|
2023-08-27 21:23:39 +02:00
|
|
|
*/
|
2023-08-25 13:04:19 +02:00
|
|
|
public static function include_grade(int $gradeid, int $itemid, bool $include, bool $required = false) {
|
2023-05-17 21:19:14 +02:00
|
|
|
global $DB;
|
|
|
|
$table = 'local_treestudyplan_gradeinc';
|
2023-08-24 23:02:41 +02:00
|
|
|
if ($include) {
|
2023-08-25 09:44:34 +02:00
|
|
|
// Make sure a record exits.
|
2023-08-25 09:33:42 +02:00
|
|
|
$r = $DB->get_record($table, ['studyitem_id' => $itemid, 'grade_item_id' => $gradeid]);
|
2023-08-24 23:02:41 +02:00
|
|
|
if ($r) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$r->include = 1;
|
2024-06-02 23:23:32 +02:00
|
|
|
$r->required = boolval($required) ? 1 : 0;
|
2023-05-17 21:19:14 +02:00
|
|
|
$id = $DB->update_record($table, $r);
|
|
|
|
} else {
|
|
|
|
$DB->insert_record($table, [
|
2023-08-25 09:33:42 +02:00
|
|
|
'studyitem_id' => $itemid,
|
|
|
|
'grade_item_id' => $gradeid,
|
2023-08-24 23:02:41 +02:00
|
|
|
'include' => 1,
|
2024-06-02 23:23:32 +02:00
|
|
|
'required' => boolval($required) ? 1 : 0 ]
|
2023-05-17 21:19:14 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
2023-08-25 09:44:34 +02:00
|
|
|
// Remove if it should not be included.
|
2023-08-25 09:33:42 +02:00
|
|
|
$r = $DB->get_record($table, ['studyitem_id' => $itemid, 'grade_item_id' => $gradeid]);
|
2023-08-24 23:02:41 +02:00
|
|
|
if ($r) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$DB->delete_records($table, ['id' => $r->id]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return success::success();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|