From 39b3e7dedfb52de52f5358739ff8ce290e48c6d3 Mon Sep 17 00:00:00 2001 From: toyomoyo Date: Mon, 11 Sep 2006 07:17:10 +0000 Subject: [PATCH] adding capaility, fixing indenting and logic (could have broken things) --- mod/choice/lib.php | 336 +++++++++++++++++++++--------------------- mod/choice/report.php | 58 ++++---- 2 files changed, 194 insertions(+), 200 deletions(-) diff --git a/mod/choice/lib.php b/mod/choice/lib.php index fd9b01de14..c27604bd75 100644 --- a/mod/choice/lib.php +++ b/mod/choice/lib.php @@ -148,7 +148,7 @@ function choice_show_form($choice, $user, $cm) { //$cdisplay is an array of the display info for a choice $cdisplay[$optionid]->text - text name of option. // ->maxanswers -maxanswers for this option // ->full - whether this option is full or not. 0=not full, 1=full -$cdisplay = array(); + $cdisplay = array(); $aid = 0; foreach ($choice->option as $optionid => $text) { @@ -189,142 +189,141 @@ $cdisplay = array(); } } - switch ($choice->display) { - case CHOICE_DISPLAY_HORIZONTAL: - echo ""; + switch ($choice->display) { + case CHOICE_DISPLAY_HORIZONTAL: + echo "
"; - foreach ($cdisplay as $cd) { - echo ""; + foreach ($cdisplay as $cd) { + echo ""; - echo "
"; - echo "optionid."\" alt=\"".strip_tags(format_text($cd->text))."\"". $cd->checked.$cd->disabled." />"; - if (!empty($cd->disabled)) { - echo format_text($cd->text."
".get_string('full', 'choice').""); - } else { - echo format_text($cd->text); - } - echo "
"; + echo "optionid."\" alt=\"".strip_tags(format_text($cd->text))."\"". $cd->checked.$cd->disabled." />"; + if (!empty($cd->disabled)) { + echo format_text($cd->text."
".get_string('full', 'choice').""); + } else { + echo format_text($cd->text); } - echo "
"; - break; + echo ""; + } + echo ""; + echo ""; + break; - case CHOICE_DISPLAY_VERTICAL: - $displayoptions->para = false; - echo ""; - foreach ($cdisplay as $cd) { - echo ""; + echo ""; + } + echo "
"; - echo "optionid."\" alt=\"".strip_tags(format_text($cd->text))."\"". $cd->checked.$cd->disabled." />"; + case CHOICE_DISPLAY_VERTICAL: + $displayoptions->para = false; + echo ""; + foreach ($cdisplay as $cd) { + echo ""; - } else if ($choice->limitanswers && ($cd->countanswers >= $cd->maxanswers)) { //if limitanswers and answers exceeded, display "full" beside the choice. - echo " ".get_string('full', 'choice').""; - } - echo ""; - echo ""; - + } elseif ($cd->maxanswers-$cd->countanswers==1) { + echo ($cd->maxanswers - $cd->countanswers); + echo " ".get_string('spaceleft', 'choice'); + } else { + echo ($cd->maxanswers - $cd->countanswers); + echo " ".get_string('spacesleft', 'choice'); + } + echo ""; + } else if ($choice->limitanswers && ($cd->countanswers >= $cd->maxanswers)) { //if limitanswers and answers exceeded, display "full" beside the choice. + echo " ".get_string('full', 'choice').""; } - echo "
"; + echo "optionid."\" alt=\"".strip_tags(format_text($cd->text))."\"". $cd->checked.$cd->disabled." />"; - echo format_text($cd->text. ' ', FORMAT_MOODLE, $displayoptions); //display text for option. + echo format_text($cd->text. ' ', FORMAT_MOODLE, $displayoptions); //display text for option. - if ($choice->limitanswers && ($choice->showresults==CHOICE_SHOWRESULTS_ALWAYS) ){ //if limit is enabled, and show results always has been selected, display info beside each choice. - echo ""; + if ($choice->limitanswers && ($choice->showresults==CHOICE_SHOWRESULTS_ALWAYS) ){ //if limit is enabled, and show results always has been selected, display info beside each choice. + echo ""; - if (!empty($cd->disabled)) { - echo get_string('full', 'choice'); - } elseif(!empty($cd->checked)) { + if (!empty($cd->disabled)) { + echo get_string('full', 'choice'); + } elseif(!empty($cd->checked)) { //currently do nothing - maybe some text could be added here to signfy that the choice has been 'selected' - } elseif ($cd->maxanswers-$cd->countanswers==1) { - echo ($cd->maxanswers - $cd->countanswers); - echo " ".get_string('spaceleft', 'choice'); - } else { - echo ($cd->maxanswers - $cd->countanswers); - echo " ".get_string('spacesleft', 'choice'); - } - echo "
"; - break; - } - //show save choice button - echo "
"; - echo "id\" />"; - if (!isguest()) { //don't show save button if the logged in user is the guest user. - echo ""; - } else { - print_string('havetologin', 'choice'); - } - echo "
"; + echo "
"; + break; + } + //show save choice button + echo "
"; + echo "id\" />"; + if (!isguest()) { //don't show save button if the logged in user is the guest user. + echo ""; + } else { + print_string('havetologin', 'choice'); + } + echo "
"; } function choice_user_submit_response($formanswer, $choice, $userid, $courseid, $cm) { -$current = get_record('choice_answers', 'choiceid', $choice->id, 'userid', $userid); + $current = get_record('choice_answers', 'choiceid', $choice->id, 'userid', $userid); $context = get_context_instance(CONTEXT_MODULE, $cm->id); $countanswers = get_records("choice_answers", "optionid", $formanswer); - if ($countanswers) { - $countans = 0; - foreach ($countanswers as $ca) { //only return enrolled users. - if (has_capability('mod/choice:choose', $context)) { - $countans = $countans+1; - } - } + if ($countanswers) { + $countans = 0; + foreach ($countanswers as $ca) { //only return enrolled users. + if (has_capability('mod/choice:choose', $context)) { + $countans = $countans+1; + } + } - $countanswers = count($countans); - } else { - $countanswers = 0; - } - $maxans = $choice->maxanswers[$formanswer]; + $countanswers = count($countans); + } else { + $countanswers = 0; + } + $maxans = $choice->maxanswers[$formanswer]; - if (!($choice->limitanswers && ($countanswers >= $maxans) )) { - if ($current) { + if (!($choice->limitanswers && ($countanswers >= $maxans) )) { + if ($current) { - $newanswer = $current; - $newanswer->optionid = $formanswer; - $newanswer->timemodified = time(); - if (! update_record("choice_answers", $newanswer)) { - error("Could not update your choice because of a database error"); - } - add_to_log($courseid, "choice", "choose again", "view.php?id=$cm->id", $choice->id, $cm->id); - } else { - $newanswer = NULL; - $newanswer->choiceid = $choice->id; - $newanswer->userid = $userid; - $newanswer->optionid = $formanswer; - $newanswer->timemodified = time(); - if (! insert_record("choice_answers", $newanswer)) { - error("Could not save your choice"); - } - add_to_log($courseid, "choice", "choose", "view.php?id=$cm->id", $choice->id, $cm->id); - } - } else { - if (!($current->optionid==$formanswer)) { //check to see if current choice already selected - if not display error - error("this choice is full!"); - } + $newanswer = $current; + $newanswer->optionid = $formanswer; + $newanswer->timemodified = time(); + if (! update_record("choice_answers", $newanswer)) { + error("Could not update your choice because of a database error"); } + add_to_log($courseid, "choice", "choose again", "view.php?id=$cm->id", $choice->id, $cm->id); + } else { + $newanswer = NULL; + $newanswer->choiceid = $choice->id; + $newanswer->userid = $userid; + $newanswer->optionid = $formanswer; + $newanswer->timemodified = time(); + if (! insert_record("choice_answers", $newanswer)) { + error("Could not save your choice"); + } + add_to_log($courseid, "choice", "choose", "view.php?id=$cm->id", $choice->id, $cm->id); + } + } else { + if (!($current->optionid==$formanswer)) { //check to see if current choice already selected - if not display error + error("this choice is full!"); + } + } } function choice_show_reportlink($choice, $courseid, $cmid) { - $context = get_context_instance(CONTEXT_MODULE, $cmid); - if ( $allanswers = get_records("choice_answers", "choiceid", $choice->id)) { - $responsecount = 0; - foreach ($allanswers as $aa) { - if (has_capability('mod/choice:readresponses', $context)) { - $responsecount++; - } + $context = get_context_instance(CONTEXT_MODULE, $cmid); + if ( $allanswers = get_records("choice_answers", "choiceid", $choice->id)) { + $responsecount = 0; + foreach ($allanswers as $aa) { + if (has_capability('mod/choice:readresponses', $context)) { + $responsecount++; } - } else { - $responsecount = 0; } - echo ''; + } else { + $responsecount = 0; + } + echo ''; } function choice_show_results($choice, $course, $cm, $forcepublish='') { - global $CFG, $COLUMN_HEIGHT, $USER; - $context = get_context_instance(CONTEXT_MODULE, $cm->id); - print_heading(get_string("responses", "choice")); - if (empty($forcepublish)) { //alow the publish setting to be overridden - $forcepublish = $choice->publish; - } + global $CFG, $COLUMN_HEIGHT, $USER; + $context = get_context_instance(CONTEXT_MODULE, $cm->id); + print_heading(get_string("responses", "choice")); + if (empty($forcepublish)) { //alow the publish setting to be overridden + $forcepublish = $choice->publish; + } /// Check to see if groups are being used in this choice if ($groupmode = groupmode($course, $cm)) { // Groups are being used @@ -333,49 +332,48 @@ function choice_show_results($choice, $course, $cm, $forcepublish='') { $currentgroup = false; } - if ($currentgroup) { - $users = get_group_users($currentgroup, "u.firstname ASC", '', 'u.id, u.picture, u.firstname, u.lastname, u.idnumber'); - } else { - $users = get_course_users($course->id, "u.firstname ASC", '', 'u.id, u.picture, u.firstname, u.lastname, u.idnumber') + get_admins(); - } - + if ($currentgroup) { + $users = get_group_users($currentgroup, "u.firstname ASC", '', 'u.id, u.picture, u.firstname, u.lastname, u.idnumber'); + } else { + $users = get_course_users($course->id, "u.firstname ASC", '', 'u.id, u.picture, u.firstname, u.lastname, u.idnumber') + get_admins(); + } - if (!$users) { - print_heading(get_string("nousersyet")); - } + if (!$users) { + print_heading(get_string("nousersyet")); + } - if ($allresponses = get_records("choice_answers", "choiceid", $choice->id)) { - foreach ($allresponses as $aa) { - $answers[$aa->userid] = $aa; - } - } else { - $answers = array () ; + if ($allresponses = get_records("choice_answers", "choiceid", $choice->id)) { + foreach ($allresponses as $aa) { + $answers[$aa->userid] = $aa; } + } else { + $answers = array () ; + } - $timenow = time(); + $timenow = time(); - foreach ($choice->option as $optionid => $text) { - $useranswer[$optionid] = array(); - } - if (!empty($users)) { - foreach ($users as $user) { - if (!empty($user->id) and !empty($answers[$user->id])) { - $answer = $answers[$user->id]; - $useranswer[(int)$answer->optionid][] = $user; - } else { - $useranswer[0][] = $user; - } + foreach ($choice->option as $optionid => $text) { + $useranswer[$optionid] = array(); + } + if (!empty($users)) { + foreach ($users as $user) { + if (!empty($user->id) and !empty($answers[$user->id])) { + $answer = $answers[$user->id]; + $useranswer[(int)$answer->optionid][] = $user; + } else { + $useranswer[0][] = $user; } } - foreach ($choice->option as $optionid => $text) { - if (!$choice->option[$optionid]) { - unset($useranswer[$optionid]); // Throw away any data that doesn't apply - } + } + foreach ($choice->option as $optionid => $text) { + if (!$choice->option[$optionid]) { + unset($useranswer[$optionid]); // Throw away any data that doesn't apply } - ksort($useranswer); + } + ksort($useranswer); - switch ($forcepublish) { - case CHOICE_PUBLISH_NAMES: + switch ($forcepublish) { + case CHOICE_PUBLISH_NAMES: $tablewidth = (int) (100.0 / count($useranswer)); if (has_capability('mod/choice:readresponses', $context)) { @@ -415,16 +413,18 @@ function choice_show_results($choice, $course, $cm, $forcepublish='') { echo ""; foreach ($userlist as $user) { // this needs to be fixed - if (!($optionid==0 && isadmin($user->id)) && !($optionid==0 && isteacher($course->id, $user->id) && !(isteacheredit($course->id, $user->id)) ) ) { // make sure admins and hidden teachers are not shown in not answered yet column. + // hide admin/editting teacher (users with editting privilages) + // show users without? I could be wrong. + if (!($optionid==0 && has_capability('mod/choice:readresponses', $context, $user->id))) { // make sure admins and hidden teachers are not shown in not answered yet column. echo ""; - if (isteacher($course->id, $USER->id) && !($optionid==0)) { + if (has_capability('mod/choice:readresponses', $context) && $optionid!=0) { echo ''; } echo ""; } @@ -462,23 +462,21 @@ function choice_show_results($choice, $course, $cm, $forcepublish='') { $count++; } - /// Print "Select all" etc. - if (has_capability('mod/choice:readresponses', $context)) { - echo ''; - } - - - + /// Print "Select all" etc. + if (has_capability('mod/choice:readresponses', $context)) { + echo ''; + } + echo "
"; print_user_picture($user->id, $course->id, $user->picture); echo ""; echo "wwwroot/user/view.php?id=$user->id&course=$course->id\">"; - echo fullname($user, $isteacher); + echo fullname($user, has_capability('moodle/site:viewfullnames', $context)); echo ""; echo "

'; - echo '

'; - echo ''.get_string('selectall', 'quiz').' / '; - echo ''.get_string('selectnone', 'quiz').' '; - echo '  '; - $options = array('delete' => get_string('delete')); - echo choose_from_menu($options, 'action', '', get_string('withselected', 'quiz'), 'if(this.selectedIndex > 0) submitFormById(\'attemptsform\');', '', true); - echo ''; - echo ''; - echo '

'; + echo '

'; + echo ''.get_string('selectall', 'quiz').' / '; + echo ''.get_string('selectnone', 'quiz').' '; + echo '  '; + $options = array('delete' => get_string('delete')); + echo choose_from_menu($options, 'action', '', get_string('withselected', 'quiz'), 'if(this.selectedIndex > 0) submitFormById(\'attemptsform\');', '', true); + echo ''; + echo ''; + echo '

"; if (has_capability('mod/choice:readresponses', $context)) { echo ""; @@ -486,7 +484,7 @@ function choice_show_results($choice, $course, $cm, $forcepublish='') { break; - case CHOICE_PUBLISH_ANONYMOUS: + case CHOICE_PUBLISH_ANONYMOUS: $tablewidth = (int) (100.0 / count($useranswer)); @@ -514,7 +512,7 @@ function choice_show_results($choice, $course, $cm, $forcepublish='') { } $column[$optionid] = 0; foreach ($userlist as $user) { - if (!($optionid==0 && isadmin($user->id)) && !($optionid==0 && isteacher($course->id, $user->id) && !(isteacheredit($course->id, $user->id)) ) ) { //make sure admins and hidden teachers are not shown in not answered yet column. + if (!($optionid==0 && has_capability('mod/choice:readresponses', $context, $user->id))) { //make sure admins and hidden teachers are not shown in not answered yet column. $column[$optionid]++; } } @@ -562,8 +560,8 @@ function choice_show_results($choice, $course, $cm, $forcepublish='') { } echo ""; - break; - } + break; + } } @@ -609,13 +607,11 @@ function choice_delete_instance($id) { if (! delete_records("choice", "id", "$choice->id")) { $result = false; - } - + } return $result; } - function choice_get_participants($choiceid) { //Returns the users with data in one choice //(users with records in choice_responses, students) diff --git a/mod/choice/report.php b/mod/choice/report.php index 0da20cd43b..0e413d1af4 100644 --- a/mod/choice/report.php +++ b/mod/choice/report.php @@ -107,37 +107,39 @@ if ($users) { foreach ($users as $user) { // this needs fixing - if (!empty($answers[$user->id]) && !($answers[$user->id]->optionid==0 && isadmin($user->id)) && - (!($answers[$user->id]->optionid==0 && isteacher($course->id, $user->id) && !(isteacheredit($course->id, $user->id)) ) ) && - !($choice->showunanswered==0 && $answers[$user->id]->optionid==0) ) { // make sure admins and hidden teachers are not shown in not answered yet column, and not answered only shown if set in config page. - - $myxls->write_string($row,0,$user->lastname); - $myxls->write_string($row,1,$user->firstname); - $studentid=(!empty($user->idnumber) ? $user->idnumber : " "); - $myxls->write_string($row,2,$studentid); - $ug2 = ''; - if ($usergrps = user_group($course->id, $user->id)) { - foreach ($usergrps as $ug) { - $ug2 = $ug2. $ug->name; + + if (!($optionid==0 && has_capability('mod/choice:readresponses', $context, $user->id))) { + + if (!empty($answers[$user->id]) && !($answers[$user->id]->optionid==0 && has_capability('mod/choice:readresponses', $context, $user->id) && $choice->showunanswered==0)) { // make sure admins and hidden teachers are not shown in not answered yet column, and not answered only shown if set in config page. + + $myxls->write_string($row,0,$user->lastname); + $myxls->write_string($row,1,$user->firstname); + $studentid=(!empty($user->idnumber) ? $user->idnumber : " "); + $myxls->write_string($row,2,$studentid); + $ug2 = ''; + if ($usergrps = user_group($course->id, $user->id)) { + foreach ($usergrps as $ug) { + $ug2 = $ug2. $ug->name; + } } - } - $myxls->write_string($row,3,$ug2); + $myxls->write_string($row,3,$ug2); - $useroption = choice_get_option_text($choice, $answers[$user->id]->optionid); - if (isset($useroption)) { - $myxls->write_string($row,4,format_string($useroption,true)); - } - $row++; + $useroption = choice_get_option_text($choice, $answers[$user->id]->optionid); + if (isset($useroption)) { + $myxls->write_string($row,4,format_string($useroption,true)); + } + $row++; + } + $pos=4; } - $pos=4; } - } /// Close the workbook - $workbook->close(); + $workbook->close(); - exit; - } + exit; + } + } // print text file if ($download == "txt" && has_capability('mod/choice:downloadresponses', $context)) { $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name,true))).'.txt'; @@ -158,9 +160,7 @@ $i=0; $row=1; if ($users) foreach ($users as $user) { - if (!empty($answers[$user->id]) && !($answers[$user->id]->optionid==0 && isadmin($user->id)) && - (!($answers[$user->id]->optionid==0 && isteacher($course->id, $user->id) && !(isteacheredit($course->id, $user->id)) ) ) && - !($choice->showunanswered==0 && $answers[$user->id]->optionid==0) ) { //make sure admins and hidden teachers are not shown in not answered yet column, and not answered only shown if set in config page. + if (!empty($answers[$user->id]) && !($answers[$user->id]->optionid==0 && has_capability('mod/choice:readresponses', $context, $user->id) && $choice->showunanswered==0)) { // make sure admins and hidden teachers are not shown in not answered yet column, and not answered only shown if set in config page. echo $user->lastname; echo "\t".$user->firstname; @@ -183,7 +183,6 @@ exit; } - choice_show_results($choice, $course, $cm, $format); //show table with students responses. //now give links for downloading spreadsheets. @@ -199,7 +198,6 @@ print_single_button("report.php", $options, get_string("downloadtext")); echo ""; -print_footer($course); - + print_footer($course); ?> -- 2.39.5