function jclic_get_students($cm, $course, $jclicid)
{
    global $CFG;
    $version_moodle = (double) substr($CFG->release, 0, 3);
    if ($version_moodle >= 1.7) {
        $context = get_context_instance(CONTEXT_MODULE, $cm->id);
        $currentgroup = get_and_set_current_group($course, groupmode($course, $cm));
        $users = get_users_by_capability($context, 'mod/jclic:submit', 'u.id, u.id', '', '', '', $currentgroup, '', false);
        $dbstudents = array();
        if (!empty($users)) {
            $select = 'SELECT u.id AS userid, u.firstname, u.lastname, u.picture ';
            $sql = 'FROM ' . $CFG->prefix . 'user u ' . 'LEFT JOIN ' . $CFG->prefix . 'jclic_sessions a ON u.id = a.user_id AND a.jclicid = ' . $jclicid . ' ' . 'WHERE u.id IN (' . implode(',', array_keys($users)) . ') ';
            $dbstudents = get_records_sql($select . $sql);
        }
    } else {
        $dbstudents = get_records_sql("SELECT DISTINCT us.userid, u.firstname, u.lastname\n  \t\t\t\t       FROM {$CFG->prefix}user u,{$CFG->prefix}user_students us, {$CFG->prefix}jclic j\n  \t\t\t\t       WHERE us.course=j.course AND j.id={$jclicid} AND u.id=us.userid");
    }
    return $dbstudents;
}
Beispiel #2
0
 }
 // call course_setup to use forced language, MDL-6926
 course_setup($course->id);
 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
 if (!forum_user_can_post($forum)) {
     if (has_capability('moodle/legacy:guest', $coursecontext, NULL, false)) {
         // User is a guest here!
         $SESSION->wantsurl = $FULLME;
         $SESSION->enrolcancel = $_SERVER['HTTP_REFERER'];
         redirect($CFG->wwwroot . '/course/enrol.php?id=' . $course->id, get_string('youneedtoenrol'));
     } else {
         print_error('nopostforum', 'forum');
     }
 }
 if (groupmode($course, $cm)) {
     // Make sure user can post here
     $mygroupid = mygroupid($course->id);
     if (!((empty($mygroupid) and $discussion->groupid == -1) || ismember($discussion->groupid) || has_capability('moodle/site:accessallgroups', $modcontext, NULL, false))) {
         print_error('nopostdiscussion', 'forum');
     }
 }
 if (!$cm->visible and !has_capability('moodle/course:manageactivities', $coursecontext)) {
     error(get_string("activityiscurrentlyhidden"));
 }
 // Load up the $post variable.
 $post = new object();
 $post->course = $course->id;
 $post->forum = $forum->id;
 $post->discussion = $parent->discussion;
 $post->parent = $parent->id;
/**
 * Return a list of teachers that the current user is able to open a dialogue with
 * 
 * Called by dialogue_get_available_users(). The list is used to populate a drop-down
 * list in the UI. The returned array of usernames is filtered to hide teacher names
 * if those teachers have a hidden role assignment, unless the list is being returned
 * for a teacher in which case those hidden teachers are listed
 * @param   object  $dialogue
 * @param   object  $context    for a user in this activity
 * @param   int     $editconversationid
 * @return  array   usernames and ids
 */
function dialogue_get_available_teachers($dialogue, $context, $editconversationid = 0)
{
    global $USER, $CFG;
    $canseehidden = has_capability('moodle/role:viewhiddenassigns', $context);
    if (!($course = get_record('course', 'id', $dialogue->course))) {
        error('Course is misconfigured');
    }
    if (!($cm = get_coursemodule_from_instance('dialogue', $dialogue->id, $course->id))) {
        error('Course Module ID was incorrect');
    }
    // get the list of teachers (actually, those who have dialogue:manage capability)
    $hiddenTeachers = array();
    if ($users = get_users_by_capability($context, 'mod/dialogue:manage', '', null, null, null, null, null, null, true, null)) {
        foreach ($users as $user) {
            $userRoles = get_user_roles($context, $user->id, true);
            foreach ($userRoles as $role) {
                if ($role->hidden == 1) {
                    $hiddenTeachers[$user->id] = 1;
                    break;
                }
            }
        }
        $canSeeHidden = false;
        if (has_capability('moodle/role:viewhiddenassigns', $context)) {
            $canSeeHidden = true;
        }
        $groupid = get_current_group($course->id);
        foreach ($users as $otheruser) {
            // ...exclude self and ...
            if ($USER->id != $otheruser->id) {
                // ...if groupmode is SEPARATEGROUPS then exclude teachers not in student's group
                if ($groupid and groupmode($course, $cm) == SEPARATEGROUPS) {
                    if (!ismember($groupid, $otheruser->id)) {
                        continue;
                    }
                }
                if (!$canSeeHidden && array_key_exists($otheruser->id, $hiddenTeachers) && $hiddenTeachers[$otheruser->id] == 1) {
                    continue;
                }
                // ...any already in open conversations unless multiple conversations allowed
                if ($dialogue->multipleconversations or count_records_select('dialogue_conversations', "dialogueid = {$dialogue->id} AND id != {$editconversationid} AND ((userid = {$USER->id} AND \n                        recipientid = {$otheruser->id}) OR (userid = {$otheruser->id} AND \n                        recipientid = {$USER->id})) AND closed = 0") == 0) {
                    $names[$otheruser->id] = fullname($otheruser);
                }
            }
        }
    }
    if (isset($names)) {
        natcasesort($names);
        return $names;
    }
    return;
}
Beispiel #4
0
function feedback_email_teachers($cm, $feedback, $course, $userid)
{
    global $CFG;
    if ($feedback->email_notification == 0) {
        // No need to do anything
        return;
    }
    $user = get_record('user', 'id', $userid);
    if (groupmode($course, $cm) == SEPARATEGROUPS) {
        // Separate groups are being used
        if (!($group = user_group($course->id, $user->id))) {
            // Try to find a group
            $group->id = 0;
            // Not in a group, never mind
        }
        $teachers = get_group_teachers($course->id, $group->id);
        // Works even if not in group
    } else {
        $teachers = get_course_teachers($course->id);
    }
    if ($teachers) {
        $strfeedbacks = get_string('modulenameplural', 'feedback');
        $strfeedback = get_string('modulename', 'feedback');
        $strcompleted = get_string('completed', 'feedback');
        foreach ($teachers as $teacher) {
            unset($info);
            $info->username = fullname($user);
            $info->feedback = format_string($feedback->name, true);
            $info->url = $CFG->wwwroot . '/mod/feedback/show_entries.php?id=' . $cm->id . '&userid=' . $userid;
            $postsubject = $strcompleted . ': ' . $info->username . ' -> ' . $feedback->name;
            $posttext = feedback_email_teachers_text($info, $course);
            $posthtml = $teacher->mailformat == 1 ? feedback_email_teachers_html($info, $course, $cm) : '';
            @email_to_user($teacher, $user, $postsubject, $posttext, $posthtml);
        }
    }
}
 /**
  * Display the report.
  */
 public function display($game, $cm, $course)
 {
     global $CFG, $SESSION, $DB;
     // Define some strings.
     $strreallydel = addslashes(get_string('deleteattemptcheck', 'game'));
     $strtimeformat = get_string('strftimedatetime');
     $strreviewquestion = get_string('reviewresponse', 'quiz');
     // Only print headers if not asked to download data.
     if (!($download = optional_param('download', null))) {
         $this->print_header_and_tabs($cm, $course, $game, $reportmode = "overview");
     }
     // Deal with actions.
     $action = optional_param('action', '', PARAM_ACTION);
     switch ($action) {
         case 'delete':
             // Some attempts need to be deleted.
             $attemptids = optional_param('attemptid', array(), PARAM_INT);
             foreach ($attemptids as $attemptid) {
                 if ($attemptid && ($todelete = get_record('game_attempts', 'id', $attemptid))) {
                     delete_records('game_attempts', 'id', $attemptid);
                     delete_records('game_queries', 'attemptid', $attemptid);
                     // Search game_attempts for other instances by this user.
                     // If none, then delete record for this game, this user from game_grades.
                     // else recalculate best grade.
                     $userid = $todelete->userid;
                     if (!record_exists('game_attempts', 'userid', $userid, 'gameid', $game->id)) {
                         delete_records('game_grades', 'userid', $userid, 'gameid', $game->id);
                     } else {
                         game_save_best_score($game, $userid);
                     }
                 }
             }
             break;
     }
     // Print information on the number of existing attempts.
     if (!$download) {
         // Do not print notices when downloading.
         if ($attemptnum = count_records('game_attempts', 'gameid', $game->id)) {
             $a = new stdClass();
             $a->attemptnum = $attemptnum;
             $a->studentnum = count_records_select('game_attempts', "gameid = '{$game->id}' AND preview = '0'", 'COUNT(DISTINCT userid)');
             $a->studentstring = $course->students;
             notify(get_string('numattempts', 'game', $a));
         }
     }
     $context = get_context_instance(CONTEXT_MODULE, $cm->id);
     // Find out current groups mode.
     if ($groupmode = groupmode($course, $cm)) {
         // Groups are being used.
         if (!$download) {
             $currentgroup = setup_and_print_groups($course, $groupmode, "report.php?id={$cm->id}&mode=overview");
         } else {
             $currentgroup = get_and_set_current_group($course, $groupmode);
         }
     } else {
         $currentgroup = get_and_set_current_group($course, $groupmode);
     }
     // Set table options.
     $noattempts = optional_param('noattempts', 0, PARAM_INT);
     $detailedmarks = optional_param('detailedmarks', 0, PARAM_INT);
     $pagesize = optional_param('pagesize', 10, PARAM_INT);
     $hasfeedback = game_has_feedback($game->id) && $game->grade > 1.0E-7;
     if ($pagesize < 1) {
         $pagesize = 10;
     }
     // Now check if asked download of data.
     if ($download) {
         $filename = clean_filename("{$course->shortname} " . format_string($game->name, true));
         $sort = '';
     }
     // Define table columns.
     $tablecolumns = array('checkbox', 'picture', 'fullname', 'timestart', 'timefinish', 'duration');
     $tableheaders = array(null, '', get_string('fullname'), get_string('startedon', 'game'), get_string('timecompleted', 'game'), get_string('attemptduration', 'game'));
     if ($game->grade) {
         $tablecolumns[] = 'grade';
         $tableheaders[] = get_string('grade', 'game') . '/' . $game->grade;
     }
     if ($detailedmarks) {
         // We want to display marks for all questions.
         // Start by getting all questions.
         $questionlist = game_questions_in_game($game->questions);
         $questionids = explode(',', $questionlist);
         $sql = "SELECT q.*, i.score AS maxgrade, i.id AS instance" . "  FROM {question} q," . "       {game_queries} i" . " WHERE i.gameid = '{$game->id}' AND q.id = i.questionid" . "   AND q.id IN ({$questionlist})";
         if (!($questions = get_records_sql($sql))) {
             print_error('No questions found');
         }
         $number = 1;
         foreach ($questionids as $key => $id) {
             if ($questions[$id]->length) {
                 // Only print questions of non-zero length.
                 $tablecolumns[] = '$' . $id;
                 $tableheaders[] = '#' . $number;
                 $questions[$id]->number = $number;
                 $number += $questions[$id]->length;
             } else {
                 // Get rid of zero length questions.
                 unset($questions[$id]);
                 unset($questionids[$key]);
             }
         }
     }
     if ($hasfeedback) {
         $tablecolumns[] = 'feedbacktext';
         $tableheaders[] = get_string('feedback', 'game');
     }
     if (!$download) {
         // Set up the table.
         $table = new flexible_table('mod-game-report-overview-report');
         $table->define_columns($tablecolumns);
         $table->define_headers($tableheaders);
         $table->define_baseurl($CFG->wwwroot . '/mod/game/report.php?mode=overview&amp;id=' . $cm->id . '&amp;noattempts=' . $noattempts . '&amp;detailedmarks=' . $detailedmarks . '&amp;pagesize=' . $pagesize);
         $table->sortable(true);
         $table->collapsible(true);
         $table->column_suppress('picture');
         $table->column_suppress('fullname');
         $table->column_class('picture', 'picture');
         $table->set_attribute('cellspacing', '0');
         $table->set_attribute('id', 'attempts');
         $table->set_attribute('class', 'generaltable generalbox');
         // Start working -- this is necessary as soon as the niceties are over.
         $table->setup();
     } else {
         if ($download == 'ODS') {
             require_once "{$CFG->libdir}/odslib.class.php";
             $filename .= ".ods";
             // Creating a workbook.
             $workbook = new MoodleODSWorkbook("-");
             // Sending HTTP headers.
             $workbook->send($filename);
             // Creating the first worksheet.
             $sheettitle = get_string('reportoverview', 'game');
             $myxls =& $workbook->add_worksheet($sheettitle);
             // Format types.
             $format =& $workbook->add_format();
             $format->set_bold(0);
             $formatbc =& $workbook->add_format();
             $formatbc->set_bold(1);
             $formatbc->set_align('center');
             $formatb =& $workbook->add_format();
             $formatb->set_bold(1);
             $formaty =& $workbook->add_format();
             $formaty->set_bg_color('yellow');
             $formatc =& $workbook->add_format();
             $formatc->set_align('center');
             $formatr =& $workbook->add_format();
             $formatr->set_bold(1);
             $formatr->set_color('red');
             $formatr->set_align('center');
             $formatg =& $workbook->add_format();
             $formatg->set_bold(1);
             $formatg->set_color('green');
             $formatg->set_align('center');
             // Here starts workshhet headers.
             $headers = array(get_string('fullname'), get_string('startedon', 'game'), get_string('timecompleted', 'game'), get_string('attemptduration', 'game'));
             if ($game->grade) {
                 $headers[] = get_string('grade', 'game') . '/' . $game->grade;
             }
             if ($detailedmarks) {
                 foreach ($questionids as $id) {
                     $headers[] = '#' . $questions[$id]->number;
                 }
             }
             if ($hasfeedback) {
                 $headers[] = get_string('feedback', 'game');
             }
             $colnum = 0;
             foreach ($headers as $item) {
                 $myxls->write(0, $colnum, $item, $formatbc);
                 $colnum++;
             }
             $rownum = 1;
         } else {
             if ($download == 'Excel') {
                 require_once "{$CFG->libdir}/excellib.class.php";
                 $filename .= ".xls";
                 // Creating a workbook.
                 $workbook = new MoodleExcelWorkbook("-");
                 // Sending HTTP headers.
                 $workbook->send($filename);
                 // Creating the first worksheet.
                 $sheettitle = get_string('reportoverview', 'game');
                 $myxls =& $workbook->add_worksheet($sheettitle);
                 // Format types.
                 $format =& $workbook->add_format();
                 $format->set_bold(0);
                 $formatbc =& $workbook->add_format();
                 $formatbc->set_bold(1);
                 $formatbc->set_align('center');
                 $formatb =& $workbook->add_format();
                 $formatb->set_bold(1);
                 $formaty =& $workbook->add_format();
                 $formaty->set_bg_color('yellow');
                 $formatc =& $workbook->add_format();
                 $formatc->set_align('center');
                 $formatr =& $workbook->add_format();
                 $formatr->set_bold(1);
                 $formatr->set_color('red');
                 $formatr->set_align('center');
                 $formatg =& $workbook->add_format();
                 $formatg->set_bold(1);
                 $formatg->set_color('green');
                 $formatg->set_align('center');
                 // Here starts workshhet headers.
                 $headers = array(get_string('fullname'), get_string('startedon', 'game'), get_string('timecompleted', 'game'), get_string('attemptduration', 'game'));
                 if ($game->grade) {
                     $headers[] = get_string('grade', 'game') . '/' . $game->grade;
                 }
                 if ($detailedmarks) {
                     foreach ($questionids as $id) {
                         $headers[] = '#' . $questions[$id]->number;
                     }
                 }
                 if ($hasfeedback) {
                     $headers[] = get_string('feedback', 'game');
                 }
                 $colnum = 0;
                 foreach ($headers as $item) {
                     $myxls->write(0, $colnum, $item, $formatbc);
                     $colnum++;
                 }
                 $rownum = 1;
             } else {
                 if ($download == 'CSV') {
                     $filename .= ".txt";
                     header("Content-Type: application/download\n");
                     header("Content-Disposition: attachment; filename=\"{$filename}\"");
                     header("Expires: 0");
                     header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
                     header("Pragma: public");
                     $headers = get_string('fullname') . "\t" . get_string('startedon', 'game') . "\t" . get_string('timecompleted', 'game') . "\t" . get_string('attemptduration', 'game');
                     if ($game->grade) {
                         $headers .= "\t" . get_string('grade', 'game') . "/" . $game->grade;
                     }
                     if ($detailedmarks) {
                         foreach ($questionids as $id) {
                             $headers .= "\t#" . $questions[$id]->number;
                         }
                     }
                     if ($hasfeedback) {
                         $headers .= "\t" . get_string('feedback', 'game');
                     }
                     echo $headers . " \n";
                 }
             }
         }
     }
     $contextlists = get_related_contexts_string(get_context_instance(CONTEXT_COURSE, $course->id));
     // Construct the SQL.
     $select = 'SELECT qa.id,' . sql_concat('u.id', '\'#\'', $db->IfNull('qa.attempt', '0')) . ' AS uniqueid, ' . 'qa.id as attemptuniqueid, qa.id AS attempt, u.id AS userid, u.firstname, u.lastname, u.picture, ' . 'qa.score, qa.timefinish, qa.timestart, qa.timefinish - qa.timestart AS duration ';
     if ($course->id != SITEID) {
         // This is too complicated, so just do it for each of the four cases.
         if (!empty($currentgroup) && empty($noattempts)) {
             // We want a particular group and we only want to see students WITH attempts.
             // So join on groups_members and do an inner join on attempts.
             $from = 'FROM {user} u JOIN {role_assignments} ra ON ra.userid = u.id ' . groups_members_join_sql() . 'JOIN {game_attempts} qa ON u.id = qa.userid AND qa.gameid = ' . $game->id;
             $where = ' WHERE ra.contextid ' . $contextlists . ' AND ' . groups_members_where_sql($currentgroup) . ' AND qa.preview = 0';
         } else {
             if (!empty($currentgroup) && !empty($noattempts)) {
                 // We want a particular group and we want to do something funky with attempts.
                 // So join on groups_members and left join on attempts...
                 $from = 'FROM {user} u JOIN {role_assignments} ra ON ra.userid = u.id ' . groups_members_join_sql() . 'LEFT JOIN {game_attempts} qa ON u.id = qa.userid AND qa.gameid = ' . $game->id;
                 $where = ' WHERE ra.contextid ' . $contextlists . ' AND ' . groups_members_where_sql($currentgroup);
                 if ($noattempts == 1) {
                     // Noattempts = 1 means only no attempts, so make the left join ask.
                     // For only records where the right is null (no attempts).
                     $where .= ' AND qa.userid IS NULL';
                     // Show ONLY no attempts.
                 } else {
                     // We are including attempts, so exclude previews.
                     $where .= ' AND qa.preview = 0';
                 }
             } else {
                 if (empty($currentgroup)) {
                     // We don't care about group, and we to do something funky with attempts.
                     // So do a left join on attempts.
                     $from = 'FROM {user} u JOIN {role_assignments} ra ON ra.userid = u.id ' . ' LEFT JOIN {game_attempts} qa ON u.id = qa.userid AND qa.gameid = ' . $game->id;
                     $where = " WHERE ra.contextid {$contextlists}";
                     if (empty($noattempts)) {
                         // Show ONLY students with attempts.
                         $where .= ' AND qa.userid IS NOT NULL AND qa.preview = 0';
                     } else {
                         if ($noattempts == 1) {
                             // The noattempts = 1 means only no attempts,.
                             // So make the left join ask for only records where the right is null (no attempts).
                             // Show ONLY students without attempts.
                             $where .= ' AND qa.userid IS NULL';
                         } else {
                             if ($noattempts == 3) {
                                 // We want all attempts.
                                 $from = 'FROM {user} u JOIN {game_attempts} qa ON u.id = qa.userid ';
                                 $where = ' WHERE qa.gameid = ' . $game->id . ' AND qa.preview = 0';
                             }
                         }
                     }
                     // The noattempts = 2 means we want all students, with or without attempts.
                 }
             }
         }
         $countsql = 'SELECT COUNT(DISTINCT(' . sql_concat('u.id', '\'#\'', $db->IfNull('qa.attempt', '0')) . ')) ' . $from . $where;
     } else {
         if (empty($noattempts)) {
             $from = 'FROM {user} u JOIN {game_attempts} qa ON u.id = qa.userid ';
             $where = ' WHERE qa.gameid = ' . $game->id . ' AND qa.preview = 0';
             $countsql = 'SELECT COUNT(DISTINCT(' . sql_concat('u.id', '\'#\'', $db->IfNull('qa.attempt', '0')) . ')) ' . $from . $where;
         }
     }
     if (!$download) {
         // Add extra limits due to initials bar.
         if ($table->get_sql_where()) {
             $where .= ' AND ' . $table->get_sql_where();
         }
         // Count the records NOW, before funky question grade sorting messes up $from.
         if (!empty($countsql)) {
             $totalinitials = count_records_sql($countsql);
             if ($table->get_sql_where()) {
                 $countsql .= ' AND ' . $table->get_sql_where();
             }
             $total = count_records_sql($countsql);
         }
         // Add extra limits due to sorting by question grade.
         if ($sort = $table->get_sql_sort()) {
             $sortparts = explode(',', $sort);
             $newsort = array();
             $questionsort = false;
             foreach ($sortparts as $sortpart) {
                 $sortpart = trim($sortpart);
                 if (substr($sortpart, 0, 1) == '$') {
                     if (!$questionsort) {
                         $qid = intval(substr($sortpart, 1));
                         $select .= ', grade ';
                         $from .= ' LEFT JOIN {question_sessions} qns ON qns.attemptid = qa.id ' . 'LEFT JOIN {question_states} qs ON qs.id = qns.newgraded ';
                         $where .= ' AND (' . sql_isnull('qns.questionid') . ' OR qns.questionid = ' . $qid . ')';
                         $newsort[] = 'grade ' . (strpos($sortpart, 'ASC') ? 'ASC' : 'DESC');
                         $questionsort = true;
                     }
                 } else {
                     $newsort[] = $sortpart;
                 }
             }
             // Reconstruct the sort string.
             $sort = ' ORDER BY ' . implode(', ', $newsort);
         }
         // Fix some wired sorting.
         if (empty($sort)) {
             $sort = ' ORDER BY qa.id';
         }
         $table->pagesize($pagesize, $total);
     }
     // If there is feedback, include it in the query.
     if ($hasfeedback) {
         $select .= ', qf.feedbacktext ';
         $from .= " JOIN {game_feedback} qf ON " . "qf.gameid = {$game->id} AND qf.mingrade <= qa.score * {$game->grade}  AND qa.score * {$game->grade} < qf.maxgrade";
     }
     // Fetch the attempts.
     if (!empty($from)) {
         // If we're in the site course and displaying no attempts, it makes no sense to do the query.
         if (!$download) {
             $attempts = get_records_sql($select . $from . $where . $sort, $table->get_page_start(), $table->get_page_size());
         } else {
             $attempts = get_records_sql($select . $from . $where . $sort);
         }
     } else {
         $attempts = array();
     }
     // Build table rows.
     if (!$download) {
         $table->initialbars($totalinitials > 20);
     }
     if (!empty($attempts) || !empty($noattempts)) {
         if ($attempts) {
             foreach ($attempts as $attempt) {
                 $picture = print_user_picture($attempt->userid, $course->id, $attempt->picture, false, true);
                 /* Uncomment the commented lines below if you are choosing to show unenrolled users and
                  * have uncommented the corresponding lines earlier in this script
                  * if (in_array($attempt->userid, $unenrolledusers)) {
                  *    $userlink = '<a class="dimmed" href="'.$CFG->wwwroot.
                  *       '/user/view.php?id='.$attempt->userid.'&amp;course='.$course->id.'">'.fullname($attempt).'</a>';
                  *}
                  *else {
                  *   $userlink = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.
                  *      $attempt->userid.'&amp;course='.$course->id.'">'.fullname($attempt).'</a>';
                  *}
                  */
                 if (!$download) {
                     $row = array('<input type="checkbox" name="attemptid[]" value="' . $attempt->attempt . '" />', $picture, $userlink, empty($attempt->attempt) ? '-' : '<a href="review.php?q=' . $game->id . '&amp;attempt=' . $attempt->attempt . '">' . userdate($attempt->timestart, $strtimeformat) . '</a>', empty($attempt->timefinish) ? '-' : '<a href="review.php?q=' . $game->id . '&amp;attempt=' . $attempt->attempt . '">' . userdate($attempt->timefinish, $strtimeformat) . '</a>', empty($attempt->attempt) ? '-' : (empty($attempt->timefinish) ? get_string('unfinished', 'game') : format_time($attempt->duration)));
                 } else {
                     $row = array(fullname($attempt), empty($attempt->attempt) ? '-' : userdate($attempt->timestart, $strtimeformat), empty($attempt->timefinish) ? '-' : userdate($attempt->timefinish, $strtimeformat), empty($attempt->attempt) ? '-' : (empty($attempt->timefinish) ? get_string('unfinished', 'game') : format_time($attempt->duration)));
                 }
                 if ($game->grade) {
                     if (!$download) {
                         $row[] = $attempt->score === null ? '-' : '<a href="review.php?q=' . $game->id . '&amp;attempt=' . $attempt->attempt . '">' . round($attempt->score * $game->grade, $game->decimalpoints) . '</a>';
                     } else {
                         $row[] = $attempt->score === null ? '-' : round($attempt->score * $game->grade, $game->decimalpoints);
                     }
                 }
                 if ($detailedmarks) {
                     if (empty($attempt->attempt)) {
                         foreach ($questionids as $questionid) {
                             $row[] = '-';
                         }
                     } else {
                         foreach ($questionids as $questionid) {
                             if ($gradedstateid = get_field('question_sessions', 'newgraded', 'attemptid', $attempt->attemptuniqueid, 'questionid', $questionid)) {
                                 $grade = round(get_field('question_states', 'grade', 'id', $gradedstateid), $game->decimalpoints);
                             } else {
                                 $grade = '--';
                             }
                             if (!$download) {
                                 $row[] = link_to_popup_window('/mod/game/reviewquestion.php?state=' . $gradedstateid . '&amp;number=' . $questions[$questionid]->number, 'reviewquestion', $grade, 450, 650, $strreviewquestion, 'none', true);
                             } else {
                                 $row[] = $grade;
                             }
                         }
                     }
                 }
                 if ($hasfeedback) {
                     if ($attempt->timefinish) {
                         $row[] = $attempt->feedbacktext;
                     } else {
                         $row[] = '-';
                     }
                 }
                 if (!$download) {
                     $table->add_data($row);
                 } else {
                     if ($download == 'Excel' or $download == 'ODS') {
                         $colnum = 0;
                         foreach ($row as $item) {
                             $myxls->write($rownum, $colnum, $item, $format);
                             $colnum++;
                         }
                         $rownum++;
                     } else {
                         if ($download == 'CSV') {
                             $text = implode("\t", $row);
                             echo $text . " \n";
                         }
                     }
                 }
             }
         }
         if (!$download) {
             // Start form.
             echo '<div id="tablecontainer">';
             echo '<form id="attemptsform" method="post" action="report.php" ' . 'onsubmit="var menu = document.getElementById(\'menuaction\'); ' . 'return (menu.options[menu.selectedIndex].value == \'delete\' ? confirm(\'' . $strreallydel . '\') : true);">';
             echo '<div>';
             echo '<input type="hidden" name="id" value="' . $cm->id . '" />';
             echo '<input type="hidden" name="mode" value="overview" />';
             // Print table.
             $table->print_html();
             // Print "Select all" etc..
             if (!empty($attempts)) {
                 echo '<table id="commands">';
                 echo '<tr><td>';
                 echo '<a href="javascript:select_all_in(\'DIV\',null,\'tablecontainer\');">' . get_string('selectall', 'game') . '</a> / ';
                 echo '<a href="javascript:deselect_all_in(\'DIV\',null,\'tablecontainer\');">' . get_string('selectnone', 'game') . '</a> ';
                 echo '&nbsp;&nbsp;';
                 $options = array('delete' => get_string('delete'));
                 echo choose_from_menu($options, 'action', '', get_string('withselected', 'game'), 'if(this.selectedIndex > 0) submitFormById(\'attemptsform\');', '', true);
                 echo '<noscript id="noscriptmenuaction" style="display: inline;"><div>';
                 echo '<input type="submit" value="' . get_string('go') . '" /></div></noscript>';
                 echo '<script type="text/javascript">' . "\n<!--\n" . 'document.getElementById("noscriptmenuaction").style.display = "none";' . "\n-->\n" . '</script>';
                 echo '</td></tr></table>';
             }
             // Close form.
             echo '</div>';
             echo '</form></div>';
             if (!empty($attempts)) {
                 echo '<table class="boxaligncenter"><tr>';
                 $options = array();
                 $options["id"] = "{$cm->id}";
                 $options["q"] = "{$game->id}";
                 $options["mode"] = "overview";
                 $options['sesskey'] = sesskey();
                 $options["noheader"] = "yes";
                 $options['noattempts'] = $noattempts;
                 $options['detailedmarks'] = $detailedmarks;
                 echo '<td>';
                 $options["download"] = "ODS";
                 print_single_button("report.php", $options, get_string("downloadods", 'game'));
                 echo "</td>\n";
                 echo '<td>';
                 $options["download"] = "Excel";
                 print_single_button("report.php", $options, get_string("downloadexcel"));
                 echo "</td>\n";
                 echo '<td>';
                 $options["download"] = "CSV";
                 print_single_button('report.php', $options, get_string("downloadtext"));
                 echo "</td>\n";
                 echo "<td>";
                 helpbutton('overviewdownload', get_string('overviewdownload', 'quiz'), 'game');
                 echo "</td>\n";
                 echo '</tr></table>';
             }
         } else {
             if ($download == 'Excel' or $download == 'ODS') {
                 $workbook->close();
                 exit;
             } else {
                 if ($download == 'CSV') {
                     exit;
                 }
             }
         }
     } else {
         if (!$download) {
             $table->print_html();
         }
     }
     // Print display options.
     echo '<div class="controls">';
     echo '<form id="options" action="report.php" method="get">';
     echo '<div>';
     echo '<p>' . get_string('displayoptions', 'game') . ': </p>';
     echo '<input type="hidden" name="id" value="' . $cm->id . '" />';
     echo '<input type="hidden" name="q" value="' . $game->id . '" />';
     echo '<input type="hidden" name="mode" value="overview" />';
     echo '<input type="hidden" name="noattempts" value="0" />';
     echo '<input type="hidden" name="detailedmarks" value="0" />';
     echo '<table id="overview-options" class="boxaligncenter">';
     echo '<tr align="left">';
     echo '<td><label for="pagesize">' . get_string('pagesize', 'game') . '</label></td>';
     echo '<td><input type="text" id="pagesize" name="pagesize" size="3" value="' . $pagesize . '" /></td>';
     echo '</tr>';
     echo '<tr align="left">';
     echo '<td colspan="2">';
     $options = array(0 => get_string('attemptsonly', 'game', $course->students));
     if ($course->id != SITEID) {
         $options[1] = get_string('noattemptsonly', 'game', $course->students);
         $options[2] = get_string('allstudents', 'game', $course->students);
         $options[3] = get_string('allattempts', 'game');
     }
     choose_from_menu($options, 'noattempts', $noattempts, '');
     echo '</td></tr>';
     echo '<tr align="left">';
     echo '<td colspan="2"><input type="checkbox" id="checkdetailedmarks" name="detailedmarks" ' . ($detailedmarks ? 'checked="checked" ' : '') . 'value="1" /> <label for="checkdetailedmarks">' . get_string('showdetailedmarks', 'game') . '</label> ';
     echo '</td></tr>';
     echo '<tr><td colspan="2" align="center">';
     echo '<input type="submit" value="' . get_string('go') . '" />';
     echo '</td></tr></table>';
     echo '</div>';
     echo '</form>';
     echo '</div>';
     echo "\n";
     return true;
 }
Beispiel #6
0
function hotpot_get_report_users($course, $formdata)
{
    $users = array();
    /// Check to see if groups are being used in this module
    $groupmode = groupmode($course, $cm);
    //TODO: there is no $cm defined!
    $currentgroup = setup_and_print_groups($course, $groupmode, "report.php?id={$cm->id}&mode=simple");
    $sort = "u.lastname ASC";
    switch ($formdata['reportusers']) {
        case 'students':
            if ($currentgroup) {
                $users = get_group_students($currentgroup, $sort);
            } else {
                $users = get_course_students($course->id, $sort);
            }
            break;
        case 'all':
            if ($currentgroup) {
                $users = get_group_users($currentgroup, $sort);
            } else {
                $users = get_course_users($course->id, $sort);
            }
            break;
    }
    return $users;
}
 /**
  * This is called from modedit.php to load the default for the groupmode element.
  *
  * @param object $course
  * @param object $cm
  */
 function modgroupmode_settings()
 {
     global $COURSE;
     return array('groupmode' => groupmode($COURSE, $this->_cm));
 }
Beispiel #8
0
/**
 * sends an email to the teachers of the course where the given feedback is placed.
 *
 * @global object
 * @global object
 * @uses FEEDBACK_ANONYMOUS_NO
 * @uses FORMAT_PLAIN
 * @param object $cm the coursemodule-record
 * @param object $feedback
 * @param object $course
 * @param int $userid
 * @return void
 */
function feedback_send_email($cm, $feedback, $course, $userid)
{
    global $CFG, $DB;
    if ($feedback->email_notification == 0) {
        // No need to do anything
        return;
    }
    $user = $DB->get_record('user', array('id' => $userid));
    if (groupmode($course, $cm) == SEPARATEGROUPS) {
        // Separate groups are being used
        $groups = $DB->get_records_sql_menu("SELECT g.name, g.id\n                                               FROM {groups} g, {groups_members} m\n                                              WHERE g.courseid = ?\n                                                    AND g.id = m.groupid\n                                                    AND m.userid = ?\n                                           ORDER BY name ASC", array($course->id, $userid));
        $groups = array_values($groups);
        $teachers = feedback_get_receivemail_users($cm->id, $groups);
    } else {
        $teachers = feedback_get_receivemail_users($cm->id);
    }
    if ($teachers) {
        $strfeedbacks = get_string('modulenameplural', 'feedback');
        $strfeedback = get_string('modulename', 'feedback');
        $strcompleted = get_string('completed', 'feedback');
        $printusername = $feedback->anonymous == FEEDBACK_ANONYMOUS_NO ? fullname($user) : get_string('anonymous_user', 'feedback');
        foreach ($teachers as $teacher) {
            $info = new object();
            $info->username = $printusername;
            $info->feedback = format_string($feedback->name, true);
            $info->url = $CFG->wwwroot . '/mod/feedback/show_entries.php?id=' . $cm->id . '&userid=' . $userid . '&do_show=showentries';
            $postsubject = $strcompleted . ': ' . $info->username . ' -> ' . $feedback->name;
            $posttext = feedback_send_email_text($info, $course);
            $posthtml = $teacher->mailformat == 1 ? feedback_send_email_html($info, $course, $cm) : '';
            if ($feedback->anonymous == FEEDBACK_ANONYMOUS_NO) {
                $eventdata = new object();
                $eventdata->modulename = 'feedback';
                $eventdata->userfrom = $user;
                $eventdata->userto = $teacher;
                $eventdata->subject = $postsubject;
                $eventdata->fullmessage = $posttext;
                $eventdata->fullmessageformat = FORMAT_PLAIN;
                $eventdata->fullmessagehtml = $posthtml;
                $eventdata->smallmessage = '';
                if (events_trigger('message_send', $eventdata) > 0) {
                }
            } else {
                $eventdata = new object();
                $eventdata->modulename = 'feedback';
                $eventdata->userfrom = $teacher;
                $eventdata->userto = $teacher;
                $eventdata->subject = $postsubject;
                $eventdata->fullmessage = $posttext;
                $eventdata->fullmessageformat = FORMAT_PLAIN;
                $eventdata->fullmessagehtml = $posthtml;
                $eventdata->smallmessage = '';
                if (events_trigger('message_send', $eventdata) > 0) {
                }
            }
        }
    }
}
Beispiel #9
0
$strgradeitemadd = get_string('gradeitemadd', 'grades');
$strgradeitemremove = get_string('gradeitemremove', 'grades');
$strgradeiteminfo = get_string('gradeiteminfo', 'grades');
$strgradeiteminfopeople = get_string('gradeiteminfopeople', 'grades');
$strgradeitemrandomassign = get_string('gradeitemrandomassign', 'grades');
$strgradeitemaddusers = get_string('gradeitemaddusers', 'grades');
$strgradeitems = get_string('gradeitems', 'grades');
$courseid = $course->id;
$listgrade_items = array();
$listmembers = array();
$nonmembers = array();
$grade_items = array();
$grade_items = get_records('grade_item', 'courseid', $course->id);
$grade_itemcount = count($grade_items);
// get current group of students and assign that to be the working list of students
if (groupmode($course) != 0) {
    if ($currentgroup = get_current_group($course->id)) {
        //groupmode is either separate or visible and there is a group
        $students = get_group_users($group);
    } else {
        //groupmode is either separate or visible and the current group is all participants
        $students = grade_get_course_students($course->id);
    }
} else {
    $students = grade_get_course_students($course->id);
    //groupmode is not set (all participants)
}
// we need to create a multidimensional array keyed by grade_itemid with all_students at each level
if (isset($grade_items)) {
    foreach ($grade_items as $grade_item) {
        $nonmembers[$grade_item->id] = array();
Beispiel #10
0
function workshop_print_league_table($workshop)
{
    // print an order table of (student) submissions showing teacher's and student's assessments
    if (!($course = get_record("course", "id", $workshop->course))) {
        error("Print league table: Course is misconfigured");
    }
    if (!($cm = get_coursemodule_from_instance("workshop", $workshop->id, $workshop->course))) {
        error("Course Module ID was incorrect");
    }
    // set $groupid if workshop is in SEPARATEGROUPS mode
    if (groupmode($course, $cm) == SEPARATEGROUPS) {
        $groupid = get_current_group($course->id);
    } else {
        $groupid = 0;
    }
    $nentries = $workshop->showleaguetable;
    if ($workshop->anonymous and workshop_is_student($workshop)) {
        $table->head = array(get_string("title", "workshop"), get_string("teacherassessments", "workshop", $course->teacher), get_string("studentassessments", "workshop", $course->student), get_string("overallgrade", "workshop"));
        $table->align = array("left", "center", "center", "center");
        $table->size = array("*", "*", "*", "*");
    } else {
        // show names
        $table->head = array(get_string("title", "workshop"), get_string("name"), get_string("teacherassessments", "workshop", $course->teacher), get_string("studentassessments", "workshop", $course->student), get_string("overallgrade", "workshop"));
        $table->align = array("left", "left", "center", "center", "center");
        $table->size = array("*", "*", "*", "*", "*");
    }
    $table->cellpadding = 2;
    $table->cellspacing = 0;
    if ($submissions = workshop_get_student_submissions($workshop)) {
        foreach ($submissions as $submission) {
            if ($groupid) {
                // check submission's group
                if (!groups_is_member($groupid, $submission->userid)) {
                    continue;
                    // skip this submission
                }
            }
            $grades[$submission->id] = workshop_submission_grade($workshop, $submission);
        }
        arsort($grades);
        // largest grade first
        reset($grades);
        $n = 1;
        while (list($submissionid, $grade) = each($grades)) {
            if (!($submission = get_record("workshop_submissions", "id", $submissionid))) {
                error("Print league table: submission not found");
            }
            if (!($user = get_record("user", "id", $submission->userid))) {
                error("Print league table: user not found");
            }
            if ($workshop->anonymous and workshop_is_student($workshop)) {
                $table->data[] = array(workshop_print_submission_title($workshop, $submission), workshop_print_submission_assessments($workshop, $submission, "teacher"), workshop_print_submission_assessments($workshop, $submission, "student"), $grade);
            } else {
                $table->data[] = array(workshop_print_submission_title($workshop, $submission), fullname($user), workshop_print_submission_assessments($workshop, $submission, "teacher"), workshop_print_submission_assessments($workshop, $submission, "student"), $grade);
            }
            $n++;
            if ($n > $nentries) {
                break;
            }
        }
        print_heading(get_string("leaguetable", "workshop"));
        print_table($table);
        workshop_print_key($workshop);
    }
}
function certificate_email_teachers($certificate)
{
    global $course, $USER, $CFG;
    if ($certificate->emailteachers == 0) {
        // No need to do anything
        return;
    }
    $certrecord = certificate_get_issue($course, $USER, $certificate->id);
    $student = $certrecord->studentname;
    $cm = get_coursemodule_from_instance("certificate", $certificate->id, $course->id);
    if (groupmode($course, $cm) == SEPARATEGROUPS) {
        // Separate groups are being used
        if (!($group = user_group($course->id, $user->id))) {
            // Try to find a group
            $group->id = 0;
            // Not in a group, never mind
        }
        $teachers = get_group_teachers($course->id, $group->id);
        // Works even if not in group
    } else {
        $teachers = get_course_teachers($course->id);
    }
    if ($teachers) {
        $strcertificates = get_string('modulenameplural', 'certificate');
        $strcertificate = get_string('modulename', 'certificate');
        $strawarded = get_string('awarded', 'certificate');
        foreach ($teachers as $teacher) {
            unset($info);
            $info->student = $student;
            $info->course = format_string($course->fullname, true);
            $info->certificate = format_string($certificate->name, true);
            $info->url = $CFG->wwwroot . '/mod/certificate/report.php?id=' . $cm->id;
            $from = $student;
            $postsubject = $strawarded . ': ' . $info->student . ' -> ' . $certificate->name;
            $posttext = certificate_email_teachers_text($info);
            $posthtml = certificate_email_teachers_html($info);
            $posthtml = $teacher->mailformat == 1 ? certificate_email_teachers_html($info) : '';
            @email_to_user($teacher, $from, $postsubject, $posttext, $posthtml);
            // If it fails, oh well, too bad.
            set_field("certificate_issues", "mailed", "1", "certificateid", $certificate->id, "userid", $USER->id);
        }
    }
}
Beispiel #12
0
/**
 * Prepares array of recent activity in course forums
 *
 * Returns all forum posts since a given time. If forum and/or user is specified then
 * this restricts the results.
 *
 * @param array &$activities An array of objects representing recent activites
 * @param int &$index ???
 * @param int $sincetime Searches only for a activitity since this timestamp
 * @param int $courseid Searches only for a activitity in a given course module (default 0 means all)
 * @param string $user Searches only for a user's activity (given by username)
 * @param array $groupid Searches only for an activity in particular groups
 * @return No return value but modifies &$activities and &$index
 */
function forum_get_recent_mod_activity(&$activities, &$index, $sincetime, $courseid, $cmid = "0", $user = "", $groupid = "")
{
    global $CFG, $USER, $COURSE;
    if ($cmid) {
        $forumselect = " AND cm.id = '{$cmid}'";
    } else {
        $forumselect = "";
    }
    if ($user) {
        $userselect = " AND u.id = '{$user}'";
    } else {
        $userselect = "";
    }
    if (is_numeric($groupid)) {
        // the behaviour of mygroupid() has been changed so we are getting array now
        $groupid = array($groupid);
    }
    $posts = get_records_sql("SELECT p.*, d.name, u.firstname, u.lastname, u.username,\n                                     u.picture, d.groupid, cm.instance, f.name,\n                                     cm.section, cm.id AS cmid\n                               FROM {$CFG->prefix}forum_posts p,\n                                    {$CFG->prefix}forum_discussions d,\n                                    {$CFG->prefix}user u,\n                                    {$CFG->prefix}course_modules cm,\n                                    {$CFG->prefix}forum f\n                              WHERE p.modified > '{$sincetime}' {$forumselect}\n                                AND p.userid = u.id {$userselect}\n                                AND d.course = '{$courseid}'\n                                AND p.discussion = d.id\n                                AND cm.instance = f.id\n                                AND cm.course = d.course\n                                AND cm.course = f.course\n                                AND f.id = d.forum\n                              ORDER BY p.discussion ASC,p.created ASC");
    if (empty($posts)) {
        return;
    }
    $groupmode = array();
    // To cache group modes of particular forums
    $cm = array();
    // To cache course modules
    $mygroupids = groups_get_groups_for_user($USER->id, $COURSE->id) or $mygroupids = array();
    foreach ($posts as $post) {
        $modcontext = get_context_instance(CONTEXT_MODULE, $post->cmid);
        // Check whether this post belongs to a discussion in a group that
        // should NOT be accessible to the current user
        // Open discussions have groupid -1
        if (!has_capability('moodle/site:accessallgroups', $modcontext) && $post->groupid != -1) {
            if (!isset($cm[$post->cmid])) {
                $cm[$post->cmid] = get_coursemodule_from_instance('forum', $post->cmid, $courseid);
            }
            if (!isset($groupmode[$post->cmid])) {
                $groupmode[$post->cmid] = groupmode($COURSE, $cm[$post->cmid]);
            }
            if ($groupmode[$post->cmid] == SEPARATEGROUPS) {
                if (!in_array($post->groupid, $mygroupids)) {
                    continue;
                }
            }
        }
        // the user wants to restrict results on selected group only
        if (is_array($groupid) && !in_array($post->groupid, $groupid)) {
            continue;
        }
        $tmpactivity = new Object();
        $tmpactivity->type = "forum";
        $tmpactivity->defaultindex = $index;
        $tmpactivity->instance = $post->instance;
        $tmpactivity->name = $post->name;
        $tmpactivity->section = $post->section;
        $tmpactivity->content->id = $post->id;
        $tmpactivity->content->discussion = $post->discussion;
        $tmpactivity->content->subject = $post->subject;
        $tmpactivity->content->parent = $post->parent;
        $tmpactivity->user->userid = $post->userid;
        $tmpactivity->user->fullname = fullname($post);
        $tmpactivity->user->picture = $post->picture;
        $tmpactivity->timestamp = $post->modified;
        $activities[] = $tmpactivity;
        $index++;
    }
    return;
}
Beispiel #13
0
/**
* this function handles the access policy to contents indexed as searchable documents. If this 
* function does not exist, the search engine assumes access is allowed.
* When this point is reached, we already know that : 
* - user is legitimate in the surrounding context
* - user may be guest and guest access is allowed to the module
* - the function may perform local checks within the module information logic
* @param path the access path to the module script code
* @param entry_type the information subclassing (usefull for complex modules, defaults to 'standard')
* @param this_id the item id within the information class denoted by entry_type. In techprojects, this id 
* points to the techproject instance in which all resources are indexed.
* @param user the user record denoting the user who searches
* @param group_id the current group used by the user when searching
* @return true if access is allowed, false elsewhere
*/
function techproject_check_text_access($path, $entry_type, $this_id, $user, $group_id, $context_id)
{
    global $CFG;
    include_once "{$CFG->dirroot}/{$path}/lib.php";
    // get the techproject object and all related stuff
    $techproject = get_record('techproject', 'id', $this_id);
    $course = get_record('course', 'id', $techproject->course);
    $module_context = get_record('context', 'id', $context_id);
    $cm = get_record('course_modules', 'id', $module_context->instanceid);
    if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $module_context)) {
        return false;
    }
    //group consistency check : checks the following situations about groups
    // if user is guest check access capabilities for guests :
    // guests can see default project, and other records if groups are liberal
    // TODO : change guestsallowed in a capability
    if (isguest() && $techproject->guestsallowed) {
        if ($group_id && groupmode($course, $cm) == SEPARATEGROUPS) {
            return false;
        }
        return true;
    }
    // trap if user is not same group and groups are separated
    $current_group = get_current_group($course->id);
    if (groupmode($course) == SEPARATEGROUPS && $group_id != $current_group && $group_id) {
        return false;
    }
    //trap if ungroupedsees is off in strict access mode and user is not teacher
    if (groupmode($course) == SEPARATEGROUPS && !$techproject->ungroupedsees && !$group_id && isteacher($user->id)) {
        return false;
    }
    return true;
}
Beispiel #14
0
/**
 * @todo Document this function
 */
function forum_menu_list($course)
{
    $menu = array();
    $currentgroup = get_and_set_current_group($course, groupmode($course));
    if ($forums = get_all_instances_in_course("forum", $course)) {
        if ($course->format == 'weeks') {
            $strsection = get_string('week');
        } else {
            $strsection = get_string('topic');
        }
        foreach ($forums as $forum) {
            if ($cm = get_coursemodule_from_instance('forum', $forum->id, $course->id)) {
                $context = get_context_instance(CONTEXT_MODULE, $cm->id);
                if (!isset($forum->visible)) {
                    if (!instance_is_visible("forum", $forum) && !has_capability('moodle/course:viewhiddenactivities', $context)) {
                        continue;
                    }
                }
                $groupmode = groupmode($course, $cm);
                // Groups are being used
                if ($groupmode == SEPARATEGROUPS && $currentgroup === false && !has_capability('moodle/site:accessallgroups', $context)) {
                    continue;
                }
            }
            $menu[$forum->id] = format_string($forum->name, true);
        }
    }
    return $menu;
}
Beispiel #15
0
    $learningtable->align[] = 'center';
}
/// Now let's process the learning forums
if ($course->id != SITEID) {
    // Only real courses have learning forums
    // Add extra field for section number, at the front
    if ($course->format == 'weeks' or $course->format == 'weekscss') {
        array_unshift($learningtable->head, $strweek);
    } else {
        array_unshift($learningtable->head, $strsection);
    }
    array_unshift($learningtable->align, "center");
    if ($learningforums) {
        $currentsection = "";
        foreach ($learningforums as $key => $forum) {
            $groupmode = groupmode($course, $forum);
            /// Can do this because forum->groupmode is defined
            $forum->visible = instance_is_visible("forum", $forum) || has_capability('moodle/course:view', $coursecontext);
            $cm = get_coursemodule_from_instance("forum", $forum->id, $course->id);
            $context = get_context_instance(CONTEXT_MODULE, $cm->id);
            if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
                $count = count_records("forum_discussions", "forum", "{$forum->id}", "groupid", $currentgroup);
            } else {
                $count = count_records("forum_discussions", "forum", "{$forum->id}");
            }
            if ($usetracking) {
                if ($forum->trackingtype == FORUM_TRACKING_OFF) {
                    $unreadlink = '-';
                    $trackedlink = '-';
                } else {
                    if ($forum->trackingtype == FORUM_TRACKING_ON || !isset($untracked[$forum->id])) {
/**
* this function handles the access policy to contents indexed as searchable documents. If this 
* function does not exist, the search engine assumes access is allowed.
* When this point is reached, we already know that : 
* - user is legitimate in the surrounding context
* - user may be guest and guest access is allowed to the module
* - the function may perform local checks within the module information logic
* @param path the access path to the module script code
* @param itemtype the information subclassing (usefull for complex modules, defaults to 'standard')
* @param this_id the item id within the information class denoted by itemtype. In forums, this id 
* points out the individual post.
* @param user the user record denoting the user who searches
* @param group_id the current group used by the user when searching
* @uses CFG, USER
* @return true if access is allowed, false elsewhere
*/
function forum_check_text_access($path, $itemtype, $this_id, $user, $group_id, $context_id)
{
    global $CFG, $USER;
    include_once "{$CFG->dirroot}/{$path}/lib.php";
    // get the forum post and all related stuff
    $post = get_record('forum_posts', 'id', $this_id);
    $discussion = get_record('forum_discussions', 'id', $post->discussion);
    $context = get_record('context', 'id', $context_id);
    $cm = get_record('course_modules', 'id', $context->instanceid);
    if (empty($cm)) {
        return false;
    }
    // Shirai 20093005 - MDL19342 - course module might have been delete
    if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $context)) {
        if (!empty($CFG->search_access_debug)) {
            echo "search reject : hidden forum resource ";
        }
        return false;
    }
    // approval check : entries should be approved for being viewed, or belongs to the user
    if ($post->userid != $USER->id && !$post->mailed && !has_capability('mod/forum:viewhiddentimeposts', $context)) {
        if (!empty($CFG->search_access_debug)) {
            echo "search reject : time hidden forum item";
        }
        return false;
    }
    // group check : entries should be in accessible groups
    $current_group = get_current_group($discussion->course);
    $course = get_record('course', 'id', $discussion->course);
    if ($group_id >= 0 && groupmode($course, $cm) == SEPARATEGROUPS && $group_id != $current_group && !has_capability('mod/forum:viewdiscussionsfromallgroups', $context)) {
        if (!empty($CFG->search_access_debug)) {
            echo "search reject : separated grouped forum item";
        }
        return false;
    }
    return true;
}
Beispiel #17
0
/**
 * Gets an array of the group IDs of all groups for the user in this course module.
 * @uses $USER     
 * @param object $cm The course-module object.
 * @param int $userid The ID of the user.
 * @return array An array of group IDs, or false. 
 */
function groups_m_get_groups_for_user($cm, $userid)
{
    //echo 'User'; print_object($cm);
    $groupingid = groups_get_grouping_for_coursemodule($cm);
    if (!$groupingid || GROUP_NOT_IN_GROUPING == $groupingid) {
        //TODO: check.
        return false;
    }
    if (!isset($cm->course) || !groupmode($cm->course, $cm)) {
        return false;
    } elseif (GROUP_ANY_GROUPING == $groupingid) {
        return groups_get_groups_for_user($userid, $cm->course);
    }
    return groups_get_groups_for_user_in_grouping($userid, $groupingid);
}
/**
* this function handles the access policy to contents indexed as searchable documents. If this 
* function does not exist, the search engine assumes access is allowed.
* When this point is reached, we already know that : 
* - user is legitimate in the surrounding context
* - user may be guest and guest access is allowed to the module
* - the function may perform local checks within the module information logic
* @param path the access path to the module script code
* @param itemtype the information subclassing (usefull for complex modules, defaults to 'standard')
* @param this_id the item id within the information class denoted by itemtype. In wikies, this id 
* points out the indexed wiki page.
* @param user the user record denoting the user who searches
* @param group_id the current group used by the user when searching
* @uses CFG
* @return true if access is allowed, false elsewhere
*/
function wiki_check_text_access($path, $itemtype, $this_id, $user, $group_id, $context_id)
{
    global $CFG;
    // get the wiki object and all related stuff
    $page = get_record('wiki_pages', 'id', $id);
    $entry = get_record('wiki_entries', 'id', $page->wiki);
    $course = get_record('course', 'id', $entry->course);
    $context = get_record('context', 'id', $context_id);
    $cm = get_record('course_modules', 'id', $context->instanceid);
    // $cm = get_coursemodule_from_instance('wiki', $wiki->id, $wiki->course);
    // $context = get_record('context', 'id', $cm->id);
    if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $context)) {
        if (!empty($CFG->search_access_debug)) {
            echo "search reject : hidden wiki ";
        }
        return false;
    }
    //group consistency check : checks the following situations about groups
    // trap if user is not same group and groups are separated
    $current_group = get_current_group($course->id);
    if (groupmode($course) == SEPARATEGROUPS && $group_id != $current_group && !has_capability('moodle/site:accessallgroups', $context)) {
        if (!empty($CFG->search_access_debug)) {
            echo "search reject : separated group owner wiki ";
        }
        return false;
    }
    return true;
}
Beispiel #19
0
    $context = get_context_instance(CONTEXT_MODULE, $cm->id);
} else {
    $cm->id = 0;
    $context = get_context_instance(CONTEXT_MODULE, $cm->id);
}
if ($user) {
    if (!has_capability('mod/forum:managesubscriptions', $context)) {
        print_error('nopermissiontosubscribe', 'forum');
    }
    if (!($user = $DB->get_record("user", array("id" => $user)))) {
        print_error('invaliduserid');
    }
} else {
    $user = $USER;
}
if (groupmode($course, $cm) and !forum_is_subscribed($user->id, $forum) and !has_capability('moodle/site:accessallgroups', $context)) {
    if (!groups_get_all_groups($course->id, $USER->id)) {
        print_error('cannotsubscribe', 'forum');
    }
}
require_login($course->id, false, $cm);
if (isguest()) {
    // Guests can't subscribe
    $navigation = build_navigation('', $cm);
    print_header($course->shortname, $course->fullname, $navigation, '', '', true, "", navmenu($course, $cm));
    echo $OUTPUT->confirm(get_string('noguestsubscribe', 'forum') . '<br /><br />' . get_string('liketologin'), get_login_url(), new moodle_url());
    echo $OUTPUT->footer();
    exit;
}
$returnto = optional_param('backtoindex', 0, PARAM_INT) ? "index.php?id=" . $course->id : "view.php?f={$id}";
if ($force and has_capability('mod/forum:managesubscriptions', $context)) {
Beispiel #20
0
     }
     if (!has_capability('moodle/course:view', $coursecontext, $user->id, false)) {
         if (has_capability('moodle/course:view', $coursecontext)) {
             print_header("{$strpersonalprofile}: ", "{$strpersonalprofile}: ", "<a href=\"../course/view.php?id={$course->id}\">{$course->shortname}</a> ->\n                                  <a href=\"index.php?id={$course->id}\">{$strparticipants}</a> -> {$fullname}", "", "", true, "&nbsp;", navmenu($course));
             print_heading(get_string('notenrolled', '', $fullname));
         } else {
             print_header("{$strpersonalprofile}: ", "{$strpersonalprofile}: ", "<a href=\"../course/view.php?id={$course->id}\">{$course->shortname}</a> ->\n                                  <a href=\"index.php?id={$course->id}\">{$strparticipants}</a> -> {$struser}", "", "", true, "&nbsp;", navmenu($course));
             print_heading(get_string('notenrolledprofile'));
         }
         print_continue($_SERVER['HTTP_REFERER']);
         print_footer($course);
         exit;
     }
 }
 // If groups are in use, make sure we can see that group
 if (groupmode($course) == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $coursecontext)) {
     require_login();
     ///this is changed because of mygroupid
     $gtrue = (bool) groups_get_groups_for_user($user->id, $course->id);
     /*TODO: $gtrue = false;
       if ($mygroups = mygroupid($course->id)){
           foreach ($mygroups as $group){
               if (ismember($group, $user->id)){
                   $gtrue = true;
               }
           }
       }*/
     if (!$gtrue) {
         print_header("{$strpersonalprofile}: ", "{$strpersonalprofile}: ", "<a href=\"../course/view.php?id={$course->id}\">{$course->shortname}</a> ->\n                              <a href=\"index.php?id={$course->id}\">{$strparticipants}</a>", "", "", true, "&nbsp;", navmenu($course));
         error(get_string("groupnotamember"), "../course/view.php?id={$course->id}");
     }
Beispiel #21
0
         $activity->name = '';
     }
     $activity->visible = $section->visible;
     $activities[$index] = $activity;
 }
 $index++;
 $i++;
 $sectionmods = explode(",", $section->sequence);
 foreach ($sectionmods as $sectionmod) {
     if (empty($mods[$sectionmod])) {
         continue;
     }
     $mod = $mods[$sectionmod];
     $instance = get_record("{$mod->modname}", "id", "{$mod->instance}");
     $coursemod = get_record_sql("SELECT m.id, m.name, cm.groupmode, cm.visible\n                                               FROM {$CFG->prefix}course_modules cm,\n                                                    {$CFG->prefix}modules m\n                                              WHERE course = '{$course->id}' {$hiddenfilter}\n                                                AND m.id = cm.module {$activityfilter}\n                                                AND cm.id = '{$sectionmod}'");
     $groupmode = groupmode($course, $coursemod);
     switch ($groupmode) {
         case SEPARATEGROUPS:
             $groupid = mygroupid($course->id);
             break;
         case VISIBLEGROUPS:
             $groupid = $selectedgroup;
             break;
         case NOGROUPS:
         default:
             $groupid = 0;
     }
     $libfile = "{$CFG->dirroot}/mod/{$coursemod->name}/lib.php";
     if (file_exists($libfile)) {
         require_once $libfile;
         $get_recent_mod_activity = $coursemod->name . "_get_recent_mod_activity";
/**
* this function handles the access policy to contents indexed as searchable documents. If this 
* function does not exist, the search engine assumes access is allowed.
* When this point is reached, we already know that : 
* - user is legitimate in the surrounding context
* - user may be guest and guest access is allowed to the module
* - the function may perform local checks within the module information logic
* @param path the access path to the module script code
* @param itemtype the information subclassing (usefull for complex modules, defaults to 'standard')
* @param this_id the item id within the information class denoted by entry_type. In chats, this id 
* points out a session history which is a close sequence of messages.
* @param user the user record denoting the user who searches
* @param group_id the current group used by the user when searching
* @uses CFG
* @return true if access is allowed, false elsewhere
*/
function chat_check_text_access($path, $itemtype, $this_id, $user, $group_id, $context_id)
{
    global $CFG;
    include_once "{$CFG->dirroot}/{$path}/lib.php";
    list($chat_id, $sessionstart, $sessionend) = split('-', $this_id);
    // get the chat session and all related stuff
    $chat = get_record('chat', 'id', $chat_id);
    $context = get_record('context', 'id', $context_id);
    $cm = get_record('course_modules', 'id', $context->instanceid);
    // $cm = get_coursemodule_from_instance('chat', $chat->id, $chat->course);
    // $context = get_context_instance(CONTEXT_MODULE, $cm->id);
    if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $context)) {
        if (!empty($CFG->search_access_debug)) {
            echo "search reject : hidden chat ";
        }
        return false;
    }
    //group consistency check : checks the following situations about groups
    // trap if user is not same group and groups are separated
    $course = get_record('course', 'id', $chat->course);
    if (groupmode($course, $cm) == SEPARATEGROUPS && !ismember($group_id) && !has_capability('moodle/site:accessallgroups', $context)) {
        if (!empty($CFG->search_access_debug)) {
            echo "search reject : chat element is in separated group ";
        }
        return false;
    }
    //ownership check : checks the following situations about user
    // trap if user is not owner and has cannot see other's entries
    // TODO : typically may be stored into indexing cache
    if (!has_capability('mod/chat:readlog', $context)) {
        if (!empty($CFG->search_access_debug)) {
            echo "search reject : cannot read past sessions ";
        }
        return false;
    }
    return true;
}
Beispiel #23
0
/** 
 *  sends an email to the teachers of the course where the given feedback is placed.
 *  @param object $cm the coursemodule-record
 *  @param $feedback
 *  @param $course
 *  @param $userid
 *  @return void
 */
function feedback_send_email($cm, $feedback, $course, $userid)
{
    global $CFG;
    if ($feedback->email_notification == 0) {
        // No need to do anything
        return;
    }
    $user = get_record('user', 'id', $userid);
    if (groupmode($course, $cm) == SEPARATEGROUPS) {
        // Separate groups are being used
        $groups = get_records_sql_menu("SELECT g.name, g.id\n                                          FROM {$CFG->prefix}groups g,\n                                                 {$CFG->prefix}groups_members m\n                                          WHERE g.courseid = '{$course->id}'\n                                             AND g.id = m.groupid\n                                             AND m.userid = '{$userid}'\n                                             ORDER BY name ASC");
        $groups = array_values($groups);
        $teachers = feedback_get_receivemail_users($cm->id, $groups);
    } else {
        $teachers = feedback_get_receivemail_users($cm->id);
    }
    if ($teachers) {
        $strfeedbacks = get_string('modulenameplural', 'feedback');
        $strfeedback = get_string('modulename', 'feedback');
        $strcompleted = get_string('completed', 'feedback');
        $printusername = $feedback->anonymous == FEEDBACK_ANONYMOUS_NO ? fullname($user) : get_string('anonymous_user', 'feedback');
        foreach ($teachers as $teacher) {
            unset($info);
            $info->username = $printusername;
            $info->feedback = format_string($feedback->name, true);
            $info->url = $CFG->wwwroot . '/mod/feedback/show_entries.php?id=' . $cm->id . '&userid=' . $userid . '&do_show=showentries';
            $postsubject = $strcompleted . ': ' . $info->username . ' -> ' . $feedback->name;
            $posttext = feedback_send_email_text($info, $course);
            $posthtml = $teacher->mailformat == 1 ? feedback_send_email_html($info, $course, $cm) : '';
            if ($feedback->anonymous == FEEDBACK_ANONYMOUS_NO) {
                @email_to_user($teacher, $user, $postsubject, $posttext, $posthtml);
            } else {
                @email_to_user($teacher, $teacher, $postsubject, $posttext, $posthtml);
            }
        }
    }
}
Beispiel #24
0
/**
* this function handles the access policy to contents indexed as searchable documents. If this 
* function does not exist, the search engine assumes access is allowed.
* When this point is reached, we already know that : 
* - user is legitimate in the surrounding context
* - user may be guest and guest access is allowed to the module
* - the function may perform local checks within the module information logic
* @param path the access path to the module script code
* @param itemtype the information subclassing (usefull for complex modules, defaults to 'standard')
* @param this_id the item id within the information class denoted by itemtype. In forums, this id 
* points out the individual post.
* @param user the user record denoting the user who searches
* @param group_id the current group used by the user when searching
* @return true if access is allowed, false elsewhere
*/
function forum_check_text_access($path, $itemtype, $this_id, $user, $group_id)
{
    global $CFG;
    include_once "{$CFG->dirroot}/{$path}/lib.php";
    // get the glossary object and all related stuff
    $post = get_record('forum_posts', 'id', $this_id);
    $discussion = get_record('forum_discussions', 'id', $post->discussion);
    $course = get_record('course', 'id', $discussion->course);
    $cm = get_coursemodule_from_instance('forum', $discussion->forum, $course->id);
    $context_module = get_context_instance(CONTEXT_MODULE, $cm->id);
    if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $context_module)) {
        return false;
    }
    // approval check : entries should be approved for being viewed, or belongs to the user
    if (!$post->mailed && !has_capability('mod/forum:viewhiddentimeposts', $context_module)) {
        return false;
    }
    // group check : entries should be in accessible groups
    $current_group = get_current_group($course->id);
    if (groupmode($course, $cm) == SEPARATEGROUPS && $group_id != $current_group && !has_capability('mod/forum:viewdiscussionsfromallgroups', $context_module)) {
        return false;
    }
    return true;
}
Beispiel #25
0
if (!($course = get_record('course', 'id', $id))) {
    error('Course ID was incorrect');
}
require_login($course->id);
$context = get_context_instance(CONTEXT_COURSE, $course->id);
if ($instanceid) {
    $instance = get_record('block_instance', 'id', $instanceid);
} else {
    if ($quickmailblock = get_record('block', 'name', 'quickmail')) {
        $instance = get_record('block_instance', 'blockid', $quickmailblock->id, 'pageid', $course->id);
    }
}
/// This block of code ensures that Quickmail will run
///     whether it is in the course or not
if (empty($instance)) {
    $groupmode = groupmode($course);
    if (has_capability('block/quickmail:cansend', get_context_instance(CONTEXT_BLOCK, $instanceid))) {
        $haspermission = true;
    } else {
        $haspermission = false;
    }
} else {
    // create a quickmail block instance
    $quickmail = block_instance('quickmail', $instance);
    $groupmode = $quickmail->groupmode();
    $haspermission = $quickmail->check_permission();
}
if (!$haspermission) {
    error('Sorry, you do not have the correct permissions to use Quickmail.');
}
if (!($courseusers = get_users_by_capability($context, 'moodle/course:view', 'u.*', 'u.lastname, u.firstname', '', '', '', '', false))) {
Beispiel #26
0
 /**
  * Get the groupmode of Quickmail.  This function pays
  * attention to the course group mode force.
  *
  * @return int The group mode of the block
  **/
 function groupmode()
 {
     return groupmode($this->course, $this->config);
 }
Beispiel #27
0
$type = required_param('type', PARAM_FILE);
// Graph Type
$group = optional_param('group', 0, PARAM_INT);
// Group ID
$sid = optional_param('sid', false, PARAM_INT);
// Student ID
$qid = optional_param('qid', 0, PARAM_INT);
// Group ID
if (!($cm = get_coursemodule_from_id('survey', $id))) {
    error("Course Module ID was incorrect");
}
if (!($course = get_record("course", "id", $cm->course))) {
    error("Course is misconfigured");
}
require_login($course->id, false, $cm);
$groupmode = groupmode($course, $cm);
// Groups are being used
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
if (!has_capability('mod/survey:readresponses', $context)) {
    if ($type != "student.png" or $sid != $USER->id) {
        error("Sorry, you aren't allowed to see this.");
    } else {
        if ($groupmode and !ismember($group)) {
            error("Sorry, you aren't allowed to see this.");
        }
    }
}
if (!($survey = get_record("survey", "id", $cm->instance))) {
    error("Survey ID was incorrect");
}
/// Check to see if groups are being used in this survey
Beispiel #28
0
function wiki_get_entry(&$wiki, &$course, $userid = 0, $groupid = 0)
{
    /// Returns the wiki entry according to the wiki type.
    /// Optionally, will return wiki entry for $userid student wiki, or
    /// $groupid group or teacher wiki.
    global $USER;
    switch ($wiki->wtype) {
        case 'student':
            /// If a specific user was requested, return it, if allowed.
            if ($userid and wiki_user_can_access_student_wiki($wiki, $userid, $course)) {
                $wentry = wiki_get_student_entry($wiki, $userid);
            } else {
                if (!($wentry = wiki_get_student_entry($wiki, $USER->id))) {
                    /*            if (wiki_is_teacher($wiki, $USER->id)) {
                                    /// If this user is a teacher, return the first entry.
                                    if ($wentries = wiki_get_entries($wiki)) {
                                        $wentry = current($wentries);
                                    }
                                }*/
                }
            }
            break;
        case 'group':
            /// If there is a groupmode, get the user's group id.
            $groupmode = groups_get_activity_groupmode($wiki);
            if ($groupmode) {
                if (!$groupid) {
                    if (($mygroupids = mygroupid($course->id)) && count($mygroupids) > 0) {
                        // Use first group. They ought to be able to change later
                        $groupid = $mygroupids[0];
                    } else {
                        // Whatever groups are in the course, pick one
                        $coursegroups = groups_get_all_groups($course->id);
                        if (!$coursegroups || count($coursegroups) == 0) {
                            error("Can't access wiki in group mode when no groups are configured for the course");
                        }
                        $unkeyed = array_values($coursegroups);
                        // Make sure first item is index 0
                        $groupid = $unkeyed[0]->id;
                    }
                }
                //echo "groupid is in wiki_get_entry ".$groupid."<br />";
                /// If a specific group was requested, return it, if allowed.
                if ($groupid and wiki_user_can_access_group_wiki($wiki, $groupid, $course)) {
                    $wentry = wiki_get_group_entry($wiki, $groupid);
                } else {
                    error("Cannot access any groups for this wiki");
                }
            } else {
                $wentry = wiki_get_group_entry($wiki, 0);
            }
            break;
        case 'teacher':
            /// If there is a groupmode, get the user's group id.
            if (groupmode($course, $wiki)) {
                $mygroupids = mygroupid($course->id);
                //same here, default to the first one
                $groupid = $groupid ? $groupid : $mygroupids[0];
            }
            /// If a specific group was requested, return it, if allowed.
            if (wiki_user_can_access_teacher_wiki($wiki, $groupid, $course)) {
                $wentry = wiki_get_teacher_entry($wiki, $groupid);
            }
            break;
    }
    return $wentry;
}
Beispiel #29
0
 function display($quiz, $cm, $course)
 {
     /// This function just displays the report
     global $CFG, $SESSION, $db, $QTYPES;
     $strnoquiz = get_string('noquiz', 'quiz');
     $strnoattempts = get_string('noattempts', 'quiz');
     /// Only print headers if not asked to download data
     $download = optional_param('download', NULL);
     if (!$download) {
         $this->print_header_and_tabs($cm, $course, $quiz, $reportmode = "analysis");
     }
     /// Construct the table for this particular report
     if (!$quiz->questions) {
         print_heading($strnoattempts);
         return true;
     }
     /// Check to see if groups are being used in this quiz
     if ($groupmode = groupmode($course, $cm)) {
         // Groups are being used
         if (!$download) {
             $currentgroup = setup_and_print_groups($course, $groupmode, "report.php?id={$cm->id}&amp;mode=analysis");
         } else {
             $currentgroup = get_and_set_current_group($course, $groupmode);
         }
     } else {
         $currentgroup = get_and_set_current_group($course, $groupmode);
     }
     // set Table and Analysis stats options
     if (!isset($SESSION->quiz_analysis_table)) {
         $SESSION->quiz_analysis_table = array('attemptselection' => 0, 'lowmarklimit' => 0, 'pagesize' => 10);
     }
     foreach ($SESSION->quiz_analysis_table as $option => $value) {
         $urlparam = optional_param($option, NULL);
         if ($urlparam === NULL) {
             ${$option} = $value;
         } else {
             ${$option} = $SESSION->quiz_analysis_table[$option] = $urlparam;
         }
     }
     $scorelimit = $quiz->sumgrades * $lowmarklimit / 100;
     // ULPGC ecastro DEBUG this is here to allow for different SQL to select attempts
     switch ($attemptselection) {
         case QUIZ_ALLATTEMPTS:
             $limit = '';
             $group = '';
             break;
         case QUIZ_HIGHESTATTEMPT:
             $limit = ', max(qa.sumgrades) ';
             $group = ' GROUP BY qa.userid ';
             break;
         case QUIZ_FIRSTATTEMPT:
             $limit = ', min(qa.timemodified) ';
             $group = ' GROUP BY qa.userid ';
             break;
         case QUIZ_LASTATTEMPT:
             $limit = ', max(qa.timemodified) ';
             $group = ' GROUP BY qa.userid ';
             break;
     }
     if ($attemptselection != QUIZ_ALLATTEMPTS) {
         $sql = 'SELECT qa.userid ' . $limit . 'FROM ' . $CFG->prefix . 'user u LEFT JOIN ' . $CFG->prefix . 'quiz_attempts qa ON u.id = qa.userid ' . 'WHERE qa.quiz = ' . $quiz->id . ' AND qa.preview = 0 ' . $group;
         $usermax = get_records_sql_menu($sql);
     }
     $groupmembers = '';
     $groupwhere = '';
     //Add this to the SQL to show only group users
     if ($currentgroup) {
         $groupmembers = ', ' . groups_members_from_sql();
         $groupwhere = ' AND ' . groups_members_where_sql($currentgroup, 'u.id');
     }
     $sql = 'SELECT  qa.* FROM ' . $CFG->prefix . 'quiz_attempts qa, ' . $CFG->prefix . 'user u ' . $groupmembers . 'WHERE u.id = qa.userid AND qa.quiz = ' . $quiz->id . ' AND qa.preview = 0 AND ( qa.sumgrades >= ' . $scorelimit . ' ) ' . $groupwhere;
     // ^^^^^^ es posible seleccionar aqu TODOS los quizzes, como quiere Jussi,
     // pero habra que llevar la cuenta ed cada quiz para restaura las preguntas (quizquestions, states)
     /// Fetch the attempts
     $attempts = get_records_sql($sql);
     if (empty($attempts)) {
         print_heading(get_string('nothingtodisplay'));
         $this->print_options_form($quiz, $cm, $attemptselection, $lowmarklimit, $pagesize);
         return true;
     }
     /// Here we rewiew all attempts and record data to construct the table
     $questions = array();
     $statstable = array();
     $questionarray = array();
     foreach ($attempts as $attempt) {
         $questionarray[] = quiz_questions_in_quiz($attempt->layout);
     }
     $questionlist = quiz_questions_in_quiz(implode(",", $questionarray));
     $questionarray = array_unique(explode(",", $questionlist));
     $questionlist = implode(",", $questionarray);
     unset($questionarray);
     foreach ($attempts as $attempt) {
         switch ($attemptselection) {
             case QUIZ_ALLATTEMPTS:
                 $userscore = 0;
                 // can be anything, not used
                 break;
             case QUIZ_HIGHESTATTEMPT:
                 $userscore = $attempt->sumgrades;
                 break;
             case QUIZ_FIRSTATTEMPT:
                 $userscore = $attempt->timemodified;
                 break;
             case QUIZ_LASTATTEMPT:
                 $userscore = $attempt->timemodified;
                 break;
         }
         if ($attemptselection == QUIZ_ALLATTEMPTS || $userscore == $usermax[$attempt->userid]) {
             $sql = "SELECT q.*, i.grade AS maxgrade, i.id AS instance" . "  FROM {$CFG->prefix}question q," . "       {$CFG->prefix}quiz_question_instances i" . " WHERE i.quiz = '{$quiz->id}' AND q.id = i.question" . "   AND q.id IN ({$questionlist})";
             if (!($quizquestions = get_records_sql($sql))) {
                 error('No questions found');
             }
             // Load the question type specific information
             if (!get_question_options($quizquestions)) {
                 error('Could not load question options');
             }
             // Restore the question sessions to their most recent states
             // creating new sessions where required
             if (!($states = get_question_states($quizquestions, $quiz, $attempt))) {
                 error('Could not restore question sessions');
             }
             $numbers = explode(',', $questionlist);
             $statsrow = array();
             foreach ($numbers as $i) {
                 if (!isset($quizquestions[$i]) or !isset($states[$i])) {
                     continue;
                 }
                 $qtype = $quizquestions[$i]->qtype == 'random' ? $states[$i]->options->question->qtype : $quizquestions[$i]->qtype;
                 $q = get_question_responses($quizquestions[$i], $states[$i]);
                 if (empty($q)) {
                     continue;
                 }
                 $qid = $q->id;
                 if (!isset($questions[$qid])) {
                     $questions[$qid]['id'] = $qid;
                     $questions[$qid]['qname'] = $quizquestions[$i]->name;
                     foreach ($q->responses as $answer => $r) {
                         $r->count = 0;
                         $questions[$qid]['responses'][$answer] = $r->answer;
                         $questions[$qid]['rcounts'][$answer] = 0;
                         $questions[$qid]['credits'][$answer] = $r->credit;
                         $statsrow[$qid] = 0;
                     }
                 }
                 $responses = get_question_actual_response($quizquestions[$i], $states[$i]);
                 foreach ($responses as $resp) {
                     if ($resp) {
                         if ($key = array_search($resp, $questions[$qid]['responses'])) {
                             $questions[$qid]['rcounts'][$key]++;
                         } else {
                             $test = new stdClass();
                             $test->responses = $QTYPES[$quizquestions[$i]->qtype]->get_correct_responses($quizquestions[$i], $states[$i]);
                             if ($key = $QTYPES[$quizquestions[$i]->qtype]->check_response($quizquestions[$i], $states[$i], $test)) {
                                 $questions[$qid]['rcounts'][$key]++;
                             } else {
                                 $questions[$qid]['responses'][] = $resp;
                                 $questions[$qid]['rcounts'][] = 1;
                                 $questions[$qid]['credits'][] = 0;
                             }
                         }
                     }
                 }
                 $statsrow[$qid] = get_question_fraction_grade($quizquestions[$i], $states[$i]);
             }
             $attemptscores[$attempt->id] = $attempt->sumgrades;
             $statstable[$attempt->id] = $statsrow;
         }
     }
     // Statistics Data table built
     unset($attempts);
     unset($quizquestions);
     unset($states);
     // now calculate statistics and set the values in the $questions array
     $top = max($attemptscores);
     $bottom = min($attemptscores);
     $gap = ($top - $bottom) / 3;
     $top -= $gap;
     $bottom += $gap;
     foreach ($questions as $qid => $q) {
         $questions[$qid] = $this->report_question_stats($q, $attemptscores, $statstable, $top, $bottom);
     }
     unset($attemptscores);
     unset($statstable);
     /// Now check if asked download of data
     if ($download = optional_param('download', NULL)) {
         $filename = clean_filename("{$course->shortname} " . format_string($quiz->name, true));
         switch ($download) {
             case "Excel":
                 $this->Export_Excel($questions, $filename);
                 break;
             case "ODS":
                 $this->Export_ODS($questions, $filename);
                 break;
             case "CSV":
                 $this->Export_CSV($questions, $filename);
                 break;
         }
     }
     /// Construct the table for this particular report
     $tablecolumns = array('id', 'qname', 'responses', 'credits', 'rcounts', 'rpercent', 'facility', 'qsd', 'disc_index', 'disc_coeff');
     $tableheaders = array(get_string('qidtitle', 'quiz_analysis'), get_string('qtexttitle', 'quiz_analysis'), get_string('responsestitle', 'quiz_analysis'), get_string('rfractiontitle', 'quiz_analysis'), get_string('rcounttitle', 'quiz_analysis'), get_string('rpercenttitle', 'quiz_analysis'), get_string('facilitytitle', 'quiz_analysis'), get_string('stddevtitle', 'quiz_analysis'), get_string('dicsindextitle', 'quiz_analysis'), get_string('disccoefftitle', 'quiz_analysis'));
     $table = new flexible_table('mod-quiz-report-itemanalysis');
     $table->define_columns($tablecolumns);
     $table->define_headers($tableheaders);
     $table->define_baseurl($CFG->wwwroot . '/mod/quiz/report.php?q=' . $quiz->id . '&amp;mode=analysis');
     $table->sortable(true);
     $table->no_sorting('rpercent');
     $table->collapsible(true);
     $table->initialbars(false);
     $table->column_class('id', 'numcol');
     $table->column_class('credits', 'numcol');
     $table->column_class('rcounts', 'numcol');
     $table->column_class('rpercent', 'numcol');
     $table->column_class('facility', 'numcol');
     $table->column_class('qsd', 'numcol');
     $table->column_class('disc_index', 'numcol');
     $table->column_class('disc_coeff', 'numcol');
     $table->column_suppress('id');
     $table->column_suppress('qname');
     $table->column_suppress('facility');
     $table->column_suppress('qsd');
     $table->column_suppress('disc_index');
     $table->column_suppress('disc_coeff');
     $table->set_attribute('cellspacing', '0');
     $table->set_attribute('id', 'itemanalysis');
     $table->set_attribute('class', 'generaltable generalbox');
     // Start working -- this is necessary as soon as the niceties are over
     $table->setup();
     $tablesort = $table->get_sql_sort();
     $sorts = explode(",", trim($tablesort));
     if ($tablesort and is_array($sorts)) {
         $sortindex = array();
         $sortorder = array();
         foreach ($sorts as $sort) {
             $data = explode(" ", trim($sort));
             $sortindex[] = trim($data[0]);
             $s = trim($data[1]);
             if ($s == "ASC") {
                 $sortorder[] = SORT_ASC;
             } else {
                 $sortorder[] = SORT_DESC;
             }
         }
         if (count($sortindex) > 0) {
             $sortindex[] = "id";
             $sortorder[] = SORT_ASC;
             foreach ($questions as $qid => $row) {
                 $index1[$qid] = $row[$sortindex[0]];
                 $index2[$qid] = $row[$sortindex[1]];
             }
             array_multisort($index1, $sortorder[0], $index2, $sortorder[1], $questions);
         }
     }
     $format_options = new stdClass();
     $format_options->para = false;
     $format_options->noclean = true;
     $format_options->newlines = false;
     // Now it is time to page the data, simply slice the keys in the array
     if (!isset($pagesize) || (int) $pagesize < 1) {
         $pagesize = 10;
     }
     $table->pagesize($pagesize, count($questions));
     $start = $table->get_page_start();
     $pagequestions = array_slice(array_keys($questions), $start, $pagesize);
     foreach ($pagequestions as $qnum) {
         $q = $questions[$qnum];
         $qid = $q['id'];
         $question = get_record('question', 'id', $qid);
         if (has_capability('moodle/question:manage', get_context_instance(CONTEXT_COURSE, $course->id))) {
             $qnumber = " (" . link_to_popup_window('/question/question.php?id=' . $qid, '&amp;cmid=' . $cm->id . 'editquestion', $qid, 450, 550, get_string('edit'), 'none', true) . ") ";
         } else {
             $qnumber = $qid;
         }
         $qname = '<div class="qname">' . format_text($question->name . " :  ", $question->questiontextformat, $format_options, $quiz->course) . '</div>';
         $qicon = print_question_icon($question, true);
         $qreview = quiz_question_preview_button($quiz, $question);
         $qtext = format_text($question->questiontext, $question->questiontextformat, $format_options, $quiz->course);
         $qquestion = $qname . "\n" . $qtext . "\n";
         $responses = array();
         foreach ($q['responses'] as $aid => $resp) {
             $response = new stdClass();
             if ($q['credits'][$aid] <= 0) {
                 $qclass = 'uncorrect';
             } elseif ($q['credits'][$aid] == 1) {
                 $qclass = 'correct';
             } else {
                 $qclass = 'partialcorrect';
             }
             $response->credit = '<span class="' . $qclass . '">(' . format_float($q['credits'][$aid], 2) . ') </span>';
             $response->text = '<span class="' . $qclass . '">' . format_text($resp, FORMAT_MOODLE, $format_options, $quiz->course) . ' </span>';
             $count = $q['rcounts'][$aid] . '/' . $q['count'];
             $response->rcount = $count;
             $response->rpercent = '(' . format_float($q['rcounts'][$aid] / $q['count'] * 100, 0) . '%)';
             $responses[] = $response;
         }
         $facility = format_float($q['facility'] * 100, 0) . "%";
         $qsd = format_float($q['qsd'], 3);
         $di = format_float($q['disc_index'], 2);
         $dc = format_float($q['disc_coeff'], 2);
         $response = array_shift($responses);
         $table->add_data(array($qnumber . "\n<br />" . $qicon . "\n " . $qreview, $qquestion, $response->text, $response->credit, $response->rcount, $response->rpercent, $facility, $qsd, $di, $dc));
         foreach ($responses as $response) {
             $table->add_data(array('', '', $response->text, $response->credit, $response->rcount, $response->rpercent, '', '', '', ''));
         }
     }
     print_heading_with_help(get_string("analysistitle", "quiz_analysis"), "itemanalysis", "quiz");
     echo '<div id="tablecontainer">';
     $table->print_html();
     echo '</div>';
     $this->print_options_form($quiz, $cm, $attemptselection, $lowmarklimit, $pagesize);
     return true;
 }
 function get_content()
 {
     global $CFG, $SITE, $COURSE, $USER;
     if (empty($CFG->bloglevel)) {
         $this->content->text = '';
         return $this->content;
     }
     if (empty($this->config->timewithin)) {
         $this->config->timewithin = BLOGDEFAULTTIMEWITHIN;
     }
     if (empty($this->config->numberoftags)) {
         $this->config->numberoftags = BLOGDEFAULTNUMBEROFTAGS;
     }
     if (empty($this->config->sort)) {
         $this->config->sort = BLOGDEFAULTSORT;
     }
     if ($this->content !== NULL) {
         return $this->content;
     }
     if (empty($this->instance)) {
         $this->content = '';
         return $this->content;
     }
     $this->content = new stdClass();
     $this->content->text = '';
     $this->content->footer = '';
     /// Get a list of tags
     $timewithin = $this->config->timewithin * 24 * 60 * 60;
     /// convert to seconds
     $sql = 'SELECT t.id, t.type, t.text, COUNT(DISTINCT bt.id) as ct ';
     $sql .= "FROM {$CFG->prefix}tags t, {$CFG->prefix}blog_tag_instance bt, {$CFG->prefix}post p ";
     $sql .= 'WHERE t.id = bt.tagid ';
     $sql .= 'AND p.id = bt.entryid ';
     $sql .= 'AND (p.publishstate = \'site\' or p.publishstate=\'public\') ';
     $sql .= "AND bt.timemodified > {$timewithin} ";
     $sql .= 'GROUP BY t.id, t.type, t.text ';
     $sql .= 'ORDER BY ct DESC, t.text ASC';
     if ($tags = get_records_sql($sql, 0, $this->config->numberoftags)) {
         /// There are 2 things to do:
         /// 1. tags with the same count should have the same size class
         /// 2. however many tags we have should be spread evenly over the
         ///    20 size classes
         $totaltags = count($tags);
         $currenttag = 0;
         $size = 20;
         $lasttagct = -1;
         $etags = array();
         foreach ($tags as $tag) {
             $currenttag++;
             if ($currenttag == 1) {
                 $lasttagct = $tag->ct;
                 $size = 20;
             } else {
                 if ($tag->ct != $lasttagct) {
                     $lasttagct = $tag->ct;
                     $size = 20 - (int) (($currenttag - 1) / $totaltags * 20);
                 }
             }
             $tag->class = "{$tag->type} s{$size}";
             $etags[] = $tag;
         }
         /// Now we sort the tag display order
         $CFG->tagsort = $this->config->sort;
         usort($etags, "blog_tags_sort");
         /// Finally we create the output
         /// Accessibility: markup as a list.
         $this->content->text .= "\n<ul class='inline-list'>\n";
         foreach ($etags as $tag) {
             switch ($CFG->bloglevel) {
                 case BLOG_USER_LEVEL:
                     $filtertype = 'user';
                     $filterselect = $USER->id;
                     break;
                 case BLOG_GROUP_LEVEL:
                     $filtertype = 'group';
                     $filterselect = get_and_set_current_group($COURSE, groupmode($COURSE));
                     break;
                 case BLOG_COURSE_LEVEL:
                     $filtertype = 'course';
                     if (isset($COURSE->id)) {
                         $filterselect = $COURSE->id;
                     } else {
                         $filterselect = $this->instance->pageid;
                     }
                     break;
                 default:
                     if (isset($COURSE->id) && $COURSE->id != SITEID) {
                         $filtertype = 'course';
                         $filterselect = $COURSE->id;
                     } else {
                         $filtertype = 'site';
                         $filterselect = SITEID;
                     }
                     break;
             }
             $link = $CFG->wwwroot . '/blog/index.php?filtertype=' . $filtertype . '&amp;filterselect=' . $filterselect . '&amp;tagid=' . $tag->id;
             $this->content->text .= '<li><a href="' . $link . '" ' . 'class="' . $tag->class . '" ' . 'title="' . get_string('numberofentries', 'blog', $tag->ct) . '">' . $tag->text . '</a></li> ';
         }
         $this->content->text .= "\n</ul>\n";
     }
     return $this->content;
 }