Improving navigation in the skill wheel see #1791

skala
Julio Montoya 13 years ago
parent f16b8eab10
commit 9abd80377d
  1. 147
      main/inc/lib/skill.lib.php
  2. 94
      main/template/default/skill/skill_wheel.js.tpl
  3. 9
      main/template/default/skill/skill_wheel_student.tpl

@ -392,7 +392,8 @@ class Skill extends Model {
$result = Database::store_result($result,'ASSOC'); $result = Database::store_result($result,'ASSOC');
return $result; return $result;
} }
/* Get one level childrens */
function get_children($skill_id, $load_user_data = false) { function get_children($skill_id, $load_user_data = false) {
$skill_rel_skill = new SkillRelSkill(); $skill_rel_skill = new SkillRelSkill();
if ($load_user_data) { if ($load_user_data) {
@ -404,11 +405,10 @@ class Skill extends Model {
return $skills; return $skills;
} }
/* Get all children of the current node (recursive)*/
function get_all_children($skill_id) { function get_all_children($skill_id) {
$skill_rel_skill = new SkillRelSkill(); $skill_rel_skill = new SkillRelSkill();
$children = $skill_rel_skill->get_children($skill_id);
$children = $skill_rel_skill->get_children($skill_id);
foreach ($children as $child) { foreach ($children as $child) {
$sub_children = $this->get_all_children($child['skill_id']); $sub_children = $this->get_all_children($child['skill_id']);
} }
@ -420,7 +420,7 @@ class Skill extends Model {
/** /**
* All parents from root to n * Gets all parents from from the wanted skill
*/ */
function get_parents($skill_id) { function get_parents($skill_id) {
$skill_rel_skill = new SkillRelSkill(); $skill_rel_skill = new SkillRelSkill();
@ -444,7 +444,10 @@ class Skill extends Model {
} }
return $skills; return $skills;
} }
/*
* Adds a new skill
*/
public function add($params) { public function add($params) {
if (!isset($params['parent_id'])) { if (!isset($params['parent_id'])) {
$params['parent_id'] = 1; $params['parent_id'] = 1;
@ -489,6 +492,26 @@ class Skill extends Model {
return null; return null;
} }
public function add_skill_to_user($user_id, $gradebook_id) {
$skill_gradebook = new SkillRelGradebook();
$skill_rel_user = new SkillRelUser();
$skill_gradebooks = $skill_gradebook->get_all(array('where'=>array('gradebook_id = ?' =>$gradebook_id)));
if (!empty($skill_gradebooks)) {
foreach ($skill_gradebooks as $skill_gradebook) {
$user_has_skill = $this->user_has_skill($user_id, $skill_gradebook['skill_id']);
if (!$user_has_skill) {
$params = array( 'user_id' => $user_id,
'skill_id' => $skill_gradebook['skill_id'],
'acquired_skill_at' => api_get_utc_datetime(),
);
$skill_rel_user->save($params);
}
}
}
}
/* Deletes a skill */
public function delete($skill_id) { public function delete($skill_id) {
/*$params = array('skill_id' => $skill_id); /*$params = array('skill_id' => $skill_id);
@ -544,43 +567,6 @@ class Skill extends Model {
return null; return null;
} }
/**
* Return true if the user has the skill
*
* @param int $userId User's id
* @param int $skillId Skill's id
* @param int $checkInParents if true, function will search also in parents of the given skill id
*
* @return bool
*/
public function user_has_skill($user_id, $skill_id) {
$skills = $this->get_user_skills($user_id);
foreach($skills as $my_skill_id) {
if ($my_skill_id == $skill_id) {
return true;
}
}
return false;
}
public function add_skill_to_user($user_id, $gradebook_id) {
$skill_gradebook = new SkillRelGradebook();
$skill_rel_user = new SkillRelUser();
$skill_gradebooks = $skill_gradebook->get_all(array('where'=>array('gradebook_id = ?' =>$gradebook_id)));
if (!empty($skill_gradebooks)) {
foreach ($skill_gradebooks as $skill_gradebook) {
$user_has_skill = $this->user_has_skill($user_id, $skill_gradebook['skill_id']);
if (!$user_has_skill) {
$params = array( 'user_id' => $user_id,
'skill_id' => $skill_gradebook['skill_id'],
'acquired_skill_at' => api_get_utc_datetime(),
);
$skill_rel_user->save($params);
}
}
}
}
/** /**
* Get user's skills * Get user's skills
@ -610,6 +596,9 @@ class Skill extends Model {
} }
public function get_skills_tree($user_id = null, $skill_id = null, $return_flat_array = false, $add_root = false) { public function get_skills_tree($user_id = null, $skill_id = null, $return_flat_array = false, $add_root = false) {
if ($skill_id == 1 ) {
$skill_id = 0;
}
if (isset($user_id) && !empty($user_id)) { if (isset($user_id) && !empty($user_id)) {
$skills = $this->get_all(true, $user_id, null, $skill_id); $skills = $this->get_all(true, $user_id, null, $skill_id);
} else { } else {
@ -624,18 +613,20 @@ class Skill extends Model {
if (!empty($skill_id)) { if (!empty($skill_id)) {
if ($add_root) { if ($add_root) {
if (!empty($skill_id)) { if (!empty($skill_id)) {
//Default root node
$skills[1] = array('id' => '1', 'name' => get_lang('Root'), 'parent_id' => '0'); $skills[1] = array('id' => '1', 'name' => get_lang('Root'), 'parent_id' => '0');
$skill_info = $this->get_skill_info($skill_id); $skill_info = $this->get_skill_info($skill_id);
//2nd node
$skills[$skill_id] = $skill_info; $skills[$skill_id] = $skill_info;
//Uncomment code below to hide the searched skill //Uncomment code below to hide the searched skill
//$skills[$skill_id]['parent_id'] = $skill_info['extra']['parent_id']; $skills[$skill_id]['data']['parent_id'] = $skill_info['parent_id'];
$skills[$skill_id]['parent_id'] = 1; $skills[$skill_id]['parent_id'] = 1;
} }
} }
} }
//var_dump($skills);
$refs = array(); $refs = array();
$skills_tree = null; $skills_tree = null;
@ -644,37 +635,36 @@ class Skill extends Model {
$family = array(); $family = array();
if (!empty($skills)) { if (!empty($skills)) {
foreach ($skills as &$skill) { foreach ($skills as &$skill) {
if ($skill['parent_id'] == 0) { if ($skill['parent_id'] == 0) {
$skill['parent_id'] = 'root'; $skill['parent_id'] = 'root';
} }
/*
if (empty($skill_id)) { // because except main keys (id, name, children) others keys are not saved while in the space tree
if ($skill['parent_id'] == 0) { $skill['data'] = array('parent_id' => $skill['parent_id']);
$skill['parent_id'] = 'root';
}
} else {
if ($skill['id'] == $skill_id) {
$skill['parent_id'] = 'root';
}
}
*/
//In order to paint all members of a family with the same color //In order to paint all members of a family with the same color
if (empty($skill_id)) { if (empty($skill_id)) {
if ($skill['parent_id'] == 1) { if ($skill['parent_id'] == 1) {
$family[$skill['id']] = $this->get_all_children($skill['id']); $family[$skill['id']] = $this->get_all_children($skill['id']);
} }
} else { } else {
if ($skill['parent_id'] == $skill_id) { if ($skill['parent_id'] == $skill_id) {
$family[$skill['id']] = $this->get_all_children($skill['id']); $family[$skill['id']] = $this->get_all_children($skill['id']);
} }
/*if ($skill_id == $skill['id']) {
$skill['parent_id'] = 1;
}*/
} }
// because except main keys (id, name, children) others keys are not saved while in the space tree
$skill['data'] = array('parent_id' => $skill['parent_id']); if (!isset($skill['data']['real_parent_id'])) {
$skill['data']['real_parent_id'] = $skill['parent_id'];
}
//User achieved the skill (depends in the gradebook with certification) //User achieved the skill (depends in the gradebook with certification)
$skill['data']['achieved'] = false; $skill['data']['achieved'] = false;
if ($user_id) { if ($user_id) {
$skill['data']['achieved'] = $this->user_has_skill($user_id, $skill['id']); $skill['data']['achieved'] = $this->user_has_skill($user_id, $skill['id']);
} }
@ -724,8 +714,7 @@ class Skill extends Model {
'children' => $refs['root']['children'], 'children' => $refs['root']['children'],
'data' => array() 'data' => array()
); );
} }
if ($return_flat_array) { if ($return_flat_array) {
return $flat_array; return $flat_array;
} }
@ -739,7 +728,6 @@ class Skill extends Model {
*/ */
public function get_skills_tree_json($user_id = null, $skill_id = null, $return_flat_array = false, $main_depth = 2) { public function get_skills_tree_json($user_id = null, $skill_id = null, $return_flat_array = false, $main_depth = 2) {
$tree = $this->get_skills_tree($user_id, $skill_id, $return_flat_array, true); $tree = $this->get_skills_tree($user_id, $skill_id, $return_flat_array, true);
$simple_tree = array(); $simple_tree = array();
if (!empty($tree['children'])) { if (!empty($tree['children'])) {
foreach ($tree['children'] as $element) { foreach ($tree['children'] as $element) {
@ -747,7 +735,8 @@ class Skill extends Model {
'children' => $this->get_skill_json($element['children'], 1, $main_depth), 'children' => $this->get_skill_json($element['children'], 1, $main_depth),
); );
} }
} }
//var_dump($simple_tree[0]['children']);
return json_encode($simple_tree[0]['children']); return json_encode($simple_tree[0]['children']);
} }
@ -855,4 +844,24 @@ class Skill extends Model {
$result = Database::query($sql); $result = Database::query($sql);
return Database::store_result($result, 'ASSOC'); return Database::store_result($result, 'ASSOC');
} }
/**
* Return true if the user has the skill
*
* @param int $userId User's id
* @param int $skillId Skill's id
* @param int $checkInParents if true, function will search also in parents of the given skill id
*
* @return bool
*/
public function user_has_skill($user_id, $skill_id) {
$skills = $this->get_user_skills($user_id);
foreach($skills as $my_skill_id) {
if ($my_skill_id == $skill_id) {
return true;
}
}
return false;
}
} }

@ -1,13 +1,13 @@
<script> <script>
/* Skill wheel settings */ /* Skill wheel settings */
var debug = true;
var url = '{{ url }}'; var url = '{{ url }}';
var skill_to_load_from_get = '{{ skill_id_to_load }}'; var skill_to_load_from_get = '{{ skill_id_to_load }}';
//Just in case we want to use it //Just in case we want to use it
var main_depth = 4; var main_depth = 4;
var main_parent_id = false; var main_parent_id = 0;
// Used to split in two word or not // Used to split in two word or not
var max_size_text_length = 14; var max_size_text_length = 14;
@ -190,28 +190,48 @@ for (i= 0; i < color_loops; i++) {
} }
/* When you click a skill partition */ /* When you click a skill partition */
function click_partition(d, path, text, icon, arc, x, y, r, p) { function click_partition(d, path, text, icon, arc, x, y, r, p, vis) {
//console.log(d.depth); //console.log(d.depth);
if (debug) {
console.log(d); console.log('Clicking a partition skill id: '+d.id);
if (d.id) { console.log(d);
console.log(d.real_parent_id + ' ' +d.parent_id); console.log('real parent_id: '+d.real_parent_id + ' parent_id: ' +d.parent_id);
main_parent_id = d.real_parent_id; console.log('depth ' + d.depth);
console.log('main_depth ' + main_depth);
} }
console.log('main_parent_id: ' + main_parent_id);
if (d.depth >= main_depth) { if (d.depth >= main_depth) {
//main_depth += main_depth; //main_depth += main_depth;
load_nodes(d.id, main_depth); if (main_parent_id) {
load_nodes(main_parent_id, main_depth);
} else {
load_nodes(d.id, main_depth);
}
}
if (d.id) {
console.log('Getting skill info');
skill_info = get_skill_info(d.parent_id);
console.log(skill_info);
main_parent_id = skill_info.extra.parent_id;
main_parent_id = d.parent_id;
console.log('Setting main_parent_id: ' + main_parent_id);
} }
//console.log(main_parent_id); //console.log(main_parent_id);
/* "No id" means that we reach the center of the wheel go to the root*/ /* "No id" means that we reach the center of the wheel go to the root*/
if (!d.id) { if (!d.id) {
load_nodes(main_parent_id, main_depth); load_nodes(main_parent_id, main_depth);
} }
console.log('Continue to click_partition');
//console.log(main_parent_id); //console.log(main_parent_id);
@ -376,11 +396,11 @@ for (i= 0; i < color_loops; i++) {
} }
/* Handles mouse clicks */ /* Handles mouse clicks */
function handle_mousedown_event(d, path, text, icon, arc, x, y, r, padding) { function handle_mousedown_event(d, path, text, icon, arc, x, y, r, padding, vis) {
switch (d3.event.which) { switch (d3.event.which) {
case 1: case 1:
//alert('Left mouse button pressed'); //alert('Left mouse button pressed');
click_partition(d, path, text, icon, arc, x, y, r, padding); click_partition(d, path, text, icon, arc, x, y, r, padding, vis);
break; break;
case 2: case 2:
//alert('Middle mouse button pressed'); //alert('Middle mouse button pressed');
@ -397,7 +417,24 @@ for (i= 0; i < color_loops; i++) {
/* /*
Loads the skills partitions thanks to a json call Loads the skills partitions thanks to a json call
*/ */
function load_nodes(load_skill_id, main_depth) { function load_nodes(load_skill_id, main_depth, extra_parent_id) {
if (debug) {
console.log('Load nodes');
console.log('Loading skill id: '+load_skill_id+' with depth ' + main_depth);
console.log('main_parent_id ' + main_parent_id);
}
if (main_parent_id && load_skill_id) {
skill_info = get_skill_info(load_skill_id);
if (skill_info && skill_info.extra) {
main_parent_id = skill_info.extra.parent_id;
} else {
main_parent_id = 0;
}
console.log('main_parent_id 2' + main_parent_id);
}
/** Define constants and size of the wheel */ /** Define constants and size of the wheel */
/** Total width of the wheel (also counts for the height) */ /** Total width of the wheel (also counts for the height) */
@ -492,6 +529,7 @@ function load_nodes(load_skill_id, main_depth) {
/* Setting icons */ /* Setting icons */
var icon = vis.selectAll("icon").data(nodes); var icon = vis.selectAll("icon").data(nodes);
/* Path settings */ /* Path settings */
path.enter().append("path") path.enter().append("path")
@ -516,11 +554,19 @@ function load_nodes(load_skill_id, main_depth) {
}) })
.on("mousedown", function(d, i) { .on("mousedown", function(d, i) {
//Handles mouse clicks //Handles mouse clicks
handle_mousedown_event(d, path, text, icon, arc, x, y, r, padding); handle_mousedown_event(d, path, text, icon, arc, x, y, r, padding, vis);
}) })
.on("click", function(d){ .on("click", function(d){
//click_partition(d, path, text, icon, arc, x, y, r, padding);
}); });
/*//Redefine the root
path_zero = vis.selectAll("#path-0").on("mousedown", function(d){
d = get_skill_info(extra_parent_id);
d.parent_id = d.extra.parent_id;
click_partition(d, path, text, icon, arc, x, y, r, padding, vis);
});*/
/* End setting skills */ /* End setting skills */
@ -549,10 +595,10 @@ function load_nodes(load_skill_id, main_depth) {
}) })
.on("mousedown", function(d, i) { .on("mousedown", function(d, i) {
//Handles 2 mouse clicks //Handles 2 mouse clicks
handle_mousedown_event(d, path, text, icon, arc, x, y, r, padding); handle_mousedown_event(d, path, text, icon, arc, x, y, r, padding, vis);
}) })
.on("click", function(d){ .on("click", function(d){
//click_partition(d, path, text, icon, arc, x, y, r, padding);
}); });
/** Managing text - maximum two words */ /** Managing text - maximum two words */
@ -567,8 +613,7 @@ function load_nodes(load_skill_id, main_depth) {
} else { } else {
insert_two_words = false; insert_two_words = false;
return d.depth ? d.name : ""; return d.depth ? d.name : "";
} }
}); });
if (insert_two_words) { if (insert_two_words) {
@ -576,7 +621,7 @@ function load_nodes(load_skill_id, main_depth) {
.attr("x", 0) .attr("x", 0)
.attr("dy", "1em") .attr("dy", "1em")
.text(function(d) { .text(function(d) {
return d.depth ? d.name.split(" ")[1] || "" : ""; return d.depth && d.name.length > max_size_text_length ? d.name.split(" ")[1] || "" : "";
}); });
} }
@ -645,7 +690,6 @@ function get_gradebook_info(id) {
return item; return item;
} }
$(document).ready(function() { $(document).ready(function() {
}); });

@ -75,8 +75,7 @@ $(document).ready(function() {
skill_id = $(this).attr('rel'); skill_id = $(this).attr('rel');
skill_name = $(this).attr('name'); skill_name = $(this).attr('name');
add_skill_in_profile_list(skill_id, skill_name); add_skill_in_profile_list(skill_id, skill_name);
}); });
/* URL link when searching skills */ /* URL link when searching skills */
$("#skill_holder").on("click", "a.load_wheel", function() { $("#skill_holder").on("click", "a.load_wheel", function() {
@ -246,7 +245,6 @@ $(document).ready(function() {
}); });
</script> </script>
<div class="container-fluid"> <div class="container-fluid">
<div class="row-fluid"> <div class="row-fluid">
@ -266,8 +264,7 @@ $(document).ready(function() {
</select> </select>
<br /><br /> <br /><br />
<div class="btn-group"> <div class="btn-group">
<a class="btn load_root" rel="0" href="#">{{ "SkillRoot"|get_lang }}</a> <a class="btn load_root" rel="0" href="#">{{ "SkillRoot"|get_lang }}</a>
<!-- <a id="clear_selection" class="btn">{{ "Clear"|get_lang }}</a> -->
</div> </div>
<ul id="skill_holder" class="holder holder_simple"> <ul id="skill_holder" class="holder holder_simple">
</ul> </ul>
@ -292,7 +289,7 @@ $(document).ready(function() {
<img src=""> <img src="">
</div> </div>
</div> </div>
</div> </div>
<div id="dialog-course-info" style="display:none;"> <div id="dialog-course-info" style="display:none;">
<div id="course_info"> <div id="course_info">
</div> </div>

Loading…
Cancel
Save