]> git.mjollnir.org Git - moodle.git/commitdiff
some quiz capabilities
authortoyomoyo <toyomoyo>
Tue, 22 Aug 2006 09:04:23 +0000 (09:04 +0000)
committertoyomoyo <toyomoyo>
Tue, 22 Aug 2006 09:04:23 +0000 (09:04 +0000)
lib/accesslib.php
mod/quiz/attempt.php
mod/quiz/comment.php
mod/quiz/edit.php
mod/quiz/index.php
mod/quiz/report.php
mod/quiz/review.php
mod/quiz/tabs.php
mod/quiz/view.php
question/type/questiontype.php

index fcdaab999d7b17685189e43b79a3305d5a9d385e..20a5c25488fac2121c1156fa22549649a4f5b25f 100755 (executable)
@@ -358,7 +358,9 @@ function load_user_capability($capability='', $context ='', $userid='') {
         $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;
@@ -415,10 +417,9 @@ function load_user_capability($capability='', $context ='', $userid='') {
                      aggregatelevel ASC
             ";
 
-
     $capabilities = array();  // Reinitialize.
     $rs = get_recordset_sql($SQL);
-    
+
     if ($rs && $rs->RecordCount() > 0) {
         while (!$rs->EOF) {
               $array = $rs->fields;
index f9387937b653b3f6675177dc067c0444f2c81d8d..34409901b6d5442a528efe04f9c67d2ad3855048 100644 (file)
 
     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);
     }
 
@@ -81,7 +83,7 @@
         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), "",
@@ -93,7 +95,7 @@
     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;
index 34f0d1806f1bfd360e74872f9113be53add593ad..e1a960ac252b5c63a019b320e4289f9b6ed2860d 100644 (file)
 
     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)) {
index a930a19565c69551657fdb66b8b0bd452a55c9b6..5109cafe897f572f5d30e6bda0e12d45cba0700e 100644 (file)
@@ -99,12 +99,13 @@ if (self.name == 'editquestion') {
     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.
@@ -309,7 +310,7 @@ if (self.name == 'editquestion') {
 
     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, '',
@@ -346,7 +347,7 @@ if (self.name == 'editquestion') {
     }
 
     // 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, '',
index e8f2986f8e3b3e78afe2cca6caf21d68aca92114..33dce3e16e194fddf68da5fe515f24256ad3459b 100644 (file)
@@ -57,7 +57,8 @@
     $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;
index 80e7d8ac056ffe526208432eb7dbdaf0b0c847f4..fedeb30684bde23110e9ccc3cb9677e7eb85e125 100644 (file)
     }
 
     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);
     }
 
index e6a4bf6928957e512308caf7479644655ba7e203..7905d2440724a4a9590af58de4d35bf16b05aaa5 100644 (file)
     }
 
     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);
         }
@@ -90,7 +93,7 @@
         /// 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.'&amp;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) {
index 1b60098de3d6819dee283c47344ca7a47d07af62..f4c5367418b107f2e3559498928638f6d33bd09d 100644 (file)
     $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'));
     }
 
index 2219f4c4af2db582e3141c1dcf2bd2fb3feaa4c1..36f6b37d1c3420221945e629d8d81d0190366ea2 100644 (file)
             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";
index 8e97e89044f601b34b946ec9c014fefe8a175a40..8c09476063d3bed5cf7936cef2dab1f2c567c926 100644 (file)
@@ -525,7 +525,11 @@ class default_questiontype {
         
         $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.'&amp;question='.$question->id,
                              'commentquestion', $strcomment, 450, 650, $strcomment, 'none', true).'</div>';