1455 lines
54 KiB
PHP
1455 lines
54 KiB
PHP
<?php
|
|
// This file is part of the Studyplan plugin for Moodle
|
|
//
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.
|
|
/**
|
|
*
|
|
* @package local_treestudyplan
|
|
* @copyright 2023 P.M. Kuipers
|
|
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
*/
|
|
|
|
namespace local_treestudyplan;
|
|
defined('MOODLE_INTERNAL') || die();
|
|
|
|
use local_treestudyplan\local\helpers\webservicehelper;
|
|
|
|
require_once($CFG->libdir.'/externallib.php');
|
|
require_once($CFG->libdir.'/badgeslib.php');
|
|
require_once($CFG->libdir.'/gradelib.php');
|
|
require_once($CFG->dirroot.'/course/modlib.php');
|
|
|
|
class studyplanservice extends \external_api {
|
|
|
|
const CAP_EDIT = "local/treestudyplan:editstudyplan";
|
|
const CAP_VIEW = "local/treestudyplan:viewuserreports";
|
|
|
|
/************************
|
|
* *
|
|
* list_studyplans *
|
|
* *
|
|
************************/
|
|
|
|
/**
|
|
* Parameter description for webservice function list_studyplans
|
|
* @return \external_function_parameters
|
|
*/
|
|
public static function list_studyplans_parameters() {
|
|
return new \external_function_parameters([
|
|
"context_id" => new \external_value(PARAM_INT, 'context to search in for studyplans', VALUE_DEFAULT),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Return value description for webservice function list_studyplans
|
|
* @return \external_description
|
|
*/
|
|
public static function list_studyplans_returns() {
|
|
return new \external_multiple_structure( studyplan::simple_structure() );
|
|
}
|
|
|
|
public static function list_studyplans($contextid = 0) {
|
|
global $CFG, $DB;
|
|
|
|
// Check if the user has the correct permissions for this context.
|
|
$context = webservicehelper::find_context($contextid);
|
|
webservicehelper::require_capabilities([self::CAP_EDIT, self::CAP_VIEW], $context);
|
|
|
|
// Now list all the studyplans in the relevant context.
|
|
$list = [];
|
|
$studyplans = studyplan::find_all($contextid);
|
|
foreach ($studyplans as $studyplan) {
|
|
$list[] = $studyplan->simple_model();
|
|
}
|
|
return $list;
|
|
|
|
}
|
|
|
|
/************************
|
|
* *
|
|
* get_studyplan_map *
|
|
* *
|
|
************************/
|
|
|
|
/**
|
|
* Parameter description for webservice function get_studyplan_map
|
|
* @return \external_function_parameters
|
|
*/
|
|
public static function get_studyplan_map_parameters() {
|
|
return new \external_function_parameters( [
|
|
"id" => new \external_value(PARAM_INT, 'id of a studyplan to check usage on', VALUE_REQUIRED),
|
|
] );
|
|
}
|
|
|
|
/**
|
|
* Return value description for webservice function get_studyplan_map
|
|
* @return \external_description
|
|
*/
|
|
public static function get_studyplan_map_returns() {
|
|
return studyplan::editor_structure();
|
|
}
|
|
|
|
public static function get_studyplan_map($id) {
|
|
if (isset($id) && $id > 0) {
|
|
$studyplan = studyplan::find_by_id($id);
|
|
webservicehelper::require_capabilities([self::CAP_EDIT, self::CAP_VIEW], $studyplan->context());
|
|
return $studyplan->editor_model();
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/************************
|
|
* *
|
|
* get_studyline_map *
|
|
* *
|
|
************************/
|
|
|
|
/**
|
|
* Parameter description for webservice function get_studyline_map
|
|
* @return \external_function_parameters
|
|
*/
|
|
public static function get_studyline_map_parameters() {
|
|
return new \external_function_parameters( [
|
|
"id" => new \external_value(PARAM_INT, 'id of a studyline to check usage on', VALUE_DEFAULT),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Return value description for webservice function get_studyline_map
|
|
* @return \external_description
|
|
*/
|
|
public static function get_studyline_map_returns() {
|
|
return new \external_multiple_structure( studyline::editor_structure() );
|
|
}
|
|
|
|
public static function get_studyline_map($id) {
|
|
|
|
$o = studyline::find_by_id($id);
|
|
webservicehelper::require_capabilities([self::CAP_EDIT, self::CAP_VIEW], $o->context());
|
|
return $o->editor_model();
|
|
}
|
|
|
|
/************************
|
|
* *
|
|
* add_studyplan *
|
|
* *
|
|
************************/
|
|
|
|
/**
|
|
* Parameter description for webservice function add_studyplan
|
|
* @return \external_function_parameters
|
|
*/
|
|
public static function add_studyplan_parameters() {
|
|
return new \external_function_parameters( [
|
|
"name" => new \external_value(PARAM_TEXT, 'name of studyplan'),
|
|
"shortname" => new \external_value(PARAM_TEXT, 'shortname of studyplan'),
|
|
"idnumber" => new \external_value(PARAM_TEXT, 'idnumber of studyplan'),
|
|
"description" => new \external_value(PARAM_TEXT, 'description of studyplan'),
|
|
"periods" => new \external_value(PARAM_INT, 'number of periods in studyplan'),
|
|
"startdate" => new \external_value(PARAM_TEXT, 'start date of studyplan'),
|
|
"enddate" => new \external_value(PARAM_TEXT, 'end date of studyplan'),
|
|
"aggregation" => new \external_value(PARAM_TEXT, 'selected aggregator', VALUE_DEFAULT),
|
|
"aggregation_config" => new \external_value(PARAM_TEXT, 'config string for aggregator', VALUE_DEFAULT),
|
|
"context_id" => new \external_value(PARAM_INT, 'context of the study plan', VALUE_DEFAULT),
|
|
] );
|
|
}
|
|
|
|
/**
|
|
* Return value description for webservice function add_studyplan
|
|
* @return \external_description
|
|
*/
|
|
public static function add_studyplan_returns() {
|
|
return studyplan::simple_structure();
|
|
}
|
|
|
|
public static function add_studyplan($name, $shortname, $idnumber, $description, $periods,
|
|
$startdate, $enddate, $aggregation = "bistate", $aggregationconfig = '', $contextid = 0) {
|
|
// Check if we have the proper rights for the requested context.
|
|
$context = webservicehelper::find_context($contextid);
|
|
webservicehelper::require_capabilities(self::CAP_EDIT, $context);
|
|
|
|
$o = studyplan::add([
|
|
'name' => $name,
|
|
'shortname' => $shortname,
|
|
'idnumber' => $idnumber,
|
|
'description' => $description,
|
|
'periods' => $periods,
|
|
'startdate' => $startdate,
|
|
'enddate' => empty($enddate) ? null : $enddate,
|
|
'aggregation' => $aggregation,
|
|
'aggregation_config' => $aggregationconfig,
|
|
'context_id' => $contextid,
|
|
]);
|
|
return $o->simple_model();
|
|
}
|
|
|
|
/************************
|
|
* *
|
|
* edit_studyplan *
|
|
* *
|
|
************************/
|
|
|
|
/**
|
|
* Parameter description for webservice function edit_studyplan
|
|
* @return \external_function_parameters
|
|
*/
|
|
public static function edit_studyplan_parameters() {
|
|
return new \external_function_parameters( [
|
|
"id" => new \external_value(PARAM_INT, 'id of studyplan'),
|
|
"name" => new \external_value(PARAM_TEXT, 'name of studyplan'),
|
|
"shortname" => new \external_value(PARAM_TEXT, 'shortname of studyplan'),
|
|
"idnumber" => new \external_value(PARAM_TEXT, 'idnumber of studyplan'),
|
|
"description" => new \external_value(PARAM_TEXT, 'description of studyplan'),
|
|
"periods" => new \external_value(PARAM_INT, 'number of periods in studyplan'),
|
|
"startdate" => new \external_value(PARAM_TEXT, 'start date of studyplan'),
|
|
"enddate" => new \external_value(PARAM_TEXT, 'end date of studyplan'),
|
|
"aggregation" => new \external_value(PARAM_TEXT, 'selected aggregator', VALUE_DEFAULT),
|
|
"aggregation_config" => new \external_value(PARAM_TEXT, 'config string for aggregator', VALUE_DEFAULT),
|
|
"context_id" => new \external_value(PARAM_INT, 'context of the study plan', VALUE_DEFAULT),
|
|
] );
|
|
}
|
|
|
|
/**
|
|
* Return value description for webservice function edit_studyplan
|
|
* @return \external_description
|
|
*/
|
|
public static function edit_studyplan_returns() {
|
|
return studyplan::simple_structure();
|
|
}
|
|
|
|
public static function edit_studyplan($id, $name, $shortname, $idnumber, $description, $periods, $startdate,
|
|
$enddate, $aggregation = "bistate", $aggregationconfig = '', $contextid = 0) {
|
|
// Validate access in the intended context.
|
|
$context = webservicehelper::find_context($contextid);
|
|
// Do not validate the context in this case, just check the permissions.
|
|
webservicehelper::require_capabilities(self::CAP_EDIT, $context, false);
|
|
|
|
$o = studyplan::find_by_id($id);
|
|
webservicehelper::require_capabilities(self::CAP_EDIT, $o->context());
|
|
|
|
$o->edit([
|
|
'name' => $name,
|
|
'shortname' => $shortname,
|
|
'idnumber' => $idnumber,
|
|
'description' => $description,
|
|
'periods' => $periods,
|
|
'startdate' => $startdate,
|
|
'enddate' => $enddate,
|
|
'aggregation' => $aggregation,
|
|
'aggregation_config' => $aggregationconfig,
|
|
'context_id' => $contextid,
|
|
]);
|
|
return $o->simple_model();
|
|
}
|
|
|
|
/************************
|
|
* *
|
|
* delete_studyplan *
|
|
* *
|
|
************************/
|
|
|
|
/**
|
|
* Parameter description for webservice function delete_studyplan
|
|
* @return \external_function_parameters
|
|
*/
|
|
public static function delete_studyplan_parameters() {
|
|
return new \external_function_parameters( [
|
|
"id" => new \external_value(PARAM_INT, 'id of studyplan'),
|
|
"force" => new \external_value(PARAM_BOOL, 'id of studyplan', VALUE_DEFAULT),
|
|
] );
|
|
}
|
|
|
|
/**
|
|
* Return value description for webservice function delete_studyplan
|
|
* @return \external_description
|
|
*/
|
|
public static function delete_studyplan_returns() {
|
|
return success::structure();
|
|
}
|
|
|
|
public static function delete_studyplan($id, $force = false) {
|
|
$o = studyplan::find_by_id($id);
|
|
// Validate if the requesting user has the right to edit the plan in it's current context.
|
|
webservicehelper::require_capabilities(self::CAP_EDIT, $o->context());
|
|
return $o->delete(!!$force)->model();
|
|
}
|
|
|
|
/************************
|
|
* *
|
|
* add_studyline *
|
|
* *
|
|
************************/
|
|
|
|
/**
|
|
* Parameter description for webservice function add_studyline
|
|
* @return \external_function_parameters
|
|
*/
|
|
public static function add_studyline_parameters() {
|
|
return new \external_function_parameters( [
|
|
"page_id" => new \external_value(PARAM_INT, 'id of studyplan to add line to'),
|
|
"name" => new \external_value(PARAM_TEXT, 'shortname of studyline'),
|
|
"shortname" => new \external_value(PARAM_TEXT, 'idnumber of studyline'),
|
|
"color" => new \external_value(PARAM_TEXT, 'description of studyline'),
|
|
"sequence" => new \external_value(PARAM_INT, 'sequence of studyline'),
|
|
] );
|
|
}
|
|
|
|
/**
|
|
* Return value description for webservice function add_studyline
|
|
* @return \external_description
|
|
*/
|
|
public static function add_studyline_returns() {
|
|
return studyline::editor_structure();
|
|
}
|
|
|
|
public static function add_studyline($pageid, $name, $shortname, $color, $sequence) {
|
|
// Validate if the requesting user has the right to edit the plan in it's current context.
|
|
$page = studyplanpage::find_by_id($pageid);
|
|
webservicehelper::require_capabilities(self::CAP_EDIT, $page->studyplan()->context());
|
|
|
|
$o = studyline::add([
|
|
'page_id' => $pageid,
|
|
'name' => $name,
|
|
'shortname' => $shortname,
|
|
'color' => $color,
|
|
'sequence' => $sequence,
|
|
]);
|
|
return $o->editor_model();
|
|
}
|
|
|
|
/************************
|
|
* *
|
|
* edit_studyline *
|
|
* *
|
|
************************/
|
|
|
|
/**
|
|
* Parameter description for webservice function edit_studyline
|
|
* @return \external_function_parameters
|
|
*/
|
|
public static function edit_studyline_parameters() {
|
|
return new \external_function_parameters( [
|
|
"id" => new \external_value(PARAM_INT, 'id of studyline'),
|
|
"name" => new \external_value(PARAM_TEXT, 'shortname of studyline'),
|
|
"shortname" => new \external_value(PARAM_TEXT, 'idnumber of studyline'),
|
|
"color" => new \external_value(PARAM_TEXT, 'description of studyline'),
|
|
] );
|
|
}
|
|
|
|
/**
|
|
* Return value description for webservice function edit_studyline
|
|
* @return \external_description
|
|
*/
|
|
public static function edit_studyline_returns() {
|
|
return studyline::editor_structure();
|
|
}
|
|
|
|
public static function edit_studyline($id, $name, $shortname, $color) {
|
|
$o = studyline::find_by_id($id);
|
|
// Validate if the requesting user has the right to edit the plan in it's current context.
|
|
webservicehelper::require_capabilities(self::CAP_EDIT, $o->context());
|
|
|
|
$o->edit([
|
|
'name' => $name,
|
|
'shortname' => $shortname,
|
|
'color' => $color,
|
|
]);
|
|
return $o->editor_model();
|
|
}
|
|
|
|
/************************
|
|
* *
|
|
* delete_studyline *
|
|
* *
|
|
************************/
|
|
|
|
/**
|
|
* Parameter description for webservice function delete_studyline
|
|
* @return \external_function_parameters
|
|
*/
|
|
public static function delete_studyline_parameters() {
|
|
return new \external_function_parameters( [
|
|
"id" => new \external_value(PARAM_INT, 'id of studyline'),
|
|
] );
|
|
}
|
|
|
|
/**
|
|
* Return value description for webservice function delete_studyline
|
|
* @return \external_description
|
|
*/
|
|
public static function delete_studyline_returns() {
|
|
return success::structure();
|
|
}
|
|
|
|
public static function delete_studyline($id) {
|
|
$o = studyline::find_by_id($id);
|
|
// Validate if the requesting user has the right to edit the plan in it's current context.
|
|
webservicehelper::require_capabilities(self::CAP_EDIT, $o->context());
|
|
return $o->delete()->model();
|
|
|
|
}
|
|
|
|
/************************
|
|
* *
|
|
* reorder_studylines *
|
|
* *
|
|
************************/
|
|
|
|
/**
|
|
* Parameter description for webservice function reorder_studylines
|
|
* @return \external_function_parameters
|
|
*/
|
|
public static function reorder_studylines_parameters() {
|
|
return new \external_function_parameters( [
|
|
"sequence" => new \external_multiple_structure(
|
|
new \external_single_structure([
|
|
"id" => new \external_value(PARAM_INT, 'id of studyline '),
|
|
"sequence" => new \external_value(PARAM_INT, 'order of studyline'),
|
|
]),
|
|
),
|
|
] );
|
|
}
|
|
|
|
/**
|
|
* Return value description for webservice function reorder_studylines
|
|
* @return \external_description
|
|
*/
|
|
public static function reorder_studylines_returns() {
|
|
return success::structure();
|
|
}
|
|
|
|
public static function reorder_studylines($resequence) {
|
|
// Validate if the requesting user has the right to edit the lines in it's current context.
|
|
foreach ($resequence as $sq) {
|
|
$o = studyline::find_by_id(($sq['id']));
|
|
webservicehelper::require_capabilities(self::CAP_EDIT, $o->context());
|
|
}
|
|
|
|
return studyline::reorder($resequence)->model();
|
|
}
|
|
|
|
/***********************************
|
|
* STUDYITEM FUNCTIONS
|
|
***********************************/
|
|
|
|
/************************
|
|
* *
|
|
* get_studyitem *
|
|
* *
|
|
************************/
|
|
|
|
/**
|
|
* Parameter description for webservice function get_studyitem
|
|
* @return \external_function_parameters
|
|
*/
|
|
public static function get_studyitem_parameters() {
|
|
return new \external_function_parameters( [
|
|
"id" => new \external_value(PARAM_INT, 'id of a study item to retrieve'),
|
|
] );
|
|
}
|
|
|
|
/**
|
|
* Return value description for webservice function get_studyitem
|
|
* @return \external_description
|
|
*/
|
|
public static function get_studyitem_returns() {
|
|
return studyitem::editor_structure();
|
|
}
|
|
|
|
public static function get_studyitem($id) {
|
|
$o = studyitem::find_by_id($id);
|
|
webservicehelper::require_capabilities([self::CAP_EDIT, self::CAP_VIEW], $o->context());
|
|
return $o->editor_model();
|
|
|
|
}
|
|
|
|
/************************
|
|
* *
|
|
* add_studyitem *
|
|
* *
|
|
************************/
|
|
|
|
/**
|
|
* Parameter description for webservice function add_studyitem
|
|
* @return \external_function_parameters
|
|
*/
|
|
public static function add_studyitem_parameters() {
|
|
return new \external_function_parameters( [
|
|
"line_id" => new \external_value(PARAM_INT, 'id of related study line'),
|
|
"type" => new \external_value(PARAM_TEXT, 'type of study item'),
|
|
"details" => new \external_single_structure([
|
|
"conditions" => new \external_value(PARAM_TEXT, 'conditions for completion', VALUE_OPTIONAL),
|
|
"competency_id" => new \external_value(PARAM_INT, 'id of referenced competency', VALUE_OPTIONAL),
|
|
"course_id" => new \external_value(PARAM_INT, 'id of referenced course', VALUE_OPTIONAL),
|
|
"badge_id" => new \external_value(PARAM_INT, 'id of referenced badge', VALUE_OPTIONAL),
|
|
"continuation_id" => new \external_value(PARAM_INT, 'id of continued item', VALUE_OPTIONAL),
|
|
]),
|
|
"slot" => new \external_value(PARAM_INT, 'slot in the study plan', VALUE_DEFAULT),
|
|
"layer" => new \external_value(PARAM_INT, 'layer in the slot', VALUE_OPTIONAL),
|
|
] );
|
|
}
|
|
|
|
/**
|
|
* Return value description for webservice function add_studyitem
|
|
* @return \external_description
|
|
*/
|
|
public static function add_studyitem_returns() {
|
|
return studyitem::editor_structure();
|
|
}
|
|
|
|
public static function add_studyitem($lineid, $type, $details, $slot = -1, $layer = 0) {
|
|
webservicehelper::require_capabilities(self::CAP_EDIT, studyline::find_by_id($lineid)->context());
|
|
|
|
$o = studyitem::add([
|
|
'line_id' => $lineid,
|
|
'type' => $type,
|
|
'slot' => $slot,
|
|
'layer' => $layer,
|
|
'competency_id' => isset($details['competency_id']) ? $details['competency_id'] : null,
|
|
'course_id' => isset($details['course_id']) ? $details['course_id'] : null,
|
|
'badge_id' => isset($details['badge_id']) ? $details['badge_id'] : null,
|
|
'continuation_id' => isset($details['continuation_id']) ? $details['continuation_id'] : null,
|
|
]);
|
|
return $o->editor_model();
|
|
}
|
|
|
|
/************************
|
|
* *
|
|
* edit_studyitem *
|
|
* *
|
|
************************/
|
|
|
|
/**
|
|
* Parameter description for webservice function edit_studyitem
|
|
* @return \external_function_parameters
|
|
*/
|
|
public static function edit_studyitem_parameters() {
|
|
return new \external_function_parameters( [
|
|
"id" => new \external_value(PARAM_INT, 'id of study item'),
|
|
"conditions" => new \external_value(PARAM_TEXT, 'conditions for completion'),
|
|
"continuation_id" => new \external_value(PARAM_INT, 'id of continued item', VALUE_DEFAULT),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Return value description for webservice function edit_studyitem
|
|
* @return \external_description
|
|
*/
|
|
public static function edit_studyitem_returns() {
|
|
return studyitem::editor_structure();
|
|
}
|
|
|
|
public static function edit_studyitem($id, $conditions, $continuationid = false) {
|
|
|
|
$o = studyitem::find_by_id($id);
|
|
webservicehelper::require_capabilities(self::CAP_EDIT, $o->context());
|
|
|
|
$config = [
|
|
'conditions' => $conditions,
|
|
];
|
|
if ($continuationid !== false) {
|
|
$config['continuation_id'] = $continuationid;
|
|
}
|
|
|
|
$o->edit($config);
|
|
return $o->editor_model();
|
|
|
|
}
|
|
|
|
/************************
|
|
* *
|
|
* reorder_studyitems *
|
|
* *
|
|
************************/
|
|
|
|
/**
|
|
* Parameter description for webservice function reorder_studyitems
|
|
* @return \external_function_parameters
|
|
*/
|
|
public static function reorder_studyitems_parameters() {
|
|
return new \external_function_parameters( [
|
|
"items" => new \external_multiple_structure(
|
|
new \external_single_structure([
|
|
"id" => new \external_value(PARAM_INT, 'id of studyitem '),
|
|
"line_id" => new \external_value(PARAM_INT, 'id of related study line'),
|
|
"slot" => new \external_value(PARAM_INT, 'slot in the study plan'),
|
|
"layer" => new \external_value(PARAM_INT, 'layer in the slot'),
|
|
]),
|
|
),
|
|
] );
|
|
}
|
|
|
|
/**
|
|
* Return value description for webservice function reorder_studyitems
|
|
* @return \external_description
|
|
*/
|
|
public static function reorder_studyitems_returns() {
|
|
return success::structure();
|
|
}
|
|
|
|
public static function reorder_studyitems($resequence) {
|
|
// Check for permissions to modify the studyplan.
|
|
foreach ($resequence as $sq) {
|
|
webservicehelper::require_capabilities(self::CAP_EDIT, studyitem::find_by_id(($sq['id']))->context());
|
|
}
|
|
|
|
return studyitem::reorder($resequence)->model();
|
|
}
|
|
|
|
/************************
|
|
* *
|
|
* delete_studyitem *
|
|
* *
|
|
************************/
|
|
|
|
/**
|
|
* Parameter description for webservice function delete_studyitem
|
|
* @return \external_function_parameters
|
|
*/
|
|
public static function delete_studyitem_parameters() {
|
|
return new \external_function_parameters( [
|
|
"id" => new \external_value(PARAM_INT, 'id of studyitem'),
|
|
] );
|
|
}
|
|
|
|
/**
|
|
* Return value description for webservice function delete_studyitem
|
|
* @return \external_description
|
|
*/
|
|
public static function delete_studyitem_returns() {
|
|
return success::structure();
|
|
}
|
|
|
|
public static function delete_studyitem($id) {
|
|
$o = studyitem::find_by_id($id);
|
|
webservicehelper::require_capabilities(self::CAP_EDIT, $o->context());
|
|
|
|
return $o->delete()->model();
|
|
}
|
|
|
|
/************************
|
|
* *
|
|
* connect_studyitems *
|
|
* *
|
|
************************/
|
|
|
|
/**
|
|
* Parameter description for webservice function connect_studyitems
|
|
* @return \external_function_parameters
|
|
*/
|
|
public static function connect_studyitems_parameters() {
|
|
return new \external_function_parameters( [
|
|
"from_id" => new \external_value(PARAM_INT, 'id of studyitem connect start '),
|
|
"to_id" => new \external_value(PARAM_INT, 'id ofstudyitem connect end'),
|
|
] );
|
|
}
|
|
|
|
/**
|
|
* Return value description for webservice function connect_studyitems
|
|
* @return \external_description
|
|
*/
|
|
public static function connect_studyitems_returns() {
|
|
return studyitemconnection::structure();
|
|
}
|
|
|
|
public static function connect_studyitems($fromid, $toid) {
|
|
// Validate permissions.
|
|
webservicehelper::require_capabilities(self::CAP_EDIT, studyitem::find_by_id($fromid)->context());
|
|
webservicehelper::require_capabilities(self::CAP_EDIT, studyitem::find_by_id($toid)->context());
|
|
|
|
$o = studyitemconnection::connect($fromid, $toid);
|
|
return $o->model();
|
|
}
|
|
|
|
/****************************
|
|
* *
|
|
* disconnect_studyitems *
|
|
* *
|
|
****************************/
|
|
|
|
/**
|
|
* Parameter description for webservice function disconnect_studyitems
|
|
* @return \external_function_parameters
|
|
*/
|
|
public static function disconnect_studyitems_parameters() {
|
|
return new \external_function_parameters( [
|
|
"from_id" => new \external_value(PARAM_INT, 'id of studyitem '),
|
|
"to_id" => new \external_value(PARAM_INT, 'id of related study line'),
|
|
] );
|
|
}
|
|
|
|
/**
|
|
* Return value description for webservice function disconnect_studyitems
|
|
* @return \external_description
|
|
*/
|
|
public static function disconnect_studyitems_returns() {
|
|
return success::structure();
|
|
}
|
|
|
|
public static function disconnect_studyitems($fromid, $toid) {
|
|
// Validate permissions.
|
|
webservicehelper::require_capabilities(self::CAP_EDIT, studyitem::find_by_id($fromid)->context());
|
|
webservicehelper::require_capabilities(self::CAP_EDIT, studyitem::find_by_id($toid)->context());
|
|
return studyitemconnection::disconnect($fromid, $toid)->model();
|
|
}
|
|
|
|
/****************************
|
|
* *
|
|
* list badges *
|
|
* *
|
|
****************************/
|
|
|
|
/**
|
|
* Parameter description for webservice function list_badges
|
|
* @return \external_function_parameters
|
|
*/
|
|
public static function list_badges_parameters() {
|
|
return new \external_function_parameters( [] );
|
|
}
|
|
|
|
/**
|
|
* Return value description for webservice function list_badges
|
|
* @return \external_description
|
|
*/
|
|
public static function list_badges_returns() {
|
|
return new \external_multiple_structure(badgeinfo::editor_structure());
|
|
}
|
|
|
|
public static function list_badges() {
|
|
$systemcontext = webservicehelper::system_context();
|
|
|
|
$result = [];
|
|
$badges = badges_get_badges(BADGE_TYPE_SITE, "timemodified");
|
|
foreach ($badges as $badge) {
|
|
// TODO: Add config option to list only active badges.
|
|
$result[] = (new badgeinfo($badge))->editor_model();
|
|
|
|
// TODO: Include course badges somehow... Just site badges is not enough.
|
|
|
|
}
|
|
return $result;
|
|
|
|
}
|
|
|
|
/****************************
|
|
* *
|
|
* include/remove grades *
|
|
* *
|
|
****************************/
|
|
|
|
/**
|
|
* Parameter description for webservice function include_grade
|
|
* @return \external_function_parameters
|
|
*/
|
|
public static function include_grade_parameters() {
|
|
return new \external_function_parameters( [
|
|
"grade_id" => new \external_value(PARAM_INT, 'id of gradeitem '),
|
|
"item_id" => new \external_value(PARAM_INT, 'id of studyitem '),
|
|
"include" => new \external_value(PARAM_BOOL, 'include or not '),
|
|
"required" => new \external_value(PARAM_BOOL, 'required grade or not', VALUE_DEFAULT),
|
|
] );
|
|
}
|
|
|
|
/**
|
|
* Return value description for webservice function include_grade
|
|
* @return \external_description
|
|
*/
|
|
public static function include_grade_returns() {
|
|
return success::structure();
|
|
}
|
|
|
|
public static function include_grade($gradeid, $itemid, $include, $required = false) {
|
|
global $USER;
|
|
|
|
// Find related course and course context.
|
|
$coursecontext = gradeinfo::get_coursecontext_by_id($gradeid);
|
|
// Do sanity checks.
|
|
\external_api::validate_context($coursecontext);
|
|
|
|
// Check correct capabilities.
|
|
if (has_capability('local/treestudyplan:editstudyplan', studyitem::find_by_id($itemid)->context()) ||
|
|
is_enrolled($coursecontext, $USER, 'local/treestudyplan:selectowngradables')) {
|
|
return gradeinfo::include_grade($gradeid, $itemid, $include, $required)->model();
|
|
} else {
|
|
return success::fail("Access denied")->model();
|
|
}
|
|
|
|
}
|
|
|
|
/****************************
|
|
* *
|
|
* list aggregators *
|
|
* *
|
|
****************************/
|
|
|
|
/**
|
|
* Parameter description for webservice function list_aggregators
|
|
* @return \external_function_parameters
|
|
*/
|
|
public static function list_aggregators_parameters() {
|
|
return new \external_function_parameters([]);
|
|
}
|
|
|
|
/**
|
|
* Return value description for webservice function list_aggregators
|
|
* @return \external_description
|
|
*/
|
|
public static function list_aggregators_returns() {
|
|
return aggregator::list_structure();
|
|
}
|
|
|
|
public static function list_aggregators() {
|
|
return aggregator::list_model();
|
|
|
|
}
|
|
|
|
/****************************
|
|
* *
|
|
* force_studyplan_scale *
|
|
* *
|
|
****************************/
|
|
|
|
/**
|
|
* Parameter description for webservice function force_studyplan_scale
|
|
* @return \external_function_parameters
|
|
*/
|
|
public static function force_studyplan_scale_parameters() {
|
|
return new \external_function_parameters( [
|
|
"studyplan_id" => new \external_value(PARAM_INT, 'id of studyplan'),
|
|
"scale_id" => new \external_value(PARAM_INT, 'scale_id to set'),
|
|
] );
|
|
}
|
|
|
|
/**
|
|
* Return value description for webservice function force_studyplan_scale
|
|
* @return \external_description
|
|
*/
|
|
public static function force_studyplan_scale_returns() {
|
|
return new \external_multiple_structure(new \external_single_structure([
|
|
"course" => courseinfo::simple_structure(),
|
|
"grades" => new \external_multiple_structure(new \external_single_structure([
|
|
"name" => new \external_value(PARAM_TEXT, 'grade name'),
|
|
"changed" => new \external_value(PARAM_TEXT, 'changed or not'),
|
|
"debug" => new \external_value(PARAM_TEXT, 'debug', VALUE_OPTIONAL),
|
|
])),
|
|
]));
|
|
}
|
|
|
|
public static function force_studyplan_scale($studyplanid, $scaleid) {
|
|
global $DB;
|
|
$dbman = $DB->get_manager();
|
|
|
|
// Validate permissions.
|
|
webservicehelper::require_capabilities(self::CAP_EDIT, studyplan::find_by_id($studyplanid)->context());
|
|
|
|
$list = [];
|
|
// Check if scaleid is valid.
|
|
$scale = \grade_scale::fetch(['id' => $scaleid]);
|
|
$scale->load_items();
|
|
|
|
if ($scale) {
|
|
$gradecfg = $DB->get_record("local_treestudyplan_gradecfg", ["scale_id" => $scale->id]);
|
|
$scalepass = ($gradecfg) ? $gradecfg->min_completed : 0;
|
|
$scalemax = count($scale->scale_items);
|
|
|
|
// Find studyline id's.
|
|
$studylineids = $DB->get_fieldset_select(studyline::TABLE, "id",
|
|
"studyplan_id = :plan_id", ['plan_id' => $studyplanid]);
|
|
foreach ($studylineids as $studylineid) {
|
|
// Find id's of studyitems of type course.
|
|
$records = $DB->get_records(studyitem::TABLE, ['line_id' => $studylineid]);
|
|
|
|
foreach ($records as $itemr) {
|
|
$studyitem = new studyitem($itemr->id);
|
|
if ($studyitem->valid() && $studyitem->type() == studyitem::COURSE) {
|
|
$courseinfo = $studyitem->getcourseinfo();
|
|
$gradables = gradeinfo::list_studyitem_gradables($studyitem);
|
|
|
|
$gradelist = [];
|
|
foreach ($gradables as $g) {
|
|
$gi = $g->get_gradeitem();
|
|
|
|
// Only change items that do not yet have grades.
|
|
// Otherwise we will need to implement grade recalculations and it is not worth the trouble. .
|
|
// If grades are given, you likely don't want to change it like this anyway.
|
|
|
|
if (!$gi->has_grades()) {
|
|
$gi->gradetype = GRADE_TYPE_SCALE;
|
|
$gi->scaleid = $scale->id;
|
|
$gi->grademin = 1;
|
|
$gi->grademax = $scalemax;
|
|
$gi->gradepass = $scalepass;
|
|
|
|
// Update, signalling with our signature and bulkupdate.
|
|
$result = $gi->update("local/treestudyplan");
|
|
|
|
$debug = "";
|
|
if ($result) {
|
|
$updated = "converted";
|
|
} else {
|
|
$updated = "error";
|
|
}
|
|
|
|
// Next update the activity's table if it has a grade field.
|
|
// Grade is generally set to the negative scale id if it is a scale.
|
|
$tablename = $gi->itemmodule;
|
|
$fieldname = "grade";
|
|
if ($result && $gi->itemtype == "mod" && $dbman->table_exists($tablename)) {
|
|
if ($dbman->field_exists($tablename, $fieldname)) {
|
|
$gradevalue = intval(0 - ($scale->id));
|
|
try {
|
|
$DB->set_field($tablename, $fieldname, $gradevalue, ["id" => $gi->iteminstance]);
|
|
} catch (\dml_exception $x) {
|
|
$updated = "fail";
|
|
$debug = strval($x);
|
|
}
|
|
}
|
|
}
|
|
|
|
} else {
|
|
$updated = "skipped";
|
|
}
|
|
|
|
$gradelist[] = [
|
|
'name' => $gi->itemname,
|
|
'changed' => $updated,
|
|
'debug' => $debug,
|
|
];
|
|
}
|
|
|
|
$list[] = [
|
|
'course' => $courseinfo->simple_model(),
|
|
'grades' => $gradelist,
|
|
];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
/****************************
|
|
* *
|
|
* list_scales *
|
|
* *
|
|
****************************/
|
|
|
|
/**
|
|
* Parameter description for webservice function list_scales
|
|
* @return \external_function_parameters
|
|
*/
|
|
public static function list_scales_parameters() {
|
|
return new \external_function_parameters( [] );
|
|
}
|
|
|
|
/**
|
|
* Return value description for webservice function list_scales
|
|
* @return \external_description
|
|
*/
|
|
public static function list_scales_returns() {
|
|
return new \external_multiple_structure(new \external_single_structure([
|
|
"id" => new \external_value(PARAM_INT, 'id of scale'),
|
|
"name" => new \external_value(PARAM_TEXT, 'scale name'),
|
|
]));
|
|
}
|
|
|
|
public static function list_scales() {
|
|
global $DB;
|
|
$list = [];
|
|
$scales = \grade_scale::fetch_all_global();
|
|
|
|
foreach ($scales as $scale) {
|
|
$list[] = [
|
|
"id" => $scale->id,
|
|
"name" => $scale->name,
|
|
];
|
|
}
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
/****************************
|
|
* *
|
|
* disable_autoenddate *
|
|
* *
|
|
****************************/
|
|
|
|
/**
|
|
* Parameter description for webservice function disable_autoenddate
|
|
* @return \external_function_parameters
|
|
*/
|
|
public static function disable_autoenddate_parameters() {
|
|
return new \external_function_parameters( [
|
|
"studyplan_id" => new \external_value(PARAM_INT, 'id of studyplan'),
|
|
] );
|
|
}
|
|
|
|
/**
|
|
* Return value description for webservice function disable_autoenddate
|
|
* @return \external_description
|
|
*/
|
|
public static function disable_autoenddate_returns() {
|
|
return success::structure();
|
|
}
|
|
|
|
public static function disable_autoenddate($studyplanid) {
|
|
global $DB;
|
|
|
|
// Validate permissions.
|
|
webservicehelper::require_capabilities(self::CAP_EDIT, studyplan::find_by_id($studyplanid)->context());
|
|
|
|
// Find studyline id's.
|
|
$studylineids = $DB->get_fieldset_select(studyline::TABLE, "id", "studyplan_id = :plan_id", ['plan_id' => $studyplanid]);
|
|
foreach ($studylineids as $studylineid) {
|
|
// Find id's of studyitems of type course.
|
|
$records = $DB->get_records(studyitem::TABLE, ['line_id' => $studylineid]);
|
|
|
|
foreach ($records as $itemr) {
|
|
$studyitem = new studyitem($itemr->id);
|
|
if ($studyitem->valid() && $studyitem->type() == studyitem::COURSE) {
|
|
$record = $DB->get_record("course_format_options",
|
|
["courseid" => $studyitem->courseid(), "name" => "automaticenddate"]);
|
|
if ($record && $record->value) {
|
|
$record->value = false;
|
|
$DB->update_record("course_format_options", $record);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return success::success()->model();
|
|
}
|
|
/****************************
|
|
* *
|
|
* duplicate studyplan *
|
|
* *
|
|
****************************/
|
|
|
|
/**
|
|
* Parameter description for webservice function duplicate_plan
|
|
* @return \external_function_parameters
|
|
*/
|
|
public static function duplicate_plan_parameters() {
|
|
return new \external_function_parameters( [
|
|
"plan_id" => new \external_value(PARAM_INT, 'id of plan to copy '),
|
|
"name" => new \external_value(PARAM_TEXT, 'name of copy '),
|
|
"shortname" => new \external_value(PARAM_TEXT, 'shortname of copy '),
|
|
] );
|
|
}
|
|
|
|
/**
|
|
* Return value description for webservice function duplicate_plan
|
|
* @return \external_description
|
|
*/
|
|
public static function duplicate_plan_returns() {
|
|
return studyplan::simple_structure();
|
|
}
|
|
|
|
public static function duplicate_plan($studyplanid, $name, $shortname) {
|
|
// Validate permissions.
|
|
webservicehelper::require_capabilities(self::CAP_EDIT, studyplan::find_by_id($studyplanid)->context());
|
|
|
|
return studyplan::duplicate_plan($studyplanid, $name, $shortname);
|
|
}
|
|
|
|
/****************************
|
|
* *
|
|
* export studyplan *
|
|
* *
|
|
****************************/
|
|
|
|
/**
|
|
* Parameter description for webservice function export_plan
|
|
* @return \external_function_parameters
|
|
*/
|
|
public static function export_plan_parameters() {
|
|
return new \external_function_parameters( [
|
|
"studyplan_id" => new \external_value(PARAM_INT, 'id of plan to export '),
|
|
"format" => new \external_value(PARAM_TEXT, 'export format', VALUE_OPTIONAL),
|
|
] );
|
|
}
|
|
|
|
/**
|
|
* Return value description for webservice function export_plan
|
|
* @return \external_description
|
|
*/
|
|
public static function export_plan_returns() {
|
|
return studyplan::export_structure();
|
|
}
|
|
|
|
public static function export_plan($studyplanid, $format = "json") {
|
|
try {
|
|
// Validate permissions.
|
|
webservicehelper::require_capabilities(self::CAP_EDIT, studyplan::find_by_id($studyplanid)->context());
|
|
|
|
$plan = studyplan::find_by_id($studyplanid);
|
|
if ($format == "csv") {
|
|
// FIXME: Make sure this webservice function gets called for the page instead of the studyplan.
|
|
return $plan->pages()[0]->export_page_csv();
|
|
} else {
|
|
return $plan->export_plan();
|
|
}
|
|
} catch (\webservice_access_exception $x) {
|
|
return [ "format" => "", "content" => ""];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Parameter description for webservice function export_studylines
|
|
* @return \external_function_parameters
|
|
*/
|
|
public static function export_studylines_parameters() {
|
|
return new \external_function_parameters( [
|
|
"studyplan_id" => new \external_value(PARAM_INT, 'id of plan to export '),
|
|
] );
|
|
}
|
|
|
|
/**
|
|
* Return value description for webservice function export_studylines
|
|
* @return \external_description
|
|
*/
|
|
public static function export_studylines_returns() {
|
|
return studyplan::export_structure();
|
|
}
|
|
|
|
public static function export_studylines($studyplanid) {
|
|
$systemcontext = webservicehelper::system_context();
|
|
|
|
try {
|
|
webservicehelper::require_capabilities(self::CAP_EDIT, studyplan::find_by_id($studyplanid)->context());
|
|
$plan = studyplan::find_by_id($studyplanid);
|
|
// FIXME: Make sure this gets called for the page instead of the studyplan.
|
|
return $plan->pages()[0]->export_studylines();
|
|
} catch (\webservice_access_exception $x) {
|
|
return [ "format" => "", "content" => ""];
|
|
}
|
|
}
|
|
/****************************
|
|
* *
|
|
* import studyplan *
|
|
* *
|
|
****************************/
|
|
|
|
/**
|
|
* Parameter description for webservice function import_plan
|
|
* @return \external_function_parameters
|
|
*/
|
|
public static function import_plan_parameters() {
|
|
return new \external_function_parameters( [
|
|
"content" => new \external_value(PARAM_TEXT, 'import file content'),
|
|
"format" => new \external_value(PARAM_TEXT, 'import format'),
|
|
"context_id" => new \external_value(PARAM_INT, 'context of the study plan', VALUE_DEFAULT),
|
|
] );
|
|
}
|
|
|
|
/**
|
|
* Return value description for webservice function import_plan
|
|
* @return \external_description
|
|
*/
|
|
public static function import_plan_returns() {
|
|
return success::structure();
|
|
}
|
|
|
|
public static function import_plan($content, $format = "application/json", $contextid = 1) {
|
|
|
|
try {
|
|
// Validate import context.
|
|
$context = webservicehelper::find_context($contextid);
|
|
webservicehelper::require_capabilities(self::CAP_EDIT, $context);
|
|
|
|
$result = studyplan::import_studyplan($content, $format, $contextid);
|
|
return (new success($result, "During study plan import"))->model();
|
|
} catch (\webservice_access_exception $x) {
|
|
return success::fail("Access denied")->model();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Parameter description for webservice function import_studylines
|
|
* @return \external_function_parameters
|
|
*/
|
|
public static function import_studylines_parameters() {
|
|
return new \external_function_parameters( [
|
|
"studyplan_id" => new \external_value(PARAM_INT, 'id of plan to export '),
|
|
"content" => new \external_value(PARAM_TEXT, 'import file content'),
|
|
"format" => new \external_value(PARAM_TEXT, 'import format'),
|
|
] );
|
|
}
|
|
|
|
/**
|
|
* Return value description for webservice function import_studylines
|
|
* @return \external_description
|
|
*/
|
|
public static function import_studylines_returns() {
|
|
return success::structure();
|
|
}
|
|
|
|
public static function import_studylines($studyplanid, $content, $format = "application/json") {
|
|
|
|
try {
|
|
$plan = studyplan::find_by_id($studyplanid);
|
|
// Validate import context.
|
|
webservicehelper::require_capabilities(self::CAP_EDIT, $plan->context());
|
|
|
|
// FIXME: Make sure this gets called for the page instead of the studyplan.
|
|
// Once proper page management is implemented.
|
|
$result = $plan->pages()[0]->import_studylines($content, $format);
|
|
return ($result ? success::success() : success::fail())->model();
|
|
} catch (\webservice_access_exception $x) {
|
|
return success::fail("Access denied")->model();
|
|
}
|
|
}
|
|
|
|
/********************************************************
|
|
* *
|
|
* Read and write course module title and desc *
|
|
* Used only in bistate aggregation method *
|
|
* *
|
|
********************************************************/
|
|
|
|
/**
|
|
* Parameter description for webservice function submit_cm_editform
|
|
* @return \external_function_parameters
|
|
*/
|
|
public static function submit_cm_editform_parameters() {
|
|
return new \external_function_parameters( [
|
|
"cmid" => new \external_value(PARAM_INT, 'id of course module'),
|
|
"formdata" => new \external_value(PARAM_RAW, 'url encoded form data'),
|
|
] );
|
|
}
|
|
|
|
/**
|
|
* Return value description for webservice function submit_cm_editform
|
|
* @return \external_description
|
|
*/
|
|
public static function submit_cm_editform_returns() {
|
|
return success::structure();
|
|
}
|
|
|
|
/** DEPRECATED, will remove hacked edit form in the future */
|
|
public static function submit_cm_editform($cmid, $formdata) {
|
|
global $CFG;
|
|
global $DB;
|
|
|
|
// Check the course module exists.
|
|
$cm = \get_coursemodule_from_id('', $cmid, 0, false, MUST_EXIST);
|
|
// Get some context ready.
|
|
$context = \context_course::instance($cm->course);
|
|
self::validate_context($context);
|
|
|
|
// Check the course exists.
|
|
$course = \get_course($cm->course);
|
|
// Require_login.
|
|
require_login($course, false, $cm); // Needed to setup proper $COURSE.
|
|
|
|
// Get needed info to create the correct form.
|
|
list($cm, $context, $module, $data, $cw) = \get_moduleinfo_data($cm, $course);
|
|
$modmoodleform = "$CFG->dirroot/mod/$module->name/mod_form.php";
|
|
if (file_exists($modmoodleform)) {
|
|
require_once($modmoodleform);
|
|
} else {
|
|
throw new \moodle_exception('noformdesc', 'error');
|
|
}
|
|
$mformclassname = 'mod_'.$module->name.'_mod_form';
|
|
|
|
// Now hack the received data into $_POST, so the mform thinks it has been submitted "normally".
|
|
foreach (explode("&", $formdata) as $pair) {
|
|
$p = explode("=", $pair, 2);
|
|
$k = urldecode($p[0]);
|
|
$v = (count($p) > 1) ? urldecode($p[1]) : "";
|
|
|
|
if (strpos($k, "[") > 0 && strpos($k, "]") == strlen($k) - 1) {
|
|
// Its a bracketet field, like filename[text] which should be separated and put into a named array.
|
|
list($k, $h) = explode("[", $k, 2);
|
|
if (strlen($k) > 0 && strlen($h) > 1) {
|
|
$h = rtrim($h, "]");
|
|
if (!array_key_exists($k, $_POST) || !is_array($_POST[$k])) {
|
|
$_POST[$k] = [];
|
|
}
|
|
$_POST[$k][$h] = $v;
|
|
}
|
|
} else {
|
|
$_POST[$k] = $v;
|
|
}
|
|
}
|
|
|
|
// Now create the mform and update the module...
|
|
// Update_moduleinfo() actually needs the mform somehow. Hence the ugly hacks.
|
|
$mform = new $mformclassname($data, $cw->section, $cm, $course);
|
|
$mform->set_data($data);
|
|
|
|
if ($fromform = $mform->get_data()) {
|
|
update_moduleinfo($cm, $fromform, $course, $mform);
|
|
return success::success()->model();
|
|
} else {
|
|
return success::fail("MForm did not recognize submission data")->model();
|
|
}
|
|
}
|
|
|
|
/************************
|
|
* *
|
|
* edit_period *
|
|
* *
|
|
************************/
|
|
|
|
/**
|
|
* Parameter description for webservice function edit_period
|
|
* @return \external_function_parameters
|
|
*/
|
|
public static function edit_period_parameters() {
|
|
return new \external_function_parameters( [
|
|
"id" => new \external_value(PARAM_INT, 'id of study item'),
|
|
"fullname" => new \external_value(PARAM_TEXT, 'Full name of period'),
|
|
"shortname" => new \external_value(PARAM_TEXT, 'Short name of period'),
|
|
"startdate" => new \external_value(PARAM_TEXT, 'start date of period'),
|
|
"enddate" => new \external_value(PARAM_TEXT, 'end date of period'),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Return value description for webservice function edit_period
|
|
* @return \external_description
|
|
*/
|
|
public static function edit_period_returns() {
|
|
return period::structure();
|
|
}
|
|
|
|
public static function edit_period($id, $fullname, $shortname, $startdate, $enddate) {
|
|
|
|
$p = period::find_by_id($id);
|
|
webservicehelper::require_capabilities(self::CAP_EDIT, $p->page()->studyplan()->context());
|
|
|
|
$p->edit([
|
|
'fullname' => $fullname,
|
|
'shortname' => $shortname,
|
|
'startdate' => $startdate,
|
|
'enddate' => $enddate,
|
|
]);
|
|
return $p->model();
|
|
|
|
}
|
|
|
|
/************************
|
|
* *
|
|
* Change course timing *
|
|
* *
|
|
************************/
|
|
|
|
/**
|
|
* Parameter description for webservice function course_period_timing
|
|
* @return \external_function_parameters
|
|
*/
|
|
public static function course_period_timing_parameters() {
|
|
return new \external_function_parameters( [
|
|
"period_id" => new \external_value(PARAM_INT, 'Period number within page'),
|
|
"course_id" => new \external_value(PARAM_INT, 'Id of course to adjust dates for'),
|
|
"span" => new \external_value(PARAM_INT, 'Period span (default 1)', VALUE_DEFAULT),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Return value description for webservice function course_period_timing
|
|
* @return \external_description
|
|
*/
|
|
public static function course_period_timing_returns() {
|
|
return courseinfo::editor_structure();
|
|
}
|
|
|
|
public static function course_period_timing($periodid, $courseid, $span = 1) {
|
|
global $DB;
|
|
$period = period::find_by_id($periodid);
|
|
$periodnr = $period->period();
|
|
$page = $period->page();
|
|
// Check for studyplan edit permissions.
|
|
webservicehelper::require_capabilities(self::CAP_EDIT, $page->studyplan()->context());
|
|
$course = \get_course($courseid);
|
|
$coursecontext = \context_course::instance($courseid);
|
|
|
|
if (webservicehelper::has_capabilities("moodle/course:update", $coursecontext)) {
|
|
|
|
// Get the proper list of all the periods for this page.
|
|
$periods = period::find_for_page($page);
|
|
|
|
$pstart = $periods[$periodnr];
|
|
|
|
// Determine end period number - Clip span between 1 and last period.
|
|
if ($span <= 1) {
|
|
$pend = $pstart;
|
|
} else if ($periodnr + ($span - 1) > $page->periods()) {
|
|
$pend = $periods[$page->periods()];
|
|
} else {
|
|
$pend = $periods[$periodnr + ($span - 1)];
|
|
}
|
|
|
|
// First disable the automatic end date option, since it messes with the timing.
|
|
// Unfortunately there is still no default option to turn this off .
|
|
$record = $DB->get_record("course_format_options", ["courseid" => $course->id, "name" => "automaticenddate"]);
|
|
if ($record && $record->value) {
|
|
$record->value = false;
|
|
$DB->update_record("course_format_options", $record);
|
|
}
|
|
|
|
// Actually perform the timing changes, while also updating the module times.
|
|
// Like what happens on a course "reset".
|
|
reset_course_userdata((object)[
|
|
'id' => $course->id,
|
|
'reset_start_date' => $pstart->startdate()->getTimestamp(),
|
|
'reset_end_date' => $pend->enddate()->getTimestamp(),
|
|
'reset_start_date_old' => $course->startdate,
|
|
'reset_end_date_old' => $course->enddate,
|
|
]);
|
|
// Purge course cache so the dates are properly reflected.
|
|
\course_modinfo::purge_course_cache($course->id);
|
|
|
|
return (new courseinfo($course->id))->editor_model();
|
|
} else {
|
|
// Probably should return a nice message.
|
|
throw new \webservice_access_exception("You do not have date change permissions on this course");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Parameter description for webservice function set_studyitem_span
|
|
* @return \external_function_parameters
|
|
*/
|
|
public static function set_studyitem_span_parameters() {
|
|
return new \external_function_parameters( [
|
|
"id" => new \external_value(PARAM_INT, 'id of study item'),
|
|
"span" => new \external_value(PARAM_INT, 'span of item'),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Return value description for webservice function set_studyitem_span
|
|
* @return \external_description
|
|
*/
|
|
public static function set_studyitem_span_returns() {
|
|
return studyitem::editor_structure();
|
|
}
|
|
|
|
public static function set_studyitem_span($id, $span = null) {
|
|
$o = studyitem::find_by_id($id);
|
|
webservicehelper::require_capabilities(self::CAP_EDIT, $o->context());
|
|
|
|
$config = [ 'span' => $span];
|
|
$o->edit($config);
|
|
return $o->editor_model();
|
|
|
|
}
|
|
|
|
}
|