From 6e9a619fdc281a7efa13f08052c6ad2fa54a10bb Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Tue, 15 Dec 2015 13:17:54 +0100 Subject: [PATCH] Fix certification generation when using sessions see #7977 --- main/document/document.php | 1 + .../gradebook_display_certificate.php | 1 - main/gradebook/lib/GradebookUtils.php | 24 +++++---- main/gradebook/lib/be/category.class.php | 17 +++++-- .../gradebook/lib/fe/gradebooktable.class.php | 5 +- main/inc/lib/api.lib.php | 5 +- main/inc/lib/certificate.lib.php | 23 +++++---- main/inc/lib/document.lib.php | 16 ++++-- main/inc/lib/export.lib.inc.php | 1 + main/inc/lib/sessionmanager.lib.php | 44 +++++++++------- main/inc/lib/usermanager.lib.php | 51 +++++++++++-------- 11 files changed, 116 insertions(+), 72 deletions(-) diff --git a/main/document/document.php b/main/document/document.php index d6f69707a8..063732681f 100755 --- a/main/document/document.php +++ b/main/document/document.php @@ -621,6 +621,7 @@ if (isset($_GET['curdirpath']) && $content_html = DocumentManager::replace_user_info_into_html( api_get_user_id(), api_get_course_id(), + api_get_session_id(), true ); diff --git a/main/gradebook/gradebook_display_certificate.php b/main/gradebook/gradebook_display_certificate.php index b46d938e4c..802f47ca13 100755 --- a/main/gradebook/gradebook_display_certificate.php +++ b/main/gradebook/gradebook_display_certificate.php @@ -61,7 +61,6 @@ switch ($action) { if ($user_info['status'] == INVITEE) { continue; } - Category::register_user_certificate($cat_id, $user_info['user_id']); } } diff --git a/main/gradebook/lib/GradebookUtils.php b/main/gradebook/lib/GradebookUtils.php index c347b56556..86b4054b7f 100644 --- a/main/gradebook/lib/GradebookUtils.php +++ b/main/gradebook/lib/GradebookUtils.php @@ -611,8 +611,10 @@ class GradebookUtils $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; } @@ -674,16 +676,18 @@ class GradebookUtils } /** - * @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; @@ -1201,7 +1205,7 @@ class GradebookUtils * * 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) @@ -1250,7 +1254,7 @@ class GradebookUtils /** * 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) @@ -1258,13 +1262,12 @@ class GradebookUtils $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) { @@ -1291,7 +1294,10 @@ class GradebookUtils $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; diff --git a/main/gradebook/lib/be/category.class.php b/main/gradebook/lib/be/category.class.php index a57a461483..3cdf490fba 100755 --- a/main/gradebook/lib/be/category.class.php +++ b/main/gradebook/lib/be/category.class.php @@ -1900,6 +1900,7 @@ class Category implements GradebookItem 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( @@ -1941,7 +1942,7 @@ class Category implements GradebookItem // 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; } @@ -2150,8 +2151,7 @@ class Category implements GradebookItem $categoryId = 0, $courseCode = null, $sessionId = 0 - ) - { + ) { if (is_null($category) && empty($categoryId)) { return false; } @@ -2177,7 +2177,12 @@ class Category implements GradebookItem $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(); @@ -2190,6 +2195,8 @@ class Category implements GradebookItem * @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) @@ -2208,7 +2215,7 @@ class Category implements GradebookItem 'order' => 'registered_at DESC', 'limit' => '1' ], - 'fisrt' + 'first' ); if (empty($resultData)) { diff --git a/main/gradebook/lib/fe/gradebooktable.class.php b/main/gradebook/lib/fe/gradebooktable.class.php index d2fe3b6d85..2bd20a3a75 100755 --- a/main/gradebook/lib/fe/gradebooktable.class.php +++ b/main/gradebook/lib/fe/gradebooktable.class.php @@ -716,7 +716,8 @@ class GradebookTable extends SortableTable $content_html = DocumentManager::replace_user_info_into_html( api_get_user_id(), - $course_code + $course_code, + api_get_session_id() ); if (!empty($content_html)) { @@ -962,7 +963,7 @@ class GradebookTable extends SortableTable 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) { diff --git a/main/inc/lib/api.lib.php b/main/inc/lib/api.lib.php index c815c5ad2c..1700baccdc 100644 --- a/main/inc/lib/api.lib.php +++ b/main/inc/lib/api.lib.php @@ -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: @@ -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 { @@ -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]; diff --git a/main/inc/lib/certificate.lib.php b/main/inc/lib/certificate.lib.php index b54f8fbcae..c9e0db1c49 100755 --- a/main/inc/lib/certificate.lib.php +++ b/main/inc/lib/certificate.lib.php @@ -200,6 +200,7 @@ class Certificate extends Model $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'] ); @@ -328,7 +329,7 @@ class Certificate extends Model $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; } @@ -353,13 +354,15 @@ class Certificate extends Model */ $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; } @@ -383,7 +386,9 @@ class Certificate extends Model } 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'])) { diff --git a/main/inc/lib/document.lib.php b/main/inc/lib/document.lib.php index 5b50fd3317..ffaa893ac6 100755 --- a/main/inc/lib/document.lib.php +++ b/main/inc/lib/document.lib.php @@ -1777,6 +1777,7 @@ class DocumentManager } $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) { @@ -1791,16 +1792,22 @@ class DocumentManager * 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 @@ -1815,6 +1822,7 @@ class DocumentManager $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( @@ -1891,7 +1899,8 @@ class DocumentManager $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, @@ -1934,6 +1943,7 @@ class DocumentManager $info_list[] = $info_to_be_replaced_in_content_html; $info_list[] = $info_to_replace_in_content_html; + return $info_list; } diff --git a/main/inc/lib/export.lib.inc.php b/main/inc/lib/export.lib.inc.php index 0286c51d25..fe2a7d71ce 100755 --- a/main/inc/lib/export.lib.inc.php +++ b/main/inc/lib/export.lib.inc.php @@ -264,6 +264,7 @@ class Export 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++; diff --git a/main/inc/lib/sessionmanager.lib.php b/main/inc/lib/sessionmanager.lib.php index e0d0487b47..006f26afad 100755 --- a/main/inc/lib/sessionmanager.lib.php +++ b/main/inc/lib/sessionmanager.lib.php @@ -1340,7 +1340,7 @@ class SessionManager * @param int $duration * @param array $extraFields * @param int $sessionAdminId - * @param boolean $sendSubscritionNotification Optional. + * @param boolean $sendSubscriptionNotification Optional. * Whether send a mail notification to users being subscribed * @return mixed */ @@ -1361,7 +1361,7 @@ class SessionManager $duration = null, $extraFields = array(), $sessionAdminId = 0, - $sendSubscritionNotification = false + $sendSubscriptionNotification = false ) { $name = trim(stripslashes($name)); $coachId = intval($coachId); @@ -1370,20 +1370,25 @@ class SessionManager $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION); if (empty($name)) { - $msg = get_lang('SessionNameIsRequired'); - return $msg; + Display::return_message(get_lang('SessionNameIsRequired'), 'warning'); + + return false; } elseif (empty($coachId)) { - $msg = get_lang('CoachIsRequired'); - return $msg; + Display::return_message(get_lang('CoachIsRequired'), 'warning'); + + return false; } elseif (!empty($startDate) && !api_is_valid_date($startDate, 'Y-m-d H:i')) { - $msg = get_lang('InvalidStartDate'); - return $msg; + Display::return_message(get_lang('InvalidStartDate'), 'warning'); + + return false; } elseif (!empty($endDate) && !api_is_valid_date($endDate, 'Y-m-d H:i')) { - $msg = get_lang('InvalidEndDate'); - return $msg; + Display::return_message(get_lang('InvalidEndDate'), 'warning'); + + return false; } elseif (!empty($startDate) && !empty($endDate) && $startDate >= $endDate) { - $msg = get_lang('StartDateShouldBeBeforeEndDate'); - return $msg; + Display::return_message(get_lang('StartDateShouldBeBeforeEndDate'), 'warning'); + + return false; } else { $sql = "SELECT id FROM $tbl_session WHERE name='" . Database::escape_string($name) . "'"; $rs = Database::query($sql); @@ -1395,8 +1400,9 @@ class SessionManager } if ($exists) { - $msg = get_lang('SessionNameAlreadyExists'); - return $msg; + Display::return_message(get_lang('SessionNameAlreadyExists'), 'warning'); + + return false; } else { $values = [ 'name' => $name, @@ -1405,7 +1411,7 @@ class SessionManager 'description'=> $description, 'show_description' => intval($showDescription), 'visibility' => $visibility, - 'send_subscription_notification' => $sendSubscritionNotification + 'send_subscription_notification' => $sendSubscriptionNotification ]; if (!empty($sessionAdminId)) { @@ -3953,16 +3959,18 @@ class SessionManager /** * @param int $user_id * @param bool $ignore_visibility_for_admins + * @param bool $ignoreTimeLimit + * * @return array */ - public static function get_sessions_by_user($user_id, $ignore_visibility_for_admins = false) + public static function get_sessions_by_user($user_id, $ignore_visibility_for_admins = false, $ignoreTimeLimit = false) { $sessionCategories = UserManager::get_sessions_by_category( $user_id, false, - $ignore_visibility_for_admins + $ignore_visibility_for_admins, + $ignoreTimeLimit ); - $sessionArray = array(); if (!empty($sessionCategories)) { foreach ($sessionCategories as $category) { diff --git a/main/inc/lib/usermanager.lib.php b/main/inc/lib/usermanager.lib.php index f16d61fd88..d6475a65a1 100755 --- a/main/inc/lib/usermanager.lib.php +++ b/main/inc/lib/usermanager.lib.php @@ -1622,9 +1622,9 @@ class UserManager //Crop the image to adjust 1:1 ratio $image = new Image($source_file); $image->crop($cropParameters); - + // Storing the new photos in 4 versions with various sizes. - + $small = new Image($source_file); $small->resize(22); $small->send_image($path.'small_'.$filename); @@ -1637,9 +1637,9 @@ class UserManager $big = new Image($source_file); // This is the original picture. $big->send_image($path.'big_'.$filename); - + $result = $small && $medium && $normal && $big; - + return $result ? $filename : false; } @@ -2294,13 +2294,16 @@ class UserManager * @param integer $user_id * @param boolean whether to fill the first element or not (to give space for courses out of categories) * @param boolean optional true if limit time from session is over, false otherwise + * @param boolean $ignoreTimeLimit ignore time start/end * @return array list of statuses [session_category][session_id] + * * @todo ensure multiple access urls are managed correctly */ public static function get_sessions_by_category( $user_id, $is_time_over = true, - $ignore_visibility_for_admins = false + $ignore_visibility_for_admins = false, + $ignoreTimeLimit = false ) { // Database Table Definitions $tbl_session = Database :: get_main_table(TABLE_MAIN_SESSION); @@ -2343,27 +2346,29 @@ class UserManager while ($row = Database::fetch_array($result, 'ASSOC')) { // User portal filters: - if ($is_time_over) { - // History - if (empty($row['access_end_date']) || $row['access_end_date'] == '0000-00-00 00:00:00') { - continue; - } - - if (isset($row['access_end_date'])) { - if ($row['access_end_date'] > $now) { + if ($ignoreTimeLimit == false) { + if ($is_time_over) { + // History + if (empty($row['access_end_date']) || $row['access_end_date'] == '0000-00-00 00:00:00') { continue; } - } - } else { - // Current user portal - if (api_is_allowed_to_create_course()) { - // Teachers can access the session depending in the access_coach date - } else { - if (isset($row['access_end_date']) && $row['access_end_date'] != '0000-00-00 00:00:00') { - if ($row['access_end_date'] <= $now) { + if (isset($row['access_end_date'])) { + if ($row['access_end_date'] > $now) { continue; } + + } + } else { + // Current user portal + if (api_is_allowed_to_create_course()) { + // Teachers can access the session depending in the access_coach date + } else { + if (isset($row['access_end_date']) && $row['access_end_date'] != '0000-00-00 00:00:00') { + if ($row['access_end_date'] <= $now) { + continue; + } + } } } } @@ -2422,7 +2427,9 @@ class UserManager case SESSION_AVAILABLE: break; case SESSION_INVISIBLE: - continue(2); + if ($ignore_visibility_for_admins == false) { + continue(2); + } } $categories[$row['session_category_id']]['sessions'][$row['id']] = array(