Project: moodle
Issue MDL-52242: Competencies listed in a completed plan must never change
When we get the competencies of a _completed_ learning plan, we need to get the list of competencies from the archived we created when the plan was marked as complete. The reason for this is because while a plan cannot be changed once it's complete, a template could change and those changes would be reflected in the plan. Once a plan is complete it should be frozen. There are a few ways to fix this: # Unlink the plan from its template upon completion (not recommended, heavy, relationship lost) # Copy the template_competencies to plan_competencies (almost acts unlink, heavy, adds weird conditional getters in $plan->get_competencies() # Add an additional condition to $plan->get_competencies() to generate the list of competencies from the archived competencies (sortorder of competencies is hard to figure out) I have started a patch for this, but tests failed because it broke the archiving mechanism. {noformat} diff --git a/admin/tool/lp/classes/plan.php b/admin/tool/lp/classes/plan.php index f55d4a7..3d84889 100644 --- a/admin/tool/lp/classes/plan.php +++ b/admin/tool/lp/classes/plan.php @@ -121,13 +121,19 @@ class plan extends persistent { */ public function get_competencies() { $competencies = array(); - if ($this->get_templateid()) { + if ($this->get_status() == self::STATUS_COMPLETE) { + // Get the competencies from the archive of the plan. + $competencies = user_competency_plan::list_competencies($this->get_id(), $this->get_userid()); + + } else if ($this->is_based_on_template()) { // Get the competencies from the template. $competencies = template_competency::list_competencies($this->get_templateid(), true); + } else { // Get the competencies from the plan. $competencies = plan_competency::list_competencies($this->get_id()); } + return $competencies; } diff --git a/admin/tool/lp/classes/user_competency_plan.php b/admin/tool/lp/classes/user_competency_plan.php index f89ed1d..8315a69 100644 --- a/admin/tool/lp/classes/user_competency_plan.php +++ b/admin/tool/lp/classes/user_competency_plan.php @@ -143,6 +143,38 @@ class user_competency_plan extends persistent { } /** + * List the competencies in this plan. + * + * @param int $planid The plan ID + * @param int $userid The user ID + * @return competency[] + */ + public static function list_competencies($planid, $userid) { + global $DB; + + // TODO Fix ordering. The order set in template_competency, or plan_competency is not applied here. + // Perhaps we should have copied the sortorder here as well. + $sql = 'SELECT c.* + FROM {' . competency::TABLE . '} c + JOIN {' . self::TABLE . '} ucp + ON ucp.competencyid = c.id + AND ucp.userid = :userid + WHERE ucp.planid = :planid'; + $params = array('userid' => $userid, 'planid' => $planid); + + // TODO MDL-52229 Handle hidden competencies. + $results = $DB->get_recordset_sql($sql, $params); + + $instances = array(); + foreach ($results as $key => $result) { + $instances[$key] = new competency(0, $result); + } + $results->close(); + + return $instances; + } + + /** * Get multiple user_competency_plan for a user. * * @param int $userid The user ID. {noformat} Along with this, this issue should also ensure that a user can not: - Remove a competency from a completed plan - Add a new competency from a completed plan
Story points: 2