Skip to content

Commit

Permalink
Redmine 687: Add option for teachers to enrol users while correcting …
Browse files Browse the repository at this point in the history
…answer forms.
  • Loading branch information
juzi committed Jun 11, 2013
1 parent aec22f7 commit 7c8f4ce
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 10 deletions.
144 changes: 134 additions & 10 deletions correct.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* @subpackage offlinequiz
* @author Juergen Zimmer
* @copyright 2012 The University of Vienna
* @since Moodle 2.2+
* @since Moodle 2.4
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
**/
Expand All @@ -35,6 +35,7 @@
$scannedpageid = optional_param('pageid', 0, PARAM_INT);
$overwrite = optional_param('overwrite', 0, PARAM_INT);
$action = optional_param('action', 'load', PARAM_TEXT);
$userchanged = optional_param('userchanged', 0, PARAM_INT);

if (!$scannedpage = $DB->get_record('offlinequiz_scanned_pages', array('id' => $scannedpageid))) {
print_error('noscannedpage', 'offlinequiz', $CFG->wwwroot . '/course/view.php?id=' . $COURSE->id, $scannedpageid);
Expand Down Expand Up @@ -151,7 +152,9 @@

// Maybe old errors have been fixed.
$scannedpage->status = 'ok';
$DB->set_field('offlinequiz_scanned_pages', 'status', 'ok', array('id' => $scannedpage->id));
$scannedpage->error = '';
$DB->set_field('offlinequiz_scanned_pages', 'error', '', array('id' => $scannedpage->id));

$groupnumber = required_param('groupnumber', PARAM_TEXT);
$groupnumber = intval($groupnumber);
Expand All @@ -178,7 +181,15 @@
// Now we check the scanned page with potentially updated information.
// $scannedpage = offlinequiz_check_for_changed_groupnumber($offlinequiz, $scanner, $scannedpage, $coursecontext, $questionsperpage, $offlinequizconfig);

$oldresultid = $scannedpage->resultid;
$scannedpage = offlinequiz_check_for_changed_user($offlinequiz, $scanner, $scannedpage, $coursecontext, $questionsperpage, $offlinequizconfig);

if ($oldresultid != $scannedpage->resultid) {
// Already process the answers but don't submit them.
$scannedpage = offlinequiz_process_scanned_page($offlinequiz, $scanner, $scannedpage, $USER->id, $questionsperpage, $coursecontext, false);
$userchanged = 1;
}

if (!$overwrite) {
$scannedpage = offlinequiz_check_scanned_page($offlinequiz, $scanner, $scannedpage, $USER->id, $coursecontext);

Expand All @@ -191,7 +202,6 @@
}
}


$userkey = $scannedpage->userkey;
$usernumber = substr($userkey, strlen($offlinequizconfig->ID_prefix), $offlinequizconfig->ID_digits);
$groupnumber = intval($scannedpage->groupnumber);
Expand Down Expand Up @@ -228,7 +238,7 @@
$scannedpage->userkey = $userkey;
}

if ($overwrite) {
if ($overwrite && !$userchanged) {
// We want to overwrite an old result, so we have to create a new one.
// Don't delete the choices stored in the DB.
// $DB->delete_records('offlinequiz_choices', array('scannedpageid' => $scannedpage->id));
Expand All @@ -244,7 +254,55 @@
$scannedpage = offlinequiz_check_scanned_page($offlinequiz, $scanner, $scannedpage, $USER->id, $coursecontext);

if ($scannedpage->resultid) {
$DB->delete_records('offlinequiz_results', array('id' => $oldresultid));

// Get all other pages of the old result and set their resultid number to the new one.
$sql = "SELECT *
FROM {offlinequiz_scanned_pages}
WHERE offlinequizid = :offlinequizid
AND resultid = :resultid
AND status = 'submitted'
AND id <> :currentpageid";
$params = array('offlinequizid' => $scannedpage->offlinequizid, 'resultid' => $oldresultid, 'currentpageid' => $scannedpage->id);

if ($oldpages = $DB->get_records_sql($sql, $params)) {

// Load the new result and the quba slots.
$newresult = $DB->get_record('offlinequiz_results', array('id' => $scannedpage->resultid));
$quba = question_engine::load_questions_usage_by_activity($newresult->usageid);
$pageslots = $quba->get_slots();

foreach ($oldpages as $page) {
$page->resultid = $scannedpage->resultid;
$DB->set_field('offlinequiz_scanned_pages', 'resultid', $scannedpage->resultid, array('id' => $page->id));

echo get_string('resubmitting', 'offlinequiz') . ' ' . $page->pagenumber . ' ... ';

// Load the choices made before from the database. This might be empty.
$pagechoices = $DB->get_records('offlinequiz_choices', array('scannedpageid' => $page->id), 'slotnumber, choicenumber');

// Choicesdata contains the choices data from the DB indexed by slotnumber and choicenumber.
$pagechoicesdata = array();
if (!empty($pagechoices)) {
foreach ($pagechoices as $pagechoice) {
if (!isset($pagechoicesdata[$pagechoice->slotnumber]) || !is_array($pagechoicesdata[$pagechoice->slotnumber])) {
$pagechoicesdata[$pagechoice->slotnumber] = array();
}
$pagechoicesdata[$pagechoice->slotnumber][$pagechoice->choicenumber] = $pagechoice;
}
}
// Determine the slice of slots we are interested in.
// We start at the top of the page (e.g. 0, 96, etc).
$pagestartindex = min(($page->pagenumber - 1) * $questionsperpage, count($pageslots));
// We end on the bottom of the page or when the questions are gone (e.g., 95, 105).
$pageendindex = min($page->pagenumber * $questionsperpage, count($pageslots));
// Submit the choices of the other pages to the new result.
$page = offlinequiz_submit_scanned_page($offlinequiz, $page, $pagechoicesdata, $pagestartindex, $pageendindex);
echo get_string('done', 'offlinequiz') . '<br/>';
}
}

// Finally, delete the old result.
$DB->delete_records('offlinequiz_results', array('id' => $oldresultid));
} else {
$scannedpage->resultid = $oldresultid;
$DB->update_record('offlinequiz_scanned_pages', $scannedpage);
Expand Down Expand Up @@ -393,17 +451,66 @@
$pagenumber = intval($scannedpage->pagenumber);

$DB->update_record('offlinequiz_scanned_pages', $scannedpage);
}


} else if ($action == 'enrol' && $offlinequizconfig->oneclickenrol) {
// =============================================
// Action enrol.
// =============================================
if (!confirm_sesskey()) {
print_error('invalidsesskey');
echo "<input class=\"imagebutton\" type=\"submit\" value=\"" . get_string('cancel')."\" name=\"submitbutton4\"
onClick=\"self.close(); return false;\"><br />";
die;
}

require_once($CFG->libdir . '/enrollib.php');
require_once($CFG->dirroot.'/enrol/manual/locallib.php');

// Check that the user has the permission to manual enrol.
require_capability('enrol/manual:enrol', $coursecontext);

$userid = $DB->get_field('user', 'id', array($offlinequizconfig->ID_field => $scannedpage->userkey), MUST_EXIST);

// Get the manual enrolment plugin
$enrol = enrol_get_plugin('manual');
if (empty($enrol)) {
throw new moodle_exception('manualpluginnotinstalled', 'enrol_manual');
}

if (!is_enrolled($coursecontext, $userid)) {
// Now we need the correct instance of the manual enrolment plugin.
if (!$instance = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual'), '*', IGNORE_MISSING)) {
if ($instanceid = $enrol->add_default_instance($course)) {
$instance = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'manual'), '*', MUST_EXIST);
}
}

if ($instance != false) {
$enrol->enrol_user($instance, $userid, $offlinequizconfig->oneclickrole);
}
}

$scannedpage->status = 'ok';
$scannedpage->error = '';

// Now check the scanned page again. The user should be enrolled now.
$scannedpage = offlinequiz_check_scanned_page($offlinequiz, $scanner, $scannedpage, $USER->id, $coursecontext);

$userkey = $scannedpage->userkey;
$usernumber = substr($userkey, strlen($offlinequizconfig->ID_prefix), $offlinequizconfig->ID_digits);
$groupnumber = intval($scannedpage->groupnumber);
$pagenumber = intval($scannedpage->pagenumber);
}

// If we correct an OK page or a suspended page we can first process it and store the choices in the database.
if ($action == 'load' ) {
$oldchoices = $DB->count_records('offlinequiz_choices', array('scannedpageid' => $scannedpage->id));
}

// If we have an OK page and the action was checkuser, setpage, or rotate we should process the page.
// If we have an OK page and the action was checkuser, setpage, etc. we should process the page.
if (($scannedpage->status == 'ok' || $scannedpage->status == 'suspended') && ($action == 'readjust' ||
$action == 'checkuser' || $action == 'setpage' || $action == 'rotate' || ($action == 'load' && !$oldchoices))) {
$action == 'checkuser' || $action == 'enrol' || $action == 'setpage' || $action == 'rotate' || ($action == 'load' && !$oldchoices))) {
// Process the scanned page and write the answers in the offlinequiz_choices table.
$scannedpage = offlinequiz_process_scanned_page($offlinequiz, $scanner, $scannedpage, $USER->id, $questionsperpage, $coursecontext);
}
Expand Down Expand Up @@ -705,6 +812,11 @@ function submitCheckuser() {
document.forms.cform.submit();
}
function submitEnrol() {
document.forms.cform.elements['action'].value='enrol'
document.forms.cform.submit();
}
function submitRotated() {
document.forms.cform.elements['action'].value='rotate'
document.forms.cform.submit();
Expand Down Expand Up @@ -766,17 +878,25 @@ function submitRotated() {
$scannedpage->error == 'usernotincourse' ||
$scannedpage->error == 'resultexists' ||
$scannedpage->error == 'doublepage' ||
$scannedpage->error == 'differentresultexists' ||
$scannedpage->error == 'differentresultexists' ||
$scannedpage->error == 'grouperror') {

echo "<input class=\"imagebutton\" type=\"submit\" value=\"".get_string('checkuserid', 'offlinequiz').
echo "<input class=\"imagebutton\" type=\"submit\" value=\"" . get_string('checkuserid', 'offlinequiz') .
"\" name=\"submitbutton4\" onClick=\"submitCheckuser(); return false;\"><br />";

if ($scannedpage->error == 'usernotincourse' && $offlinequizconfig->oneclickenrol) {
echo "<input class=\"imagebutton\" type=\"submit\" value=\"" . get_string('enroluser', 'offlinequiz') .
"\" name=\"submitbutton6\" onClick=\"submitEnrol(); return false;\"><br />";

}

// Show enabled save button if the error state allows it.
if ($scannedpage->error != 'doublepage' &&
$scannedpage->error != 'resultexists' &&
$scannedpage->error != 'nonexistinguser' &&
$scannedpage->error != 'usernotincourse' &&
$scannedpage->error != 'grouperror') {
$scannedpage->error != 'grouperror' &&
$scannedpage->error != 'differentresultexists') {
echo "<input class=\"imagebutton\" type=\"submit\" value=\"".get_string('saveandshow', 'offlinequiz').
"\" name=\"submitbutton2\" onClick=\"document.forms.cform.show.value=1; return checkinput()\"><br />";
echo "<input class=\"imagebutton\" type=\"submit\" value=\"".get_string('save', 'offlinequiz')."\" name=\"submitbutton1\" onClick=\"return checkinput()\">";
Expand All @@ -799,6 +919,10 @@ function submitRotated() {
echo "<input type=\"hidden\" name=\"action\" value=\"update\">\n";
echo "<input type=\"hidden\" name=\"show\" value=\"0\">\n";

if ($userchanged) {
echo "<input type=\"hidden\" name=\"userchanged\" value=\"1\">\n";
}

foreach ($itemdata as $dkey => $items) {
foreach ($items as $key => $item) {
echo "<input type=\"hidden\" name=\"item[$dkey-$key]\" value=\"$item\">\n";
Expand Down
6 changes: 6 additions & 0 deletions lang/de/offlinequiz.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
$string['copytogroup'] = 'Alle Fragen zu Gruppe {$a} ';
$string['correcterror'] = 'korrigieren';
$string['correctforgroup'] = 'Korrekturbogen für Gruppe {$a}';
$string['correctionoptionsheading'] = 'Einstellungen für Korrektur von Antworbögen.';
$string['correctupdated'] = 'Korrekturbogen für Gruppe {$a} angepasst.';
$string['correct'] = 'richtig';
$string['couldnotgrab'] = 'Kann Bild {$a} nicht erfassen.';
Expand Down Expand Up @@ -117,6 +118,7 @@
$string['editscannedform'] = 'Scannerbeleg bearbeiten';
$string['editthislist'] = 'Diese Liste bearbeiten';
$string['emptygroups'] = 'Einige Offline-Test-Gruppen enthalten keine Fragen. Bitte fügen Sie Fragen hinzu.';
$string['enroluser'] = 'Teilnehmer/in einschreiben';
$string['errorreport'] = 'Importfehler-Bericht';
$string['everythingon'] = 'Aktiviert';
$string['Excelformat'] = 'Excel-Format';
Expand Down Expand Up @@ -268,6 +270,10 @@
$string['offlinequiz:preview'] = 'Test-Vorschau';
$string['offlinequiz:viewreports'] = 'Test-Berichte sehen';
$string['offlinequiz:view'] = 'Test-Informationen sehen';
$string['oneclickenrol'] = '1-Click Einschreibung';
$string['oneclickenroldesc'] = 'Wenn Sie diese Option aktivieren haben die Lehrenden beim Korrigieren von Antwortbögen (Fehler "Teilnehmer/in nicht im Kurs") die Möglichkeit Nutzer mit einem Click in den Kurs einzuschreiben.';
$string['oneclickrole'] = 'Rolle für Einschreibung.';
$string['oneclickroledesc'] = 'Wählen Sie hier die Rolle aus, die bei der 1-Click Einschreibung verwendet werden soll.';
$string['onlylocalcategories'] = 'Keine öffentlichen Fragekategorien zulassen.';
$string['orderingofflinequiz'] = 'Reihenfolge und Seitenumbrüche';
$string['otherresultexists'] = 'Ein abweichendes Ergebnis für {$a} existiert bereits. Import ignoriert! Löschen Sie das Ergebnis zuerst.';
Expand Down
6 changes: 6 additions & 0 deletions lang/en/offlinequiz.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
$string['correct'] = 'correct';
$string['correcterror'] = 'solve';
$string['correctforgroup'] = 'Correct answers for Group {$a}';
$string['correctionoptionsheading'] = 'Correction options.';
$string['correctupdated'] = 'Updated correction form for group {$a}.';
$string['couldnotgrab'] = 'Could not grab image {$a}';
$string['couldnotregister'] = 'Could not register user {$a}';
Expand Down Expand Up @@ -134,6 +135,7 @@
$string['editquestions'] = 'Edit questions';
$string['editscannedform'] = 'Edit scanned form';
$string['emptygroups'] = 'Some offline quiz groups are empty. Please add some questions.';
$string['enroluser'] = 'Enrol user';
$string['errorreport'] = 'Report of import errors';
$string['everythingon'] = 'enabled';
$string['Excelformat'] = 'Excel format';
Expand Down Expand Up @@ -297,6 +299,10 @@
$string['offlinequiz:viewreports'] = 'View offline quiz reports';
$string['offlinequiz:view'] = 'View offline quiz information';
$string['offlinequizwillopen'] = 'Offline-Test öffnet am {$a}';
$string['oneclickenrol'] = '1-Click Enrolment';
$string['oneclickenroldesc'] = 'If this option is activated teachers have the possiblity to enrol users with one click while correcting answer forms (error "User not in course").';
$string['oneclickrole'] = 'Role for 1-Click Enrolment.';
$string['oneclickroledesc'] = 'Choose the role used 1-click enrolment.';
$string['onlylocalcategories'] = 'Only local question categories';
$string['orderingofflinequiz'] = 'Order and paging';
$string['orderandpaging_help'] = 'The numbers 10, 20, 30, ... opposite each question indicate the order of the questions. The numbers increase in steps of 10 to leave space for additional questions to be inserted. To reorder the questions, change the numbers then click the "Reorder questions" button.
Expand Down
21 changes: 21 additions & 0 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,25 @@

$settings->add(new admin_setting_configselect('offlinequiz/papergray', get_string('papergray', 'offlinequiz'),
get_string('configpapergray', 'offlinequiz'), array('value' => 670, 'fix' => true), $options));

$settings->add(new admin_setting_heading('correctionheading',
get_string('correctionoptionsheading', 'offlinequiz'), ''));

// Admin setting to allow teachers to enrol users with one click while correcting answer forms.
$settings->add(new admin_setting_configcheckbox('offlinequiz/oneclickenrol', get_string('oneclickenrol', 'offlinequiz'),
get_string('oneclickenroldesc', 'offlinequiz'), 0));

$studentroles = $DB->get_records('role', array('archetype' => 'student'), 'sortorder');
$options = array();
$default = null;
foreach ($studentroles as $role) {
$options[$role->id] = $role->name;
if ($default) {
$default = $role->id;
}
}

$settings->add(new admin_setting_configselect('offlinequiz/oneclickrole', get_string('oneclickrole', 'offlinequiz'),
get_string('oneclickroledesc', 'offlinequiz'), array('value' => $default, 'fix' => true), $options));

}

0 comments on commit 7c8f4ce

Please sign in to comment.