MDL-51646 tool_lp: Implement plan status
This commit is contained in:
committed by
Frederic Massart
parent
8a9030c18e
commit
58405003f8
@@ -1210,12 +1210,12 @@ class api {
|
||||
|
||||
// The user cannot view the drafts.
|
||||
if (!plan::can_read_user_draft($userid)) {
|
||||
$select = ' AND status != :statusdraft';
|
||||
$select .= ' AND status != :statusdraft';
|
||||
$params['statusdraft'] = plan::STATUS_DRAFT;
|
||||
}
|
||||
// The user cannot view the non-drafts.
|
||||
if (!plan::can_read_user($userid)) {
|
||||
$select = ' AND status = :statusdraft';
|
||||
$select .= ' AND status = :statusdraft';
|
||||
$params['statusdraft'] = plan::STATUS_DRAFT;
|
||||
}
|
||||
|
||||
@@ -1248,7 +1248,6 @@ class api {
|
||||
* @return \tool_lp\plan
|
||||
*/
|
||||
public static function update_plan(stdClass $record) {
|
||||
global $USER;
|
||||
$plan = new plan($record->id);
|
||||
|
||||
// Validate that the plan as it is can be managed.
|
||||
@@ -1280,7 +1279,6 @@ class api {
|
||||
* @return \tool_lp\plan
|
||||
*/
|
||||
public static function read_plan($id) {
|
||||
global $USER;
|
||||
$plan = new plan($id);
|
||||
|
||||
if (!$plan->can_read()) {
|
||||
@@ -1298,7 +1296,6 @@ class api {
|
||||
* @return bool Success?
|
||||
*/
|
||||
public static function delete_plan($id) {
|
||||
global $USER;
|
||||
$plan = new plan($id);
|
||||
|
||||
if (!$plan->can_manage()) {
|
||||
|
||||
@@ -28,7 +28,7 @@ defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
|
||||
|
||||
use moodleform;
|
||||
use tool_lp\api;
|
||||
|
||||
use tool_lp\plan as planpersistent;
|
||||
require_once($CFG->libdir.'/formslib.php');
|
||||
|
||||
/**
|
||||
@@ -45,6 +45,7 @@ class plan extends moodleform {
|
||||
*/
|
||||
public function definition() {
|
||||
$mform = $this->_form;
|
||||
$context = $this->_customdata['context'];
|
||||
|
||||
$mform->addElement('hidden', 'id');
|
||||
$mform->setType('id', PARAM_INT);
|
||||
@@ -62,11 +63,21 @@ class plan extends moodleform {
|
||||
$mform->addElement('date_selector', 'duedate', get_string('duedate', 'tool_lp'));
|
||||
$mform->addHelpButton('duedate', 'duedate', 'tool_lp');
|
||||
|
||||
// Display status selector in form.
|
||||
$status = planpersistent::get_status_list($this->_customdata['userid']);
|
||||
if (!empty($status) && count($status) > 1) {
|
||||
$mform->addElement('select', 'status', get_string('status', 'tool_lp'), $status);
|
||||
} else if (count($status) === 1) {
|
||||
$mform->addElement('static', 'staticstatus', get_string('status', 'tool_lp'), current($status));
|
||||
} else {
|
||||
throw new required_capability_exception($context, 'tool/lp:planmanage', 'nopermissions', '');
|
||||
}
|
||||
|
||||
$this->add_action_buttons(true, get_string('savechanges', 'tool_lp'));
|
||||
|
||||
if (!empty($this->_customdata['id'])) {
|
||||
if (isset($this->_customdata['plan'])) {
|
||||
if (!$this->is_submitted()) {
|
||||
$plan = api::read_plan($this->_customdata['id']);
|
||||
$plan = $this->_customdata['plan'];
|
||||
$record = $plan->to_record();
|
||||
$record->description = array('text' => $record->description, 'format' => $record->descriptionformat);
|
||||
$this->set_data($record);
|
||||
|
||||
@@ -226,4 +226,22 @@ class plan extends persistent {
|
||||
|| self::can_manage_user_draft($planuserid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of status depending on capabilities.
|
||||
*
|
||||
* @param int $userid The user to whom the plan would belong.
|
||||
* @return array
|
||||
*/
|
||||
public static function get_status_list($userid) {
|
||||
$status = array();
|
||||
if (self::can_manage_user_draft($userid)) {
|
||||
$status[self::STATUS_DRAFT] = get_string('planstatusdraft', 'tool_lp');
|
||||
}
|
||||
if (self::can_manage_user($userid)) {
|
||||
$status[self::STATUS_ACTIVE] = get_string('planstatusactive', 'tool_lp');
|
||||
$status[self::STATUS_COMPLETE] = get_string('planstatuscomplete', 'tool_lp');
|
||||
}
|
||||
return $status;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -55,12 +55,25 @@ $PAGE->set_pagelayout('admin');
|
||||
$PAGE->set_heading($pagetitle);
|
||||
$output = $PAGE->get_renderer('tool_lp');
|
||||
|
||||
// TODO MDL-51646 Handle user creating plan, editing plan, drafts, etc...
|
||||
if (!\tool_lp\plan::can_manage_user_draft($userid)) {
|
||||
// Custom data to pass to the form.
|
||||
$customdata = array('userid' => $userid, 'context' => $context);
|
||||
|
||||
// User can create plan if he can_manage_user with active/complete status
|
||||
// or if he can_manage_user_draft with draft status.
|
||||
$cancreate = \tool_lp\plan::can_manage_user_draft($userid) || \tool_lp\plan::can_manage_user($userid);
|
||||
|
||||
// If editing plan get the plan and check if user has permissions to edit it.
|
||||
if ($id) {
|
||||
$plan = \tool_lp\api::read_plan($id);
|
||||
if (!$plan->can_manage()) {
|
||||
throw new required_capability_exception($context, 'tool/lp:planmanage', 'nopermissions', '');
|
||||
}
|
||||
$customdata['plan'] = $plan;
|
||||
} else if (!$cancreate) {
|
||||
throw new required_capability_exception($context, 'tool/lp:planmanage', 'nopermissions', '');
|
||||
}
|
||||
|
||||
$customdata = array('id' => $id, 'userid' => $userid);
|
||||
|
||||
$form = new \tool_lp\form\plan(null, $customdata);
|
||||
if ($form->is_cancelled()) {
|
||||
redirect(new moodle_url('/admin/tool/lp/plans.php?userid=' . $userid));
|
||||
|
||||
@@ -304,4 +304,121 @@ class tool_lp_api_testcase extends advanced_testcase {
|
||||
$this->assertEquals($comprelated->get_idnumber(), $competency2->get_idnumber());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test update plan.
|
||||
*/
|
||||
public function test_update_plan() {
|
||||
$this->resetAfterTest(true);
|
||||
$dg = $this->getDataGenerator();
|
||||
$usermanageowndraft = $dg->create_user();
|
||||
$usermanageown = $dg->create_user();
|
||||
$usermanagedraft = $dg->create_user();
|
||||
$usermanage = $dg->create_user();
|
||||
|
||||
$syscontext = context_system::instance();
|
||||
|
||||
// Creating specific roles.
|
||||
$manageowndraftrole = $dg->create_role(array(
|
||||
'name' => 'User manage own draft',
|
||||
'shortname' => 'manage-own-draft'
|
||||
));
|
||||
$manageownrole = $dg->create_role(array(
|
||||
'name' => 'User manage own',
|
||||
'shortname' => 'manage-own'
|
||||
));
|
||||
$managedraftrole = $dg->create_role(array(
|
||||
'name' => 'User manage draft',
|
||||
'shortname' => 'manage-draft'
|
||||
));
|
||||
$managerole = $dg->create_role(array(
|
||||
'name' => 'User manage',
|
||||
'shortname' => 'manage'
|
||||
));
|
||||
|
||||
assign_capability('tool/lp:planmanageowndraft', CAP_ALLOW, $manageowndraftrole, $syscontext->id);
|
||||
assign_capability('tool/lp:planviewowndraft', CAP_ALLOW, $manageowndraftrole, $syscontext->id);
|
||||
|
||||
assign_capability('tool/lp:planmanageown', CAP_ALLOW, $manageownrole, $syscontext->id);
|
||||
assign_capability('tool/lp:planviewown', CAP_ALLOW, $manageownrole, $syscontext->id);
|
||||
|
||||
assign_capability('tool/lp:planmanagedraft', CAP_ALLOW, $managedraftrole, $syscontext->id);
|
||||
assign_capability('tool/lp:planviewdraft', CAP_ALLOW, $managedraftrole, $syscontext->id);
|
||||
|
||||
assign_capability('tool/lp:planmanage', CAP_ALLOW, $managerole, $syscontext->id);
|
||||
assign_capability('tool/lp:planview', CAP_ALLOW, $managerole, $syscontext->id);
|
||||
|
||||
$dg->role_assign($manageowndraftrole, $usermanageowndraft->id, $syscontext->id);
|
||||
$dg->role_assign($manageownrole, $usermanageown->id, $syscontext->id);
|
||||
$dg->role_assign($managedraftrole, $usermanagedraft->id, $syscontext->id);
|
||||
$dg->role_assign($managerole, $usermanage->id, $syscontext->id);
|
||||
|
||||
// Create first learning plan with user create draft.
|
||||
$this->setUser($usermanageowndraft);
|
||||
$plan = array (
|
||||
'name' => 'plan own draft',
|
||||
'description' => 'plan own draft',
|
||||
'userid' => $usermanageowndraft->id
|
||||
);
|
||||
$plan = api::create_plan((object)$plan);
|
||||
$record = $plan->to_record();
|
||||
$record->name = 'plan own draft modified';
|
||||
|
||||
// Check if user create draft can edit the plan name.
|
||||
$plan = api::update_plan($record);
|
||||
$this->assertInstanceOf('\tool_lp\plan', $plan);
|
||||
|
||||
// Thrown exception when manageowndraft user try to change the status.
|
||||
$record->status = \tool_lp\plan::STATUS_ACTIVE;
|
||||
try {
|
||||
$plan = api::update_plan($record);
|
||||
$this->fail('User with manage own draft capability cannot edit the plan status.');
|
||||
} catch (required_capability_exception $e) {
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
|
||||
// Test when user with manage own plan capability try to edit other user plan.
|
||||
$record->status = \tool_lp\plan::STATUS_DRAFT;
|
||||
$record->name = 'plan create draft modified 2';
|
||||
$this->setUser($usermanageown);
|
||||
try {
|
||||
$plan = api::update_plan($record);
|
||||
$this->fail('User with manage own plan capability can only edit his own plan.');
|
||||
} catch (required_capability_exception $e) {
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
|
||||
// User with manage plan capability cannot edit the other user plans with status draft.
|
||||
$this->setUser($usermanage);
|
||||
$record->status = \tool_lp\plan::STATUS_COMPLETE;
|
||||
try {
|
||||
$plan = api::update_plan($record);
|
||||
$this->fail('User with manage plan capability cannot edit the other user plans with status draft');
|
||||
} catch (required_capability_exception $e) {
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
|
||||
// User with manage draft capability can edit other user's learning plan if the status is draft.
|
||||
$this->setUser($usermanagedraft);
|
||||
$record->status = \tool_lp\plan::STATUS_DRAFT;
|
||||
$record->name = 'plan manage draft modified 3';
|
||||
$plan = api::update_plan($record);
|
||||
$this->assertInstanceOf('\tool_lp\plan', $plan);
|
||||
|
||||
// User with manage plan capability can create/edit learning plan if status is active/complete.
|
||||
$this->setUser($usermanage);
|
||||
$plan = array (
|
||||
'name' => 'plan create',
|
||||
'description' => 'plan create',
|
||||
'userid' => $usermanage->id,
|
||||
'status' => \tool_lp\plan::STATUS_ACTIVE
|
||||
);
|
||||
$plan = api::create_plan((object)$plan);
|
||||
$record = $plan->to_record();
|
||||
$record->name = 'plan create own modified';
|
||||
$record->status = \tool_lp\plan::STATUS_COMPLETE;
|
||||
$plan = api::update_plan($record);
|
||||
$this->assertInstanceOf('\tool_lp\plan', $plan);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user