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-28 11:26:14 +02:00
|
|
|
* Model class for study plan
|
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;
|
2024-04-19 16:46:30 +02:00
|
|
|
|
|
|
|
use DateInterval;
|
|
|
|
|
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');
|
2023-11-12 23:49:50 +01:00
|
|
|
require_once($CFG->libdir.'/filelib.php');
|
2023-08-28 11:26:14 +02:00
|
|
|
/**
|
|
|
|
* Model class for study plan
|
|
|
|
*/
|
2023-05-17 21:19:14 +02:00
|
|
|
class studyplan {
|
|
|
|
|
2023-08-27 21:23:39 +02:00
|
|
|
/** @var string */
|
2023-05-17 21:19:14 +02:00
|
|
|
const TABLE = "local_treestudyplan";
|
2024-03-09 22:51:34 +01:00
|
|
|
/** @var string */
|
|
|
|
const TABLE_COACH = "local_treestudyplan_coach";
|
|
|
|
|
2023-08-28 11:26:14 +02:00
|
|
|
/**
|
|
|
|
* Cache retrieved studyitems in this session
|
|
|
|
* @var array */
|
|
|
|
private static $cache = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cache retrieved pages for the studyplan in this session
|
|
|
|
* @var array */
|
2024-05-31 20:41:18 +02:00
|
|
|
private $pagecache = [];
|
2023-05-17 21:19:14 +02:00
|
|
|
|
2023-08-27 21:23:39 +02:00
|
|
|
/**
|
2023-08-27 22:20:17 +02:00
|
|
|
* Holds database record
|
2023-08-27 21:23:39 +02:00
|
|
|
* @var stdClass
|
|
|
|
*/
|
2023-08-27 23:27:07 +02:00
|
|
|
private $r;
|
2023-08-27 21:23:39 +02:00
|
|
|
/** @var int */
|
2023-05-17 21:19:14 +02:00
|
|
|
private $id;
|
2023-08-27 23:27:07 +02:00
|
|
|
/** @var aggregator */
|
2023-05-17 21:19:14 +02:00
|
|
|
private $aggregator;
|
|
|
|
|
2023-08-28 11:26:14 +02:00
|
|
|
/**
|
|
|
|
* Hold context object once retrieved.
|
|
|
|
* @var \context
|
|
|
|
*/
|
|
|
|
private $context = null;
|
|
|
|
/**
|
|
|
|
* Cache lookup of linked users (saves queries).
|
|
|
|
* @var int[]
|
|
|
|
*/
|
|
|
|
private $linkeduserids = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return configured aggregator for this studyplan
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public function aggregator(): aggregator {
|
2023-05-17 21:19:14 +02:00
|
|
|
return $this->aggregator;
|
|
|
|
}
|
|
|
|
|
2023-08-27 23:27:07 +02:00
|
|
|
/**
|
|
|
|
* Find record in database and return management object
|
2023-08-28 11:26:14 +02:00
|
|
|
* Cache objects to avoid multiple creation events in one session.
|
2023-08-27 23:27:07 +02:00
|
|
|
* @param int $id Id of database record
|
|
|
|
*/
|
2023-08-25 17:33:20 +02:00
|
|
|
public static function find_by_id($id): self {
|
2023-08-28 11:26:14 +02:00
|
|
|
if (!array_key_exists($id, self::$cache)) {
|
|
|
|
self::$cache[$id] = new self($id);
|
2023-08-24 23:02:41 +02:00
|
|
|
}
|
2023-08-28 11:26:14 +02:00
|
|
|
return self::$cache[$id];
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
|
2023-08-27 23:27:07 +02:00
|
|
|
/**
|
|
|
|
* Construct new instance from DB record
|
|
|
|
* @param int $id Id of database record
|
|
|
|
*/
|
2023-05-17 21:19:14 +02:00
|
|
|
private function __construct($id) {
|
|
|
|
global $DB;
|
|
|
|
$this->id = $id;
|
2024-06-02 19:23:40 +02:00
|
|
|
$this->r = $DB->get_record(self::TABLE, ['id' => $id], "*", MUST_EXIST);
|
2023-05-17 21:19:14 +02:00
|
|
|
|
2023-08-27 15:12:54 +02:00
|
|
|
$this->aggregator = aggregator::create_or_default($this->r->aggregation, $this->r->aggregation_config);
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
|
2023-08-27 22:20:17 +02:00
|
|
|
/**
|
|
|
|
* Return database identifier
|
|
|
|
* @return int
|
|
|
|
*/
|
2023-08-24 23:02:41 +02:00
|
|
|
public function id() {
|
2023-05-17 21:19:14 +02:00
|
|
|
return $this->id;
|
|
|
|
}
|
|
|
|
|
2023-08-27 22:20:17 +02:00
|
|
|
/**
|
|
|
|
* Return short name
|
|
|
|
* @return string
|
|
|
|
*/
|
2023-08-24 23:02:41 +02:00
|
|
|
public function shortname() {
|
2023-06-26 13:03:50 +02:00
|
|
|
return $this->r->shortname;
|
|
|
|
}
|
|
|
|
|
2024-05-15 22:57:54 +02:00
|
|
|
/**
|
|
|
|
* Return idnumber
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function idnumber() {
|
|
|
|
return $this->r->idnumber;
|
|
|
|
}
|
|
|
|
|
2023-08-27 22:20:17 +02:00
|
|
|
/**
|
|
|
|
* Return full name
|
|
|
|
* @return string
|
|
|
|
*/
|
2023-08-24 23:02:41 +02:00
|
|
|
public function name() {
|
2023-05-17 21:19:14 +02:00
|
|
|
return $this->r->name;
|
|
|
|
}
|
|
|
|
|
2024-03-10 15:56:35 +01:00
|
|
|
/**
|
|
|
|
* True if studyplan is suspended
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function suspended() {
|
|
|
|
return boolval($this->r->suspended);
|
|
|
|
}
|
|
|
|
|
2024-04-19 16:46:30 +02:00
|
|
|
/**
|
|
|
|
* Determine earliest start date of a page
|
|
|
|
* @return \DateTime|null
|
|
|
|
*/
|
|
|
|
public function startdate() {
|
|
|
|
$date = null;
|
2024-06-02 23:23:32 +02:00
|
|
|
foreach ($this->pages() as $p) {
|
2024-04-19 16:46:30 +02:00
|
|
|
if (!isset($date) || $p->startdate() < $date) {
|
|
|
|
$date = $p->startdate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $date;
|
|
|
|
}
|
|
|
|
|
2024-06-02 23:23:32 +02:00
|
|
|
/**
|
|
|
|
* Determine studyplan icon
|
|
|
|
* @return string Url of icon
|
|
|
|
*/
|
2023-10-23 21:54:09 +02:00
|
|
|
private function icon() {
|
|
|
|
global $CFG;
|
|
|
|
$fs = \get_file_storage();
|
|
|
|
// Returns an array of `stored_file` instances.
|
|
|
|
$files = $fs->get_area_files(\context_system::instance()->id, 'local_treestudyplan', 'icon', $this->id);
|
|
|
|
|
2024-06-02 23:23:32 +02:00
|
|
|
if (count($files) > 0) {
|
2023-10-23 21:54:09 +02:00
|
|
|
$file = array_shift($files);
|
|
|
|
if ($file->get_filename() == ".") {
|
|
|
|
// Get next file if the first is the directory itself.
|
|
|
|
$file = array_shift($files);
|
|
|
|
}
|
|
|
|
|
|
|
|
$url = \moodle_url::make_pluginfile_url(
|
|
|
|
$file->get_contextid(),
|
|
|
|
$file->get_component(),
|
|
|
|
$file->get_filearea(),
|
|
|
|
$file->get_itemid(),
|
|
|
|
$file->get_filepath(),
|
|
|
|
$file->get_filename(),
|
|
|
|
false // Do not force download of the file.
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// Try the configured default in settings.
|
|
|
|
$defaulticon = get_config('local_treestudyplan', 'defaulticon');
|
|
|
|
if (empty($defaulticon)) {
|
|
|
|
// Fall back to the standard (ugly) default image.
|
|
|
|
$url = new \moodle_url($CFG->wwwroot . "/local/treestudyplan/pix/default_icon.png");
|
|
|
|
} else {
|
2024-06-02 23:23:32 +02:00
|
|
|
$url = \moodle_url::make_pluginfile_url(
|
|
|
|
\context_system::instance()->id,
|
|
|
|
'local_treestudyplan',
|
|
|
|
'defaulticon',
|
|
|
|
0,
|
|
|
|
"/",
|
|
|
|
$defaulticon
|
|
|
|
);
|
2023-10-23 21:54:09 +02:00
|
|
|
}
|
2024-06-02 19:23:40 +02:00
|
|
|
|
2023-10-23 21:54:09 +02:00
|
|
|
}
|
|
|
|
return $url->out();
|
|
|
|
}
|
|
|
|
|
2024-06-02 19:23:40 +02:00
|
|
|
/**
|
2023-10-23 21:54:09 +02:00
|
|
|
* Return description with all file references resolved
|
|
|
|
*/
|
|
|
|
public function description() {
|
2023-11-12 23:49:50 +01:00
|
|
|
$text = \file_rewrite_pluginfile_urls(
|
2023-10-23 21:54:09 +02:00
|
|
|
// The content of the text stored in the database.
|
|
|
|
$this->r->description,
|
|
|
|
// The pluginfile URL which will serve the request.
|
|
|
|
'pluginfile.php',
|
2024-06-02 19:23:40 +02:00
|
|
|
|
2024-06-02 23:23:32 +02:00
|
|
|
// The combination of contextid / component / filearea / itemid.
|
|
|
|
// Form the virtual bucket that file are stored in.
|
|
|
|
\context_system::instance()->id, // System instance is always used for this.
|
2023-10-23 21:54:09 +02:00
|
|
|
'local_treestudyplan',
|
|
|
|
'studyplan',
|
|
|
|
$this->id
|
|
|
|
);
|
|
|
|
return $text;
|
|
|
|
}
|
2023-08-28 11:26:14 +02:00
|
|
|
/**
|
|
|
|
* Return the studyplan pages associated with this plan
|
2023-11-05 15:49:32 +01:00
|
|
|
* @param bool $refresh Set to true to force a refresh of the pages
|
2023-08-28 11:26:14 +02:00
|
|
|
* @return studyplanpage[]
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public function pages($refresh=false): array {
|
2024-06-02 23:23:32 +02:00
|
|
|
if (((bool)$refresh) || empty($this->pagecache)) {
|
2023-08-28 11:26:14 +02:00
|
|
|
$this->pagecache = studyplanpage::find_studyplan_children($this);
|
2023-07-23 16:25:08 +02:00
|
|
|
}
|
2023-08-28 11:26:14 +02:00
|
|
|
return $this->pagecache;
|
2023-07-23 16:25:08 +02:00
|
|
|
}
|
|
|
|
|
2023-05-17 21:19:14 +02:00
|
|
|
/**
|
2023-08-27 22:20:17 +02:00
|
|
|
* Return the context the studyplan is associated to
|
|
|
|
* @return \context
|
2023-05-17 21:19:14 +02:00
|
|
|
*/
|
2023-08-25 17:33:20 +02:00
|
|
|
public function context(): \context {
|
2023-12-13 21:19:45 +01:00
|
|
|
global $DB;
|
2023-08-24 23:02:41 +02:00
|
|
|
if (!isset($this->context)) {
|
2023-08-24 23:09:20 +02:00
|
|
|
try {
|
2023-12-13 21:19:45 +01:00
|
|
|
$this->context = contextinfo::context_by_id($this->r->context_id);
|
2023-08-25 10:41:56 +02:00
|
|
|
} catch (\dml_missing_record_exception $x) {
|
2023-12-13 21:19:45 +01:00
|
|
|
/* The associated context cannot be found.
|
|
|
|
Probably the category was removed, but the studyplan was not.
|
|
|
|
Revert the studyplan back to the system context to avoid lost studyplans.
|
2023-12-13 23:49:06 +01:00
|
|
|
Note that the daily cleanup task calls this function to make sure
|
|
|
|
studyplans without a valid context are not lost.
|
2023-12-13 21:19:45 +01:00
|
|
|
*/
|
|
|
|
$this->context = \context_system::instance();
|
|
|
|
$this->r->context_id = $this->context->id;
|
|
|
|
$DB->update_record(self::TABLE, $this->r);
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->context;
|
|
|
|
}
|
|
|
|
|
2023-08-27 22:20:17 +02:00
|
|
|
/**
|
|
|
|
* Webservice structure for basic info
|
|
|
|
* @param int $value Webservice requirement constant
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public static function simple_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, 'id of studyplan'),
|
2024-06-02 19:23:40 +02:00
|
|
|
"userid" => new \external_value(PARAM_INT, 'id of user the plan is shown for', VALUE_OPTIONAL),
|
2023-05-17 21:19:14 +02:00
|
|
|
"name" => new \external_value(PARAM_TEXT, 'name of studyplan'),
|
2023-08-25 10:41:56 +02:00
|
|
|
"shortname" => new \external_value(PARAM_TEXT, 'shortname of studyplan'),
|
2023-08-09 12:20:05 +02:00
|
|
|
"idnumber" => new \external_value(PARAM_TEXT, 'idnumber of curriculum'),
|
2023-05-17 21:19:14 +02:00
|
|
|
"context_id" => new \external_value(PARAM_INT, 'context_id of studyplan'),
|
2023-10-21 23:24:25 +02:00
|
|
|
"description" => new \external_value(PARAM_RAW, 'description of studyplan'),
|
2023-10-19 17:48:29 +02:00
|
|
|
"descriptionformat" => new \external_value(PARAM_INT, 'description format'),
|
2024-06-02 19:23:40 +02:00
|
|
|
"icon" => new \external_value(PARAM_RAW, 'icon for this plan'),
|
2023-05-17 21:19:14 +02:00
|
|
|
"aggregation" => new \external_value(PARAM_TEXT, 'selected aggregator'),
|
|
|
|
"aggregation_config" => new \external_value(PARAM_TEXT, 'config string for aggregator'),
|
|
|
|
"aggregation_info" => aggregator::basic_structure(),
|
2023-08-24 23:02:41 +02:00
|
|
|
"pages" => new \external_multiple_structure(studyplanpage::simple_structure(), 'pages'),
|
2024-06-02 19:23:40 +02:00
|
|
|
"progress" => new \external_value(PARAM_FLOAT, "fraction of completed modules", VALUE_OPTIONAL),
|
2024-06-02 23:23:32 +02:00
|
|
|
"amteaching" => new \external_value(
|
|
|
|
PARAM_BOOL,
|
|
|
|
"Current user is teaching one or more courses in this studyplan",
|
|
|
|
VALUE_OPTIONAL
|
|
|
|
),
|
2024-06-02 19:23:40 +02:00
|
|
|
"suspended" => new \external_value(PARAM_BOOL, 'if studyplan is suspended', VALUE_OPTIONAL),
|
2023-08-24 23:02:41 +02:00
|
|
|
], 'Basic studyplan info', $value);
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
|
2023-08-27 23:29:46 +02:00
|
|
|
/**
|
2023-08-27 22:20:17 +02:00
|
|
|
* Webservice model for basic info
|
2024-06-03 04:00:46 +02:00
|
|
|
* @param int|null $userid Optional id of user, so progress/teaching info for user can be included
|
2023-08-27 22:20:17 +02:00
|
|
|
* @return array Webservice data model
|
|
|
|
*/
|
2024-05-18 16:48:35 +02:00
|
|
|
public function simple_model($userid=null) {
|
2023-11-12 23:49:50 +01:00
|
|
|
global $USER;
|
2023-07-27 12:28:04 +02:00
|
|
|
$pages = [];
|
2023-08-24 23:02:41 +02:00
|
|
|
foreach ($this->pages() as $p) {
|
2023-07-27 12:28:04 +02:00
|
|
|
$pages[] = $p->simple_model();
|
|
|
|
}
|
|
|
|
|
2023-11-12 23:49:50 +01:00
|
|
|
$model = [
|
2023-05-17 21:19:14 +02:00
|
|
|
'id' => $this->r->id,
|
|
|
|
'name' => $this->r->name,
|
|
|
|
'shortname' => $this->r->shortname,
|
2023-08-09 12:20:05 +02:00
|
|
|
'idnumber' => $this->r->idnumber,
|
2023-05-17 21:19:14 +02:00
|
|
|
'context_id' => $this->context()->id,
|
2023-10-23 21:54:09 +02:00
|
|
|
'description' => $this->description(),
|
2023-10-19 17:48:29 +02:00
|
|
|
'descriptionformat' => $this->r->descriptionformat,
|
2023-10-23 21:54:09 +02:00
|
|
|
'icon' => $this->icon(),
|
2023-07-27 12:28:04 +02:00
|
|
|
'aggregation' => $this->r->aggregation,
|
|
|
|
'aggregation_config' => $this->aggregator->config_string(),
|
2023-05-17 21:19:14 +02:00
|
|
|
'aggregation_info' => $this->aggregator->basic_model(),
|
2023-07-27 12:28:04 +02:00
|
|
|
'pages' => $pages,
|
2024-03-10 15:56:35 +01:00
|
|
|
'suspended' => boolval($this->r->suspended),
|
2023-05-17 21:19:14 +02:00
|
|
|
];
|
2024-06-02 19:23:40 +02:00
|
|
|
if (isset($userid)) {
|
2024-05-18 16:48:35 +02:00
|
|
|
$model["userid"] = $userid;
|
|
|
|
$model["progress"] = $this->scanuserprogress($userid);
|
2024-06-02 19:23:40 +02:00
|
|
|
$model['amteaching'] = teachingfinder::is_teaching_studyplan($this, $userid);
|
2024-05-18 16:48:35 +02:00
|
|
|
}
|
|
|
|
return $model;
|
|
|
|
}
|
2024-05-18 16:46:27 +02:00
|
|
|
|
2024-05-18 16:48:35 +02:00
|
|
|
/**
|
|
|
|
* Webservice model for basic info in coach mode
|
|
|
|
* @return array Webservice data model
|
|
|
|
*/
|
|
|
|
public function simple_model_coach() {
|
|
|
|
$model = $this->simple_model();
|
|
|
|
$users = $this->find_linked_userids();
|
|
|
|
$sum = 0;
|
2024-06-02 23:23:32 +02:00
|
|
|
foreach ($users as $uid) {
|
2024-05-18 16:48:35 +02:00
|
|
|
$sum += $this->scanuserprogress($uid);
|
2024-06-02 19:23:40 +02:00
|
|
|
}
|
2024-05-18 16:48:35 +02:00
|
|
|
|
|
|
|
$model["progress"] = $sum / count($users);
|
2024-05-18 16:46:27 +02:00
|
|
|
|
2023-11-12 23:49:50 +01:00
|
|
|
return $model;
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
|
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, 'id of studyplan'),
|
|
|
|
"name" => new \external_value(PARAM_TEXT, 'name of studyplan'),
|
2023-08-25 10:41:56 +02:00
|
|
|
"shortname" => new \external_value(PARAM_TEXT, 'shortname of studyplan'),
|
2023-08-09 12:20:05 +02:00
|
|
|
"idnumber" => new \external_value(PARAM_TEXT, 'idnumber of curriculum'),
|
2023-10-21 23:24:25 +02:00
|
|
|
"description" => new \external_value(PARAM_RAW, 'description of studyplan'),
|
|
|
|
"descriptionformat" => new \external_value(PARAM_INT, 'description format'),
|
2024-06-02 19:23:40 +02:00
|
|
|
"icon" => new \external_value(PARAM_RAW, 'icon for this plan'),
|
2023-05-17 21:19:14 +02:00
|
|
|
"context_id" => new \external_value(PARAM_INT, 'context_id of studyplan'),
|
|
|
|
"aggregation" => new \external_value(PARAM_TEXT, 'selected aggregator'),
|
|
|
|
"aggregation_config" => new \external_value(PARAM_TEXT, 'config string for aggregator'),
|
|
|
|
"aggregation_info" => aggregator::basic_structure(),
|
2023-07-23 16:25:08 +02:00
|
|
|
"pages" => new \external_multiple_structure(studyplanpage::editor_structure()),
|
2023-05-17 21:19:14 +02:00
|
|
|
"advanced" => new \external_single_structure([
|
|
|
|
"force_scales" => new \external_single_structure([
|
|
|
|
"scales" => new \external_multiple_structure(new \external_single_structure([
|
|
|
|
"id" => new \external_value(PARAM_INT, 'id of scale'),
|
|
|
|
"name" => new \external_value(PARAM_TEXT, 'name of scale'),
|
|
|
|
])),
|
2023-08-24 23:02:41 +02:00
|
|
|
], "Scale forcing on stuff", VALUE_OPTIONAL),
|
|
|
|
], "Advanced features available", VALUE_OPTIONAL),
|
2024-06-02 19:23:40 +02:00
|
|
|
"suspended" => new \external_value(PARAM_BOOL, 'if studyplan is suspended', VALUE_OPTIONAL),
|
2024-03-10 15:56:35 +01:00
|
|
|
], 'Studyplan full structure', $value);
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
|
2023-08-27 21:57:21 +02:00
|
|
|
/**
|
|
|
|
* Webservice model for editor info
|
|
|
|
* @return array Webservice data model
|
|
|
|
*/
|
2023-08-24 23:02:41 +02:00
|
|
|
public function editor_model() {
|
2023-05-17 21:19:14 +02:00
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$model = [
|
|
|
|
'id' => $this->r->id,
|
|
|
|
'name' => $this->r->name,
|
|
|
|
'shortname' => $this->r->shortname,
|
2023-08-09 09:48:06 +02:00
|
|
|
'idnumber' => $this->r->idnumber,
|
2023-10-23 21:54:09 +02:00
|
|
|
'description' => $this->description(),
|
2023-10-21 23:24:25 +02:00
|
|
|
'descriptionformat' => $this->r->descriptionformat,
|
2023-10-23 21:54:09 +02:00
|
|
|
'icon' => $this->icon(),
|
2023-05-17 21:19:14 +02:00
|
|
|
'context_id' => $this->context()->id,
|
|
|
|
"aggregation" => $this->r->aggregation,
|
|
|
|
"aggregation_config" => $this->aggregator->config_string(),
|
2023-08-24 23:02:41 +02:00
|
|
|
'aggregation_info' => $this->aggregator->basic_model(),
|
2023-07-23 16:25:08 +02:00
|
|
|
'pages' => [],
|
2024-03-10 15:56:35 +01:00
|
|
|
'suspended' => boolval($this->r->suspended),
|
2023-05-17 21:19:14 +02:00
|
|
|
];
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
foreach ($this->pages() as $p) {
|
2023-07-23 16:25:08 +02:00
|
|
|
$model['pages'][] = $p->editor_model();
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
if (has_capability('local/treestudyplan:forcescales', \context_system::instance())) {
|
|
|
|
|
|
|
|
if (!array_key_exists('advanced', $model)) {
|
|
|
|
// Create advanced node if it does not exist.
|
2023-05-17 21:19:14 +02:00
|
|
|
$model['advanced'] = [];
|
|
|
|
}
|
|
|
|
|
2023-08-25 09:44:34 +02:00
|
|
|
// Get a list of available scales.
|
2023-08-24 23:02:41 +02:00
|
|
|
$scales = array_map( function($scale) {
|
2024-06-02 19:23:40 +02:00
|
|
|
return [ "id" => $scale->id, "name" => $scale->name ];
|
2023-08-25 17:33:20 +02:00
|
|
|
}, \grade_scale::fetch_all(['courseid' => 0]) );
|
2023-05-17 21:19:14 +02:00
|
|
|
|
|
|
|
$model['advanced']['force_scales'] = [
|
|
|
|
'scales' => $scales,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $model;
|
|
|
|
}
|
|
|
|
|
2023-08-28 11:26:14 +02:00
|
|
|
/**
|
|
|
|
* Add a new studyplan
|
|
|
|
* @param array $fields Properties for study line ['name', 'shortname', 'description', 'idnumber', 'context_id', 'aggregation',
|
|
|
|
* 'aggregation_config', 'periods', 'startdate', 'enddate'];
|
|
|
|
* @param bool $bare If true, do not create a first page with copy of studyplan names
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public static function add($fields, $bare = false): self {
|
2023-05-17 21:19:14 +02:00
|
|
|
global $CFG, $DB;
|
2023-08-24 23:02:41 +02:00
|
|
|
|
2024-06-02 23:23:32 +02:00
|
|
|
$addable = ['name', 'shortname', 'description', 'descriptionformat',
|
|
|
|
'idnumber', 'context_id', 'aggregation', 'aggregation_config'];
|
2024-05-27 22:11:32 +02:00
|
|
|
$info = ['enddate' => null, "template" => 0, "suspended" => 0];
|
2023-08-24 23:02:41 +02:00
|
|
|
foreach ($addable as $f) {
|
|
|
|
if (array_key_exists($f, $fields)) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$info[$f] = $fields[$f];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$id = $DB->insert_record(self::TABLE, $info);
|
2023-08-25 17:33:20 +02:00
|
|
|
$plan = self::find_by_id($id); // Make sure the new studyplan is immediately cached.
|
2023-07-23 16:47:02 +02:00
|
|
|
|
2024-06-02 23:23:32 +02:00
|
|
|
// Add a single page and initialize it with placeholder data.
|
|
|
|
// This makes it easier to create a new study plan.
|
2023-11-05 15:49:32 +01:00
|
|
|
// On import, adding an empty page messes things up , so we have an option to skip this....
|
2023-08-24 23:02:41 +02:00
|
|
|
if (!$bare) {
|
2023-08-07 23:07:59 +02:00
|
|
|
|
2023-12-12 22:42:37 +01:00
|
|
|
$pageaddable = ['name', 'shortname', 'description', 'descriptionformat', 'periods', 'startdate', 'enddate'];
|
2023-08-07 23:07:59 +02:00
|
|
|
$pageinfo = ['studyplan_id' => $id];
|
2023-08-24 23:02:41 +02:00
|
|
|
foreach ($pageaddable as $f) {
|
|
|
|
if (array_key_exists($f, $fields)) {
|
|
|
|
if ($f == "name") {
|
2023-08-07 23:07:59 +02:00
|
|
|
$pageinfo["fullname"] = $fields[$f];
|
|
|
|
} else {
|
|
|
|
$pageinfo[$f] = $fields[$f];
|
|
|
|
}
|
2023-07-23 16:47:02 +02:00
|
|
|
}
|
|
|
|
}
|
2023-08-07 23:07:59 +02:00
|
|
|
|
2024-06-02 19:23:40 +02:00
|
|
|
$page = studyplanpage::add($pageinfo, $bare);
|
2023-08-07 23:07:59 +02:00
|
|
|
$plan->page_cache = [$page];
|
2023-07-23 16:47:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $plan;
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
|
2023-08-28 11:26:14 +02:00
|
|
|
/**
|
|
|
|
* Edit study line properties
|
2024-03-10 15:56:35 +01:00
|
|
|
* @param array $fields Changed properties for study line ['name', 'shortname', 'description', 'idnumber',
|
|
|
|
* 'context_id', 'aggregation', 'aggregation_config', 'suspended']
|
2023-08-28 11:26:14 +02:00
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public function edit($fields): self {
|
2023-05-17 21:19:14 +02:00
|
|
|
global $DB;
|
2024-03-10 15:56:35 +01:00
|
|
|
$editable = [
|
|
|
|
'name',
|
|
|
|
'shortname',
|
|
|
|
'description',
|
|
|
|
'descriptionformat',
|
|
|
|
'idnumber',
|
|
|
|
'context_id',
|
|
|
|
'aggregation',
|
|
|
|
'aggregation_config',
|
2024-04-19 16:46:30 +02:00
|
|
|
'suspended',
|
2024-06-02 23:23:32 +02:00
|
|
|
'template',
|
2024-03-10 15:56:35 +01:00
|
|
|
];
|
2024-06-02 19:23:40 +02:00
|
|
|
$info = ['id' => $this->id ];
|
2023-08-24 23:02:41 +02:00
|
|
|
foreach ($editable as $f) {
|
|
|
|
if (array_key_exists($f, $fields)) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$info[$f] = $fields[$f];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$DB->update_record(self::TABLE, $info);
|
2023-08-25 12:16:51 +02:00
|
|
|
// Reload record after edit.
|
2023-08-24 23:02:41 +02:00
|
|
|
$this->r = $DB->get_record(self::TABLE, ['id' => $this->id], "*", MUST_EXIST);
|
|
|
|
|
2023-08-25 12:16:51 +02:00
|
|
|
// Reload the context...
|
2023-08-24 23:02:41 +02:00
|
|
|
$this->context = null;
|
2023-05-17 21:19:14 +02:00
|
|
|
$this->context();
|
2023-08-25 09:44:34 +02:00
|
|
|
// Reload aggregator.
|
2023-08-27 15:12:54 +02:00
|
|
|
$this->aggregator = aggregator::create_or_default($this->r->aggregation, $this->r->aggregation_config);
|
2023-07-23 16:47:02 +02:00
|
|
|
|
2023-05-17 21:19:14 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2023-08-28 11:26:14 +02:00
|
|
|
/**
|
|
|
|
* Delete studyline
|
|
|
|
* @param bool $force Force deletion even if study line contains items
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public function delete($force = false): success {
|
2023-05-17 21:19:14 +02:00
|
|
|
global $DB;
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
if ($force) {
|
2023-07-23 16:25:08 +02:00
|
|
|
$children = studyplanpage::find_studyplan_children($this);
|
2023-08-24 23:02:41 +02:00
|
|
|
foreach ($children as $c) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$c->delete($force);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
if ($DB->count_records('local_treestudyplan_page', ['studyplan_id' => $this->id]) > 0) {
|
2023-07-23 16:25:08 +02:00
|
|
|
return success::fail('cannot delete studyplan that still has pages');
|
2023-08-25 09:33:42 +02:00
|
|
|
} else {
|
2024-05-22 22:16:47 +02:00
|
|
|
// Delete any links to this studyplan before deleting the studyplan itself.
|
2024-05-22 21:46:56 +02:00
|
|
|
$DB->delete_records("local_treestudyplan_coach", ["studyplan_id" => $this->id]);
|
|
|
|
$DB->delete_records("local_treestudyplan_cohort", ["studyplan_id" => $this->id]);
|
|
|
|
$DB->delete_records("local_treestudyplan_user", ["studyplan_id" => $this->id]);
|
2024-06-02 23:23:32 +02:00
|
|
|
|
2023-05-17 21:19:14 +02:00
|
|
|
$DB->delete_records('local_treestudyplan', ['id' => $this->id]);
|
|
|
|
return success::success();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-28 11:26:14 +02:00
|
|
|
/**
|
|
|
|
* Find all studyplans in a given context or the system context
|
2024-04-19 16:46:30 +02:00
|
|
|
* @param int $contextid Optional contextid to search in. ANY context used if left empty
|
2023-08-28 11:26:14 +02:00
|
|
|
* @return studyplan[]
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public static function find_all($contextid = -1): array {
|
2023-05-17 21:19:14 +02:00
|
|
|
global $DB, $USER;
|
|
|
|
$list = [];
|
2023-05-19 16:45:15 +02:00
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
if ($contextid <= 0) {
|
|
|
|
$ids = $DB->get_fieldset_select(self::TABLE, "id", "");
|
2023-08-25 10:41:56 +02:00
|
|
|
} else {
|
2023-08-24 23:02:41 +02:00
|
|
|
if ($contextid == 1) {
|
2023-05-19 16:45:15 +02:00
|
|
|
$contextid = 1;
|
|
|
|
$where = "context_id <= :contextid OR context_id IS NULL";
|
|
|
|
} else {
|
|
|
|
$where = "context_id = :contextid";
|
|
|
|
}
|
2023-08-24 23:02:41 +02:00
|
|
|
$ids = $DB->get_fieldset_select(self::TABLE, "id", $where, ["contextid" => $contextid]);
|
2023-05-19 16:45:15 +02:00
|
|
|
}
|
2023-08-24 23:02:41 +02:00
|
|
|
|
|
|
|
foreach ($ids as $id) {
|
2023-08-25 17:33:20 +02:00
|
|
|
$list[] = self::find_by_id($id);
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
2024-04-19 16:46:30 +02:00
|
|
|
/**
|
|
|
|
* Find all template studyplans in a given context or the system context
|
|
|
|
* @param int $contextid Optional contextid to search in. ANY context used if left empty
|
|
|
|
* @return studyplan[]
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public static function find_template($contextid = -1): array {
|
2024-04-19 16:46:30 +02:00
|
|
|
global $DB, $USER;
|
|
|
|
$list = [];
|
|
|
|
|
|
|
|
$templatewhere = "template = 1";
|
|
|
|
if ($contextid <= 0) {
|
|
|
|
$ids = $DB->get_fieldset_select(self::TABLE, "id", $templatewhere);
|
|
|
|
} else {
|
|
|
|
if ($contextid == 1) {
|
|
|
|
$contextid = 1;
|
|
|
|
$where = "context_id <= :contextid OR context_id IS NULL";
|
|
|
|
} else {
|
|
|
|
$where = "context_id = :contextid";
|
|
|
|
}
|
|
|
|
$ids = $DB->get_fieldset_select(self::TABLE, "id", "{$where} AND {$templatewhere}", ["contextid" => $contextid]);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($ids as $id) {
|
|
|
|
$list[] = self::find_by_id($id);
|
|
|
|
}
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Count all template studyplans in a given context or the system context
|
|
|
|
* @param int $contextid Optional contextid to search in. ANY context used if left empty
|
|
|
|
* @return int
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public static function count_template($contextid = -1): int {
|
2024-04-19 16:46:30 +02:00
|
|
|
global $DB, $USER;
|
|
|
|
$list = [];
|
|
|
|
|
|
|
|
$templatewhere = "template = 1";
|
|
|
|
if ($contextid <= 0) {
|
|
|
|
return $DB->count_records_select(self::TABLE, $templatewhere);
|
|
|
|
} else {
|
|
|
|
if ($contextid == 1) {
|
|
|
|
$contextid = 1;
|
|
|
|
$where = "context_id <= :contextid OR context_id IS NULL";
|
|
|
|
} else {
|
|
|
|
$where = "context_id = :contextid";
|
|
|
|
}
|
|
|
|
return $DB->count_records_select(self::TABLE, "{$where} AND {$templatewhere}", ["contextid" => $contextid]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-28 11:26:14 +02:00
|
|
|
/**
|
|
|
|
* Find all studyplans in a given context or the system context with a specific short name
|
|
|
|
* (Used in generating random grades for development)
|
|
|
|
* @param string $shortname Shortname to match
|
2024-05-15 22:57:54 +02:00
|
|
|
* @param int $contextid Optional contextid to search in. All contexts searched if empty.
|
2023-08-28 11:26:14 +02:00
|
|
|
* @return studyplan[]
|
|
|
|
*/
|
2023-08-25 17:33:20 +02:00
|
|
|
public static function find_by_shortname($shortname, $contextid = 0): array {
|
2023-05-17 21:19:14 +02:00
|
|
|
global $DB;
|
|
|
|
$list = [];
|
|
|
|
|
2024-05-15 22:57:54 +02:00
|
|
|
$where = "shortname = :shortname ";
|
|
|
|
if ($contextid == 1) {
|
|
|
|
$where .= " AND (context_id = :contextid OR context_id IS NULL)";
|
|
|
|
} else if ($contextid > 1) {
|
|
|
|
$where .= " AND context_id = :contextid";
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
2024-06-02 19:23:40 +02:00
|
|
|
|
2023-08-25 10:41:56 +02:00
|
|
|
$ids = $DB->get_fieldset_select(self::TABLE, "id", $where, ["shortname" => $shortname, "contextid" => $contextid]);
|
2023-08-24 23:02:41 +02:00
|
|
|
foreach ($ids as $id) {
|
2023-08-25 17:33:20 +02:00
|
|
|
$list[] = self::find_by_id($id);
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
2024-05-15 22:57:54 +02:00
|
|
|
/**
|
|
|
|
* Find all studyplans in a given context or the system context with a specific idnumber
|
|
|
|
* @param string $idnumber IDNumber to match
|
|
|
|
* @param int $contextid Optional contextid to search in. All contexts searched if empty.
|
|
|
|
* @return studyplan[]
|
|
|
|
*/
|
|
|
|
public static function find_by_idnumber($idnumber, $contextid = 0): array {
|
|
|
|
global $DB;
|
|
|
|
$list = [];
|
|
|
|
|
|
|
|
$where = "idnumber = :idnumber ";
|
|
|
|
if ($contextid == 1) {
|
|
|
|
$where .= " AND (context_id = :contextid OR context_id IS NULL)";
|
|
|
|
} else if ($contextid > 1) {
|
|
|
|
$where .= " AND context_id = :contextid";
|
|
|
|
}
|
|
|
|
|
|
|
|
$ids = $DB->get_fieldset_select(self::TABLE, "id", $where, ["idnumber" => $idnumber, "contextid" => $contextid]);
|
|
|
|
foreach ($ids as $id) {
|
|
|
|
$list[] = self::find_by_id($id);
|
|
|
|
}
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find all studyplans in a given context or the system context with a specific full name
|
|
|
|
* @param string $name Full name to match
|
|
|
|
* @param int $contextid Optional contextid to search in. All contexts searched if empty.
|
|
|
|
* @return studyplan[]
|
|
|
|
*/
|
|
|
|
public static function find_by_fullname($name, $contextid = 0): array {
|
|
|
|
global $DB;
|
|
|
|
$list = [];
|
|
|
|
|
|
|
|
$where = "name = :name ";
|
|
|
|
if ($contextid == 1) {
|
|
|
|
$where .= " AND (context_id = :contextid OR context_id IS NULL)";
|
|
|
|
} else if ($contextid > 1) {
|
|
|
|
$where .= " AND context_id = :contextid";
|
|
|
|
}
|
|
|
|
|
|
|
|
$ids = $DB->get_fieldset_select(self::TABLE, "id", $where, ["name" => $name, "contextid" => $contextid]);
|
|
|
|
foreach ($ids as $id) {
|
|
|
|
$list[] = self::find_by_id($id);
|
|
|
|
}
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
2023-08-28 11:26:14 +02:00
|
|
|
/**
|
|
|
|
* Find all studyplans for a given user
|
|
|
|
* @param int $userid Id of the user to search for
|
|
|
|
* @return studyplan[]
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public static function find_for_user($userid): array {
|
2023-05-17 21:19:14 +02:00
|
|
|
global $DB;
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
$sql = "SELECT s.id FROM {local_treestudyplan} s
|
2023-05-17 21:19:14 +02:00
|
|
|
INNER JOIN {local_treestudyplan_cohort} j ON j.studyplan_id = s.id
|
|
|
|
INNER JOIN {cohort_members} cm ON j.cohort_id = cm.cohortid
|
2024-05-22 23:24:06 +02:00
|
|
|
INNER JOIN {user} u ON cm.userid = u.id
|
2024-05-22 23:18:05 +02:00
|
|
|
WHERE cm.userid = :userid AND u.deleted != 1";
|
2023-08-25 17:33:20 +02:00
|
|
|
$cohortplanids = $DB->get_fieldset_sql($sql, ['userid' => $userid]);
|
2023-05-17 21:19:14 +02:00
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
$sql = "SELECT s.id FROM {local_treestudyplan} s
|
2023-05-17 21:19:14 +02:00
|
|
|
INNER JOIN {local_treestudyplan_user} j ON j.studyplan_id = s.id
|
2024-05-22 23:18:05 +02:00
|
|
|
INNER JOIN {user} u ON j.user_id = u.id
|
|
|
|
WHERE j.user_id = :userid AND u.deleted != 1";
|
2023-08-25 17:33:20 +02:00
|
|
|
$userplanids = $DB->get_fieldset_sql($sql, ['userid' => $userid]);
|
2023-05-17 21:19:14 +02:00
|
|
|
|
|
|
|
$plans = [];
|
2023-08-25 17:33:20 +02:00
|
|
|
foreach ($cohortplanids as $id) {
|
|
|
|
$plans[$id] = self::find_by_id($id);
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
2023-08-25 17:33:20 +02:00
|
|
|
foreach ($userplanids as $id) {
|
2023-08-24 23:02:41 +02:00
|
|
|
if (!array_key_exists($id, $plans)) {
|
2023-08-25 17:33:20 +02:00
|
|
|
$plans[$id] = self::find_by_id($id);
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $plans;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-08-28 11:26:14 +02:00
|
|
|
/**
|
|
|
|
* Check if a given user has associated studyplans
|
|
|
|
* @param int $userid Id of the user to search for
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public static function exist_for_user($userid): bool {
|
2023-05-17 21:19:14 +02:00
|
|
|
global $DB;
|
|
|
|
$count = 0;
|
2023-11-12 23:49:50 +01:00
|
|
|
$sql = "SELECT COUNT(s.id) FROM {local_treestudyplan} s
|
2023-05-17 21:19:14 +02:00
|
|
|
INNER JOIN {local_treestudyplan_cohort} j ON j.studyplan_id = s.id
|
|
|
|
INNER JOIN {cohort_members} cm ON j.cohort_id = cm.cohortid
|
2024-05-22 23:24:06 +02:00
|
|
|
INNER JOIN {user} u ON cm.userid = u.id
|
2024-05-22 23:18:05 +02:00
|
|
|
WHERE cm.userid = :userid AND u.deleted != 1";
|
2023-05-17 21:19:14 +02:00
|
|
|
$count += $DB->count_records_sql($sql, ['userid' => $userid]);
|
|
|
|
|
2023-11-12 23:49:50 +01:00
|
|
|
$sql = "SELECT COUNT(s.id) FROM {local_treestudyplan} s
|
2023-05-17 21:19:14 +02:00
|
|
|
INNER JOIN {local_treestudyplan_user} j ON j.studyplan_id = s.id
|
2024-05-22 23:18:05 +02:00
|
|
|
INNER JOIN {user} u ON j.user_id = u.id
|
|
|
|
WHERE j.user_id = :userid AND u.deleted != 1";
|
2023-05-17 21:19:14 +02:00
|
|
|
$count += $DB->count_records_sql($sql, ['userid' => $userid]);
|
|
|
|
|
|
|
|
return ($count > 0);
|
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
/**
|
2023-05-17 21:19:14 +02:00
|
|
|
* Retrieve the users linked to this studyplan.
|
2023-08-28 11:26:14 +02:00
|
|
|
* @return stdClass[] User objects
|
2023-05-17 21:19:14 +02:00
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public function find_linked_users(): array {
|
2023-05-17 21:19:14 +02:00
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$users = [];
|
|
|
|
|
|
|
|
$uids = $this->find_linked_userids();
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
foreach ($uids as $uid) {
|
2023-08-25 10:41:56 +02:00
|
|
|
$users[] = $DB->get_record("user", ["id" => $uid]);
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $users;
|
|
|
|
}
|
|
|
|
|
2023-08-25 17:33:20 +02:00
|
|
|
/**
|
2023-05-17 21:19:14 +02:00
|
|
|
* Retrieve the user id's of the users linked to this studyplan.
|
|
|
|
* @return array of int (User Id)
|
|
|
|
*/
|
2023-06-16 23:12:17 +02:00
|
|
|
public function find_linked_userids(): array {
|
2023-05-17 21:19:14 +02:00
|
|
|
global $DB;
|
|
|
|
|
2023-08-28 11:26:14 +02:00
|
|
|
if ($this->linkeduserids === null) {
|
2023-06-16 23:12:17 +02:00
|
|
|
$uids = [];
|
2023-08-24 23:02:41 +02:00
|
|
|
// First get directly linked userids.
|
2023-06-16 23:12:17 +02:00
|
|
|
$sql = "SELECT j.user_id FROM {local_treestudyplan_user} j
|
2024-05-22 23:18:05 +02:00
|
|
|
INNER JOIN {user} u ON j.user_id = u.id
|
|
|
|
WHERE j.studyplan_id = :planid AND u.deleted != 1";
|
2023-06-16 23:12:17 +02:00
|
|
|
$ulist = $DB->get_fieldset_sql($sql, ['planid' => $this->id]);
|
2023-05-17 21:19:14 +02:00
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
$uids = array_merge($uids, $ulist);
|
|
|
|
foreach ($ulist as $uid) {
|
2023-08-25 10:41:56 +02:00
|
|
|
$users[] = $DB->get_record("user", ["id" => $uid]);
|
2023-06-16 23:12:17 +02:00
|
|
|
}
|
2023-05-17 21:19:14 +02:00
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
// Next het users linked though cohort.
|
2023-08-28 11:26:14 +02:00
|
|
|
$sql = "SELECT cm.userid FROM {local_treestudyplan_cohort} j
|
2023-06-16 23:12:17 +02:00
|
|
|
INNER JOIN {cohort_members} cm ON j.cohort_id = cm.cohortid
|
2024-05-22 23:24:06 +02:00
|
|
|
INNER JOIN {user} u ON cm.userid = u.id
|
2024-05-22 23:18:05 +02:00
|
|
|
WHERE j.studyplan_id = :planid AND u.deleted != 1";
|
2023-06-16 23:12:17 +02:00
|
|
|
$ulist = $DB->get_fieldset_sql($sql, ['planid' => $this->id]);
|
2023-05-17 21:19:14 +02:00
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
$uids = array_merge($uids, $ulist);
|
2023-05-17 21:19:14 +02:00
|
|
|
|
2023-08-28 11:26:14 +02:00
|
|
|
$this->linkeduserids = array_unique($uids);
|
2023-06-16 23:12:17 +02:00
|
|
|
}
|
2023-08-28 11:26:14 +02:00
|
|
|
return $this->linkeduserids;
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
|
2024-06-02 23:23:32 +02:00
|
|
|
/**
|
|
|
|
* Check if this studyplan is linked to a particular user
|
2023-08-24 23:02:41 +02:00
|
|
|
* @param bool|stdClass $user The userid or user record of the user
|
2023-05-17 21:19:14 +02:00
|
|
|
*/
|
2023-08-24 23:02:41 +02:00
|
|
|
public function has_linked_user($user) {
|
|
|
|
if (is_int($user)) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$userid = $user;
|
|
|
|
} else {
|
|
|
|
$userid = $user->id;
|
|
|
|
}
|
|
|
|
$uids = $this->find_linked_userids();
|
2023-08-24 23:02:41 +02:00
|
|
|
if (in_array($userid, $uids)) {
|
2023-05-17 21:19:14 +02:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-02 23:23:32 +02:00
|
|
|
/**
|
|
|
|
* Check if this studyplan is linked to a particular user
|
2024-03-09 22:51:34 +01:00
|
|
|
* @param bool|stdClass|null $user The userid or user record of the user Leave empty to check current user.
|
|
|
|
*/
|
|
|
|
public function is_coach($user=null) {
|
2024-06-02 23:23:32 +02:00
|
|
|
global $DB, $USER;
|
|
|
|
|
|
|
|
if (! (premium::enabled() && \get_config("local_treestudyplan", "enablecoach"))) {
|
2024-03-10 21:51:37 +01:00
|
|
|
// If coach role is not available, return false immediately.
|
|
|
|
return false;
|
|
|
|
}
|
2024-06-02 19:23:40 +02:00
|
|
|
if ($user == null) {
|
2024-03-09 22:51:34 +01:00
|
|
|
$user = $USER;
|
|
|
|
$userid = $USER->id;
|
|
|
|
} else if (is_numeric($user)) {
|
|
|
|
$userid = intval($user);
|
|
|
|
} else {
|
|
|
|
$userid = $user->id;
|
|
|
|
}
|
2024-06-02 23:23:32 +02:00
|
|
|
$r = $DB->get_record(self::TABLE_COACH, ["studyplan_id" => $this->id, "user_id" => $userid]);
|
2024-03-09 22:51:34 +01:00
|
|
|
|
2024-06-02 19:23:40 +02:00
|
|
|
if ($r && has_capability(associationservice::CAP_COACH, $this->context(), $userid)) {
|
2024-03-09 22:51:34 +01:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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, 'id of studyplan'),
|
2023-11-27 23:18:55 +01:00
|
|
|
"userid" => new \external_value(PARAM_INT, 'id of user the plan is shown for'),
|
2023-05-17 21:19:14 +02:00
|
|
|
"name" => new \external_value(PARAM_TEXT, 'name of studyplan'),
|
2023-08-25 10:41:56 +02:00
|
|
|
"shortname" => new \external_value(PARAM_TEXT, 'shortname of studyplan'),
|
2023-10-21 23:24:25 +02:00
|
|
|
"description" => new \external_value(PARAM_RAW, 'description of studyplan'),
|
|
|
|
"descriptionformat" => new \external_value(PARAM_INT, 'description format'),
|
2024-06-02 19:23:40 +02:00
|
|
|
"icon" => new \external_value(PARAM_RAW, 'icon for this plan'),
|
|
|
|
"progress" => new \external_value(PARAM_FLOAT, "fraction of completed modules"),
|
2023-08-25 10:41:56 +02:00
|
|
|
"idnumber" => new \external_value(PARAM_TEXT, 'idnumber of curriculum'),
|
2023-07-23 16:25:08 +02:00
|
|
|
"pages" => new \external_multiple_structure(studyplanpage::user_structure()),
|
2023-05-17 21:19:14 +02:00
|
|
|
"aggregation_info" => aggregator::basic_structure(),
|
2023-08-24 23:02:41 +02:00
|
|
|
], 'Studyplan with user info', $value);
|
2023-05-17 21:19:14 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-10-23 23:19:14 +02:00
|
|
|
/**
|
|
|
|
* Scan user progress (completed modules) over all pages for a specific user
|
|
|
|
* @param int $userid ID of user to check for
|
|
|
|
* @return float Fraction of completion
|
|
|
|
*/
|
|
|
|
private function scanuserprogress($userid) {
|
|
|
|
$progress = 0;
|
|
|
|
$pages = $this->pages();
|
|
|
|
foreach ($pages as $p) {
|
2023-12-01 11:12:21 +01:00
|
|
|
$prg = $p->scanuserprogress($userid);
|
|
|
|
$progress += $prg;
|
2023-10-23 23:19:14 +02:00
|
|
|
}
|
2024-06-02 23:23:32 +02:00
|
|
|
// Now average it out over the amount of pages.
|
|
|
|
if (count($pages) > 0) {
|
2023-12-01 11:12:21 +01:00
|
|
|
return $progress / count($pages);
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2023-10-23 23:19:14 +02:00
|
|
|
}
|
|
|
|
|
2023-08-27 21:57:21 +02:00
|
|
|
/**
|
|
|
|
* Webservice model for user info
|
|
|
|
* @param int $userid ID of user to check specific info for
|
|
|
|
* @return array Webservice data model
|
|
|
|
*/
|
2023-08-24 23:02:41 +02:00
|
|
|
public function user_model($userid) {
|
2023-12-01 11:12:21 +01:00
|
|
|
$progress = $this->scanuserprogress($userid);
|
|
|
|
if (is_nan($progress)) {
|
|
|
|
$progress = 0;
|
|
|
|
}
|
2023-05-17 21:19:14 +02:00
|
|
|
$model = [
|
|
|
|
'id' => $this->r->id,
|
2023-11-27 23:18:55 +01:00
|
|
|
'userid' => $userid,
|
2023-05-17 21:19:14 +02:00
|
|
|
'name' => $this->r->name,
|
|
|
|
'shortname' => $this->r->shortname,
|
2023-10-23 21:54:09 +02:00
|
|
|
'description' => $this->description(),
|
2023-10-21 23:24:25 +02:00
|
|
|
'descriptionformat' => $this->r->descriptionformat,
|
2023-10-23 21:54:09 +02:00
|
|
|
'icon' => $this->icon(),
|
2023-08-09 09:48:06 +02:00
|
|
|
'idnumber' => $this->r->idnumber,
|
2023-12-01 11:12:21 +01:00
|
|
|
'progress' => $progress,
|
2023-07-23 16:25:08 +02:00
|
|
|
'pages' => [],
|
2023-05-17 21:19:14 +02:00
|
|
|
'aggregation_info' => $this->aggregator->basic_model(),
|
|
|
|
];
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
foreach ($this->pages() as $p) {
|
2023-07-23 16:25:08 +02:00
|
|
|
$model['pages'][] = $p->user_model($userid);
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
return $model;
|
|
|
|
}
|
|
|
|
|
2023-08-28 11:26:14 +02:00
|
|
|
/**
|
|
|
|
* Duplicate a studyplan by id
|
|
|
|
* Function used by webservices and returns webservices model
|
|
|
|
* @param int $planid Id if studyplan
|
|
|
|
* @param string $name New fullname of studyplan
|
|
|
|
* @param string $shortname New shortname of studyplan
|
|
|
|
* @return array Simple webservices model of plan
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public static function duplicate_plan($planid, $name, $shortname): array {
|
2023-08-25 17:33:20 +02:00
|
|
|
$ori = self::find_by_id($planid);
|
2024-06-02 19:23:40 +02:00
|
|
|
$new = $ori->duplicate($name, $shortname, $ori->context()->id);
|
2023-05-17 21:19:14 +02:00
|
|
|
return $new->simple_model();
|
|
|
|
}
|
|
|
|
|
2023-08-28 11:26:14 +02:00
|
|
|
/**
|
|
|
|
* Duplicate this studyplan
|
|
|
|
* @param string $name New fullname of studyplan
|
|
|
|
* @param string $shortname New shortname of studyplan
|
2024-06-03 04:00:46 +02:00
|
|
|
* @param int $contextid Id of context for new plan
|
|
|
|
* @param string|null $idnumber New idnumber (duplicate old one if left empty)
|
2024-06-03 23:24:16 +02:00
|
|
|
* @param string|null $newstartdate If provided, all dates in the copy will be shifted so the plan starts at this date
|
2023-08-28 11:26:14 +02:00
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public function duplicate($name, $shortname, $contextid, $idnumber=null, $newstartdate = null): self {
|
2023-08-24 23:02:41 +02:00
|
|
|
// First duplicate the studyplan structure.
|
2023-08-25 17:33:20 +02:00
|
|
|
$newplan = self::add([
|
2023-05-17 21:19:14 +02:00
|
|
|
'name' => $name,
|
|
|
|
'shortname' => $shortname,
|
2024-06-02 19:23:40 +02:00
|
|
|
'idnumber' => ($idnumber ? $idnumber : $this->r->idnumber),
|
2024-04-19 16:46:30 +02:00
|
|
|
'context_id' => $contextid,
|
2023-05-17 21:19:14 +02:00
|
|
|
'description' => $this->r->description,
|
2023-10-23 21:54:09 +02:00
|
|
|
'descriptionformat' => $this->r->descriptionformat,
|
|
|
|
'aggregation' => $this->r->aggregation,
|
2024-06-02 23:23:32 +02:00
|
|
|
'aggregation_config' => $this->r->aggregation_config,
|
2024-06-02 19:23:40 +02:00
|
|
|
], true);
|
2023-05-17 21:19:14 +02:00
|
|
|
|
2024-04-19 16:46:30 +02:00
|
|
|
// Copy any files related to this studyplan.
|
2024-06-02 19:23:40 +02:00
|
|
|
$areas = ["icon", "studyplan"];
|
2024-04-19 16:46:30 +02:00
|
|
|
$fs = \get_file_storage();
|
|
|
|
foreach ($areas as $area) {
|
|
|
|
$files = $fs->get_area_files(
|
|
|
|
\context_system::instance()->id,
|
|
|
|
"local_treestudyplan",
|
|
|
|
$area,
|
|
|
|
$this->id()
|
|
|
|
);
|
|
|
|
foreach ($files as $file) {
|
|
|
|
$path = $file->get_filepath();
|
|
|
|
$filename = $file->get_filename();
|
|
|
|
if ($filename != ".") {
|
|
|
|
// Prepare new file info for the target file.
|
|
|
|
$fileinfo = [
|
|
|
|
'contextid' => \context_system::instance()->id, // System context.
|
|
|
|
'component' => 'local_treestudyplan', // Your component name.
|
|
|
|
'filearea' => $area, // Area name.
|
|
|
|
'itemid' => $newplan->id(), // Study plan id.
|
|
|
|
'filepath' => $path, // Original file path.
|
|
|
|
'filename' => $filename, // Original file name.
|
|
|
|
];
|
|
|
|
// Copy existing file into new file.
|
|
|
|
$fs->create_file_from_storedfile($fileinfo, $file);
|
|
|
|
debug::write("Copied {$area}::{$path}{$filename} from {$this->id} to {$newplan->id()}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-23 21:54:09 +02:00
|
|
|
|
2023-08-25 09:44:34 +02:00
|
|
|
// Next, copy the studylines.
|
2024-06-02 19:23:40 +02:00
|
|
|
$timeless = \get_config("local_treestudyplan", "timelessperiods");
|
2024-04-19 16:46:30 +02:00
|
|
|
if (!$timeless && $newstartdate) {
|
2024-06-02 19:23:40 +02:00
|
|
|
$newstart = new \DateTime(date("Y-m-d", $newstartdate));
|
2024-04-19 16:46:30 +02:00
|
|
|
$oldstart = $this->startdate();
|
|
|
|
$timeoffset = $oldstart->diff($newstart);
|
|
|
|
} else {
|
|
|
|
$timeoffset = new \DateInterval("P0D");
|
|
|
|
}
|
2023-08-24 23:02:41 +02:00
|
|
|
|
2024-04-19 16:46:30 +02:00
|
|
|
foreach ($this->pages() as $page) {
|
2024-06-02 19:23:40 +02:00
|
|
|
$newchild = $page->duplicate($newplan, $timeoffset);
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
|
2023-08-07 23:07:59 +02:00
|
|
|
return $newplan;
|
2023-08-24 23:02:41 +02:00
|
|
|
}
|
2023-05-17 21:19:14 +02:00
|
|
|
|
2023-08-27 23:27:07 +02:00
|
|
|
/**
|
|
|
|
* Description of export structure for webservices
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public static function export_structure(): \external_description {
|
2023-05-17 21:19:14 +02:00
|
|
|
return new \external_single_structure([
|
|
|
|
"format" => new \external_value(PARAM_TEXT, 'format of studyplan export'),
|
2023-11-11 20:17:45 +01:00
|
|
|
"content" => new \external_value(PARAM_RAW, 'exported studyplan content'),
|
2023-08-24 23:02:41 +02:00
|
|
|
], 'Exported studyplan');
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
|
2023-08-28 11:26:14 +02:00
|
|
|
/**
|
|
|
|
* Export this page into a json model
|
|
|
|
* @return array
|
|
|
|
*/
|
2023-08-24 23:02:41 +02:00
|
|
|
public function export_plan() {
|
2023-05-17 21:19:14 +02:00
|
|
|
$model = $this->export_model();
|
|
|
|
$json = json_encode([
|
2023-08-25 10:41:56 +02:00
|
|
|
"type" => "studyplan",
|
|
|
|
"version" => 2.0,
|
2024-06-02 23:23:32 +02:00
|
|
|
"studyplan" => $model,
|
2023-08-24 23:02:41 +02:00
|
|
|
], \JSON_PRETTY_PRINT);
|
2023-05-17 21:19:14 +02:00
|
|
|
return [ "format" => "application/json", "content" => $json];
|
|
|
|
}
|
|
|
|
|
2023-08-27 21:57:21 +02:00
|
|
|
/**
|
|
|
|
* Export essential information for export
|
|
|
|
* @return array information model
|
|
|
|
*/
|
2023-08-24 23:02:41 +02:00
|
|
|
public function export_model() {
|
2023-12-12 22:42:37 +01:00
|
|
|
$exportfiles = $this->export_files('studyplan');
|
|
|
|
$iconfiles = $this->export_files('icon');
|
2023-05-17 21:19:14 +02:00
|
|
|
$model = [
|
|
|
|
'name' => $this->r->name,
|
|
|
|
'shortname' => $this->r->shortname,
|
|
|
|
'description' => $this->r->description,
|
2023-12-12 22:42:37 +01:00
|
|
|
'descriptionformat' => $this->r->descriptionformat,
|
2023-05-17 21:19:14 +02:00
|
|
|
"aggregation" => $this->r->aggregation,
|
|
|
|
"aggregation_config" => json_decode($this->aggregator->config_string()),
|
|
|
|
'aggregation_info' => $this->aggregator->basic_model(),
|
2023-07-23 16:25:08 +02:00
|
|
|
'pages' => $this->export_pages_model(),
|
2023-12-12 22:42:37 +01:00
|
|
|
'files' => $exportfiles,
|
|
|
|
'iconfiles' => $iconfiles,
|
2023-05-17 21:19:14 +02:00
|
|
|
];
|
|
|
|
return $model;
|
|
|
|
}
|
|
|
|
|
2023-12-12 22:42:37 +01:00
|
|
|
/**
|
|
|
|
* Export files from file storage
|
|
|
|
* @param string $area Name of the file area to export
|
|
|
|
* @return array information model
|
|
|
|
*/
|
|
|
|
public function export_files($area) {
|
|
|
|
$exportfiles = [];
|
|
|
|
$fs = get_file_storage();
|
|
|
|
$files = $fs->get_area_files(
|
|
|
|
\context_system::instance()->id,
|
|
|
|
'local_treestudyplan',
|
|
|
|
$area,
|
|
|
|
$this->id);
|
|
|
|
foreach ($files as $file) {
|
|
|
|
if ($file->get_filename() != ".") {
|
|
|
|
$contents = $file->get_content();
|
|
|
|
$exportfiles[] = [
|
|
|
|
"name" => $file->get_filename(),
|
|
|
|
"path" => $file->get_filepath(),
|
|
|
|
"content" => convert_uuencode($contents),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $exportfiles;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Import previously exported files into the file storage
|
|
|
|
* @param mixed $importfiles List of files to import from string in the format exported in export_model()
|
2024-06-03 04:00:46 +02:00
|
|
|
* @param string $area Name of the file area to import
|
2023-12-12 22:42:37 +01:00
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public function import_files($importfiles, $area) {
|
2023-12-12 22:42:37 +01:00
|
|
|
$fs = get_file_storage();
|
2024-06-02 23:23:32 +02:00
|
|
|
foreach ($importfiles as $file) {
|
2023-12-12 22:42:37 +01:00
|
|
|
if ($file['name'] != ".") {
|
|
|
|
$fileinfo = [
|
2024-06-02 23:23:32 +02:00
|
|
|
'contextid' => \context_system::instance()->id, // ID of the system context.
|
2023-12-12 22:42:37 +01:00
|
|
|
'component' => 'local_treestudyplan', // Your component name.
|
|
|
|
'filearea' => $area, // Usually = table name.
|
|
|
|
'itemid' => $this->id, // ID of the studyplanpage.
|
|
|
|
'filepath' => $file["path"], // Path of the file.
|
|
|
|
'filename' => $file["name"], // Name of the file..
|
|
|
|
];
|
2024-06-02 19:23:40 +02:00
|
|
|
|
2023-12-12 22:42:37 +01:00
|
|
|
$fs->create_file_from_string($fileinfo, convert_uudecode($file["content"]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-27 21:57:21 +02:00
|
|
|
/**
|
2023-08-27 22:20:17 +02:00
|
|
|
* Export all pages
|
2023-08-27 21:57:21 +02:00
|
|
|
* @return array information model
|
|
|
|
*/
|
2023-08-24 23:02:41 +02:00
|
|
|
public function export_pages_model() {
|
2023-07-23 16:25:08 +02:00
|
|
|
$pages = [];
|
2023-08-24 23:02:41 +02:00
|
|
|
foreach ($this->pages() as $p) {
|
2023-07-23 16:25:08 +02:00
|
|
|
$pages[] = $p->export_model();
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
2023-07-23 16:25:08 +02:00
|
|
|
return $pages;
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
|
2023-08-28 11:26:14 +02:00
|
|
|
/**
|
|
|
|
* Import studyplan from file contents
|
|
|
|
* @param string $content String
|
|
|
|
* @param string $format Format description
|
|
|
|
* @param int $contextid The context to import into
|
|
|
|
*/
|
2023-08-25 13:04:19 +02:00
|
|
|
public static function import_studyplan($content, $format = "application/json", $contextid = 1) {
|
2023-08-25 10:41:56 +02:00
|
|
|
if ($format != "application/json") {
|
|
|
|
return false;
|
|
|
|
}
|
2023-08-24 23:02:41 +02:00
|
|
|
|
|
|
|
$content = json_decode($content, true);
|
|
|
|
if ($content["type"] == "studyplan" && $content["version"] >= 2.0) {
|
|
|
|
|
2023-12-12 22:42:37 +01:00
|
|
|
$planmodel = $content["studyplan"];
|
2023-08-24 23:02:41 +02:00
|
|
|
// Make sure the aggregation_config is re-encoded as json text.
|
2023-12-12 22:42:37 +01:00
|
|
|
$planmodel["aggregation_config"] = json_encode( $planmodel["aggregation_config"]);
|
2023-05-17 21:19:14 +02:00
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
// And make sure the context_id is set to the provided context for import.
|
2023-12-12 22:42:37 +01:00
|
|
|
$planmodel["context_id"] = $contextid;
|
2023-07-23 16:25:08 +02:00
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
// Create a new plan, based on the given parameters - this is the import studyplan part.
|
2023-12-12 22:42:37 +01:00
|
|
|
$plan = self::add( $planmodel, true);
|
|
|
|
|
2024-06-02 23:23:32 +02:00
|
|
|
// Import the files.
|
2023-12-12 22:42:37 +01:00
|
|
|
if (isset( $planmodel['files']) && is_array( $planmodel['files'])) {
|
2024-06-02 19:23:40 +02:00
|
|
|
$plan->import_files( $planmodel['files'], "studyplan");
|
2023-12-12 22:42:37 +01:00
|
|
|
}
|
2024-06-02 23:23:32 +02:00
|
|
|
// Import the icon.
|
2023-12-12 22:42:37 +01:00
|
|
|
if (isset( $planmodel['iconfiles']) && is_array( $planmodel['iconfiles'])) {
|
2024-06-02 19:23:40 +02:00
|
|
|
$plan->import_files( $planmodel['iconfiles'], "icon");
|
2023-12-12 22:42:37 +01:00
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
// Now import each page.
|
2023-12-12 22:42:37 +01:00
|
|
|
return $plan->import_pages_model($planmodel["pages"]);
|
2023-05-17 21:19:14 +02:00
|
|
|
|
2023-08-24 23:09:20 +02:00
|
|
|
} else {
|
2023-08-25 13:04:19 +02:00
|
|
|
debugging("Invalid format and type: {$content['type']} version {$content['version']}");
|
2023-05-17 21:19:14 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-28 11:26:14 +02:00
|
|
|
/**
|
|
|
|
* Import studyplan pages from file contents
|
|
|
|
* @param string $content String
|
|
|
|
* @param string $format Format description
|
|
|
|
*/
|
2023-08-25 13:04:19 +02:00
|
|
|
public function import_pages($content, $format = "application/json") {
|
2023-08-25 10:41:56 +02:00
|
|
|
if ($format != "application/json") {
|
|
|
|
return false;
|
|
|
|
}
|
2023-08-24 23:02:41 +02:00
|
|
|
$content = json_decode($content, true);
|
|
|
|
if ($content["version"] >= 2.0) {
|
|
|
|
if ($content["type"] == "studyplanpage") {
|
2023-08-25 09:44:34 +02:00
|
|
|
// Import single page from a studyplanpage (wrapped in array of one page).
|
2023-07-23 16:25:08 +02:00
|
|
|
return $this->import_pages_model([$content["page"]]);
|
2023-08-24 23:09:20 +02:00
|
|
|
} else if ($content["type"] == "studyplan") {
|
2023-08-24 23:02:41 +02:00
|
|
|
// Import all pages from the studyplan.
|
2023-07-23 16:25:08 +02:00
|
|
|
return $this->import_pages_model($content["studyplan"]["pages"]);
|
|
|
|
}
|
2023-08-24 23:09:20 +02:00
|
|
|
} else {
|
2023-05-17 21:19:14 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-28 11:26:14 +02:00
|
|
|
/**
|
|
|
|
* Import pages from decoded array model
|
|
|
|
* @param array $model Decoded array
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
protected function import_pages_model($model): bool {
|
2023-08-25 10:41:56 +02:00
|
|
|
$this->pages(); // Make sure the page cache is initialized, since we will be adding to it.
|
2023-08-24 23:02:41 +02:00
|
|
|
foreach ($model as $p) {
|
2023-07-23 16:25:08 +02:00
|
|
|
$p["studyplan_id"] = $this->id();
|
|
|
|
$page = studyplanpage::add($p);
|
|
|
|
$this->page_cache[] = $page;
|
2023-07-27 16:58:23 +02:00
|
|
|
$page->import_periods_model($p["perioddesc"]);
|
2023-07-23 16:25:08 +02:00
|
|
|
$page->import_studylines_model($p["studylines"]);
|
2023-12-12 22:42:37 +01:00
|
|
|
if ($p['files']) {
|
2024-06-02 19:23:40 +02:00
|
|
|
$page->import_files($p["files"], 'studyplanpage');
|
2023-12-12 22:42:37 +01:00
|
|
|
}
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-06-27 07:33:27 +02:00
|
|
|
/**
|
|
|
|
* Mark the studyplan as changed regarding courses and associated cohorts
|
|
|
|
*/
|
2023-08-24 23:02:41 +02:00
|
|
|
public function mark_csync_changed() {
|
2023-06-27 07:33:27 +02:00
|
|
|
global $DB;
|
2023-08-24 23:02:41 +02:00
|
|
|
$DB->update_record(self::TABLE, ['id' => $this->id, "csync_flag" => 1]);
|
2023-08-25 11:52:05 +02:00
|
|
|
// Manually set it in the cache, if something unexpected happened, an exception has already been thrown anyway.
|
|
|
|
$this->r->csync_flag = 1;
|
2023-06-27 07:33:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-08-28 11:26:14 +02:00
|
|
|
* Clear the studyplan as changed regarding courses and associated cohorts
|
2023-06-27 07:33:27 +02:00
|
|
|
*/
|
2023-08-24 23:02:41 +02:00
|
|
|
public function clear_csync_changed() {
|
2023-06-27 07:33:27 +02:00
|
|
|
global $DB;
|
2023-08-24 23:02:41 +02:00
|
|
|
$DB->update_record(self::TABLE, ['id' => $this->id, "csync_flag" => 0]);
|
2023-08-25 11:52:05 +02:00
|
|
|
// Manually set it in the cache, if something unexpected happened, an exception has already been thrown anyway.
|
|
|
|
$this->r->csync_flag = 0;
|
2023-06-27 07:33:27 +02:00
|
|
|
}
|
|
|
|
|
2023-08-28 11:26:14 +02:00
|
|
|
/**
|
|
|
|
* Check if the studyplan as changed regarding courses and associated cohorts
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public function has_csync_changed(): bool {
|
2023-08-25 10:41:56 +02:00
|
|
|
return ($this->r->csync_flag > 0) ? true : false;
|
2023-06-27 07:33:27 +02:00
|
|
|
}
|
|
|
|
|
2023-06-16 13:49:47 +02:00
|
|
|
/**
|
|
|
|
* See if the specified course id is linked in this studyplan
|
2023-08-28 11:26:14 +02:00
|
|
|
* @param int $courseid Id of course to check
|
2023-06-16 13:49:47 +02:00
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public function course_linked($courseid): bool {
|
2023-06-16 13:49:47 +02:00
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$sql = "SELECT COUNT(i.id)
|
|
|
|
FROM {local_treestudyplan}
|
|
|
|
INNER JOIN {local_treestudyplan_line} l ON p.id = l.studyplan_id
|
2023-12-01 11:12:21 +01:00
|
|
|
{local_treestudyplan_item} i ON l.id = i.line_id
|
2023-08-24 23:02:41 +02:00
|
|
|
WHERE p.id = :planid
|
2023-06-16 13:49:47 +02:00
|
|
|
AND i.course_id = :courseid";
|
2023-08-24 23:02:41 +02:00
|
|
|
$count = $DB->get_field_sql($sql, ["courseid" => $courseid, "planid" => $this->id]);
|
2023-06-16 13:49:47 +02:00
|
|
|
|
2023-08-25 10:41:56 +02:00
|
|
|
return ($count > 0) ? true : false;
|
2023-06-16 13:49:47 +02:00
|
|
|
}
|
|
|
|
|
2023-06-16 23:12:17 +02:00
|
|
|
/**
|
2024-03-08 11:54:39 +01:00
|
|
|
* Get all study lines linked to this plan (quickly)
|
2023-06-16 23:12:17 +02:00
|
|
|
* Used for cohort enrolment cascading
|
2024-03-08 11:54:39 +01:00
|
|
|
* @return studyline[]
|
2023-06-16 23:12:17 +02:00
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public function get_all_studylines(): array {
|
2023-06-16 23:12:17 +02:00
|
|
|
global $DB;
|
|
|
|
|
2024-03-08 11:54:39 +01:00
|
|
|
$sql = "SELECT l.id
|
2023-06-19 22:48:33 +02:00
|
|
|
FROM {local_treestudyplan} p
|
2023-08-07 22:20:45 +02:00
|
|
|
INNER JOIN {local_treestudyplan_page} pg ON p.id = pg.studyplan_id
|
|
|
|
INNER JOIN {local_treestudyplan_line} l ON pg.id = l.page_id
|
2024-03-08 11:54:39 +01:00
|
|
|
WHERE p.id = :studyplan_id";
|
|
|
|
$fields = $DB->get_fieldset_sql($sql, ["studyplan_id" => $this->id]);
|
2023-06-19 22:48:33 +02:00
|
|
|
|
2024-03-08 11:54:39 +01:00
|
|
|
$list = [];
|
2024-06-02 23:23:32 +02:00
|
|
|
foreach ($fields as $id) {
|
2024-03-08 11:54:39 +01:00
|
|
|
$list[] = studyline::find_by_id($id);
|
|
|
|
}
|
|
|
|
return $list;
|
2023-06-19 22:48:33 +02:00
|
|
|
}
|
2023-06-16 23:12:17 +02:00
|
|
|
|
2023-06-19 22:48:33 +02:00
|
|
|
/**
|
|
|
|
* List the cohort id's associated with this studyplan
|
|
|
|
*/
|
2023-08-24 23:02:41 +02:00
|
|
|
public function get_linked_cohort_ids() {
|
2023-08-25 09:44:34 +02:00
|
|
|
global $CFG, $DB;
|
2023-06-19 22:48:33 +02:00
|
|
|
|
|
|
|
$sql = "SELECT DISTINCT j.cohort_id FROM {local_treestudyplan_cohort} j
|
|
|
|
WHERE j.studyplan_id = :studyplan_id";
|
2023-06-26 21:44:31 +02:00
|
|
|
$fields = $DB->get_fieldset_sql($sql, ['studyplan_id' => $this->id]);
|
2023-06-16 23:12:17 +02:00
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
2023-06-30 12:14:11 +02:00
|
|
|
/**
|
|
|
|
* List the user id's explicitly associated with this studyplan
|
2023-08-28 11:26:14 +02:00
|
|
|
* @return int[]
|
2023-06-30 12:14:11 +02:00
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public function get_linked_user_ids(): array {
|
2023-08-25 09:44:34 +02:00
|
|
|
global $CFG, $DB;
|
2023-06-19 22:48:33 +02:00
|
|
|
|
2023-06-30 12:14:11 +02:00
|
|
|
$sql = "SELECT DISTINCT j.user_id FROM {local_treestudyplan_user} j
|
|
|
|
WHERE j.studyplan_id = :studyplan_id";
|
|
|
|
$fields = $DB->get_fieldset_sql($sql, ['studyplan_id' => $this->id]);
|
|
|
|
return $fields;
|
|
|
|
}
|
2023-06-16 13:49:47 +02:00
|
|
|
/**
|
2023-08-28 11:26:14 +02:00
|
|
|
* See if the specified badge is linked in this studyplan
|
|
|
|
* @param int $badgeid Badge id
|
2023-06-16 13:49:47 +02:00
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public function badge_linked($badgeid): bool {
|
2023-06-16 13:49:47 +02:00
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$sql = "SELECT COUNT(i.id)
|
|
|
|
FROM {local_treestudyplan}
|
|
|
|
INNER JOIN {local_treestudyplan_line} l ON p.id = l.studyplan_id
|
|
|
|
INNER JOIN {local_treestudyplan_item} i ON l.id = i.line_id
|
2023-08-24 23:02:41 +02:00
|
|
|
WHERE p.id = :planid
|
2023-06-16 13:49:47 +02:00
|
|
|
AND i.badge_id = :badgeid";
|
2023-08-24 23:02:41 +02:00
|
|
|
$count = $DB->get_field_sql($sql, ["badgeid" => $badgeid, "planid" => $this->id]);
|
2023-06-16 13:49:47 +02:00
|
|
|
|
2023-08-25 10:41:56 +02:00
|
|
|
return ($count > 0) ? true : false;
|
2023-06-16 13:49:47 +02:00
|
|
|
}
|
2023-08-25 11:52:05 +02:00
|
|
|
}
|