Course order - Minor imporvements adding break in order to stop the loop when searching for the course code see BT#5558

skala
Julio Montoya 12 years ago
parent 9c86897424
commit d31f38d7ec
  1. 32
      main/inc/lib/auth.lib.php
  2. 68
      main/inc/lib/course.lib.php

@ -21,7 +21,7 @@ class Auth {
* Constructor * Constructor
*/ */
public function __construct() { public function __construct() {
} }
/** /**
@ -183,16 +183,18 @@ class Auth {
} }
} }
foreach ($user_courses as $key => $course) { $target_course = array();
foreach ($user_courses as $count => $course) {
if ($course2move == $course['code']) { if ($course2move == $course['code']) {
// source_course is the course where we clicked the up or down icon // source_course is the course where we clicked the up or down icon
$source_course = $course; $source_course = $course;
// target_course is the course before/after the source_course (depending on the up/down icon) // target_course is the course before/after the source_course (depending on the up/down icon)
if ($direction == 'up') { if ($direction == 'up') {
$target_course = $user_courses[$key - 1]; $target_course = $user_courses[$count - 1];
} else { } else {
$target_course = $user_courses[$key + 1]; $target_course = $user_courses[$count + 1];
} }
break;
} }
} }
@ -444,7 +446,7 @@ class Auth {
// Get course list auto-register // Get course list auto-register
$sql = "SELECT course_code FROM $TABLE_COURSE_FIELD_VALUE tcfv INNER JOIN $TABLE_COURSE_FIELD tcf ON tcfv.field_id = tcf.id $sql = "SELECT course_code FROM $TABLE_COURSE_FIELD_VALUE tcfv INNER JOIN $TABLE_COURSE_FIELD tcf ON tcfv.field_id = tcf.id
WHERE tcf.field_variable = 'special_course' AND tcfv.field_value = 1 "; WHERE tcf.field_variable = 'special_course' AND tcfv.field_value = 1 ";
$special_course_result = Database::query($sql); $special_course_result = Database::query($sql);
@ -472,14 +474,14 @@ class Auth {
$url_access_id = api_get_current_access_url_id(); $url_access_id = api_get_current_access_url_id();
$tbl_url_rel_course = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE); $tbl_url_rel_course = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
$sql = "SELECT COUNT(*) FROM $tbl_course course INNER JOIN $tbl_url_rel_course as url_rel_course ON (url_rel_course.course_code=course.code) $sql = "SELECT COUNT(*) FROM $tbl_course course INNER JOIN $tbl_url_rel_course as url_rel_course ON (url_rel_course.course_code=course.code)
WHERE access_url_id = $url_access_id "; WHERE access_url_id = $url_access_id ";
$result = Database::query($sql); $result = Database::query($sql);
list($num_records) = Database::fetch_row($result); list($num_records) = Database::fetch_row($result);
$sql = "SELECT course.id FROM $tbl_course course INNER JOIN $tbl_url_rel_course as url_rel_course $sql = "SELECT course.id FROM $tbl_course course INNER JOIN $tbl_url_rel_course as url_rel_course
ON (url_rel_course.course_code=course.code) ON (url_rel_course.course_code=course.code)
WHERE access_url_id = $url_access_id AND WHERE access_url_id = $url_access_id AND
RAND()*$num_records< $random_value RAND()*$num_records< $random_value
$without_special_courses ORDER BY RAND() LIMIT 0, $random_value"; $without_special_courses ORDER BY RAND() LIMIT 0, $random_value";
} else { } else {
@ -555,7 +557,7 @@ class Auth {
$TABLE_COURSE_FIELD_VALUE = Database :: get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES); $TABLE_COURSE_FIELD_VALUE = Database :: get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES);
// get course list auto-register // get course list auto-register
$sql = "SELECT course_code FROM $TABLE_COURSE_FIELD_VALUE tcfv INNER JOIN $TABLE_COURSE_FIELD tcf ON tcfv.field_id = tcf.id $sql = "SELECT course_code FROM $TABLE_COURSE_FIELD_VALUE tcfv INNER JOIN $TABLE_COURSE_FIELD tcf ON tcfv.field_id = tcf.id
WHERE tcf.field_variable = 'special_course' AND tcfv.field_value = 1 "; WHERE tcf.field_variable = 'special_course' AND tcfv.field_value = 1 ";
$special_course_result = Database::query($sql); $special_course_result = Database::query($sql);
@ -616,7 +618,7 @@ class Auth {
public function subscribe_user($course_code) { public function subscribe_user($course_code) {
$user_id = api_get_user_id(); $user_id = api_get_user_id();
$all_course_information = CourseManager::get_course_information($course_code); $all_course_information = CourseManager::get_course_information($course_code);
if ($all_course_information['registration_code'] == '' || $_POST['course_registration_code'] == $all_course_information['registration_code']) { if ($all_course_information['registration_code'] == '' || $_POST['course_registration_code'] == $all_course_information['registration_code']) {
if (api_is_platform_admin()) { if (api_is_platform_admin()) {
$status_user_in_new_course = COURSEMANAGER; $status_user_in_new_course = COURSEMANAGER;
@ -633,7 +635,7 @@ class Auth {
$message = get_lang('EnrollToCourseSuccessful'); $message = get_lang('EnrollToCourseSuccessful');
} else { } else {
$message = get_lang('ErrorContactPlatformAdmin'); $message = get_lang('ErrorContactPlatformAdmin');
} }
return array('message' => $message); return array('message' => $message);
} else { } else {
if (isset($_POST['course_registration_code']) && $_POST['course_registration_code'] != $all_course_information['registration_code']) { if (isset($_POST['course_registration_code']) && $_POST['course_registration_code'] != $all_course_information['registration_code']) {
@ -641,11 +643,11 @@ class Auth {
} }
$message = get_lang('CourseRequiresPassword') . '<br />'; $message = get_lang('CourseRequiresPassword') . '<br />';
$message .= $all_course_information['title'].' ('.$all_course_information['visual_code'].') '; $message .= $all_course_information['title'].' ('.$all_course_information['visual_code'].') ';
$action = api_get_path(WEB_CODE_PATH) . "auth/courses.php?action=subscribe_user_with_password&sec_token=" . $_SESSION['sec_token']; $action = api_get_path(WEB_CODE_PATH) . "auth/courses.php?action=subscribe_user_with_password&sec_token=" . $_SESSION['sec_token'];
$form = new FormValidator('subscribe_user_with_password', 'post', $action); $form = new FormValidator('subscribe_user_with_password', 'post', $action);
$form->addElement('hidden', 'sec_token', $_SESSION['sec_token']); $form->addElement('hidden', 'sec_token', $_SESSION['sec_token']);
$form->addElement('hidden', 'subscribe_user_with_password', $all_course_information['code']); $form->addElement('hidden', 'subscribe_user_with_password', $all_course_information['code']);
$form->addElement('text', 'course_registration_code'); $form->addElement('text', 'course_registration_code');
$form->addElement('button', 'submit', get_lang('SubmitRegistrationCode')); $form->addElement('button', 'submit', get_lang('SubmitRegistrationCode'));
$content = $form->return_form(); $content = $form->return_form();

@ -2045,57 +2045,49 @@ class CourseManager {
$TABLECOURSE = Database::get_main_table(TABLE_MAIN_COURSE); $TABLECOURSE = Database::get_main_table(TABLE_MAIN_COURSE);
$TABLECOURSUSER = Database::get_main_table(TABLE_MAIN_COURSE_USER); $TABLECOURSUSER = Database::get_main_table(TABLE_MAIN_COURSE_USER);
$course_title = Database::result(Database::query('SELECT title FROM '.$TABLECOURSE. $course_title = Database::result(Database::query('SELECT title FROM '.$TABLECOURSE.' WHERE code="'.$course_code.'"'), 0, 0);
' WHERE code="'.$course_code.'"'), 0, 0);
$result = Database::query('SELECT course.code as code, course.title as title, cu.sort as sort FROM '.$TABLECOURSUSER.' as cu, '.$TABLECOURSE.' as course $sql = 'SELECT course.code as code, course.title as title, cu.sort as sort FROM '.$TABLECOURSUSER.' as cu, '.$TABLECOURSE.' as course
WHERE course.code = cu.course_code WHERE course.code = cu.course_code AND user_id = "'.$user_id.'" AND
AND user_id = "'.$user_id.'" cu.relation_type<>'.COURSE_RELATION_TYPE_RRHH.' AND
AND cu.relation_type<>'.COURSE_RELATION_TYPE_RRHH.' user_course_cat = 0
AND user_course_cat=0 ORDER BY cu.sort'); ORDER BY cu.sort';
$result = Database::query($sql);
$course_title_precedent = ''; $course_title_precedent = '';
$counter = 0; $counter = 0;
$course_found = false; $course_found = false;
$course_sort = 1; $course_sort = 1;
while ($courses = Database::fetch_array($result)){ if (Database::num_rows($result) > 0) {
while ($courses = Database::fetch_array($result)) {
if ($course_title_precedent == '') { if ($course_title_precedent == '') {
$course_title_precedent = $courses['title']; $course_title_precedent = $courses['title'];
} }
if (api_strcasecmp($course_title_precedent, $course_title) < 0) {
if (api_strcasecmp($course_title_precedent, $course_title) < 0) { $course_found = true;
$course_sort = $courses['sort'];
$course_found = true; if ($counter == 0) {
$course_sort = $courses['sort']; $sql = 'UPDATE '.$TABLECOURSUSER.' SET sort = sort+1 WHERE user_id= "'.$user_id.'" AND relation_type<>'.COURSE_RELATION_TYPE_RRHH.' AND user_course_cat="0" AND sort > "'.$course_sort.'"';
$course_sort++;
} else {
$sql = 'UPDATE '.$TABLECOURSUSER.' SET sort = sort+1 WHERE user_id= "'.$user_id.'" AND relation_type<>'.COURSE_RELATION_TYPE_RRHH.' AND user_course_cat="0" AND sort >= "'.$course_sort.'"';
}
Database::query($sql);
break;
if ($counter == 0) {
$sql = 'UPDATE '.$TABLECOURSUSER.' SET sort = sort+1 WHERE user_id= "'.$user_id.'" AND relation_type<>'.COURSE_RELATION_TYPE_RRHH.' AND user_course_cat="0" AND sort > "'.$course_sort.'"';
$course_sort++;
} else { } else {
$sql = 'UPDATE '.$TABLECOURSUSER.' SET sort = sort+1 WHERE user_id= "'.$user_id.'" AND relation_type<>'.COURSE_RELATION_TYPE_RRHH.' AND user_course_cat="0" AND sort >= "'.$course_sort.'"'; $course_title_precedent = $courses['title'];
} }
$counter++;
Database::query($sql);
break;
} else {
$course_title_precedent = $courses['title'];
} }
$counter++; // We must register the course in the beginning of the list
} if (!$course_found) {
$course_sort = Database::result(Database::query('SELECT min(sort) as min_sort FROM '.$TABLECOURSUSER.' WHERE user_id="'.$user_id.'" AND user_course_cat="0"'), 0, 0);
// We must register the course in the beginning of the list Database::query('UPDATE '.$TABLECOURSUSER.' SET sort = sort+1 WHERE user_id= "'.$user_id.'" AND user_course_cat="0"');
if (Database::num_rows($result) > 0 && !$course_found) { }
$course_sort = Database::result(Database::query('SELECT min(sort) as min_sort FROM '.$TABLECOURSUSER.
' WHERE user_id="'.$user_id.'" AND user_course_cat="0"'), 0, 0);
Database::query('UPDATE '.$TABLECOURSUSER.' SET sort = sort+1
WHERE user_id= "'.$user_id.'" AND user_course_cat="0"');
} }
return $course_sort; return $course_sort;
} }

Loading…
Cancel
Save