Skip to content

Commit

Permalink
Fix certification generation when using sessions see #7977
Browse files Browse the repository at this point in the history
  • Loading branch information
jmontoyaa committed Dec 15, 2015
1 parent c3be69c commit 6e9a619
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 72 deletions.
1 change: 1 addition & 0 deletions main/document/document.php
Expand Up @@ -621,6 +621,7 @@ function insertAtCarret() {
$content_html = DocumentManager::replace_user_info_into_html(
api_get_user_id(),
api_get_course_id(),
api_get_session_id(),
true
);

Expand Down
1 change: 0 additions & 1 deletion main/gradebook/gradebook_display_certificate.php
Expand Up @@ -61,7 +61,6 @@ function confirmation() {
if ($user_info['status'] == INVITEE) {
continue;
}

Category::register_user_certificate($cat_id, $user_info['user_id']);
}
}
Expand Down
24 changes: 15 additions & 9 deletions main/gradebook/lib/GradebookUtils.php
Expand Up @@ -611,8 +611,10 @@ public static function get_certificate_by_user_id($cat_id, $user_id)
$table_certificate = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE);
$sql = 'SELECT * FROM ' . $table_certificate . '
WHERE cat_id="' . intval($cat_id) . '" AND user_id="' . intval($user_id) . '"';

$result = Database::query($sql);
$row = Database::fetch_array($result, 'ASSOC');

return $row;
}

Expand Down Expand Up @@ -674,16 +676,18 @@ public static function get_list_gradebook_certificates_by_user_id($user_id, $cat
}

/**
* @param $user_id
* @param $course_code
* @param int $user_id
* @param string $course_code
* @param int $sessionId
* @param bool $is_preview
* @param bool $hide_print_button
*
* @return array
*/
public static function get_user_certificate_content($user_id, $course_code, $is_preview = false, $hide_print_button = false)
public static function get_user_certificate_content($user_id, $course_code, $sessionId, $is_preview = false, $hide_print_button = false)
{
// Generate document HTML
$content_html = DocumentManager::replace_user_info_into_html($user_id, $course_code, $is_preview);
$content_html = DocumentManager::replace_user_info_into_html($user_id, $course_code, $sessionId, $is_preview);
$new_content_html = null;
$variables = null;
$contentHead = null;
Expand Down Expand Up @@ -1201,7 +1205,7 @@ public static function updateEvaluationWeight($id, $weight)
*
* Get the achieved certificates for a user in courses
* @param int $userId The user id
* @param type $includeNonPublicCertificates Whether include the non-plublic certificates
* @param bool $includeNonPublicCertificates Whether include the non-plublic certificates
* @return array
*/
public static function getUserCertificatesInCourses($userId, $includeNonPublicCertificates = true)
Expand Down Expand Up @@ -1250,21 +1254,20 @@ public static function getUserCertificatesInCourses($userId, $includeNonPublicCe
/**
* Get the achieved certificates for a user in course sessions
* @param int $userId The user id
* @param type $includeNonPublicCertificates Whether include the non-plublic certificates
* @param bool $includeNonPublicCertificates Whether include the non-plublic certificates
* @return array
*/
public static function getUserCertificatesInSessions($userId, $includeNonPublicCertificates = true)
{
$userId = intval($userId);
$sessionList = [];

$sessions = SessionManager::get_sessions_by_user($userId);
$sessions = SessionManager::get_sessions_by_user($userId, true, true);

foreach ($sessions as $session) {
if (empty($session['courses'])) {
continue;
}

$sessionCourses = SessionManager::get_course_list_by_session_id($session['session_id']);

foreach ($sessionCourses as $course) {
Expand All @@ -1291,7 +1294,10 @@ public static function getUserCertificatesInSessions($userId, $includeNonPublicC

$courseGradebookId = $courseGradebookCategory[0]->get_id();

$certificateInfo = GradebookUtils::get_certificate_by_user_id($courseGradebookId, $userId);
$certificateInfo = GradebookUtils::get_certificate_by_user_id(
$courseGradebookId,
$userId
);

if (empty($certificateInfo)) {
continue;
Expand Down
17 changes: 12 additions & 5 deletions main/gradebook/lib/be/category.class.php
Expand Up @@ -1900,6 +1900,7 @@ public function lock_all_items($locked)
public static function register_user_certificate($category_id, $user_id)
{
$courseId = api_get_course_int_id();
$courseCode = api_get_course_id();
$sessionId = api_get_session_id();
// Generating the total score for a course
$cats_course = Category::load(
Expand Down Expand Up @@ -1941,7 +1942,7 @@ public static function register_user_certificate($category_id, $user_id)
// A student always sees only the teacher's repartition
$scoretotal_display = $scoredisplay->display_score($scoretotal, SCORE_DIV_PERCENT);

if (!self::userFinishedCourse($user_id, $cats_course[0])) {
if (!self::userFinishedCourse($user_id, $cats_course[0], 0, $courseCode, $sessionId)) {
return false;
}

Expand Down Expand Up @@ -2150,8 +2151,7 @@ public static function userFinishedCourse(
$categoryId = 0,
$courseCode = null,
$sessionId = 0
)
{
) {
if (is_null($category) && empty($categoryId)) {
return false;
}
Expand All @@ -2177,7 +2177,12 @@ public static function userFinishedCourse(
$category = $cats_course[0];
}

$currentScore = self::getCurrentScore($userId, $category->get_id(), $courseCode, $sessionId);
$currentScore = self::getCurrentScore(
$userId,
$category->get_id(),
$courseCode,
$sessionId
);

$minCertificateScore = $category->get_certificate_min_score();

Expand All @@ -2190,6 +2195,8 @@ public static function userFinishedCourse(
* @param int $categoryId The gradebook category
* @param int $courseCode The course code
* @param int $sessionId Optional. The session id
* @param bool $recalculate
*
* @return float The score
*/
public static function getCurrentScore($userId, $categoryId, $courseCode, $sessionId = 0, $recalculate = false)
Expand All @@ -2208,7 +2215,7 @@ public static function getCurrentScore($userId, $categoryId, $courseCode, $sessi
'order' => 'registered_at DESC',
'limit' => '1'
],
'fisrt'
'first'
);

if (empty($resultData)) {
Expand Down
5 changes: 3 additions & 2 deletions main/gradebook/lib/fe/gradebooktable.class.php
Expand Up @@ -716,7 +716,8 @@ public function get_table_data($from = 1, $per_page = null, $column = null, $dir

$content_html = DocumentManager::replace_user_info_into_html(
api_get_user_id(),
$course_code
$course_code,
api_get_session_id()
);

if (!empty($content_html)) {
Expand Down Expand Up @@ -962,7 +963,7 @@ private function build_name_link($item, $type = 'detail')
case 'E' :
$cat = new Category();
$course_id = CourseManager::get_course_by_category($categoryId);
$show_message = $cat->show_message_resource_delete($course_id);
$show_message = $cat->show_message_resource_delete($course_id);

// course/platform admin can go to the view_results page
if (api_is_allowed_to_edit() && $show_message===false) {
Expand Down
5 changes: 2 additions & 3 deletions main/inc/lib/api.lib.php
Expand Up @@ -1162,7 +1162,6 @@ function api_protect_course_script($print_headers = false, $allow_session_admins
if (api_is_platform_admin($allow_session_admins)) {
return true;
}

if (isset($course_info) && isset($course_info['visibility'])) {
switch ($course_info['visibility']) {
default:
Expand Down Expand Up @@ -1902,7 +1901,7 @@ function api_format_course_array($course_data)
if (array_key_exists('add_teachers_to_sessions_courses', $course_data)) {
$_course['add_teachers_to_sessions_courses'] = $course_data['add_teachers_to_sessions_courses'];
}

if (file_exists(api_get_path(SYS_COURSE_PATH).$course_data['directory'].'/course-pic85x85.png')) {
$url_image = api_get_path(WEB_COURSE_PATH).$course_data['directory'].'/course-pic85x85.png';
} else {
Expand Down Expand Up @@ -8058,7 +8057,7 @@ function api_unique_multidim_array($array, $key){
$temp_array = array();
$i = 0;
$key_array = array();

foreach($array as $val){
if(!in_array($val[$key],$key_array)){
$key_array[$i] = $val[$key];
Expand Down
23 changes: 14 additions & 9 deletions main/inc/lib/certificate.lib.php
Expand Up @@ -200,6 +200,7 @@ public function generate($params = array())
$new_content_html = GradebookUtils::get_user_certificate_content(
$this->user_id,
$my_category[0]->get_course_code(),
$my_category[0]->get_session_id(),
false,
$params['hide_print_button']
);
Expand Down Expand Up @@ -328,7 +329,7 @@ public function parse_certificate_variables($array)
$final_content = array();

if (!empty($content)) {
foreach($content as $key => $value) {
foreach ($content as $key => $value) {
$my_header = str_replace(array('((', '))') , '', $headers[$key]);
$final_content[$my_header] = $value;
}
Expand All @@ -353,13 +354,15 @@ public function parse_certificate_variables($array)
*/

$break_space = " \n\r ";

$text = $final_content['gradebook_institution'].' - '.$final_content['gradebook_sitename'].' - '.get_lang('Certification').$break_space.
get_lang('Student'). ': '.$final_content['user_firstname'].' '.$final_content['user_lastname'].$break_space.
get_lang('Teacher'). ': '.$final_content['teacher_firstname'].' '.$final_content['teacher_lastname'].$break_space.
get_lang('Date'). ': '.$final_content['date_certificate'].$break_space.
get_lang('Score'). ': '.$final_content['gradebook_grade'].$break_space.
'URL'. ': '.$final_content['certificate_link'];
$text =
$final_content['gradebook_institution'].' - '.
$final_content['gradebook_sitename'].' - '.
get_lang('Certification').$break_space.
get_lang('Student'). ': '.$final_content['user_firstname'].' '.$final_content['user_lastname'].$break_space.
get_lang('Teacher'). ': '.$final_content['teacher_firstname'].' '.$final_content['teacher_lastname'].$break_space.
get_lang('Date'). ': '.$final_content['date_certificate'].$break_space.
get_lang('Score'). ': '.$final_content['gradebook_grade'].$break_space.
'URL'. ': '.$final_content['certificate_link'];

return $text;
}
Expand All @@ -383,7 +386,9 @@ public function show()
} else {
// Check the course-level setting to make sure the certificate
// can be printed publicly
if (isset($this->certificate_data) && isset($this->certificate_data['cat_id'])) {
if (isset($this->certificate_data) &&
isset($this->certificate_data['cat_id'])
) {
$gradebook = new Gradebook();
$gradebook_info = $gradebook->get($this->certificate_data['cat_id']);
if (!empty($gradebook_info['course_code'])) {
Expand Down
16 changes: 13 additions & 3 deletions main/inc/lib/document.lib.php
Expand Up @@ -1777,6 +1777,7 @@ public static function get_default_certificate_id($course_id, $session_id = 0)
}
$sql = 'SELECT document_id FROM ' . $tbl_category . '
WHERE course_code="' . Database::escape_string($course_id) . '" ' . $sql_session;

$rs = Database::query($sql);
$num = Database::num_rows($rs);
if ($num == 0) {
Expand All @@ -1791,16 +1792,22 @@ public static function get_default_certificate_id($course_id, $session_id = 0)
* allow replace user info in file html
* @param int $user_id
* @param string $course_code
* @param int $sessionId
* @param bool $is_preview
* @return string The html content of the certificate
*/
public static function replace_user_info_into_html($user_id, $course_code, $is_preview = false)
public static function replace_user_info_into_html($user_id, $course_code, $sessionId, $is_preview = false)
{
$user_id = intval($user_id);
$course_info = api_get_course_info($course_code);
$tbl_document = Database::get_course_table(TABLE_DOCUMENT);
$course_id = $course_info['real_id'];
$document_id = self::get_default_certificate_id($course_code);

$document_id = self::get_default_certificate_id(
$course_code,
$sessionId
);

$my_content_html = null;
if ($document_id) {
$sql = "SELECT path FROM $tbl_document
Expand All @@ -1815,6 +1822,7 @@ public static function replace_user_info_into_html($user_id, $course_code, $is_p
$my_content_html = file_get_contents($filepath);
}
$all_user_info = self::get_all_info_to_certificate($user_id, $course_code, $is_preview);

$info_to_be_replaced_in_content_html = $all_user_info[0];
$info_to_replace_in_content_html = $all_user_info[1];
$new_content = str_replace(
Expand Down Expand Up @@ -1891,7 +1899,8 @@ static function get_all_info_to_certificate($user_id, $course_id, $is_preview =
$url = api_get_path(WEB_PATH) . 'certificates/index.php?id=' . $info_grade_certificate['id'];

//replace content
$info_to_replace_in_content_html = array($first_name,
$info_to_replace_in_content_html = array(
$first_name,
$last_name,
$organization_name,
$portal_name,
Expand Down Expand Up @@ -1934,6 +1943,7 @@ static function get_all_info_to_certificate($user_id, $course_id, $is_preview =

$info_list[] = $info_to_be_replaced_in_content_html;
$info_list[] = $info_to_replace_in_content_html;

return $info_list;
}

Expand Down
1 change: 1 addition & 0 deletions main/inc/lib/export.lib.inc.php
Expand Up @@ -264,6 +264,7 @@ public static function convert_array_to_html($data, $params = array())
foreach ($data as &$printable_data_row) {
$column = 0;
foreach ($printable_data_row as &$printable_data_cell) {

$table->setCellContents($row, $column, $printable_data_cell);
//$table->updateCellAttributes($row, $column, $atributes);
$column++;
Expand Down

0 comments on commit 6e9a619

Please sign in to comment.