|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00027 defined('MOODLE_INTERNAL') || die; 00028 00029 require_once("$CFG->libdir/externallib.php"); 00030 00034 class core_course_external extends external_api { 00035 00040 public static function get_course_contents_parameters() { 00041 return new external_function_parameters( 00042 array('courseid' => new external_value(PARAM_INT, 'course id'), 00043 'options' => new external_multiple_structure ( 00044 new external_single_structure( 00045 array('name' => new external_value(PARAM_ALPHANUM, 'option name'), 00046 'value' => new external_value(PARAM_RAW, 'the value of the option, this param is personaly validated in the external function.') 00047 ) 00048 ), 'Options, not used yet, might be used in later version', VALUE_DEFAULT, array()) 00049 ) 00050 ); 00051 } 00052 00059 public static function get_course_contents($courseid, $options) { 00060 global $CFG, $DB; 00061 require_once($CFG->dirroot . "/course/lib.php"); 00062 00063 //validate parameter 00064 $params = self::validate_parameters(self::get_course_contents_parameters(), 00065 array('courseid' => $courseid, 'options' => $options)); 00066 00067 //retrieve the course 00068 $course = $DB->get_record('course', array('id' => $params['courseid']), '*', MUST_EXIST); 00069 00070 //check course format exist 00071 if (!file_exists($CFG->dirroot . '/course/format/' . $course->format . '/lib.php')) { 00072 throw new moodle_exception('cannotgetcoursecontents', 'webservice', '', null, get_string('courseformatnotfound', 'error', '', $course->format)); 00073 } else { 00074 require_once($CFG->dirroot . '/course/format/' . $course->format . '/lib.php'); 00075 } 00076 00077 // now security checks 00078 $context = get_context_instance(CONTEXT_COURSE, $course->id); 00079 try { 00080 self::validate_context($context); 00081 } catch (Exception $e) { 00082 $exceptionparam = new stdClass(); 00083 $exceptionparam->message = $e->getMessage(); 00084 $exceptionparam->courseid = $course->id; 00085 throw new moodle_exception('errorcoursecontextnotvalid', 'webservice', '', $exceptionparam); 00086 } 00087 00088 $canupdatecourse = has_capability('moodle/course:update', $context); 00089 00090 //create return value 00091 $coursecontents = array(); 00092 00093 if ($canupdatecourse or $course->visible 00094 or has_capability('moodle/course:viewhiddencourses', $context)) { 00095 00096 //retrieve sections 00097 $modinfo = get_fast_modinfo($course); 00098 $sections = get_all_sections($course->id); 00099 00100 //for each sections (first displayed to last displayed) 00101 foreach ($sections as $key => $section) { 00102 00103 $showsection = (has_capability('moodle/course:viewhiddensections', $context) or $section->visible or !$course->hiddensections); 00104 if (!$showsection) { 00105 continue; 00106 } 00107 00108 // reset $sectioncontents 00109 $sectionvalues = array(); 00110 $sectionvalues['id'] = $section->id; 00111 $sectionvalues['name'] = get_section_name($course, $section); 00112 $summary = file_rewrite_pluginfile_urls($section->summary, 'webservice/pluginfile.php', $context->id, 'course', 'section', $section->id); 00113 $sectionvalues['visible'] = $section->visible; 00114 $sectionvalues['summary'] = format_text($summary, $section->summaryformat); 00115 $sectioncontents = array(); 00116 00117 //for each module of the section 00118 foreach ($modinfo->sections[$section->section] as $cmid) { //matching /course/lib.php:print_section() logic 00119 $cm = $modinfo->cms[$cmid]; 00120 00121 // stop here if the module is not visible to the user 00122 if (!$cm->uservisible) { 00123 continue; 00124 } 00125 00126 $module = array(); 00127 00128 //common info (for people being able to see the module or availability dates) 00129 $module['id'] = $cm->id; 00130 $module['name'] = format_string($cm->name, true); 00131 $module['modname'] = $cm->modname; 00132 $module['modplural'] = $cm->modplural; 00133 $module['modicon'] = $cm->get_icon_url()->out(false); 00134 $module['indent'] = $cm->indent; 00135 00136 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id); 00137 00138 if (!empty($cm->showdescription)) { 00139 $module['description'] = $cm->get_content(); 00140 } 00141 00142 //url of the module 00143 $url = $cm->get_url(); 00144 if ($url) { //labels don't have url 00145 $module['url'] = $cm->get_url()->out(); 00146 } 00147 00148 $canviewhidden = has_capability('moodle/course:viewhiddenactivities', 00149 get_context_instance(CONTEXT_MODULE, $cm->id)); 00150 //user that can view hidden module should know about the visibility 00151 $module['visible'] = $cm->visible; 00152 00153 //availability date (also send to user who can see hidden module when the showavailabilyt is ON) 00154 if ($canupdatecourse or ($CFG->enableavailability && $canviewhidden && $cm->showavailability)) { 00155 $module['availablefrom'] = $cm->availablefrom; 00156 $module['availableuntil'] = $cm->availableuntil; 00157 } 00158 00159 $baseurl = 'webservice/pluginfile.php'; 00160 00161 //call $modulename_export_contents 00162 //(each module callback take care about checking the capabilities) 00163 require_once($CFG->dirroot . '/mod/' . $cm->modname . '/lib.php'); 00164 $getcontentfunction = $cm->modname.'_export_contents'; 00165 if (function_exists($getcontentfunction)) { 00166 if ($contents = $getcontentfunction($cm, $baseurl)) { 00167 $module['contents'] = $contents; 00168 } 00169 } 00170 00171 //assign result to $sectioncontents 00172 $sectioncontents[] = $module; 00173 00174 } 00175 $sectionvalues['modules'] = $sectioncontents; 00176 00177 // assign result to $coursecontents 00178 $coursecontents[] = $sectionvalues; 00179 } 00180 } 00181 return $coursecontents; 00182 } 00183 00188 public static function get_course_contents_returns() { 00189 return new external_multiple_structure( 00190 new external_single_structure( 00191 array( 00192 'id' => new external_value(PARAM_INT, 'Section ID'), 00193 'name' => new external_value(PARAM_TEXT, 'Section name'), 00194 'visible' => new external_value(PARAM_INT, 'is the section visible', VALUE_OPTIONAL), 00195 'summary' => new external_value(PARAM_RAW, 'Section description'), 00196 'modules' => new external_multiple_structure( 00197 new external_single_structure( 00198 array( 00199 'id' => new external_value(PARAM_INT, 'activity id'), 00200 'url' => new external_value(PARAM_URL, 'activity url', VALUE_OPTIONAL), 00201 'name' => new external_value(PARAM_TEXT, 'activity module name'), 00202 'description' => new external_value(PARAM_RAW, 'activity description', VALUE_OPTIONAL), 00203 'visible' => new external_value(PARAM_INT, 'is the module visible', VALUE_OPTIONAL), 00204 'modicon' => new external_value(PARAM_URL, 'activity icon url'), 00205 'modname' => new external_value(PARAM_PLUGIN, 'activity module type'), 00206 'modplural' => new external_value(PARAM_TEXT, 'activity module plural name'), 00207 'availablefrom' => new external_value(PARAM_INT, 'module availability start date', VALUE_OPTIONAL), 00208 'availableuntil' => new external_value(PARAM_INT, 'module availability en date', VALUE_OPTIONAL), 00209 'indent' => new external_value(PARAM_INT, 'number of identation in the site'), 00210 'contents' => new external_multiple_structure( 00211 new external_single_structure( 00212 array( 00213 // content info 00214 'type'=> new external_value(PARAM_TEXT, 'a file or a folder or external link'), 00215 'filename'=> new external_value(PARAM_FILE, 'filename'), 00216 'filepath'=> new external_value(PARAM_PATH, 'filepath'), 00217 'filesize'=> new external_value(PARAM_INT, 'filesize'), 00218 'fileurl' => new external_value(PARAM_URL, 'downloadable file url', VALUE_OPTIONAL), 00219 'content' => new external_value(PARAM_RAW, 'Raw content, will be used when type is content', VALUE_OPTIONAL), 00220 'timecreated' => new external_value(PARAM_INT, 'Time created'), 00221 'timemodified' => new external_value(PARAM_INT, 'Time modified'), 00222 'sortorder' => new external_value(PARAM_INT, 'Content sort order'), 00223 00224 // copyright related info 00225 'userid' => new external_value(PARAM_INT, 'User who added this content to moodle'), 00226 'author' => new external_value(PARAM_TEXT, 'Content owner'), 00227 'license' => new external_value(PARAM_TEXT, 'Content license'), 00228 ) 00229 ), VALUE_DEFAULT, array() 00230 ) 00231 ) 00232 ), 'list of module' 00233 ) 00234 ) 00235 ) 00236 ); 00237 } 00238 00243 public static function get_courses_parameters() { 00244 return new external_function_parameters( 00245 array('options' => new external_single_structure( 00246 array('ids' => new external_multiple_structure( 00247 new external_value(PARAM_INT, 'Course id') 00248 , 'List of course id. If empty return all courses 00249 except front page course.', 00250 VALUE_OPTIONAL) 00251 ), 'options - operator OR is used', VALUE_DEFAULT, array()) 00252 ) 00253 ); 00254 } 00255 00261 public static function get_courses($options) { 00262 global $CFG, $DB; 00263 require_once($CFG->dirroot . "/course/lib.php"); 00264 00265 //validate parameter 00266 $params = self::validate_parameters(self::get_courses_parameters(), 00267 array('options' => $options)); 00268 00269 //retrieve courses 00270 if (!key_exists('ids', $params['options']) 00271 or empty($params['options']['ids'])) { 00272 $courses = $DB->get_records('course'); 00273 } else { 00274 $courses = $DB->get_records_list('course', 'id', $params['options']['ids']); 00275 } 00276 00277 //create return value 00278 $coursesinfo = array(); 00279 foreach ($courses as $course) { 00280 00281 // now security checks 00282 $context = get_context_instance(CONTEXT_COURSE, $course->id); 00283 try { 00284 self::validate_context($context); 00285 } catch (Exception $e) { 00286 $exceptionparam = new stdClass(); 00287 $exceptionparam->message = $e->getMessage(); 00288 $exceptionparam->courseid = $course->id; 00289 throw new moodle_exception( 00290 get_string('errorcoursecontextnotvalid', 'webservice', $exceptionparam)); 00291 } 00292 require_capability('moodle/course:view', $context); 00293 00294 $courseinfo = array(); 00295 $courseinfo['id'] = $course->id; 00296 $courseinfo['fullname'] = $course->fullname; 00297 $courseinfo['shortname'] = $course->shortname; 00298 $courseinfo['categoryid'] = $course->category; 00299 $courseinfo['summary'] = $course->summary; 00300 $courseinfo['summaryformat'] = $course->summaryformat; 00301 $courseinfo['format'] = $course->format; 00302 $courseinfo['startdate'] = $course->startdate; 00303 $courseinfo['numsections'] = $course->numsections; 00304 00305 //some field should be returned only if the user has update permission 00306 $courseadmin = has_capability('moodle/course:update', $context); 00307 if ($courseadmin) { 00308 $courseinfo['categorysortorder'] = $course->sortorder; 00309 $courseinfo['idnumber'] = $course->idnumber; 00310 $courseinfo['showgrades'] = $course->showgrades; 00311 $courseinfo['showreports'] = $course->showreports; 00312 $courseinfo['newsitems'] = $course->newsitems; 00313 $courseinfo['visible'] = $course->visible; 00314 $courseinfo['maxbytes'] = $course->maxbytes; 00315 $courseinfo['hiddensections'] = $course->hiddensections; 00316 $courseinfo['groupmode'] = $course->groupmode; 00317 $courseinfo['groupmodeforce'] = $course->groupmodeforce; 00318 $courseinfo['defaultgroupingid'] = $course->defaultgroupingid; 00319 $courseinfo['lang'] = $course->lang; 00320 $courseinfo['timecreated'] = $course->timecreated; 00321 $courseinfo['timemodified'] = $course->timemodified; 00322 $courseinfo['forcetheme'] = $course->theme; 00323 $courseinfo['enablecompletion'] = $course->enablecompletion; 00324 $courseinfo['completionstartonenrol'] = $course->completionstartonenrol; 00325 $courseinfo['completionnotify'] = $course->completionnotify; 00326 } 00327 00328 if ($courseadmin or $course->visible 00329 or has_capability('moodle/course:viewhiddencourses', $context)) { 00330 $coursesinfo[] = $courseinfo; 00331 } 00332 } 00333 00334 return $coursesinfo; 00335 } 00336 00341 public static function get_courses_returns() { 00342 return new external_multiple_structure( 00343 new external_single_structure( 00344 array( 00345 'id' => new external_value(PARAM_INT, 'course id'), 00346 'shortname' => new external_value(PARAM_TEXT, 'course short name'), 00347 'categoryid' => new external_value(PARAM_INT, 'category id'), 00348 'categorysortorder' => new external_value(PARAM_INT, 00349 'sort order into the category', VALUE_OPTIONAL), 00350 'fullname' => new external_value(PARAM_TEXT, 'full name'), 00351 'idnumber' => new external_value(PARAM_RAW, 'id number', VALUE_OPTIONAL), 00352 'summary' => new external_value(PARAM_RAW, 'summary'), 00353 'summaryformat' => new external_value(PARAM_INT, 00354 'the summary text Moodle format'), 00355 'format' => new external_value(PARAM_PLUGIN, 00356 'course format: weeks, topics, social, site,..'), 00357 'showgrades' => new external_value(PARAM_INT, 00358 '1 if grades are shown, otherwise 0', VALUE_OPTIONAL), 00359 'newsitems' => new external_value(PARAM_INT, 00360 'number of recent items appearing on the course page', VALUE_OPTIONAL), 00361 'startdate' => new external_value(PARAM_INT, 00362 'timestamp when the course start'), 00363 'numsections' => new external_value(PARAM_INT, 'number of weeks/topics'), 00364 'maxbytes' => new external_value(PARAM_INT, 00365 'largest size of file that can be uploaded into the course', 00366 VALUE_OPTIONAL), 00367 'showreports' => new external_value(PARAM_INT, 00368 'are activity report shown (yes = 1, no =0)', VALUE_OPTIONAL), 00369 'visible' => new external_value(PARAM_INT, 00370 '1: available to student, 0:not available', VALUE_OPTIONAL), 00371 'hiddensections' => new external_value(PARAM_INT, 00372 'How the hidden sections in the course are displayed to students', 00373 VALUE_OPTIONAL), 00374 'groupmode' => new external_value(PARAM_INT, 'no group, separate, visible', 00375 VALUE_OPTIONAL), 00376 'groupmodeforce' => new external_value(PARAM_INT, '1: yes, 0: no', 00377 VALUE_OPTIONAL), 00378 'defaultgroupingid' => new external_value(PARAM_INT, 'default grouping id', 00379 VALUE_OPTIONAL), 00380 'timecreated' => new external_value(PARAM_INT, 00381 'timestamp when the course have been created', VALUE_OPTIONAL), 00382 'timemodified' => new external_value(PARAM_INT, 00383 'timestamp when the course have been modified', VALUE_OPTIONAL), 00384 'enablecompletion' => new external_value(PARAM_INT, 00385 'Enabled, control via completion and activity settings. Disbaled, 00386 not shown in activity settings.', 00387 VALUE_OPTIONAL), 00388 'completionstartonenrol' => new external_value(PARAM_INT, 00389 '1: begin tracking a student\'s progress in course completion 00390 after course enrolment. 0: does not', 00391 VALUE_OPTIONAL), 00392 'completionnotify' => new external_value(PARAM_INT, 00393 '1: yes 0: no', VALUE_OPTIONAL), 00394 'lang' => new external_value(PARAM_SAFEDIR, 00395 'forced course language', VALUE_OPTIONAL), 00396 'forcetheme' => new external_value(PARAM_PLUGIN, 00397 'name of the force theme', VALUE_OPTIONAL), 00398 ), 'course' 00399 ) 00400 ); 00401 } 00402 00407 public static function create_courses_parameters() { 00408 $courseconfig = get_config('moodlecourse'); //needed for many default values 00409 return new external_function_parameters( 00410 array( 00411 'courses' => new external_multiple_structure( 00412 new external_single_structure( 00413 array( 00414 'fullname' => new external_value(PARAM_TEXT, 'full name'), 00415 'shortname' => new external_value(PARAM_TEXT, 'course short name'), 00416 'categoryid' => new external_value(PARAM_INT, 'category id'), 00417 'idnumber' => new external_value(PARAM_RAW, 'id number', VALUE_OPTIONAL), 00418 'summary' => new external_value(PARAM_RAW, 'summary', VALUE_OPTIONAL), 00419 'summaryformat' => new external_value(PARAM_INT, 00420 'the summary text Moodle format', VALUE_DEFAULT, FORMAT_MOODLE), 00421 'format' => new external_value(PARAM_PLUGIN, 00422 'course format: weeks, topics, social, site,..', 00423 VALUE_DEFAULT, $courseconfig->format), 00424 'showgrades' => new external_value(PARAM_INT, 00425 '1 if grades are shown, otherwise 0', VALUE_DEFAULT, 00426 $courseconfig->showgrades), 00427 'newsitems' => new external_value(PARAM_INT, 00428 'number of recent items appearing on the course page', 00429 VALUE_DEFAULT, $courseconfig->newsitems), 00430 'startdate' => new external_value(PARAM_INT, 00431 'timestamp when the course start', VALUE_OPTIONAL), 00432 'numsections' => new external_value(PARAM_INT, 'number of weeks/topics', 00433 VALUE_DEFAULT, $courseconfig->numsections), 00434 'maxbytes' => new external_value(PARAM_INT, 00435 'largest size of file that can be uploaded into the course', 00436 VALUE_DEFAULT, $courseconfig->maxbytes), 00437 'showreports' => new external_value(PARAM_INT, 00438 'are activity report shown (yes = 1, no =0)', VALUE_DEFAULT, 00439 $courseconfig->showreports), 00440 'visible' => new external_value(PARAM_INT, 00441 '1: available to student, 0:not available', VALUE_OPTIONAL), 00442 'hiddensections' => new external_value(PARAM_INT, 00443 'How the hidden sections in the course are displayed to students', 00444 VALUE_DEFAULT, $courseconfig->hiddensections), 00445 'groupmode' => new external_value(PARAM_INT, 'no group, separate, visible', 00446 VALUE_DEFAULT, $courseconfig->groupmode), 00447 'groupmodeforce' => new external_value(PARAM_INT, '1: yes, 0: no', 00448 VALUE_DEFAULT, $courseconfig->groupmodeforce), 00449 'defaultgroupingid' => new external_value(PARAM_INT, 'default grouping id', 00450 VALUE_DEFAULT, 0), 00451 'enablecompletion' => new external_value(PARAM_INT, 00452 'Enabled, control via completion and activity settings. Disabled, 00453 not shown in activity settings.', 00454 VALUE_OPTIONAL), 00455 'completionstartonenrol' => new external_value(PARAM_INT, 00456 '1: begin tracking a student\'s progress in course completion after 00457 course enrolment. 0: does not', 00458 VALUE_OPTIONAL), 00459 'completionnotify' => new external_value(PARAM_INT, 00460 '1: yes 0: no', VALUE_OPTIONAL), 00461 'lang' => new external_value(PARAM_SAFEDIR, 00462 'forced course language', VALUE_OPTIONAL), 00463 'forcetheme' => new external_value(PARAM_PLUGIN, 00464 'name of the force theme', VALUE_OPTIONAL), 00465 ) 00466 ), 'courses to create' 00467 ) 00468 ) 00469 ); 00470 } 00471 00477 public static function create_courses($courses) { 00478 global $CFG, $DB; 00479 require_once($CFG->dirroot . "/course/lib.php"); 00480 require_once($CFG->libdir . '/completionlib.php'); 00481 00482 00483 $params = self::validate_parameters(self::create_courses_parameters(), 00484 array('courses' => $courses)); 00485 00486 $availablethemes = get_plugin_list('theme'); 00487 $availablelangs = get_string_manager()->get_list_of_translations(); 00488 00489 $transaction = $DB->start_delegated_transaction(); 00490 00491 foreach ($params['courses'] as $course) { 00492 00493 // Ensure the current user is allowed to run this function 00494 $context = get_context_instance(CONTEXT_COURSECAT, $course['categoryid']); 00495 try { 00496 self::validate_context($context); 00497 } catch (Exception $e) { 00498 $exceptionparam = new stdClass(); 00499 $exceptionparam->message = $e->getMessage(); 00500 $exceptionparam->catid = $course['categoryid']; 00501 throw new moodle_exception( 00502 get_string('errorcatcontextnotvalid', 'webservice', $exceptionparam)); 00503 } 00504 require_capability('moodle/course:create', $context); 00505 00506 // Make sure lang is valid 00507 if (key_exists('lang', $course) and empty($availablelangs[$course['lang']])) { 00508 throw new moodle_exception( 00509 get_string('errorinvalidparam', 'webservice', 'lang')); 00510 } 00511 00512 // Make sure theme is valid 00513 if (key_exists('forcetheme', $course)) { 00514 if (!empty($CFG->allowcoursethemes)) { 00515 if (empty($availablethemes[$course['forcetheme']])) { 00516 throw new moodle_exception( 00517 get_string('errorinvalidparam', 'webservice', 'forcetheme')); 00518 } else { 00519 $course['theme'] = $course['forcetheme']; 00520 } 00521 } 00522 } 00523 00524 //force visibility if ws user doesn't have the permission to set it 00525 $category = $DB->get_record('course_categories', array('id' => $course['categoryid'])); 00526 if (!has_capability('moodle/course:visibility', $context)) { 00527 $course['visible'] = $category->visible; 00528 } 00529 00530 //set default value for completion 00531 $courseconfig = get_config('moodlecourse'); 00532 if (completion_info::is_enabled_for_site()) { 00533 if (!key_exists('enablecompletion', $course)) { 00534 $course['enablecompletion'] = $courseconfig->enablecompletion; 00535 } 00536 if (!key_exists('completionstartonenrol', $course)) { 00537 $course['completionstartonenrol'] = $courseconfig->completionstartonenrol; 00538 } 00539 } else { 00540 $course['enablecompletion'] = 0; 00541 $course['completionstartonenrol'] = 0; 00542 } 00543 00544 $course['category'] = $course['categoryid']; 00545 00546 //Note: create_course() core function check shortname, idnumber, category 00547 $course['id'] = create_course((object) $course)->id; 00548 00549 $resultcourses[] = array('id' => $course['id'], 'shortname' => $course['shortname']); 00550 } 00551 00552 $transaction->allow_commit(); 00553 00554 return $resultcourses; 00555 } 00556 00561 public static function create_courses_returns() { 00562 return new external_multiple_structure( 00563 new external_single_structure( 00564 array( 00565 'id' => new external_value(PARAM_INT, 'course id'), 00566 'shortname' => new external_value(PARAM_TEXT, 'short name'), 00567 ) 00568 ) 00569 ); 00570 } 00571 00572 } 00573 00578 class moodle_course_external extends external_api { 00579 00585 public static function get_courses_parameters() { 00586 return core_course_external::get_courses_parameters(); 00587 } 00588 00595 public static function get_courses($options) { 00596 return core_course_external::get_courses($options); 00597 } 00598 00604 public static function get_courses_returns() { 00605 return core_course_external::get_courses_returns(); 00606 } 00607 00613 public static function create_courses_parameters() { 00614 return core_course_external::create_courses_parameters(); 00615 } 00616 00623 public static function create_courses($courses) { 00624 return core_course_external::create_courses($courses); 00625 } 00626 00632 public static function create_courses_returns() { 00633 return core_course_external::create_courses_returns(); 00634 } 00635 00636 }