Skip to content

Commit

Permalink
Continuing implementation of UC26, UC27
Browse files Browse the repository at this point in the history
Current implementation draft has questions successfully being auto-generated/displayed to the user, but question navigation and saving is currently buggy. Will continue work later. ~ 8 hours of work (including previous commit)
  • Loading branch information
Jacob committed Mar 21, 2018
1 parent 1a2a094 commit 8a5897f
Show file tree
Hide file tree
Showing 17 changed files with 390 additions and 52 deletions.
5 changes: 3 additions & 2 deletions classes/Authenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,17 @@ public static function registerRepresentative($fName, $lName, $email, $altEmail,
* @param $postalCode
* @param $phone
* @param $gradYear
* @param $women
* @param $password
* @param $confirmPassword
* @return bool
* @throws Exception
*/
public static function registerStudent($fName, $lName, $email, $altEmail, $address, $city, $province, $postalCode, $phone, $gradYear, $password, $confirmPassword)
public static function registerStudent($fName, $lName, $email, $altEmail, $address, $city, $province, $postalCode, $phone, $gradYear, $women, $password, $confirmPassword)
{
if ($password === $confirmPassword) {
//TODO: upon implementing email verification, the "true" below should be changed to false
$student = new Student($fName, $lName, $email, $altEmail, $address, $city, new Province($province, Province::MODE_ISO), $postalCode, $phone, $gradYear, $password, false);
$student = new Student($fName, $lName, $email, $altEmail, $address, $city, new Province($province, Province::MODE_ISO), $postalCode, $phone, $gradYear, $password, false, $women);
$student->addPermission(new Permission(Permission::PERMISSION_STUDENT));
if (self::userExists($student)) {
return false;
Expand Down
65 changes: 59 additions & 6 deletions classes/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,7 @@ private function processPOST(): bool
* postalCode : string <=10 characters in length
* phoneNumber : int <=15 digits in length
* gradYear : int 4 digits in length
* women : int (0 or 1)
* password : string
* confirmPassword : string
*/
Expand All @@ -616,6 +617,7 @@ private function processPOST(): bool
((int)$this->scrubbed["postalCode"]),
((int)$this->scrubbed["phoneNumber"]),
((int)$this->scrubbed["gradYear"]),
(((int)$this->scrubbed["women"]) === 1 ? true : false),
$this->scrubbed["password"],
$this->scrubbed["confirmPassword"]
];
Expand All @@ -638,20 +640,20 @@ private function processPOST(): bool
* //TODO: fill in other required fields
*/
case "registerRepresentative":
//TODO: complete representative registration handling
$args = [
//TODO: complete representative registration handling
$args = [

];
];
// call_user_func_array("Authenticator::registerRepresentative", $args);
break;
break;
/**
* Required POST variables for this case:
* requestType : "activateUser"
* email : string
* password : string
* tokenID : string
*/
case "activateUser":
case "activateUser":
try {
$args = [
$this->scrubbed["email"],
Expand All @@ -667,7 +669,7 @@ private function processPOST(): bool
} catch (Error | Exception $e) {
$_SESSION["localErrors"][] = $e;
}
break;
break;
/**
* Required POST variables for this case:
* requestType : "login"
Expand Down Expand Up @@ -893,6 +895,7 @@ private function processPOST(): bool
}
/**
* Required POST variables for this case:
* requestType : "resetPassword"
* password : string
* confirmpassword : string
*/
Expand All @@ -914,6 +917,56 @@ private function processPOST(): bool
}
break;
}
/**
* Required POST variables for this case:
* requestType : "nextQuestion"
* question : int
* answer : string
* importance : int
*/
case "nextQuestion":
try {
$success = QuestionHandler::saveAnswer(
self::getLoggedInUser(),
new QuestionMC($this->scrubbed["question"]),
$_POST["answer"],
((int)$this->scrubbed["importance"])
);
if ($success) {
$_SESSION["localNotifications"][] = "Question response successfully saved";
$_SESSION["nextQuestion"] = true;
} else {
$_SESSION["localWarnings"][] = "Warning: unable to save question data";
}
} catch (Exception | Error $e) {
$_SESSION["localErrors"][] = $e;
}
break;
/**
* Required POST variables for this case:
* requestType : "prevQuestion"
* question : int
* answer : string
* importance : int
*/
case "prevQuestion":
try {
$success = QuestionHandler::saveAnswer(
self::getLoggedInUser(),
new QuestionMC($this->scrubbed["question"]),
$_POST["answer"],
((int)$this->scrubbed["importance"])
);
if ($success) {
$_SESSION["localNotifications"][] = "Question response successfully saved";
$_SESSION["prevQuestion"] = true;
} else {
$_SESSION["localWarnings"][] = "Warning: unable to save question data";
}
} catch (Exception | Error $e) {
$_SESSION["localErrors"][] = $e;
}
break;
}
return true; //temporary return value
}
Expand Down
122 changes: 105 additions & 17 deletions classes/QuestionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ public static function getCurrentQuestion(Student $student)
{
$aq = $student->getAnsweredQuestions();
if (!is_null($aq) and count($aq) > 0) {
$answered = array_reverse(array_map(function ($o) {
$o->getQuestion();
}, $aq));
foreach ($answered as $item) {
$answered = [];
foreach ($aq as $a) {
$answered[] = $a->getQuestion();
}
foreach (array_reverse($answered) as $item) {
if (self::checkQuestionDependency($student, $item)) {
return $item;
}
Expand Down Expand Up @@ -62,6 +63,35 @@ public static function getNthQuestion(Student $student, int $n)
}
}

/**
* Returns the index for the specified question, considering which questions have been answered already by the given
* student, and which have yet to be answered. Returns -1 if the question cannot be found.
*
* @param Student $student
* @param Question $question
* @return int
*/
public static function getQuestionIndex(Student $student, Question $question): int
{
$aq = $student->getAnsweredQuestions();
$answered = [];
if (!is_null($aq) and count($aq) > 0) {
foreach ($aq as $a) {
$answered[] = $a->getQuestion();
}
}

$questions = array_merge($answered, self::getUnansweredQuestions($student));
$IDs = [];
/**
* @var $questions Question[]
*/
foreach ($questions as $q) {
$IDs[] = $q->getPkID();
}
return array_search($question->getPkID(), $IDs, true);
}

/**
* Returns the second most recently answered question from a user which could also be answered again based on
* question dependencies.
Expand All @@ -73,10 +103,11 @@ public static function getPreviousQuestion(Student $student)
{
$aq = $student->getAnsweredQuestions();
if (!is_null($aq) and count($aq) > 0) {
$answered = array_reverse(array_map(function ($o) {
$o->getQuestion();
}, $aq));
foreach (array_slice($answered,1) as $item) {
$answered = [];
foreach ($aq as $a) {
$answered[] = $a->getQuestion();
}
foreach (array_reverse(array_slice($answered, 1)) as $item) {
if (self::checkQuestionDependency($student, $item)) {
return $item;
}
Expand All @@ -86,7 +117,31 @@ public static function getPreviousQuestion(Student $student)
}

/**
* Returns an array of questions that have yet to be answered by the
* Returns the QuestionAnswered object for the student if the student has responded to the given question.
*
* @param Student $student
* @param Question $question
* @return QuestionAnswered
*/
public static function getStudentAnswer(Student $student, Question $question)
{
$aq = $student->getAnsweredQuestions();
if (!is_null($aq) and count($aq) > 0) {
$answered = [];
foreach ($aq as $a) {
$answered[] = $a->getQuestion();
}
for ($i = 0; $i < count($answered); $i++) {
if ($answered[$i] === $question) {
return $aq[$i];
}
}
}
return null;
}

/**
* Returns an array of questions that have yet to be answered by the student
*
* @param Student $student
* @return Question[]
Expand All @@ -103,7 +158,7 @@ public static function getUnansweredQuestions(Student $student)
if ($questionIDs) {
try {
foreach ($questionIDs as $questionID) {
$output[] = new QuestionMC($questionID);
$output[] = new QuestionMC($questionID["pkquestionid"]);
}
} catch (Exception | Error $e) {
return [];
Expand All @@ -112,6 +167,24 @@ public static function getUnansweredQuestions(Student $student)
return $output;
}

/**
* Returns an array of questions that have yet to be answered by the student, taking account of depdendencies.
* For example, if a question's prerequisite's haven't been met, then it won't show up in the output list.
*
* @param Student $student
* @return Question[]
*/
public static function getAvailableUnansweredQuestions(Student $student)
{
$output = self::getUnansweredQuestions($student);
foreach ($output as &$question) {
if (!self::checkQuestionDependency($student, $question)) {
unset($question);
}
}
return array_values($output);
}

/**
* Checks to see if the given question has been answered by the user
*
Expand All @@ -123,10 +196,11 @@ public static function isQuestionAnswered(Student $student, Question $question):
{
$aq = $student->getAnsweredQuestions();
if (!is_null($aq) and count($aq) > 0) {
$answered = array_reverse(array_map(function ($o) {
$o->getQuestion();
}, $aq));
return in_array($question, $answered, true);
$answered = [];
foreach ($aq as $a) {
$answered[] = $a->getQuestion()->getPkID();
}
return in_array($question->getPkID(), $answered, true);
}
return false;
}
Expand All @@ -136,11 +210,25 @@ public static function isQuestionAnswered(Student $student, Question $question):
* @param Question $question
* @param $answer
* @param int $importance
* @return bool
*/
public static function saveAnswer(Student $student, Question $question, $answer, int $importance): void
public static function saveAnswer(Student $student, Question $question, $answer, int $importance): bool
{
$student->addAnsweredQuestion(new QuestionAnswered($student, $question, $answer, $importance));
$student->updateToDatabase();
if (self::isQuestionAnswered(Controller::getLoggedInUser(), $question)) {
$answered = $student->getAnsweredQuestions();
$student->removeAllAnsweredQuestions();
foreach ($answered as $item) {
if ($item->getQuestion()->getPkID() === $question->getPkID()) {
$item->removeFromDatabase();
$student->addAnsweredQuestion(new QuestionAnswered($question, $student, $answer, $importance));
} else {
$student->addAnsweredQuestion($item);
}
}
} else {
$student->addAnsweredQuestion(new QuestionAnswered($question, $student, $answer, $importance));
}
return $student->updateToDatabase();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion classes/entities/Dependency.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Dependency
public function __construct(Question $question, int $parentID)
{
$dbc = new DatabaseConnection();
$params = ["i", $question->getPkID(), $parentID];
$params = ["ii", $question->getPkID(), $parentID];
$answer = $dbc->query("select", "SELECT jsanswer FROM tblquestionorder WHERE fkquestionid = ? AND fkqparent = ?", $params);
if ($answer) {
$result = [
Expand Down
4 changes: 2 additions & 2 deletions classes/entities/QuestionAnswered.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class QuestionAnswered extends DataBasedEntity
* @param Student $student
* @throws Exception
*/
public function __construct2(Question $question, Student &$student)
public function __construct2(Question $question, Student $student)
{
$dbc = new DatabaseConnection();
$params = ["ii", $student->getPkID(), $question->getPkID()];
Expand Down Expand Up @@ -60,7 +60,7 @@ public function __construct2(Question $question, Student &$student)
* @param int $importance
* @throws Exception
*/
public function __construct4(Question $question, Student &$student, $answer, int $importance)
public function __construct4(Question $question, Student $student, $answer, int $importance)
{
$result = [
$this->setQuestion($question),
Expand Down
12 changes: 10 additions & 2 deletions classes/entities/Student.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,18 @@ class Student extends User
* @param int $gradYear
* @param string $password
* @param bool $active
* @param bool $women
* @throws Exception
*/
public function __construct12(string $firstName, string $lastName, string $email, string $altEmail, string $streetAddress, string $city, Province $province, int $postalCode, int $phone, int $gradYear, string $password, bool $active)
public function __construct13(string $firstName, string $lastName, string $email, string $altEmail, string $streetAddress, string $city, Province $province, int $postalCode, int $phone, int $gradYear, string $password, bool $active, bool $women)
{
parent::__construct12($firstName, $lastName, $email, $altEmail, $streetAddress, $city, $province, $postalCode, $phone, $gradYear, $password, $active);
$result = [
$this->setGender($women)
];
if (in_array(false, $result, true)) {
throw new Exception("Student->__construct12($firstName, $lastName, $email, $altEmail, $streetAddress, $city, ".$province->getISO().", $postalCode, $phone, $gradYear, $password, $active, $women) - Unable to construct User object; variable assignment failure - (" . implode(" ", array_keys($result, false, true)) . ")");
}
}

/**
Expand Down Expand Up @@ -125,7 +132,8 @@ public function __construct2($identifier, int $mode = self::MODE_UserID)
$this->setDesiredMajor(new Major($student["fkmajorid"])),
$this->setGPA($student["ngpa"]),
$this->setHouseholdIncome($student["nhouseincome"]),
$this->setSAT($student["nsat"])
$this->setSAT($student["nsat"]),
$this->setGender($student["isgender"])
];

$this->removeAllPreferredMajors();
Expand Down
7 changes: 1 addition & 6 deletions pages/pageassembly/profilenav/profilenav.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@
<a href="profile.php">Contact Information</a>
</li>
<?php
try {
$isStudent = $controller::getLoggedInUser()->hasPermission(new Permission(Permission::PERMISSION_STUDENT));
} catch (Exception $e) {
$isStudent = false;
}
if ($isStudent) {
if ($controller->userHasAccess([Permission::PERMISSION_STUDENT])) {
?>
<li>
<a href="eduprofile.php">Education</a>
Expand Down
2 changes: 1 addition & 1 deletion pages/register/css/register.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pages/register/css/register.min.css.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8a5897f

Please sign in to comment.