} // Get the date the grade was entered if (isset($grades[$this->member->get('id')]['assets'][$asset->id]) && !is_null($grades[$this->member->get('id')]['assets'][$asset->id]['date'])) { $date = Date::of($grades[$this->member->get('id')]['assets'][$asset->id]['date'])->format('r'); } else { $date = "N/A"; } if (isset($asset->unit_id) && $asset->unit_id) { $details['forms'][$unit->get('id')][] = array('title' => $title, 'score' => $score, 'date' => $date, 'url' => $url); } else { $details['aux'][] = array('title' => $asset->title, 'score' => $score, 'date' => $date); } $isValidForm = false; } if ($isValidForm) { $dep = \Components\Courses\Models\PdfFormDeployment::fromCrumb($crumb, $this->course->offering()->section()->get('id')); switch ($dep->getState()) { // Form isn't available yet case 'pending': $details['forms'][$unit->get('id')][] = array('title' => $title, 'score' => 'Not yet open', 'date' => 'N/A', 'url' => $url); break; // Form availability has expired // Form availability has expired case 'expired': // Get whether or not we should show scores at this point $results_closed = $dep->getResultsClosed(); // Form is still active and they are allowed to see their score if ($results_closed == 'score' || $results_closed == 'details') { $score = $grades[$this->member->get('id')]['assets'][$asset->id]['score']; } else { // Score has been withheld by form creator
/** * Submit and save a form response * * @return void */ public function submitTask() { if (!($crumb = Request::getVar('crumb', false))) { App::abort(422); } $attempt = Request::getInt('attempt', 1); $att = $attempt > 1 ? '&attempt=' . $attempt : ''; $dep = PdfFormDeployment::fromCrumb($crumb); $ended = false; // Make sure they're not trying to take the form too many times if ($attempt > $dep->getAllowedAttempts()) { App::abort(403, Lang::txt('COM_COURSES_WARNING_EXCEEDED_ATTEMPTS')); } // Check to see if the time limit has been reached if ($limit = $dep->getTimeLimit()) { $resp = $dep->getRespondent($this->member, $attempt); $now = strtotime(Date::of('now')); $start = strtotime($resp->getStartTime()); $end = strtotime($dep->getEndTime()); $dur = $limit * 60; if ($now > $start + $dur || $dep->getEndTime() && $end < $now) { $ended = true; } } list($complete, $answers) = $dep->getForm()->getQuestionAnswerMap($_POST, $ended); if ($complete) { $resp = $dep->getRespondent($this->member, $attempt); if (!$resp->getEndTime()) { $resp->saveAnswers($_POST)->markEnd(); } App::redirect(Route::url($this->base . '&task=form.complete&crumb=' . $crumb . $att, false)); return; } else { $this->setView('form', 'complete'); $this->_task = 'complete'; $this->view->incomplete = array_filter($answers, function ($ans) { return is_null($ans[0]); }); $this->completeTask(); } }
/** * Query to sync form scores with gradebook * * @param obj $course * @param array $member_id * @return void */ public function syncGrades($course, $member_id = null) { if (!is_null($member_id) && !empty($member_id)) { if (!is_array($member_id)) { $member_id = (array) $member_id; } } else { // Pull all section members $members = $course->offering()->section()->members(array('student' => 1)); $member_id = array(); // Get member id's for refresh filter foreach ($members as $member) { $member_id[] = $member->get('id'); } } if (count($member_id) == 0) { return; } // Get the assets $asset = new Asset($this->_db); $assets = $asset->find(array('w' => array('course_id' => $course->get('id'), 'section_id' => $course->offering()->section()->get('id'), 'offering_id' => $course->offering()->get('id'), 'asset_type' => 'form'))); // Query for existing data $query = "SELECT * FROM `#__courses_grade_book` WHERE `member_id` IN (" . implode(',', $member_id) . ") AND `scope` IN ('asset')"; $this->_db->setQuery($query); $results = $this->_db->loadObjectList(); $existing_grades = array(); foreach ($results as $r) { $existing_grades[$r->member_id . '.' . $r->scope_id] = array('id' => $r->id, 'score' => $r->score); } $inserts = array(); $updates = array(); $deletes = array(); if (count($assets) > 0) { foreach ($assets as $asset) { // Add null values for unpublished forms that may have already been taken if ($asset->state != 1) { $deletes[] = $asset->id; continue; } $crumb = false; // Check for result for given student on form $crumb = $asset->url; if (!$crumb || strlen($crumb) != 20 || $asset->state != 1) { // Break foreach, this is not a valid form! continue; } include_once dirname(__DIR__) . DS . 'models' . DS . 'formDeployment.php'; $dep = \Components\Courses\Models\PdfFormDeployment::fromCrumb($crumb, $course->offering()->section()->get('id')); $results = $dep->getResults('member_id', $member_id); switch ($dep->getState()) { // Form isn't available yet case 'pending': // Null value foreach ($member_id as $u) { $key = $u . '.' . $asset->id; if (!array_key_exists($key, $existing_grades)) { $inserts[] = "('{$u}', NULL, 'asset', '{$asset->id}', NULL)"; } else { if (!is_null($existing_grades[$key]['score'])) { $updates[] = "UPDATE `#__courses_grade_book` SET `score` = NULL WHERE `id` = '" . $existing_grades[$key]['id'] . "'"; } } } break; // Form availability has expired - students either get a 0, or their score (no nulls) // Form availability has expired - students either get a 0, or their score (no nulls) case 'expired': foreach ($member_id as $u) { $score = isset($results[$u]['score']) ? $results[$u]['score'] : '0.00'; $finished = isset($results[$u]['finished']) ? '\'' . $results[$u]['finished'] . '\'' : 'NULL'; $key = $u . '.' . $asset->id; if (!array_key_exists($key, $existing_grades)) { $inserts[] = "('{$u}', '{$score}', 'asset', '{$asset->id}', {$finished})"; } else { if ($existing_grades[$key]['score'] != $score) { $updates[] = "UPDATE `#__courses_grade_book` SET `score` = '{$score}', `score_recorded` = {$finished} WHERE `id` = '" . $existing_grades[$key]['id'] . "'"; } } } break; // Form is still active - students either get their score, or a null // Form is still active - students either get their score, or a null case 'active': foreach ($member_id as $u) { $resp = $dep->getRespondent($u); // Form is active and they have completed it! if ($resp->getEndTime() && $resp->getEndTime() != '') { $score = isset($results[$u]['score']) ? '\'' . $results[$u]['score'] . '\'' : 'NULL'; $key = $u . '.' . $asset->id; if (!array_key_exists($key, $existing_grades)) { $inserts[] = "('{$u}', {$score}, 'asset', '{$asset->id}', '" . $results[$u]['finished'] . "')"; } else { if ($existing_grades[$key]['score'] != $score) { $updates[] = "UPDATE `#__courses_grade_book` SET `score` = {$score}, `score_recorded` = '" . $results[$u]['finished'] . "' WHERE `id` = '" . $existing_grades[$key]['id'] . "'"; } } } else { $key = $u . '.' . $asset->id; if (!array_key_exists($key, $existing_grades)) { $inserts[] = "('{$u}', NULL, 'asset', '{$asset->id}', NULL)"; } else { if (!is_null($existing_grades[$key]['score'])) { $updates[] = "UPDATE `#__courses_grade_book` SET `score` = NULL, `score_recorded` = NULL WHERE `id` = '" . $existing_grades[$key]['id'] . "'"; } } } } break; } } // Build query and run if (count($inserts) > 0) { $query = "INSERT INTO `#__courses_grade_book` (`member_id`, `score`, `scope`, `scope_id`, `score_recorded`) VALUES\n"; $query .= implode(",\n", $inserts); $this->_db->setQuery($query); $this->_db->query(); } if (count($updates) > 0) { foreach ($updates as $update) { $query = $update; $this->_db->setQuery($query); $this->_db->query(); } } if (count($deletes) > 0) { $query = "DELETE FROM `#__courses_grade_book` WHERE `scope` = 'asset' AND `scope_id` IN (" . implode(',', $deletes) . ")"; $this->_db->setQuery($query); $this->_db->query(); } } }
/** * Copy existing form and data to new form * * @return mixed **/ public function copy() { $title = $this->getTitle(); $questions = $this->getPageLayout(); $oldId = $this->getId(); // Save new $this->id = null; $id = $this->getId(); $this->setTitle($title); $base = $this->base . $oldId; // Copy actual files if (is_dir($base)) { // Scan for versions $versions = array(); $dirs = scandir($base); foreach ($dirs as $dir) { if (is_numeric($dir) && is_dir($base . DS . $dir) && $dir != '.' && $dir != '..') { $versions[] = $dir; } } if (!empty($versions)) { $base .= DS . max($versions); } \Filesystem::copyDirectory($base, $this->base . $id); } // Copy questions $this->setPageLayout($questions); // Copy deployment (only most recent) try { $dep = PdfFormDeployment::latestFromFormId($oldId); $dep->setId(null); $dep->setFormId($id); $dep->genNewCrumb(); $dep->save(); // Return the deployment crumb return $dep->getCrumb(); } catch (\Hubzero\Error\Exception\RuntimeException $e) { // Just return the form id return $id; } }
echo Route::url('index.php?option=com_courses&controller=form'); ?> " method="get"> <input type="hidden" name="task" value="layout" /> <input type="hidden" name="formId" value="<?php echo $form['id']; ?> " /> <button type="submit"><?php echo Lang::txt('COM_COURSES_EDIT'); ?> </button> </form> <br /> <?php if ($deps = \Components\Courses\Models\PdfFormDeployment::forForm($form['id'])) { ?> <table class="tablesorter nested"> <thead> <tr> <th><?php echo Lang::txt('COM_COURSES_HEADER_DEPLOYMENT'); ?> </th> <th><?php echo Lang::txt('COM_COURSES_HEADER_USER'); ?> </th> <th><?php echo Lang::txt('COM_COURSES_HEADER_START_DATE'); ?>