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/>.
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @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;
|
|
|
|
use \grade_item;
|
|
|
|
use \grade_scale;
|
|
|
|
use \grade_outcome;
|
|
|
|
|
|
|
|
class courseinfo {
|
|
|
|
const TABLE = 'course';
|
2023-08-24 23:02:41 +02:00
|
|
|
|
2023-05-17 21:19:14 +02:00
|
|
|
private $course;
|
|
|
|
private $context;
|
2023-06-30 12:14:11 +02:00
|
|
|
private $coursecontext;
|
2023-05-17 21:19:14 +02:00
|
|
|
private $studyitem;
|
|
|
|
private static $contentitems = null;
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
public function id() {
|
2023-05-17 21:19:14 +02:00
|
|
|
return $this->course->id;
|
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
public function shortname() {
|
2023-05-17 21:19:14 +02:00
|
|
|
return $this->course->shortname;
|
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
public function course() {
|
2023-08-25 10:41:56 +02:00
|
|
|
return $this->course; // Php arrays are assigned by copy.
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
public function course_context() {
|
2023-05-17 21:19:14 +02:00
|
|
|
return $this->coursecontext;
|
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
public function category_context() {
|
2023-05-17 21:19:14 +02:00
|
|
|
return $this->context;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function get_contentitems() {
|
|
|
|
global $PAGE;
|
2023-08-24 23:02:41 +02:00
|
|
|
if (empty(static::$contentitems)) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$PAGE->set_context(\context_system::instance());
|
|
|
|
static::$contentitems = (new content_item_readonly_repository())->find_all();
|
|
|
|
}
|
|
|
|
return static::$contentitems;
|
|
|
|
}
|
|
|
|
|
2023-08-25 17:33:20 +02:00
|
|
|
protected function am_teacher() {
|
2023-05-17 21:19:14 +02:00
|
|
|
global $USER;
|
|
|
|
return is_enrolled($this->coursecontext, $USER, 'mod/assign:grade');
|
|
|
|
}
|
|
|
|
|
2023-08-25 17:33:20 +02:00
|
|
|
protected function i_can_select_gradables($userid = -1) {
|
2023-05-17 21:19:14 +02:00
|
|
|
global $USER, $DB;
|
2023-08-24 23:02:41 +02:00
|
|
|
if ($userid <= 0) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$usr = $USER;
|
2023-08-25 09:33:42 +02:00
|
|
|
} else {
|
2023-05-17 21:19:14 +02:00
|
|
|
$usr = $DB->get_record('user', ['id' => $userid, 'deleted' => 0]);
|
|
|
|
}
|
|
|
|
return($usr && is_enrolled($this->coursecontext, $usr, 'local/treestudyplan:selectowngradables'));
|
2023-08-24 23:02:41 +02:00
|
|
|
}
|
2023-05-17 21:19:14 +02:00
|
|
|
|
|
|
|
public static function get_contentitem($name) {
|
|
|
|
$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];
|
|
|
|
}
|
|
|
|
}
|
2023-08-24 23:02:41 +02:00
|
|
|
return null;
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
public function __construct($id, studyitem $studyitem = null) {
|
2023-05-17 21:19:14 +02:00
|
|
|
global $DB;
|
|
|
|
$this->studyitem = $studyitem;
|
|
|
|
$this->course = \get_course($id);
|
|
|
|
$this->context = \context_coursecat::instance($this->course->category);
|
|
|
|
$this->coursecontext = \context_course::instance($this->course->id);
|
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
public static function exists($id) {
|
2023-05-17 21:19:14 +02:00
|
|
|
global $DB;
|
|
|
|
return is_numeric($id) && $DB->record_exists(self::TABLE, ['id' => $id]);
|
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
public static function id_from_shortname($shortname) {
|
2023-05-17 21:19:14 +02:00
|
|
|
global $DB;
|
2023-08-24 23:02:41 +02:00
|
|
|
|
2023-05-17 21:19:14 +02:00
|
|
|
return $DB->get_field(self::TABLE, "id", ['shortname' => $shortname]);
|
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
public static function coursetiming($course) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$now = time();
|
2023-08-24 23:02:41 +02:00
|
|
|
if ($now > $course->startdate) {
|
|
|
|
if ($course->enddate > 0 && $now > $course->enddate) {
|
2023-05-17 21:19:14 +02:00
|
|
|
return "past";
|
2023-08-25 11:52:05 +02:00
|
|
|
} else {
|
2023-05-17 21:19:14 +02:00
|
|
|
return "present";
|
|
|
|
}
|
2023-08-25 10:41:56 +02:00
|
|
|
} else {
|
2023-05-17 21:19:14 +02:00
|
|
|
return "future";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
public function timing() {
|
2023-05-17 21:19:14 +02:00
|
|
|
return self::coursetiming($this->course);
|
|
|
|
}
|
2023-08-24 23:02:41 +02:00
|
|
|
|
|
|
|
public function displayname() {
|
|
|
|
$displayfield = get_config("local_treestudyplan", "display_field");
|
2023-05-17 21:19:14 +02:00
|
|
|
if ($displayfield == "idnumber") {
|
2023-08-24 23:02:41 +02:00
|
|
|
$idnumber = trim(preg_replace("/\s+/u", " ", $this->course->idnumber));
|
|
|
|
if (strlen($idnumber) > 0) {
|
2023-05-17 21:19:14 +02:00
|
|
|
return $this->course->idnumber;
|
|
|
|
}
|
2023-08-24 23:02:41 +02:00
|
|
|
} else if (strpos( $displayfield , "customfield_") === 0) {
|
|
|
|
$fieldname = substr($displayfield, strlen("customfield_"));
|
|
|
|
|
2023-05-17 21:19:14 +02:00
|
|
|
$handler = \core_customfield\handler::get_handler('core_course', 'course');
|
|
|
|
$datas = $handler->get_instance_data($this->course->id);
|
2023-08-24 23:02:41 +02:00
|
|
|
foreach ($datas as $data) {
|
|
|
|
if ($data->get_field()->get('shortname') == $fieldname) {
|
|
|
|
$value = trim(preg_replace("/\s+/u", " ", $data->get_value()));
|
|
|
|
if (strlen($value) > 0) {
|
2023-05-17 21:19:14 +02:00
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-08-24 23:02:41 +02:00
|
|
|
// Fallback to shortname when the specified display field fails, since shortname is never empty.
|
2023-05-17 21:19:14 +02:00
|
|
|
return $this->course->shortname;
|
|
|
|
}
|
|
|
|
|
2023-08-25 13:04:19 +02:00
|
|
|
public static function simple_structure($value = VALUE_REQUIRED) {
|
2023-05-17 21:19:14 +02:00
|
|
|
return new \external_single_structure([
|
|
|
|
"id" => new \external_value(PARAM_INT, 'linked course id'),
|
|
|
|
"fullname" => new \external_value(PARAM_TEXT, 'linked course name'),
|
|
|
|
"shortname" => new \external_value(PARAM_TEXT, 'linked course shortname'),
|
|
|
|
"displayname" => new \external_value(PARAM_TEXT, 'linked course displayname'),
|
|
|
|
"context" => contextinfo::structure(VALUE_OPTIONAL),
|
2023-08-24 23:02:41 +02:00
|
|
|
], 'referenced course information', $value);
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function simple_model() {
|
|
|
|
$contextinfo = new contextinfo($this->context);
|
|
|
|
$info = [
|
|
|
|
'id' => $this->course->id,
|
|
|
|
'fullname' => $this->course->fullname,
|
|
|
|
'shortname' => $this->course->shortname,
|
|
|
|
'displayname' => $this->displayname(),
|
|
|
|
'context' => $contextinfo->model()
|
|
|
|
];
|
2023-08-24 23:02:41 +02:00
|
|
|
|
2023-05-17 21:19:14 +02:00
|
|
|
return $info;
|
|
|
|
}
|
|
|
|
|
2023-08-25 13:04:19 +02:00
|
|
|
public static function editor_structure($value = VALUE_REQUIRED) {
|
2023-05-17 21:19:14 +02:00
|
|
|
return new \external_single_structure([
|
|
|
|
"id" => new \external_value(PARAM_INT, 'linked course id'),
|
|
|
|
"fullname" => new \external_value(PARAM_TEXT, 'linked course name'),
|
|
|
|
"shortname" => new \external_value(PARAM_TEXT, 'linked course shortname'),
|
|
|
|
"displayname" => new \external_value(PARAM_TEXT, 'linked course displayname'),
|
|
|
|
"context" => contextinfo::structure(VALUE_OPTIONAL),
|
|
|
|
"ctxid" => new \external_value(PARAM_INT, 'course context id name'),
|
2023-08-25 11:52:05 +02:00
|
|
|
"grades" => new \external_multiple_structure(gradeinfo::editor_structure(),
|
|
|
|
'grade list (legacy list)', VALUE_OPTIONAL),
|
2023-05-17 21:19:14 +02:00
|
|
|
"completion" => corecompletioninfo::editor_structure(VALUE_OPTIONAL),
|
|
|
|
"timing" => new \external_value(PARAM_TEXT, '(past|present|future)'),
|
|
|
|
"startdate" => new \external_value(PARAM_TEXT, 'Course start date'),
|
|
|
|
"enddate" => new \external_value(PARAM_TEXT, 'Course end date'),
|
|
|
|
"amteacher" => new \external_value(PARAM_BOOL, 'Requesting user is teacher in this course'),
|
2023-07-30 04:08:57 +02:00
|
|
|
"canupdatecourse" => new \external_value(PARAM_BOOL, "If the current user can update this course"),
|
2023-05-17 21:19:14 +02:00
|
|
|
"canselectgradables" => new \external_value(PARAM_BOOL, 'Requesting user can change selected gradables'),
|
|
|
|
"tag" => new \external_value(PARAM_TEXT, 'Tag'),
|
2023-08-24 23:02:41 +02:00
|
|
|
], 'referenced course information', $value);
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
|
2023-08-25 13:04:19 +02:00
|
|
|
public function editor_model(studyitem $studyitem = null, $usecorecompletioninfo = false) {
|
2023-05-17 21:19:14 +02:00
|
|
|
global $DB;
|
|
|
|
$contextinfo = new contextinfo($this->context);
|
2023-08-24 23:02:41 +02:00
|
|
|
|
2023-05-17 21:19:14 +02:00
|
|
|
$timing = $this->timing();
|
2023-08-24 23:02:41 +02:00
|
|
|
|
2023-05-17 21:19:14 +02:00
|
|
|
$info = [
|
|
|
|
'id' => $this->course->id,
|
|
|
|
'fullname' => $this->course->fullname,
|
|
|
|
'shortname' => $this->course->shortname,
|
|
|
|
'displayname' => $this->displayname(),
|
|
|
|
'context' => $contextinfo->model(),
|
|
|
|
'ctxid' => $this->coursecontext->id,
|
|
|
|
'timing' => $timing,
|
2023-08-24 23:02:41 +02:00
|
|
|
'startdate' => date("Y-m-d", $this->course->startdate, ),
|
|
|
|
'enddate' => date("Y-m-d", $this->course->enddate),
|
2023-08-25 17:33:20 +02:00
|
|
|
'amteacher' => $this->am_teacher(),
|
2023-08-24 23:02:41 +02:00
|
|
|
'canupdatecourse' => \has_capability("moodle/course:update", $this->coursecontext),
|
2023-08-25 17:33:20 +02:00
|
|
|
'canselectgradables' => $this->i_can_select_gradables(),
|
2023-05-17 21:19:14 +02:00
|
|
|
'tag' => "Editormodel",
|
|
|
|
'grades' => [],
|
|
|
|
];
|
2023-08-24 23:02:41 +02:00
|
|
|
|
|
|
|
if (!$usecorecompletioninfo) {
|
|
|
|
$gradables = gradeinfo::list_course_gradables($this->course, $studyitem);
|
|
|
|
|
|
|
|
foreach ($gradables as $gradable) {
|
2023-06-16 23:12:17 +02:00
|
|
|
$info['grades'][] = $gradable->editor_model($studyitem);
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
2023-08-24 23:09:20 +02:00
|
|
|
} else {
|
2023-05-17 21:19:14 +02:00
|
|
|
$cc = new corecompletioninfo($this->course);
|
|
|
|
$info['completion'] = $cc->editor_model();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $info;
|
|
|
|
}
|
|
|
|
|
2023-08-25 13:04:19 +02:00
|
|
|
public static function user_structure($value = VALUE_REQUIRED) {
|
2023-05-17 21:19:14 +02:00
|
|
|
return new \external_single_structure([
|
|
|
|
"id" => new \external_value(PARAM_INT, 'linked course id'),
|
|
|
|
"fullname" => new \external_value(PARAM_TEXT, 'linked course name'),
|
|
|
|
"shortname" => new \external_value(PARAM_TEXT, 'linked course shortname'),
|
|
|
|
"displayname" => new \external_value(PARAM_TEXT, 'linked course displayname'),
|
|
|
|
"context" => contextinfo::structure(VALUE_OPTIONAL),
|
|
|
|
"ctxid" => new \external_value(PARAM_INT, 'course context id name'),
|
2023-08-24 23:02:41 +02:00
|
|
|
"grades" => new \external_multiple_structure(gradeinfo::user_structure(), 'grade list (legacy list)', VALUE_OPTIONAL),
|
|
|
|
"completion" => corecompletioninfo::user_structure(VALUE_OPTIONAL),
|
2023-05-17 21:19:14 +02:00
|
|
|
"timing" => new \external_value(PARAM_TEXT, '(past|present|future)'),
|
|
|
|
"startdate" => new \external_value(PARAM_TEXT, 'Course start date'),
|
|
|
|
"enddate" => new \external_value(PARAM_TEXT, 'Course end date'),
|
2023-08-24 23:02:41 +02:00
|
|
|
], 'course information', $value);
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
|
2023-08-25 13:04:19 +02:00
|
|
|
public function user_model($userid, $usecorecompletioninfo = false) {
|
2023-05-17 21:19:14 +02:00
|
|
|
global $DB;
|
|
|
|
$contextinfo = new contextinfo($this->context);
|
|
|
|
|
|
|
|
$timing = $this->timing();
|
|
|
|
|
|
|
|
$info = [
|
|
|
|
'id' => $this->course->id,
|
|
|
|
'fullname' => $this->course->fullname,
|
|
|
|
'shortname' => $this->course->shortname,
|
|
|
|
'displayname' => $this->displayname(),
|
|
|
|
'context' => $contextinfo->model(),
|
|
|
|
'ctxid' => $this->coursecontext->id,
|
|
|
|
'timing' => $timing,
|
2023-08-24 23:02:41 +02:00
|
|
|
'startdate' => date("Y-m-d", $this->course->startdate),
|
|
|
|
'enddate' => date("Y-m-d", $this->course->enddate),
|
2023-05-17 21:19:14 +02:00
|
|
|
'grades' => [],
|
|
|
|
];
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
if (!$usecorecompletioninfo) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$gradables = gradeinfo::list_studyitem_gradables($this->studyitem);
|
2023-08-24 23:02:41 +02:00
|
|
|
foreach ($gradables as $gi) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$info['grades'][] = $gi->user_model($userid);
|
|
|
|
}
|
2023-08-24 23:09:20 +02:00
|
|
|
} else {
|
2023-05-17 21:19:14 +02:00
|
|
|
$cc = new corecompletioninfo($this->course);
|
|
|
|
$info['completion'] = $cc->user_model($userid);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $info;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|