Permalink
Browse files

MDL-9885 'Use Permissions from Roles and Permissions System' almost d…

…one. Need to allow "Save as New Question" for those who have view permission on a question and add permission in at least one context.
  • Loading branch information...
1 parent ea7a090 commit 3281b7d07554b45a5e3e6fceaccc1ac1e67844a8 jamiesensei committed May 27, 2007
Showing with 209 additions and 0 deletions.
  1. +102 −0 question/type/match/edit_match_form.php
  2. +107 −0 question/type/missingtype/edit_missingtype_form.php
@@ -0,0 +1,102 @@
+<?php
+/**
+ * Defines the editing form for the match question type.
+ *
+ * @copyright &copy; 2007 Jamie Pratt
+ * @author Jamie Pratt me@jamiep.org
+ * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
+ * @package questionbank
+ * @subpackage questiontypes
+ */
+
+/**
+ * match editing form definition.
+ */
+class question_edit_match_form extends question_edit_form {
+ /**
+ * Add question-type specific form fields.
+ *
+ * @param object $mform the form being built.
+ */
+ function definition_inner(&$mform) {
+ $mform->addElement('advcheckbox', 'shuffleanswers', get_string('shuffle', 'quiz'), null, null, array(0,1));
+ $mform->setHelpButton('shuffleanswers', array('matchshuffle', get_string('shuffle','quiz'), 'quiz'));
+ $mform->setDefault('shuffleanswers', 1);
+
+ $mform->addElement('static', 'answersinstruct', get_string('choices', 'quiz'), get_string('filloutthreequestions', 'quiz'));
+ $mform->closeHeaderBefore('answersinstruct');
+
+ $repeated = array();
+ $repeated[] =& $mform->createElement('header', 'choicehdr', get_string('questionno', 'quiz', '{no}'));
+ $repeated[] =& $mform->createElement('textarea', 'subquestions', get_string('question', 'quiz'), array('cols'=>40, 'rows'=>3));
+ $repeated[] =& $mform->createElement('text', 'subanswers', get_string('answer', 'quiz'), array('size'=>50));
+
+ if (isset($this->question->options)){
+ $countsubquestions = count($this->question->options->subquestions);
+ } else {
+ $countsubquestions = 0;
+ }
+ if (!$mform->isFrozen()){
+ $repeatsatstart = (QUESTION_NUMANS_START > ($countsubquestions + QUESTION_NUMANS_ADD))?
+ QUESTION_NUMANS_START : ($countsubquestions + QUESTION_NUMANS_ADD);
+ } else {
+ $repeatsatstart = $countsubquestions;
+ }
+ $mform->setType('subanswer', PARAM_TEXT);
+ $mform->setType('subquestion', PARAM_TEXT);
+
+ $this->repeat_elements($repeated, $repeatsatstart, array(), 'noanswers', 'addanswers', QUESTION_NUMANS_ADD, get_string('addmoreqblanks', 'qtype_match'));
+
+ }
+
+ function set_data($question) {
+ if (isset($question->options)){
+ $subquestions = $question->options->subquestions;
+ if (count($subquestions)) {
+ $key = 0;
+ foreach ($subquestions as $subquestion){
+ $default_values['subanswers['.$key.']'] = $subquestion->answertext;
+ $default_values['subquestions['.$key.']'] = $subquestion->questiontext;
+ $key++;
+ }
+ }
+ $default_values['shuffleanswers'] = $question->options->shuffleanswers;
+ $question = (object)((array)$question + $default_values);
+ }
+ parent::set_data($question);
+ }
+
+ function qtype() {
+ return 'match';
+ }
+
+ function validation($data){
+ $errors = array();
+ $answers = $data['subanswers'];
+ $questions = $data['subquestions'];
+ $questioncount = 0;
+ foreach ($questions as $key => $question){
+ $trimmedquestion = trim($question);
+ $trimmedanswer = trim($answers[$key]);
+ if (!empty($trimmedanswer) && !empty($trimmedquestion)){
+ $questioncount++;
+ }
+ if (!empty($trimmedquestion) && empty($trimmedanswer)){
+ $errors['subanswers['.$key.']'] = get_string('nomatchinganswerforq', 'qtype_match', $trimmedquestion);
+ }
+ }
+ if ($questioncount==0){
+ $errors['subquestions[0]'] = get_string('notenoughquestions', 'qtype_match', 3);
+ $errors['subquestions[1]'] = get_string('notenoughquestions', 'qtype_match', 3);
+ $errors['subquestions[2]'] = get_string('notenoughquestions', 'qtype_match', 3);
+ } elseif ($questioncount==1){
+ $errors['subquestions[1]'] = get_string('notenoughquestions', 'qtype_match', 3);
+ $errors['subquestions[2]'] = get_string('notenoughquestions', 'qtype_match', 3);
+
+ } elseif ($questioncount==2){
+ $errors['subquestions[2]'] = get_string('notenoughquestions', 'qtype_match', 3);
+ }
+ return $errors;
+ }
+}
+?>
@@ -0,0 +1,107 @@
+<?php
+/**
+ * Defines the editing form for the missingtype question type.
+ *
+ * @copyright &copy; 2007 Jamie Pratt
+ * @author Jamie Pratt me@jamiep.org
+ * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
+ * @package questionbank
+ * @subpackage questiontypes
+ */
+
+/**
+ * missingtype editing form definition.
+ */
+class question_edit_missingtype_form extends question_edit_form {
+ /**
+ * Add question-type specific form fields.
+ *
+ * @param object $mform the form being built.
+ */
+ function definition_inner(&$mform) {
+ $creategrades = get_grade_options();
+ $gradeoptions = $creategrades->gradeoptionsfull;
+ $repeated = array();
+ $repeated[] =& $mform->createElement('header', 'choicehdr', get_string('choiceno', 'qtype_multichoice', '{no}'));
+ $repeated[] =& $mform->createElement('text', 'answer', get_string('answer', 'quiz'));
+ $repeated[] =& $mform->createElement('select', 'fraction', get_string('grade'), $gradeoptions);
+ $repeated[] =& $mform->createElement('htmleditor', 'feedback', get_string('feedback', 'quiz'));
+
+ if (isset($this->question->options)){
+ $countanswers = count($this->question->options->answers);
+ } else {
+ $countanswers = 0;
+ }
+ if (!$mform->isFrozen()){
+ $repeatsatstart = (QUESTION_NUMANS_START > ($countanswers + QUESTION_NUMANS_ADD))?
+ QUESTION_NUMANS_START : ($countanswers + QUESTION_NUMANS_ADD);
+ } else {
+ $repeatsatstart = $countanswers;
+ }
+ $repeatedoptions = array();
+ $repeatedoptions['fraction']['default'] = 0;
+ $mform->setType('answer', PARAM_NOTAGS);
+ $this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions, 'noanswers', 'addanswers', QUESTION_NUMANS_ADD, get_string('addmorechoiceblanks', 'qtype_multichoice'));
+ }
+
+ function set_data($question) {
+ if (isset($question->options)){
+ $answers = $question->options->answers;
+ if (count($answers)) {
+ $key = 0;
+ foreach ($answers as $answer){
+ $default_values['answer['.$key.']'] = $answer->answer;
+ $default_values['fraction['.$key.']'] = $answer->fraction;
+ $default_values['feedback['.$key.']'] = $answer->feedback;
+ $key++;
+ }
+ }
+ $question = (object)((array)$question + $default_values);
+ }
+ parent::set_data($question);
+ }
+
+ function qtype() {
+ return 'missingtype';
+ }
+
+ function validation($data){
+ $errors = array();
+ $answers = $data['answer'];
+ $answercount = 0;
+
+ $totalfraction = 0;
+ $maxfraction = -1;
+
+ foreach ($answers as $key => $answer){
+ //check no of choices
+ $trimmedanswer = trim($answer);
+ if (!empty($trimmedanswer)){
+ $answercount++;
+ }
+ //check grades
+ if ($answer != '') {
+ if ($data['fraction'][$key] > 0) {
+ $totalfraction += $data['fraction'][$key];
+ }
+ if ($data['fraction'][$key] > $maxfraction) {
+ $maxfraction = $data['fraction'][$key];
+ }
+ }
+ }
+
+ if ($answercount==0){
+ $errors['answer[0]'] = get_string('notenoughanswers', 'qtype_multichoice', 1);
+ }
+
+
+ $totalfraction = round($totalfraction, 2);
+ if ($totalfraction != 1) {
+ $totalfraction = $totalfraction * 100;
+ $errors['fraction[0]'] = get_string('errfractionsaddwrong', 'qtype_multichoice', $totalfraction);
+ }
+
+ return $errors;
+ }
+}
+?>

0 comments on commit 3281b7d

Please sign in to comment.