Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions db/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,101 @@

function xmldb_local_api_extend_install()
{
global $CFG, $DB;

/**** Create the required custom field for the user profile ****/

require_once($CFG->dirroot.'/user/profile/definelib.php');
require_once($CFG->dirroot.'/user/profile/field/text/define.class.php');
require_once($CFG->dirroot.'/local/api_extend/lib.php');

// Get all categories.
$categories = $DB->get_records('user_info_category', null, 'sortorder ASC');


// Check if field already exist
foreach ($categories as $category) {
if ($fields = $DB->get_records('user_info_field', array('categoryid' => $category->id), 'sortorder ASC')) {
foreach ($fields as $field) {
if ($field->shortname === 'unicaf_uuid') {
$field_exist = true;
break;
}
}
}
}

// create field only if it doesn't exist
if (empty($field_exist)){
$data = new stdClass();
$data->id = 0;
$data->action = 'editfield';
$data->datatype = 'text';
$data->shortname = 'unicaf_uuid';
$data->name = 'Unicaf UUID';
$data->description = '';
$data->required = '0';
$data->locked = '1';
$data->forceunique = '1';
$data->signup = '0';
$data->visible = '0';
$data->categoryid = '1';
$data->defaultdata = '';
$data->param1 = 30;
$data->param2 = 2048;
$data->param3 = '0';
$data->param4 = '';
$data->param5 = '';
$data->submitbutton = 'Save changes';
$data->descriptionformat = '1';
$formfield = new profile_define_text();
$formfield->define_save($data);
}

/**** End for the custom field creation ****/


/**** Create custom roles ****/

// Create the rest roles
$path = __DIR__.'/../roles/';
$dir = new DirectoryIterator($path);

foreach ($dir as $fileinfo) {
if (!$fileinfo->isDot()) {

$filename = $fileinfo->getFilename();

// separate webservice
if ($filename === 'webservice.xml') {
$filename_webservice = $filename;
} else {
$filename_array[] = $filename;
}
}
}

// proceed creating webservice role if we have the file.
if (!empty($filename_webservice) && !empty($filename_array)){
$filepath = $path . $filename_webservice;

if (file_exists($filepath)) {
$array = prep_role_array($filepath);
$webservice_role_id = createRoleWithPermissions($array);
}

if (!empty($webservice_role_id)) {
foreach ($filename_array as $filename) {
$filepath = $path . $filename;
if (file_exists($filepath)) {
$array = prep_role_array($filepath);
createRoleWithPermissions($array, $webservice_role_id);
}
}
}

}
/**** End of custom role creation ****/

return true;
}
Expand Down
15 changes: 15 additions & 0 deletions db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@
'description' => 'Get assign and quiz overrides',
'capabilities' => 'mod/assign:view',
'type' => 'read',
],
'unicaf_get_roles_by_shortname' => [
'classname' => 'unicaf_roles_by_shortname',
'methodname' => 'unicaf_get_roleid',
'classpath' => 'local/api_extend/externallib.php',
'description' => 'Return ROLE id , search by shortname',
'type' => 'read',
]
];

Expand All @@ -142,5 +149,13 @@
'restrictedusers' => 1,
'enabled' => 1,
'shortname' => 'apiextendcalls'
],
'UNICAF Webservice' => [
'functions' => [
'unicaf_get_roles_by_shortname'
],
'restrictedusers' => 1,
'enabled'=>1,
'shortname'=>'getroleid'
]
];
104 changes: 103 additions & 1 deletion db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,109 @@

function xmldb_local_api_extend_upgrade($oldversion) {

return true;
global $CFG, $DB;

if ($oldversion < 2023022404) {

/**** Create the required custom field for the user profile ****/

require_once($CFG->dirroot.'/user/profile/definelib.php');
require_once($CFG->dirroot.'/user/profile/field/text/define.class.php');

// Get all categories.
$categories = $DB->get_records('user_info_category', null, 'sortorder ASC');


// Check if field already exist
foreach ($categories as $category) {
if ($fields = $DB->get_records('user_info_field', array('categoryid' => $category->id), 'sortorder ASC')) {
foreach ($fields as $field) {
if ($field->shortname === 'unicaf_uuid') {
$field_exist = true;
break;
}
}
}
}

// create field only if it doesn't exist
if (empty($field_exist)){
$data = new stdClass();
$data->id = 0;
$data->action = 'editfield';
$data->datatype = 'text';
$data->shortname = 'unicaf_uuid';
$data->name = 'Unicaf UUID';
$data->description = '';
$data->required = '0';
$data->locked = '1';
$data->forceunique = '1';
$data->signup = '0';
$data->visible = '0';
$data->categoryid = '1';
$data->defaultdata = '';
$data->param1 = 30;
$data->param2 = 2048;
$data->param3 = '0';
$data->param4 = '';
$data->param5 = '';
$data->submitbutton = 'Save changes';
$data->descriptionformat = '1';
$formfield = new profile_define_text();
$formfield->define_save($data);
}

/**** End for the custom field creation ****/


/**** Create custom roles ****/

// Create the rest roles
$path = __DIR__.'/../roles/';
$dir = new DirectoryIterator($path);

foreach ($dir as $fileinfo) {
if (!$fileinfo->isDot()) {

$filename = $fileinfo->getFilename();

// separate webservice
if ($filename === 'webservice.xml') {
$filename_webservice = $filename;
} else {
$filename_array[] = $filename;
}
}
}

// proceed creating webservice role if we have the file.
if (!empty($filename_webservice) && !empty($filename_array)){
$filepath = $path . $filename_webservice;

if (file_exists($filepath)) {
$array = prep_role_array($filepath);
$webservice_role_id = createRoleWithPermissions($array);
}

if (!empty($webservice_role_id)) {
foreach ($filename_array as $filename) {
$filepath = $path . $filename;
if (file_exists($filepath)) {
$array = prep_role_array($filepath);
createRoleWithPermissions($array, $webservice_role_id);
}
}
}

}
/**** End of custom role creation ****/

return true;

} // End of version 20210423000.00


return true;
}


Expand Down
55 changes: 55 additions & 0 deletions externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -986,3 +986,58 @@ public static function update_quiz_activity_parameters(): external_function_para
]);
}
}

class unicaf_roles_by_shortname extends external_api {

/**
* Returns description of method parameters
* @return external_function_parameters
*/
public static function unicaf_get_roleid_parameters() {
return new external_function_parameters(
array('shortname' => new external_value(PARAM_TEXT, 'role shortname'))
);
}

/**
* Returns welcome message
* @return string welcome message
*/
public static function unicaf_get_roleid($shortname = 'teacher') {
global $CFG, $DB;

//Parameter validation
//REQUIRED
$params = self::validate_parameters(self::unicaf_get_roleid_parameters(),
array('shortname' => $shortname));

$context = context_system::instance();

require_capability('moodle/role:manage', $context);

$role = $DB->get_record('role', array('shortname'=>$shortname));
if ($role === false) {
throw new moodle_exception('notexist','Invalid shortname');
}

$resultrole = array('id' => $role->id, 'shortname' => $role->shortname);

return $resultrole;
}

/**
* Returns description of method result value
* @return external_description
*/
public static function unicaf_get_roleid_returns() {
return new external_single_structure(
array(
'id' => new external_value(PARAM_TEXT, 'role id'),
'shortname' => new external_value(PARAM_TEXT, 'short name'),
)
);
}



}
2 changes: 1 addition & 1 deletion lang/en/local_api_extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

$string['pluginname'] = 'API Extend Calls';
$string['pluginname'] = 'API Extend Calls & Unicaf Webservice';
Loading