feat(profile,publications): integrate working Google Scholar import flow and unified faculty profile system

This commit is contained in:
Harshil Vora
2025-10-18 02:53:32 -07:00
parent 8fe0ef8112
commit ba28588e38
19 changed files with 915 additions and 139 deletions

View File

@@ -6,9 +6,19 @@ use Illuminate\Http\Request;
use Yajra\DataTables\Facades\DataTables;
use App\Models\Publication;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use App\Services\ScholarService;
class PublicationsController extends Controller
{
public function index()
{
$user = auth()->user();
$scholarUrl = $user->scholar_url;
return view('pages.publications.index', compact('scholarUrl'));
}
public function edit($id)
{
$publication = Publication::findOrFail($id);
@@ -23,7 +33,6 @@ class PublicationsController extends Controller
{
$publication = Publication::findOrFail($id);
// Validate the request data
$validated = $request->validate([
'first_author_name' => 'required|string',
'co_authors' => 'nullable|string',
@@ -43,34 +52,27 @@ class PublicationsController extends Controller
'paper_file' => 'nullable|mimes:jpg,jpeg,png,pdf,doc,docx,zip',
]);
// Combine start date and time
$startDateTime = date('Y-m-d H:i:s', strtotime("{$validated['start_date']} {$validated['start_time']}"));
$endDateTime = date('Y-m-d H:i:s', strtotime("{$validated['end_date']} {$validated['end_time']}"));
// Handle the file upload if a new file is provided
if ($request->hasFile('paper_file')) {
// Delete old file if exists
if ($publication->paper_file && Storage::disk('public')->exists($publication->paper_file)) {
Storage::disk('public')->delete($publication->paper_file);
}
// Extract year from start_date
$year = date('Y', strtotime($validated['start_date']));
$username = $publication->user->name;
$originalName = $request->file('paper_file')->getClientOriginalName();
$fileName = $username . '_' . $originalName;
// Create path structure: year/faculty_name/Publications
$folderPath = 'proofs/' . $year . '/' . $username . '/Publications';
// Store file in the specified path
$paperFilePath = $request->file('paper_file')->storeAs($folderPath, $fileName, 'public');
$publication->paper_file = $paperFilePath;
}
// Update other fields
$publication->first_author_name = $validated['first_author_name'];
$publication->co_authors = $validated['co_authors'];
$publication->start_date = $startDateTime;
@@ -96,7 +98,6 @@ class PublicationsController extends Controller
return redirect()->route('coordinator.PublicationsResponses')
->with('status', 'Publication updated successfully');
} else {
// For regular users
return redirect()->route('faculty.PublicationsResponses')
->with('status', 'Publication updated successfully');
}
@@ -106,7 +107,6 @@ class PublicationsController extends Controller
{
$publication = Publication::findOrFail($id);
// Delete the file if it exists
if ($publication->paper_file && Storage::disk('public')->exists($publication->paper_file)) {
Storage::disk('public')->delete($publication->paper_file);
}
@@ -122,18 +122,14 @@ class PublicationsController extends Controller
$isAdmin = $user->role->name === 'Admin';
$isCoordinator = $user->role->name === 'Coordinator';
// Query based on role
if ($isAdmin) {
// Admin sees all records
$publications = Publication::with('user', 'department');
} elseif ($isCoordinator) {
// Coordinator sees only their department's records
$publications = Publication::with('user', 'department')
->whereHas('user', function ($query) use ($user) {
$query->where('department_id', $user->department_id);
});
} else {
// Regular users see only their own records
$publications = Publication::with('user', 'department')
->where('faculty_id', $user->id);
}
@@ -166,16 +162,13 @@ class PublicationsController extends Controller
->addColumn('action', function ($publication) {
$actions = [];
// View paper_file button for everyone
if ($publication->paper_file) {
$actions[] = '<a href="' . asset('storage/' . $publication->paper_file) . '" target="_blank" class="btn btn-sm btn-primary mr-1">View Paper</a>';
} else {
$actions[] = 'No Paper';
}
// Edit button with role-appropriate route
$userRole = auth()->user()->role->name;
// Determine the appropriate route based on user role
if ($userRole === 'Admin') {
$editRoute = route('admin.Publications.edit', $publication->id);
} elseif ($userRole === 'Coordinator') {
@@ -194,4 +187,135 @@ class PublicationsController extends Controller
->rawColumns(['action'])
->make(true);
}
}
/**
* Fetch all publications from user's linked Google Scholar profile.
*/
public function fetchScholar(Request $request)
{
try {
$user = auth()->user();
\Log::info('📡 FetchScholar Triggered', ['scholar_url' => $user->scholar_url]);
if (!$user || !$user->scholar_url) {
return response()->json([
'success' => false,
'message' => 'No Google Scholar URL found for the logged-in user.'
], 400);
}
$scholarService = new \App\Services\ScholarService();
$publications = $scholarService->fetchPublications($user->scholar_url);
if (empty($publications)) {
return response()->json([
'success' => false,
'message' => 'No publications found from the provided Scholar profile.'
]);
}
return response()->json([
'success' => true,
'count' => count($publications),
'data' => $publications,
]);
} catch (\Throwable $e) {
return response()->json([
'success' => false,
'message' => 'Error fetching publications: '.$e->getMessage(),
], 500);
}
}
/**
* Import selected publications (skipping duplicates).
*/
public function importScholar(Request $request)
{
try {
$user = auth()->user();
$facultyId = $user->id;
$validated = $request->validate([
'publications' => 'required|array',
'publications.*.title' => 'required|string',
'publications.*.authors' => 'nullable|string',
'publications.*.journal' => 'nullable|string',
'publications.*.year' => 'nullable|string',
'publications.*.link' => 'nullable|url',
]);
$rows = $validated['publications'];
$imported = 0;
$skipped = 0;
foreach ($rows as $pub) {
$exists = \App\Models\Publication::where('faculty_id', $facultyId)
->whereRaw('LOWER(title) = ?', [strtolower($pub['title'])])
->exists();
if ($exists) {
$skipped++;
continue;
}
// 🔹 Derive year safely (default to current year if blank)
$year = (int)($pub['year'] ?? date('Y'));
$year = ($year < 1900 || $year > date('Y') + 1) ? date('Y') : $year;
// 🔹 Set start/end to that year's first/last day
$startDate = "{$year}-01-01";
$endDate = "{$year}-12-31";
// 🔍 Smart Tag Detection
$journalLower = strtolower($pub['journal'] ?? '');
// Detect peer-reviewed journals
$isPeerReviewed = (
str_contains($journalLower, 'ieee') ||
str_contains($journalLower, 'elsevier') ||
str_contains($journalLower, 'springer') ||
str_contains($journalLower, 'scopus') ||
str_contains($journalLower, 'wiley') ||
str_contains($journalLower, 'nature') ||
str_contains($journalLower, 'taylor & francis')
) ? 'yes' : 'no';
// Detect Scopus / SCI tags
$isScopus = str_contains($journalLower, 'scopus') ? 'Yes' : 'No';
$isSci = str_contains($journalLower, 'sci') ? 'Yes' : 'No';
\App\Models\Publication::create([
'faculty_id' => $facultyId,
'department_id' => $user->department_id,
'affiliation' => $pub['journal'] ?? '-',
'organizing_institute' => null,
'venue_address' => null,
'title' => $pub['title'],
'first_author_name' => $user->name,
'co_authors' => $pub['authors'] ?? '-',
'journal_name' => $pub['journal'] ?? '-',
'start_date' => $startDate,
'end_date' => $endDate,
'num_days' => 365,
'activity_type' => 'Google Scholar',
'is_peer_reviewed' => $isPeerReviewed,
'is_scopus_indexed' => $isScopus,
'is_sci_indexed' => $isSci,
'external_link' => $pub['link'] ?? null,
]);
$imported++;
}
return response()->json([
'message' => "Imported $imported publications, skipped $skipped duplicates."
]);
} catch (\Throwable $e) {
return response()->json(['error' => $e->getMessage()], 500);
}
}
}