Skip to content

Commit

Permalink
The teacher can now move selected questions to a chosen category or d…
Browse files Browse the repository at this point in the history
…elete them, see http://moodle.org/mod/forum/discuss.php?d=16239 and http://moodle.org/mod/forum/discuss.php?d=19734

There is also a 'Deselect all' button which I implemented provisionally by putting an extra function checknone() into javascript-static.js. This should be cleaned up by someone who knows javascript.
  • Loading branch information
gustav_delius committed Mar 6, 2005
1 parent 556963f commit 03f9425
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 21 deletions.
8 changes: 7 additions & 1 deletion lang/en/quiz.php
Expand Up @@ -7,7 +7,7 @@
$string['addingquestions'] = 'This side of the page is where you manage your database of questions. Questions are stored in categories to help you keep them organised, and can be used by any quiz in your course or even other courses if you choose to \'publish\' them. <br /><br />After you select or create a question category you will be able to create or edit questions. You can select any of these questions to add to your quiz over on the other side of this page.';
$string['addquestions'] = 'Add questions';
$string['addquestionstoquiz'] = 'Add questions to current quiz';
$string['addrandom1'] = ' Add ';
$string['addrandom1'] = ' << Add ';
$string['addrandom2'] = 'random questions ';
$string['addselectedtoquiz'] = 'Add selected to quiz';
$string['addtoquiz'] = 'Add to quiz';
Expand Down Expand Up @@ -50,6 +50,7 @@
$string['categoryinfo'] = 'Category info';
$string['categorymove'] = 'The category \'$a->name\' contains $a->count questions. Please choose another category to move them to.';
$string['categorymoveto'] = 'Move them to this category';
$string['categorynoedit'] = 'You do not have editing privileges in the category \'$a\'.';
$string['categoryupdated'] = 'The category was successfully updated';
$string['checkanswer'] = 'Check';
$string['choice'] = 'Choice';
Expand Down Expand Up @@ -84,6 +85,8 @@
$string['defaultinfo'] = 'The default category for questions.';
$string['deleteattemptcheck'] = 'Are you absolutely sure you want to completely delete these attempts?';
$string['deletequestioncheck'] = 'Are you absolutely sure you want to delete \'$a\'?';
$string['deletequestionscheck'] = 'Are you absolutely sure you want to delete the following questions? <div>$a</div>';
$string['deleteselected'] = 'Delete selected';
$string['description'] = 'Description';
$string['discrimination'] = 'Discrim. Index';
$string['eachattemptbuildsonthelast'] = 'Each attempt builds on the last';
Expand Down Expand Up @@ -167,6 +170,7 @@
$string['missingword'] = 'Missing word format';
$string['modulename'] = 'Quiz';
$string['modulenameplural'] = 'Quizzes';
$string['moveto'] = 'Move to >>';
$string['multianswer'] = 'Embedded Answers (Cloze)';
$string['multichoice'] = 'Multiple Choice';
$string['multiplier'] = 'Multiplier';
Expand Down Expand Up @@ -209,6 +213,7 @@
$string['questionname'] = 'Question name';
$string['questionnametoolong'] = 'Question name too long at line $a (255 char. max). It has been truncated.';
$string['questions'] = 'Questions';
$string['questionsinuse'] = '(* Questions marked by an asterix are already in use in some quizzes. These question will not be deleted from these quizzes but only from the category list.)';
$string['questionsperpage'] = 'Max number of questions per page';
$string['questiontypesetupoptions'] = 'Setup options for question types:';
$string['quizavailable'] = 'The quiz is available until: $a';
Expand Down Expand Up @@ -263,6 +268,7 @@
$string['score'] = 'Raw score';
$string['select'] = 'Select';
$string['selectall'] = 'Select all';
$string['selectnone'] = 'Deselect all';
$string['selectcategoryabove'] = 'Select a category above';
$string['shortanswer'] = 'Short Answer';
$string['show'] = 'Show';
Expand Down
7 changes: 7 additions & 0 deletions lib/javascript-static.js
Expand Up @@ -23,6 +23,13 @@ function checkall() {
void(el[i].checked=1)
}

function checknone() {
void(d=document);
void(el=d.getElementsByTagName('INPUT'));
for(i=0;i<el.length;i++)
void(el[i].checked=0)
}

function lockoptions(form, master, subitems) {
// subitems is an array of names of sub items
// requires that each item in subitems has a
Expand Down
92 changes: 87 additions & 5 deletions mod/quiz/edit.php
Expand Up @@ -10,6 +10,9 @@
$page = optional_param('page', 0);
$perpage = optional_param('perpage', 20);

$strquizzes = get_string('modulenameplural', 'quiz');
$strediting = get_string('editquestions', "quiz");

if ($modform = data_submitted() and !empty($modform->course)) { // data submitted

$SESSION->modform = $modform; // Save the form in the current session
Expand Down Expand Up @@ -165,7 +168,25 @@
quiz_questiongrades_update($modform->grades, $modform->instance);
}

if (isset($_REQUEST['delete']) and confirm_sesskey()) { /// Delete a question from the list
if (isset($_REQUEST['move']) and confirm_sesskey()) { /// Move selected questions to new category
if (!$tocategory = get_record('quiz_categories', 'id', $_REQUEST['category'])) {
error('Invalid category');
}
if (!isteacheredit($category->course)) {
error(get_string('categorynoedit', 'quiz', $tocategory->name), edit.php);
}
$rawquestions = $_POST;
foreach ($rawquestions as $key => $value) { // Parse input for question ids
if (substr($key, 0, 1) == "q") {
$key = substr($key,1);
if (!set_field('quiz_questions', 'category', $tocategory->id, 'id', $key)) {
error('Could not update category field');
}
}
}
}

if (isset($_REQUEST['delete']) and confirm_sesskey()) { /// Remove a question from the quiz
$questions = explode(",", $modform->questions);
foreach ($questions as $key => $question) {
if ($question == $delete) {
Expand All @@ -183,6 +204,70 @@
}
}

if (isset($_REQUEST['deleteselected'])) { // delete selected questions from the category

if (isset($confirm) and confirm_sesskey()) { // teacher has already confirmed the action
if ($confirm == md5($deleteselected)) {
if ($questionlist = explode(',', $deleteselected)) {
// for each question either hide it if it is in use or delete it
foreach ($questionlist as $questionid) {
if (record_exists('quiz_responses', 'question', $questionid) or
record_exists('quiz_responses', 'originalquestion', $questionid) or
record_exists('quiz_question_grades', 'question', $questionid)) {
if (!set_field('quiz_questions', 'hidden', 1, 'id', $questionid)) {
error('Was not able to hide question');
}
} else {
if (!delete_records("quiz_questions", "id", $questionid)) {
error("An error occurred trying to delete question (id $questionid)");
}
}
}
}
redirect("edit.php");
} else {
error("Confirmation string was incorrect");
}

} else { // teacher still has to confirm
// make a list of all the questions that are selected
$rawquestions = $_POST;
$questionlist = ''; // comma separated list of ids of questions to be deleted
$questionnames = ''; // string with names of questions separated by <br /> with
// an asterix in front of those that are in use
$inuse = false; // set to true if at least one of the questions is in use
foreach ($rawquestions as $key => $value) { // Parse input for question ids
if (substr($key, 0, 1) == "q") {
$key = substr($key,1);
$questionlist .= $key.',';
if (record_exists('quiz_responses', 'question', $key) or
record_exists('quiz_responses', 'originalquestion', $key) or
record_exists('quiz_question_grades', 'question', $key)) {
$questionnames .= '* ';
$inuse = true;
}
$questionnames .= get_field('quiz_questions', 'name', 'id', $key).'<br />';
}
}
if (!$questionlist) { // no questions were selected
redirect('edit.php');
}
$questionlist = rtrim($questionlist, ',');

// Add an explanation about questions in use
if ($inuse) {
$questionnames .= get_string('questionsinuse', 'quiz');
}
print_header_simple($strediting, '',
"<a href=\"index.php?id=$course->id\">$strquizzes</a>".
" -> $strediting");
notice_yesno(get_string("deletequestionscheck", "quiz", $questionnames),
"edit.php?sesskey=$USER->sesskey&amp;deleteselected=$questionlist&amp;confirm=".md5($questionlist), "edit.php");
print_footer($course);
exit;
}
}

if (isset($_REQUEST['setgrades']) and confirm_sesskey()) { /// The grades have been updated, so update our internal list
$rawgrades = $_POST;
unset($modform->grades);
Expand Down Expand Up @@ -222,9 +307,6 @@

$SESSION->modform = $modform;

$strquizzes = get_string('modulenameplural', 'quiz');
$strediting = get_string('editquestions', "quiz");

// Print basic page layout.

if (isset($modform->instance) and record_exists_sql("SELECT * FROM {$CFG->prefix}quiz_attempts WHERE quiz = '$modform->instance' AND NOT (userid = '$USER->id') LIMIT 1")){
Expand Down Expand Up @@ -293,7 +375,7 @@
print_spacer(5,1);
// continues with list of questions
print_simple_box_start("center", "100%");
quiz_print_cat_question_list($modform->category,
quiz_print_cat_question_list($course, $modform->category,
isset($modform->instance), $modform->recurse, $page, $perpage, $modform->showhidden);
print_simple_box_end();
if (!isset($modform->instance)) {
Expand Down
37 changes: 22 additions & 15 deletions mod/quiz/locallib.php
Expand Up @@ -1321,7 +1321,7 @@ function quiz_print_question_list($questionlist, $grades, $allowdelete=true) {
}


function quiz_print_cat_question_list($categoryid, $quizselected=true, $recurse=1, $page, $perpage, $showhidden=false) {
function quiz_print_cat_question_list($course, $categoryid, $quizselected=true, $recurse=1, $page, $perpage, $showhidden=false) {
// Prints the table of questions in a category with interactions

global $QUIZ_QUESTION_TYPE, $USER;
Expand All @@ -1334,9 +1334,11 @@ function quiz_print_cat_question_list($categoryid, $quizselected=true, $recurse=
$strnoquestions = get_string("noquestions", "quiz");
$strselect = get_string("select", "quiz");
$strselectall = get_string("selectall", "quiz");
$strselectnone = get_string("selectnone", "quiz");
$strcreatenewquestion = get_string("createnewquestion", "quiz");
$strquestionname = get_string("questionname", "quiz");
$strdelete = get_string("delete");
$strdeleteselected = get_string("deleteselected", 'quiz');
$stredit = get_string("edit");
$straction = get_string("action");
$strrestore = get_string('restore');
Expand Down Expand Up @@ -1416,7 +1418,7 @@ function quiz_print_cat_question_list($categoryid, $quizselected=true, $recurse=
echo "<table border=\"0\" cellpadding=\"5\" cellspacing=\"2\" width=\"100%\">";
echo "<tr>";
if ($canedit) {
echo "<th width=\"100\" nowrap=\"nowrap\">$straction</th>";
echo "<th width=\"95\" nowrap=\"nowrap\">$straction</th>";
}
echo "<th width=\"100%\" align=\"left\" nowrap=\"nowrap\">$strquestionname</th><th width=\"*\" nowrap=\"nowrap\">$strtype</th>";
echo "</tr>\n";
Expand All @@ -1443,9 +1445,7 @@ function quiz_print_cat_question_list($categoryid, $quizselected=true, $recurse=
echo "<a title=\"$strdelete\" href=\"question.php?id=$question->id&amp;delete=$question->id\"><img
src=\"../../pix/t/delete.gif\" border=\"0\" alt=\"$strdelete\" /></a>";
}
if ($quizselected) {
echo "&nbsp;<input title=\"$strselect\" type=\"checkbox\" name=\"q$question->id\" value=\"1\" />";
}
echo "&nbsp;<input title=\"$strselect\" type=\"checkbox\" name=\"q$question->id\" value=\"1\" />";
echo "</td>\n";
}
if ($question->hidden) {
Expand All @@ -1458,22 +1458,29 @@ function quiz_print_cat_question_list($categoryid, $quizselected=true, $recurse=
echo "</td>\n";
echo "</tr>\n";
}
echo "</table>\n";

echo "<input type=\"button\" onclick=\"checkall()\" value=\"$strselectall\" />\n";
echo "<input type=\"button\" onclick=\"checknone()\" value=\"$strselectnone\" />\n";
echo "&nbsp;<input type=\"submit\" name=\"deleteselected\" value=\"$strdeleteselected\" />\n";
echo "<br />";

echo '<table><tr><td>';
if ($quizselected) {
echo "<input type=\"submit\" name=\"add\" value=\"<< $straddselectedtoquiz\" />\n";
echo '</td><td>';
}
echo '<input type="submit" name="move" value="'.get_string('moveto', 'quiz')."\" />\n";
quiz_category_select_menu($course->id, false, true, $category->id);
echo "</td></tr></table>";

$numquestions = count_records_select('quiz_questions', "category IN ($categorylist) AND qtype != '".RANDOM."'");
echo '<tr><td colspan="3">';
print_paging_bar($numquestions, $page, $perpage,
"edit.php?perpage=$perpage&amp;");
echo '</td></tr>';

if ($quizselected) {
echo "<tr>\n<td colspan=\"3\">";
echo "<input type=\"submit\" name=\"add\" value=\"<< $straddselectedtoquiz\" />\n";
// echo "<input type=\"submit\" name=\"delete\" value=\"XX Delete selected\">";
echo "<input type=\"button\" onclick=\"checkall()\" value=\"$strselectall\" />\n";
echo "</td></tr>";
}
echo "</table>\n";
echo "</form>\n";
if ($quizselected and isteacheredit($category->course)) {
if ($quizselected) {
for ($i=1;$i<=10; $i++) {
$randomcount[$i] = $i;
}
Expand Down
4 changes: 4 additions & 0 deletions mod/quiz/question.php
Expand Up @@ -147,6 +147,10 @@
$quizlist[$grade->quiz] = $grade->quiz;
}
}

if (isset($form->makecopy)) { // explicitly requested copies should be unhidden
$question->hidden = 0;
}

// Logic to determine whether old version should be overwritten
$makecopy = isset($form->makecopy) || (!$form->id); unset($form->makecopy);
Expand Down

0 comments on commit 03f9425

Please sign in to comment.