PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/course/publish/forms.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 405 lines | 304 code | 60 blank | 41 comment | 28 complexity | 76588adbd19c99d4f74c78014c921c09 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, Apache-2.0, BSD-3-Clause, AGPL-3.0
  1. <?php
  2. ///////////////////////////////////////////////////////////////////////////
  3. // //
  4. // This file is part of Moodle - http://moodle.org/ //
  5. // Moodle - Modular Object-Oriented Dynamic Learning Environment //
  6. // //
  7. // Moodle is free software: you can redistribute it and/or modify //
  8. // it under the terms of the GNU General Public License as published by //
  9. // the Free Software Foundation, either version 3 of the License, or //
  10. // (at your option) any later version. //
  11. // //
  12. // Moodle is distributed in the hope that it will be useful, //
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of //
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
  15. // GNU General Public License for more details. //
  16. // //
  17. // You should have received a copy of the GNU General Public License //
  18. // along with Moodle. If not, see <http://www.gnu.org/licenses/>. //
  19. // //
  20. ///////////////////////////////////////////////////////////////////////////
  21. /*
  22. * @package course
  23. * @subpackage publish
  24. * @author Jerome Mouneyrac <jerome@mouneyrac.com>
  25. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL
  26. * @copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com
  27. *
  28. * The forms used for course publication
  29. */
  30. require_once($CFG->libdir . '/formslib.php');
  31. require_once($CFG->dirroot . "/" . $CFG->admin . "/registration/lib.php");
  32. require_once($CFG->dirroot . "/course/publish/lib.php");
  33. /*
  34. * Hub selector to choose on which hub we want to publish.
  35. */
  36. class hub_publish_selector_form extends moodleform {
  37. public function definition() {
  38. global $CFG;
  39. $mform = & $this->_form;
  40. $share = $this->_customdata['share'];
  41. $mform->addElement('header', 'site', get_string('selecthub', 'hub'));
  42. $mform->addElement('static', 'info', '', get_string('selecthubinfo', 'hub') . html_writer::empty_tag('br'));
  43. $registrationmanager = new registration_manager();
  44. $registeredhubs = $registrationmanager->get_registered_on_hubs();
  45. //Public hub list
  46. $options = array();
  47. foreach ($registeredhubs as $hub) {
  48. $hubname = $hub->hubname;
  49. $mform->addElement('hidden', clean_param($hub->huburl, PARAM_ALPHANUMEXT), $hubname);
  50. $mform->setType(clean_param($hub->huburl, PARAM_ALPHANUMEXT), PARAM_ALPHANUMEXT);
  51. if (empty($hubname)) {
  52. $hubname = $hub->huburl;
  53. }
  54. $mform->addElement('radio', 'huburl', null, ' ' . $hubname, $hub->huburl);
  55. if ($hub->huburl == HUB_MOODLEORGHUBURL) {
  56. $mform->setDefault('huburl', $hub->huburl);
  57. }
  58. }
  59. $mform->addElement('hidden', 'id', $this->_customdata['id']);
  60. $mform->setType('id', PARAM_INT);
  61. if ($share) {
  62. $buttonlabel = get_string('shareonhub', 'hub');
  63. $mform->addElement('hidden', 'share', true);
  64. $mform->setType('share', PARAM_BOOL);
  65. } else {
  66. $buttonlabel = get_string('advertiseonhub', 'hub');
  67. $mform->addElement('hidden', 'advertise', true);
  68. $mform->setType('advertise', PARAM_BOOL);
  69. }
  70. $this->add_action_buttons(false, $buttonlabel);
  71. }
  72. }
  73. /*
  74. * Course publication form
  75. */
  76. class course_publication_form extends moodleform {
  77. public function definition() {
  78. global $CFG, $DB, $USER, $OUTPUT;
  79. $strrequired = get_string('required');
  80. $mform = & $this->_form;
  81. $huburl = $this->_customdata['huburl'];
  82. $hubname = $this->_customdata['hubname'];
  83. $course = $this->_customdata['course'];
  84. $advertise = $this->_customdata['advertise'];
  85. $share = $this->_customdata['share'];
  86. $page = $this->_customdata['page'];
  87. $site = get_site();
  88. //hidden parameters
  89. $mform->addElement('hidden', 'huburl', $huburl);
  90. $mform->setType('huburl', PARAM_URL);
  91. $mform->addElement('hidden', 'hubname', $hubname);
  92. $mform->setType('hubname', PARAM_TEXT);
  93. //check on the hub if the course has already been published
  94. $registrationmanager = new registration_manager();
  95. $registeredhub = $registrationmanager->get_registeredhub($huburl);
  96. $publicationmanager = new course_publish_manager();
  97. $publications = $publicationmanager->get_publications($registeredhub->huburl, $course->id, $advertise);
  98. if (!empty($publications)) {
  99. //get the last publication of this course
  100. $publication = array_pop($publications);
  101. $function = 'hub_get_courses';
  102. $options = new stdClass();
  103. $options->ids = array($publication->hubcourseid);
  104. $options->allsitecourses = 1;
  105. $params = array('search' => '', 'downloadable' => $share,
  106. 'enrollable' => !$share, 'options' => $options);
  107. $serverurl = $huburl . "/local/hub/webservice/webservices.php";
  108. require_once($CFG->dirroot . "/webservice/xmlrpc/lib.php");
  109. $xmlrpcclient = new webservice_xmlrpc_client($serverurl, $registeredhub->token);
  110. try {
  111. $result = $xmlrpcclient->call($function, $params);
  112. $publishedcourses = $result['courses'];
  113. } catch (Exception $e) {
  114. $error = $OUTPUT->notification(get_string('errorcourseinfo', 'hub', $e->getMessage()));
  115. $mform->addElement('static', 'errorhub', '', $error);
  116. }
  117. }
  118. if (!empty($publishedcourses)) {
  119. $publishedcourse = $publishedcourses[0];
  120. $hubcourseid = $publishedcourse['id'];
  121. $defaultfullname = $publishedcourse['fullname'];
  122. $defaultshortname = $publishedcourse['shortname'];
  123. $defaultsummary = $publishedcourse['description'];
  124. $defaultlanguage = $publishedcourse['language'];
  125. $defaultpublishername = $publishedcourse['publishername'];
  126. $defaultpublisheremail = $publishedcourse['publisheremail'];
  127. $defaultcontributornames = $publishedcourse['contributornames'];
  128. $defaultcoverage = $publishedcourse['coverage'];
  129. $defaultcreatorname = $publishedcourse['creatorname'];
  130. $defaultlicenceshortname = $publishedcourse['licenceshortname'];
  131. $defaultsubject = $publishedcourse['subject'];
  132. $defaultaudience = $publishedcourse['audience'];
  133. $defaulteducationallevel = $publishedcourse['educationallevel'];
  134. $defaultcreatornotes = $publishedcourse['creatornotes'];
  135. $defaultcreatornotesformat = $publishedcourse['creatornotesformat'];
  136. $screenshotsnumber = $publishedcourse['screenshots'];
  137. $privacy = $publishedcourse['privacy'];
  138. if (($screenshotsnumber > 0) and !empty($privacy)) {
  139. $page->requires->yui_module('moodle-block_community-imagegallery',
  140. 'M.blocks_community.init_imagegallery',
  141. array(array('imageids' => array($hubcourseid),
  142. 'imagenumbers' => array($screenshotsnumber),
  143. 'huburl' => $huburl)));
  144. }
  145. } else {
  146. $defaultfullname = $course->fullname;
  147. $defaultshortname = $course->shortname;
  148. $defaultsummary = clean_param($course->summary, PARAM_TEXT);
  149. if (empty($course->lang)) {
  150. $language = get_site()->lang;
  151. if (empty($language)) {
  152. $defaultlanguage = current_language();
  153. } else {
  154. $defaultlanguage = $language;
  155. }
  156. } else {
  157. $defaultlanguage = $course->lang;
  158. }
  159. $defaultpublishername = $USER->firstname . ' ' . $USER->lastname;
  160. $defaultpublisheremail = $USER->email;
  161. $defaultcontributornames = '';
  162. $defaultcoverage = '';
  163. $defaultcreatorname = $USER->firstname . ' ' . $USER->lastname;
  164. $defaultlicenceshortname = 'cc';
  165. $defaultsubject = 'none';
  166. $defaultaudience = HUB_AUDIENCE_STUDENTS;
  167. $defaulteducationallevel = HUB_EDULEVEL_TERTIARY;
  168. $defaultcreatornotes = '';
  169. $defaultcreatornotesformat = FORMAT_HTML;
  170. $screenshotsnumber = 0;
  171. }
  172. //the input parameters
  173. $mform->addElement('header', 'moodle', get_string('publicationinfo', 'hub'));
  174. $mform->addElement('text', 'name', get_string('coursename', 'hub'),
  175. array('class' => 'metadatatext'));
  176. $mform->addRule('name', $strrequired, 'required', null, 'client');
  177. $mform->setType('name', PARAM_TEXT);
  178. $mform->setDefault('name', $defaultfullname);
  179. $mform->addHelpButton('name', 'name', 'hub');
  180. $mform->addElement('hidden', 'id', $this->_customdata['id']);
  181. $mform->setType('id', PARAM_INT);
  182. if ($share) {
  183. $buttonlabel = get_string('shareon', 'hub', !empty($hubname) ? $hubname : $huburl);
  184. $mform->addElement('hidden', 'share', $share);
  185. $mform->setType('share', PARAM_BOOL);
  186. $mform->addElement('text', 'demourl', get_string('demourl', 'hub'),
  187. array('class' => 'metadatatext'));
  188. $mform->setType('demourl', PARAM_URL);
  189. $mform->setDefault('demourl', new moodle_url("/course/view.php?id=" . $course->id));
  190. $mform->addHelpButton('demourl', 'demourl', 'hub');
  191. }
  192. if ($advertise) {
  193. if (empty($publishedcourses)) {
  194. $buttonlabel = get_string('advertiseon', 'hub', !empty($hubname) ? $hubname : $huburl);
  195. } else {
  196. $buttonlabel = get_string('readvertiseon', 'hub', !empty($hubname) ? $hubname : $huburl);
  197. }
  198. $mform->addElement('hidden', 'advertise', $advertise);
  199. $mform->setType('advertise', PARAM_BOOL);
  200. $mform->addElement('hidden', 'courseurl', $CFG->wwwroot . "/course/view.php?id=" . $course->id);
  201. $mform->setType('courseurl', PARAM_URL);
  202. $mform->addElement('static', 'courseurlstring', get_string('courseurl', 'hub'));
  203. $mform->setDefault('courseurlstring', new moodle_url("/course/view.php?id=" . $course->id));
  204. $mform->addHelpButton('courseurlstring', 'courseurl', 'hub');
  205. }
  206. $mform->addElement('text', 'courseshortname', get_string('courseshortname', 'hub'),
  207. array('class' => 'metadatatext'));
  208. $mform->setDefault('courseshortname', $defaultshortname);
  209. $mform->addHelpButton('courseshortname', 'courseshortname', 'hub');
  210. $mform->setType('courseshortname', PARAM_TEXT);
  211. $mform->addElement('textarea', 'description', get_string('description'), array('rows' => 10,
  212. 'cols' => 57));
  213. $mform->addRule('description', $strrequired, 'required', null, 'client');
  214. $mform->setDefault('description', $defaultsummary);
  215. $mform->setType('description', PARAM_TEXT);
  216. $mform->addHelpButton('description', 'description', 'hub');
  217. $languages = get_string_manager()->get_list_of_languages();
  218. core_collator::asort($languages);
  219. $mform->addElement('select', 'language', get_string('language'), $languages);
  220. $mform->setDefault('language', $defaultlanguage);
  221. $mform->addHelpButton('language', 'language', 'hub');
  222. $mform->addElement('text', 'publishername', get_string('publishername', 'hub'),
  223. array('class' => 'metadatatext'));
  224. $mform->setDefault('publishername', $defaultpublishername);
  225. $mform->addRule('publishername', $strrequired, 'required', null, 'client');
  226. $mform->addHelpButton('publishername', 'publishername', 'hub');
  227. $mform->setType('publishername', PARAM_NOTAGS);
  228. $mform->addElement('text', 'publisheremail', get_string('publisheremail', 'hub'),
  229. array('class' => 'metadatatext'));
  230. $mform->setDefault('publisheremail', $defaultpublisheremail);
  231. $mform->addRule('publisheremail', $strrequired, 'required', null, 'client');
  232. $mform->addHelpButton('publisheremail', 'publisheremail', 'hub');
  233. $mform->setType('publisheremail', PARAM_EMAIL);
  234. $mform->addElement('text', 'creatorname', get_string('creatorname', 'hub'),
  235. array('class' => 'metadatatext'));
  236. $mform->addRule('creatorname', $strrequired, 'required', null, 'client');
  237. $mform->setType('creatorname', PARAM_NOTAGS);
  238. $mform->setDefault('creatorname', $defaultcreatorname);
  239. $mform->addHelpButton('creatorname', 'creatorname', 'hub');
  240. $mform->addElement('text', 'contributornames', get_string('contributornames', 'hub'),
  241. array('class' => 'metadatatext'));
  242. $mform->setDefault('contributornames', $defaultcontributornames);
  243. $mform->addHelpButton('contributornames', 'contributornames', 'hub');
  244. $mform->setType('contributornames', PARAM_NOTAGS);
  245. $mform->addElement('text', 'coverage', get_string('tags', 'hub'),
  246. array('class' => 'metadatatext'));
  247. $mform->setType('coverage', PARAM_TEXT);
  248. $mform->setDefault('coverage', $defaultcoverage);
  249. $mform->addHelpButton('coverage', 'tags', 'hub');
  250. require_once($CFG->libdir . "/licenselib.php");
  251. $licensemanager = new license_manager();
  252. $licences = $licensemanager->get_licenses();
  253. $options = array();
  254. foreach ($licences as $license) {
  255. $options[$license->shortname] = get_string($license->shortname, 'license');
  256. }
  257. $mform->addElement('select', 'licence', get_string('license'), $options);
  258. $mform->setDefault('licence', $defaultlicenceshortname);
  259. unset($options);
  260. $mform->addHelpButton('licence', 'licence', 'hub');
  261. $options = $publicationmanager->get_sorted_subjects();
  262. //prepare data for the smartselect
  263. foreach ($options as $key => &$option) {
  264. $keylength = strlen($key);
  265. if ($keylength == 10) {
  266. $option = "&nbsp;&nbsp;" . $option;
  267. } else if ($keylength == 12) {
  268. $option = "&nbsp;&nbsp;&nbsp;&nbsp;" . $option;
  269. }
  270. }
  271. $options = array('none' => get_string('none', 'hub')) + $options;
  272. $mform->addElement('select', 'subject', get_string('subject', 'hub'), $options);
  273. unset($options);
  274. $mform->addHelpButton('subject', 'subject', 'hub');
  275. $mform->setDefault('subject', $defaultsubject);
  276. $mform->addRule('subject', $strrequired, 'required', null, 'client');
  277. $this->init_javascript_enhancement('subject', 'smartselect', array('selectablecategories' => false, 'mode' => 'compact'));
  278. $options = array();
  279. $options[HUB_AUDIENCE_EDUCATORS] = get_string('audienceeducators', 'hub');
  280. $options[HUB_AUDIENCE_STUDENTS] = get_string('audiencestudents', 'hub');
  281. $options[HUB_AUDIENCE_ADMINS] = get_string('audienceadmins', 'hub');
  282. $mform->addElement('select', 'audience', get_string('audience', 'hub'), $options);
  283. $mform->setDefault('audience', $defaultaudience);
  284. unset($options);
  285. $mform->addHelpButton('audience', 'audience', 'hub');
  286. $options = array();
  287. $options[HUB_EDULEVEL_PRIMARY] = get_string('edulevelprimary', 'hub');
  288. $options[HUB_EDULEVEL_SECONDARY] = get_string('edulevelsecondary', 'hub');
  289. $options[HUB_EDULEVEL_TERTIARY] = get_string('eduleveltertiary', 'hub');
  290. $options[HUB_EDULEVEL_GOVERNMENT] = get_string('edulevelgovernment', 'hub');
  291. $options[HUB_EDULEVEL_ASSOCIATION] = get_string('edulevelassociation', 'hub');
  292. $options[HUB_EDULEVEL_CORPORATE] = get_string('edulevelcorporate', 'hub');
  293. $options[HUB_EDULEVEL_OTHER] = get_string('edulevelother', 'hub');
  294. $mform->addElement('select', 'educationallevel', get_string('educationallevel', 'hub'), $options);
  295. $mform->setDefault('educationallevel', $defaulteducationallevel);
  296. unset($options);
  297. $mform->addHelpButton('educationallevel', 'educationallevel', 'hub');
  298. $editoroptions = array('maxfiles' => 0, 'maxbytes' => 0, 'trusttext' => false, 'forcehttps' => false);
  299. $mform->addElement('editor', 'creatornotes', get_string('creatornotes', 'hub'), '', $editoroptions);
  300. $mform->addRule('creatornotes', $strrequired, 'required', null, 'client');
  301. $mform->setType('creatornotes', PARAM_CLEANHTML);
  302. $mform->addHelpButton('creatornotes', 'creatornotes', 'hub');
  303. if ($advertise) {
  304. if (!empty($screenshotsnumber)) {
  305. if (!empty($privacy)) {
  306. $baseurl = new moodle_url($huburl . '/local/hub/webservice/download.php',
  307. array('courseid' => $hubcourseid, 'filetype' => HUB_SCREENSHOT_FILE_TYPE));
  308. $screenshothtml = html_writer::empty_tag('img',
  309. array('src' => $baseurl, 'alt' => $defaultfullname));
  310. $screenshothtml = html_writer::tag('div', $screenshothtml,
  311. array('class' => 'coursescreenshot',
  312. 'id' => 'image-' . $hubcourseid));
  313. } else {
  314. $screenshothtml = get_string('existingscreenshotnumber', 'hub', $screenshotsnumber);
  315. }
  316. $mform->addElement('static', 'existingscreenshots', get_string('existingscreenshots', 'hub'), $screenshothtml);
  317. $mform->addHelpButton('existingscreenshots', 'deletescreenshots', 'hub');
  318. $mform->addElement('checkbox', 'deletescreenshots', '', ' ' . get_string('deletescreenshots', 'hub'));
  319. }
  320. $mform->addElement('hidden', 'existingscreenshotnumber', $screenshotsnumber);
  321. $mform->setType('existingscreenshotnumber', PARAM_INT);
  322. }
  323. $mform->addElement('filemanager', 'screenshots', get_string('addscreenshots', 'hub'), null,
  324. array('subdirs' => 0,
  325. 'maxbytes' => 1000000,
  326. 'maxfiles' => 3
  327. ));
  328. $mform->addHelpButton('screenshots', 'screenshots', 'hub');
  329. $this->add_action_buttons(false, $buttonlabel);
  330. //set default value for creatornotes editor
  331. $data = new stdClass();
  332. $data->creatornotes = array();
  333. $data->creatornotes['text'] = $defaultcreatornotes;
  334. $data->creatornotes['format'] = $defaultcreatornotesformat;
  335. $this->set_data($data);
  336. }
  337. function validation($data, $files) {
  338. global $CFG;
  339. $errors = array();
  340. if ($this->_form->_submitValues['subject'] == 'none') {
  341. $errors['subject'] = get_string('mustselectsubject', 'hub');
  342. }
  343. return $errors;
  344. }
  345. }