Skip to content

Commit

Permalink
loader sign in test and create portfolio courseId
Browse files Browse the repository at this point in the history
  • Loading branch information
kevincharles committed Mar 15, 2023
1 parent e63643b commit 44181aa
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 20 deletions.
83 changes: 83 additions & 0 deletions src/Api/createPortfolioCourse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: access');
header('Access-Control-Allow-Methods: POST');
header('Access-Control-Allow-Credentials: true');
header('Content-Type: application/json');

include 'db_connection.php';

$jwtArray = include 'jwtArray.php';
$userId = $jwtArray['userId'];

$success = true;
$message = '';

if ($userId == '') {
$success = false;
$message = 'Error: You need to sign in';
}

$portfolioCourseId = '';
//Test if user already has a portfolio course UserId Course
if ($success) {
//Test if the user has a portfolio course
//If they don't then make one.
$sql = "
SELECT courseId
FROM course
WHERE portfolioCourseForUserId = '$userId'
";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
$portfolioCourseId = $row['courseId'];
} else {
//Make the portfolio course as the user doesn't have one
$portfolioCourseId = include 'randomId.php';
$defaultRoleId = include 'randomId.php';

$sql = "
INSERT INTO course
(courseId,label,isPublic,defaultRoleId,portfolioCourseForUserId)
VALUES
('$portfolioCourseId','Portfolio Course',1,'$defaultRoleId','$userId')
";
$conn->query($sql);

$sql = "
INSERT INTO course_role
(courseId,roleId,label,isIncludedInGradebook,canViewCourse,canViewUnassignedContent,
canViewContentSource,canEditContent,canPublishContent,canProctor,canViewAndModifyGrades,
canViewActivitySettings,canModifyActivitySettings,canModifyCourseSettings,canViewUsers,
canManageUsers,isAdmin,isOwner)
VALUES
('$portfolioCourseId','$defaultRoleId','Owner',0,1,1,1,1,1,1,1,1,1,1,1,1,1,1)
";
$conn->query($sql);

$sql = "
INSERT INTO course_user
(courseId,userId,dateEnrolled,roleId)
VALUES
('$portfolioCourseId','$userId',NOW(),'$defaultRoleId')
";
$conn->query($sql);
}
}

$response_arr = [
'success' => $success,
'message' => $message,
'portfolioCourseId' => $portfolioCourseId,
];

// set response code - 200 OK
http_response_code(200);

// make it json format
echo json_encode($response_arr);
$conn->close();

?>
49 changes: 49 additions & 0 deletions src/Api/getPorfolioCourseId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: access');
header('Access-Control-Allow-Methods: GET');
header('Access-Control-Allow-Credentials: true');
header('Content-Type: application/json');

include 'db_connection.php';

$jwtArray = include 'jwtArray.php';
$userId = $jwtArray['userId'];

$success = true;
$message = '';

if ($userId == '') {
$success = false;
$message = 'Error: You need to sign in';
}

$portfolioCourseId = '';

//Assume we are updating the activity and need the current settings
if ($success) {
$sql = "
SELECT courseId FROM course WHERE portfolioCourseForUserId = '$userId'
";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
$portfolioCourseId = $row['courseId'];
}
}

$response_arr = [
'success' => $success,
'message' => $message,
'portfolioCourseId' => $portfolioCourseId,
];

http_response_code(200);

// make it json format
echo json_encode($response_arr);

$conn->close();

?>
1 change: 0 additions & 1 deletion src/Api/getPortfolioActivityData.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
'isNew' => false,
];
} else {
$activityData['isNew'] = true;
//It's a new activity or a hack
//So test if it's a hack
$sql = "
Expand Down
10 changes: 9 additions & 1 deletion src/Tools/_framework/Paths/Portfolio.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@ export async function action() {
}
}

export async function loader(){
export async function loader({params}){

//If we didn't create the course yet for this user then create it
if (params.courseId == "not_created"){
const response = await fetch('/api/createPortfolioCourse.php');
const data = await response.json();
return redirect(`/portfolio/${data.portfolioCourseId}`)
}

const response = await fetch('/api/getPortfolio.php');
const data = await response.json();
return {publicActivities:data.publicActivities,
Expand Down
44 changes: 28 additions & 16 deletions src/Tools/_framework/Paths/SiteHeader.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
import React, { useRef, useState } from 'react';
import { Outlet, useNavigate } from 'react-router';
import { Outlet, useLoaderData, useNavigate } from 'react-router';
import { NavLink } from 'react-router-dom';
import styled from 'styled-components';
import Button from '../../../_reactComponents/PanelHeaderComponents/Button';
import { checkIfUserClearedOut } from '../../../_utils/applicationUtils';
import RouterLogo from '../RouterLogo';


export async function loader(){
//Check if signedIn
const profileInfo = await checkIfUserClearedOut();
let signedIn = true;
if (profileInfo.cookieRemoved){
signedIn = false;
}
let portfolioCourseId = null;
if (signedIn){
//Check on portfolio courseId
const response = await fetch('/api/getPorfolioCourseId.php');
const data = await response.json();
portfolioCourseId = data.portfolioCourseId;
if (data.portfolioCourseId == ""){
portfolioCourseId = "not_created";
}
}
return {signedIn,portfolioCourseId}
}

const SignInButtonContainer = styled.div`
margin: auto 10px auto 0px;
Expand Down Expand Up @@ -78,21 +97,14 @@ function MenuItem({to,children}){
}


export default function SiteHeader(props) {
export function SiteHeader(props) {
let navigate = useNavigate();
let checkingCookie = useRef(false);
const [signedIn, setSignedIn] = useState(null);

//Only ask once
if (!checkingCookie.current) {
checkingCookie.current = true;
checkIfUserClearedOut().then(({ cookieRemoved }) => {
setSignedIn(!cookieRemoved);
})
}
let data = useLoaderData();
console.log("site header data",data)


let signInButton = <Button dataTest="Nav to course" size="medium" onClick={() => navigate('/course')} value="Go to Course" />
if (!signedIn) {
if (!data.signedIn) {
signInButton = <Button dataTest="Nav to signin" onClick={() => navigate('/SignIn')} size="medium" value="Sign In" />
}

Expand All @@ -101,17 +113,17 @@ export default function SiteHeader(props) {
<HeaderContainer>
<Branding>
<RouterLogo /><p>Doenet</p>
</Branding>
</Branding>
<BarMenu>
<MenuItem to="/">Home</MenuItem>
<MenuItem to="community">Community</MenuItem>
{signedIn ? <MenuItem to="portfolio">Portfolio</MenuItem> : null}
{data.signedIn ? <MenuItem to={`portfolio/${data.portfolioCourseId}`}>Portfolio</MenuItem> : null}
</BarMenu>
<SignInButtonContainer>{signInButton}</SignInButtonContainer>
</HeaderContainer>

<ContentContainer>
<Outlet context={{signedIn}}/>
<Outlet context={{signedIn:data.signedIn}}/>
</ContentContainer>
</TopContainer>
</>)
Expand Down
5 changes: 3 additions & 2 deletions src/Tools/singlepage/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@ import ToolRoot from '../_framework/NewToolRoot';
import { MathJaxContext } from 'better-react-mathjax';
import { mathjaxConfig } from '../../Core/utils/math';
import DarkmodeController from '../_framework/DarkmodeController';
import SiteHeader from "../_framework/Paths/SiteHeader";
import Community from "../_framework/Paths/Community";
import {loader as siteLoader, SiteHeader} from "../_framework/Paths/SiteHeader";
import {loader as caroselLoader, Home} from "../_framework/Paths/Home";
import {loader as portfolioActivitySettingsLoader, action as portfolioActivitySettingsAction, PortfolioActivitySettings} from "../_framework/Paths/PortfolioActivitySettings";
import {loader as portfolioLoader, action as portfolioAction, Portfolio } from "../_framework/Paths/Portfolio";

const router = createBrowserRouter([
{
path: "/",
loader: siteLoader,
element: <SiteHeader />,
children: [
{
Expand All @@ -46,7 +47,7 @@ const router = createBrowserRouter([
element: <Community />,
},
{
path: "portfolio",
path: "portfolio/:courseId",
loader: portfolioLoader,
action: portfolioAction,
element: <Portfolio />,
Expand Down

0 comments on commit 44181aa

Please sign in to comment.