$usercontexts = get_parent_contexts($context);
$listofcontexts = '('.implode(',', $usercontexts).')';
} else { // else, we load everything
+ echo $userid;
$userroles = get_records('role_assignments','userid',$userid);
+
$usercontexts = array();
foreach ($userroles as $userrole) {
$usercontexts[] = $userrole->contextid;
aggregatelevel ASC
";
-
$capabilities = array(); // Reinitialize.
$rs = get_recordset_sql($SQL);
-
+
if ($rs && $rs->RecordCount() > 0) {
while (!$rs->EOF) {
$array = $rs->fields;
require_login($course->id, false, $cm);
$isteacher = isteacher($course->id);
-
+
+ $coursecontext = get_context_instance(CONTEXT_COURSE, $id); // course context
+ $context = get_context_instance(CONTEXT_MODULE, $cm->id);
// if no questions have been set up yet redirect to edit.php
- if (!$quiz->questions and isteacheredit($course->id)) {
+ if (!$quiz->questions and has_capability('mod/quiz:manage', $context) {
redirect('edit.php?quizid='.$quiz->id);
}
print_header($course->shortname.': '.format_string($quiz->name), '', '', '', '', false, '', '', false, '');
include('protect_js.php');
} else {
- $strupdatemodule = isteacheredit($course->id)
+ $strupdatemodule = has_capability('moodle/course:manageactivities', $coursecontext)
? update_module_button($cm->id, $course->id, get_string('modulename', 'quiz'))
: "";
print_header_simple(format_string($quiz->name), "",
echo '<div id="overDiv" style="position:absolute; visibility:hidden; z-index:1000;"></div>'; // for overlib
/// Print the quiz name heading and tabs for teacher
- if ($isteacher) {
+ if (has_capability('mod/quiz:preview', $context)) {
$currenttab = 'preview';
include('tabs.php');
} else {
/// Load attempt or create a new attempt if there is no unfinished one
- if ($isteacher and $forcenew) { // teacher wants a new preview
+ if (has_capability('mod/quiz:preview', $context) and $forcenew) { // teacher wants a new preview
// so we set a finish time on the current attempt (if any).
// It will then automatically be deleted below
set_field('quiz_attempts', 'timefinish', $timestamp, 'quiz', $quiz->id, 'userid', $USER->id);
if (!$attempt) {
// Check if this is a preview request from a teacher
// in which case the previous previews should be deleted
- if ($isteacher) {
+ if (has_capability('mod/quiz:preview', $context) {
if ($oldattempts = get_records_select('quiz_attempts', "quiz = '$quiz->id'
AND userid = '$USER->id'")) {
delete_records('quiz_attempts', 'quiz', $quiz->id, 'userid', $USER->id);
// Start a new attempt and initialize the question sessions
$attempt = quiz_create_attempt($quiz, $attemptnumber);
// If this is an attempt by a teacher mark it as a preview
- if ($isteacher) {
+ if (has_capability('mod/quiz:preview', $context)) {
$attempt->preview = 1;
}
// Save the attempt
/// Print the quiz page ////////////////////////////////////////////////////////
/// Print the preview heading
- if ($isteacher) {
+ if (has_capability('mod/quiz:preview', $context)) {
print_heading(get_string('previewquiz', 'quiz', format_string($quiz->name)));
unset($buttonoptions);
$buttonoptions['q'] = $quiz->id;
require_login($course->id);
- if (!isteacher($course->id)) {
- error('This page is for teachers only');
- }
+ $cm = get_coursemodule_from_instance('quiz', $quiz->id);
+ $context = get_context_instance(CONTEXT_MODULE, $cm->id);
+
+ require_capability('mod/quiz:grade', $context);
// Load question
if (! $question = get_record('question', 'id', $questionid)) {
if (! $course = get_record("course", "id", $modform->course)) {
error("This course doesn't exist");
}
+
+ $coursecontext = get_context_instance(CONTEXT_COURSE, $id);
require_login($course->id, false);
- if (!isteacheredit($course->id)) {
- error("You can't modify this course!");
- }
+ $context = get_context_instance(CONTEXT_MODULE, $cm->id);
+ require_capability('mod/quiz:manage', $context);
if (isset($modform->instance)
&& empty($modform->grades)) // Construct an array to hold all the grades.
if (isset($modform->instance) and record_exists_sql("SELECT * FROM {$CFG->prefix}quiz_attempts WHERE quiz = '$modform->instance' AND preview = '0' LIMIT 1")){
// one column layout with table of questions used in this quiz
- $strupdatemodule = isteacheredit($course->id)
+ $strupdatemodule = has_capability('moodle/course:manageactivities', $coursecontext)
? update_module_button($modform->cmid, $course->id, get_string('modulename', 'quiz'))
: "";
print_header_simple($streditingquiz, '',
}
// two column layout with quiz info in left column
- $strupdatemodule = isteacheredit($course->id)
+ $strupdatemodule = has_capability('moodle/course:manageactivities', $coursecontext)
? update_module_button($modform->cmid, $course->id, get_string('modulename', 'quiz'))
: "";
print_header_simple($streditingquiz, '',
$strattempts = get_string("attempts", "quiz");
$strusers = $course->students;
- if (isteacher($course->id)) {
+ $context = get_context_instance(CONTEXT_COURSE, $id);
+ if (has_capability('mod/quiz:viewreports', $context)) {
$gradecol = $strattempts;
} else {
$gradecol = $strbestgrade;
$currentsection = "";
foreach ($quizzes as $quiz) {
+
+ $cm = get_coursemodule_from_instance('quiz', $quiz->id);
+ $context = get_context_instance(CONTEXT_MODULE, $cm->id);
+
if (!$quiz->visible) {
//Show dimmed if the mod is hidden
$link = "<a class=\"dimmed\" href=\"view.php?id=$quiz->coursemodule\">".format_string($quiz->name,true)."</a>";
$closequiz = $quiz->timeclose ? userdate($quiz->timeclose) : '';
- if (isteacher($course->id)) {
+ if (has_capability('mod/quiz:viewreports', $context)) {
if ($a->attemptnum = count_records('quiz_attempts', 'quiz', $quiz->id, 'preview', 0)) {
$a->studentnum = count_records_select('quiz_attempts', "quiz = '$quiz->id' AND preview = '0'", 'COUNT(DISTINCT userid)');
$a->studentstring = $course->students;
}
require_login($course->id, false);
-
- if (!isteacher($course->id)) {
- error("You are not allowed to use this script");
- }
+ $context = get_context_instance(CONTEXT_MODULE, $cm->id);
+ require_capability('mod/quiz:viewre ports', $context);
// if no questions have been set up yet redirect to edit.php
- if (!$quiz->questions and isteacheredit($course->id)) {
+ if (!$quiz->questions and has_capability('mod/quiz:manage', $context)) {
redirect('edit.php?quizid='.$quiz->id);
}
}
require_login($course->id, false, $cm);
+ $context = get_context_instance(CONTEXT_MODULE, $cm->id);
+ $coursecontext = get_context_instance(CONTEXT_COURSE, $id);
$isteacher = isteacher($course->id);
$options = quiz_get_reviewoptions($quiz, $attempt, $isteacher);
$popup = $isteacher ? 0 : $quiz->popup; // Controls whether this is shown in a javascript-protected window.
- if (!$isteacher) {
+ // this capability is not correct, need to find the right one
+ if (!has_capability('mod/quiz:manage', $context)) {
if (!$attempt->timefinish) {
redirect('attempt.php?q='.$quiz->id);
}
/// Include Javascript protection for this page
include('protect_js.php');
} else {
- $strupdatemodule = isteacheredit($course->id)
+ $strupdatemodule = has_capability('moodle/course:manageactivities', $coursecontext)
? update_module_button($cm->id, $course->id, get_string('modulename', 'quiz'))
: "";
print_header_simple(format_string($quiz->name), "",
echo '<div id="overDiv" style="position:absolute; visibility:hidden; z-index:1000;"></div>'; // for overlib
/// Print heading and tabs if this is part of a preview
- if ($isteacher) {
+ if (has_capability('mod/quiz:preview', $context)) {
if ($attempt->userid == $USER->id) { // this is the report on a preview
$currenttab = 'preview';
} else {
$picture = print_user_picture($student->id, $course->id, $student->picture, false, true);
$table->data[] = array($picture, '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$student->id.'&course='.$course->id.'">'.fullname($student, true).'</a>');
}
- if ($isteacher and count($attempts = get_records_select('quiz_attempts', "quiz = '$quiz->id' AND userid = '$attempt->userid'", 'attempt ASC')) > 1) {
+ if (has_capability('mod/quiz:grade', $context) and count($attempts = get_records_select('quiz_attempts', "quiz = '$quiz->id' AND userid = '$attempt->userid'", 'attempt ASC')) > 1) {
// print list of attempts
$attemptlist = '';
foreach ($attempts as $at) {
$tabs = array();
$row = array();
$inactive = array();
-
- $row[] = new tabobject('info', "$CFG->wwwroot/mod/quiz/view.php?q=$quiz->id", get_string('info', 'quiz'));
+
if (has_capability('mod/quiz:viewreports', $context)) {
+ $row[] = new tabobject('info', "$CFG->wwwroot/mod/quiz/view.php?q=$quiz->id", get_string('info', 'quiz'));
$row[] = new tabobject('reports', "$CFG->wwwroot/mod/quiz/report.php?q=$quiz->id", get_string('results', 'quiz'));
}
- $row[] = new tabobject('preview', "$CFG->wwwroot/mod/quiz/attempt.php?q=$quiz->id", get_string('preview', 'quiz'));
- if (has_capability('mod/quiz:manage', $context)) {
+ if (has_capability('mod/quiz:viewreports', $context)) {
+ $row[] = new tabobject('preview', "$CFG->wwwroot/mod/quiz/attempt.php?q=$quiz->id", get_string('preview', 'quiz'));
+ }
+ if (has_capability('mod/quiz:preview', $context)) {
$row[] = new tabobject('edit', "$CFG->wwwroot/mod/quiz/edit.php?quizid=$quiz->id", get_string('edit'));
}
error("The course module for the quiz with id $q is missing");
}
}
-
+
require_login($course->id, false, $cm);
+ $context = get_context_instance(CONTEXT_MODULE, $cm->id);
$isteacher = isteacher($course->id);
// if no questions have been set up yet redirect to edit.php
- if (!$quiz->questions and isteacheredit($course->id)) {
+ if (!$quiz->questions and has_capability('mod/quiz:manage', $context)) {
redirect('edit.php?quizid='.$quiz->id);
}
// Print the main part of the page
// Print heading and tabs for teacher
- if ($isteacher) {
- $currenttab = 'info';
- include('tabs.php');
- }
+
+ $currenttab = 'info';
+ include('tabs.php');
+
print_heading(format_string($quiz->name));
if (trim(strip_tags($quiz->intro))) {
// This is all the teacher will get
- if ($isteacher) {
+ if (has_capability('mod/quiz:manage', $context)) {
if ($a->attemptnum = count_records('quiz_attempts', 'quiz', $quiz->id, 'preview', 0)) {
$a->studentnum = count_records_select('quiz_attempts', "quiz = '$quiz->id' AND preview = '0'", 'COUNT(DISTINCT userid)');
$a->studentstring = $course->students;
exit;
}
- if (isguest()) {
-
- $wwwroot = $CFG->wwwroot.'/login/index.php';
- if (!empty($CFG->loginhttps)) {
- $wwwroot = str_replace('http:','https:', $wwwroot);
- }
-
- notice_yesno(get_string('guestsno', 'quiz').'<br /><br />'.get_string('liketologin'),
- $wwwroot, $_SERVER['HTTP_REFERER']);
- print_footer($course);
- echo '</td></tr></table>';
- exit;
- }
-
if ($attempts = quiz_get_user_attempts($quiz->id, $USER->id)) {
$numattempts = count($attempts);
} else {
if ($attempt->timefinish > 0) { // attempt has finished
$timetaken = format_time($attempt->timefinish - $attempt->timestart);
$datecompleted = userdate($attempt->timefinish);
- } else if ($available) { // The student can continue this attempt, so put appropriate link
+ } else if ($available && has_capability('mod/quiz:attempt', $context)) { // The student can continue this attempt, so put appropriate link
$timetaken = format_time(time() - $attempt->timestart);
$datecompleted = "\n".'<script language="javascript" type="text/javascript">';
$datecompleted .= "\n<!--\n"; // -->
$lastattempt = $lastattempt_obj->timefinish;
}
if($numattempts == 1 && $quiz->delay1) {
- if ($timenow - $quiz->delay1 > $lastattempt) {
- print_start_quiz_button($quiz, $attempts, $numattempts, $unfinished, $cm);
+ if (($timenow - $quiz->delay1 > $lastattempt) && has_capability('mod/quiz:attempt', $context)) {
+ print_start_quiz_button($quiz, $attempts, $numattempts, $unfinished, $cm);
} else {
$notify_msg = get_string('temporaryblocked', 'quiz') . '<b>'. userdate($lastattempt + $quiz->delay1). '<b>';
print_simple_box($notify_msg, "center");
}
} else if($numattempts > 1 && $quiz->delay2) {
- if ($timenow - $quiz->delay2 > $lastattempt) {
+ if (($timenow - $quiz->delay2 > $lastattempt) && has_capability('mod/quiz:attempt', $context)) {
print_start_quiz_button($quiz, $attempts, $numattempts, $unfinished, $cm);
} else {
$notify_msg = get_string('temporaryblocked', 'quiz') . '<b>'. userdate($lastattempt + $quiz->delay2). '<b>';
print_simple_box($notify_msg, "center");
}
- } else {
+ } elseif (has_capability('mod/quiz:attempt', $context)) {
print_start_quiz_button($quiz, $attempts, $numattempts, $unfinished, $cm);
}
- } else {
+ } elseif (has_capability('mod/quiz:attempt', $context)) {
print_start_quiz_button($quiz, $attempts, $numattempts, $unfinished, $cm);
}
echo "</div>\n";
$comment = stripslashes($state->comment);
$commentlink = '';
- if (isset($options->questioncommentlink)) {
+
+ $cm = get_coursemodule_from_instance('quiz', $quiz->id);
+ $context = get_context_instance(CONTEXT_MODULE, $context);
+
+ if (isset($options->questioncommentlink) && has_capability('mod/quiz:grade', $context)) {
$strcomment = get_string('commentorgrade', 'quiz');
$commentlink = '<div class="commentlink">'.link_to_popup_window ($options->questioncommentlink.'?attempt='.$state->attempt.'&question='.$question->id,
'commentquestion', $strcomment, 450, 650, $strcomment, 'none', true).'</div>';