/** * Construct this renderable. * * @param int $userid */ public function __construct($userid) { $this->userid = $userid; $this->plans = api::list_user_plans($userid); $this->context = context_user::instance($userid); if (plan::can_manage_user($userid) || plan::can_manage_user_draft($userid)) { $addplan = new single_button(new moodle_url('/admin/tool/lp/editplan.php', array('userid' => $userid)), get_string('addnewplan', 'tool_lp'), 'get'); $this->navigation[] = $addplan; } }
list($title, $subtitle, $returnurl) = \tool_lp\page_helper::setup_for_plan($userid, $url, null, $pagetitle, $returntype); } else { $plan = \core_competency\api::read_plan($id); // The userid parameter must be the same as the owner of the plan. if ($userid != $plan->get_userid()) { throw new coding_exception('Inconsistency between the userid parameter and the userid of the plan'); } $pagetitle = get_string('editplan', 'tool_lp'); list($title, $subtitle, $returnurl) = \tool_lp\page_helper::setup_for_plan($userid, $url, $plan, $pagetitle, $returntype); } $output = $PAGE->get_renderer('tool_lp'); // Custom data to pass to the form. $customdata = array('userid' => $userid, 'context' => $PAGE->context, 'persistent' => $plan); // User can create plan if he can_manage_user with active/complete status // or if he can_manage_user_draft with draft status. $cancreate = \core_competency\plan::can_manage_user_draft($userid) || \core_competency\plan::can_manage_user($userid); // If editing plan, check if user has permissions to edit it. if ($plan != null) { if (!$plan->can_manage()) { throw new required_capability_exception($PAGE->context, 'moodle/competency:planmanage', 'nopermissions', ''); } if (!$plan->can_be_edited()) { throw new coding_exception('Completed plan can not be edited'); } } else { if (!$cancreate) { throw new required_capability_exception($PAGE->context, 'moodle/competency:planmanage', 'nopermissions', ''); } } $form = new \tool_lp\form\plan($url->out(false), $customdata); if ($form->is_cancelled()) {
public function test_can_manage_user_draft() { $this->resetAfterTest(true); $manage = create_role('Manage', 'manage', 'Plan manager'); $manageown = create_role('Manageown', 'manageown', 'Own plan manager'); $managedraft = create_role('Managedraft', 'managedraft', 'Draft plan manager'); $manageowndraft = create_role('Manageowndraft', 'manageowndraft', 'Own draft plan manager'); $u1 = $this->getDataGenerator()->create_user(); $u2 = $this->getDataGenerator()->create_user(); $u3 = $this->getDataGenerator()->create_user(); $u4 = $this->getDataGenerator()->create_user(); $u5 = $this->getDataGenerator()->create_user(); $syscontext = context_system::instance(); $u1context = context_user::instance($u1->id); $u2context = context_user::instance($u2->id); $u3context = context_user::instance($u3->id); $u4context = context_user::instance($u4->id); $u5context = context_user::instance($u5->id); assign_capability('moodle/competency:planmanage', CAP_ALLOW, $manage, $syscontext->id); assign_capability('moodle/competency:planmanageown', CAP_ALLOW, $manageown, $syscontext->id); assign_capability('moodle/competency:planmanagedraft', CAP_ALLOW, $managedraft, $syscontext->id); assign_capability('moodle/competency:planmanageowndraft', CAP_ALLOW, $manageowndraft, $syscontext->id); role_assign($manage, $u1->id, $syscontext->id); role_assign($manageown, $u2->id, $syscontext->id); role_assign($managedraft, $u3->id, $syscontext->id); role_assign($managedraft, $u4->id, $u2context->id); role_assign($manageowndraft, $u5->id, $syscontext->id); accesslib_clear_all_caches_for_unit_testing(); $this->setUser($u1); $this->assertFalse(plan::can_manage_user_draft($u1->id)); $this->assertFalse(plan::can_manage_user_draft($u2->id)); $this->assertFalse(plan::can_manage_user_draft($u3->id)); $this->assertFalse(plan::can_manage_user_draft($u4->id)); $this->assertFalse(plan::can_manage_user_draft($u5->id)); $this->setUser($u2); $this->assertFalse(plan::can_manage_user_draft($u1->id)); $this->assertFalse(plan::can_manage_user_draft($u2->id)); $this->assertFalse(plan::can_manage_user_draft($u3->id)); $this->assertFalse(plan::can_manage_user_draft($u4->id)); $this->assertFalse(plan::can_manage_user_draft($u5->id)); $this->setUser($u3); $this->assertTrue(plan::can_manage_user_draft($u1->id)); $this->assertTrue(plan::can_manage_user_draft($u2->id)); $this->assertTrue(plan::can_manage_user_draft($u3->id)); $this->assertTrue(plan::can_manage_user_draft($u4->id)); $this->assertTrue(plan::can_manage_user_draft($u5->id)); $this->setUser($u4); $this->assertFalse(plan::can_manage_user_draft($u1->id)); $this->assertTrue(plan::can_manage_user_draft($u2->id)); $this->assertFalse(plan::can_manage_user_draft($u3->id)); $this->assertFalse(plan::can_manage_user_draft($u4->id)); $this->assertFalse(plan::can_manage_user_draft($u5->id)); $this->setUser($u5); $this->assertFalse(plan::can_manage_user_draft($u1->id)); $this->assertFalse(plan::can_manage_user_draft($u2->id)); $this->assertFalse(plan::can_manage_user_draft($u3->id)); $this->assertFalse(plan::can_manage_user_draft($u4->id)); $this->assertTrue(plan::can_manage_user_draft($u5->id)); }