diff --git a/competency/classes/related_competency.php b/competency/classes/related_competency.php index 677118235e5..58b548e230a 100644 --- a/competency/classes/related_competency.php +++ b/competency/classes/related_competency.php @@ -138,24 +138,25 @@ class related_competency extends persistent { public static function get_related_competencies($competencyid) { global $DB; - $sql = "(SELECT c.*, " . $DB->sql_concat('rc.relatedcompetencyid', "'_'", 'rc.competencyid') . " AS rid + $fields = competency::get_sql_fields('c', 'c_'); + $sql = "(SELECT $fields, " . $DB->sql_concat('rc.relatedcompetencyid', "'_'", 'rc.competencyid') . " AS rid FROM {" . self::TABLE . "} rc JOIN {" . competency::TABLE . "} c ON c.id = rc.relatedcompetencyid WHERE rc.competencyid = :cid) UNION ALL - (SELECT c.*, " . $DB->sql_concat('rc.competencyid', "'_'", 'rc.relatedcompetencyid') . " AS rid + (SELECT $fields, " . $DB->sql_concat('rc.competencyid', "'_'", 'rc.relatedcompetencyid') . " AS rid FROM {" . self::TABLE . "} rc JOIN {" . competency::TABLE . "} c ON c.id = rc.competencyid WHERE rc.relatedcompetencyid = :cid2) - ORDER BY path, sortorder ASC"; + ORDER BY c_path ASC, c_sortorder ASC"; $competencies = array(); $records = $DB->get_recordset_sql($sql, array('cid' => $competencyid, 'cid2' => $competencyid)); foreach ($records as $record) { unset($record->rid); - $competencies[$record->id] = new competency(null, $record); + $competencies[$record->c_id] = new competency(null, competency::extract_record($record, 'c_')); } $records->close(); diff --git a/competency/classes/template_competency.php b/competency/classes/template_competency.php index 8b24882008b..41019e28acb 100644 --- a/competency/classes/template_competency.php +++ b/competency/classes/template_competency.php @@ -107,6 +107,8 @@ class template_competency extends persistent { $params[] = 1; } + $sql .= ' ORDER BY tpl.id ASC'; + $results = $DB->get_records_sql($sql, $params); $instances = array(); @@ -199,11 +201,10 @@ class template_competency extends persistent { FROM {' . competency::TABLE . '} comp JOIN {' . self::TABLE . '} tplcomp ON tplcomp.competencyid = comp.id - WHERE tplcomp.templateid = ?'; + WHERE tplcomp.templateid = ? + ORDER BY tplcomp.sortorder ASC'; $params = array($templateid); - $sql .= 'ORDER BY tplcomp.sortorder ASC'; - $results = $DB->get_records_sql($sql, $params); $instances = array(); diff --git a/competency/classes/user_competency_course.php b/competency/classes/user_competency_course.php index 93e2c3c5f0e..e731ca6526f 100644 --- a/competency/classes/user_competency_course.php +++ b/competency/classes/user_competency_course.php @@ -263,24 +263,27 @@ class user_competency_course extends persistent { public static function get_least_proficient_competencies_for_course($courseid, $skip = 0, $limit = 0) { global $DB; - $fields = competency::get_sql_fields('c'); + $fields = competency::get_sql_fields('c', 'c_'); $params = array('courseid' => $courseid); - $sql = 'SELECT ' . $fields . ', SUM(COALESCE(ucc.proficiency, 0)) AS timesproficient ' . - ' FROM {' . competency::TABLE . '} c - JOIN {' . course_competency::TABLE . '} cc - ON c.id = cc.competencyid - LEFT JOIN {' . self::TABLE . '} ucc - ON ucc.competencyid = c.id AND ucc.courseid = cc.courseid - WHERE cc.courseid = :courseid - GROUP BY c.id - ORDER BY timesproficient ASC, c.id DESC'; + $sql = 'SELECT ' . $fields . ' + FROM (SELECT cc.competencyid, SUM(COALESCE(ucc.proficiency, 0)) AS timesproficient + FROM {' . course_competency::TABLE . '} cc + LEFT JOIN {' . self::TABLE . '} ucc + ON ucc.competencyid = cc.competencyid + AND ucc.courseid = cc.courseid + WHERE cc.courseid = :courseid + GROUP BY cc.competencyid + ) p + JOIN {' . competency::TABLE . '} c + ON c.id = p.competencyid + ORDER BY p.timesproficient ASC, c.id DESC'; $results = $DB->get_records_sql($sql, $params, $skip, $limit); $a = $DB->get_records_sql('SELECT * from {' . self::TABLE . '}'); $comps = array(); foreach ($results as $r) { - $c = competency::extract_record($r); + $c = competency::extract_record($r, 'c_'); $comps[] = new competency(0, $c); } return $comps; diff --git a/competency/classes/user_competency_plan.php b/competency/classes/user_competency_plan.php index 1bbe9371102..81b7791b819 100644 --- a/competency/classes/user_competency_plan.php +++ b/competency/classes/user_competency_plan.php @@ -340,24 +340,26 @@ class user_competency_plan extends persistent { public static function get_least_proficient_competencies_for_template($templateid, $skip = 0, $limit = 0) { global $DB; - $fields = competency::get_sql_fields('c'); + $fields = competency::get_sql_fields('c', 'c_'); $params = array('templateid' => $templateid, 'notproficient' => false); - $sql = 'SELECT ' . $fields . ', COUNT(c.id) AS timesnotproficient ' . - ' FROM {' . self::TABLE . '} ucp - JOIN {' . plan::TABLE . '} p - ON ucp.planid = p.id + $sql = 'SELECT ' . $fields . ' + FROM (SELECT ucp.competencyid, COUNT(ucp.competencyid) AS timesnotproficient + FROM {' . self::TABLE . '} ucp + JOIN {' . plan::TABLE . '} p + ON p.id = ucp.planid + WHERE p.templateid = :templateid + AND (ucp.proficiency = :notproficient OR ucp.proficiency IS NULL) + GROUP BY ucp.competencyid + ) p JOIN {' . competency::TABLE . '} c - ON ucp.competencyid = c.id - WHERE p.templateid = :templateid - AND (ucp.proficiency = :notproficient OR ucp.proficiency IS NULL) - GROUP BY c.id - ORDER BY timesnotproficient DESC'; + ON c.id = p.competencyid + ORDER BY p.timesnotproficient DESC, c.id ASC'; $results = $DB->get_records_sql($sql, $params, $skip, $limit); $comps = array(); foreach ($results as $r) { - $c = competency::extract_record($r); + $c = competency::extract_record($r, 'c_'); $comps[] = new competency(0, $c); } return $comps; diff --git a/competency/classes/user_evidence_competency.php b/competency/classes/user_evidence_competency.php index b424d06c135..0c3272bcf34 100644 --- a/competency/classes/user_evidence_competency.php +++ b/competency/classes/user_evidence_competency.php @@ -122,7 +122,8 @@ class user_evidence_competency extends persistent { JOIN {" . user_evidence::TABLE . "} ue ON uec.userevidenceid = ue.id AND uc.userid = ue.userid - AND ue.id = ?"; + AND ue.id = ? + ORDER BY uc.id ASC"; $usercompetencies = array(); $records = $DB->get_recordset_sql($sql, array($userevidenceid));