Skip to content

Commit

Permalink
MDL-52743 tool_lp: Course competency statistics
Browse files Browse the repository at this point in the history
Note - the statistics you see depends on whether you can recieve competency
ratings in the course. So students see their own progress as a %. Graders
see the least proficient competencies for the course.
  • Loading branch information
Damyon Wiese authored and Frederic Massart committed Apr 18, 2016
1 parent 5548789 commit f1979b6
Show file tree
Hide file tree
Showing 11 changed files with 447 additions and 0 deletions.
48 changes: 48 additions & 0 deletions admin/tool/lp/classes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -1037,6 +1037,30 @@ public static function list_courses_using_competency($competencyid) {
return $result;
}

/**
* Count the proficient competencies in a course for one user.
*
* @param int $courseid The id of the course to check.
* @param int $userid The id of the user to check.
* @return int
*/
public static function count_proficient_competencies_in_course_for_user($courseid, $userid) {
static::require_enabled();
// Check the user have access to the course.
self::validate_course($courseid);

// First we do a permissions check.
$context = context_course::instance($courseid);

$capabilities = array('tool/lp:coursecompetencyview', 'tool/lp:coursecompetencymanage');
if (!has_any_capability($capabilities, $context)) {
throw new required_capability_exception($context, 'tool/lp:coursecompetencyview', 'nopermissions', '');
}

// OK - all set.
return user_competency_course::count_proficient_competencies($courseid, $userid);
}

/**
* Count all the competencies in a course.
*
Expand Down Expand Up @@ -4772,6 +4796,7 @@ public static function grade_competency_in_course($courseorid, $userid, $compete
* @return int
*/
public static function count_plans_for_template($templateorid, $status = 0) {
static::require_enabled();
$template = $templateorid;
if (!is_object($template)) {
$template = new template($template);
Expand All @@ -4795,6 +4820,7 @@ public static function count_plans_for_template($templateorid, $status = 0) {
* @return int
*/
public static function count_user_competency_plans_for_template($templateorid, $proficiency = null) {
static::require_enabled();
$template = $templateorid;
if (!is_object($template)) {
$template = new template($template);
Expand Down Expand Up @@ -4833,6 +4859,27 @@ public static function list_plans_for_template($templateorid, $status = 0, $skip
return plan::get_records_for_template($template->get_id(), $status, $skip, $limit);
}

/**
* Get the most often not completed competency for this course.
*
* Requires tool/lp:coursecompetencyview capability at the course context.
*
* @param int $courseid The course id
* @param int $skip The number of records to skip
* @param int $limit The max number of records to return
* @return competency[]
*/
public static function get_least_proficient_competencies_for_course($courseid, $skip = 0, $limit = 100) {
static::require_enabled();
$coursecontext = context_course::instance($courseid);

if (!has_any_capability(array('tool/lp:competencyview', 'tool/lp:competencymanage'), $coursecontext)) {
throw new required_capability_exception($coursecontext, 'tool/lp:competencyview', 'nopermissions', '');
}

return user_competency_course::get_least_proficient_competencies_for_course($courseid, $skip, $limit);
}

/**
* Get the most often not completed competency for this template.
*
Expand All @@ -4844,6 +4891,7 @@ public static function list_plans_for_template($templateorid, $status = 0, $skip
* @return competency[]
*/
public static function get_least_proficient_competencies_for_template($templateorid, $skip = 0, $limit = 100) {
static::require_enabled();
$template = $templateorid;
if (!is_object($template)) {
$template = new template($template);
Expand Down
58 changes: 58 additions & 0 deletions admin/tool/lp/classes/course_competency_statistics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Course competency statistics class
*
* @package tool_lp
* @copyright 2016 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace tool_lp;
defined('MOODLE_INTERNAL') || die();

/**
* Course competency statistics class.
*
* @package tool_lp
* @copyright 2016 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class course_competency_statistics {

/** @var $competencycount The number of competencies in the course */
public $competencycount = 0;

/** @var $proficientcompetencycount The number of proficient competencies for the current user */
public $proficientcompetencycount = 0;

/** @var $leastproficientcompetencies The competencies in this course that were proficient the least times */
public $leastproficientcompetencies = array();

/**
* Return the custom definition of the properties of this model.
*
* @param int $courseid The course we want to generate statistics for.
*/
public function __construct($courseid) {
global $USER;

$this->competencycount = api::count_competencies_in_course($courseid);
$this->proficientcompetencycount = api::count_proficient_competencies_in_course_for_user($courseid, $USER->id);
$this->leastproficientcompetencies = api::get_least_proficient_competencies_for_course($courseid, 0, 3);
}
}
2 changes: 2 additions & 0 deletions admin/tool/lp/classes/external.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
use tool_lp\external\user_evidence_competency_exporter;
use tool_lp\external\competency_exporter;
use tool_lp\external\course_competency_exporter;
use tool_lp\external\course_competency_statistics_exporter;
use tool_lp\external\course_summary_exporter;
use tool_lp\external\course_module_summary_exporter;
use tool_lp\external\plan_exporter;
Expand Down Expand Up @@ -1892,6 +1893,7 @@ public static function data_for_course_competencies_page_returns() {
'canmanagecoursecompetencies' => new external_value(PARAM_BOOL, 'User can manage linked course competencies'),
'canconfigurecoursecompetencies' => new external_value(PARAM_BOOL, 'User can configure course competency settings'),
'settings' => course_competency_settings_exporter::get_read_structure(),
'statistics' => course_competency_statistics_exporter::get_read_structure(),
'competencies' => new external_multiple_structure(new external_single_structure(array(
'competency' => competency_exporter::get_read_structure(),
'coursecompetency' => course_competency_exporter::get_read_structure(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Class for exporting a course competency statistics summary.
*
* @package tool_lp
* @copyright 2016 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_lp\external;
defined('MOODLE_INTERNAL') || die();

use renderer_base;
use moodle_url;

/**
* Class for exporting a course competency statistics summary.
*
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class course_competency_statistics_exporter extends exporter {

public static function define_properties() {
return array(
'competencycount' => array(
'type' => PARAM_INT,
),
'proficientcompetencycount' => array(
'type' => PARAM_INT,
),
);
}

public static function define_other_properties() {
return array(
'proficientcompetencypercentage' => array(
'type' => PARAM_FLOAT
),
'proficientcompetencypercentageformatted' => array(
'type' => PARAM_RAW
),
'leastproficient' => array(
'type' => competency_exporter::read_properties_definition(),
'multiple' => true
),
'leastproficientcount' => array(
'type' => PARAM_INT
),
'canbegradedincourse' => array(
'type' => PARAM_BOOL
),
'canmanagecoursecompetencies' => array(
'type' => PARAM_BOOL
),
);
}

protected static function define_related() {
return array('context' => 'context');
}

protected function get_other_values(renderer_base $output) {
$proficientcompetencypercentage = 0;
$proficientcompetencypercentageformatted = '';
if ($this->data->competencycount > 0) {
$proficientcompetencypercentage = ((float) $this->data->proficientcompetencycount / (float) $this->data->competencycount) * 100.0;
$proficientcompetencypercentageformatted = format_float($proficientcompetencypercentage);
}
$competencies = array();
$contextcache = array();
foreach ($this->data->leastproficientcompetencies as $competency) {
if (!isset($contextcache[$competency->get_competencyframeworkid()])) {
$contextcache[$competency->get_competencyframeworkid()] = $competency->get_context();
}
$context = $contextcache[$competency->get_competencyframeworkid()];
$exporter = new competency_exporter($competency, array('context' => $context));
$competencies[] = $exporter->export($output);
}
return array(
'proficientcompetencypercentage' => $proficientcompetencypercentage,
'proficientcompetencypercentageformatted' => $proficientcompetencypercentageformatted,
'leastproficient' => $competencies,
'leastproficientcount' => count($competencies),
'canbegradedincourse' => has_capability('tool/lp:coursecompetencygradable', $this->related['context']),
'canmanagecoursecompetencies' => has_capability('tool/lp:coursecompetencymanage', $this->related['context'])
);
}
}
6 changes: 6 additions & 0 deletions admin/tool/lp/classes/output/course_competencies_page.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@
use context_course;
use tool_lp\api;
use tool_lp\course_competency;
use tool_lp\course_competency_statistics;
use tool_lp\competency;
use tool_lp\external\competency_exporter;
use tool_lp\external\competency_path_exporter;
use tool_lp\external\course_competency_exporter;
use tool_lp\external\course_competency_statistics_exporter;
use tool_lp\external\course_competency_settings_exporter;
use tool_lp\external\course_module_summary_exporter;
use tool_lp\external\user_competency_exporter;
Expand Down Expand Up @@ -78,6 +80,7 @@ public function __construct($courseid) {
$this->canmanagecoursecompetencies = has_capability('tool/lp:coursecompetencymanage', $this->context);
$this->canconfigurecoursecompetencies = has_capability('tool/lp:coursecompetencyconfigure', $this->context);
$this->coursecompetencysettings = api::read_course_competency_settings($courseid);
$this->coursecompetencystatistics = new course_competency_statistics($courseid);

// Check the lowest level in which the user can manage the competencies.
$this->manageurl = null;
Expand Down Expand Up @@ -178,6 +181,9 @@ public function export_for_template(renderer_base $output) {
$data->canconfigurecoursecompetencies = $this->canconfigurecoursecompetencies;
$exporter = new course_competency_settings_exporter($this->coursecompetencysettings);
$data->settings = $exporter->export($output);
$related = array('context' => $this->context);
$exporter = new course_competency_statistics_exporter($this->coursecompetencystatistics, $related);
$data->statistics = $exporter->export($output);
$data->manageurl = null;
if ($this->canmanagecompetencyframeworks) {
$data->manageurl = $this->manageurl->out(true);
Expand Down
56 changes: 56 additions & 0 deletions admin/tool/lp/classes/user_competency_course.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,60 @@ public static function get_multiple($userid, $courseid, array $competenciesorids

return self::get_records_select("userid = :userid AND courseid = :courseid AND $sql", $params);
}

/**
* Count the proficient competencies in this course for one user.
*
* @param int $courseid The course id
* @param int $userid The user id
* @return int
*/
public static function count_proficient_competencies($courseid, $userid) {
global $DB;

$sql = 'SELECT COUNT(comp.id)
FROM {' . self::TABLE . '} usercoursecomp
JOIN {' . competency::TABLE . '} comp
ON usercoursecomp.competencyid = comp.id
WHERE usercoursecomp.courseid = ? AND usercoursecomp.userid = ? AND usercoursecomp.proficiency = ?';
$params = array($courseid, $userid, true);

$results = $DB->count_records_sql($sql, $params);

return $results;
}

/**
* Get the list of competencies that were completed the least times in a course.
*
* @param int $courseid
* @param int $skip The number of competencies to skip
* @param int $limit The max number of competencies to return
* @return competency[]
*/
public static function get_least_proficient_competencies_for_course($courseid, $skip = 0, $limit = 0) {
global $DB;

$fields = competency::get_sql_fields('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 {' . user_competency_course::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';

$results = $DB->get_records_sql($sql, $params, $skip, $limit);
$a = $DB->get_records_sql('SELECT * from {' . user_competency_course::TABLE . '}');

$comps = array();
foreach ($results as $r) {
$c = competency::extract_record($r);
$comps[] = new competency(0, $c);
}
return $comps;
}
}
2 changes: 2 additions & 0 deletions admin/tool/lp/lang/en/tool_lp.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
$string['competenciesarenotenabled'] = 'The competencies module is not enabled.';
$string['competenciesforframework'] = 'Competencies for {$a}';
$string['competenciesmostoftennotproficient'] = 'Competencies most often not proficient in completed plans';
$string['competenciesmostoftennotproficientincourse'] = 'Competencies most often not proficient in this course';
$string['competenciessettings'] = 'Competencies settings';
$string['competencycannotbedeleted'] = 'The competency \'{$a}\' can not be deleted';
$string['competencycreated'] = 'Competency created';
Expand Down Expand Up @@ -427,5 +428,6 @@
$string['visible'] = 'Visible';
$string['visible_help'] = 'A competency framework can be hidden from teachers. This could be useful if a framework is still in the process of being developed.';
$string['when'] = 'When';
$string['xcompetenciesproficientoutofyincourse'] = 'You are proficient in {$a->x} out of {$a->y} competencies in this course.';
$string['xcompetencieslinkedoutofy'] = '{$a->x} out of {$a->y} competencies linked to courses';
$string['xplanscompletedoutofy'] = '{$a->x} out of {$a->y} plans completed for this template';
2 changes: 2 additions & 0 deletions admin/tool/lp/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
.path-admin-tool-lp [data-region="competencylinktree"] ul li {
list-style-type: none;
}
.path-admin-tool-lp [data-region="coursecompetencystatistics"] .progresstext,
.path-admin-tool-lp [data-region="templatestatistics"] .progresstext {
display: inline-block;
height: 40px;
vertical-align: top;
}
.path-admin-tool-lp [data-region="coursecompetencystatistics"] .progress,
.path-admin-tool-lp [data-region="templatestatistics"] .progress {
width: 10em;
display: inline-block;
Expand Down
Loading

0 comments on commit f1979b6

Please sign in to comment.