Skip to content

Commit

Permalink
Add sub categories skills to a user see BT#15624
Browse files Browse the repository at this point in the history
  • Loading branch information
jmontoyaa committed May 20, 2019
1 parent af2a1ec commit 9976ca6
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 97 deletions.
80 changes: 39 additions & 41 deletions main/gradebook/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,6 @@ function confirmation() {
}

$viewTitle = '';

// DISPLAY HEADERS AND MESSAGES
if (!isset($_GET['exportpdf'])) {
if (isset($_GET['studentoverview'])) {
Expand Down Expand Up @@ -673,7 +672,7 @@ function confirmation() {
if (isset($_GET['studentoverview'])) {
//@todo this code also seems to be deprecated ...
$cats = Category::load($selectCat);
$stud_id = (api_is_allowed_to_edit() ? null : $stud_id);
$stud_id = api_is_allowed_to_edit() ? null : $stud_id;
$allcat = $cats[0]->get_subcategories($stud_id, $course_code, $session_id);
$alleval = $cats[0]->get_evaluations($stud_id, true);
$alllink = $cats[0]->get_links($stud_id, true);
Expand Down Expand Up @@ -729,57 +728,56 @@ function confirmation() {
} else {
// Student view

//in any other case (no search, no pdf), print the available gradebooks
// In any other case (no search, no pdf), print the available gradebooks
// Important note: loading a category will actually load the *contents* of
// this category. This means that, to show the categories of a course,
// we have to show the root category and show its subcategories that
// are inside this course. This is done at the time of calling
// $cats[0]->get_subcategories(), not at the time of doing Category::load()
// $category comes from GET['selectcat']

//if $category = 0 (which happens when GET['selectcat'] is undefined)
// if $category = 0 (which happens when GET['selectcat'] is undefined)
// then Category::load() will create a new 'root' category with empty
// course and session fields in memory (Category::create_root_category())
if ($_in_course === true) {
$cats = Category:: load(
null,
null,
$course_code,
null,
null,
$session_id,
false
);
if (empty($cats)) {
// There is no category for this course+session, so create one
$cat = new Category();
if (!empty($session_id)) {
$sessionName = api_get_session_name($session_id);
$cat->set_name($course_code.' - '.get_lang('Session').' '.$sessionName);
$cat->set_session_id($session_id);
} else {
$cat->set_name($course_code);
}
$cat->set_course_code($course_code);
$cat->set_description(null);
$cat->set_user_id($stud_id);
$cat->set_parent_id(0);
$cat->set_weight(100);
$cat->set_visible(0);
$cat->set_certificate_min_score(75);
$can_edit = api_is_allowed_to_edit(true, true);
if ($can_edit) {
$cat->add();
}
unset($cat);

$cats = Category:: load(
null,
null,
$course_code,
null,
null,
$session_id,
false
);

if (empty($cats)) {
// There is no category for this course+session, so create one
$cat = new Category();
if (!empty($session_id)) {
$sessionName = api_get_session_name($session_id);
$cat->set_name($course_code.' - '.get_lang('Session').' '.$sessionName);
$cat->set_session_id($session_id);
} else {
$cat->set_name($course_code);
}
$cat->set_course_code($course_code);
$cat->set_description(null);
$cat->set_user_id($stud_id);
$cat->set_parent_id(0);
$cat->set_weight(100);
$cat->set_visible(0);
$cat->set_certificate_min_score(75);
$can_edit = api_is_allowed_to_edit(true, true);
if ($can_edit) {
$cat->add();
}
unset($cats);
unset($cat);
}
$cats = Category::load($selectCat, null, null, null, null, null, false);

$cats = Category::load($selectCat, null, null, null, null, null, false);
// With this fix the teacher only can view 1 gradebook
if (api_is_platform_admin()) {
$stud_id = (api_is_allowed_to_edit() ? null : api_get_user_id());
$stud_id = api_is_allowed_to_edit() ? null : api_get_user_id();
}

$allcat = $cats[0]->get_subcategories($stud_id, $course_code, $session_id);
Expand All @@ -789,10 +787,10 @@ function confirmation() {

// add params to the future links (in the table shown)
$addparams = ['selectcat' => $selectCat];

if (isset($_GET['studentoverview'])) {
$addparams['studentoverview'] = '';
}

//$addparams['cidReq']='';
if (isset($_GET['cidReq']) && $_GET['cidReq'] != '') {
$addparams['cidReq'] = Security::remove_XSS($_GET['cidReq']);
Expand All @@ -819,7 +817,7 @@ function confirmation() {
$stud_id
);

if (isset($certificate['pdf_url']) && $hideCertificateExport !== 'true') {
if ($hideCertificateExport !== 'true' && isset($certificate['pdf_url'])) {
$actionsLeft .= Display::url(
Display::returnFontAwesomeIcon('file-pdf-o').get_lang('DownloadCertificatePdf'),
$certificate['pdf_url'],
Expand Down
38 changes: 17 additions & 21 deletions main/gradebook/lib/be/category.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -798,14 +798,18 @@ public static function deleteFromCourse($courseCode)

/**
* Show message resource delete.
*
* @param string $courseCode
*
* @return mixed
*/
public function show_message_resource_delete($course_id)
public function show_message_resource_delete($courseCode)
{
$table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
$sql = 'SELECT count(*) AS num
FROM '.$table.'
WHERE
course_code = "'.Database::escape_string($course_id).'" AND
course_code = "'.Database::escape_string($courseCode).'" AND
visible = 3';
$res = Database::query($sql);
$option = Database::fetch_array($res, 'ASSOC');
Expand Down Expand Up @@ -2117,6 +2121,9 @@ public static function generateUserCertificate(
$user_id,
$sendNotification = false
) {
$user_id = (int) $user_id;
$category_id = (int) $category_id;

// Generating the total score for a course
$cats_course = self::load(
$category_id,
Expand Down Expand Up @@ -2145,32 +2152,21 @@ public static function generateUserCertificate(
$courseInfo = api_get_course_info($courseCode);
$courseId = $courseInfo['real_id'];

//@todo move these in a function
$sum_categories_weight_array = [];
if (isset($cats_course) && !empty($cats_course)) {
$categories = self::load(null, null, null, $category_id);
if (!empty($categories)) {
foreach ($categories as $subCategory) {
$sum_categories_weight_array[$subCategory->get_id()] = $subCategory->get_weight();
}
} else {
$sum_categories_weight_array[$category_id] = $cats_course[0]->get_weight();
}
}

$cattotal = self::load($category_id);
$scoretotal = $cattotal[0]->calc_score($user_id);

// Do not remove this the gradebook/lib/fe/gradebooktable.class.php
// file load this variable as a global
$scoredisplay = ScoreDisplay::instance();

$my_score_in_gradebook = $scoredisplay->display_score(
$scoretotal,
SCORE_SIMPLE
);

$userFinishedCourse = self::userFinishedCourse(
$user_id,
$cats_course[0],
$category,
true
);

Expand All @@ -2182,13 +2178,13 @@ public static function generateUserCertificate(
api_get_user_id(),
$user_id
);
$userHasSkills = false;

$userHasSkills = false;
if ($skillToolEnabled) {
$skill = new Skill();
$skill->addSkillToUser(
$user_id,
$category_id,
$category,
$courseId,
$sessionId
);
Expand All @@ -2201,7 +2197,7 @@ public static function generateUserCertificate(
);
$userHasSkills = !empty($userSkills);

if (!$category->getGenerateCertificates() && $userHasSkills) {
if ($userHasSkills && !$category->getGenerateCertificates()) {
return [
'badge_link' => Display::toolbarButton(
get_lang('ExportBadges'),
Expand All @@ -2213,7 +2209,7 @@ public static function generateUserCertificate(
}

$my_certificate = GradebookUtils::get_certificate_by_user_id(
$cats_course[0]->get_id(),
$category->get_id(),
$user_id
);

Expand All @@ -2225,7 +2221,7 @@ public static function generateUserCertificate(
api_get_utc_datetime()
);
$my_certificate = GradebookUtils::get_certificate_by_user_id(
$cats_course[0]->get_id(),
$category->get_id(),
$user_id
);
}
Expand Down
10 changes: 0 additions & 10 deletions main/gradebook/lib/fe/gradebooktable.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -465,9 +465,6 @@ public function get_table_data($from = 1, $per_page = null, $column = null, $dir

if ($this->teacherView) {
$weight_total_links += $data[3];
} else {
$cattotal = Category::load($_GET['selectcat']);
$scoretotal = $cattotal[0]->calc_score($this->userId);
}

// Edit (for admins).
Expand Down Expand Up @@ -647,13 +644,6 @@ public function get_table_data($from = 1, $per_page = null, $column = null, $dir
// Weight
$row[] = $invisibility_span_open.$weight.$invisibility_span_close;

if ($this->teacherView) {
//$weight_total_links += intval($data[3]);
} else {
$cattotal = Category::load($_GET['selectcat']);
$scoretotal = $cattotal[0]->calc_score($this->userId);
}

// Admins get an edit column.
if (api_is_allowed_to_edit(null, true) &&
isset($_GET['user_id']) == false &&
Expand Down
23 changes: 13 additions & 10 deletions main/inc/lib/certificate.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ public function __construct(
if ($this->user_id) {
// Need to be called before any operation
$this->check_certificate_path();

// To force certification generation
if ($this->force_certificate_generation) {
$this->generate([], $sendNotification);
Expand Down Expand Up @@ -227,14 +226,18 @@ public function generate($params = [], $sendNotification = false)
if (isset($my_category[0]) && !empty($categoryId) &&
$my_category[0]->is_certificate_available($this->user_id)
) {
$courseInfo = api_get_course_info($my_category[0]->get_course_code());
/** @var Category $category */
$category = $my_category[0];

$courseInfo = api_get_course_info($category->get_course_code());
$courseId = $courseInfo['real_id'];
$sessionId = $my_category[0]->get_session_id();
$sessionId = $category->get_session_id();

$skill = new Skill();

$skill->addSkillToUser(
$this->user_id,
$this->certificate_data['cat_id'],
$category,
$courseId,
$sessionId
);
Expand All @@ -243,13 +246,13 @@ public function generate($params = [], $sendNotification = false)
if (!empty($this->certificate_data)) {
$new_content_html = GradebookUtils::get_user_certificate_content(
$this->user_id,
$my_category[0]->get_course_code(),
$my_category[0]->get_session_id(),
$category->get_course_code(),
$category->get_session_id(),
false,
$params['hide_print_button']
);

if ($my_category[0]->get_id() == strval(intval($this->certificate_data['cat_id']))) {
if ($category->get_id() == $categoryId) {
$name = $this->certificate_data['path_certificate'];
$myPathCertificate = $this->certification_user_path.basename($name);

Expand Down Expand Up @@ -288,7 +291,7 @@ public function generate($params = [], $sendNotification = false)
$result = @file_put_contents($myPathCertificate, $newContent);
if ($result) {
// Updating the path
self::updateUserCertificateInfo(
$this->updateUserCertificateInfo(
$this->certificate_data['cat_id'],
$this->user_id,
$path_certificate
Expand All @@ -309,7 +312,7 @@ public function generate($params = [], $sendNotification = false)
$subject = get_lang('NotificationCertificateSubject');
$message = nl2br(get_lang('NotificationCertificateTemplate'));
$score = $this->certificate_data['score_certificate'];
Certificate::sendNotification(
self::sendNotification(
$subject,
$message,
api_get_user_info($this->user_id),
Expand Down Expand Up @@ -473,7 +476,7 @@ public function updateUserCertificateInfo(
$path_certificate,
$updateCertificateData = true
) {
if (!UserManager::is_user_certified($categoryId, $user_id) && $updateCertificateData) {
if ($updateCertificateData && !UserManager::is_user_certified($categoryId, $user_id)) {
$table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE);
$now = api_get_utc_datetime();
$sql = 'UPDATE '.$table.' SET
Expand Down
Loading

0 comments on commit 9976ca6

Please sign in to comment.