Extracting add/edit type functions for reuse

This commit is contained in:
Chris Scribner 2011-09-01 13:52:09 -04:00
parent 560ed50ca5
commit 879e97bd3b
2 changed files with 80 additions and 52 deletions

View file

@ -56,6 +56,7 @@ define('LTI_LAUNCH_CONTAINER_EMBED', 2);
define('LTI_LAUNCH_CONTAINER_EMBED_NO_BLOCKS', 3); define('LTI_LAUNCH_CONTAINER_EMBED_NO_BLOCKS', 3);
define('LTI_LAUNCH_CONTAINER_WINDOW', 4); define('LTI_LAUNCH_CONTAINER_WINDOW', 4);
define('LTI_TOOL_STATE_ANY', 0);
define('LTI_TOOL_STATE_CONFIGURED', 1); define('LTI_TOOL_STATE_CONFIGURED', 1);
define('LTI_TOOL_STATE_PENDING', 2); define('LTI_TOOL_STATE_PENDING', 2);
define('LTI_TOOL_STATE_REJECTED', 3); define('LTI_TOOL_STATE_REJECTED', 3);
@ -392,8 +393,8 @@ function lti_get_domain_from_url($url){
} }
} }
function lti_get_tool_by_url_match($url, $courseid = null){ function lti_get_tool_by_url_match($url, $courseid = null, $state = LTI_TOOL_STATE_CONFIGURED){
$possibletools = lti_get_tools_by_url($url, LTI_TOOL_STATE_CONFIGURED, $courseid); $possibletools = lti_get_tools_by_url($url, $state, $courseid);
return lti_get_best_tool_by_url($url, $possibletools); return lti_get_best_tool_by_url($url, $possibletools);
} }
@ -642,6 +643,79 @@ function lti_get_type_type_config($id) {
return $type; return $type;
} }
function lti_prepare_type_for_save($type, $config){
$type->baseurl = $config->lti_toolurl;
$type->tooldomain = lti_get_domain_from_url($config->lti_toolurl);
$type->name = $config->lti_typename;
$type->coursevisible = !empty($config->lti_coursevisible) ? $config->lti_coursevisible : 0;
$config->lti_coursevisible = $type->coursevisible;
$type->timemodified = time();
unset ($config->lti_typename);
unset ($config->lti_toolurl);
}
function lti_update_type($type, $config){
global $DB;
lti_prepare_type_for_save($type, $config);
if ($DB->update_record('lti_types', $type)) {
foreach ($config as $key => $value) {
if (substr($key, 0, 4)=='lti_' && !is_null($value)) {
$record = new StdClass();
$record->typeid = $type->id;
$record->name = substr($key, 4);
$record->value = $value;
lti_update_config($record);
}
}
}
}
function lti_add_type($type, $config){
global $USER, $SITE, $DB;
lti_prepare_type_for_save($type, $config);
if(!isset($type->state)){
$type->state = LTI_TOOL_STATE_PENDING;
}
if(!isset($type->timecreated)){
$type->timecreated = time();
}
if(!isset($type->createdby)){
$type->createdby = $USER->id;
}
if(!isset($type->course)){
$type->course = $SITE->id;
}
//Create a salt value to be used for signing passed data to extension services
$config->lti_servicesalt = uniqid('', true);
$id = $DB->insert_record('lti_types', $type);
if ($id) {
foreach ($config as $key => $value) {
if (substr($key, 0, 4)=='lti_' && !is_null($value)) {
$record = new StdClass();
$record->typeid = $id;
$record->name = substr($key, 4);
$record->value = $value;
lti_add_config($record);
}
}
}
}
/** /**
* Add a tool configuration in the database * Add a tool configuration in the database
* *

View file

@ -80,65 +80,19 @@ $data = data_submitted();
if (confirm_sesskey() && isset($data->submitbutton)) { if (confirm_sesskey() && isset($data->submitbutton)) {
$type = new StdClass(); $type = new StdClass();
$type->name = $data->lti_typename;
$type->baseurl = $data->lti_toolurl;
$type->tooldomain = lti_get_domain_from_url($data->lti_toolurl);
$type->course = $SITE->id;
$type->coursevisible = !empty($data->lti_coursevisible) ? $data->lti_coursevisible : 0;
$type->timemodified = time();
$data->lti_coursevisible = $type->coursevisible;//When not checked, it does not appear in data array. Set it manually.
if (isset($id)) { if (isset($id)) {
$type->id = $id; $type->id = $id;
if ($DB->update_record('lti_types', $type)) { lti_update_type($type, $data);
unset ($data->lti_typename);
foreach ($data as $key => $value) {
if (substr($key, 0, 4)=='lti_' && !is_null($value)) {
$record = new StdClass();
$record->typeid = $id;
$record->name = substr($key, 4);
$record->value = $value;
if (lti_update_config($record)) {
$statusmsg = get_string('changessaved');
} else {
$errormsg = get_string('errorwithsettings', 'admin');
}
}
}
}
redirect($redirect); redirect($redirect);
die; die;
} else { } else {
$type->createdby = $USER->id;
$type->timecreated = time();
$type->state = LTI_TOOL_STATE_CONFIGURED; $type->state = LTI_TOOL_STATE_CONFIGURED;
//Create a salt value to be used for signing passed data to extension services lti_add_type($type, $data);
$data->lti_servicesalt = uniqid('', true);
$id = $DB->insert_record('lti_types', $type);
if ($id) {
unset ($data->lti_typename);
foreach ($data as $key => $value) {
if (substr($key, 0, 4)=='lti_' && !is_null($value)) {
$record = new StdClass();
$record->typeid = $id;
$record->name = substr($key, 4);
$record->value = $value;
if (lti_add_config($record)) {
$statusmsg = get_string('changessaved');
} else {
$errormsg = get_string('errorwithsettings', 'admin');
}
}
}
} else {
$errormsg = get_string('errorwithsettings', 'admin');
}
redirect($redirect); redirect($redirect);
die; die;
} }