From f5831eeac0181f12c52525f76f265136bc74779e Mon Sep 17 00:00:00 2001 From: tjhunt Date: Fri, 6 Mar 2009 04:06:48 +0000 Subject: [PATCH] quiz editing: coding style cleanup. --- mod/quiz/editlib.php | 569 +++++++++++++++++++++---------------------- 1 file changed, 280 insertions(+), 289 deletions(-) diff --git a/mod/quiz/editlib.php b/mod/quiz/editlib.php index ec9c662dc6..d10a281952 100644 --- a/mod/quiz/editlib.php +++ b/mod/quiz/editlib.php @@ -30,7 +30,7 @@ * @package quiz *//** */ -require_once("locallib.php"); +require_once($CFG->dirroot . '/mod/quiz/locallib.php'); define('NUM_QS_TO_SHOW_IN_RANDOM', 3); @@ -77,20 +77,20 @@ function quiz_delete_empty_page($layout, $index) { } /** -* Add a question to a quiz -* -* Adds a question to a quiz by updating $quiz as well as the -* quiz and quiz_question_instances tables. It also adds a page break -* if required. -* @return boolean false if the question was already in the quiz -* @param int $id The id of the question to be added -* @param object $quiz The extended quiz object as used by edit.php -* This is updated by this function -* @param int $page Which page in quiz to add the question on; if 0 (default), add at the end -*/ -function quiz_add_quiz_question($id, &$quiz, $page=0) { + * Add a question to a quiz + * + * Adds a question to a quiz by updating $quiz as well as the + * quiz and quiz_question_instances tables. It also adds a page break + * if required. + * @return boolean false if the question was already in the quiz + * @param int $id The id of the question to be added + * @param object $quiz The extended quiz object as used by edit.php + * This is updated by this function + * @param int $page Which page in quiz to add the question on; if 0 (default), add at the end + */ +function quiz_add_quiz_question($id, &$quiz, $page = 0) { global $DB; - $questions = explode(",", $quiz->questions); + $questions = explode(',', $quiz->questions); if (in_array($id, $questions)) { return false; } @@ -101,37 +101,36 @@ function quiz_add_quiz_question($id, &$quiz, $page=0) { $end = end($breaks); $last = prev($breaks); $last = $last ? $last : -1; - if (!$quiz->questionsperpage or - (($end - $last -1) < $quiz->questionsperpage)) { + if (!$quiz->questionsperpage || (($end - $last -1) < $quiz->questionsperpage)) { array_pop($questions); } } - if(is_int($page) && $page >= 1){ - $numofpages=quiz_number_of_pages($quiz->questions); - if ($numofpages<$page){ + if (is_int($page) && $page >= 1) { + $numofpages = quiz_number_of_pages($quiz->questions); + if ($numofpages<$page) { //the page specified does not exist in quiz - $page=0; - }else{ + $page = 0; + } else { // add ending page break - the following logic requires doing //this at this point $questions[] = 0; - $currentpage=1; - $addnow=false; - foreach ($questions as $question){ - if($question==0){ + $currentpage = 1; + $addnow = false; + foreach ($questions as $question) { + if ($question == 0) { $currentpage++; //The current page is the one after the one we want to add on, //so we add the question before adding the current page. - if ($currentpage==$page+1){ - $questions_new[]=$id; + if ($currentpage == $page + 1) { + $questions_new[] = $id; } } - $questions_new[]=$question; + $questions_new[] = $question; } - $questions=$questions_new; + $questions = $questions_new; } } - if ($page==0){ + if ($page == 0) { // add question $questions[] = $id; // add ending page break @@ -139,7 +138,7 @@ function quiz_add_quiz_question($id, &$quiz, $page=0) { } // Save new questionslist in database - $quiz->questions = implode(",", $questions); + $quiz->questions = implode(',', $questions); if (!$DB->set_field('quiz', 'questions', $quiz->questions, array('id' => $quiz->id))) { print_error('cannotsavequestion', 'quiz'); } @@ -199,15 +198,15 @@ function quiz_save_new_layout($quiz) { } /** -* Save changes to question instance -* -* Saves changes to the question grades in the quiz_question_instances table. -* It does not update 'sumgrades' in the quiz table. -* @return boolean Indicates success or failure. -* @param integer grade The maximal grade for the question -* @param integer $questionid The id of the question -* @param integer $quizid The id of the quiz to update / add the instances for. -*/ + * Save changes to question instance + * + * Saves changes to the question grades in the quiz_question_instances table. + * It does not update 'sumgrades' in the quiz table. + * @return boolean Indicates success or failure. + * @param integer grade The maximal grade for the question + * @param integer $questionid The id of the question + * @param integer $quizid The id of the quiz to update / add the instances for. + */ function quiz_update_question_instance($grade, $questionid, $quizid) { global $DB; if ($instance = $DB->get_record('quiz_question_instances', array('quiz' => $quizid, 'question' => $questionid))) { @@ -218,7 +217,7 @@ function quiz_update_question_instance($grade, $questionid, $quizid) { $instance->quiz = $quizid; $instance->question = $questionid; $instance->grade = $grade; - return $DB->insert_record("quiz_question_instances", $instance); + return $DB->insert_record('quiz_question_instances', $instance); } } @@ -269,41 +268,41 @@ function quiz_move_question_down($layout, $questionid) { } /** -* Prints a list of quiz questions for the edit.php main view for edit -* ($reordertool=false) and order and paging ($reordertool=true) tabs -* -* @return int sum of maximum grades -* @param object $quiz This is not the standard quiz object used elsewhere but -* it contains the quiz layout in $quiz->questions and the grades in -* $quiz->grades -* @param object $pageurl The url of the current page with the parameters required -* for links returning to the current page, as a moodle_url object -* @param boolean $allowdelete Indicates whether the delete icons should be displayed -* @param boolean $reordertool Indicates whether the reorder tool should be displayed -* @param boolean $quiz_qbanktool Indicates whether the question bank should be displayed -* @param boolean $hasattempts Indicates whether the quiz has attempts -*/ -function quiz_print_question_list($quiz, $pageurl, $allowdelete=true, - $reordertool=false, $quiz_qbanktool=false, - $hasattempts=false) { + * Prints a list of quiz questions for the edit.php main view for edit + * ($reordertool = false) and order and paging ($reordertool = true) tabs + * + * @return int sum of maximum grades + * @param object $quiz This is not the standard quiz object used elsewhere but + * it contains the quiz layout in $quiz->questions and the grades in + * $quiz->grades + * @param object $pageurl The url of the current page with the parameters required + * for links returning to the current page, as a moodle_url object + * @param boolean $allowdelete Indicates whether the delete icons should be displayed + * @param boolean $reordertool Indicates whether the reorder tool should be displayed + * @param boolean $quiz_qbanktool Indicates whether the question bank should be displayed + * @param boolean $hasattempts Indicates whether the quiz has attempts + */ +function quiz_print_question_list($quiz, $pageurl, $allowdelete = true, + $reordertool = false, $quiz_qbanktool = false, + $hasattempts = false) { global $USER, $CFG, $QTYPES, $DB; - $strorder = get_string("order"); - $strquestionname = get_string("questionname", "quiz"); - $strgrade = get_string("grade"); + $strorder = get_string('order'); + $strquestionname = get_string('questionname', 'quiz'); + $strgrade = get_string('grade'); $strremove = get_string('remove', 'quiz'); - $stredit = get_string("edit"); - $strview = get_string("view"); - $straction = get_string("action"); - $strmove = get_string("move"); - $strmoveup = get_string("moveup"); - $strmovedown = get_string("movedown"); - $strsave=get_string('save',"quiz"); - $strreorderquestions=get_string("reorderquestions","quiz"); - - $strselectall = get_string("selectall", "quiz"); - $strselectnone = get_string("selectnone", "quiz"); - $strtype = get_string("type", "quiz"); - $strpreview = get_string("preview", "quiz"); + $stredit = get_string('edit'); + $strview = get_string('view'); + $straction = get_string('action'); + $strmove = get_string('move'); + $strmoveup = get_string('moveup'); + $strmovedown = get_string('movedown'); + $strsave = get_string('save', 'quiz'); + $strreorderquestions = get_string('reorderquestions', 'quiz'); + + $strselectall = get_string('selectall', 'quiz'); + $strselectnone = get_string('selectnone', 'quiz'); + $strtype = get_string('type', 'quiz'); + $strpreview = get_string('preview', 'quiz'); if ($quiz->questions) { list($usql, $params) = $DB->get_in_or_equal(explode(',', $quiz->questions)); @@ -316,76 +315,74 @@ function quiz_print_question_list($quiz, $pageurl, $allowdelete=true, $questions = array(); } - $layout=quiz_clean_layout($quiz->questions); + $layout = quiz_clean_layout($quiz->questions); $order = explode(',', $layout); - $lastindex = count($order)-1; + $lastindex = count($order) - 1; if ($hasattempts) { - $disabled='disabled="disabled"'; - $movedisabled=''; - $pagingdisabled=''; + $disabled = 'disabled="disabled"'; + $movedisabled = ''; + $pagingdisabled = ''; } else { - $disabled=''; - $movedisabled=''; - $pagingdisabled=''; + $disabled = ''; + $movedisabled = ''; + $pagingdisabled = ''; } if ($quiz->shufflequestions) { - $movedisabled='disabled="disabled"'; + $movedisabled = 'disabled="disabled"'; } - if($quiz->questionsperpage){ - $pagingdisabled='disabled="disabled"'; + if ($quiz->questionsperpage) { + $pagingdisabled = 'disabled="disabled"'; } - - $reordercontrolssetdefaultsubmit='
'. - '
'; - $reordercontrols1='
'. - '
'; + $reordercontrolssetdefaultsubmit = '
' . + '
'; + $reordercontrols1 = '
' . + '
'; $reordercontrols1 .= '
' . '
'; - $a = ''; - - $reordercontrols2top='
'. - get_string("moveselectedonpage","quiz", $a) . - ''.' -
'; - $reordercontrols2bottom='
'. - '
'. - get_string("moveselectedonpage","quiz",$a) . - ' '.'
'; - - $reordercontrols3=''. - $strselectall.' /'; - $reordercontrols3.= ' '. - $strselectnone.''; - - $reordercontrolstop='
'. - $reordercontrolssetdefaultsubmit. - $reordercontrols1.$reordercontrols2top.$reordercontrols3."
"; - $reordercontrolsbottom='
'. - $reordercontrolssetdefaultsubmit. - $reordercontrols2bottom.$reordercontrols1.$reordercontrols3."
"; - - if($reordertool){ - + $a = ''; + + $reordercontrols2top = '
' . + get_string('moveselectedonpage', 'quiz', $a) . + '' . ' +
'; + $reordercontrols2bottom = '
' . + '
' . + get_string('moveselectedonpage', 'quiz', $a) . + ' ' . '
'; + + $reordercontrols3 = '' . + $strselectall . ' /'; + $reordercontrols3.= ' ' . + $strselectnone . ''; + + $reordercontrolstop = '
' . + $reordercontrolssetdefaultsubmit . + $reordercontrols1 . $reordercontrols2top . $reordercontrols3 . "
"; + $reordercontrolsbottom = '
' . + $reordercontrolssetdefaultsubmit . + $reordercontrols2bottom . $reordercontrols1 . $reordercontrols3 . "
"; + + if ($reordertool) { echo '
'; echo $pageurl->hidden_params_out(); - echo ''; + echo ''; echo $reordercontrolstop; } @@ -393,7 +390,7 @@ function quiz_print_question_list($quiz, $pageurl, $allowdelete=true, //the current question ordinal (no descriptions) $qno = 1; //the current question (includes questions and descriptions) - $questioncount=0; + $questioncount = 0; //the ordinal of current element in the layout //(includes page breaks, questions and descriptions) $count = 0; @@ -402,41 +399,40 @@ function quiz_print_question_list($quiz, $pageurl, $allowdelete=true, $sumgrade = 0; - $pageopen=false; - + $pageopen = false; $returnurl = $pageurl->out(); - $questiontotalcount=count($order); + $questiontotalcount = count($order); foreach ($order as $i => $qnum) { - $reordercheckbox=''; - $reordercheckboxlabel=''; - $reordercheckboxlabelclose=''; + $reordercheckbox = ''; + $reordercheckboxlabel = ''; + $reordercheckboxlabelclose = ''; - if ($qnum and empty($questions[$qnum])) { + if ($qnum && empty($questions[$qnum])) { continue; } // If the questiontype is missing change the question type - if ($qnum and !array_key_exists($questions[$qnum]->qtype, $QTYPES)) { + if ($qnum && !array_key_exists($questions[$qnum]->qtype, $QTYPES)) { $questions[$qnum]->qtype = 'missingtype'; } - $deletex="delete.gif"; + $deletex = "delete.gif"; if ($qnum != 0 || ($qnum == 0 && !$pageopen)) { //this is either a question or a page break after another // (no page is currently open) if (!$pageopen) { //if no page is open, start display of a page $pagecount++; - echo '
'. - get_string('page').' '.$pagecount. + echo '
' . + get_string('page') . ' ' . $pagecount . '
'; $pageopen = true; } if ($qnum == 0 && $i < $questiontotalcount) { // This is the second successive page break. Tell the user the page is empty. echo '
'; - print_string("noquestionsonpage", "quiz"); + print_string('noquestionsonpage', 'quiz'); echo '
'; if ($allowdelete && !$quiz->questionsperpage) { echo '
'; @@ -448,46 +444,45 @@ function quiz_print_question_list($quiz, $pageurl, $allowdelete=true, } } - if($qnum!=0){ + if ($qnum!=0) { $question = $questions[$qnum]; $questionparams = array('returnurl' => $returnurl, - 'cmid'=>$quiz->cmid, 'id' => $question->id); + 'cmid' => $quiz->cmid, 'id' => $question->id); $questionurl = new moodle_url("$CFG->wwwroot/question/question.php", $questionparams); $questioncount++; //this is an actual question - /* Display question start */ ?>
id. - '" id="s'.$question->id.'" />'; - $reordercheckboxlabel=''; + $reordercheckbox = ''; + $reordercheckboxlabel = ''; + $reordercheckboxlabelclose = ''; + if ($reordertool) { + $reordercheckbox = ''; + $reordercheckboxlabel = ''; } if (!$quiz->shufflequestions) { // Print and increment question number - $questioncountstring=""; - if ($questioncount>999 OR ($reordertool && $questioncount>99)){ - $questioncountstring= - "$reordercheckboxlabel$questioncount". - $reordercheckboxlabelclose.$reordercheckbox; - }else{ - $questioncountstring=$reordercheckboxlabel.$questioncount. - $reordercheckboxlabelclose.$reordercheckbox; + $questioncountstring = ''; + if ($questioncount>999 || ($reordertool && $questioncount>99)) { + $questioncountstring = + "$reordercheckboxlabel$questioncount" . + $reordercheckboxlabelclose . $reordercheckbox; + } else { + $questioncountstring = $reordercheckboxlabel . $questioncount . + $reordercheckboxlabelclose . $reordercheckbox; } echo $questioncountstring; $qno += $question->length; } else { - echo "$reordercheckboxlabel * $reordercheckboxlabelclose". + echo "$reordercheckboxlabel * $reordercheckboxlabelclose" . " $reordercheckbox"; } @@ -498,31 +493,31 @@ function quiz_print_question_list($quiz, $pageurl, $allowdelete=true, = $lastindex - 1) { - $upbuttonclass="upwithoutdown"; + $upbuttonclass = 'upwithoutdown'; } - echo "out_action(array('up' => $question->id))."\">out_action(array('up' => $question->id)) . "\">pixpath/t/up.gif\" class=\"iconsmall $upbuttonclass\" alt=\"$strmoveup\" />"; } } if ($count < $lastindex - 1) { - if(!$hasattempts){ - echo "out_action(array('down' => $question->id))."\">pixpath/t/down.gif\" class=\"iconsmall\"". + if (!$hasattempts) { + echo "out_action(array('down' => $question->id)) . "\">pixpath/t/down.gif\" class=\"iconsmall\"" . " alt=\"$strmovedown\" />"; } } if ($allowdelete && question_has_capability_on($question, 'use', $question->category)) { // remove from quiz, not question delete. if (!$hasattempts) { - echo "out_action(array('remove' => $question->id))."\"> - pixpath/t/delete.gif\" ". + echo "out_action(array('remove' => $question->id)) . "\"> + pixpath/t/delete.gif\" " . "class=\"iconsmall\" alt=\"$strremove\" />"; } } @@ -538,14 +533,14 @@ function quiz_print_question_list($quiz, $pageurl, $allowdelete=true, hidden_params_out(); ?> decimalpoints + 2) . '" - value="'.(0 + $quiz->grades[$qnum]). - '" tabindex="'.($lastindex+$qno).'" />'; + echo ''; ?> qtype == 'random') { - echo ''.get_string("configurerandomquestion","quiz").''; + echo '' . get_string("configurerandomquestion", "quiz") . ''; } ?> @@ -554,15 +549,15 @@ function quiz_print_question_list($quiz, $pageurl, $allowdelete=true,
'; + echo ''; ?> @@ -572,7 +567,7 @@ function quiz_print_question_list($quiz, $pageurl, $allowdelete=true, } ?>
- qtype == 'random') { // it is a random question if (!$reordertool) { quiz_print_randomquestion($question, $pageurl, $quiz, $quiz_qbanktool); @@ -600,27 +595,27 @@ function quiz_print_question_list($quiz, $pageurl, $allowdelete=true, } } //a page break: end the existing page. - if($qnum == 0){ - if($pageopen){ - if(!$reordertool){ + if ($qnum == 0) { + if ($pageopen) { + if (!$reordertool) { quiz_print_pagecontrols($quiz, $pageurl, $pagecount, $hasattempts); - }else if ($i<$questiontotalcount-1){ + } else if ($i<$questiontotalcount - 1) { //do not include the last page break for reordering //to avoid creating a new extra page in the end - echo ''; + echo ''; } echo "
"; - if(!$reordertool){ + if (!$reordertool) { echo "
"; print_single_button($pageurl->out(true), - array("cmid"=>$quiz->cmid, - "courseid"=>$quiz->course, - "addpage"=>$count, - "sesskey"=>sesskey()), - get_string("addpagehere","quiz"), + array('cmid' => $quiz->cmid, + 'courseid' => $quiz->course, + 'addpage' => $count, + 'sesskey' => sesskey()), + get_string('addpagehere', 'quiz'), 'get', '_self', false, @@ -629,19 +624,17 @@ function quiz_print_question_list($quiz, $pageurl, $allowdelete=true, echo "
"; } $pagecount; - $pageopen=false; + $pageopen = false; $count++; } } } - if($reordertool){ + if ($reordertool) { echo $reordercontrolsbottom; echo '
'; } - - return $sumgrade; } @@ -654,7 +647,7 @@ function quiz_print_question_list($quiz, $pageurl, $allowdelete=true, * @param unknown_type $page * @param unknown_type $hasattempts */ -function quiz_print_pagecontrols($quiz,$pageurl,$page, $hasattempts){ +function quiz_print_pagecontrols($quiz, $pageurl, $page, $hasattempts) { global $CFG; echo '
'; @@ -666,7 +659,7 @@ function quiz_print_pagecontrols($quiz,$pageurl,$page, $hasattempts){ $defaultcategory = question_make_default_categories($contexts->all()); // Create the url the question page will return to - $returnurl_addtoquiz = new moodle_url($pageurl->out(true), array("addonpage" => $page)); + $returnurl_addtoquiz = new moodle_url($pageurl->out(true), array('addonpage' => $page)); // Print a button linking to the choose question type page. $newquestionparams = array('returnurl' => $returnurl_addtoquiz->out(false), @@ -687,7 +680,7 @@ function quiz_print_pagecontrols($quiz,$pageurl,$page, $hasattempts){ - /> + />
@@ -705,47 +698,45 @@ function quiz_print_pagecontrols($quiz,$pageurl,$page, $hasattempts){ * @return object an object with properties newrandomcategory and addonpage if operation successful. * if operation failed, returns false. */ -function quiz_process_randomquestion_formdata(&$qcobject){ - global $CFG,$DB; - $newrandomcategory=0; - $addonpage=0; - $newquestioninfo=false; - if ($qcobject->catform_rand->is_cancelled()){ +function quiz_process_randomquestion_formdata(&$qcobject) { + global $CFG, $DB; + $newrandomcategory = 0; + $addonpage = 0; + $newquestioninfo = false; + if ($qcobject->catform_rand->is_cancelled()) { return false; - }elseif ($catformdata = $qcobject->catform_rand->get_data()) { - $newquestioninfo=new stdClass; - $addonpage=$catformdata->addonpage; - $newquestioninfo->addonpage=$catformdata->addonpage; + } elseif ($catformdata = $qcobject->catform_rand->get_data()) { + $newquestioninfo = new stdClass; + $addonpage = $catformdata->addonpage; + $newquestioninfo->addonpage = $catformdata->addonpage; if (!$catformdata->id) {//new category - $newrandomcategory=$qcobject->add_category($catformdata->parent, - $catformdata->name, $catformdata->info,true); - if(!is_null($newrandomcategory)){ - $newquestioninfo->newrandomcategory=$newrandomcategory; + $newrandomcategory = $qcobject->add_category($catformdata->parent, + $catformdata->name, $catformdata->info, true); + if (!is_null($newrandomcategory)) { + $newquestioninfo->newrandomcategory = $newrandomcategory; if (! $newcategory = $DB->get_record('question_categories', - array('id'=>$newrandomcategory))) { - $newquestioninfo->newrandomcategory=false; + array('id' => $newrandomcategory))) { + $newquestioninfo->newrandomcategory = false; } - }else{ - $newquestioninfo->newrandomcategory=false; + } else { + $newquestioninfo->newrandomcategory = false; } } else { - $newquestioninfo->newrandomcategory=false; + $newquestioninfo->newrandomcategory = false; } } return($newquestioninfo); } - /** * Print a given single question in quiz for the edit tab of edit.php. * Meant to be used from quiz_print_question_list() - * + * * @param object $question A question object from the database questions table * @param object $returnurl The url to get back to this page, for example after editing. * @param object $quiz The quiz in the context of which the question is being displayed - * */ -function quiz_print_singlequestion($question, $returnurl, $quiz){ +function quiz_print_singlequestion($question, $returnurl, $quiz) { global $QTYPES; echo '
'; echo quiz_question_edit_button($quiz->cmid, $question, $returnurl, quiz_question_tostring($question) . ' '); @@ -759,13 +750,13 @@ function quiz_print_singlequestion($question, $returnurl, $quiz){ /** * Print a given random question in quiz for the edit tab of edit.php. * Meant to be used from quiz_print_question_list() - * + * * @param object $question A question object from the database questions table * @param object $questionurl The url of the question editing page as a moodle_url object * @param object $quiz The quiz in the context of which the question is being displayed - * @param boolean $quiz_qbanktool Indicate to this function if the question bank window open + * @param boolean $quiz_qbanktool Indicate to this function if the question bank window open */ -function quiz_print_randomquestion(&$question, &$pageurl, &$quiz,$quiz_qbanktool){ +function quiz_print_randomquestion(&$question, &$pageurl, &$quiz, $quiz_qbanktool) { global $DB, $QTYPES, $THEME; check_theme_arrows(); echo '
'; @@ -809,7 +800,7 @@ function quiz_print_randomquestion(&$question, &$pageurl, &$quiz,$quiz_qbanktool $a = new stdClass; $a->catname = '' . $category->name . ''; $a->link = $linkcategorycontents; - echo get_string('addnewquestionsqbank','quiz', $a); + echo get_string('addnewquestionsqbank', 'quiz', $a); } else { // Category has questions @@ -850,12 +841,12 @@ function quiz_print_randomquestion(&$question, &$pageurl, &$quiz,$quiz_qbanktool /** * Print a given single question in quiz for the reordertool tab of edit.php. * Meant to be used from quiz_print_question_list() - * + * * @param object $question A question object from the database questions table * @param object $questionurl The url of the question editing page as a moodle_url object * @param object $quiz The quiz in the context of which the question is being displayed */ -function quiz_print_singlequestion_reordertool($question, $returnurl, $quiz){ +function quiz_print_singlequestion_reordertool($question, $returnurl, $quiz) { echo '
'; echo '