Exemplo n.º 1
0
 /**
  * Export this data so it can be used as the context for a mustache template.
  *
  * @param renderer_base $output
  * @return stdClass
  */
 public function export_for_template(renderer_base $output)
 {
     global $CFG;
     require_once $CFG->libdir . '/externallib.php';
     $r = new stdClass();
     $r->id = (int) $this->record->id;
     $r->rawname = clean_param($this->record->rawname, PARAM_TAG);
     $r->name = clean_param($this->record->name, PARAM_TAG);
     $format = clean_param($this->record->descriptionformat, PARAM_INT);
     list($r->description, $r->descriptionformat) = external_format_text($this->record->description, $format, \context_system::instance()->id, 'core', 'tag', $r->id);
     $r->flag = clean_param($this->record->flag, PARAM_INT);
     if (isset($this->record->official)) {
         $r->official = clean_param($this->record->official, PARAM_INT);
     } else {
         $r->official = $this->record->tagtype === 'official' ? 1 : 0;
     }
     $url = new moodle_url('/tag/index.php', array('id' => $this->record->id));
     $r->viewurl = $url->out(false);
     $manageurl = new moodle_url('/tag/manage.php', array('sesskey' => sesskey(), 'tagid' => $this->record->id));
     $url = new moodle_url($manageurl);
     $url->param('action', 'changetype');
     $url->param('tagtype', $r->official ? 'default' : 'official');
     $r->changetypeurl = $url->out(false);
     $url = new moodle_url($manageurl);
     $url->param('action', $this->record->flag ? 'resetflag' : 'setflag');
     $r->changeflagurl = $url->out(false);
     return $r;
 }
Exemplo n.º 2
0
 /**
  * Returns a list of glossaries in a provided list of courses.
  *
  * If no list is provided all glossaries that the user can view will be returned.
  *
  * @param array $courseids the course IDs.
  * @return array of glossaries
  * @since Moodle 3.1
  */
 public static function get_glossaries_by_courses($courseids = array())
 {
     $params = self::validate_parameters(self::get_glossaries_by_courses_parameters(), array('courseids' => $courseids));
     $warnings = array();
     $courses = array();
     $courseids = $params['courseids'];
     if (empty($courseids)) {
         $courses = enrol_get_my_courses();
         $courseids = array_keys($courses);
     }
     // Array to store the glossaries to return.
     $glossaries = array();
     // Ensure there are courseids to loop through.
     if (!empty($courseids)) {
         list($courses, $warnings) = external_util::validate_courses($courseids, $courses);
         // Get the glossaries in these courses, this function checks users visibility permissions.
         $glossaries = get_all_instances_in_courses('glossary', $courses);
         foreach ($glossaries as $glossary) {
             $context = context_module::instance($glossary->coursemodule);
             $glossary->name = external_format_string($glossary->name, $context->id);
             list($glossary->intro, $glossary->introformat) = external_format_text($glossary->intro, $glossary->introformat, $context->id, 'mod_glossary', 'intro', null);
         }
     }
     $result = array();
     $result['glossaries'] = $glossaries;
     $result['warnings'] = $warnings;
     return $result;
 }
Exemplo n.º 3
0
 /**
  * Export this data so it can be used as the context for a mustache template.
  *
  * @param renderer_base $output
  * @return stdClass
  */
 public function export_for_template(renderer_base $output)
 {
     global $CFG;
     require_once $CFG->libdir . '/externallib.php';
     $r = new stdClass();
     $r->id = (int) $this->record->id;
     $r->tagcollid = clean_param($this->record->tagcollid, PARAM_INT);
     $r->rawname = clean_param($this->record->rawname, PARAM_TAG);
     $r->name = clean_param($this->record->name, PARAM_TAG);
     $format = clean_param($this->record->descriptionformat, PARAM_INT);
     list($r->description, $r->descriptionformat) = external_format_text($this->record->description, $format, \context_system::instance()->id, 'core', 'tag', $r->id);
     $r->flag = clean_param($this->record->flag, PARAM_INT);
     if (isset($this->record->isstandard)) {
         $r->isstandard = clean_param($this->record->isstandard, PARAM_INT) ? 1 : 0;
     }
     $r->official = $r->isstandard;
     // For backwards compatibility.
     $url = core_tag_tag::make_url($r->tagcollid, $r->rawname);
     $r->viewurl = $url->out(false);
     $manageurl = new moodle_url('/tag/manage.php', array('sesskey' => sesskey(), 'tagid' => $this->record->id));
     $url = new moodle_url($manageurl);
     $url->param('action', 'changetype');
     $url->param('isstandard', $r->isstandard ? 0 : 1);
     $r->changetypeurl = $url->out(false);
     $url = new moodle_url($manageurl);
     $url->param('action', $this->record->flag ? 'resetflag' : 'setflag');
     $r->changeflagurl = $url->out(false);
     return $r;
 }
Exemplo n.º 4
0
 /**
  * Return a list of comments
  *
  * @param string $contextlevel ('system, course, user', etc..)
  * @param int $instanceid
  * @param string $component the name of the component
  * @param int $itemid the item id
  * @param string $area comment area
  * @param int $page page number
  * @return array of comments and warnings
  * @since Moodle 2.9
  */
 public static function get_comments($contextlevel, $instanceid, $component, $itemid, $area = '', $page = 0)
 {
     $warnings = array();
     $arrayparams = array('contextlevel' => $contextlevel, 'instanceid' => $instanceid, 'component' => $component, 'itemid' => $itemid, 'area' => $area, 'page' => $page);
     $params = self::validate_parameters(self::get_comments_parameters(), $arrayparams);
     $context = self::get_context_from_params($params);
     self::validate_context($context);
     require_capability('moodle/comment:view', $context);
     $args = new stdClass();
     $args->context = $context;
     $args->area = $params['area'];
     $args->itemid = $params['itemid'];
     $args->component = $params['component'];
     $commentobject = new comment($args);
     $comments = $commentobject->get_comments($params['page']);
     // False means no permissions to see comments.
     if ($comments === false) {
         throw new moodle_exception('nopermissions', 'error', '', 'view comments');
     }
     foreach ($comments as $key => $comment) {
         list($comments[$key]->content, $comments[$key]->format) = external_format_text($comment->content, $comment->format, $context->id, $params['component'], '', 0);
     }
     $results = array('comments' => $comments, 'warnings' => $warnings);
     return $results;
 }
Exemplo n.º 5
0
 /**
  * Export the step configuration.
  *
  * @param   renderer_base   $output     The renderer.
  * @return  object
  */
 public function export_for_template(\renderer_base $output)
 {
     global $PAGE;
     $step = $this->step;
     $result = (object) ['stepid' => $step->get_id(), 'title' => external_format_text(stepsource::get_string_from_input($step->get_title()), FORMAT_HTML, $PAGE->context->id, 'tool_usertours')[0], 'content' => external_format_text(stepsource::get_string_from_input($step->get_content()), FORMAT_HTML, $PAGE->context->id, 'tool_usertours')[0], 'element' => $step->get_target()->convert_to_css()];
     $result->content = str_replace("\n", "<br>\n", $result->content);
     foreach ($step->get_config_keys() as $key) {
         $result->{$key} = $step->get_config($key);
     }
     return $result;
 }
Exemplo n.º 6
0
 /**
  * Returns a list of wikis in a provided list of courses,
  * if no list is provided all wikis that the user can view will be returned.
  *
  * @param array $courseids The courses IDs.
  * @return array Containing a list of warnings and a list of wikis.
  * @since Moodle 3.1
  */
 public static function get_wikis_by_courses($courseids = array())
 {
     $returnedwikis = array();
     $warnings = array();
     $params = self::validate_parameters(self::get_wikis_by_courses_parameters(), array('courseids' => $courseids));
     $mycourses = array();
     if (empty($params['courseids'])) {
         $mycourses = enrol_get_my_courses();
         $params['courseids'] = array_keys($mycourses);
     }
     // Ensure there are courseids to loop through.
     if (!empty($params['courseids'])) {
         list($courses, $warnings) = external_util::validate_courses($params['courseids'], $mycourses);
         // Get the wikis in this course, this function checks users visibility permissions.
         // We can avoid then additional validate_context calls.
         $wikis = get_all_instances_in_courses('wiki', $courses);
         foreach ($wikis as $wiki) {
             $context = context_module::instance($wiki->coursemodule);
             // Entry to return.
             $module = array();
             // First, we return information that any user can see in (or can deduce from) the web interface.
             $module['id'] = $wiki->id;
             $module['coursemodule'] = $wiki->coursemodule;
             $module['course'] = $wiki->course;
             $module['name'] = external_format_string($wiki->name, $context->id);
             $viewablefields = [];
             if (has_capability('mod/wiki:viewpage', $context)) {
                 list($module['intro'], $module['introformat']) = external_format_text($wiki->intro, $wiki->introformat, $context->id, 'mod_wiki', 'intro', $wiki->id);
                 $viewablefields = array('firstpagetitle', 'wikimode', 'defaultformat', 'forceformat', 'editbegin', 'editend', 'section', 'visible', 'groupmode', 'groupingid');
             }
             // Check additional permissions for returning optional private settings.
             if (has_capability('moodle/course:manageactivities', $context)) {
                 $additionalfields = array('timecreated', 'timemodified');
                 $viewablefields = array_merge($viewablefields, $additionalfields);
             }
             foreach ($viewablefields as $field) {
                 $module[$field] = $wiki->{$field};
             }
             // Check if user can add new pages.
             $module['cancreatepages'] = wiki_can_create_pages($context);
             $returnedwikis[] = $module;
         }
     }
     $result = array();
     $result['wikis'] = $returnedwikis;
     $result['warnings'] = $warnings;
     return $result;
 }
Exemplo n.º 7
0
 /**
  * Export this data so it can be used as the context for a mustache template.
  *
  * @param renderer_base $output
  * @return stdClass
  */
 public function export_for_template(renderer_base $output)
 {
     global $CFG;
     require_once $CFG->libdir . '/externallib.php';
     $r = new stdClass();
     $r->id = (int) $this->record->id;
     $r->tagcollid = clean_param($this->record->tagcollid, PARAM_INT);
     $r->rawname = clean_param($this->record->rawname, PARAM_TAG);
     $r->name = clean_param($this->record->name, PARAM_TAG);
     $format = clean_param($this->record->descriptionformat, PARAM_INT);
     list($r->description, $r->descriptionformat) = external_format_text($this->record->description, $format, \context_system::instance()->id, 'core', 'tag', $r->id);
     $r->flag = clean_param($this->record->flag, PARAM_INT);
     if (isset($this->record->isstandard)) {
         $r->isstandard = clean_param($this->record->isstandard, PARAM_INT) ? 1 : 0;
     }
     $r->official = $r->isstandard;
     // For backwards compatibility.
     $url = core_tag_tag::make_url($r->tagcollid, $r->rawname);
     $r->viewurl = $url->out(false);
     return $r;
 }
Exemplo n.º 8
0
    public function test_external_format_text()
    {
        $settings = external_settings::get_instance();
        $currentraw = $settings->get_raw();
        $currentfilter = $settings->get_filter();
        $settings->set_raw(true);
        $settings->set_filter(false);
        $context = context_system::instance();
        $test = '$$ \\pi $$';
        $testformat = FORMAT_MARKDOWN;
        $correct = array($test, $testformat);
        $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0), $correct);
        $settings->set_raw(false);
        $settings->set_filter(true);
        $test = '$$ \\pi $$';
        $testformat = FORMAT_MARKDOWN;
        $correct = array('<span class="nolink"><span class="filter_mathjaxloader_equation"><p>$$ \\pi $$</p>
</span></span>', FORMAT_HTML);
        $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0), $correct);
        $settings->set_raw($currentraw);
        $settings->set_filter($currentfilter);
    }
Exemplo n.º 9
0
 /**
  * Get the feedback text that should be show to a student who got the given grade in the given quiz.
  *
  * @param int $quizid quiz instance id
  * @param float $grade the grade to check
  * @return array of warnings and status result
  * @since Moodle 3.1
  * @throws moodle_exception
  */
 public static function get_quiz_feedback_for_grade($quizid, $grade)
 {
     global $DB;
     $params = array('quizid' => $quizid, 'grade' => $grade);
     $params = self::validate_parameters(self::get_quiz_feedback_for_grade_parameters(), $params);
     $warnings = array();
     list($quiz, $course, $cm, $context) = self::validate_quiz($params['quizid']);
     $result = array();
     $result['feedbacktext'] = '';
     $result['feedbacktextformat'] = FORMAT_MOODLE;
     $feedback = quiz_feedback_record_for_grade($params['grade'], $quiz);
     if (!empty($feedback->feedbacktext)) {
         list($text, $format) = external_format_text($feedback->feedbacktext, $feedback->feedbacktextformat, $context->id, 'mod_quiz', 'feedback', $feedback->id);
         $result['feedbacktext'] = $text;
         $result['feedbacktextformat'] = $format;
     }
     $result['warnings'] = $warnings;
     return $result;
 }
Exemplo n.º 10
0
 /**
  * Get all groupings in the specified course
  *
  * @param int $courseid id of course
  * @return array of grouping objects (id, courseid, name, enrolmentkey)
  * @since Moodle 2.3
  */
 public static function get_course_groupings($courseid)
 {
     global $CFG;
     require_once "{$CFG->dirroot}/group/lib.php";
     require_once "{$CFG->libdir}/filelib.php";
     $params = self::validate_parameters(self::get_course_groupings_parameters(), array('courseid' => $courseid));
     // Now security checks.
     $context = context_course::instance($params['courseid']);
     try {
         self::validate_context($context);
     } catch (Exception $e) {
         $exceptionparam = new stdClass();
         $exceptionparam->message = $e->getMessage();
         $exceptionparam->courseid = $params['courseid'];
         throw new moodle_exception('errorcoursecontextnotvalid', 'webservice', '', $exceptionparam);
     }
     require_capability('moodle/course:managegroups', $context);
     $gs = groups_get_all_groupings($params['courseid']);
     $groupings = array();
     foreach ($gs as $grouping) {
         list($grouping->description, $grouping->descriptionformat) = external_format_text($grouping->description, $grouping->descriptionformat, $context->id, 'grouping', 'description', $grouping->id);
         $groupings[] = (array) $grouping;
     }
     return $groupings;
 }
Exemplo n.º 11
0
 /**
  * Returns a list of databases in a provided list of courses,
  * if no list is provided all databases that the user can view will be returned.
  *
  * @param array $courseids the course ids
  * @return array the database details
  * @since Moodle 2.9
  */
 public static function get_databases_by_courses($courseids = array())
 {
     global $CFG;
     $params = self::validate_parameters(self::get_databases_by_courses_parameters(), array('courseids' => $courseids));
     $warnings = array();
     if (!empty($params['courseids'])) {
         $courses = array();
         $courseids = $params['courseids'];
     } else {
         $courses = enrol_get_my_courses();
         $courseids = array_keys($courses);
     }
     // Array to store the databases to return.
     $arrdatabases = array();
     // Ensure there are courseids to loop through.
     if (!empty($courseids)) {
         // Array of the courses we are going to retrieve the databases from.
         $dbcourses = array();
         // Go through the courseids.
         foreach ($courseids as $cid) {
             // Check the user can function in this context.
             try {
                 $context = context_course::instance($cid);
                 self::validate_context($context);
                 // Check if this course was already loaded (by enrol_get_my_courses).
                 if (!isset($courses[$cid])) {
                     $courses[$cid] = get_course($cid);
                 }
                 $dbcourses[$cid] = $courses[$cid];
             } catch (Exception $e) {
                 $warnings[] = array('item' => 'course', 'itemid' => $cid, 'warningcode' => '1', 'message' => 'No access rights in course context ' . $e->getMessage());
             }
         }
         // Get the databases in this course, this function checks users visibility permissions.
         // We can avoid then additional validate_context calls.
         $databases = get_all_instances_in_courses("data", $dbcourses);
         foreach ($databases as $database) {
             $datacontext = context_module::instance($database->coursemodule);
             // Entry to return.
             $newdb = array();
             // First, we return information that any user can see in the web interface.
             $newdb['id'] = $database->id;
             $newdb['coursemodule'] = $database->coursemodule;
             $newdb['course'] = $database->course;
             $newdb['name'] = $database->name;
             // Format intro.
             list($newdb['intro'], $newdb['introformat']) = external_format_text($database->intro, $database->introformat, $datacontext->id, 'mod_data', 'intro', null);
             // This information should be only available if the user can see the database entries.
             if (has_capability('mod/data:viewentry', $datacontext)) {
                 $viewablefields = array('comments', 'timeavailablefrom', 'timeavailableto', 'timeviewfrom', 'timeviewto', 'requiredentries', 'requiredentriestoview');
                 // This is for avoid a long repetitive list and for
                 // checking that we are retrieving all the required fields.
                 foreach ($viewablefields as $field) {
                     // We do not use isset because it won't work for existing null values.
                     if (!property_exists($database, $field)) {
                         throw new invalid_response_exception('Missing database module required field: ' . $field);
                     }
                     $newdb[$field] = $database->{$field};
                 }
             }
             // Check additional permissions for returning optional private settings.
             // I avoid intentionally to use can_[add|update]_moduleinfo.
             if (has_capability('moodle/course:manageactivities', $context)) {
                 $additionalfields = array('maxentries', 'rssarticles', 'singletemplate', 'listtemplate', 'listtemplateheader', 'listtemplatefooter', 'addtemplate', 'rsstemplate', 'rsstitletemplate', 'csstemplate', 'jstemplate', 'asearchtemplate', 'approval', 'scale', 'assessed', 'assesstimestart', 'assesstimefinish', 'defaultsort', 'defaultsortdir', 'editany', 'notification');
                 // This is for avoid a long repetitive list.
                 foreach ($additionalfields as $field) {
                     if (property_exists($database, $field)) {
                         $newdb[$field] = $database->{$field};
                     }
                 }
             }
             $arrdatabases[] = $newdb;
         }
     }
     $result = array();
     $result['databases'] = $arrdatabases;
     $result['warnings'] = $warnings;
     return $result;
 }
Exemplo n.º 12
0
 /**
  * Returns a list of chats in a provided list of courses,
  * if no list is provided all chats that the user can view will be returned.
  *
  * @param array $courseids the course ids
  * @return array of chats details
  * @since Moodle 3.0
  */
 public static function get_chats_by_courses($courseids = array())
 {
     global $CFG;
     $returnedchats = array();
     $warnings = array();
     $params = self::validate_parameters(self::get_chats_by_courses_parameters(), array('courseids' => $courseids));
     $courses = array();
     if (empty($params['courseids'])) {
         $courses = enrol_get_my_courses();
         $params['courseids'] = array_keys($courses);
     }
     // Ensure there are courseids to loop through.
     if (!empty($params['courseids'])) {
         list($courses, $warnings) = external_util::validate_courses($params['courseids'], $courses);
         // Get the chats in this course, this function checks users visibility permissions.
         // We can avoid then additional validate_context calls.
         $chats = get_all_instances_in_courses("chat", $courses);
         foreach ($chats as $chat) {
             $chatcontext = context_module::instance($chat->coursemodule);
             // Entry to return.
             $chatdetails = array();
             // First, we return information that any user can see in the web interface.
             $chatdetails['id'] = $chat->id;
             $chatdetails['coursemodule'] = $chat->coursemodule;
             $chatdetails['course'] = $chat->course;
             $chatdetails['name'] = external_format_string($chat->name, $chatcontext->id);
             // Format intro.
             list($chatdetails['intro'], $chatdetails['introformat']) = external_format_text($chat->intro, $chat->introformat, $chatcontext->id, 'mod_chat', 'intro', null);
             if (has_capability('mod/chat:chat', $chatcontext)) {
                 $chatdetails['chatmethod'] = $CFG->chat_method;
                 $chatdetails['keepdays'] = $chat->keepdays;
                 $chatdetails['studentlogs'] = $chat->studentlogs;
                 $chatdetails['chattime'] = $chat->chattime;
                 $chatdetails['schedule'] = $chat->schedule;
             }
             if (has_capability('moodle/course:manageactivities', $chatcontext)) {
                 $chatdetails['timemodified'] = $chat->timemodified;
                 $chatdetails['section'] = $chat->section;
                 $chatdetails['visible'] = $chat->visible;
                 $chatdetails['groupmode'] = $chat->groupmode;
                 $chatdetails['groupingid'] = $chat->groupingid;
             }
             $returnedchats[] = $chatdetails;
         }
     }
     $result = array();
     $result['chats'] = $returnedchats;
     $result['warnings'] = $warnings;
     return $result;
 }
Exemplo n.º 13
0
 /**
  * Return information (files and text fields) for the given plugins in the assignment.
  *
  * @param  assign $assign the assignment object
  * @param  array $assignplugins array of assignment plugins (submission or feedback)
  * @param  stdClass $item the item object (submission or grade)
  * @return array an array containing the plugins returned information
  */
 private static function get_plugins_data($assign, $assignplugins, $item)
 {
     global $CFG;
     $plugins = array();
     $fs = get_file_storage();
     foreach ($assignplugins as $assignplugin) {
         if (!$assignplugin->is_enabled() or !$assignplugin->is_visible()) {
             continue;
         }
         $plugin = array('name' => $assignplugin->get_name(), 'type' => $assignplugin->get_type());
         // Subtype is 'assignsubmission', type is currently 'file' or 'onlinetext'.
         $component = $assignplugin->get_subtype() . '_' . $assignplugin->get_type();
         $fileareas = $assignplugin->get_file_areas();
         foreach ($fileareas as $filearea => $name) {
             $fileareainfo = array('area' => $filearea);
             $fileareainfo['files'] = external_util::get_area_files($assign->get_context()->id, $component, $filearea, $item->id);
             $plugin['fileareas'][] = $fileareainfo;
         }
         $editorfields = $assignplugin->get_editor_fields();
         foreach ($editorfields as $name => $description) {
             $editorfieldinfo = array('name' => $name, 'description' => $description, 'text' => $assignplugin->get_editor_text($name, $item->id), 'format' => $assignplugin->get_editor_format($name, $item->id));
             // Now format the text.
             foreach ($fileareas as $filearea => $name) {
                 list($editorfieldinfo['text'], $editorfieldinfo['format']) = external_format_text($editorfieldinfo['text'], $editorfieldinfo['format'], $assign->get_context()->id, $component, $filearea, $item->id);
             }
             $plugin['editorfields'][] = $editorfieldinfo;
         }
         $plugins[] = $plugin;
     }
     return $plugins;
 }
Exemplo n.º 14
0
 /**
  * Format the text using the external API.
  * This function should we used when text formatting is required in external functions.
  *
  * @return array an array containing the text formatted and the text format
  */
 public function format_external_text()
 {
     if ($this->editorcontext === null) {
         // Switch on the event type to decide upon the appropriate context to use for this event.
         $this->editorcontext = $this->properties->context;
         if ($this->properties->eventtype != 'user' && $this->properties->eventtype != 'course' && $this->properties->eventtype != 'site' && $this->properties->eventtype != 'group') {
             // We don't have a context here, do a normal format_text.
             return external_format_text($this->properties->description, $this->properties->format, $this->editorcontext->id);
         }
     }
     // Work out the item id for the editor, if this is a repeated event then the files will be associated with the original.
     if (!empty($this->properties->repeatid) && $this->properties->repeatid > 0) {
         $itemid = $this->properties->repeatid;
     } else {
         $itemid = $this->properties->id;
     }
     return external_format_text($this->properties->description, $this->properties->format, $this->editorcontext->id, 'calendar', 'event_description', $itemid);
 }
Exemplo n.º 15
0
 /**
  * Search courses following the specified criteria.
  *
  * @param string $criterianame  Criteria name (search, modulelist (only admins), blocklist (only admins), tagid)
  * @param string $criteriavalue Criteria value
  * @param int $page             Page number (for pagination)
  * @param int $perpage          Items per page
  * @param array $requiredcapabilities Optional list of required capabilities (used to filter the list).
  * @param int $limittoenrolled  Limit to only enrolled courses
  * @return array of course objects and warnings
  * @since Moodle 3.0
  * @throws moodle_exception
  */
 public static function search_courses($criterianame, $criteriavalue, $page = 0, $perpage = 0, $requiredcapabilities = array(), $limittoenrolled = 0)
 {
     global $CFG;
     require_once $CFG->libdir . '/coursecatlib.php';
     $warnings = array();
     $parameters = array('criterianame' => $criterianame, 'criteriavalue' => $criteriavalue, 'page' => $page, 'perpage' => $perpage, 'requiredcapabilities' => $requiredcapabilities);
     $params = self::validate_parameters(self::search_courses_parameters(), $parameters);
     self::validate_context(context_system::instance());
     $allowedcriterianames = array('search', 'modulelist', 'blocklist', 'tagid');
     if (!in_array($params['criterianame'], $allowedcriterianames)) {
         throw new invalid_parameter_exception('Invalid value for criterianame parameter (value: ' . $params['criterianame'] . '),' . 'allowed values are: ' . implode(',', $allowedcriterianames));
     }
     if ($params['criterianame'] == 'modulelist' or $params['criterianame'] == 'blocklist') {
         require_capability('moodle/site:config', context_system::instance());
     }
     $paramtype = array('search' => PARAM_RAW, 'modulelist' => PARAM_PLUGIN, 'blocklist' => PARAM_INT, 'tagid' => PARAM_INT);
     $params['criteriavalue'] = clean_param($params['criteriavalue'], $paramtype[$params['criterianame']]);
     // Prepare the search API options.
     $searchcriteria = array();
     $searchcriteria[$params['criterianame']] = $params['criteriavalue'];
     $options = array();
     if ($params['perpage'] != 0) {
         $offset = $params['page'] * $params['perpage'];
         $options = array('offset' => $offset, 'limit' => $params['perpage']);
     }
     // Search the courses.
     $courses = coursecat::search_courses($searchcriteria, $options, $params['requiredcapabilities']);
     $totalcount = coursecat::search_courses_count($searchcriteria, $options, $params['requiredcapabilities']);
     if (!empty($limittoenrolled)) {
         // Get the courses where the current user has access.
         $enrolled = enrol_get_my_courses(array('id', 'cacherev'));
     }
     $finalcourses = array();
     $categoriescache = array();
     foreach ($courses as $course) {
         if (!empty($limittoenrolled)) {
             // Filter out not enrolled courses.
             if (!isset($enrolled[$course->id])) {
                 $totalcount--;
                 continue;
             }
         }
         $coursecontext = context_course::instance($course->id);
         // Category information.
         if (!isset($categoriescache[$course->category])) {
             $categoriescache[$course->category] = coursecat::get($course->category);
         }
         $category = $categoriescache[$course->category];
         // Retrieve course overfiew used files.
         $files = array();
         foreach ($course->get_course_overviewfiles() as $file) {
             $fileurl = moodle_url::make_webservice_pluginfile_url($file->get_contextid(), $file->get_component(), $file->get_filearea(), null, $file->get_filepath(), $file->get_filename())->out(false);
             $files[] = array('filename' => $file->get_filename(), 'fileurl' => $fileurl, 'filesize' => $file->get_filesize());
         }
         // Retrieve the course contacts,
         // we need here the users fullname since if we are not enrolled can be difficult to obtain them via other Web Services.
         $coursecontacts = array();
         foreach ($course->get_course_contacts() as $contact) {
             $coursecontacts[] = array('id' => $contact['user']->id, 'fullname' => $contact['username']);
         }
         // Allowed enrolment methods (maybe we can self-enrol).
         $enroltypes = array();
         $instances = enrol_get_instances($course->id, true);
         foreach ($instances as $instance) {
             $enroltypes[] = $instance->enrol;
         }
         // Format summary.
         list($summary, $summaryformat) = external_format_text($course->summary, $course->summaryformat, $coursecontext->id, 'course', 'summary', null);
         $displayname = get_course_display_name_for_list($course);
         $coursereturns = array();
         $coursereturns['id'] = $course->id;
         $coursereturns['fullname'] = external_format_string($course->fullname, $coursecontext->id);
         $coursereturns['displayname'] = external_format_string($displayname, $coursecontext->id);
         $coursereturns['shortname'] = external_format_string($course->shortname, $coursecontext->id);
         $coursereturns['categoryid'] = $course->category;
         $coursereturns['categoryname'] = $category->name;
         $coursereturns['summary'] = $summary;
         $coursereturns['summaryformat'] = $summaryformat;
         $coursereturns['overviewfiles'] = $files;
         $coursereturns['contacts'] = $coursecontacts;
         $coursereturns['enrollmentmethods'] = $enroltypes;
         $finalcourses[] = $coursereturns;
     }
     return array('total' => $totalcount, 'courses' => $finalcourses, 'warnings' => $warnings);
 }
Exemplo n.º 16
0
 /**
  * Returns a list of books in a provided list of courses,
  * if no list is provided all books that the user can view will be returned.
  *
  * @param array $courseids the course ids
  * @return array of books details
  * @since Moodle 3.0
  */
 public static function get_books_by_courses($courseids = array())
 {
     global $CFG;
     $returnedbooks = array();
     $warnings = array();
     $params = self::validate_parameters(self::get_books_by_courses_parameters(), array('courseids' => $courseids));
     $courses = array();
     if (empty($params['courseids'])) {
         $courses = enrol_get_my_courses();
         $params['courseids'] = array_keys($courses);
     }
     // Ensure there are courseids to loop through.
     if (!empty($params['courseids'])) {
         list($courses, $warnings) = external_util::validate_courses($params['courseids'], $courses);
         // Get the books in this course, this function checks users visibility permissions.
         // We can avoid then additional validate_context calls.
         $books = get_all_instances_in_courses("book", $courses);
         foreach ($books as $book) {
             $context = context_module::instance($book->coursemodule);
             // Entry to return.
             $bookdetails = array();
             // First, we return information that any user can see in the web interface.
             $bookdetails['id'] = $book->id;
             $bookdetails['coursemodule'] = $book->coursemodule;
             $bookdetails['course'] = $book->course;
             $bookdetails['name'] = external_format_string($book->name, $context->id);
             // Format intro.
             list($bookdetails['intro'], $bookdetails['introformat']) = external_format_text($book->intro, $book->introformat, $context->id, 'mod_book', 'intro', null);
             $bookdetails['numbering'] = $book->numbering;
             $bookdetails['navstyle'] = $book->navstyle;
             $bookdetails['customtitles'] = $book->customtitles;
             if (has_capability('moodle/course:manageactivities', $context)) {
                 $bookdetails['revision'] = $book->revision;
                 $bookdetails['timecreated'] = $book->timecreated;
                 $bookdetails['timemodified'] = $book->timemodified;
                 $bookdetails['section'] = $book->section;
                 $bookdetails['visible'] = $book->visible;
                 $bookdetails['groupmode'] = $book->groupmode;
                 $bookdetails['groupingid'] = $book->groupingid;
             }
             $returnedbooks[] = $bookdetails;
         }
     }
     $result = array();
     $result['books'] = $returnedbooks;
     $result['warnings'] = $warnings;
     return $result;
 }
Exemplo n.º 17
0
    public function test_external_format_text()
    {
        $settings = external_settings::get_instance();
        $currentraw = $settings->get_raw();
        $currentfilter = $settings->get_filter();
        $settings->set_raw(true);
        $settings->set_filter(false);
        $context = context_system::instance();
        $test = '$$ \\pi $$';
        $testformat = FORMAT_MARKDOWN;
        $correct = array($test, $testformat);
        $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0), $correct);
        $settings->set_raw(false);
        $settings->set_filter(true);
        $test = '$$ \\pi $$';
        $testformat = FORMAT_MARKDOWN;
        $correct = array('<span class="nolink"><span class="filter_mathjaxloader_equation"><p>$$ \\pi $$</p>
</span></span>', FORMAT_HTML);
        $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0), $correct);
        // Filters can be opted out from by the developer.
        $test = '$$ \\pi $$';
        $testformat = FORMAT_MARKDOWN;
        $correct = array('<p>$$ \\pi $$</p>
', FORMAT_HTML);
        $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0, ['filter' => false]), $correct);
        $test = '<p><a id="test"></a><a href="#test">Text</a></p>';
        $testformat = FORMAT_HTML;
        $correct = array($test, FORMAT_HTML);
        $options = array('allowid' => true);
        $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0, $options), $correct);
        $test = '<p><a id="test"></a><a href="#test">Text</a></p>';
        $testformat = FORMAT_HTML;
        $correct = array('<p><a></a><a href="#test">Text</a></p>', FORMAT_HTML);
        $options = new StdClass();
        $options->allowid = false;
        $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0, $options), $correct);
        $test = '<p><a id="test"></a><a href="#test">Text</a></p>' . "\n" . 'Newline';
        $testformat = FORMAT_MOODLE;
        $correct = array('<p><a id="test"></a><a href="#test">Text</a></p> Newline', FORMAT_HTML);
        $options = new StdClass();
        $options->newlines = false;
        $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0, $options), $correct);
        $test = '<p><a id="test"></a><a href="#test">Text</a></p>';
        $testformat = FORMAT_MOODLE;
        $correct = array('<div class="text_to_html">' . $test . '</div>', FORMAT_HTML);
        $options = new StdClass();
        $options->para = true;
        $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0, $options), $correct);
        $test = '<p><a id="test"></a><a href="#test">Text</a></p>';
        $testformat = FORMAT_MOODLE;
        $correct = array($test, FORMAT_HTML);
        $options = new StdClass();
        $options->context = $context;
        $this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0, $options), $correct);
        $settings->set_raw($currentraw);
        $settings->set_filter($currentfilter);
    }
Exemplo n.º 18
0
 /**
  * Function to export the renderer data in a format that is suitable for a
  * mustache template. This means raw records are generated as in to_record,
  * but all strings are correctly passed through external_format_text (or external_format_string).
  *
  * @param renderer_base $output Used to do a final render of any components that need to be rendered for export.
  * @return stdClass
  */
 public final function export(renderer_base $output)
 {
     $data = new stdClass();
     $properties = self::read_properties_definition();
     $context = $this->get_context();
     $values = (array) $this->data;
     $othervalues = $this->get_other_values($output);
     if (array_intersect_key($values, $othervalues)) {
         // Attempt to replace a standard property.
         throw new coding_exception('Cannot override a standard property value.');
     }
     $values += $othervalues;
     $record = (object) $values;
     foreach ($properties as $property => $definition) {
         if (isset($data->{$property})) {
             // This happens when we have already defined the format properties.
             continue;
         } else {
             if (!property_exists($record, $property) && array_key_exists('default', $definition)) {
                 // We have a default value for this property.
                 $record->{$property} = $definition['default'];
             } else {
                 if (!property_exists($record, $property) && !empty($definition['optional'])) {
                     // Fine, this property can be omitted.
                     continue;
                 } else {
                     if (!property_exists($record, $property)) {
                         // Whoops, we got something that wasn't defined.
                         throw new coding_exception('Unexpected property ' . $property);
                     }
                 }
             }
         }
         $data->{$property} = $record->{$property};
         // If the field is PARAM_RAW and has a format field.
         if ($propertyformat = self::get_format_field($properties, $property)) {
             if (!property_exists($record, $propertyformat)) {
                 // Whoops, we got something that wasn't defined.
                 throw new coding_exception('Unexpected property ' . $propertyformat);
             }
             $format = $record->{$propertyformat};
             list($text, $format) = external_format_text($data->{$property}, $format, $context->id, 'core_competency', '', 0);
             $data->{$property} = $text;
             $data->{$propertyformat} = $format;
         } else {
             if ($definition['type'] === PARAM_TEXT) {
                 if (!empty($definition['multiple'])) {
                     foreach ($data->{$property} as $key => $value) {
                         $data->{$property}[$key] = external_format_string($value, $context->id);
                     }
                 } else {
                     $data->{$property} = external_format_string($data->{$property}, $context->id);
                 }
             }
         }
     }
     return $data;
 }
Exemplo n.º 19
0
 /**
  * Get list of courses user is enrolled in (only active enrolments are returned).
  * Please note the current user must be able to access the course, otherwise the course is not included.
  *
  * @param int $userid
  * @return array of courses
  */
 public static function get_users_courses($userid)
 {
     global $USER, $DB;
     // Do basic automatic PARAM checks on incoming data, using params description
     // If any problems are found then exceptions are thrown with helpful error messages
     $params = self::validate_parameters(self::get_users_courses_parameters(), array('userid' => $userid));
     $courses = enrol_get_users_courses($params['userid'], true, 'id, shortname, fullname, idnumber, visible,
                summary, summaryformat, format, showgrades, lang, enablecompletion, category');
     $result = array();
     foreach ($courses as $course) {
         $context = context_course::instance($course->id, IGNORE_MISSING);
         try {
             self::validate_context($context);
         } catch (Exception $e) {
             // current user can not access this course, sorry we can not disclose who is enrolled in this course!
             continue;
         }
         if ($userid != $USER->id and !has_capability('moodle/course:viewparticipants', $context)) {
             // we need capability to view participants
             continue;
         }
         list($enrolledsqlselect, $enrolledparams) = get_enrolled_sql($context);
         $enrolledsql = "SELECT COUNT('x') FROM ({$enrolledsqlselect}) enrolleduserids";
         $enrolledusercount = $DB->count_records_sql($enrolledsql, $enrolledparams);
         list($course->summary, $course->summaryformat) = external_format_text($course->summary, $course->summaryformat, $context->id, 'course', 'summary', null);
         $course->fullname = external_format_string($course->fullname, $context->id);
         $course->shortname = external_format_string($course->shortname, $context->id);
         $result[] = array('id' => $course->id, 'shortname' => $course->shortname, 'fullname' => $course->fullname, 'idnumber' => $course->idnumber, 'visible' => $course->visible, 'enrolledusercount' => $enrolledusercount, 'summary' => $course->summary, 'summaryformat' => $course->summaryformat, 'format' => $course->format, 'showgrades' => $course->showgrades, 'lang' => $course->lang, 'enablecompletion' => $course->enablecompletion, 'category' => $course->category);
     }
     return $result;
 }
Exemplo n.º 20
0
 /**
  * Returns a list of glossaries in a provided list of courses.
  *
  * If no list is provided all glossaries that the user can view will be returned.
  *
  * @param array $courseids the course IDs.
  * @return array of glossaries
  * @since Moodle 3.1
  */
 public static function get_glossaries_by_courses($courseids = array())
 {
     $params = self::validate_parameters(self::get_glossaries_by_courses_parameters(), array('courseids' => $courseids));
     $warnings = array();
     $courses = array();
     $courseids = $params['courseids'];
     if (empty($courseids)) {
         $courses = enrol_get_my_courses();
         $courseids = array_keys($courses);
     }
     // Array to store the glossaries to return.
     $glossaries = array();
     $modes = array();
     // Ensure there are courseids to loop through.
     if (!empty($courseids)) {
         list($courses, $warnings) = external_util::validate_courses($courseids, $courses);
         // Get the glossaries in these courses, this function checks users visibility permissions.
         $glossaries = get_all_instances_in_courses('glossary', $courses);
         foreach ($glossaries as $glossary) {
             $context = context_module::instance($glossary->coursemodule);
             $glossary->name = external_format_string($glossary->name, $context->id);
             list($glossary->intro, $glossary->introformat) = external_format_text($glossary->intro, $glossary->introformat, $context->id, 'mod_glossary', 'intro', null);
             $glossary->introfiles = external_util::get_area_files($context->id, 'mod_glossary', 'intro', false, false);
             // Make sure we have a number of entries per page.
             if (!$glossary->entbypage) {
                 $glossary->entbypage = $CFG->glossary_entbypage;
             }
             // Add the list of browsing modes.
             if (!isset($modes[$glossary->displayformat])) {
                 $modes[$glossary->displayformat] = self::get_browse_modes_from_display_format($glossary->displayformat);
             }
             $glossary->browsemodes = $modes[$glossary->displayformat];
             $glossary->canaddentry = has_capability('mod/glossary:write', $context) ? 1 : 0;
         }
     }
     $result = array();
     $result['glossaries'] = $glossaries;
     $result['warnings'] = $warnings;
     return $result;
 }
Exemplo n.º 21
0
 /**
  * Returns a list of forums in a provided list of courses,
  * if no list is provided all forums that the user can view
  * will be returned.
  *
  * @param array $courseids the course ids
  * @return array the forum details
  * @since Moodle 2.5
  */
 public static function get_forums_by_courses($courseids = array())
 {
     global $CFG;
     require_once $CFG->dirroot . "/mod/forum/lib.php";
     $params = self::validate_parameters(self::get_forums_by_courses_parameters(), array('courseids' => $courseids));
     if (empty($params['courseids'])) {
         // Get all the courses the user can view.
         $courseids = array_keys(enrol_get_my_courses());
     } else {
         $courseids = $params['courseids'];
     }
     // Array to store the forums to return.
     $arrforums = array();
     // Ensure there are courseids to loop through.
     if (!empty($courseids)) {
         // Array of the courses we are going to retrieve the forums from.
         $dbcourses = array();
         // Mod info for courses.
         $modinfocourses = array();
         // Go through the courseids and return the forums.
         foreach ($courseids as $courseid) {
             // Check the user can function in this context.
             try {
                 $context = context_course::instance($courseid);
                 self::validate_context($context);
                 // Get the modinfo for the course.
                 $modinfocourses[$courseid] = get_fast_modinfo($courseid);
                 $dbcourses[$courseid] = $modinfocourses[$courseid]->get_course();
             } catch (Exception $e) {
                 continue;
             }
         }
         // Get the forums in this course. This function checks users visibility permissions.
         if ($forums = get_all_instances_in_courses("forum", $dbcourses)) {
             foreach ($forums as $forum) {
                 $course = $dbcourses[$forum->course];
                 $cm = $modinfocourses[$course->id]->get_cm($forum->coursemodule);
                 $context = context_module::instance($cm->id);
                 // Skip forums we are not allowed to see discussions.
                 if (!has_capability('mod/forum:viewdiscussion', $context)) {
                     continue;
                 }
                 // Format the intro before being returning using the format setting.
                 list($forum->intro, $forum->introformat) = external_format_text($forum->intro, $forum->introformat, $context->id, 'mod_forum', 'intro', 0);
                 $forum->cmid = $forum->coursemodule;
                 // Add the forum to the array to return.
                 $arrforums[$forum->id] = $forum;
             }
         }
     }
     return $arrforums;
 }
Exemplo n.º 22
0
 /**
  * Returns a list of scorms in a provided list of courses,
  * if no list is provided all scorms that the user can view will be returned.
  *
  * @param array $courseids the course ids
  * @return array the scorm details
  * @since Moodle 3.0
  */
 public static function get_scorms_by_courses($courseids = array())
 {
     global $CFG;
     $returnedscorms = array();
     $warnings = array();
     $params = self::validate_parameters(self::get_scorms_by_courses_parameters(), array('courseids' => $courseids));
     $courses = array();
     if (empty($params['courseids'])) {
         $courses = enrol_get_my_courses();
         $params['courseids'] = array_keys($courses);
     }
     // Ensure there are courseids to loop through.
     if (!empty($params['courseids'])) {
         list($courses, $warnings) = external_util::validate_courses($params['courseids'], $courses);
         // Get the scorms in this course, this function checks users visibility permissions.
         // We can avoid then additional validate_context calls.
         $scorms = get_all_instances_in_courses("scorm", $courses);
         $fs = get_file_storage();
         foreach ($scorms as $scorm) {
             $context = context_module::instance($scorm->coursemodule);
             // Entry to return.
             $module = array();
             // First, we return information that any user can see in (or can deduce from) the web interface.
             $module['id'] = $scorm->id;
             $module['coursemodule'] = $scorm->coursemodule;
             $module['course'] = $scorm->course;
             $module['name'] = external_format_string($scorm->name, $context->id);
             list($module['intro'], $module['introformat']) = external_format_text($scorm->intro, $scorm->introformat, $context->id, 'mod_scorm', 'intro', $scorm->id);
             // Check if the SCORM open and return warnings if so.
             list($open, $openwarnings) = scorm_get_availability_status($scorm, true, $context);
             if (!$open) {
                 foreach ($openwarnings as $warningkey => $warningdata) {
                     $warnings[] = array('item' => 'scorm', 'itemid' => $scorm->id, 'warningcode' => $warningkey, 'message' => get_string($warningkey, 'scorm', $warningdata));
                 }
             } else {
                 $module['packagesize'] = 0;
                 // SCORM size.
                 if ($scorm->scormtype === SCORM_TYPE_LOCAL or $scorm->scormtype === SCORM_TYPE_LOCALSYNC) {
                     if ($packagefile = $fs->get_file($context->id, 'mod_scorm', 'package', 0, '/', $scorm->reference)) {
                         $module['packagesize'] = $packagefile->get_filesize();
                         // Download URL.
                         $module['packageurl'] = moodle_url::make_webservice_pluginfile_url($context->id, 'mod_scorm', 'package', 0, '/', $scorm->reference)->out(false);
                     }
                 }
                 $module['protectpackagedownloads'] = get_config('scorm', 'protectpackagedownloads');
                 $viewablefields = array('version', 'maxgrade', 'grademethod', 'whatgrade', 'maxattempt', 'forcecompleted', 'forcenewattempt', 'lastattemptlock', 'displayattemptstatus', 'displaycoursestructure', 'sha1hash', 'md5hash', 'revision', 'launch', 'skipview', 'hidebrowse', 'hidetoc', 'nav', 'navpositionleft', 'navpositiontop', 'auto', 'popup', 'width', 'height', 'timeopen', 'timeclose', 'displayactivityname', 'scormtype', 'reference');
                 // Check additional permissions for returning optional private settings.
                 if (has_capability('moodle/course:manageactivities', $context)) {
                     $additionalfields = array('updatefreq', 'options', 'completionstatusrequired', 'completionscorerequired', 'autocommit', 'timemodified', 'section', 'visible', 'groupmode', 'groupingid');
                     $viewablefields = array_merge($viewablefields, $additionalfields);
                 }
                 foreach ($viewablefields as $field) {
                     $module[$field] = $scorm->{$field};
                 }
             }
             $returnedscorms[] = $module;
         }
     }
     $result = array();
     $result['scorms'] = $returnedscorms;
     $result['warnings'] = $warnings;
     return $result;
 }
Exemplo n.º 23
0
 /**
  * Returns an array of courses the user is enrolled in, and for each course all of the seplments that the user can
  * view within that course.
  *
  * @param array $courseids An optional array of course ids. If provided only seplments within the given course
  * will be returned. If the user is not enrolled in a given course a warning will be generated and returned.
  * @param array $capabilities An array of additional capability checks you wish to be made on the course context.
  * @return An array of courses and warnings.
  * @since  Moodle 2.4
  */
 public static function get_seplments($courseids = array(), $capabilities = array())
 {
     global $USER, $DB, $CFG;
     require_once "{$CFG->dirroot}/mod/sepl/locallib.php";
     $params = self::validate_parameters(self::get_seplments_parameters(), array('courseids' => $courseids, 'capabilities' => $capabilities));
     $warnings = array();
     $fields = 'sortorder,shortname,fullname,timemodified';
     $courses = enrol_get_users_courses($USER->id, true, $fields);
     // Used to test for ids that have been requested but can't be returned.
     if (count($params['courseids']) > 0) {
         foreach ($params['courseids'] as $courseid) {
             if (!in_array($courseid, array_keys($courses))) {
                 unset($courses[$courseid]);
                 $warnings[] = array('item' => 'course', 'itemid' => $courseid, 'warningcode' => '2', 'message' => 'User is not enrolled or does not have requested capability');
             }
         }
     }
     foreach ($courses as $id => $course) {
         if (count($params['courseids']) > 0 && !in_array($id, $params['courseids'])) {
             unset($courses[$id]);
         }
         $context = context_course::instance($id);
         try {
             self::validate_context($context);
         } catch (Exception $e) {
             unset($courses[$id]);
             $warnings[] = array('item' => 'course', 'itemid' => $id, 'warningcode' => '1', 'message' => 'No access rights in course context ' . $e->getMessage() . $e->getTraceAsString());
             continue;
         }
         if (count($params['capabilities']) > 0 && !has_all_capabilities($params['capabilities'], $context)) {
             unset($courses[$id]);
         }
     }
     $extrafields = 'm.id as seplmentid, ' . 'm.course, ' . 'm.nosubmissions, ' . 'm.submissiondrafts, ' . 'm.sendnotifications, ' . 'm.sendlatenotifications, ' . 'm.sendstudentnotifications, ' . 'm.duedate, ' . 'm.allowsubmissionsfromdate, ' . 'm.grade, ' . 'm.timemodified, ' . 'm.completionsubmit, ' . 'm.cutoffdate, ' . 'm.teamsubmission, ' . 'm.requireallteammemberssubmit, ' . 'm.teamsubmissiongroupingid, ' . 'm.blindmarking, ' . 'm.revealidentities, ' . 'm.attemptreopenmethod, ' . 'm.maxattempts, ' . 'm.markingworkflow, ' . 'm.markingallocation, ' . 'm.requiresubmissionstatement, ' . 'm.intro, ' . 'm.introformat';
     $coursearray = array();
     foreach ($courses as $id => $course) {
         $seplmentarray = array();
         // Get a list of seplments for the course.
         if ($modules = get_coursemodules_in_course('sepl', $courses[$id]->id, $extrafields)) {
             foreach ($modules as $module) {
                 $context = context_module::instance($module->id);
                 try {
                     self::validate_context($context);
                     require_capability('mod/sepl:view', $context);
                 } catch (Exception $e) {
                     $warnings[] = array('item' => 'module', 'itemid' => $module->id, 'warningcode' => '1', 'message' => 'No access rights in module context');
                     continue;
                 }
                 $configrecords = $DB->get_recordset('sepl_plugin_config', array('seplment' => $module->seplmentid));
                 $configarray = array();
                 foreach ($configrecords as $configrecord) {
                     $configarray[] = array('id' => $configrecord->id, 'seplment' => $configrecord->seplment, 'plugin' => $configrecord->plugin, 'subtype' => $configrecord->subtype, 'name' => $configrecord->name, 'value' => $configrecord->value);
                 }
                 $configrecords->close();
                 $seplment = array('id' => $module->seplmentid, 'cmid' => $module->id, 'course' => $module->course, 'name' => $module->name, 'nosubmissions' => $module->nosubmissions, 'submissiondrafts' => $module->submissiondrafts, 'sendnotifications' => $module->sendnotifications, 'sendlatenotifications' => $module->sendlatenotifications, 'sendstudentnotifications' => $module->sendstudentnotifications, 'duedate' => $module->duedate, 'allowsubmissionsfromdate' => $module->allowsubmissionsfromdate, 'grade' => $module->grade, 'timemodified' => $module->timemodified, 'completionsubmit' => $module->completionsubmit, 'cutoffdate' => $module->cutoffdate, 'teamsubmission' => $module->teamsubmission, 'requireallteammemberssubmit' => $module->requireallteammemberssubmit, 'teamsubmissiongroupingid' => $module->teamsubmissiongroupingid, 'blindmarking' => $module->blindmarking, 'revealidentities' => $module->revealidentities, 'attemptreopenmethod' => $module->attemptreopenmethod, 'maxattempts' => $module->maxattempts, 'markingworkflow' => $module->markingworkflow, 'markingallocation' => $module->markingallocation, 'requiresubmissionstatement' => $module->requiresubmissionstatement, 'configs' => $configarray);
                 // Return or not intro and file attachments depending on the plugin settings.
                 $sepl = new sepl($context, null, null);
                 if ($sepl->show_intro()) {
                     list($seplment['intro'], $seplment['introformat']) = external_format_text($module->intro, $module->introformat, $context->id, 'mod_sepl', ASSIGN_INTROATTACHMENT_FILEAREA, 0);
                     $fs = get_file_storage();
                     if ($files = $fs->get_area_files($context->id, 'mod_sepl', ASSIGN_INTROATTACHMENT_FILEAREA, 0, 'timemodified', false)) {
                         $seplment['introattachments'] = array();
                         foreach ($files as $file) {
                             $filename = $file->get_filename();
                             $seplment['introattachments'][] = array('filename' => $filename, 'mimetype' => $file->get_mimetype(), 'fileurl' => moodle_url::make_webservice_pluginfile_url($context->id, 'mod_sepl', ASSIGN_INTROATTACHMENT_FILEAREA, 0, '/', $filename)->out(false));
                         }
                     }
                 }
                 $seplmentarray[] = $seplment;
             }
         }
         $coursearray[] = array('id' => $courses[$id]->id, 'fullname' => $courses[$id]->fullname, 'shortname' => $courses[$id]->shortname, 'timemodified' => $courses[$id]->timemodified, 'seplments' => $seplmentarray);
     }
     $result = array('courses' => $coursearray, 'warnings' => $warnings);
     return $result;
 }
Exemplo n.º 24
0
 /**
  * Test get_users_courses
  */
 public function test_get_users_courses()
 {
     global $USER;
     $this->resetAfterTest(true);
     $coursedata1 = array('summary' => 'Lightwork Course 1 description', 'summaryformat' => FORMAT_MOODLE, 'lang' => 'en', 'enablecompletion' => true, 'showgrades' => true);
     $course1 = self::getDataGenerator()->create_course($coursedata1);
     $course2 = self::getDataGenerator()->create_course();
     $courses = array($course1, $course2);
     // Enrol $USER in the courses.
     // We use the manual plugin.
     $roleid = null;
     $contexts = array();
     foreach ($courses as $course) {
         $contexts[$course->id] = context_course::instance($course->id);
         $roleid = $this->assignUserCapability('moodle/course:viewparticipants', $contexts[$course->id]->id, $roleid);
         $this->getDataGenerator()->enrol_user($USER->id, $course->id, $roleid, 'manual');
     }
     // Call the external function.
     $enrolledincourses = core_enrol_external::get_users_courses($USER->id);
     // We need to execute the return values cleaning process to simulate the web service server.
     $enrolledincourses = external_api::clean_returnvalue(core_enrol_external::get_users_courses_returns(), $enrolledincourses);
     // Check we retrieve the good total number of enrolled users.
     $this->assertEquals(2, count($enrolledincourses));
     // We need to format summary and summaryformat before to compare them with those values returned by the webservice.
     list($course1->summary, $course1->summaryformat) = external_format_text($course1->summary, $course1->summaryformat, $contexts[$course1->id]->id, 'course', 'summary', 0);
     // Check there are no differences between $course1 properties and course values returned by the webservice
     // only for those fields listed in the $coursedata1 array.
     foreach ($enrolledincourses as $courseenrol) {
         if ($courseenrol['id'] == $course1->id) {
             foreach ($coursedata1 as $fieldname => $value) {
                 $this->assertEquals($courseenrol[$fieldname], $course1->{$fieldname});
             }
         }
     }
 }
Exemplo n.º 25
0
 /**
  * Return the course information that is public (visible by every one)
  *
  * @param  course_in_list $course        course in list object
  * @param  stdClass       $coursecontext course context object
  * @return array the course information
  * @since  Moodle 3.2
  */
 protected static function get_course_public_information(course_in_list $course, $coursecontext)
 {
     static $categoriescache = array();
     // Category information.
     if (!array_key_exists($course->category, $categoriescache)) {
         $categoriescache[$course->category] = coursecat::get($course->category, IGNORE_MISSING);
     }
     $category = $categoriescache[$course->category];
     // Retrieve course overview used files.
     $files = array();
     foreach ($course->get_course_overviewfiles() as $file) {
         $fileurl = moodle_url::make_webservice_pluginfile_url($file->get_contextid(), $file->get_component(), $file->get_filearea(), null, $file->get_filepath(), $file->get_filename())->out(false);
         $files[] = array('filename' => $file->get_filename(), 'fileurl' => $fileurl, 'filesize' => $file->get_filesize(), 'filepath' => $file->get_filepath(), 'mimetype' => $file->get_mimetype(), 'timemodified' => $file->get_timemodified());
     }
     // Retrieve the course contacts,
     // we need here the users fullname since if we are not enrolled can be difficult to obtain them via other Web Services.
     $coursecontacts = array();
     foreach ($course->get_course_contacts() as $contact) {
         $coursecontacts[] = array('id' => $contact['user']->id, 'fullname' => $contact['username']);
     }
     // Allowed enrolment methods (maybe we can self-enrol).
     $enroltypes = array();
     $instances = enrol_get_instances($course->id, true);
     foreach ($instances as $instance) {
         $enroltypes[] = $instance->enrol;
     }
     // Format summary.
     list($summary, $summaryformat) = external_format_text($course->summary, $course->summaryformat, $coursecontext->id, 'course', 'summary', null);
     $displayname = get_course_display_name_for_list($course);
     $coursereturns = array();
     $coursereturns['id'] = $course->id;
     $coursereturns['fullname'] = external_format_string($course->fullname, $coursecontext->id);
     $coursereturns['displayname'] = external_format_string($displayname, $coursecontext->id);
     $coursereturns['shortname'] = external_format_string($course->shortname, $coursecontext->id);
     $coursereturns['categoryid'] = $course->category;
     $coursereturns['categoryname'] = $category == null ? '' : $category->name;
     $coursereturns['summary'] = $summary;
     $coursereturns['summaryformat'] = $summaryformat;
     $coursereturns['summaryfiles'] = external_util::get_area_files($coursecontext->id, 'course', 'summary', false, false);
     $coursereturns['overviewfiles'] = $files;
     $coursereturns['contacts'] = $coursecontacts;
     $coursereturns['enrollmentmethods'] = $enroltypes;
     return $coursereturns;
 }
Exemplo n.º 26
0
 /**
  * Returns a list of forum discussions optionally sorted and paginated.
  *
  * @param int $forumid the forum instance id
  * @param string $sortby sort by this element (id, timemodified, timestart or timeend)
  * @param string $sortdirection sort direction: ASC or DESC
  * @param int $page page number
  * @param int $perpage items per page
  *
  * @return array the forum discussion details including warnings
  * @since Moodle 2.8
  */
 public static function get_forum_discussions_paginated($forumid, $sortby = 'timemodified', $sortdirection = 'DESC', $page = -1, $perpage = 0)
 {
     global $CFG, $DB, $USER;
     require_once $CFG->dirroot . "/mod/forum/lib.php";
     $warnings = array();
     $discussions = array();
     $params = self::validate_parameters(self::get_forum_discussions_paginated_parameters(), array('forumid' => $forumid, 'sortby' => $sortby, 'sortdirection' => $sortdirection, 'page' => $page, 'perpage' => $perpage));
     // Compact/extract functions are not recommended.
     $forumid = $params['forumid'];
     $sortby = $params['sortby'];
     $sortdirection = $params['sortdirection'];
     $page = $params['page'];
     $perpage = $params['perpage'];
     $sortallowedvalues = array('id', 'timemodified', 'timestart', 'timeend');
     if (!in_array($sortby, $sortallowedvalues)) {
         throw new invalid_parameter_exception('Invalid value for sortby parameter (value: ' . $sortby . '),' . 'allowed values are: ' . implode(',', $sortallowedvalues));
     }
     $sortdirection = strtoupper($sortdirection);
     $directionallowedvalues = array('ASC', 'DESC');
     if (!in_array($sortdirection, $directionallowedvalues)) {
         throw new invalid_parameter_exception('Invalid value for sortdirection parameter (value: ' . $sortdirection . '),' . 'allowed values are: ' . implode(',', $directionallowedvalues));
     }
     $forum = $DB->get_record('forum', array('id' => $forumid), '*', MUST_EXIST);
     $course = $DB->get_record('course', array('id' => $forum->course), '*', MUST_EXIST);
     $cm = get_coursemodule_from_instance('forum', $forum->id, $course->id, false, MUST_EXIST);
     // Validate the module context. It checks everything that affects the module visibility (including groupings, etc..).
     $modcontext = context_module::instance($cm->id);
     self::validate_context($modcontext);
     // Check they have the view forum capability.
     require_capability('mod/forum:viewdiscussion', $modcontext, null, true, 'noviewdiscussionspermission', 'forum');
     $sort = 'd.' . $sortby . ' ' . $sortdirection;
     $alldiscussions = forum_get_discussions($cm, $sort, true, -1, -1, true, $page, $perpage);
     if ($alldiscussions) {
         $canviewfullname = has_capability('moodle/site:viewfullnames', $modcontext);
         // Get the unreads array, this takes a forum id and returns data for all discussions.
         $unreads = array();
         if ($cantrack = forum_tp_can_track_forums($forum)) {
             if ($forumtracked = forum_tp_is_tracked($forum)) {
                 $unreads = forum_get_discussions_unread($cm);
             }
         }
         // The forum function returns the replies for all the discussions in a given forum.
         $replies = forum_count_discussion_replies($forumid, $sort, -1, $page, $perpage);
         foreach ($alldiscussions as $discussion) {
             // This function checks for qanda forums.
             // Note that the forum_get_discussions returns as id the post id, not the discussion id so we need to do this.
             $discussionrec = clone $discussion;
             $discussionrec->id = $discussion->discussion;
             if (!forum_user_can_see_discussion($forum, $discussionrec, $modcontext)) {
                 $warning = array();
                 // Function forum_get_discussions returns forum_posts ids not forum_discussions ones.
                 $warning['item'] = 'post';
                 $warning['itemid'] = $discussion->id;
                 $warning['warningcode'] = '1';
                 $warning['message'] = 'You can\'t see this discussion';
                 $warnings[] = $warning;
                 continue;
             }
             $discussion->numunread = 0;
             if ($cantrack && $forumtracked) {
                 if (isset($unreads[$discussion->discussion])) {
                     $discussion->numunread = (int) $unreads[$discussion->discussion];
                 }
             }
             $discussion->numreplies = 0;
             if (!empty($replies[$discussion->discussion])) {
                 $discussion->numreplies = (int) $replies[$discussion->discussion]->replies;
             }
             // Load user objects from the results of the query.
             $user = new stdclass();
             $user->id = $discussion->userid;
             $user = username_load_fields_from_object($user, $discussion);
             $discussion->userfullname = fullname($user, $canviewfullname);
             // We can have post written by users that are deleted. In this case, those users don't have a valid context.
             $usercontext = context_user::instance($user->id, IGNORE_MISSING);
             if ($usercontext) {
                 $discussion->userpictureurl = moodle_url::make_webservice_pluginfile_url($usercontext->id, 'user', 'icon', null, '/', 'f1')->out(false);
             } else {
                 $discussion->userpictureurl = '';
             }
             $usermodified = new stdclass();
             $usermodified->id = $discussion->usermodified;
             $usermodified = username_load_fields_from_object($usermodified, $discussion, 'um');
             $discussion->usermodifiedfullname = fullname($usermodified, $canviewfullname);
             // We can have post written by users that are deleted. In this case, those users don't have a valid context.
             $usercontext = context_user::instance($usermodified->id, IGNORE_MISSING);
             if ($usercontext) {
                 $discussion->usermodifiedpictureurl = moodle_url::make_webservice_pluginfile_url($usercontext->id, 'user', 'icon', null, '/', 'f1')->out(false);
             } else {
                 $discussion->usermodifiedpictureurl = '';
             }
             // Rewrite embedded images URLs.
             list($discussion->message, $discussion->messageformat) = external_format_text($discussion->message, $discussion->messageformat, $modcontext->id, 'mod_forum', 'post', $discussion->id);
             // List attachments.
             if (!empty($discussion->attachment)) {
                 $discussion->attachments = array();
                 $fs = get_file_storage();
                 if ($files = $fs->get_area_files($modcontext->id, 'mod_forum', 'attachment', $discussion->id, "filename", false)) {
                     foreach ($files as $file) {
                         $filename = $file->get_filename();
                         $discussion->attachments[] = array('filename' => $filename, 'mimetype' => $file->get_mimetype(), 'fileurl' => file_encode_url($CFG->wwwroot . '/webservice/pluginfile.php', '/' . $modcontext->id . '/mod_forum/attachment/' . $discussion->id . '/' . $filename));
                     }
                 }
             }
             $discussions[] = $discussion;
         }
     }
     $result = array();
     $result['discussions'] = $discussions;
     $result['warnings'] = $warnings;
     return $result;
 }
Exemplo n.º 27
0
/**
 *
 * Give user record from mdl_user, build an array conntains
 * all user details
 *
 * Warning: description file urls are 'webservice/pluginfile.php' is use.
 *          it can be changed with $CFG->moodlewstextformatlinkstoimagesfile
 *
 * @param stdClass $user user record from mdl_user
 * @param stdClass $context context object
 * @param stdClass $course moodle course
 * @param array $userfields required fields
 * @return array|null
 */
function user_get_user_details($user, $course = null, array $userfields = array())
{
    global $USER, $DB, $CFG;
    require_once $CFG->dirroot . "/user/profile/lib.php";
    //custom field library
    require_once $CFG->dirroot . "/lib/filelib.php";
    // file handling on description and friends
    $defaultfields = user_get_default_fields();
    if (empty($userfields)) {
        $userfields = $defaultfields;
    }
    foreach ($userfields as $thefield) {
        if (!in_array($thefield, $defaultfields)) {
            throw new moodle_exception('invaliduserfield', 'error', '', $thefield);
        }
    }
    // Make sure id and fullname are included
    if (!in_array('id', $userfields)) {
        $userfields[] = 'id';
    }
    if (!in_array('fullname', $userfields)) {
        $userfields[] = 'fullname';
    }
    if (!empty($course)) {
        $context = context_course::instance($course->id);
        $usercontext = context_user::instance($user->id);
        $canviewdetailscap = has_capability('moodle/user:viewdetails', $context) || has_capability('moodle/user:viewdetails', $usercontext);
    } else {
        $context = context_user::instance($user->id);
        $usercontext = $context;
        $canviewdetailscap = has_capability('moodle/user:viewdetails', $usercontext);
    }
    $currentuser = $user->id == $USER->id;
    $isadmin = is_siteadmin($USER);
    $showuseridentityfields = get_extra_user_fields($context);
    if (!empty($course)) {
        $canviewhiddenuserfields = has_capability('moodle/course:viewhiddenuserfields', $context);
    } else {
        $canviewhiddenuserfields = has_capability('moodle/user:viewhiddendetails', $context);
    }
    $canviewfullnames = has_capability('moodle/site:viewfullnames', $context);
    if (!empty($course)) {
        $canviewuseremail = has_capability('moodle/course:useremail', $context);
    } else {
        $canviewuseremail = false;
    }
    $cannotviewdescription = !empty($CFG->profilesforenrolledusersonly) && !$currentuser && !$DB->record_exists('role_assignments', array('userid' => $user->id));
    if (!empty($course)) {
        $canaccessallgroups = has_capability('moodle/site:accessallgroups', $context);
    } else {
        $canaccessallgroups = false;
    }
    if (!$currentuser && !$canviewdetailscap && !has_coursecontact_role($user->id)) {
        // skip this user details
        return null;
    }
    $userdetails = array();
    $userdetails['id'] = $user->id;
    if (($isadmin or $currentuser) and in_array('username', $userfields)) {
        $userdetails['username'] = $user->username;
    }
    if ($isadmin or $canviewfullnames) {
        if (in_array('firstname', $userfields)) {
            $userdetails['firstname'] = $user->firstname;
        }
        if (in_array('lastname', $userfields)) {
            $userdetails['lastname'] = $user->lastname;
        }
    }
    $userdetails['fullname'] = fullname($user);
    if (in_array('customfields', $userfields)) {
        $fields = $DB->get_recordset_sql("SELECT f.*\n                                            FROM {user_info_field} f\n                                            JOIN {user_info_category} c\n                                                 ON f.categoryid=c.id\n                                        ORDER BY c.sortorder ASC, f.sortorder ASC");
        $userdetails['customfields'] = array();
        foreach ($fields as $field) {
            require_once $CFG->dirroot . '/user/profile/field/' . $field->datatype . '/field.class.php';
            $newfield = 'profile_field_' . $field->datatype;
            $formfield = new $newfield($field->id, $user->id);
            if ($formfield->is_visible() and !$formfield->is_empty()) {
                $userdetails['customfields'][] = array('name' => $formfield->field->name, 'value' => $formfield->data, 'type' => $field->datatype, 'shortname' => $formfield->field->shortname);
            }
        }
        $fields->close();
        // unset customfields if it's empty
        if (empty($userdetails['customfields'])) {
            unset($userdetails['customfields']);
        }
    }
    // profile image
    if (in_array('profileimageurl', $userfields)) {
        $profileimageurl = moodle_url::make_pluginfile_url($usercontext->id, 'user', 'icon', NULL, '/', 'f1');
        $userdetails['profileimageurl'] = $profileimageurl->out(false);
    }
    if (in_array('profileimageurlsmall', $userfields)) {
        $profileimageurlsmall = moodle_url::make_pluginfile_url($usercontext->id, 'user', 'icon', NULL, '/', 'f2');
        $userdetails['profileimageurlsmall'] = $profileimageurlsmall->out(false);
    }
    //hidden user field
    if ($canviewhiddenuserfields) {
        $hiddenfields = array();
        // address, phone1 and phone2 not appears in hidden fields list
        // but require viewhiddenfields capability
        // according to user/profile.php
        if ($user->address && in_array('address', $userfields)) {
            $userdetails['address'] = $user->address;
        }
    } else {
        $hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
    }
    if ($user->phone1 && in_array('phone1', $userfields) && (in_array('phone1', $showuseridentityfields) or $canviewhiddenuserfields)) {
        $userdetails['phone1'] = $user->phone1;
    }
    if ($user->phone2 && in_array('phone2', $userfields) && (in_array('phone2', $showuseridentityfields) or $canviewhiddenuserfields)) {
        $userdetails['phone2'] = $user->phone2;
    }
    if (isset($user->description) && (!isset($hiddenfields['description']) && !$cannotviewdescription or $isadmin)) {
        if (in_array('description', $userfields)) {
            // Always return the descriptionformat if description is requested.
            list($userdetails['description'], $userdetails['descriptionformat']) = external_format_text($user->description, $user->descriptionformat, $usercontext->id, 'user', 'profile', null);
        }
    }
    if (in_array('country', $userfields) && (!isset($hiddenfields['country']) or $isadmin) && $user->country) {
        $userdetails['country'] = $user->country;
    }
    if (in_array('city', $userfields) && (!isset($hiddenfields['city']) or $isadmin) && $user->city) {
        $userdetails['city'] = $user->city;
    }
    if (in_array('url', $userfields) && $user->url && (!isset($hiddenfields['webpage']) or $isadmin)) {
        $url = $user->url;
        if (strpos($user->url, '://') === false) {
            $url = 'http://' . $url;
        }
        $user->url = clean_param($user->url, PARAM_URL);
        $userdetails['url'] = $user->url;
    }
    if (in_array('icq', $userfields) && $user->icq && (!isset($hiddenfields['icqnumber']) or $isadmin)) {
        $userdetails['icq'] = $user->icq;
    }
    if (in_array('skype', $userfields) && $user->skype && (!isset($hiddenfields['skypeid']) or $isadmin)) {
        $userdetails['skype'] = $user->skype;
    }
    if (in_array('yahoo', $userfields) && $user->yahoo && (!isset($hiddenfields['yahooid']) or $isadmin)) {
        $userdetails['yahoo'] = $user->yahoo;
    }
    if (in_array('aim', $userfields) && $user->aim && (!isset($hiddenfields['aimid']) or $isadmin)) {
        $userdetails['aim'] = $user->aim;
    }
    if (in_array('msn', $userfields) && $user->msn && (!isset($hiddenfields['msnid']) or $isadmin)) {
        $userdetails['msn'] = $user->msn;
    }
    if (in_array('firstaccess', $userfields) && (!isset($hiddenfields['firstaccess']) or $isadmin)) {
        if ($user->firstaccess) {
            $userdetails['firstaccess'] = $user->firstaccess;
        } else {
            $userdetails['firstaccess'] = 0;
        }
    }
    if (in_array('lastaccess', $userfields) && (!isset($hiddenfields['lastaccess']) or $isadmin)) {
        if ($user->lastaccess) {
            $userdetails['lastaccess'] = $user->lastaccess;
        } else {
            $userdetails['lastaccess'] = 0;
        }
    }
    if (in_array('email', $userfields) && ($isadmin or $currentuser or $canviewuseremail or in_array('email', $showuseridentityfields) or $user->maildisplay == 1 or $user->maildisplay == 2 and enrol_sharing_course($user, $USER))) {
        $userdetails['email'] = $user->email;
    }
    if (in_array('interests', $userfields) && !empty($CFG->usetags)) {
        require_once $CFG->dirroot . '/tag/lib.php';
        if ($interests = tag_get_tags_csv('user', $user->id, TAG_RETURN_TEXT)) {
            $userdetails['interests'] = $interests;
        }
    }
    //Departement/Institution/Idnumber are not displayed on any profile, however you can get them from editing profile.
    if ($isadmin or $currentuser or in_array('idnumber', $showuseridentityfields)) {
        if (in_array('idnumber', $userfields) && $user->idnumber) {
            $userdetails['idnumber'] = $user->idnumber;
        }
    }
    if ($isadmin or $currentuser or in_array('institution', $showuseridentityfields)) {
        if (in_array('institution', $userfields) && $user->institution) {
            $userdetails['institution'] = $user->institution;
        }
    }
    if ($isadmin or $currentuser or in_array('department', $showuseridentityfields)) {
        if (in_array('department', $userfields) && isset($user->department)) {
            //isset because it's ok to have department 0
            $userdetails['department'] = $user->department;
        }
    }
    if (in_array('roles', $userfields) && !empty($course)) {
        // not a big secret
        $roles = get_user_roles($context, $user->id, false);
        $userdetails['roles'] = array();
        foreach ($roles as $role) {
            $userdetails['roles'][] = array('roleid' => $role->roleid, 'name' => $role->name, 'shortname' => $role->shortname, 'sortorder' => $role->sortorder);
        }
    }
    // If groups are in use and enforced throughout the course, then make sure we can meet in at least one course level group
    if (in_array('groups', $userfields) && !empty($course) && $canaccessallgroups) {
        $usergroups = groups_get_all_groups($course->id, $user->id, $course->defaultgroupingid, 'g.id, g.name,g.description,g.descriptionformat');
        $userdetails['groups'] = array();
        foreach ($usergroups as $group) {
            list($group->description, $group->descriptionformat) = external_format_text($group->description, $group->descriptionformat, $context->id, 'group', 'description', $group->id);
            $userdetails['groups'][] = array('id' => $group->id, 'name' => $group->name, 'description' => $group->description, 'descriptionformat' => $group->descriptionformat);
        }
    }
    //list of courses where the user is enrolled
    if (in_array('enrolledcourses', $userfields) && !isset($hiddenfields['mycourses'])) {
        $enrolledcourses = array();
        if ($mycourses = enrol_get_users_courses($user->id, true)) {
            foreach ($mycourses as $mycourse) {
                if ($mycourse->category) {
                    $coursecontext = context_course::instance($mycourse->id);
                    $enrolledcourse = array();
                    $enrolledcourse['id'] = $mycourse->id;
                    $enrolledcourse['fullname'] = format_string($mycourse->fullname, true, array('context' => $coursecontext));
                    $enrolledcourse['shortname'] = format_string($mycourse->shortname, true, array('context' => $coursecontext));
                    $enrolledcourses[] = $enrolledcourse;
                }
            }
            $userdetails['enrolledcourses'] = $enrolledcourses;
        }
    }
    //user preferences
    if (in_array('preferences', $userfields) && $currentuser) {
        $preferences = array();
        $userpreferences = get_user_preferences();
        foreach ($userpreferences as $prefname => $prefvalue) {
            $preferences[] = array('name' => $prefname, 'value' => $prefvalue);
        }
        $userdetails['preferences'] = $preferences;
    }
    return $userdetails;
}
Exemplo n.º 28
0
    /**
     * Returns a list of forums in a provided list of courses,
     * if no list is provided all forums that the user can view
     * will be returned.
     *
     * @param array $courseids the course ids
     * @return array the forum details
     * @since Moodle 2.5
     */
    public static function get_forums_by_courses($courseids = array()) {
        global $CFG, $DB, $USER;

        require_once($CFG->dirroot . "/mod/forum/lib.php");

        $params = self::validate_parameters(self::get_forums_by_courses_parameters(), array('courseids' => $courseids));

        if (empty($params['courseids'])) {
            // Get all the courses the user can view.
            $courseids = array_keys(enrol_get_my_courses());
        } else {
            $courseids = $params['courseids'];
        }

        // Array to store the forums to return.
        $arrforums = array();

        // Ensure there are courseids to loop through.
        if (!empty($courseids)) {
            // Go through the courseids and return the forums.
            foreach ($courseids as $cid) {
                // Get the course context.
                $context = context_course::instance($cid);
                // Check the user can function in this context.
                self::validate_context($context);
                // Get the forums in this course.
                if ($forums = $DB->get_records('forum', array('course' => $cid))) {
                    // Get the modinfo for the course.
                    $modinfo = get_fast_modinfo($cid);
                    // Get the forum instances.
                    $foruminstances = $modinfo->get_instances_of('forum');
                    // Loop through the forums returned by modinfo.
                    foreach ($foruminstances as $forumid => $cm) {
                        // If it is not visible or present in the forums get_records call, continue.
                        if (!$cm->uservisible || !isset($forums[$forumid])) {
                            continue;
                        }
                        // Set the forum object.
                        $forum = $forums[$forumid];
                        // Get the module context.
                        $context = context_module::instance($cm->id);
                        // Check they have the view forum capability.
                        require_capability('mod/forum:viewdiscussion', $context);
                        // Format the intro before being returning using the format setting.
                        list($forum->intro, $forum->introformat) = external_format_text($forum->intro, $forum->introformat,
                            $context->id, 'mod_forum', 'intro', 0);
                        // Add the course module id to the object, this information is useful.
                        $forum->cmid = $cm->id;
                        // Add the forum to the array to return.
                        $arrforums[$forum->id] = (array) $forum;
                    }
                }
            }
        }

        return $arrforums;
    }
Exemplo n.º 29
0
    /**
     * Get categories
     *
     * @param array $criteria Criteria to match the results
     * @param booln $addsubcategories obtain only the category (false) or its subcategories (true - default)
     * @return array list of categories
     * @since Moodle 2.3
     */
    public static function get_categories($criteria = array(), $addsubcategories = true) {
        global $CFG, $DB;
        require_once($CFG->dirroot . "/course/lib.php");

        // Validate parameters.
        $params = self::validate_parameters(self::get_categories_parameters(),
                array('criteria' => $criteria, 'addsubcategories' => $addsubcategories));

        // Retrieve the categories.
        $categories = array();
        if (!empty($params['criteria'])) {

            $conditions = array();
            $wheres = array();
            foreach ($params['criteria'] as $crit) {
                $key = trim($crit['key']);

                // Trying to avoid duplicate keys.
                if (!isset($conditions[$key])) {

                    $context = context_system::instance();
                    $value = null;
                    switch ($key) {
                        case 'id':
                            $value = clean_param($crit['value'], PARAM_INT);
                            break;

                        case 'idnumber':
                            if (has_capability('moodle/category:manage', $context)) {
                                $value = clean_param($crit['value'], PARAM_RAW);
                            } else {
                                // We must throw an exception.
                                // Otherwise the dev client would think no idnumber exists.
                                throw new moodle_exception('criteriaerror',
                                        'webservice', '', null,
                                        'You don\'t have the permissions to search on the "idnumber" field.');
                            }
                            break;

                        case 'name':
                            $value = clean_param($crit['value'], PARAM_TEXT);
                            break;

                        case 'parent':
                            $value = clean_param($crit['value'], PARAM_INT);
                            break;

                        case 'visible':
                            if (has_capability('moodle/category:manage', $context)
                                or has_capability('moodle/category:viewhiddencategories',
                                        context_system::instance())) {
                                $value = clean_param($crit['value'], PARAM_INT);
                            } else {
                                throw new moodle_exception('criteriaerror',
                                        'webservice', '', null,
                                        'You don\'t have the permissions to search on the "visible" field.');
                            }
                            break;

                        case 'theme':
                            if (has_capability('moodle/category:manage', $context)) {
                                $value = clean_param($crit['value'], PARAM_THEME);
                            } else {
                                throw new moodle_exception('criteriaerror',
                                        'webservice', '', null,
                                        'You don\'t have the permissions to search on the "theme" field.');
                            }
                            break;

                        default:
                            throw new moodle_exception('criteriaerror',
                                    'webservice', '', null,
                                    'You can not search on this criteria: ' . $key);
                    }

                    if (isset($value)) {
                        $conditions[$key] = $crit['value'];
                        $wheres[] = $key . " = :" . $key;
                    }
                }
            }

            if (!empty($wheres)) {
                $wheres = implode(" AND ", $wheres);

                $categories = $DB->get_records_select('course_categories', $wheres, $conditions);

                // Retrieve its sub subcategories (all levels).
                if ($categories and !empty($params['addsubcategories'])) {
                    $newcategories = array();

                    // Check if we required visible/theme checks.
                    $additionalselect = '';
                    $additionalparams = array();
                    if (isset($conditions['visible'])) {
                        $additionalselect .= ' AND visible = :visible';
                        $additionalparams['visible'] = $conditions['visible'];
                    }
                    if (isset($conditions['theme'])) {
                        $additionalselect .= ' AND theme= :theme';
                        $additionalparams['theme'] = $conditions['theme'];
                    }

                    foreach ($categories as $category) {
                        $sqlselect = $DB->sql_like('path', ':path') . $additionalselect;
                        $sqlparams = array('path' => $category->path.'/%') + $additionalparams; // It will NOT include the specified category.
                        $subcategories = $DB->get_records_select('course_categories', $sqlselect, $sqlparams);
                        $newcategories = $newcategories + $subcategories;   // Both arrays have integer as keys.
                    }
                    $categories = $categories + $newcategories;
                }
            }

        } else {
            // Retrieve all categories in the database.
            $categories = $DB->get_records('course_categories');
        }

        // The not returned categories. key => category id, value => reason of exclusion.
        $excludedcats = array();

        // The returned categories.
        $categoriesinfo = array();

        // We need to sort the categories by path.
        // The parent cats need to be checked by the algo first.
        usort($categories, "core_course_external::compare_categories_by_path");

        foreach ($categories as $category) {

            // Check if the category is a child of an excluded category, if yes exclude it too (excluded => do not return).
            $parents = explode('/', $category->path);
            unset($parents[0]); // First key is always empty because path start with / => /1/2/4.
            foreach ($parents as $parentid) {
                // Note: when the parent exclusion was due to the context,
                // the sub category could still be returned.
                if (isset($excludedcats[$parentid]) and $excludedcats[$parentid] != 'context') {
                    $excludedcats[$category->id] = 'parent';
                }
            }

            // Check category depth is <= maxdepth (do not check for user who can manage categories).
            if ((!empty($CFG->maxcategorydepth) && count($parents) > $CFG->maxcategorydepth)
                    and !has_capability('moodle/category:manage', $context)) {
                $excludedcats[$category->id] = 'depth';
            }

            // Check the user can use the category context.
            $context = context_coursecat::instance($category->id);
            try {
                self::validate_context($context);
            } catch (Exception $e) {
                $excludedcats[$category->id] = 'context';

                // If it was the requested category then throw an exception.
                if (isset($params['categoryid']) && $category->id == $params['categoryid']) {
                    $exceptionparam = new stdClass();
                    $exceptionparam->message = $e->getMessage();
                    $exceptionparam->catid = $category->id;
                    throw new moodle_exception('errorcatcontextnotvalid', 'webservice', '', $exceptionparam);
                }
            }

            // Return the category information.
            if (!isset($excludedcats[$category->id])) {

                // Final check to see if the category is visible to the user.
                if ($category->visible
                        or has_capability('moodle/category:viewhiddencategories', context_system::instance())
                        or has_capability('moodle/category:manage', $context)) {

                    $categoryinfo = array();
                    $categoryinfo['id'] = $category->id;
                    $categoryinfo['name'] = $category->name;
                    list($categoryinfo['description'], $categoryinfo['descriptionformat']) =
                        external_format_text($category->description, $category->descriptionformat,
                                $context->id, 'coursecat', 'description', null);
                    $categoryinfo['parent'] = $category->parent;
                    $categoryinfo['sortorder'] = $category->sortorder;
                    $categoryinfo['coursecount'] = $category->coursecount;
                    $categoryinfo['depth'] = $category->depth;
                    $categoryinfo['path'] = $category->path;

                    // Some fields only returned for admin.
                    if (has_capability('moodle/category:manage', $context)) {
                        $categoryinfo['idnumber'] = $category->idnumber;
                        $categoryinfo['visible'] = $category->visible;
                        $categoryinfo['visibleold'] = $category->visibleold;
                        $categoryinfo['timemodified'] = $category->timemodified;
                        $categoryinfo['theme'] = $category->theme;
                    }

                    $categoriesinfo[] = $categoryinfo;
                } else {
                    $excludedcats[$category->id] = 'visibility';
                }
            }
        }

        // Sorting the resulting array so it looks a bit better for the client developer.
        usort($categoriesinfo, "core_course_external::compare_categories_by_sortorder");

        return $categoriesinfo;
    }
Exemplo n.º 30
0
 /**
  * Returns the complete list of grade items for users in a course.
  *
  * @param int $courseid Course Id
  * @param int $userid   Only this user (optional)
  * @param int $groupid  Get users from this group only
  *
  * @return array the grades tables
  * @since Moodle 3.2
  */
 public static function get_grade_items($courseid, $userid = 0, $groupid = 0)
 {
     global $CFG, $USER;
     list($params, $course, $context, $user, $groupid) = self::check_report_access($courseid, $userid, $groupid);
     $userid = $params['userid'];
     // We pass userid because it can be still 0.
     list($gradeitems, $warnings) = self::get_report_data($course, $context, $user, $userid, $groupid, false);
     foreach ($gradeitems as $gradeitem) {
         if (isset($gradeitem['feedback']) and isset($gradeitem['feedbackformat'])) {
             list($gradeitem['feedback'], $gradeitem['feedbackformat']) = external_format_text($gradeitem['feedback'], $gradeitem['feedbackformat'], $context->id);
         }
     }
     $result = array();
     $result['usergrades'] = $gradeitems;
     $result['warnings'] = $warnings;
     return $result;
 }