/** * @param object $context a context * @return string A URL for editing questions in this context. */ function question_edit_url($context) { global $CFG, $SITE; if (!has_any_capability(question_get_question_capabilities(), $context)) { return false; } $baseurl = $CFG->wwwroot . '/question/edit.php?'; $defaultcategory = question_get_default_category($context->id); if ($defaultcategory) { $baseurl .= 'cat=' . $defaultcategory->id . ',' . $context->id . '&'; } switch ($context->contextlevel) { case CONTEXT_SYSTEM: return $baseurl . 'courseid=' . $SITE->id; case CONTEXT_COURSECAT: // This is nasty, becuase we can only edit questions in a course // context at the moment, so for now we just return false. return false; case CONTEXT_COURSE: return $baseurl . 'courseid=' . $context->instanceid; case CONTEXT_MODULE: return $baseurl . 'cmid=' . $context->instanceid; } }
protected function get_default_category($cid) { $context = context_course::instance($cid); $result = question_get_default_category($context->id); $result = $result ? $result : question_make_default_categories(array($context)); return $result; }
/** * Gets the default category in the most specific context. * If no categories exist yet then default ones are created in all contexts. * * @param array $contexts The context objects for this context and all parent contexts. * @return object The default category - the category in the course context */ function question_make_default_categories($contexts) { global $DB; static $preferredlevels = array(CONTEXT_COURSE => 4, CONTEXT_MODULE => 3, CONTEXT_COURSECAT => 2, CONTEXT_SYSTEM => 1); $toreturn = null; $preferredness = 0; // If it already exists, just return it. foreach ($contexts as $key => $context) { if (!($exists = $DB->record_exists("question_categories", array('contextid' => $context->id)))) { // Otherwise, we need to make one $category = new stdClass(); $contextname = print_context_name($context, false, true); $category->name = get_string('defaultfor', 'question', $contextname); $category->info = get_string('defaultinfofor', 'question', $contextname); $category->contextid = $context->id; $category->parent = 0; $category->sortorder = 999; // By default, all categories get this number, and are sorted alphabetically. $category->stamp = make_unique_id_code(); if (!($category->id = $DB->insert_record('question_categories', $category))) { print_error('cannotcreatedefaultcat', '', '', print_context_name($context)); } } else { $category = question_get_default_category($context->id); } if ($preferredlevels[$context->contextlevel] > $preferredness && has_any_capability(array('moodle/question:usemine', 'moodle/question:useall'), $context)) { $toreturn = $category; $preferredness = $preferredlevels[$context->contextlevel]; } } if (!is_null($toreturn)) { $toreturn = clone $toreturn; } return $toreturn; }
/** * Returns welcome message * @return string welcome message */ public static function get_quiz() { global $USER, $DB, $CFG; $randomcolors = array('green', 'yellow', 'blue', 'red', 'purple'); $randomicons = array('entertainment', 'sports', 'music', 'tvmovies', 'science', 'geography', 'history', 'knowledge', 'food'); $randomsingletypes = array('single-select-item', 'single-select'); //Parameter validation $params = self::validate_parameters(self::get_quiz_parameters(), array()); //Context validation //self::validate_context(context_user::get_instance($USER->id)); //Capability checking //if (!has_capability('moodle/user:viewdetails', $context)) { // throw new moodle_exception('cannotviewprofile'); //} $courses = enrol_get_users_courses($USER->id, true, 'id, shortname, fullname, idnumber, visible'); $result = array(); foreach ($courses as $course) { // Find all questions the user is allowed to see $coursecontext = context_course::instance($course->id); $questions = array(); require_once $CFG->dirroot . "/question/editlib.php"; $category = question_get_default_category($coursecontext->id); $catquestions = get_questions_category($category); // only keep multichoice yet. foreach ($catquestions as $question) { if ($question->qtype == "multichoice") { $questions[$question->id] = $question; } } if (!empty($questions)) { // pick random theme color and icon. $color = $randomcolors[array_rand($randomcolors)]; $icon = $randomicons[array_rand($randomicons)]; $quizresult = new stdClass(); $quizresult->name = $course->shortname; $quizresult->id = $icon; $quizresult->theme = $color; $quizresult->quizzes = array(); // Only get 8 questions maxim $i = 0; foreach ($questions as $question) { if ($i < 8) { $i++; // Convert them into JSON expected code $questionresult = new stdClass(); $questionresult->question = clean_param($question->questiontext, PARAM_TEXT); $questionresult->options = array(); $answernum = 0; $jsonanswer = array(); foreach ($question->options->answers as $answer) { // Find out if the answer is the correct one (obviouly we don't support multiple answer yet) if ($answer->fraction > 0) { $jsonanswer[] = $answernum; } $questionresult->options[] = clean_param($answer->answer, PARAM_TEXT); $answernum++; } // Detect moodeka quiz type. if (count($jsonanswer) > 1) { $type = 'multi-select'; } else { if ($answernum == 4 and rand(0, 1)) { $type = 'four-quarter'; } else { $type = $randomsingletypes[array_rand($randomsingletypes)]; } } $questionresult->answer = $jsonanswer; $questionresult->type = $type; $quizresult->quizzes[] = $questionresult; } } $result[] = $quizresult; } } return $result; }