Skip to content

Commit

Permalink
MDL-31861 add web service function save_definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
pcharsle committed Jun 13, 2014
1 parent 337075d commit 0ff4ebb
Show file tree
Hide file tree
Showing 5 changed files with 505 additions and 21 deletions.
28 changes: 14 additions & 14 deletions grade/grading/form/guide/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public function update_or_check_guide(stdClass $newdefinition, $usermodified = n
} else {
$newcomment = $newdefinition->guide['comments']; // New ones to be saved.
}
$currentcomments = $currentdefinition->guide_comment;
$currentcomments = $currentdefinition->guide_comments;
$commentfields = array('sortorder', 'description');
foreach ($newcomment as $id => $comment) {
if (preg_match('/^NEWID\d+$/', $id)) {
Expand Down Expand Up @@ -309,7 +309,7 @@ protected function load_definition() {
$this->definition = $definition;
// Now get criteria.
$this->definition->guide_criteria = array();
$this->definition->guide_comment = array();
$this->definition->guide_comments = array();
$criteria = $DB->get_recordset('gradingform_guide_criteria', array('definitionid' => $this->definition->id), 'sortorder');
foreach ($criteria as $criterion) {
foreach (array('id', 'sortorder', 'description', 'descriptionformat',
Expand All @@ -327,7 +327,7 @@ protected function load_definition() {
$comments = $DB->get_recordset('gradingform_guide_comments', array('definitionid' => $this->definition->id), 'sortorder');
foreach ($comments as $comment) {
foreach (array('id', 'sortorder', 'description', 'descriptionformat') as $fieldname) {
$this->definition->guide_comment[$comment->id][$fieldname] = $comment->{$fieldname};
$this->definition->guide_comments[$comment->id][$fieldname] = $comment->{$fieldname};
}
}
$comments->close();
Expand Down Expand Up @@ -404,8 +404,8 @@ public function get_definition_for_editing($addemptycriterion = false) {
} else if (!$definition && $addemptycriterion) {
$properties->guide['criteria'] = array('addcriterion' => 1);
}
if (!empty($definition->guide_comment)) {
$properties->guide['comments'] = $definition->guide_comment;
if (!empty($definition->guide_comments)) {
$properties->guide['comments'] = $definition->guide_comments;
} else if (!$definition && $addemptycriterion) {
$properties->guide['comments'] = array('addcomment' => 1);
}
Expand Down Expand Up @@ -508,7 +508,7 @@ public function render_preview(moodle_page $page) {
}

$criteria = $this->definition->guide_criteria;
$comments = $this->definition->guide_comment;
$comments = $this->definition->guide_comments;
$output = $this->get_renderer($page);

$guide = '';
Expand Down Expand Up @@ -652,16 +652,16 @@ public function get_min_max_score() {

/**
* @return array An array containing 2 key/value pairs which hold the external_multiple_structure
* for the 'guide_criteria' and the 'guide_comment'.
* for the 'guide_criteria' and the 'guide_comments'.
* @see gradingform_controller::get_external_definition_details()
* @since Moodle 2.5
*/
public static function get_external_definition_details() {
$guide_criteria = new external_multiple_structure(
new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'criterion id'),
'sortorder' => new external_value(PARAM_INT, 'sortorder'),
'id' => new external_value(PARAM_INT, 'criterion id', VALUE_OPTIONAL),
'sortorder' => new external_value(PARAM_INT, 'sortorder', VALUE_OPTIONAL),
'description' => new external_value(PARAM_RAW, 'description', VALUE_OPTIONAL),
'descriptionformat' => new external_format_value('description', VALUE_OPTIONAL),
'shortname' => new external_value(PARAM_TEXT, 'description'),
Expand All @@ -671,17 +671,17 @@ public static function get_external_definition_details() {
)
)
);
$guide_comment = new external_multiple_structure(
$guide_comments = new external_multiple_structure(
new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'criterion id'),
'sortorder' => new external_value(PARAM_INT, 'sortorder'),
'id' => new external_value(PARAM_INT, 'criterion id', VALUE_OPTIONAL),
'sortorder' => new external_value(PARAM_INT, 'sortorder', VALUE_OPTIONAL),
'description' => new external_value(PARAM_RAW, 'description', VALUE_OPTIONAL),
'descriptionformat' => new external_format_value('description', VALUE_OPTIONAL)
)
), 'comments', VALUE_OPTIONAL
);
return array('guide_criteria' => $guide_criteria, 'guide_comment' => $guide_comment);
return array('guide_criteria' => $guide_criteria, 'guide_comments' => $guide_comments);
}

/**
Expand Down Expand Up @@ -895,7 +895,7 @@ public function render_grading_element($page, $gradingformelement) {
}
}
$criteria = $this->get_controller()->get_definition()->guide_criteria;
$comments = $this->get_controller()->get_definition()->guide_comment;
$comments = $this->get_controller()->get_definition()->guide_comments;
$options = $this->get_controller()->get_options();
$value = $gradingformelement->getValue();
$html = '';
Expand Down
8 changes: 4 additions & 4 deletions grade/grading/form/rubric/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -668,15 +668,15 @@ public static function get_external_definition_details() {
$rubric_criteria = new external_multiple_structure(
new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'criterion id'),
'sortorder' => new external_value(PARAM_INT, 'sortorder'),
'id' => new external_value(PARAM_INT, 'criterion id', VALUE_OPTIONAL),
'sortorder' => new external_value(PARAM_INT, 'sortorder', VALUE_OPTIONAL),
'description' => new external_value(PARAM_RAW, 'description', VALUE_OPTIONAL),
'descriptionformat' => new external_format_value('description', VALUE_OPTIONAL),
'levels' => new external_multiple_structure(
new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'level id'),
'score' => new external_value(PARAM_FLOAT, 'score'),
'id' => new external_value(PARAM_INT, 'level id', VALUE_OPTIONAL),
'score' => new external_value(PARAM_FLOAT, 'score', VALUE_OPTIONAL),
'definition' => new external_value(PARAM_RAW, 'definition', VALUE_OPTIONAL),
'definitionformat' => new external_format_value('definition', VALUE_OPTIONAL)
)
Expand Down
143 changes: 140 additions & 3 deletions lib/classes/grading_external.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ private static function grading_area() {
'cmid' => new external_value(PARAM_INT, 'course module id'),
'contextid' => new external_value(PARAM_INT, 'context id'),
'component' => new external_value(PARAM_TEXT, 'component name'),
'areaname' => new external_value(PARAM_TEXT, 'area name'),
'activemethod' => new external_value(PARAM_TEXT, 'active method', VALUE_OPTIONAL),
'definitions' => new external_multiple_structure(self::definition(), 'definitions')
)
Expand All @@ -240,11 +241,11 @@ private static function grading_area() {
private static function definition() {
global $CFG;
$definition = array();
$definition['id'] = new external_value(PARAM_INT, 'definition id');
$definition['id'] = new external_value(PARAM_INT, 'definition id', VALUE_OPTIONAL);
$definition['method'] = new external_value(PARAM_TEXT, 'method');
$definition['name'] = new external_value(PARAM_TEXT, 'name');
$definition['description'] = new external_value(PARAM_RAW, 'description');
$definition['descriptionformat'] = new external_format_value('description');
$definition['description'] = new external_value(PARAM_RAW, 'description', VALUE_OPTIONAL);
$definition['descriptionformat'] = new external_format_value('description', VALUE_OPTIONAL);
$definition['status'] = new external_value(PARAM_INT, 'status');
$definition['copiedfromid'] = new external_value(PARAM_INT, 'copied from id', VALUE_OPTIONAL);
$definition['timecreated'] = new external_value(PARAM_INT, 'creation time');
Expand Down Expand Up @@ -442,4 +443,140 @@ public static function get_gradingform_instances_returns() {
);
}

/**
* Describes the parameters for save_definitions
*
* @return external_function_parameters
* @since Moodle 2.8
*/
public static function save_definitions_parameters() {
return new external_function_parameters(
array(
'areas' => new external_multiple_structure(self::grading_area(), 'areas with definitions to save')
)
);
}

/**
* Saves the areas and definitions
* @param array $areas array of areas containing definitions to be saved
* @return null
* @throws invalid_parameter_exception
* @since Moodle 2.8
*/
public static function save_definitions($areas) {
$params = self::validate_parameters(self::save_definitions_parameters(),
array('areas' => $areas));

foreach ($params['areas'] as $area) {

$context = context::instance_by_id($area['contextid']);
require_capability('moodle/grade:managegradingforms', $context);
$gradingmanager = get_grading_manager($context, $area['component'], $area['areaname']);
$gradingmanager->set_active_method($area['activemethod']);
$availablemethods = $gradingmanager->get_available_methods();

foreach ($area['definitions'] as $definition) {
if (array_key_exists($definition['method'], $availablemethods)) {
$controller = $gradingmanager->get_controller($definition['method']);
$controller->update_definition(self::create_definition_object($definition));
} else {
throw new invalid_parameter_exception('Unknown Grading method: '. $definition['method']);
}
}
}
}

/**
* Describes the return value for save_definitions
*
* @return external_single_structure
* @since Moodle 2.8
*/
public static function save_definitions_returns() {
return null;
}

/**
* Creates a definition stdClass object using the values from the definition
* array that is passed in as a parameter
*
* @param array $definition
* @return stdClass definition object
* @since Moodle 2.8
*/
private static function create_definition_object($definition) {
global $CFG;

$method = $definition['method'];
$definitionobject = new stdClass();
foreach ($definition as $key => $value) {
if (!is_array($value)) {
$definitionobject->$key = $value;
}
}
$text = '';
$format = FORMAT_MOODLE;
if (isset($definition['description'])) {
$text = $definition['description'];
if (isset($definition['descriptionformat'])) {
$format = $definition['descriptionformat'];
}
}
$definitionobject->description_editor = array('text' => $text, 'format' => $format);

require_once("$CFG->libdir/filelib.php");
require_once($CFG->dirroot.'/grade/grading/form/'.$method.'/lib.php');
$details = call_user_func('gradingform_'.$method.'_controller::get_external_definition_details');
$methodarray = array();
foreach (array_keys($details) as $definitionkey) {
$items = array();
$idnumber = 1;
foreach ($definition[$method][$definitionkey] as $item) {
$processeditem = self::set_new_ids($item, $idnumber);
$items[$processeditem['id']] = $processeditem;
$idnumber++;
}
$definitionobjectkey = substr($definitionkey, strlen($method.'_'));
$methodarray[$definitionobjectkey] = $items;
$definitionobject->$method = $methodarray;
}

return $definitionobject;
}

/**
* Recursively iterates through arrays. Any array without an id key-value combination
* is assumed to be an array of values to be inserted and an id key-value is added with
* the value matching the regex '/^NEWID\d+$/' that is expected by each grading form implementation.
*
* @param array $arraytoset the array to be processed
* @param int $startnumber the starting number for the new id numbers
* @return array with missing id keys added for all arrays
* @since Moodle 2.8
*/
private static function set_new_ids($arraytoset, $startnumber) {
$result = array();
$foundid = false;
$number = $startnumber;
foreach ($arraytoset as $key1 => $value1) {
if (is_array($value1)) {
foreach ($value1 as $key2 => $value2) {
$processedvalue = self::set_new_ids($value2, $number);
$result[$key1][$processedvalue['id']] = $processedvalue;
$number++;
}
} else {
$result[$key1] = $value1;
}
if ($key1 === 'id') {
$foundid = true;
}
}
if (!$foundid) {
$result['id'] = 'NEWID'.$number;
}
return $result;
}

}
7 changes: 7 additions & 0 deletions lib/db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,13 @@
'type' => 'read'
),

'core_grading_save_definitions' => array(
'classname' => 'core_grading_external',
'methodname' => 'save_definitions',
'description' => 'Save grading definitions',
'type' => 'write'
),

'core_grading_get_gradingform_instances' => array(
'classname' => 'core_grading_external',
'methodname' => 'get_gradingform_instances',
Expand Down
Loading

0 comments on commit 0ff4ebb

Please sign in to comment.