Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue#34 researcher preferences should be put apart #42

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
16bec2f
Resolving issues filtering with no organizations
SamuelVch98 Jul 27, 2024
585ace3
use toast to display errors
SamuelVch98 Jul 29, 2024
0da87f0
Merge branch 'UCL-INGI:main' into main
SamuelVch98 Jul 29, 2024
eada3ca
Fix small issues
SamuelVch98 Jul 30, 2024
7cae218
create unique file for toast and use it
SamuelVch98 Jul 30, 2024
4a633f6
Merge branch 'UCL-INGI:main' into main
SamuelVch98 Jul 30, 2024
99f16d6
Merge branch 'UCL-INGI:main' into main
SamuelVch98 Aug 20, 2024
cf3938d
Merge branch 'UCL-INGI:main' into main
SamuelVch98 Sep 12, 2024
ad8658c
Merge branch 'UCL-INGI:main' into main
SamuelVch98 Oct 1, 2024
1177b11
indicate the type of user to be displayed
SamuelVch98 Oct 9, 2024
9f8b4cc
Fix the bug that prevents you from changing organisation if none was …
SamuelVch98 Oct 9, 2024
ec2bdc1
Merge branch 'UCL-INGI:main' into main
SamuelVch98 Oct 13, 2024
97023c8
Merge branch 'UCL-INGI:main' into main
SamuelVch98 Oct 15, 2024
af8da85
Merge branch 'UCL-INGI:main' into main
SamuelVch98 Oct 15, 2024
4db087a
Merge branch 'UCL-INGI:main' into main
SamuelVch98 Oct 16, 2024
d40c27b
Merge branch 'UCL-INGI:main' into main
SamuelVch98 Oct 17, 2024
84d1373
Define the current year statically
SamuelVch98 Sep 25, 2024
707ff55
Creating a new route for creating and modifying preferences (research…
SamuelVch98 Oct 3, 2024
bc00072
add rank in researcher's preferences
SamuelVch98 Oct 9, 2024
88ced26
addition of a button to display a history of a researcher's preferences
SamuelVch98 Oct 9, 2024
be49b75
small changes
SamuelVch98 Oct 15, 2024
29b43a3
small fix + rebase
SamuelVch98 Oct 16, 2024
73dd5c5
small fix + rebase
SamuelVch98 Oct 17, 2024
c181b3f
fix requested changes
SamuelVch98 Oct 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion course.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def add_course():

db.session.add(new_course)
db.session.commit()
return redirect(url_for("course.courses", current_year=year))
return redirect(url_for("course.courses", year=year))
except Exception as e:
db.session.rollback()
raise e
Expand Down
18 changes: 9 additions & 9 deletions course_preference.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@
course_preference_bp = Blueprint('course_preference', __name__)


def delete_old_preferences(researcher_id, course_ids, current_year):
def delete_old_preferences(researcher_id, current_year):
try:
db.session.query(PreferenceAssignment).filter(
PreferenceAssignment.researcher_id == researcher_id,
PreferenceAssignment.course_year == current_year,
PreferenceAssignment.course_id.in_(course_ids)
).delete()
db.session.commit()
except Exception as e:
Expand All @@ -34,29 +33,30 @@ def save_preference():

preferences = data['preferences']
SamuelVch98 marked this conversation as resolved.
Show resolved Hide resolved
if preferences is None:
return make_response("No preferences received", 500)
flash("No preferences received", "error")

user_id = session["user_id"]
researcher = Researcher.query.filter(Researcher.user_id == user_id).first()
if researcher is None:
return make_response("User is not a researcher", 500)
flash("Researcher not found", "error")

new_course_ids = {preference['course_id'] for preference in preferences}
delete_old_preferences(researcher.id, new_course_ids, current_year)
delete_old_preferences(researcher.id, current_year)

rank = 0
SamuelVch98 marked this conversation as resolved.
Show resolved Hide resolved
for preference in preferences:
SamuelVch98 marked this conversation as resolved.
Show resolved Hide resolved
try:
rank += 1
SamuelVch98 marked this conversation as resolved.
Show resolved Hide resolved
course_id = preference['course_id']
course_year = preference['course_year']
new_preference = PreferenceAssignment(course_id=course_id, course_year=course_year,
new_preference = PreferenceAssignment(rank=rank, course_id=course_id, course_year=course_year,
researcher_id=researcher.id)
db.session.add(new_preference)
db.session.commit()
except Exception as e:
db.session.rollback()
raise e

return redirect(url_for("user.user_profile", user_id=user_id, current_year=current_year))
return redirect(url_for("user.preferences", user_id=user_id, current_year=current_year))


@course_preference_bp.route('/delete_preference', methods=['GET'])
Expand All @@ -72,4 +72,4 @@ def delete_preference():
db.session.rollback()
raise e

return redirect(url_for("user.user_profile", user_id=session["user_id"], current_year=current_year))
return redirect(url_for("user.preferences", user_id=session["user_id"], current_year=current_year))
3 changes: 2 additions & 1 deletion db.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class Teacher(db.Model):
class PreferenceAssignment(db.Model):
__tablename__ = 'preference_assignment'
id = db.Column(db.Integer, primary_key=True)
rank = db.Column(db.Integer, nullable=False)
course_id = db.Column(db.Integer, nullable=False)
course_year = db.Column(db.Integer, nullable=False)
researcher_id = db.Column(db.Integer, db.ForeignKey('researcher.id'), nullable=False)
Expand All @@ -124,7 +125,7 @@ class PreferenceAssignment(db.Model):
)

course = db.relationship('Course', backref=db.backref('course_preference_assignment', lazy=True))
researcher = db.relationship('Researcher', backref=db.backref('researcher_preference_assignment', lazy=True))
researcher = db.relationship('Researcher', backref=db.backref('preferences', lazy=True))


class Configuration(db.Model):
Expand Down
32 changes: 12 additions & 20 deletions templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,15 @@
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link"
href="{{ url_for('user.user_profile', user_id=session.user_id, current_year=dynamic_year) }}">My
href="{{ url_for('user.user_profile', user_id=session.user_id) }}">My
Profile</a>
</li>
{% if is_researcher %}
<li>
<a class="nav-link"
href="{{ url_for('user.preferences', user_id=session.user_id, current_year=dynamic_year) }}">Preferences</a>
</li>
{% endif %}
<li>
<a class="nav-link"
href="{{ url_for('course.evaluations', user_id=session.user_id, current_year=dynamic_year) }}">Evaluations</a>
Expand Down Expand Up @@ -92,8 +98,8 @@
</ul>
</li>
<li>
<a class="nav-link"
href="{{ url_for('config.manage_years', configurations=configurations) }}">Configurations</a>
<a class="nav-link"
href="{{ url_for('config.manage_years', configurations=configurations) }}">Configurations</a>
SamuelVch98 marked this conversation as resolved.
Show resolved Hide resolved
</li>
{% endif %}
{% block additionalnav %}{% endblock %}
Expand All @@ -102,7 +108,8 @@
<div class="me-0">
<div class="navbar-text me-2">
<input type="text" class="form-control form-control-sm" id="dynamicYearInput"
value="{{ dynamic_year }} - {{ dynamic_year + 1 }}" style="width: 100px;" disabled>
value="{{ dynamic_year }} - {{ dynamic_year + 1 }}" style="width: 100px;"
disabled>
</div>
</div>

Expand All @@ -120,21 +127,6 @@
<div id="content">
{% block pagecontent %}{% endblock %}
</div>
{% block additionalfooter %}
<script>
$(document).ready(function () {
$('#confirmationModal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget);

var message = button.data('message');
var url = button.data('url');

var modal = $(this);
modal.find('.modal-body').text(message);
modal.find('#confirmationForm').attr('action', url);
});
});
</script>
{% endblock %}
{% block additionalfooter %}{% endblock %}
</body>
</html>
210 changes: 210 additions & 0 deletions templates/preferences.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
{% extends "layout.html" %}
{% block pagetitle %}Preferences{% endblock %}
{% block additionalpageheader %}
<style>
.move-icon {
cursor: move;
}

.delete-icon {
cursor: pointer;
color: red;
}
</style>
{% endblock %}

{% block pagecontent %}
{% include "toast.html" %}
<div class="container-fluid">
<form id="preferencesForm" method="post">
<br>
<h2>The preferences of {{ researcher.user.name }} {{ researcher.user.first_name }}</h2>
<table class="table table-hover" id="staticPreferencesTable">
<thead>
<tr>
<th>Code</th>
<th>Title</th>
<th>Semester</th>
{% if session.is_admin %}
<th>
<select class="form-select form-select-sm d-inline" id="yearSelect"
name="yearSelect">
{% for config in configurations %}
<option value="{{ config.year }}" {% if config.year == current_year %}
selected {% endif %}
data-redirect="{{ url_for('user.preferences', user_id=researcher.user.id,
current_year=config.year) }}">
{{ config.year }}-{{ config.year + 1 }}
</option>
{% endfor %}
</select>
</th>
{% endif %}
</tr>
</thead>
<tbody class="table-group-divider">
{% for preference in preferences %}
<tr>
<td>{{ preference.course.code }}</td>
<td>{{ preference.course.title }}</td>
<td>{{ preference.course.quadri }}</td>
{% if not session.is_admin %}
<td>
<a href="{{ url_for('course_preference.delete_preference', preference=preference.id) }}"
class="delete-icon">
<i class="fas fa-trash delete-icon"></i>
</a>
</td>
{% else %}
<td></td>
{% endif %}
</tr>
{% endfor %}
</tbody>
</table>

<table class="table table-hover" id="editablePreferencesTable" style="display: none;">
<thead>
<tr>
<th>Code</th>
</tr>
</thead>
<tbody class="table-group-divider">
{% for preference in preferences %}
<tr>
<td>
<select class="form-control course-select">
{% for course in courses %}
<option value="{{ course.id }}-{{ course.year }}"
{% if course.id == preference.course.id %}selected{% endif %}>
{{ course.code }} - {{ course.title }}
</option>
{% endfor %}
</select>
</td>
<td>
<i class="fas fa-trash delete-icon"></i>
<i class="fas fa-arrows-alt move-icon"></i>
</td>
</tr>
{% endfor %}

<!-- Hidden row template -->
<tr id="template-row" style="display: none;">
<td>
<select class="form-control course-select">
{% for course in courses %}
<option value="{{ course.id }}-{{ course.year }}">
{{ course.code }} - {{ course.title }}
</option>
{% endfor %}
</select>
</td>
<td>
<i class="fas fa-trash delete-icon"></i>
<i class="fas fa-arrows-alt move-icon"></i>
</td>
</tr>
</tbody>
</table>

{% if not session.is_admin %}
<button type="button" id="addPreferencesButton" class="btn btn-primary">Edit Preferences
</button>
{% endif %}
<button type="button" id="savePreferencesButton" class="btn btn-success"
style="display: none;">
Save Preferences
</button>
<button type="button" id="cancelEditButton" class="btn btn-secondary"
style="display: none;">
Cancel
</button>
<button type="button" id="addRowButton" class="btn btn-primary" style="display: none;">
Add Row
</button>
</form>
</div>
{% endblock %}

{% block additionalfooter %}
<script>
$(document).ready(function () {
// Handle year select change
handleYearSelectChange();

//Handle preferences
$('#addPreferencesButton').click(function () {
$('#staticPreferencesTable').hide();
$('#editablePreferencesTable').show();
$('#addPreferencesButton').hide();
$('#savePreferencesButton').show();
$('#cancelEditButton').show();
$('#addRowButton').show();
});

$('#cancelEditButton').click(function () {
$('#editablePreferencesTable').hide();
$('#staticPreferencesTable').show();
$('#addPreferencesButton').show();
$('#savePreferencesButton').hide();
$('#cancelEditButton').hide();
$('#addRowButton').hide();
});

$('#addRowButton').click(function () {
const newRow = $('#template-row').clone().removeAttr('id').removeAttr('style');
$('#editablePreferencesTable tbody').append(newRow);

$("#editablePreferencesTable tbody").sortable({
items: 'tr',
handle: '.move-icon',
axis: 'y',
cursor: 'move'
});
});

// Event listener pour le bouton de suppression
$(document).on('click', '.delete-icon', function () {
$(this).closest('tr').remove();
});

$('#savePreferencesButton').click(function () {
const preferences = [];
!$('#editablePreferencesTable tbody tr').not('#template-row').each(function () {
const courseValue = $(this).find('select.course-select').val();

if (courseValue) {
const [course_id, course_year] = courseValue.split("-");
preferences.push({course_id: course_id, course_year: course_year});
}
});

const data = {
preferences: preferences,
};

$.ajax({
type: 'POST',
url: "{{ url_for('course_preference.save_preference') }}",
contentType: 'application/json',
data: JSON.stringify(data),
success: function (response) {
alert('Preferences updated successfully.');
location.reload(); // Refresh the page to show the updated preferences
},
error: function (error) {
alert('Error updating the preferences.');
}
});
});

$("#editablePreferencesTable tbody").sortable({
items: 'tr',
handle: '.move-icon',
axis: 'y',
cursor: 'move'
});
});
</script>
{% endblock %}
Loading