Adding new actions to the certificate page, generate certificate, delete all certificates, download certificates as PDF + a fix in the certificate min score see #3708

skala
Julio Montoya 13 years ago
parent 012940efe3
commit 83ee45581b
  1. 90
      main/gradebook/gradebook_display_certificate.php
  2. 80
      main/gradebook/index.php
  3. 78
      main/gradebook/lib/be/category.class.php
  4. 11
      main/gradebook/lib/fe/displaygradebook.php
  5. 8
      main/gradebook/lib/gradebook_functions.inc.php
  6. 24
      main/inc/lib/certificate.lib.php
  7. 19
      main/inc/lib/pdf.lib.php

@ -35,20 +35,69 @@ if (!api_is_allowed_to_edit()) {
api_not_allowed(true);
}
$cat_id = isset($_GET['cat_id']) ? (int)$_GET['cat_id'] : null;
$action = isset($_GET['action']) && $_GET['action'] ? $_GET['action'] : null;
switch ($action) {
case 'export_all_certificates':
$params['orientation'] = 'landscape';
$page_format = $params['orientation'] == 'landscape' ? 'A4-L' : 'A4';
$pdf = new PDF($page_format, $params['orientation'], $params);
$certificate_list = get_list_users_certificates($cat_id);
$certificate_path_list = array();
if (!empty($certificate_list)) {
foreach ($certificate_list as $index=>$value) {
$list_certificate = get_list_gradebook_certificates_by_user_id($value['user_id'], $cat_id);
foreach ($list_certificate as $value_certificate) {
$certificate_obj = new Certificate($value_certificate['id']);
$certificate_obj->generate(array('hide_print_button' => true));
if ($certificate_obj->html_file_is_generated()) {
$certificate_path_list[]= $certificate_obj->html_file;
}
}
}
}
if (!empty($certificate_path_list)) {
$pdf->html_to_pdf($certificate_path_list, get_lang('Certificates'));
}
break;
case 'generate_all_certificates':
$user_list = Coursemanager::get_user_list_from_course_code(api_get_course_id(), api_get_session_id());
if (!empty($user_list)) {
foreach ($user_list as $user_info) {
Category::register_user_certificate($cat_id, $user_info['user_id']);
}
}
break;
case 'delete_all_certificates':
$certificate_list = get_list_users_certificates($cat_id);
if (!empty($certificate_list)) {
foreach ($certificate_list as $index=>$value) {
$list_certificate = get_list_gradebook_certificates_by_user_id($value['user_id'], $cat_id);
foreach ($list_certificate as $value_certificate) {
$certificate_obj = new Certificate($value_certificate['id']);
$certificate_obj->delete(true);
}
}
}
break;
}
$course_code = api_get_course_id();
$interbreadcrumb[] = array ('url' => Security::remove_XSS($_SESSION['gradebook_dest']).'?', 'name' => get_lang('Gradebook'));
$interbreadcrumb[] = array ('url' => '#','name' => get_lang('GradebookListOfStudentsCertificates'));
$this_section = SECTION_COURSES;
Display::display_header('');
if ($_GET['action'] == 'delete') {
$check = Security::check_token('get');
if ($check) {
$certificate = new Certificate($_GET['certificate_id']);
$result = $certificate->delete();
$result = $certificate->delete(true);
Security::clear_token();
if ($result ==true) {
Display::display_confirmation_message(get_lang('CertificateRemoved'));
@ -61,10 +110,9 @@ if ($_GET['action'] == 'delete') {
$token = Security::get_token();
echo Display::page_header(get_lang('GradebookListOfStudentsCertificates'));
$cat_id = isset($_GET['cat_id']) ? (int)$_GET['cat_id'] : null;
//@todo replace all this code with something like get_total_weight()
$cats = Category :: load ($cat_id, null, null, null, null, null, false);
$cats = Category :: load($cat_id, null, null, null, null, null, false);
if (!empty($cats)) {
@ -104,33 +152,43 @@ if (!empty($cats)) {
}
}
$certificate_list = get_list_users_certificates($cat_id);
$certificate_list = get_list_users_certificates($cat_id);
echo '<div class="btn-group">';
$url = api_get_self().'?action=generate_all_certificates'.'&'.api_get_cidReq().'&cat_id='.$cat_id;
echo Display::url(get_lang('GenerateCertificates'), $url, array('class' => 'btn'));
$url = api_get_self().'?action=delete_all_certificates'.'&'.api_get_cidReq().'&cat_id='.$cat_id;
echo Display::url(get_lang('DeleteAllCertificates'), $url, array('class' => 'btn'));
if (count($certificate_list) > 0) {
$url = api_get_self().'?action=export_all_certificates'.'&'.api_get_cidReq().'&cat_id='.$cat_id;
echo Display::url(get_lang('ExportAllCertificatesToPDF'), $url, array('class' => 'btn'));
}
echo '</div>';
if (count($certificate_list)==0) {
echo Display::display_warning_message(get_lang('NoResultsAvailable'));
echo Display::display_warning_message(get_lang('NoResultsAvailable'));
} else {
echo '<table class="data_table">';
echo '<br /><br /><table class="data_table">';
foreach ($certificate_list as $index=>$value) {
echo '<tr>
<td width="100%" class="actions">'.get_lang('Student').' : '.api_get_person_name($value['firstname'], $value['lastname']).' ( '.$value['username'].')</td>';
<td width="100%" class="actions">'.get_lang('Student').' : '.api_get_person_name($value['firstname'], $value['lastname']).' ('.$value['username'].')</td>';
echo '</tr>';
echo '<tr><td>
<table class="data_table">';
$list_certificate = get_list_gradebook_certificates_by_user_id ($value['user_id'], $cat_id);
$list_certificate = get_list_gradebook_certificates_by_user_id($value['user_id'], $cat_id);
foreach ($list_certificate as $value_certificate) {
echo '<tr>';
echo '<td width="50%">'.get_lang('Score').' : '.$value_certificate['score_certificate'].'</td>';
echo '<td width="30%">'.get_lang('Date').' : '.api_convert_and_format_date($value_certificate['created_at']).'</td>';
echo '<td width="20%">';
//$url = "index.php?export_certificate=yes&cat_id=".$cat_id."&user=".$value['user_id'];
echo '<td width="20%">';
$url = api_get_path(WEB_PATH).'certificates/index.php?id='.$value_certificate['id'];
$certificates = Display::url(get_lang('Certificate'), $url, array('target'=>'_blank', 'class' => 'btn'));
echo $certificates;
$certificates = Display::url(get_lang('Certificate'), $url, array('target'=>'_blank', 'class' => 'btn'));
echo $certificates;
echo '<a onclick="return confirmation();" href="gradebook_display_certificate.php?sec_token='.$token.'&cidReq='.$course_code.'&action=delete&cat_id='.$cat_id.'&certificate_id='.$value_certificate['id'].'">
'.Display::return_icon('delete.png',get_lang('Delete')).'
</a>';

@ -617,7 +617,7 @@ if (empty ($_GET['selectcat'])) {
}
$simple_search_form='';
if (isset ($_GET['studentoverview'])) {
if (isset($_GET['studentoverview'])) {
//@todo this code also seems to be deprecated ...
$cats= Category :: load($category);
$stud_id= (api_is_allowed_to_edit() ? null : $stud_id);
@ -732,81 +732,13 @@ if ($category != '0') {
$course_id = Database::get_course_by_category($category_id);
$show_message = $cat->show_message_resource_delete($course_id);
if ($show_message=='') {
if ($show_message == '') {
//student
if (!api_is_allowed_to_edit()) {
// generating the total score for a course
$cats_course = Category :: load ($category_id, null, null, null, null, null, false);
$alleval_course = $cats_course[0]->get_evaluations($stud_id,true);
$alllink_course = $cats_course[0]->get_links($stud_id,true);
$evals_links = array_merge($alleval_course, $alllink_course);
$item_value = 0;
$item_total = 0;
//@todo move these in a function
$sum_categories_weight_array = array();
if (isset($cats_course) && !empty($cats_course)) {
$categories = Category::load(null, null, null, $category_id);
if (!empty($categories)) {
foreach($categories as $category) {
$sum_categories_weight_array[$category->get_id()] = $category->get_weight();
}
} else {
$sum_categories_weight_array[$category_id] = $cats_course[0]->get_weight();
}
}
$item_total_value = 0;
$item_value = 0;
for ($count=0; $count < count($evals_links); $count++) {
$item = $evals_links[$count];
$score = $item->calc_score($stud_id);
$score_denom = ($score[1]==0) ? 1 : $score[1];
$item_value = $score[0]/$score_denom*$item->get_weight();
$item_total += $item->get_weight();
$item_total_value += $item_value;
}
$item_total_value = (float)$item_total_value;
$cattotal = Category :: load($category_id);
$scoretotal= $cattotal[0]->calc_score(api_get_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 = round($scoretotal[0],2);
$my_score_in_gradebook = $scoredisplay->display_score($scoretotal, SCORE_SIMPLE);
//Show certificate
$certificate_min_score = $cats[0]->get_certificate_min_score();
$scoretotal_display = $scoredisplay->display_score($scoretotal, SCORE_DIV_PERCENT); //a student always sees only the teacher's repartition
//var_dump($certificate_min_score, $item_total_value);
if (isset($certificate_min_score) && $item_total_value >= $certificate_min_score) {
$my_certificate = get_certificate_by_user_id($cats[0]->get_id(), api_get_user_id());
if (empty($my_certificate)) {
register_user_info_about_certificate($category_id, api_get_user_id(), $my_score_in_gradebook, api_get_utc_datetime());
$my_certificate = get_certificate_by_user_id($cats[0]->get_id(), api_get_user_id());
}
if (!empty($my_certificate)) {
$url = api_get_path(WEB_PATH) .'certificates/index.php?id='.$my_certificate['id'];
$certificates = Display::url(Display::return_icon('certificate.png', get_lang('Certificates'), array(), 32), $url, array('target'=>'_blank'));
echo '<div class="actions" align="right">';
echo Display::url($url, $url, array('target'=>'_blank'));
echo $certificates;
echo '</div>';
}
}
$certificate_html = Category::register_user_certificate($category_id, $stud_id);
if ($certificate_html) {
echo $certificate_html;
}
} //end hack
}
}

@ -1360,5 +1360,81 @@ class Category implements GradebookItem
}
event_system($event_type, LOG_GRADEBOOK_ID, $this->id);
}
}
}
static function register_user_certificate($category_id, $user_id) {
// generating the total score for a course
$cats_course = Category :: load($category_id, null, null, null, null, null, false);
$alleval_course = $cats_course[0]->get_evaluations($user_id, true);
$alllink_course = $cats_course[0]->get_links($user_id, true);
$evals_links = array_merge($alleval_course, $alllink_course);
$item_total = 0;
//@todo move these in a function
$sum_categories_weight_array = array();
if (isset($cats_course) && !empty($cats_course)) {
$categories = Category::load(null, null, null, $category_id);
if (!empty($categories)) {
foreach($categories as $category) {
$sum_categories_weight_array[$category->get_id()] = $category->get_weight();
}
} else {
$sum_categories_weight_array[$category_id] = $cats_course[0]->get_weight();
}
}
$main_weight = $cats_course[0]->get_weight();
$item_total_value = 0;
$item_value = 0;
for ($count=0; $count < count($evals_links); $count++) {
$item = $evals_links[$count];
$score = $item->calc_score($user_id);
$divide = ( ($score[1])==0 ) ? 1 : $score[1];
$sub_cat_percentage = $sum_categories_weight_array[$item->get_category_id()];
$item_value = $score[0]/$divide*$item->get_weight()*$sub_cat_percentage/$main_weight;
$item_total_value += $item_value;
}
$item_total_value = (float)$item_total_value;
$cattotal = Category :: 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);
//Show certificate
$certificate_min_score = $cats_course[0]->get_certificate_min_score();
$scoretotal_display = $scoredisplay->display_score($scoretotal, SCORE_DIV_PERCENT); //a student always sees only the teacher's repartition
if (isset($certificate_min_score) && $item_total_value >= $certificate_min_score) {
$my_certificate = get_certificate_by_user_id($cats_course[0]->get_id(), $user_id);
if (empty($my_certificate)) {
register_user_info_about_certificate($category_id, $user_id, $my_score_in_gradebook, api_get_utc_datetime());
$my_certificate = get_certificate_by_user_id($cats_course[0]->get_id(), $user_id);
}
if (!empty($my_certificate)) {
$certificate_obj = new Certificate($my_certificate['id']);
$url = api_get_path(WEB_PATH) .'certificates/index.php?id='.$my_certificate['id'];
$certificates = Display::url(Display::return_icon('certificate.png', get_lang('Certificates'), array(), 32), $url, array('target'=>'_blank'));
$html = '<div class="actions" align="right">';
$html .= Display::url($url, $url, array('target'=>'_blank'));
$html .= $certificates;
$html .= '</div>';
return $html;
}
} else {
return false;
}
}
}

@ -392,22 +392,19 @@ class DisplayGradebook
$item_value =0;
$item_total = 0;
$item_total_value = 0;
//var_dump($sum_categories_weight_array);
for ($count=0; $count < count($evals_links); $count++) {
$item = $evals_links[$count];
$score = $item->calc_score($user_id);
$divide = ( ($score[1])==0 ) ? 1 : $score[1];
$sub_cat_percentage = $sum_categories_weight_array[$item->get_category_id()];
$item_value = $score[0]/$divide;
//$item_value = $item_value*$item->get_weight();
$sub_cat_percentage = $sum_categories_weight_array[$item->get_category_id()];
//$item_value = $score[0]/$divide;
$item_value = $score[0]/$divide*$item->get_weight()*$sub_cat_percentage/$main_weight;
//var_dump($score[0].' '.$divide.' '.$item->get_weight().' - '.$item_value.' '.$sub_cat_percentage);
$item_value_total +=$item_value;
}
$item_total = $main_weight;
$total_score = array($item_value_total, $item_total);
$scorecourse_display = $scoredisplay->display_score($total_score, SCORE_DIV_PERCENT);
@ -422,7 +419,7 @@ class DisplayGradebook
$header = '<div class="actions"><table>';
$header .= '<tr>';
if (!$selectcat == '0') {
$header .= '<td style=" "><a href="' . api_get_self() . '?selectcat=' . $catobj->get_parent_id() . '">'.Display::return_icon('back.png',get_lang('BackTo').' '.get_lang('RootCat'),'',ICON_SIZE_MEDIUM).'</a></td>';
$header .= '<td><a href="' . api_get_self() . '?selectcat=' . $catobj->get_parent_id() . '">'.Display::return_icon('back.png',get_lang('BackTo').' '.get_lang('RootCat'),'',ICON_SIZE_MEDIUM).'</a></td>';
}
$header .= '<td>' . get_lang('CurrentCategory') . '</td>' .
'<td><form name="selector"><select name="selectcat" onchange="document.selector.submit()">';

@ -591,7 +591,7 @@ function get_list_gradebook_certificates_by_user_id($user_id, $cat_id = null) {
return $list_certificate;
}
function get_user_certificate_content($user_id, $course_code, $is_preview = false) {
function get_user_certificate_content($user_id, $course_code, $is_preview = false, $hide_print_button = false) {
//generate document HTML
$content_html = DocumentManager::replace_user_info_into_html($user_id, $course_code, $is_preview);
@ -605,8 +605,10 @@ function get_user_certificate_content($user_id, $course_code, $is_preview = fals
$new_content_html = str_replace('/main/img/', api_get_path(WEB_IMG_PATH), $new_content_html);
//add print header
$print = '<style media="print" type="text/css">#print_div {visibility:hidden;}</style>';
$print .= '<a href="javascript:window.print();" style="float:right; padding:4px;" id="print_div"><img src="' . api_get_path(WEB_CODE_PATH) . 'img/printmgr.gif" alt="' . get_lang('Print') . '" /> ' . get_lang('Print') . '</a>';
if ($hide_print_button == false) {
$print = '<style media="print" type="text/css">#print_div {visibility:hidden;}</style>';
$print .= '<a href="javascript:window.print();" style="float:right; padding:4px;" id="print_div"><img src="' . api_get_path(WEB_CODE_PATH) . 'img/printmgr.gif" alt="' . get_lang('Print') . '" /> ' . get_lang('Print') . '</a>';
}
//add header
$new_content_html = $new_content[0] . $print . '</head>' . $new_content_html;

@ -62,8 +62,8 @@ class Certificate extends Model {
//Setting the qr and html variables
if (isset($certificate_id) && !empty($this->certification_user_path)) {
$pathinfo = pathinfo($this->certificate_data['path_certificate']);
$this->html_file = $this->certification_user_path.basename($this->certificate_data['path_certificate']);
$this->qr_file = $this->certification_user_path.$pathinfo['filename'].'_qr.png';
$this->html_file = $this->certification_user_path.basename($this->certificate_data['path_certificate']);
$this->qr_file = $this->certification_user_path.$pathinfo['filename'].'_qr.png';
}
}
@ -99,11 +99,10 @@ class Certificate extends Model {
* the teacher from the gradebook tool to re-generate the certificate because
* the original version wa flawed.
*/
public function delete($id = null) {
public function delete($force_delete = false) {
if (!empty($this->certificate_data)) {
if (!is_null($this->html_file) || $this->html_file!='' || strlen($this->html_file)) {
if (!is_null($this->html_file) || $this->html_file != '' || strlen($this->html_file)) {
//Deleting HTML file
if (is_file($this->html_file)) {
@unlink($this->html_file);
@ -116,8 +115,8 @@ class Certificate extends Model {
//Deleting QR code PNG image file
if (is_file($this->qr_file)) {
@unlink($this->qr_file);
}
if ($delete_db) {
}
if ($delete_db || $force_delete) {
return parent::delete($this->certificate_data['id']);
}
} else {
@ -131,7 +130,7 @@ class Certificate extends Model {
* Generates an HTML Certificate and fills the path_certificate field in the DB
* */
public function generate() {
public function generate($params = array()) {
//The user directory should be set
if (empty($this->certification_user_path) && $this->force_certificate_generation == false) {
return false;
@ -140,6 +139,8 @@ class Certificate extends Model {
require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/gradebook_functions.inc.php';
require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/scoredisplay.class.php';
$params['hide_print_button'] = isset($params['hide_print_button']) ? true : false;
$my_category = Category :: load($this->certificate_data['cat_id']);
if (isset($my_category[0]) && $my_category[0]->is_certificate_available($this->user_id)) {
@ -166,8 +167,8 @@ class Certificate extends Model {
$skill->add_skill_to_user($this->user_id, $this->certificate_data['cat_id']);
if (is_dir($this->certification_user_path)) {
if (!empty($this->certificate_data)) {
$new_content_html = get_user_certificate_content($this->user_id, $my_category[0]->get_course_code(), false);
if (!empty($this->certificate_data)) {
$new_content_html = get_user_certificate_content($this->user_id, $my_category[0]->get_course_code(), false, $params['hide_print_button']);
if ($my_category[0]->get_id() == strval(intval($this->certificate_data['cat_id']))) {
$name = $this->certificate_data['path_certificate'];
@ -225,7 +226,6 @@ class Certificate extends Model {
Database::query($sql);
}
}
/**
*
@ -360,4 +360,4 @@ class Certificate extends Model {
}
exit;
}
}
}

@ -116,14 +116,17 @@ class PDF {
}
$course_data = array();
if (!empty($course_code)) {
$course_data = api_get_course_info($course_code);
} else {
$course_data = api_get_course_info();
}
//clean styles and javascript document
$clean_search = array (
'@<script[^>]*?>.*?</script>@si',
'@<style[^>]*?>.*?</style>@si'
'@<style[^>]*?>.*?</style>@si'
);
//Formatting the pdf
@ -184,17 +187,14 @@ class PDF {
}
$document_html = @file_get_contents($file);
$document_html = preg_replace($clean_search, '', $document_html);
$document_html = preg_replace($clean_search, '', $document_html);
//absolute path for frames.css //TODO: necessary?
$absolute_css_path = api_get_path(WEB_CODE_PATH).'css/'.api_get_setting('stylesheets').'/frames.css';
$document_html = str_replace('href="./css/frames.css"', $absolute_css_path, $document_html);
//$document_html=str_replace('<link rel="stylesheet" http://my.chamilo.net/main/css/chamilo/frames.css type="text/css" />','', $document_html);
if (!empty($course_data['path'])) {
$document_html= str_replace('../','',$document_html);
//$document_path = api_get_path(WEB_COURSE_PATH).$course_data['path'].'/document/';
$document_html= str_replace('../','',$document_html);
$document_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document/';
$doc = new DOMDocument();
@ -210,8 +210,7 @@ class PDF {
if (api_get_path(REL_PATH) != '/') {
$old_src = str_replace(api_get_path(REL_PATH), '', $old_src);
}
$old_src_fixed = str_replace('courses/'.$course_data['path'].'/document/', '', $old_src);
//$old_src_fixed = str_replace('courses/'.$course_data['path'].'/document/', '', $old_src_fixed);
$old_src_fixed = str_replace('courses/'.$course_data['path'].'/document/', '', $old_src);
$new_path = $document_path.$old_src_fixed;
$document_html= str_replace($old_src, $new_path, $document_html);
}

Loading…
Cancel
Save