Exemplo n.º 1
0
 /**
  * Returns user's results for a specific choice
  * and a list of those users that did not answered yet.
  *
  * @param int $choiceid the choice instance id
  * @return array of responses details
  * @since Moodle 3.0
  */
 public static function get_choice_results($choiceid)
 {
     global $USER, $PAGE;
     $params = self::validate_parameters(self::get_choice_results_parameters(), array('choiceid' => $choiceid));
     if (!($choice = choice_get_choice($params['choiceid']))) {
         throw new moodle_exception("invalidcoursemodule", "error");
     }
     list($course, $cm) = get_course_and_cm_from_instance($choice, 'choice');
     $context = context_module::instance($cm->id);
     self::validate_context($context);
     $groupmode = groups_get_activity_groupmode($cm);
     // Check if we have to include responses from inactive users.
     $onlyactive = $choice->includeinactive ? false : true;
     $users = choice_get_response_data($choice, $cm, $groupmode, $onlyactive);
     // Show those who haven't answered the question.
     if (!empty($choice->showunanswered)) {
         $choice->option[0] = get_string('notanswered', 'choice');
         $choice->maxanswers[0] = 0;
     }
     $results = prepare_choice_show_results($choice, $course, $cm, $users);
     $options = array();
     $fullnamecap = has_capability('moodle/site:viewfullnames', $context);
     foreach ($results->options as $optionid => $option) {
         $userresponses = array();
         $numberofuser = 0;
         $percentageamount = 0;
         if (property_exists($option, 'user') and (has_capability('mod/choice:readresponses', $context) or choice_can_view_results($choice))) {
             $numberofuser = count($option->user);
             $percentageamount = (double) $numberofuser / (double) $results->numberofuser * 100.0;
             if ($choice->publish) {
                 foreach ($option->user as $userresponse) {
                     $response = array();
                     $response['userid'] = $userresponse->id;
                     $response['fullname'] = fullname($userresponse, $fullnamecap);
                     $userpicture = new user_picture($userresponse);
                     $userpicture->size = 1;
                     // Size f1.
                     $response['profileimageurl'] = $userpicture->get_url($PAGE)->out(false);
                     // Add optional properties.
                     foreach (array('answerid', 'timemodified') as $field) {
                         if (property_exists($userresponse, 'answerid')) {
                             $response[$field] = $userresponse->{$field};
                         }
                     }
                     $userresponses[] = $response;
                 }
             }
         }
         $options[] = array('id' => $optionid, 'text' => external_format_string($option->text, $context->id), 'maxanswer' => $option->maxanswer, 'userresponses' => $userresponses, 'numberofuser' => $numberofuser, 'percentageamount' => $percentageamount);
     }
     $warnings = array();
     return array('options' => $options, 'warnings' => $warnings);
 }
Exemplo n.º 2
0
    } else {
        if (!is_enrolled($context)) {
            // Only people enrolled can make a choice
            $SESSION->wantsurl = qualified_me();
            $SESSION->enrolcancel = get_local_referer(false);
            $coursecontext = context_course::instance($course->id);
            $courseshortname = format_string($course->shortname, true, array('context' => $coursecontext));
            echo $OUTPUT->box_start('generalbox', 'notice');
            echo '<p align="center">' . get_string('notenrolledchoose', 'choice') . '</p>';
            echo $OUTPUT->container_start('continuebutton');
            echo $OUTPUT->single_button(new moodle_url('/enrol/index.php?', array('id' => $course->id)), get_string('enrolme', 'core_enrol', $courseshortname));
            echo $OUTPUT->container_end();
            echo $OUTPUT->box_end();
        }
    }
}
// print the results at the bottom of the screen
if (choice_can_view_results($choice, $current, $choiceopen)) {
    if (!empty($choice->showunanswered)) {
        $choice->option[0] = get_string('notanswered', 'choice');
        $choice->maxanswers[0] = 0;
    }
    $results = prepare_choice_show_results($choice, $course, $cm, $allresponses);
    $renderer = $PAGE->get_renderer('mod_choice');
    echo $renderer->display_result($results);
} else {
    if (!$choiceformshown) {
        echo $OUTPUT->box(get_string('noresultsviewable', 'choice'));
    }
}
echo $OUTPUT->footer();
Exemplo n.º 3
0
 /**
  * Test choice_can_view_results
  * @return void
  */
 public function test_choice_can_view_results()
 {
     global $DB, $USER;
     $this->resetAfterTest();
     $this->setAdminUser();
     // Setup test data.
     $course = $this->getDataGenerator()->create_course();
     $choice = $this->getDataGenerator()->create_module('choice', array('course' => $course->id));
     $context = context_module::instance($choice->cmid);
     $cm = get_coursemodule_from_instance('choice', $choice->id);
     // Default values are false, user cannot view results.
     $canview = choice_can_view_results($choice);
     $this->assertFalse($canview);
     // Show results forced.
     $choice->showresults = CHOICE_SHOWRESULTS_ALWAYS;
     $DB->update_record('choice', $choice);
     $canview = choice_can_view_results($choice);
     $this->assertTrue($canview);
     // Show results after closing.
     $choice->showresults = CHOICE_SHOWRESULTS_AFTER_CLOSE;
     $DB->update_record('choice', $choice);
     $canview = choice_can_view_results($choice);
     $this->assertFalse($canview);
     $choice->timeclose = time() - HOURSECS;
     $DB->update_record('choice', $choice);
     $canview = choice_can_view_results($choice);
     $this->assertTrue($canview);
     // Show results after answering.
     $choice->timeclose = 0;
     $choice->showresults = CHOICE_SHOWRESULTS_AFTER_ANSWER;
     $DB->update_record('choice', $choice);
     $canview = choice_can_view_results($choice);
     $this->assertFalse($canview);
     // Get the first option.
     $choicewithoptions = choice_get_choice($choice->id);
     $optionids = array_keys($choicewithoptions->option);
     choice_user_submit_response($optionids[0], $choice, $USER->id, $course, $cm);
     $canview = choice_can_view_results($choice);
     $this->assertTrue($canview);
 }
Exemplo n.º 4
0
Arquivo: lib.php Projeto: dg711/moodle
/**
 * Check if the module has any update that affects the current user since a given time.
 *
 * @param  cm_info $cm course module data
 * @param  int $from the time to check updates from
 * @param  array $filter  if we need to check only specific updates
 * @return stdClass an object with the different type of areas indicating if they were updated or not
 * @since Moodle 3.2
 */
function choice_check_updates_since(cm_info $cm, $from, $filter = array())
{
    global $DB;
    $updates = new stdClass();
    $choice = $DB->get_record($cm->modname, array('id' => $cm->instance), '*', MUST_EXIST);
    list($available, $warnings) = choice_get_availability_status($choice);
    if (!$available) {
        return $updates;
    }
    $updates = course_check_module_updates_since($cm, $from, array(), $filter);
    if (!choice_can_view_results($choice)) {
        return $updates;
    }
    // Check if there are new responses in the choice.
    $updates->answers = (object) array('updated' => false);
    $select = 'choiceid = :id AND timemodified > :since';
    $params = array('id' => $choice->id, 'since' => $from);
    $answers = $DB->get_records_select('choice_answers', $select, $params, '', 'id');
    if (!empty($answers)) {
        $updates->answers->updated = true;
        $updates->answers->itemids = array_keys($answers);
    }
    return $updates;
}